Transfer from http://www.cnblogs.com/TankXiao/archive/2012/11/13/2749055.html
Previously wrote an "HTTP protocol explanation", this time continue to introduce the HTTP protocol compression.
This article uses Fiddler to view HTTP request and response, and if you are unfamiliar with this tool, you can refer to the [Fiddler Tutorial] first.
HTTP compression refers to the method of compressing the "text content" of a transmission between a Web server and a browser. HTTP uses a common compression algorithm, such as gzip to compress html,javascript, CSS files. Can greatly reduce the amount of data transmitted by the network, improve the speed of users to display Web pages. Of course, it also adds a bit of overhead to the server. This article is to understand the concept of HTTP compression from the point of view of HTTP protocol.
Read Catalogue
- The difference between HTTP content encoding and HTTP compression
- The process of HTTP compression
- Example: Observing HTTP compression with fiddler
- Content encoding Type
- Advantages of compression
- The disadvantages of gzip
- How gzip is Compressed
- HTTP response can be compressed, HTTP request can also be compressed
The difference between HTTP content encoding and HTTP compression
HTTP compression, in the HTTP protocol, is actually a kind of content encoding.
In the HTTP protocol, the content (that is, the body part) can be encoded, which can be encoded using gzip. So as to achieve the purpose of compression. You can also use other encodings to scramble or encrypt content to prevent unauthorized third parties from seeing the contents of the document.
So we say that HTTP compression, in fact, is a kind of HTTP content encoding. So let's not confuse the two concepts of HTTP compression and HTTP content encoding.
The process of HTTP compression
1. The browser sends an HTTP request to the Web server with Accept-encoding:gzip, deflate in request. (Tell the server that the browser supports gzip compression)
2. After the Web server receives the request, it generates the original response, which has the original Content-type and Content-length.
3. The Web server uses gzip to encode the response, with Content-type and content-length (compressed size) in the header, and Content-encoding:gzip added. Then send the response to the browser.
4. After the browser receives the response, the response is decoded according to the Content-encoding:gzip. After you get to the original response, the page is then displayed.
Such as:
Example: Fiddler observing HTTP Compression
Seeing is believing, let's look at a practical example, and I found that the blog park was using gzip compression.
The use of Fiddler can be clearly seen.
In Fiddler, you have to manually go to decode each time. Too troublesome. Click on the "Decode" button on the toolbar to automatically Decode.
Content encoding Type
HTTP defines a number of standard content encoding types, and allows for the addition of more encodings in the form of extensions.
The content-encoding header uses these standardized codes to illustrate the algorithm used in encoding
Content-encoding value
Gzip indicates that the entity uses the GNU ZIP code
Compress indicates that the entity uses UNIX's file compression program
Deflate indicates that the entity is compressed in zlib format.
The identity indicates that the entity is not encoded. When there is no content-encoding header, it is implied that the situation
gzip, compress, and deflate codes are lossless compression algorithms that reduce the size of transmitted messages without causing loss of information. Gzip is usually the most efficient and most widely used.
Advantages of compression
HTTP compression can compress plain text to 40% of the original content, saving 60% of data transfer.
Example: Blog home before compression is: 46124 bytes. After compression is: 16368bytes. Only the original 35%. 65% savings in data transfer, which greatly improves performance
There is a picture for proof.
The disadvantages of gzip
JPEG files with gzip compression are not good enough.
How gzip is Compressed
In short, gzip compression is to find similar strings in a text file and temporarily replace them to make the entire file smaller. This form of compression works well for the web because HTML and CSS files often contain a large number of duplicate strings, such as spaces, tags.
HTTP response can be compressed, HTTP request can also be compressed
The browser is not compressed for request. However, some HTTP programs encode the request when it is sent. Such as.
HTTP protocol (c) compression