I'm building a web server from scratch for fun and I am noticing some strange behaviors from Chrome. The request I get from Chrome for a GET request to /
looks like this:
GET / HTTP/1.1
Host: localhost:8000
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
DNT: 1
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Nothing special, but it also make another request to my server and it doesn't send anything, which results in my server waiting for an incoming message forever.
This behavior is not found with Safari. I have set the server to respond with 403 to every single request, but the persistent connection remains. What is the purpose of this connection and how should I handle it? Am I missing something from the HTTP protocol?
HTTP/1.1 browsers tend to open multiple TCP connections. If your server can only act upon one connection at a time, you might end up in a deadlock. You should either use threads or nonblocking IO (select
) to handle each connection when it has a request ready.
Chrome is probably opening an additional socket so that it's ready if the first one closes, but I'm not sure. HTTP/1.1 has no way to cancel a request/response, other than closing the socket, and it's also possible that the server would send Connection: close
after finishing the response, so the browser is preparing itself for the future requests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With