from static method

Future<HttpRequestContext> from(
  1. HttpRequest request,
  2. Angel app,
  3. String path
)

Magically transforms an HttpRequest into a RequestContext.

Implementation

static Future<HttpRequestContext> from(
    HttpRequest request, Angel app, String path) {
  var ctx = HttpRequestContext().._container = app.container!.createChild();

  var override = request.method;

  if (app.allowMethodOverrides == true) {
    override =
        request.headers.value('x-http-method-override')?.toUpperCase() ??
            request.method;
  }

  ctx.app = app;
  ctx._contentType = request.headers.contentType == null
      ? MediaType('text', 'plain')
      : MediaType.parse(request.headers.contentType.toString());
  ctx._override = override;

  /*
  // Faster way to get path
  List<int> _path = [];

  // Go up until we reach a ?
  for (int ch in request.uri.toString().codeUnits) {
    if (ch != $question)
      _path.add(ch);
    else
      break;
  }

  // Remove trailing slashes
  int lastSlash = -1;

  for (int i = _path.length - 1; i >= 0; i--) {
    if (_path[i] == $slash)
      lastSlash = i;
    else
      break;
  }

  if (lastSlash > -1)
    ctx._path = String.fromCharCodes(_path.take(lastSlash));
  else
    ctx._path = String.fromCharCodes(_path);
    */

  ctx._path = path;
  ctx._io = request;

  return Future.value(ctx);
}