benrady / shinatra

A simple web server in 5 lines of bash
811 stars 81 forks source link

Update shinatra.sh #16

Open jojenki opened 6 years ago

jojenki commented 6 years ago

There's no value in keeping it open other than causing some browsers and commands to hang. This will force-close the connection after the response is sent.

benrady commented 6 years ago

I think you're right, but what is the purpose of adding the -N option? That's not available in BSD netcat.

jojenki commented 6 years ago

-N tells netcat to close the stream after it sends the message. On Debian, netcat would hold the connection open, even without the "Connection: keep-alive" header and even with a "Connection: close" header. (The latter is just a heads-up to the client and not something netcat should be expected to handle anyway.)

I see the "-N" in OpenBSD. Which distro are you not seeing it in?

http://man.openbsd.org/nc

benrady commented 6 years ago

I'm looking at version 1.105-7 on Ubuntu 16.04.3 from the netcat-openbsd package. Also didn't see it on my Mac's netcat version but I don't remember where that came from.

benrady commented 6 years ago

Thanks for the PR. I really have no specific memory of why that header is there, so I want to make sure I understand what's going on before changing anything.

Re-reading the RFC, I don't think it's possible to handle all the requirements without adding a lot of logic to the script (which I'm definitely not willing to do...it takes the punchline out of the joke).

So my first question is, if not all versions of netcat support the -N option, wouldn't you want Connection:keep-alive to give a hint to the clients that they need to close the connection because the server won't? Persistent connections are the default in HTTP 1.1.

Secondly, are there any specific clients/browsers where you've seen requests hang? What Connection headers do they send?

Thanks again!

bendem commented 3 years ago

wouldn't you want Connection:keep-alive to give a hint to the clients that they need to close the connection because the server won't?

Actually, adding keep-alive tells the client to keep the connection open so they can re-use it, if you want the browser to close the connection because you aren't going to do it yourself, you need to pass close, then -N is not required.

benrady commented 3 years ago

Have you tried that yet? When I tried making that change it didn't seem to make a difference with Chrome or Firefox.

On Thu, Aug 20, 2020 at 8:17 AM bendem notifications@github.com wrote:

wouldn't you want Connection:keep-alive to give a hint to the clients that they need to close the connection because the server won't?

Actually, adding keep-alive tells the client to keep the connection open so they can re-use it, if you want the browser to close the connection because you aren't going to do it yourself, you need to pass close, then -N is not required.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/benrady/shinatra/pull/16#issuecomment-677658744, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAN4YBWMBBMEBKIHY7VKV3SBUO55ANCNFSM4EZDT2WQ .

-- Ben

bendem commented 3 years ago

Have you tried that yet? When I tried making that change it didn't seem to make a difference with Chrome or Firefox.

I have not, but I have read the latest RFC, first point of persistent connection, if anybody sets connection to close, the exchange ends with the connection closed: https://tools.ietf.org/html/rfc7230#section-6.3

I get weird behaviour with chrome/firefox and edge, but it's mostly due to the trailing double line return after the response body (edge hangs without it, firefox and chrome hang when it's present).