71 lines
2.5 KiB
PHP
Executable File
71 lines
2.5 KiB
PHP
Executable File
<?php
|
|
|
|
/*
|
|
The MIT License
|
|
|
|
Copyright (c) 2013 Mashape (http://mashape.com)
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
a copy of this software and associated documentation files (the
|
|
"Software"), to deal in the Software without restriction, including
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
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));
|
|
}
|
|
|
|
?>
|