Chunks chunks chunks

If you happen to use cURL to fetch a remote webpage and return the data to a client, watch out for content-chunking (aka “chunked transfer-encoding“). cURL deals with the chunking seamlessly to return a single response to your php script, wherein lies the potential problem.

If you do what I did and pass all the response headers that came from the server along to the client, you’ll end up telling the client that the response is chunked, but not send the response the client is expecting. This will result in the client hanging while it waits for you to signal you’re done sending the chunk.

You have to wrap the response body as follows:

  • the length of the body, in hex, followed by a newline
  • the body, followed by a newline
  • a zero, followed by a newline
  • a newline

See this page for reference and an example, reproduced in a simplified form below:

[blank line here]