What happens when a HTTP request is terminated prematurely?

Suppose, I enter a URL in my browser and browser submits the HTTP request. The remote HTTP server accepts the request and initiates a long task to serve the request.

If I terminate the request before it is complete (for example, press Esc or in Firefox), how is the request closed? Will the browser communicate this abort request to the server (I think it doesn't)?

Presuming no, upon completion of the long task, what will the server do with the result? Does it send it back anyway? If it does, what will happen? Does it reach till my PC? Or gets lost on the way?

This is just for my curiosity.

Thanks for your time :)


Well, it all depends on what the server is doing. Typically, it won't "detect" the terminated request until a send is attempted. At that point, the script will get a user_abort message from the web server (in php, you can tell it to ignore the user aborts with ignore_user_abort(true);). Without attempting to send data to the client, there's no way for the server to know that the request was aborted.

When it tries to send the request to the closed TCP connection, what happens is completely dependent on the server software. Apache works like this: If the sent data is still while a dynamic script is processing, it will tell the script about the abort and let it handle it how it wants (PHP --by default-- terminates). If the script is done, or it's a static file request, it will just ignore the closed connection and return.