nginxscgi

nginx returning netstring with wrong length?


I installed nginx (nginx version: nginx/1.7.9) via macports on my mac running the latest OSX.

I configured a URI to use SCGI:

location /server {
    include /Users/ruipacheco/Projects/Assorted/nginx/conf/scgi_params;
    scgi_pass unix:/var/tmp/rpc.sock;
    #scgi_pass 127.0.0.1:9000;
}

And when I do a GET request on 127.0.0.1/server, I see the following on my SCGI server:

633:CONTENT_LENGTH0REQUEST_METHODGETREQUEST_URI/serverQUERY_STRINGCONTENT_TYPEDOCUMENT_URI/serverDOCUMENT_ROOT/opt/local/htmlSCGI1SERVER_PROTOCOLHTTP/1.1REMOTE_ADDR127.0.0.1REMOTE_PORT62088SERVER_PORT80SERVER_NAMElocalhostHTTP_HOST127.0.0.1HTTP_CONNECTIONkeep-aliveHTTP_CACHE_CONTROLmax-age=0HTTP_ACCEPTtext/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8HTTP_USER_AGENTMozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36HTTP_DNT1HTTP_ACCEPT_ENCODINGgzip, deflate, sdchHTTP_ACCEPT_LANGUAGEen-US,en;q=0.8,End of file

The problem is that the length of the netstring, 633, does not match the interpretation. If I understand the netstrings spec correctly, 633 should be the length of characters between the first : and the last ,:

Any string of 8-bit bytes may be encoded as [len]":"[string]",". Here [string] is the string and [len] is a nonempty sequence of ASCII digits giving the length of [string] in decimal. The ASCII digits are <30> for 0, <31> for 1, and so on up through <39> for 9. Extra zeros at the front of [len] are prohibited: [len] begins with <30> exactly when [string] is empty.

For example, the string hello world! is encoded as 31 32 3a 68 65 6c 6c 6f 20 77 6f 72 6c 64 21 2c, i.e., 12:hello world!,.

So, I'm getting the wrong length. How can this be explained?


Solution

  • As far as I can tell, your example response has correct length.

    According to example here: http://en.wikipedia.org/wiki/Simple_Common_Gateway_Interface

    Field values are preceded and followed by <00> symbol (ASCII symbol with hex code 00), eg.:

    REQUEST_METHOD <00>GET<00>
    

    Once I added missing spaces to your response snippet – it quickly got back to 633 bytes, as advertised.

    I suppose somewhere in the process of passing that response to us here, some piece of software stripped <00>'s, which is a totally normal behaviour?

    Anyway, the answer seems to be – your nginx is either returning a correct length, or your response is stripping <00>'s somewhere.