Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the benefits of block transmission coding in HTTP

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "what are the benefits of block transmission coding in HTTP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Normally, the response body message body of HTTP is sent to the client as the whole packet, and the header "Content-Length" is used to indicate the length of the message body. This length is very important to the client, because for persistent connections, TCP does not end immediately after the request, but can send multiple requests / responses. The client needs to know which location is the end of the response message and the beginning of the subsequent response. Therefore, Content-Length is particularly important. The server must tell the client exactly what the length of the message body is. If the Content-Length is shorter than the actual length returned, the content will be truncated. If it is longer than the physical content, the client will remain in the pendding state until all the message body returns the request.

The emergence of Web2.0 makes the web page rich and colorful, and the content is much more complex than the early web pages, so we will encounter a problem. For a complex page, if you wait until the message body is fully created and then calculate the Content-Length to return to the client, there will be a long waiting process on the client side, and for the user, the waiting time for a page is no more than 3 seconds. Therefore, how to make the response content as early as possible for users to see is a problem to be considered by the HTTP protocol.

Block transfer coding (Transfer-Encoding) is such a solution: it divides the data into a series of data blocks and sends them to the client in multiple blocks. When the server sends the data, it no longer needs to tell the client in advance the total size of the content sent. It only needs to add Transfer-Encoding: chunked to the response header to tell the browser that I am using the block transfer code, so that Content-Length is not needed. This is the function of block transmission coding Transfer-Encoding.

Benefits of block transmission coding:

HTTP block transfer coding allows the server to maintain HTTP persistent links for dynamically generated content. Typically, persistent links require the server to send Content-Length header fields before starting to send the message body, but for dynamically generated content, it is unknowable until the content is created.

The block transmission coding allows the server to send the header field at the end. It is important for situations where the value of the header field cannot be known until the content is generated, such as when the content of the message is signed with a hash, and the result of the hash is transmitted through the HTTP header field. When there is no block transmission coding, the server must buffer the content until the values of the header fields are calculated after completion and send the values of those header fields before sending the content.

HTTP servers sometimes use gzip to shorten the time it takes to transfer. Block transmission coding can be used to separate multiple parts of a compressed object. In this case, the block is not compressed separately, but the entire load is compressed, and the compressed output is transmitted in blocks using the scheme described in this article. In the case of compression, block coding is beneficial to send data while compressing, rather than completing the compression process to know the size of the compressed data.

Specifying Transfer-Encoding: chunked in the header means that the entire response will transmit the content using a block transfer code, with a complete message body consisting of n blocks and ending with the last block of size 0. Each non-empty block consists of two parts: the length of the block (expressed in hexadecimal) followed by a CRLF (carriage return and line feed), excluding the carriage return newline at the end. The second part is the data itself, which also ends with CRLF (carriage return and line feeds). The last block is a single row, consisting of only the block size (0) and CRLF, and does not contain any data.

You can now implement a simple HTTP Server with Python to specify the header Transfer-Encoding of the Response.

#-*-coding:utf-8-*-import socketif _ _ name__ ='_ _ main__': PORT = 8000 sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM) sock.bind (('127.0.0.1, PORT)) sock.listen (1) print' Serving HTTP on port% s...'% PORT while 1: conn, addr = sock.accept () print conn Addr request = conn.recv (1024) # HTTP response message conn.sendall ("HTTP/1.1 200OK\ r\ n") # status line conn.sendall ("Content-Type: text/plain\ r\ n") conn.sendall ("Transfer-Encoding: chunked\ r\ n") # declare block transfer code conn.sendall ("\ r\ n") # blank line Conn.sendall ("b\ r\ n") # 11 byte length conn.sendall ("hello world\ r\ n") # message body conn.sendall ("0\ r\ n") # Last block 0 length conn.sendall ("\ r\ n") conn.close ()

Test with telnet request:

Telnet localhost 8000Trying:: 1...telnet: connect to address:: 1: Connection refusedTrying fe80::1...telnet: connect to address fe80::1: Connection refusedTrying 127.0.0.1...Connected to localhost.Escape character is'^] '.get HTTP/1.1 / # send GET request HTTP/1.1 200OKContent-Type: text/plainTransfer-Encoding: chunkedbhello world0 what are the benefits of block transfer coding in HTTP? Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report