Home  >  Article  >  Backend Development  >  Galaxy s4 active implementation code to determine the end of the HTTP request in Keep-Alive mode

Galaxy s4 active implementation code to determine the end of the HTTP request in Keep-Alive mode

WBOY
WBOYOriginal
2016-07-29 08:46:301192browse

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 server in long connection (also called persistent connection) mode does not disconnect the connection after sending the data, but keeps it for the next HTTP request. When used, the benefits of long connections are obvious. By sharing a TCP connection, the overhead of establishing/disconnecting connections during subsequent requests can be saved. 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 it will not provide Content-Length, but use chunk encoding 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 the 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 the code The code is as follows:


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

The above introduces the implementation code of Galaxy s4 active to judge the end of HTTP request in Keep-Alive mode, including the content of Galaxy s4 active. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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