From 5e7e62fee2d62557a461ef54cb1373bd98428009 Mon Sep 17 00:00:00 2001 From: denkuy Date: Sat, 3 Nov 2018 02:10:47 +0100 Subject: [PATCH] Handle content-encoding and transfer-encoding headers http.Client decodes responses, so we can drop those headers when responding Additionally we will reply with header 'content-length: -1' when http.Client decoded a message --- lib/src/proxy_layer.dart | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/src/proxy_layer.dart b/lib/src/proxy_layer.dart index dbd04dcb..30acd106 100644 --- a/lib/src/proxy_layer.dart +++ b/lib/src/proxy_layer.dart @@ -138,19 +138,24 @@ class Proxy { mediaType = _fallbackMediaType; } - var proxiedHeaders = new Map.from(rs.headers); + /// if [http.Client] does not provide us with a content length + /// OR [http.Client] is about to decode the response (bytecount returned by [http.Response].stream != known length) + /// then we can not provide a value downstream => set to '-1' for 'unspecified length' + var isContentLengthUnknown = rs.contentLength == null || + rs.headers[HttpHeaders.contentEncodingHeader]?.isNotEmpty == true || + rs.headers[HttpHeaders.transferEncodingHeader]?.isNotEmpty == true; + + var proxiedHeaders = new Map.from(rs.headers) + ..remove(HttpHeaders.contentEncodingHeader) // drop, http.Client has decoded + ..remove(HttpHeaders.transferEncodingHeader) // drop, http.Client has decoded + ..[HttpHeaders.contentLengthHeader] = "${isContentLengthUnknown ? '-1' : rs.contentLength}"; res ..contentType = mediaType ..statusCode = rs.statusCode ..headers.addAll(proxiedHeaders); - var stream = rs.stream; - - // [upgrading to dart2] Keeping this workaround as a reference. It's not properly typed for dart2 - //if (rs.headers[HttpHeaders.contentEncodingHeader] == 'gzip') stream = stream.transform(gzip.encoder); - - await stream.pipe(res); + await rs.stream.pipe(res); return false; }