To determine the end of an HTTP request for Keep-alive mode _php tutorial

Source: Internet
Author: User
When using short connection mode, each HTTP request corresponds to a TCP connection, the connection is immediately disconnected after the request completes, and the server returns EOF. So you can judge the end of a request based on EOF, the following code (PHP) is common:

Copy to ClipboardWhat to refer to: [www.bkjia.com]$FP is a handle produced by Fsockopen ()
while (!feof ($fp)) {
Echo fgets ($FP);
}

(Note: The short connection mode is marked with "Connection:close" in the head, and the long connection is marked with "connection:keep-alive". Currently http/1.0 uses short connections by default, and http/1.1 uses long connections by default. )

The long connection (also known as persistent connection) mode of HTTP after sending the data after the server does not disconnect, but to keep the next HTTP request to use, so the benefits of long connection is obvious, by sharing a TCP connection to save the subsequent request to establish/disconnect the cost of the connection . EOF is not sent until the end of the TCP connection (timeout or error), so we cannot use the method above to determine the end of an HTTP request. This is also an issue that you will encounter when using long connections. There are two main methods of judging today:

(1) based on the Content-length field in the header. This field indicates the length of the body, and we can determine the end of the text by receiving the specified length of the character.
(2) in the absence of content-length, according to Transfer-encoding. Sometimes the server cannot determine the size of the body, because the body may be generated dynamically, so it does not provide content-length, but instead uses chunk encoding to send the body one piece at a time. Each chunk block consists of the head and the body two parts, the head is specified by a 16 binary number of the length of the body, and finally a 0-length chunk block to represent the end of the entire HTTP body.

Here I use PHP to achieve the content-length when the way to judge:

1. Get Content-length value

Copy to ClipboardWhat to refer to: [www.bkjia.com]$length = 0;
$line = ";
while ($line!== "\ r \ n") {
$line = fgets ($FP);
if (substr ($line, 0,) = = = = ' Content-length: ') {
$length = Intval (substr ($line, 16));
}
}

2. Get the text

Copy to ClipboardWhat to refer to: [www.bkjia.com]$sum = 0;
while ($sum < $length) {
$line = fgets ($FP);
$sum + = strlen ($line);
Echo $line;
}

http://www.bkjia.com/PHPjc/364733.html www.bkjia.com true http://www.bkjia.com/PHPjc/364733.html techarticle when using short connection mode, each HTTP request corresponds to a TCP connection, the connection is immediately disconnected after the request completes, and the server returns EOF. So the end of a request can be judged based on EOF, below ...

  • Related Article

    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.