From c870b4bab6fc835a8ee1869001cdfc12d02dad18 Mon Sep 17 00:00:00 2001 From: thomashii Date: Tue, 23 Nov 2021 18:27:01 +0800 Subject: [PATCH] Updated response header --- packages/framework/CHANGELOG.md | 5 +++++ packages/framework/README.md | 2 +- packages/framework/lib/src/core/driver.dart | 17 ++++++++++++----- .../lib/src/core/response_context.dart | 10 ++++------ .../lib/src/http/http_response_context.dart | 2 ++ packages/framework/pubspec.yaml | 2 +- 6 files changed, 25 insertions(+), 13 deletions(-) diff --git a/packages/framework/CHANGELOG.md b/packages/framework/CHANGELOG.md index 628f1781..33cc0c4d 100644 --- a/packages/framework/CHANGELOG.md +++ b/packages/framework/CHANGELOG.md @@ -1,5 +1,10 @@ # Change Log +## 4.2.2 + +* Added `Date` to response header +* Updated `Server: Angel3` response header + ## 4.2.1 * Updated `package:angel3_container` diff --git a/packages/framework/README.md b/packages/framework/README.md index 03633f00..9163883f 100644 --- a/packages/framework/README.md +++ b/packages/framework/README.md @@ -9,7 +9,7 @@ Angel3 framework is a high-powered HTTP server with support for dependency injection, sophisticated routing, authentication, ORM, graphql etc. It is designed to keep the core minimal but extensible through a series of plugin packages. It won't dictate which features, databases or web templating engine to use. This flexibility enable Angel3 framework to grow with your application as new features can be added to handle the new use cases. -This package is the core package of [Angel 3](https://github.com/dukefirehawk/angel). To see more details, please refer to the [Developer Guide](https://angel3-docs.dukefirehawk.com). +This package is the core package of [Angel3](https://github.com/dukefirehawk/angel). For more information, visit us at [Angel3 Website](https://angel3-framework.web.app). ## Installation and Setup diff --git a/packages/framework/lib/src/core/driver.dart b/packages/framework/lib/src/core/driver.dart index 3239f086..97d0bbdf 100644 --- a/packages/framework/lib/src/core/driver.dart +++ b/packages/framework/lib/src/core/driver.dart @@ -333,7 +333,13 @@ abstract class Driver< return req.close(); } - if (!res.isBuffered) return res.close().then(_cleanup); + if (!res.isBuffered) { + if (res.isOpen) { + return res.close().then(_cleanup); + } + + return Future.value(); + } var finalizers = ignoreFinalizers == true ? Future.value() @@ -346,10 +352,10 @@ abstract class Driver< setHeader(response, key, res.headers[key] ?? ''); } - setContentLength(response, res.buffer!.length); + setContentLength(response, res.buffer?.length ?? 0); setChunkedEncoding(response, res.chunked ?? true); - List outputBuffer = res.buffer!.toBytes(); + var outputBuffer = res.buffer?.toBytes() ?? []; if (res.encoders.isNotEmpty) { var allowedEncodings = req.headers @@ -366,9 +372,9 @@ abstract class Driver< if (allowedEncodings != null) { for (var encodingName in allowedEncodings) { - Converter, List>? encoder; var key = encodingName; + Converter, List>? encoder; if (res.encoders.containsKey(encodingName)) { encoder = res.encoders[encodingName]; } else if (encodingName == '*') { @@ -377,7 +383,8 @@ abstract class Driver< if (encoder != null) { setHeader(response, 'content-encoding', key); - outputBuffer = res.encoders[key]!.convert(outputBuffer); + outputBuffer = + res.encoders[key]?.convert(outputBuffer) ?? []; setContentLength(response, outputBuffer.length); break; } diff --git a/packages/framework/lib/src/core/response_context.dart b/packages/framework/lib/src/core/response_context.dart index 87911023..de41610d 100644 --- a/packages/framework/lib/src/core/response_context.dart +++ b/packages/framework/lib/src/core/response_context.dart @@ -22,10 +22,8 @@ final RegExp _straySlashes = RegExp(r'(^/+)|(/+$)'); abstract class ResponseContext implements StreamConsumer>, StreamSink>, StringSink { final Map properties = {}; - final CaseInsensitiveMap _headers = CaseInsensitiveMap.from({ - 'content-type': 'text/plain', - 'server': 'angel', - }); + final CaseInsensitiveMap _headers = CaseInsensitiveMap.from( + {'content-type': 'text/plain', 'server': 'Angel3'}); final log = Logger('ResponseContext'); @@ -112,14 +110,14 @@ abstract class ResponseContext /// /// Returns `null` if the header is invalidly formatted. int? get contentLength { - return int.tryParse(headers['content-length']!); + return int.tryParse(headers['content-length'] ?? '-1'); } /// Gets or sets the content length to send back to a client. /// /// If [value] is `null`, then the header will be removed. set contentLength(int? value) { - if (value == null) { + if (value == null || value == -1) { headers.remove('content-length'); } else { headers['content-length'] = value.toString(); diff --git a/packages/framework/lib/src/http/http_response_context.dart b/packages/framework/lib/src/http/http_response_context.dart index 4f6602ac..e7195481 100644 --- a/packages/framework/lib/src/http/http_response_context.dart +++ b/packages/framework/lib/src/http/http_response_context.dart @@ -90,6 +90,8 @@ class HttpResponseContext extends ResponseContext { ..cookies.addAll(cookies); headers.forEach(rawResponse.headers.set); + rawResponse.headers.date = DateTime.now(); + if (headers.containsKey('content-length')) { rawResponse.contentLength = int.tryParse(headers['content-length']!) ?? rawResponse.contentLength; diff --git a/packages/framework/pubspec.yaml b/packages/framework/pubspec.yaml index b8af70f5..bfd8739b 100644 --- a/packages/framework/pubspec.yaml +++ b/packages/framework/pubspec.yaml @@ -1,5 +1,5 @@ name: angel3_framework -version: 4.2.1 +version: 4.2.2 description: A high-powered HTTP server extensible framework with dependency injection, routing and much more. homepage: https://angel3-framework.web.app/ repository: https://github.com/dukefirehawk/angel/tree/master/packages/framework