我正在尝试gzip .html文件然后管道它HttpResponse.
import 'dart:io';
void main() {
File f = new File('some_template.html');
HttpServer.bind('localhost', 8080)
.then((HttpServer server) {
server.listen((HttpRequest request) {
HttpResponse response = request.response;
f.openRead()
.transform(GZIP.encoder)
.pipe(response);
});
});
}
Run Code Online (Sandbox Code Playgroud)
没有错误,但浏览器不是提供html页面,而是下载压缩的html页面.小心给我一个提示?
如果客户端接受压缩数据并满足其他一些要求,HttpServer会自动将数据压缩为GZIP(见下文).即使它没有,你也不能只是压缩数据并期望浏览器理解它.浏览器需要纯文本(HTML),并且可能只是将二进制数据下载到磁盘.您还需要设置标头的内容编码.
dart:io除以下情况外,自动压缩数据:
Content-Length设置:Content-Length标题必须是GZIP 之后的长度,dart:io因此不能压缩数据,Accept-Encoding),或Content-Encoding头已经由开发人员设置.Dart的http实现的一些相关部分:
// _writeHeaders (http_impl.dart):
if (acceptEncodings != null &&
acceptEncodings
.expand((list) => list.split(","))
.any((encoding) => encoding.trim().toLowerCase() == "gzip") &&
contentEncoding == null) {
headers.set(HttpHeaders.CONTENT_ENCODING, "gzip");
_asGZip = true;
}
// _addStream (same file):
if (_asGZip) {
stream = stream.transform(GZIP.encoder);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
834 次 |
| 最近记录: |