rstudio/rstudio — pull request #186 by jcheng5 (2014-10-29 04:37:32+00:00)
Title: Fix RPubs upload error "invalid 'n' argument"

Fixes a pretty bad bug where 25-75% of RPubs publish attempts seem to fail with this error message.

It seems to have started sometime in the last few weeks, probably when I revamped the RPubs production servers after shellshock. The production servers started returning some (but not all) HTTP responses using [chunked transfer encoding](http://en.wikipedia.org/wiki/Chunked_transfer_encoding):

> Chunked transfer encoding is a data transfer mechanism in version 1.1 of the Hypertext Transfer Protocol (HTTP) in which data is sent in a series of "chunks". It uses the Transfer-Encoding HTTP header in place of the Content-Length header, which the earlier version of the protocol would otherwise require.[1] Because the Content-Length header is not used, the sender does not need to know the length of the content before it starts transmitting a response to the receiver. Senders can begin transmitting dynamically-generated content before knowing the total size of that content.

> The size of each chunk is sent right before the chunk itself so that the receiver can tell when it has finished receiving data for that chunk. The data transfer is terminated by a final chunk of length zero.

The readResponse function was expecting every response to have a Content-Length, so it would choke on chunked responses. This is easily fixed in the case of method="curl", as curl handles decoding the chunks for us; we just need to know not to look for a Content-Length, no big deal. In the case of method="internal" though, we need to do the chunk decoding ourselves. Still not too big of a deal, we don't have to worry too much about being fast or robust here as it's <100 bytes of pure-ASCII JSON that's being returned in every case.