HTTP - how to send multiple pre-cached gzipped chunks?

↘锁芯ラ 提交于 2019-12-18 06:49:07

问题


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 :/


回答1:


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.



来源:https://stackoverflow.com/questions/16740034/http-how-to-send-multiple-pre-cached-gzipped-chunks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!