Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Chrome open a connection but not send anything

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?

like image 645
Derek 朕會功夫 Avatar asked Mar 08 '23 13:03

Derek 朕會功夫


1 Answers

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.

like image 119
Josh Lee Avatar answered Mar 17 '23 01:03

Josh Lee