note 89786 added to function.http-chunked-decode
| From: | MarquesJohansson at osu1 dot php dot net | Date: | Mon, 23 Mar 2009 16:48:06 +0000 |
| Subject: | note 89786 added to function.http-chunked-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152333@lists.php.net to get a copy of this message | ||
Here's a PHP implementation for those less interested in installing a pecl module. It's a
bit naive (doesn't handle multiple byte encodings), but it works. I'm also including my
is_hex() implementation at no additional cost.
if (!function_exists('http-chunked-decode')) {
/**
* dechunk an http 'transfer-encoding: chunked' message
*
* @param string $chunk the encoded message
* @return string the decoded message. If $chunk wasn't encoded properly it will be
returned unmodified.
*/
function http_chunked_decode($chunk) {
$pos = 0;
$len = strlen($chunk);
$dechunk = null;
while(($pos < $len)
&& ($chunkLenHex = substr($chunk,$pos, ($newlineAt =
strpos($chunk,"\n",$pos+1))-$pos)))
{
if (! is_hex($chunkLenHex)) {
trigger_error('Value is not properly chunk encoded', E_USER_WARNING);
return $chunk;
}
$pos = $newlineAt + 1;
$chunkLen = hexdec(rtrim($chunkLenHex,"\r\n"));
$dechunk .= substr($chunk, $pos, $chunkLen);
$pos = strpos($chunk, "\n", $pos + $chunkLen) + 1;
}
return $dechunk;
}
}
/**
* determine if a string can represent a number in hexadecimal
*
* @param string $hex
* @return boolean true if the string is a hex, otherwise false
*/
function is_hex($hex) {
// regex is for weenies
$hex = strtolower(trim(ltrim($hex,"0")));
if (empty($hex)) { $hex = 0; };
$dec = hexdec($hex);
return ($hex == dechex($dec));
}
----
Server IP: 208.69.120.35
Probable Submitter: 208.236.9.10
----
Manual Page -- http://www.php.net/manual/en/function.http-chunked-decode.php
Edit -- https://master.php.net/note/edit/89786
Del: integrated -- https://master.php.net/note/delete/89786/integrated
Del: useless -- https://master.php.net/note/delete/89786/useless
Del: bad code -- https://master.php.net/note/delete/89786/bad+code
Del: spam -- https://master.php.net/note/delete/89786/spam
Del: non-english -- https://master.php.net/note/delete/89786/non-english
Del: in docs -- https://master.php.net/note/delete/89786/in+docs
Del: other reasons-- https://master.php.net/note/delete/89786
Reject -- https://master.php.net/note/reject/89786
Search -- https://master.php.net/manage/user-notes.php