Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question regarding browser behavior when a response is sent from a server

Scenario:

  • The browser submits a HTTP request to a server.
  • The user simultaneously clicks on a bookmark or on another link on the page resulting in a new request to the server.
  • The server now sends back two HTTP responses (or the browser gets responses from two servers).

How does the browser decide which of the responses to actually process?

I know what will happen - am trying to understand why. Any references or websites that explain this would also be much appreciated.

Thank you,

vivek.

Edit: Saw this similar question after asking. Please merge/delete if appropriate.

like image 858
Vivek Kodira Avatar asked Jun 01 '11 15:06

Vivek Kodira


1 Answers

The short answer to your specific question is that receiving a server's response (within a browser) is different from receiving a browser's request (within a server). When the browser opens a new connection to the server, what it's doing is creating a socket and then calling connect and send on that socket. When the server gets this incoming connection, it might not care if this is the same client as some previous connection. If does care (e.g. it has logged-in sessions or shopping carts) it has to use cookies or whatnot if to associate this connection with previous ones. (I'm ignoring persistent connections, which are beyond the scope of your question.)

But when the browser receives the response from the server, it does so by calling recv on the same socket that it used to send the request, so it knows which request that response goes with before it even starts reading it. In theoretical terms, the browser is maintaining state information about the connections it has open. In practical terms, it has a list or array of sockets.

The browser also keeps track of which windows and tabs are associated with which sockets. This is how it can update the spinners and status lines to reflect the status of the corresponding connections. And if the user clicks the stop button, it knows which socket (or sockets) to close.

So in your scenario, the user has clicked a link or bookmark in a window or tab associated with an existing socket representing a connection to a server where the server's response hasn't been received yet. The browser can simply close that socket as if the user had clicked the stop button. And even if it didn't close it, the browser knows the user no longer wants to see the response. Meanwhile it opens a new socket to the server the user is interested in.

like image 68
gatkin Avatar answered Jan 02 '23 17:01

gatkin