2016-09-15 19:53:01 +00:00
|
|
|
library angel_framework.http.request_context;
|
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:body_parser/body_parser.dart';
|
|
|
|
import '../../src/extensible.dart';
|
|
|
|
import 'angel_base.dart';
|
|
|
|
import 'route.dart';
|
2016-04-18 03:27:23 +00:00
|
|
|
|
|
|
|
/// A convenience wrapper around an incoming HTTP request.
|
2016-04-21 20:37:02 +00:00
|
|
|
class RequestContext extends Extensible {
|
2016-04-18 03:27:23 +00:00
|
|
|
/// The [Angel] instance that is responding to this request.
|
2016-09-15 19:53:01 +00:00
|
|
|
AngelBase app;
|
2016-04-18 03:27:23 +00:00
|
|
|
|
|
|
|
/// Any cookies sent with this request.
|
|
|
|
List<Cookie> get cookies => underlyingRequest.cookies;
|
|
|
|
|
|
|
|
/// All HTTP headers sent with this request.
|
|
|
|
HttpHeaders get headers => underlyingRequest.headers;
|
|
|
|
|
|
|
|
/// The requested hostname.
|
|
|
|
String get hostname => underlyingRequest.headers.value(HttpHeaders.HOST);
|
|
|
|
|
|
|
|
/// The user's IP.
|
|
|
|
String get ip => remoteAddress.address;
|
|
|
|
|
|
|
|
/// This request's HTTP method.
|
|
|
|
String get method => underlyingRequest.method;
|
|
|
|
|
|
|
|
/// All post data submitted to the server.
|
|
|
|
Map body = {};
|
|
|
|
|
|
|
|
/// The content type of an incoming request.
|
|
|
|
ContentType contentType;
|
|
|
|
|
|
|
|
/// Any and all files sent to the server with this request.
|
|
|
|
List<FileUploadInfo> files = [];
|
|
|
|
|
|
|
|
/// The URL parameters extracted from the request URI.
|
|
|
|
Map params = {};
|
|
|
|
|
|
|
|
/// The requested path.
|
|
|
|
String path;
|
|
|
|
|
|
|
|
/// The parsed request query string.
|
|
|
|
Map query = {};
|
|
|
|
|
|
|
|
/// The remote address requesting this resource.
|
|
|
|
InternetAddress remoteAddress;
|
|
|
|
|
|
|
|
/// The route that matched this request.
|
|
|
|
Route route;
|
|
|
|
|
|
|
|
/// The user's HTTP session.
|
|
|
|
HttpSession session;
|
|
|
|
|
|
|
|
/// Is this an **XMLHttpRequest**?
|
|
|
|
bool get xhr => underlyingRequest.headers.value("X-Requested-With")
|
|
|
|
?.trim()
|
|
|
|
?.toLowerCase() == 'xmlhttprequest';
|
|
|
|
|
|
|
|
/// The underlying [HttpRequest] instance underneath this context.
|
|
|
|
HttpRequest underlyingRequest;
|
|
|
|
|
|
|
|
/// Magically transforms an [HttpRequest] into a RequestContext.
|
|
|
|
static Future<RequestContext> from(HttpRequest request,
|
2016-09-15 19:53:01 +00:00
|
|
|
Map parameters, AngelBase app, Route sourceRoute) async {
|
2016-04-18 03:27:23 +00:00
|
|
|
RequestContext context = new RequestContext();
|
|
|
|
|
|
|
|
context.app = app;
|
|
|
|
context.contentType = request.headers.contentType;
|
|
|
|
context.remoteAddress = request.connectionInfo.remoteAddress;
|
|
|
|
context.params = parameters;
|
2016-06-23 19:05:55 +00:00
|
|
|
context.path = request.uri.toString().replaceAll("?" + request.uri.query, "").replaceAll(new RegExp(r'\/+$'), '');
|
2016-04-18 03:27:23 +00:00
|
|
|
context.route = sourceRoute;
|
|
|
|
context.session = request.session;
|
|
|
|
context.underlyingRequest = request;
|
|
|
|
|
|
|
|
BodyParseResult bodyParseResult = await parseBody(request);
|
|
|
|
context.query = bodyParseResult.query;
|
|
|
|
context.body = bodyParseResult.body;
|
|
|
|
context.files = bodyParseResult.files;
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
}
|