Updated response header

This commit is contained in:
thomashii 2021-11-23 18:27:01 +08:00
parent d0fc25c4e1
commit c870b4bab6
6 changed files with 25 additions and 13 deletions

View file

@ -1,5 +1,10 @@
# Change Log # Change Log
## 4.2.2
* Added `Date` to response header
* Updated `Server: Angel3` response header
## 4.2.1 ## 4.2.1
* Updated `package:angel3_container` * Updated `package:angel3_container`

View file

@ -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. 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 ## Installation and Setup

View file

@ -333,7 +333,13 @@ abstract class Driver<
return req.close(); 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 var finalizers = ignoreFinalizers == true
? Future.value() ? Future.value()
@ -346,10 +352,10 @@ abstract class Driver<
setHeader(response, key, res.headers[key] ?? ''); setHeader(response, key, res.headers[key] ?? '');
} }
setContentLength(response, res.buffer!.length); setContentLength(response, res.buffer?.length ?? 0);
setChunkedEncoding(response, res.chunked ?? true); setChunkedEncoding(response, res.chunked ?? true);
List<int> outputBuffer = res.buffer!.toBytes(); var outputBuffer = res.buffer?.toBytes() ?? <int>[];
if (res.encoders.isNotEmpty) { if (res.encoders.isNotEmpty) {
var allowedEncodings = req.headers var allowedEncodings = req.headers
@ -366,9 +372,9 @@ abstract class Driver<
if (allowedEncodings != null) { if (allowedEncodings != null) {
for (var encodingName in allowedEncodings) { for (var encodingName in allowedEncodings) {
Converter<List<int>, List<int>>? encoder;
var key = encodingName; var key = encodingName;
Converter<List<int>, List<int>>? encoder;
if (res.encoders.containsKey(encodingName)) { if (res.encoders.containsKey(encodingName)) {
encoder = res.encoders[encodingName]; encoder = res.encoders[encodingName];
} else if (encodingName == '*') { } else if (encodingName == '*') {
@ -377,7 +383,8 @@ abstract class Driver<
if (encoder != null) { if (encoder != null) {
setHeader(response, 'content-encoding', key); setHeader(response, 'content-encoding', key);
outputBuffer = res.encoders[key]!.convert(outputBuffer); outputBuffer =
res.encoders[key]?.convert(outputBuffer) ?? <int>[];
setContentLength(response, outputBuffer.length); setContentLength(response, outputBuffer.length);
break; break;
} }

View file

@ -22,10 +22,8 @@ final RegExp _straySlashes = RegExp(r'(^/+)|(/+$)');
abstract class ResponseContext<RawResponse> abstract class ResponseContext<RawResponse>
implements StreamConsumer<List<int>>, StreamSink<List<int>>, StringSink { implements StreamConsumer<List<int>>, StreamSink<List<int>>, StringSink {
final Map properties = {}; final Map properties = {};
final CaseInsensitiveMap<String> _headers = CaseInsensitiveMap<String>.from({ final CaseInsensitiveMap<String> _headers = CaseInsensitiveMap<String>.from(
'content-type': 'text/plain', {'content-type': 'text/plain', 'server': 'Angel3'});
'server': 'angel',
});
final log = Logger('ResponseContext'); final log = Logger('ResponseContext');
@ -112,14 +110,14 @@ abstract class ResponseContext<RawResponse>
/// ///
/// Returns `null` if the header is invalidly formatted. /// Returns `null` if the header is invalidly formatted.
int? get contentLength { 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. /// Gets or sets the content length to send back to a client.
/// ///
/// If [value] is `null`, then the header will be removed. /// If [value] is `null`, then the header will be removed.
set contentLength(int? value) { set contentLength(int? value) {
if (value == null) { if (value == null || value == -1) {
headers.remove('content-length'); headers.remove('content-length');
} else { } else {
headers['content-length'] = value.toString(); headers['content-length'] = value.toString();

View file

@ -90,6 +90,8 @@ class HttpResponseContext extends ResponseContext<HttpResponse> {
..cookies.addAll(cookies); ..cookies.addAll(cookies);
headers.forEach(rawResponse.headers.set); headers.forEach(rawResponse.headers.set);
rawResponse.headers.date = DateTime.now();
if (headers.containsKey('content-length')) { if (headers.containsKey('content-length')) {
rawResponse.contentLength = int.tryParse(headers['content-length']!) ?? rawResponse.contentLength = int.tryParse(headers['content-length']!) ??
rawResponse.contentLength; rawResponse.contentLength;

View file

@ -1,5 +1,5 @@
name: angel3_framework 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. description: A high-powered HTTP server extensible framework with dependency injection, routing and much more.
homepage: https://angel3-framework.web.app/ homepage: https://angel3-framework.web.app/
repository: https://github.com/dukefirehawk/angel/tree/master/packages/framework repository: https://github.com/dukefirehawk/angel/tree/master/packages/framework