Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Transfer-Encoding: chunked" header in PHP

Tags:

http

php

header

i want to add Transfer-Encoding: chunked header to the file that i'm outputing (its just generated plain text), but when i add:

header("Transfer-Encoding: chunked");
flush();

the browser doesn't want to open the file.

The webpage at ... might be temporarily down or it may have moved permanently to a new web address.

what i need to do for it to work?

like image 538
Ddwerffdsf Avatar asked Jan 26 '11 20:01

Ddwerffdsf


People also ask

How do I set Transfer-Encoding chunked?

To enable chunked transfer encoding, set the value for AspEnableChunkedEncoding to True for the site, the server, or the virtual directory that you want to enable chunked transfer encoding for: Open a command prompt. Change to the Inetpub\Adminscripts folder.

What is the use of Transfer-Encoding chunked?

Chunked transfer encoding allows a server to maintain an HTTP persistent connection for dynamically generated content. In this case, the HTTP Content-Length header cannot be used to delimit the content and the next HTTP request/response, as the content size is not yet known.

How do you stop Transfer-Encoding chunked?

Try adding "&headers=false" to your request. That should shorten it up and cause the response to be less likely to be chunked. Also, are you sending a HTTP/1.1 or HTTP/1.0 request? Try sending a HTTP/1.0 if your device cannot handle a HTTP/1.1 request.


1 Answers

You need to send the Content-Length with every chunk you send. Look at Wikipedia for a first impression, how a chunked encoding looks like. Its not that trivial and in many cases its oversized.

Update: First you send the headers, because they must always send before any content (also with chunked encoding). Then you send (for every chunk) the size (in hexadecimal) followed by the content. Remember flush() after every chunk. At last you must send a zero-size chunk to make sure, that the connection get closed properly.

Its not tested, but something like this

header("Transfer-Encoding: chunked");
echo "5\r\n";
echo "Hello";
echo "\r\n\r\n";
flush();
echo "5\r\n";
echo "World";
echo "\r\n";
flush();
echo "0\r\n\r\n";
flush();
like image 193
KingCrunch Avatar answered Sep 25 '22 10:09

KingCrunch