gpt4 book ai didi

php - 如何正确处理分块编码请求?

转载 作者:可可西里 更新时间:2023-11-01 00:52:56 25 4
gpt4 key购买 nike

我有两个网站:一个使用带 PHP 的 Lighttpd,第二个使用 Apache,这两个网站都无法正确处理分块传输编码。

我从我的移动设备 J2ME 发送此请求,无法将此传输类型更改为任何其他类型。

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

所以我的问题是:如何在服务器端正确处理分块编码请求?

最佳答案

编辑:您运行的是哪个版本的 PHP/Apache/LightHTTP?因为有 this bug以前在 PHP 中,但它似乎在 5.2.13 和 5.3.2 中消失了。

如果上面的链接没有帮助,我想知道 PHP 到底看到了什么,你能把它放在你的 api 中并发布结果吗? (当然是编辑下来的)。

$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 看到了什么,如果它没有解码分块编码,那么我们可以手动解码它。

结束编辑。 (留下下面的内容以防其他人发现它有用)

我认为这是对您上一个问题的跟进。我假设您正在从 PHP 读取流?

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

<?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
?>

关于php - 如何正确处理分块编码请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3289574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com