Search code examples
httpnginxcontent-typefastcgiplaintext

nginx + fcgiwrapper sporadic Prblem: Delivers application/octet-stream instead of text/plain


I am new to this forum. This is my first question.

I have a nginx-server + fcgiwrapper set up to run Programs on user request (no PHP).

For testing I have a simple bash script, which displays the environment variables and sets two cookies, a second bash-script prints "Hello World" as text/plain and another bash-script prints "Hello World" as text/html.

Another Program written in C is supposed to read Text from stdin, parse it and print Text based in the input to stdout, which the should be displayed as text/plain in the requesting webbrowser. (the requesting browser needs to use POST).

However sometimes it displays the returned text as "text/plain" (which it should do), but sometimes the browser wants to download the returned text, as if it was "application/octet-stream".

But, if I test the C-Program in a prepared Environment

Environment Variables:
CONTENT_LENGTH=30
REQUEST_METHOD=POST
HTTP_COOKIE=NAME=TEST; ID=200

it works every time, shows no errors and at the beginning it prints:

Content-type: text/plain (plus two newlines)

I have found that depending on the contents length it sometimes works and sometimes doesn't. (This only happens when the program is started through a webbrowser.) In Firefox, using the dev-tools, I could see that the answers Content-type was

application/octet-stream

and if I save it, it turns out to be a text file which contains the text that should have been displayed in the browser directly. What am I doing wrong?

Edit: I have already searched for similar problems with no success + all other things work perfectly + This also happens with different browsers (epiphany, lynx, internet explorer on Windows)


Solution

  • Through trial and error (with curl + firefox-dev-tools) I have found, that the character

    0x11

    in combination with: Content-type: text/plain
    makes nginx deliver Content-type: application/octet-stream .

    I don't know why this happens, but i found that the C-program produces the error, because it prints 0x11 or ^Q or dc1. This phenomenom also happens with files containing this character.