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

Source: Internet
Author: User
When a short connection is used, each HTTP request corresponds to a TCP connection. after the request is complete, the connection is immediately disconnected, and the server returns EOF. Therefore, you can determine the end of a request based on EOF. the following code (PHP) is very common:
The 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
The 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
The 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: info-contact@alibabacloud.com 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.