开发者

Decoding HTTP chunked + gzipped content

开发者 https://www.devze.com 2023-04-12 07:56 出处:网络
How would one decode a server response that is 1) transfer-encode: chunked 2) content-type: gzip I need to do this manually and can\'t just use curl to send the request. I need to decode from the r

How would one decode a server response that is

1) transfer-encode: chunked 2) content-type: gzip

I need to do this manually and can't just use curl to send the request. I need to decode from the raw $string.

Here's a function that is su开发者_StackOverflowpposed to unchunk a HTTP reponse (php):

function unchunkHttpResponse($str=null) {
if (!is_string($str) or strlen($str) < 1) { return false; }
$eol = "\r\n";
$add = strlen($eol);
$tmp = $str;
$str = '';
do {
    $tmp = ltrim($tmp);
    $pos = strpos($tmp, $eol);
    if ($pos === false) { return false; }
    $len = hexdec(substr($tmp,0,$pos));
    if (!is_numeric($len) or $len < 0) { return false; }
    $str .= substr($tmp, ($pos + $add), $len);
    $tmp  = substr($tmp, ($len + $pos + $add));
    $check = trim($tmp);
    } while(!empty($check));
unset($tmp);
return $str;
}

But it seems to not work with gzipped content (gzinflate is not in this snippet). I tried doing gzinflate on the returned content but that doesn't work at all.

Any ideas? Or how do gzip + transfer encoding chunked work together?

Thanks


Could you start by verifying that this function indeed properly aggregates chunked response by trying it against some text response. I'm not proficient in php so I can't say for sure if this function will work, but as gzipped response will be binary, I wonder if concatenating it to string is OK thing to do. And could you explain why you need to "decode from the raw $string"? Why using some client that is proven to handle all HTTP details properly, and inflating whatever it returns is not working for you?

0

精彩评论

暂无评论...
验证码 换一张
取 消