note 12941 deleted from ref.http by philip
| From: | philip@php.net | Date: | Thu, 12 Dec 2002 23:07:20 +0000 |
| Subject: | note 12941 deleted from ref.http by philip | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-40818@lists.php.net to get a copy of this message | ||
ABOUT HTTP/1.1 TRANSFERT ENCODING HEADER CHUNKED
When you try to get page by using http/1.1 connection don't forget this:
- Put "Connection:close" in request header to close connection after answer is received
(http 1.1 use persistent connection as default to give avaibility to do multiple request in the same
connection) (Avoid wait time)
- Put "Host:yourwebserver" because it's mandatory
- Keep in mind that if you try to get a page served by a CGI engine like php, you'll probably
get a "transfert-encoding=chunked" line in answer header's. Means that content is
split into several pieces. See script below to see chunk format.
-Remember that each request header line put be followed by CRLF (\r\n) and last header line by
CRLFCRLF
eg: Get Request
GET /dir/page.php HTTP/1.1<CRLF>
Host:yourhost<CRLF>
Connection:close<CRLF><CRLF>
eg:Chunked answer
HTTP/1.1 200 OK
Date: Wed, 16 May 2001 00:25:24 GMT Server: Apache/1.3.6
Xpowered-By: PHP/4.0.4pl1
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html
f43
<HTML>.....<BODY>.....<B
fbb
R>..........</BO
6
DY>
0
Ok ? To decode chunked message explode http answer (called result) to chunked content
$result=explode("\r\n\r\n",$result);
$header=$result[0];
$chunked_content=$result[1];
Decode_Chunked_Message($chunked_content);
Below,Decode_chunked_Message function return decoded content:
function Decode_Chunked_Message($message) {
//This function apply after http answer is fully received. It cannot garanties all is received.
//You can use $DEBUG=True; to see each chunk decoded with info
//$DEBUG=True;
//CHUNKED MESSAGES ARE FORMATTED LIKE THIS (Extension Not Supported)
//HEXA_CHUNK_SIZE|CRLF|DATA|HEXA_CHUNK_SIZE|CRLF|HEXA_CHUNK_SIZE|CRLF|DATA|HEXA_CHUNK_SIZE|...|0
//(0 means next CHUNK SIZE=0)
//pipe are not in chunked message (just for your eyes...)
$CRLF_LENGHT = 2;// equal to "\r\n"
$chunk_pos=0; //Start at position 0 of message
$crlf_pos = strpos ($message , "\r\n" , $chunk_pos);//Look for first
$chunk_size=substr($message,$chunk_pos,$crlf_pos);
$octets_to_read = hexdec($chunk_size);
$start_read = $crlf_pos + $CRLF_LENGHT;
while($octets_to_read>0){
if($DEBUG) {
echo "chunk_pos : $chunk_pos<br>
crlf_pos : $crlf_pos<br>
octets_to_read : $octets_to_read ($chunk_size)<br>
start_read : $start_read<br><br><br>";
}
$buffer.=substr($message,$start_read,$octets_to_read);
if($DEBUG) {
echo "Buffer size:".strlen($buffer)."<br>";
echo "Buffer
content:".htmlentities(substr($message,$start_read,$octets_to_read))."<br>";
}
$chunk_pos = $start_read + $octets_to_read + $CRLF_LENGHT;
if(strlen($message)>$chunk_pos) {
$crlf_pos = @strpos ($message , "\r\n" , $chunk_pos);
$chunk_size=substr($message,$chunk_pos,$crlf_pos-$chunk_pos);
$octets_to_read = hexdec($chunk_size);
$start_read = $crlf_pos + $CRLF_LENGHT;
} else $octets_to_read=0;
}
return $buffer;
}