From 0057f1166167fc89dae80863e26636e62dbba13c Mon Sep 17 00:00:00 2001 From: thosakwe Date: Tue, 20 Dec 2016 22:10:03 -0500 Subject: [PATCH] 35 --- README.md | 2 +- lib/src/http/request_context.dart | 10 +++------- lib/src/http/response_context.dart | 24 ++++++++++++++++++++++++ pubspec.yaml | 2 +- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0d0e917a..7023539d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # angel_framework -[![pub 1.0.0-dev.34](https://img.shields.io/badge/pub-1.0.0--dev.34-red.svg)](https://pub.dartlang.org/packages/angel_framework) +[![pub 1.0.0-dev.35](https://img.shields.io/badge/pub-1.0.0--dev.35-red.svg)](https://pub.dartlang.org/packages/angel_framework) [![build status](https://travis-ci.org/angel-dart/framework.svg)](https://travis-ci.org/angel-dart/framework) Core libraries for the Angel Framework. \ No newline at end of file diff --git a/lib/src/http/request_context.dart b/lib/src/http/request_context.dart index a759d101..52cad7dd 100644 --- a/lib/src/http/request_context.dart +++ b/lib/src/http/request_context.dart @@ -56,8 +56,7 @@ class RequestContext extends Extensible { Map get query => _body.query; /// The remote address requesting this resource. - InternetAddress get remoteAddress => - io.connectionInfo.remoteAddress; + InternetAddress get remoteAddress => io.connectionInfo.remoteAddress; /// The user's HTTP session. HttpSession get session => io.session; @@ -67,10 +66,7 @@ class RequestContext extends Extensible { /// Is this an **XMLHttpRequest**? bool get xhr => - io.headers - .value("X-Requested-With") - ?.trim() - ?.toLowerCase() == + io.headers.value("X-Requested-With")?.trim()?.toLowerCase() == 'xmlhttprequest'; @deprecated @@ -91,7 +87,7 @@ class RequestContext extends Extensible { .replaceAll(new RegExp(r'/+$'), ''); ctx._io = request; - ctx._body = await parseBody(request); + ctx._body = (await parseBody(request)) ?? {}; return ctx; } diff --git a/lib/src/http/response_context.dart b/lib/src/http/response_context.dart index 50488ace..41db6b77 100644 --- a/lib/src/http/response_context.dart +++ b/lib/src/http/response_context.dart @@ -10,6 +10,9 @@ import '../extensible.dart'; import 'angel_base.dart'; import 'controller.dart'; +final RegExp _contentType = + new RegExp(r'([^/\n]+)\/\s*([^;\n]+)\s*(;\s*charset=([^$;\n]+))?'); + final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)'); /// A convenience wrapper around an outgoing HTTP request. @@ -52,6 +55,27 @@ class ResponseContext extends Extensible { '`ResponseContext#underlyingResponse` is deprecated. Please update your application to use the newer `ResponseContext#io`.'); } + /// Gets the Content-Type header. + ContentType get contentType { + if (!headers.containsKey(HttpHeaders.CONTENT_TYPE)) return null; + + var header = headers[HttpHeaders.CONTENT_TYPE]; + var match = _contentType.firstMatch(header); + + if (match == null) + throw new Exception('Malformed Content-Type response header: "$header".'); + + if (match[4]?.isNotEmpty != true) + return new ContentType(match[1], match[2]); + else + return new ContentType(match[1], match[2], charset: match[4]); + } + + /// Sets the Content-Type header. + void set contentType(ContentType contentType) { + headers[HttpHeaders.CONTENT_TYPE] = contentType.toString(); + } + ResponseContext(this.io, this.app); /// Set this to true if you will manually close the response. diff --git a/pubspec.yaml b/pubspec.yaml index b89bd4ac..f71fcdd6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_framework -version: 1.0.0-dev.34 +version: 1.0.0-dev.35 description: Core libraries for the Angel framework. author: Tobe O homepage: https://github.com/angel-dart/angel_framework