"Transfer-Encoding: Chunked" Header in PHP

“Transfer-Encoding: chunked” header in PHP. What is padding for?

The padding is for filling the server buffer, as I understand.

Without it the server will wait until PHP will fill it and after this will flush it - even if in PHP code you do flush().

Related:

  • PHP Output buffer flush issue on Apache/Linux (May 2010; on Server Fault)

Transfer-Encoding: chunked sent twice (chunk size included in response body)

As @Joe pointed out before, that is the normal behavior when Chunked transfer enconding is enabled. My tests where not accurate because I was requesting Apache directly on the server. Actually, when I was experiencing the problem in Chrome I was querying a Nginx service as a proxy for Apache.

By running tcpdump I realized that Nginx was rechunking responses, but only when rewritting HTTP status header (header('HTTP/1.1 200 OK')) in PHP. The solution to sending Transfer-Encoding: chunked twice is to set chunked_transfer_encoding off in the location context of my Nginx .php handler.

PHP: How to read POST body with Transfer-Encoding: chunked, no Content-Length

Might as well write this as an answer I suppose.

The issue you describe is documented here: https://bugs.php.net/bug.php?id=60826

Highlights from the thread:

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 :

The same configuration with PHP running as mod_php is fine, the body
request is available through php://input.

And

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.

Possible Workarounds:

0) Use httpd with mod_php

1) Use a different web server.

2) Look at patches for mod_fcgid

3) Maybe (not recommended) flip always_populate_raw_post_data and see if there is anything in $HTTP_RAW_POST_DATA

PHP Audio transfer with chunked encoding

Android prior to 2.3 does not support chunked encoding for its own player. Other players that implement their own clients may also have issues. I have not tested iOS. Chunked encoding is also problematic for many desktop streaming clients as well. It's best not to use it to maximize compatibility.

There are two ways to do this. The first is to return an HTTP/1.0 response. This means that you do not have to specify a content length, and the response stops when the server stops sending it.

The second way is similar to the first, but allows you to work with HTTP/1.1. Just set the header Connection: close. Then, you do not have to specify content length, and do not have to send your data in chunked encoding.

POST request to PHP7 with chunked encoding does not properly return result

It is/was a bug in PHP7 which was fixed very recently with https://github.com/php/php-src/pull/1745 . It is not yet within any official release but eventually will end up there some time.

Before the above PR, a bug was also reported describing a similar issue: https://bugs.php.net/bug.php?id=71466

Reading php://input from a request that is using Transfer-Encoding: chunked

In my scenario, this is due to the bug pointed out by @Phil in the comments

This specifically affects php-fpm, and for most users they can upgrade Apache to 2.4.46+.

Bug: https://bz.apache.org/bugzilla/show_bug.cgi?id=57087

If you're a Debian user, as of writing this; there isn't an official stable release for 2.4.46 yet, and it is in testing phase as shown here. We fell-back back to mod_php instead while we wait for a stable release, others may be inclined to use Nginx instead.

EDIT Debian has now released a stable version where this is fixed, it only took them 10 years, and the timing couldn't have been better.

Why Transfer-Encoding request header does not interpret correctly?

See this example in the documentation:

You send a chunked POST with curl like this:

curl -H "Transfer-Encoding: chunked" -d "payload to send" http://example.com

You don't need to try to create the chunked encoded payload yourself. In your case:

curl --data "user=mehran" --header "Transfer-Encoding: chunked" "http://172.16.17.10/pg3.php"

is enough for curl to send:

POST / HTTP/1.1
...
Transfer-Encoding: chunked
Content-Type: application/x-www-form-urlencoded

b
user=mehran
0



Related Topics



Leave a reply



Submit