如何正确处理分块编码请求? [英] How to handle chunked encoding request properly?

查看:215
本文介绍了如何正确处理分块编码请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个网站:一个使用Lighttpd与PHP和第二个与Apache和这个句柄chunked传输编码正确。

I have two websites: one with Lighttpd with PHP and second with Apache and neither of this handle chunked transfer encoding properly.

我从我的手机发送这个请求, J2ME,并且不可能将此传输类型更改为任何其他类型。

I send this request from my mobile, J2ME and there is no possibility to change this transfer type to any other.

所以我唯一的方法是以其他方式处理分块传输的enconcoded请求。任何解决方案都将是好的,只要我可以在我的CentOS服务器上启用它,我可以安装和更改一切必要的。

So my only way to go is to handle chunked transfer enconcoded requests in other way. Any solution will be good as long as I can enable it on my CentOS server where I can install and change everything that will be necessary.

所以我的问题是:如何

推荐答案

编辑:
PHP / Apache / LightHTTP的哪个版本运行?由于在PHP之前有这个bug ,但它似乎已经消失了5.2.13和5.3.2。

Which version of PHP/Apache/LightHTTP are you running? As there was this bug in PHP before, but it seems to be gone with 5.2.13 and 5.3.2.

如果上面的链接没有帮助,我想知道PHP正在看什么,你可以把它放在你的api和post的结果? (当然编辑)。

If the link above doesn't help, I'd like to know exactly what PHP is seeing, can you put this in your api and post the results? (edited down of course).

$input = file_get_contents('php://input');
$stdin = file_get_contents('php://stdin');

print "FILES: ";
print_r($_FILES);

print("<br>POST: ");
print_r($_POST);

print("<br>input: ".$input);
print("<br>stdin: ".$stdin);
die;

这样我们可以看到PHP看到了什么,如果它不解码分块编码,手动解码。

This way we can see what PHP is seeing, and if it's NOT decoding chunked encoding, then we CAN manually decode it.

结束编辑。 (如果有人认为有用,请留下以下内容)

End EDIT. (leaving the below in case someone else finds it useful)

我假设这是您上一个问题的后续追踪。我假设你正在从PHP读取流?

I assume this is a followup to your previous question. And I'm assuming you're reading the stream from PHP?

我写了这几年,它从一个分块的编码流读取,然后可以做任何你想要的输出。如果是大文件,不要将其读入字符串,而是写入文件。

I wrote this a few years back, it reads from a chunked encoded stream, you can then do whatever you want with the output. If it's a large file don't read it into a string, instead write to a file.

<?php
define('CRLF', "\r\n");
define('BUFFER_LENGTH', 8192);
$headers = '';
$body = '';
$length = 0;

$fp = fsockopen($host, $port, $errno, $errstr, $timeout);

// get headers FIRST
do
{
    // use fgets() not fread(), fgets stops reading at first newline
    // or buffer which ever one is reached first
    $data = fgets($fp, BUFFER_LENGTH);
    // a sincle CRLF indicates end of headers
    if ($data === false || $data == CRLF || feof($fp)) {
        // break BEFORE OUTPUT
        break;
    }
    $headers .= $data;
}
while (true);
// end of headers

// read from chunked stream
// loop though the stream
do
{
    // NOTE: for chunked encoding to work properly make sure
    // there is NOTHING (besides newlines) before the first hexlength

    // get the line which has the length of this chunk (use fgets here)
    $line = fgets($fp, BUFFER_LENGTH);

    // if it's only a newline this normally means it's read
    // the total amount of data requested minus the newline
    // continue to next loop to make sure we're done
    if ($line == CRLF) {
        continue;
    }

    // the length of the block is sent in hex decode it then loop through
    // that much data get the length
    // NOTE: hexdec() ignores all non hexadecimal chars it finds
    $length = hexdec($line);

    if (!is_int($length)) {
        trigger_error('Most likely not chunked encoding', E_USER_ERROR);
    }

    // zero is sent when at the end of the chunks
    // or the end of the stream or error
    if ($line === false || $length < 1 || feof($fp)) {
        // break out of the streams loop
        break;
    }

    // loop though the chunk
    do
    {
        // read $length amount of data
        // (use fread here)
        $data = fread($fp, $length);

        // remove the amount received from the total length on the next loop
        // it'll attempt to read that much less data
        $length -= strlen($data);

        // PRINT out directly
        #print $data;
        #flush();
        // you could also save it directly to a file here

        // store in string for later use
        $body .= $data;

        // zero or less or end of connection break
        if ($length <= 0 || feof($fp))
        {
            // break out of the chunk loop
            break;
        }
    }
    while (true);
    // end of chunk loop
}
while (true);
// end of stream loop

// $body and $headers should contain your stream data
?>

这篇关于如何正确处理分块编码请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆