Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart-如何提供gzip编码的html页面_Dart - Fatal编程技术网

Dart-如何提供gzip编码的html页面

Dart-如何提供gzip编码的html页面,dart,Dart,我正在尝试gzip一个.html文件,然后通过管道将其传输到HttpResponse 导入'dart:io'; void main(){ 文件f=新文件('some_template.html'); HttpServer.bind('localhost',8080) .然后((HttpServer服务器){ 侦听((HttpRequest请求){ HttpResponse response=request.response; f、 openRead() .transform(GZIP.encod

我正在尝试gzip一个.html文件,然后通过管道将其传输到
HttpResponse

导入'dart:io';
void main(){
文件f=新文件('some_template.html');
HttpServer.bind('localhost',8080)
.然后((HttpServer服务器){
侦听((HttpRequest请求){
HttpResponse response=request.response;
f、 openRead()
.transform(GZIP.encoder)
.管道(响应);
});
});
}

没有错误,但浏览器不提供html页面,而是下载压缩的html页面。能给我一个提示吗?

如果客户端接受压缩后的数据,并且满足了其他一些要求,HttpServer会自动将数据压缩为GZIP(见下文)。即使没有,你也不能仅仅压缩数据并期望浏览器理解它。浏览器需要纯文本(HTML),可能只是将二进制数据下载到磁盘上。您还需要设置标题的内容编码

dart:io
自动压缩数据,以下情况除外:

  • 设置了
    Content-Length
    Content-Length
    头必须是GZIP之后的长度,因此
    dart:io
    无法压缩数据
  • 客户端不接受它(以
    接受编码
    发送),或
  • 内容编码
    标题已由开发人员设置
Dart http实现的一些相关部分:

//\u写入头(http\u impl.dart):
if(acceptEncodings!=null&&
接受编码
.expand((list)=>list.split(“,”)
.any((编码)=>encoding.trim().toLowerCase()==“gzip”)&&
contentEncoding==null){
headers.set(HttpHeaders.CONTENT_编码,“gzip”);
_asGZip=true;
}
//\u addStream(同一文件):
如果(_asGZip){
stream=stream.transform(GZIP.encoder);
}

在最新的SDK中,您必须启用和

例如

import "dart:io";

void main() {
  HttpServer
  .bind(InternetAddress.ANY_IP_V4, 8080)
  .then((server) {
    server.autoCompress = true;
    server.listen((HttpRequest request) {
      request.response.write('Hello, world!');
      request.response.close();
    });
  });
}
以下是响应标题:

content-encoding:gzip
content-type:text/plain; charset=utf-8
transfer-encoding:chunked
x-content-type-options:nosniff
x-frame-options:SAMEORIGIN
x-xss-protection:1; mode=block
请注意,如果chunckedTransferEncoding为HTTP/1.1,则会自动设置它


如上所述,如果设置
内容长度
标题,则无法使用自动压缩。如果要设置
内容长度
标题,则必须是压缩后的内容长度

下面的代码说明了如何使用压缩数据的内容长度为已经压缩的资源提供服务

导入'dart:convert';
导入“dart:io”;
变量页=
r''
你好,世界!!!
你好,世界!!!
''';
void requestHandler(HttpRequest请求){
List compressed=GZIP.encode(UTF8.encode(第页));
请求.响应
..headers.contentType=contentType.HTML
..headers.set(HttpHeaders.CONTENT_编码'gzip')
..headers.contentLength=压缩的.length
…添加(压缩)
…关闭();
}
main()异步{
var server=wait HttpServer.bind(InternetAddress.ANY_IP_V4,8080);
server.autoCompress=true;
server.defaultResponseHeaders.chunkedTransferEncoding=true;
listen(requestHandler);
}

是的,但具体是如何做到的
server.autoCompress=true;server.defaultResponseHeaders.chunkedTransferEncoding=true
使我的
HttpRequest
调用报告
ERR\u CONTENT\u DECODING\u FAILED
。从技术上讲,您不必设置chunkedTransferEncoding,因为它是在检测到HTTP 1.1时自动设置的。您可以直接查看Dart SDK的源代码。已删除该源代码,消除了该错误,但根据“网络选项卡”和“页面洞察”报告,它似乎没有被压缩。在1.9.3+中确认了这一点?是的,这就是我们在中所做的。我在上面添加了一个简单的示例。希望能有所帮助。正如瑟伦在回答中提到的,我的问题是代码在其他地方设置了内容长度。