HTTP - 如何发送多个预缓存的gzip块? [英] HTTP - how to send multiple pre-cached gzipped chunks?

查看:124
本文介绍了HTTP - 如何发送多个预缓存的gzip块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我在内存中有2个单独的gziped html块。
我可以将chunk1 + chunk2发送给HTTP客户端吗?任何浏览器是否支持这个?
或者没有办法做到这一点,我必须gzip整个流而不是个别块?



我想为客户端提供例如chunk1 + chunk2和chunk2 + chunk1等(不同的顺序),但我不想每次压缩整个页面,我不想缓存整个页面。我想使用预压缩的缓存块并发送它们。

nodejs代码(节点v0.10.7):

  //创建预缓存的数据缓冲区
var zlib = require('zlib');
var chunk1,chunk2;
zlib.gzip(new Buffer('test1'),function(err,data){
chunk1 = data;
});
zlib.gzip(new Buffer('test2'),function(err,data){
chunk2 = data;
});


var http = require('http');
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text / plain','Content-Encoding':'gzip'});
//写两个预置缓冲区
res.write(chunk1); //如果我只发送这一个,一切都正常
res.write(chunk2); //如果我发送两个chunks Chrome尝试下载文件
res.end();
})。listen(8080);

当我的示例服务器返回此类响应时,Chrome浏览器显示下载窗口(它不理解它:/

解决方案

我还没有尝试过,但如果http客户端符合 RFC 1952 ,那么它们应该接受级联的gzip流,并将它们解压缩,就像数据全部压缩成一个流一样。 RFC 2616 中的1.1标准实际上是指RFC 1952。



如果用chunks表示分块传输编码,那么它与压缩无关。如果客户端确实接受连接的流,那么没有理由使分块传输编码的边界必须对齐与gzip流内。



至于如何做到这一点,只需gzip你的作品,并直接连接它们。 o需要其他格式或准备。


Lets say I have 2 individually gziped html chunks in memory. Can I send chunk1+chunk2 to HTTP client? Does any browser supports this? Or there is no way to do this and I have to gzip the whole stream not individual chunks?

I want to serve to clients for example chunk1+chunk2 and chunk2+chunk1 etc (different order) but I don't want to compress the whole page every time and I dont want to cache the whole page. I want to use precompressed cached chunks and send them.

nodejs code (node v0.10.7):

// creating pre cached data buffers
var zlib = require('zlib');
var chunk1, chunk2;
zlib.gzip(new Buffer('test1'), function(err, data){
  chunk1 = data;
});
zlib.gzip(new Buffer('test2'), function(err, data){
  chunk2 = data;
});


var http = require('http');
http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain', 'Content-Encoding': 'gzip'});
      // writing two pre gziped buffers
      res.write(chunk1); // if I send only this one everything is OK
      res.write(chunk2); // if I send two chunks Chrome trying to download file
      res.end();
}).listen(8080);

When my example server returns this kind of response Chrome browser display download window (it doesnt understand it :/

解决方案

I haven't tried it, but if the http clients are compliant with RFC 1952, then they should accept concatenated gzip streams, and decompress them with the same result as if the data were all compressed into one stream. The HTTP 1.1 standard in RFC 2616 does in fact refer to RFC 1952.

If by "chunks" you are referring to chunked transfer encoding, then that is independent of the compression. If the clients do accept concatenated streams, then there is no reason for chunked transfer encoded boundaries to have to align with the gzip streams within.

As to how to do it, simply gzip your pieces and directly concatenate them. No other formatting or preparation is required.

这篇关于HTTP - 如何发送多个预缓存的gzip块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆