http


Author: yifei / Created: May 29, 2017, 11:05 p.m. / Modified: May 29, 2017, 11:07 p.m. / Edit

Connection

Connection: keep-alive is for http/1.0+, it's deprecated but widely used for compatiblitiy

by defautl, in http/1.1, connection is persistent

Connection: close closes a connection explicitly.

For modern browsers, they will reuse connections to a server, which means all requests to a server is connected through a single or a few connections.

Content-Encoding

Browser sends Accept-Encoding header to tell the server which encoding it can understand. Server responses with Content-Encoding it choose from the Accept-Encoding.

The Accept-Encoding can be a list of methods with q saying their favorite rate.

The legal methods are:

1. indentity
2. gzip
3. compress
4. deflate
5. br

<- Accept-Encoding: gzip; q=0.9, deflate; q=0.8 -> Content-Encoding: gzip

Browsers and the requests lib will do this automatically, but not urllib2

see also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding

Transfer-Endcoding

transfer encoding is used to streaming http response, Otherwise, the server has to generate the whole response, and then serve it.

-> Transfer-Encoding: chunked

5\r\n
12345\r\n
a\r\n
0123456789\r\n
<number>\r\n
0xnumber bytes\r\n
0\r\n
\r\n

Note, the length of bytes is in hex number format. the last chunk has to be 0, which means the end of chunks.

if the server does not use 0\r\n\r\n to end the transmission. requests will raise an Exception[2]

See also:

[1] https://imququ.com/post/transfer-encoding-header-in-http.html [2] http://blog.csdn.net/wangzuxi/article/details/40377467


评论区