jhonny-me/GzipMiddleware

wrap up all infomation needed.

Closed this issue · 5 comments

  • gzip protocol
  • c library for gzip
  • how nodejs do the compress
  • how to run c in swift
  • serve files in vapor
  • write a vapor middleware
  • server and client configure to enable gzip

For gzip:

its a format more than a protocol. It contains some depressed data and file info. And you can use gzip command line to compress file or gunzip to decompress file.

For zlib:

its a pure compress algorithm that boost gzip

how does server and client support gzip:

HTTP压缩的过程
  1. 浏览器发送Http request 给Web服务器,  request 中有Accept-Encoding: gzip, deflate。 (告诉服务器, 浏览器支持gzip压缩)
  2. Web服务器接到request后, 生成原始的Response, 其中有原始的Content-Type和Content-Length。
  3. Web服务器通过Gzip,来对Response进行编码, 编码后header中有Content-Type和Content-Length(压缩后的大小), 并且增加了Content-Encoding:gzip.  然后把Response发送给浏览器。
  4. 浏览器接到Response后,根据Content-Encoding:gzip来对Response 进行解码。 获取到原始response后, 然后显示出网页。

link: http://blog.csdn.net/sun_p0/article/details/50341761

how does vapor serve file:

vapor is using a FileManager to read file into nsdata.

then use a FileMiddleware to put all data into response's body, and make a content-type field to the file's extension.

how to run c in swift:
we can use bridging header, then import bridging header.
Or we can use modulemap: https://github.com/1024jp/GzipSwift/blob/develop/zlib/module.modulemap

and there are lots of unsafe pointer in C.


How nodejs do the compress:
https://nodejs.org/api/zlib.html

Vapor middleware: https://vapor.github.io/documentation/guide/middleware.html#errors

Maybe it is good a idea to use middleware in mobile response parse.

Note:

  • using zlib can costs lots of performence, so we need to cache the result.