php-如何正确处理分块编码请求?
作者:互联网
我有两个网站:一个使用PHP的Lighttpd,另一个使用Apache的网站,并且这两个站点都不能正确处理分块传输编码.
我是从手机J2ME发送此请求的,因此无法将此传输类型更改为其他任何类型.
因此,我唯一的方法是以其他方式处理分块传输编码请求.只要能在我的CentOS服务器上启用它并安装和更改所有必要的东西,任何解决方案都将是好的.
所以我的问题是:如何在服务器端正确处理分块编码请求?
解决方法:
编辑:
您正在运行哪个版本的PHP / Apache / LightHTTP?以前在PHP中有this bug,但是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
?>
标签:lighttpd,centos,http,java-me,php 来源: https://codeday.me/bug/20191024/1916791.html