handle method

Future<void> handle(
  1. HttpRequest request
)

Handles an incoming HttpRequest by running global middleware, then dispatching to the router.

This is the main entry point for HTTP request processing.

HttpServer.bind('0.0.0.0', 8080).then((server) {
  server.listen(kernel.handle);
});

Implementation

Future<void> handle(HttpRequest request) async {
  // Buffer the request body to prevent "stream has already been listened to" errors.
  // This allows multiple components (CSRF, Logging, Controllers) to read the body.
  // Only buffer body if content exists to prevent stream issues
  // For GET/HEAD requests with no body, we skip buffering to improve performance
  if (request.contentLength > 0) {
    try {
      await request.form().buffer();
    } catch (e) {
      App().archeryLogger.error("Error buffering request body", {
        "origin": "Kernel.handle()",
        "error": e.toString(),
      });
    }
  }

  await _runMiddleware(request, 0);
}