textOld method

Future<HttpResponse> textOld([
  1. dynamic data
])

Implementation

Future<HttpResponse> textOld([dynamic data]) async {
  // 1. Set static headers in bulk (if framework allows) or quickly
  staticSecurityHeaders.forEach(response.headers.set);
  response.headers.contentType = ContentType.text;
  response.headers.contentLength = utf8.encode(data).length;
  response.headers.set(HttpHeaders.connectionHeader, 'keep-alive');

  // 2. Logic-gate the expensive key generation
  var csrfCookie = cookies.firstWhereOrNull((c) => c.name == 'archery_csrf_token');

  if (csrfCookie == null) {
    final newCookie = Cookie('archery_csrf_token', App.generateKey()) // Expensive call
      ..httpOnly = true
      ..secure = true
      ..sameSite = SameSite.lax
      ..path = '/';
    response.cookies.add(newCookie);
  }

  return response
    ..statusCode = HttpStatus.ok
    ..write(data)
    ..close();
}