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

。_饼干妹妹 提交于 2019-12-10 03:36:29

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6203552/question-regarding-browser-behavior-when-a-response-is-sent-from-a-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!