最後更新: 2022-12-21
目錄
Chunked Encode
Code
def wmsg(tn, msg): tn.write(msg + '\r\n') tn = connect(server, port) wmsg(tn, 'POST /test.php HTTP/1.1') wmsg(tn, 'Host: ' + server) wmsg(tn, 'Transfer-Encoding: chunked') wmsg(tn, 'Content-Type: text/plain') # 此 encode 是沒有 'Content-Length' 的 #wmsg(tn, 'Content-Length: 4') wmsg(tn, '') # 開始 Chunked Body 內容 wmsg(tn, '2') # 開始有 2 bytes data 來 wmsg(tn, 'A ') wmsg(tn, '4') # 開始有 4 bytes data 來 wmsg(tn, 'test') wmsg(tn, '0') # 結束 chunk wmsg(tn, '') # 結束 body
說明
1) Header
Transfer-Encoding: chunked
Data is sent in a series of chunks. The Content-Length header is omitted in this case
2)
wmsg(tn, '2') # 開始有 2 bytes data 來 wmsg(tn, 'A ')
beginning of each chunk you need to add the length of the current chunk in hexadecimal format,
followed by '\r\n' and then the chunk itself, followed by another '\r\n'.
3)
wmsg(tn, '0') # 結束 chunk wmsg(tn, '') # 結束 body
The terminating chunk is a regular chunk, with the exception that its length is zero.
It is followed by the trailer('\r\n')
Transfer-Encoding
Transfer-Encoding 也是 hop-by-hop headers
i.e.
// Several values can be listed, separated by a comma
Transfer-Encoding: gzip, chunked
Gzip + Chunk
// gzip 所有內容再把它 chunk
Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Content-Encoding: gzip