Code used to determine the end of an HTTP request in Keep-Alive Mode

Source: Internet
Author: User

Therefore, you can determine the end of a request based on EOF. The following code (PHP) is very common:
Copy codeThe Code is as follows:
// $ Fp is the handle generated by fsockopen ().
While (! Feof ($ fp )){
Echo fgets ($ fp );

(Note: The short Connection mode is marked with "Connection: close" in the header and "Connection: keep-alive" in the persistent Connection mode. Currently, HTTP/1.0 uses short connections by default, and HTTP/1.1 uses long connections by default .)
However, after sending data, the server does not disconnect the persistent connection mode (also known as persistent connection). Instead, the server keeps the connection for the next HTTP request. Therefore, the advantage of persistent connection is obvious, by sharing a TCP connection, you can save the overhead of establishing/disconnecting requests in the future. The EOF is sent until the TCP connection ends (timeout or error), so we cannot use the above method to judge the end of an HTTP request. This is also a problem that occurs when persistent connections are used. Currently, two methods are available:
(1) According to the Content-Length Field in the header. This field indicates the length of the body. We can end the judgment by receiving the characters of the specified length.
(2) When there is no Content-Length, it is based on Transfer-Encoding. In some cases, the server cannot determine the size of the body, because the body may be dynamically generated, so Content-Length is not provided, but the chunk encoding is used to send the body one by one. Each chunk consists of a header and a body. A hexadecimal number specifies the length of the body; finally, a chunk block with a length of 0 indicates the end of the entire HTTP body.
The following code uses PHP to determine Content-Length:
1. Get the Content-Length Value
Copy codeThe Code is as follows:
$ Length = 0;
$ Line = '';
While ($ line! = "\ R \ n "){
$ Line = fgets ($ fp );
If (substr ($ line, 0, 15) === 'content-Length :'){
$ Length = intval (substr ($ line, 16 ));

2. Obtain the body
Copy codeThe Code is as follows:
$ Sum = 0;
While ($ sum <$ length ){
$ Line = fgets ($ fp );
$ Sum + = strlen ($ line );
Echo $ line;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.