Home > Backend Development > PHP Tutorial > Implementation code to determine the end of HTTP request in Keep-Alive mode_PHP tutorial

Implementation code to determine the end of HTTP request in Keep-Alive mode_PHP tutorial

WBOY
Release: 2016-07-21 15:25:46
Original
868 people have browsed it

Therefore, the end of a request can be judged based on EOF. The following code (PHP) is very common:

Copy code 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 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 called persistent connection) mode of HTTP does not disconnect the server after sending the data, but keeps it for the next HTTP request, so the benefits of long connections are obvious, by sharing a TCP connection Save the overhead of establishing/disconnecting connections on future requests. EOF will not be 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 encountered when using long connections. There are currently two main methods of judgment:
(1) Based on the Content-Length field in the header. This field indicates the length of the text. We can judge the end based on receiving characters of the specified length.
(2) When there is no Content-Length, based on Transfer-Encoding. Sometimes the server cannot determine the size of the text because the text may be dynamically generated, so Content-Length will not be provided. Instead, chunk encoding is used to send the text piece by piece. Each chunk block consists of two parts: a header and a body. A hexadecimal number in the header specifies the length of the body. Finally, a chunk block with a length of 0 indicates the end of the entire HTTP body.
Below I use PHP to implement the judgment method when there is Content-Length:
1. Get the Content-Length value
Copy code The code is as follows:

$length = 0;
$line = '';
while($line !== "rn") {
$line = fgets($fp);
if(substr($line, 0, 15) === 'Content-Length:') {
$length = intval(substr($line, 16));
}
}

2. Get the text
Copy code The code is as follows:

$sum = 0;
while($sum < $length) {
$line = fgets($fp);
$sum += strlen($line) ;
echo $line;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324034.htmlTechArticleSo the end of a request can be judged based on EOF. The following code (PHP) is very common: Copy the code The code is as follows : // $fp is the handle generated by fsockopen() while(!feof($fp)) { echo fgets(...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template