|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60826 raw POST data missing with chunked encoding, FastCGI
Submitted: 2012-01-20 21:15 UTC Modified: 2020-02-27 17:50 UTC
Avg. Score:4.9 ± 0.2
Reproduced:13 of 13 (100.0%)
Same Version:10 (76.9%)
Same OS:6 (46.2%)
From: clarkwise at gmail dot com Assigned: ab (profile)
Status: Not a bug Package: CGI/CLI related
PHP Version: 5.3.9 OS: Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
21 + 7 = ?
Subscribe to this entry?

 [2012-01-20 21:15 UTC] clarkwise at gmail dot com
When a POST is sent with the header "Transfer-Encoding: chunked" and PHP 5.3 is 
running via FastCGI, $HTTP_RAW_POST_DATA is not set. In IIS, the receiving PHP 
process simply hangs and does not execute at all. If chunked encoding is not set, 
it executes successfully and $HTTP_RAW_POST_DATA is populated.

Comparing ISAPI to FastCGI (using PHP 5.2 which has both implementations), PHP 
ISAPI works fine with "Transfer-Encoding: chunked" but PHP FastCGI does not.

This issue also occurred running Linux/Apache with PHP 5.3 FastCGI. In that 
scenario, the PHP process did not completely hang, but $HTTP_RAW_POST_DATA and 
php://input were empty when the script executed.

Test script:
Two files, postsend.php and postreceive.php, can be found within the question here:

Expected result:
$HTTP_RAW_POST_DATA and the php://input stream should contain the raw binary data 
that was sent in the POST.

Actual result:
On Windows/IIS, the PHP process hangs and does not execute.
On Linux/Apache, the PHP process executes but $HTTP_RAW_POST_DATA and php://input 
are empty.


iis-cgi-chunked-poc (last revision 2020-02-27 17:50 UTC by
IIS-FastCGI-transfer-encoding-chuncked (last revision 2012-04-11 13:52 UTC by andres at fenestrae dot com)

Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-02 00:35 UTC] timo dot witte at googlemail dot com
I have the same problem on my machine and can confirm this bug!
i think this bug affects mod_spdy aswell, because it sends the POST data chunked! 
( ).
 [2012-02-09 19:17 UTC]
I've additionally tested the two files on nginx with PHP as FCGI. The request simply doesn't go throught to PHP and nginx gives 401 back. I've attached gdb to the PHP and set a break in the accept loop - there was no reaction when using the two test files, but requests without data got down to PHP. So my conclusion were - apache simply ignores the request body where nginx gives a correct http status. Anyways, this has nothing to do with PHP. IIS, Apache and Nginx seem to have no implementation for chunked incoming body stuff, but they do handle that a different way.

Btw. Nginx has the "chunkin" module for such actions.
 [2012-02-29 09:42 UTC]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: ab
 [2012-03-01 08:31 UTC]
@clarkwise at gmail dot com

So could you confirm it works with nginx+chunkin?

As well, it's was not hanging with IIS (v7.5) for me. Which IIS version do you 
 [2012-03-01 20:24 UTC] clarkwise at gmail dot com
Since I am using Windows XP, I'm stuck using IIS 5.1. When I have the opportunity 
to try nginx w/ chunkin or IIS 7.5, I'll update with further details. Thanks.
 [2012-03-05 10:08 UTC] florian dot fernandez at navaho dot fr
Same problem here. Apache 2.2.15 and PHP 5.3.10 running as FastCGI.

POST datas sent with "Transfer-encoding: chunked" are not received as they should be. The raw request looks like this :

POST receiveChunk.php HTTP/1.1
User-Agent: PECL::HTTP/1.7.2 (PHP/5.3.8)
Accept: */*
Pragma: no-cache
Connection: keep-alive
Cache-Control: no-cache
Content-Length: 682668
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

(here, the datas)

and the request as the receiving script sees it :

User-Agent: PECL::HTTP/1.7.2 (PHP/5.3.8)
Accept: */*
Pragma: no-cache
Connection: close
Cache-Control: no-cache

As you can see, the request body is empty.

However, this problem does not occur when PHP runs as mod_php under Apache. Problem confirmed under PHP 5.3.8 and 5.3.9.

I don't know if that is a PHP bug and maybe the developers could confirm or not that the request body are wrong before it reaches PHP. I think that it appears to be a mod_fcgid bug and should be reported as one to Apache bug list.

Here are the scripts to reproduce the bug :

sendChunk.php :
$fileContentToUp = 'ICAgICAgICAgIgICAgIA==';

$header_array = array(
	'Pragma' => 'no-cache',
	'Connection' => 'keep-alive',
	'Cache-Control' => 'no-cache',
	'Content-Length' => strlen($fileContentToUp) ,
	'Transfer-Encoding' => 'chunked'

$options = array(headers => $header_array);

$r = new HttpRequest('receiveChunk.php', HTTP_METH_POST, $options);
$r->setContentType('text/html; charset=UTF-8');
$result = $r->send();

echo '$r->getRawRequestMessage() => ';
echo "\n\n";
echo '$r->send()->getBody() => ';
echo "\n\n";

receiveChunk.php :
$HttpResponse = new HttpResponse();
echo 'http_get_request_header => '."\n";
echo "\n\n";
echo 'http_get_request_body => '."\n";
echo "\n\n";

Hope that helps.
 [2012-03-05 11:46 UTC] florian dot fernandez at navaho dot fr
I forgot to mention that this is on a Linux system
 [2012-03-09 09:33 UTC] florian dot fernandez at navaho dot fr
The problem seems to have been resolved with php 5.4.0 and apache 2.4.1. I can't reproduce it with this configuration and chunked http requests are now coming through to php now.

I am going to try with php 5.3.10 and apache 2.4.1 and give feedback on where it goes.
 [2012-03-09 09:48 UTC]
Try a snapshot please, 5.3.10 has nothing else but security fixes unrelated to 
this issue.
 [2012-03-13 05:58 UTC] florian dot fernandez at navaho dot fr
First of all, the problem is still here with PHP 5.4 and Apache 2.4.1, I previously made the mistake to precise the content-length in my headers.

So here is what I found. If you send chunked http request but do not send the content-length (as we should do using HTTP 1.1 protocol) the body part of the request is empty (php://input returns nothing). This result remains valid for the following configuration where PHP runs via FastCGI : 

PHP 5.3.10 - APACHE 2.4.1
PHP 5.3-201203101230 - APACHE 2.4.1
PHP 5.4 - APACHE 2.4.1
PHP 5.4-201203101230 - APACHE 2.4.1

The same configuration with PHP running as mod_php is fine, the body request is available through php://input.
 [2012-03-23 15:27 UTC] clarkwise at gmail dot com
Making the chunked request to a stock nginx 1.0.13 installation
on Ubuntu yields a "411 Length Required" message, but enabling
the ngx_chunkin module does indeed correctly serve the request
and populate php://input and $HTTP_RAW_POST_DATA.

This was done with both Ubuntu's distribution of
PHP 5.3.6 and the php5.4-201203221530 snapshot.
 [2012-03-27 08:52 UTC] florian dot fernandez at navaho dot fr
So, it is not a PHP problem ?
 [2012-03-27 13:34 UTC]
@florian dot fernandez at navaho dot fr

Looks like that. Could you try nginx+chunkin to confirm please?
 [2012-03-27 20:26 UTC] clarkwise at gmail dot com
I finally got my hands on Windows Server 2008 running IIS 7.0.
Same problem; when POST request is chunked, the response times out.

Perhaps I'm missing a setting that would enable the receipt of 
chunked incoming requests, but I have only read about settings
that chunk the outgoing response.
 [2012-04-03 13:02 UTC] florian dot fernandez at navaho dot fr
@ab at php dot net

Unfortunately, I won't be able to test with nginx because the system administrators won't let me install it (Only Apache is allowed). Sorry for the inconvenience. I'll try to open a new bug on Apache bug list or mod_fcigd bug list if I find it : maybe they'll look into it.
 [2012-04-11 13:24 UTC] andres at fenestrae dot com
The problem lies in php-cgi.exe.
I have been able to reproduce the problem on IIS 7.5.
In Fast-cgi the data is delivered using method similar to chunking.

In case of "Transfer-Encoding: chunked" IIS supplies Content-length:-1 in the request data, which is casted to uint in sapi_cgi_read_post. Causing the routine to read data until it has read the full 4G (2^32-1).
Also, both the methods sapi_cgi_read_post(cgi_main.c) and fcgi_read(fastcgi.c) try to read the complete buffer time after time, causing the end-marker (0-sized chunk) only to be seen when it would be at buffer-boundary. However the sapi_cgi_read_post funtion is called several times after the actual content has been read causing the fcgi_read method to block.
I expect similar cause to apply for apache.

Solution appears
1) do not loop in fcgi_read; this way when reading the final marker, it is returned to the calling code.
2) Set SG(request_info).content_length to SG(read_post_bytes) + read_bytes when tmp_read_bytes ==0 && SG(request_info).content_length == -1
 [2012-04-23 07:52 UTC] florian dot fernandez at navaho dot fr
@ andres at fenestrae dot com

Have you been able to try what you suggested in your last post ?
 [2012-04-23 08:00 UTC] andres at fenestrae dot com
florian dot fernandez at navaho dot fr 

I have been able to try my solution in combination with 5.2.17.

See the patches in this topic for the exact code-changes I made.
 [2012-04-23 08:08 UTC] florian dot fernandez at navaho dot fr
Sorry, I didn't see it before. I'll give it a try with 5.4.0
 [2012-04-23 10:59 UTC] florian dot fernandez at navaho dot fr
Maybe I did something wrong but the patch doesn't seem to work with PHP 5.4.0 on Linux.
 [2012-04-23 14:34 UTC] andres at fenestrae dot com
@florian dot fernandez at navaho dot fr


I'm afraid i cannot help you any further on Linux.
The solution is not specific for Windows.
What I did to tackle the problem at first is to write a small program that ran instead of php-cgi which copied whatever it received on stdin to an other a mailslot.
This output I fed into php-cgi to reproduce an pinpoint the cause. 

Regards Andre
 [2012-05-30 21:10 UTC] dominic dot benson at thirdlight dot com
For Apache mod_fcgid, I have a patch available in the bug I filed at

Essentially, PHP is following the spec, and not reading beyond the FastCGI 
CONTENT_LENGTH header value - so correct fixes are necessarily above PHP, in the 
FastCGI or web server implementation.
 [2012-08-17 08:12 UTC]
-Status: Assigned +Status: Not a bug
 [2012-08-17 08:12 UTC]
at the end of the day, this is not a php bug
 [2016-06-27 23:47 UTC] ravikumar dot nie at gmail dot com
Well I am still facing this problem.
Any hints to solve this issue.

I am using fastcgi and php and trying to read the POST Data sent as transfer-encoding:chunked.

Appreciate any help
 [2018-02-05 09:03 UTC] sukarsa at ingenovie dot com
Is there any solution to request with chunked encoding with php?
I observe that server is waiting indefinitely until client time-out and then process the packet with empty body.
I'm using php 7.2 and I think it still have this problem.
 [2020-02-27 17:50 UTC]
The following patch has been added/updated:

Patch Name: iis-cgi-chunked-poc
Revision:   1582825823
 [2020-02-27 17:50 UTC]
In a strict sense this is indeed not a PHP bug, because the CGI
specification (which is only informational, though), mandates[1]:

| As transfer-codings are not supported on the request-body, the
| server MUST remove any such codings from the message-body, and
| recalculate the CONTENT_LENGTH.  If this is not possible (for
| example, because of large buffering requirements), the server
| SHOULD reject the client request.

However, even though this is not done by IIS, its behavior still
appears to be reasonable, since it acknowledges the `Expect:
100-continue` header by immediately responding with `HTTP/1.1 100
Continue`, and then calls php-cgi.exe with CONTENT_LENGTH=-1 and
makes the full payload available on stdin.  So it seems the (F)CGI
SAPI *could* support chunked uploads in combination with IIS.

I have attached iis-cgi-chunked-poc as *PoC* for IIS and CGI only
(not FCGI).

[1] <>
 [2021-02-16 22:50 UTC] ribarra at expedienteazul dot com
Problem persists in Amazon Linux 2 distro.
PHP 7.2.34 FPM
Server version: Apache/2.4.46 ()

If I post a multipart/form-data with Content-Length defined it works, however if I post a multipart/form-data with Transfer-Encoding: chunked only, PHP doesn't get any of the _PÖST or _FILES variables.


OST /upload.php HTTP/1.1
content-type: multipart/form-data; boundary=--------------------------657923799994993493862187
Content-Length: 2772
Connection: close

Content-Disposition: form-data; name="my_field"^M
Content-Disposition: form-data; name="my_logo"; filename="logo.png"^M
Content-Type: image/png^M

POST /upload.php?type=win HTTP/1.1
content-type: multipart/form-data; boundary=--------------------------701658193841349554663905
Connection: close
Transfer-Encoding: chunked

Content-Disposition: form-data; name="my_field"^M
Content-Disposition: form-data; name="my_logo"; filename="logo.png"^M
Content-Type: image/png^M
 [2021-04-28 09:35 UTC] mdiss at talk21 dot com

Apache's bug for this is here and has apparently been fixed in Apache/2.4.47!  Not had chance to test it yet.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 17 04:01:28 2024 UTC