2016-09-15 19:53:01 +00:00
|
|
|
library angel_framework.http.request_context;
|
2016-10-22 20:41:36 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
2016-10-22 20:41:36 +00:00
|
|
|
import 'package:angel_route/src/extensible.dart';
|
2016-09-15 19:53:01 +00:00
|
|
|
import 'package:body_parser/body_parser.dart';
|
|
|
|
import 'angel_base.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-10-22 20:41:36 +00:00
|
|
|
BodyParseResult _body;
|
|
|
|
ContentType _contentType;
|
|
|
|
String _path;
|
|
|
|
HttpRequest _underlyingRequest;
|
|
|
|
|
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.
|
2016-10-22 20:41:36 +00:00
|
|
|
Map get body => _body.body;
|
2016-04-18 03:27:23 +00:00
|
|
|
|
|
|
|
/// The content type of an incoming request.
|
2016-10-22 20:41:36 +00:00
|
|
|
ContentType get contentType => _contentType;
|
2016-04-18 03:27:23 +00:00
|
|
|
|
|
|
|
/// Any and all files sent to the server with this request.
|
2016-10-22 20:41:36 +00:00
|
|
|
List<FileUploadInfo> get files => _body.files;
|
2016-04-18 03:27:23 +00:00
|
|
|
|
|
|
|
/// The URL parameters extracted from the request URI.
|
|
|
|
Map params = {};
|
|
|
|
|
|
|
|
/// The requested path.
|
2016-10-22 20:41:36 +00:00
|
|
|
String get path => _path;
|
2016-04-18 03:27:23 +00:00
|
|
|
|
|
|
|
/// The parsed request query string.
|
2016-10-22 20:41:36 +00:00
|
|
|
Map get query => _body.query;
|
2016-04-18 03:27:23 +00:00
|
|
|
|
|
|
|
/// The remote address requesting this resource.
|
2016-10-22 20:41:36 +00:00
|
|
|
InternetAddress get remoteAddress =>
|
|
|
|
underlyingRequest.connectionInfo.remoteAddress;
|
2016-04-18 03:27:23 +00:00
|
|
|
|
|
|
|
/// The user's HTTP session.
|
2016-10-22 20:41:36 +00:00
|
|
|
HttpSession get session => underlyingRequest.session;
|
|
|
|
|
|
|
|
/// The [Uri] instance representing the path this request is responding to.
|
|
|
|
Uri get uri => underlyingRequest.uri;
|
2016-04-18 03:27:23 +00:00
|
|
|
|
|
|
|
/// Is this an **XMLHttpRequest**?
|
2016-10-22 20:41:36 +00:00
|
|
|
bool get xhr =>
|
|
|
|
underlyingRequest.headers
|
|
|
|
.value("X-Requested-With")
|
|
|
|
?.trim()
|
|
|
|
?.toLowerCase() ==
|
|
|
|
'xmlhttprequest';
|
2016-04-18 03:27:23 +00:00
|
|
|
|
|
|
|
/// The underlying [HttpRequest] instance underneath this context.
|
2016-10-22 20:41:36 +00:00
|
|
|
HttpRequest get underlyingRequest => _underlyingRequest;
|
|
|
|
|
|
|
|
/// Magically transforms an [HttpRequest] into a [RequestContext].
|
|
|
|
static Future<RequestContext> from(HttpRequest request, AngelBase app) async {
|
|
|
|
RequestContext ctx = new RequestContext();
|
|
|
|
|
|
|
|
ctx.app = app;
|
|
|
|
ctx._contentType = request.headers.contentType;
|
|
|
|
ctx._path = request.uri
|
|
|
|
.toString()
|
|
|
|
.replaceAll("?" + request.uri.query, "")
|
|
|
|
.replaceAll(new RegExp(r'/+$'), '');
|
|
|
|
ctx._underlyingRequest = request;
|
|
|
|
|
|
|
|
ctx._body = await parseBody(request);
|
|
|
|
|
|
|
|
return ctx;
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
2016-10-22 20:41:36 +00:00
|
|
|
}
|