Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a http server detect that a client has cancelled their request?

My web app must process and serve a lot of data to display certain pages. Sometimes, the user closes or refreshes a page while the server is still busy processing it. This means the server will continue to process data for several minutes only to send it to a client who is no longer listening.

Is it possible to detect that the connection has been broken, and react to it?

In this particular project, we're using Django and NginX, or Apache. I assumed this is possible because the Django development server appears to react to cancelled requests by printing Broken Pipe exceptions. I'd love to have it raise an exception that my application code could catch. It appears JSP can do this. So can node.js here.

Alternatively, I could register an unload event handler on the page in question, have it do a synchronous XHR requesting that the previous request from this user be cancelled, and do some kind of inter-process communication to make it so. Perhaps if the slower data processing were handed to another process that I could more easily identify and kill, without killing the responding process...

like image 242
Nick Retallack Avatar asked Apr 16 '10 10:04

Nick Retallack


People also ask

How does HTTP detect the end of a request?

If the client sends a "Content-Length" header, the server must parse it and use it to determine the end of the request. If there was no such header but the "Transfer-Encoding: chunked" header was present, then the server must be able to parse a chunked request (link from mgiuca's answer).

Can HTTP request be Cancelled?

We can use the AbortController object and the associated AbortSignal with the Fetch API to make cancelable HTTP requests. Once the AbortSignal is sent, the HTTP request is canceled and won't be sent if the cancellation signal is sent before the HTTP request is done.

Can Web server send data without a request from client?

You can use web-sockets which are two way communication between client and server that remain open and allow the server to send without request from the client. However web-sockets are new and browser support is only for the most current browsers.

How does the Web server respond to a request?

A client sends a request, the server sends a response, the connection closes. You can experiment with HTTP by writing your own Protocol that accepts a connection, reads the request, and sends back an HTTP-formatted response.


1 Answers

While @Oded is correct that HTTP is stateless between requests, app servers can indeed detect when the underlying TCP/IP connection has broken for the request being processed. Why is this? Because TCP is a stateful protocol for reliable connections.

A common technique for .Net web apps processing a resource intensive request is to check Response.IsClientConnected (docs) before starting the resource intensive work. There is no point in wasting CPU cycles to send an expensive response to a client that isn't there anymore.

private void Page_Load(object sender, EventArgs e) {     // Check whether the browser remains     // connected to the server.     if (Response.IsClientConnected)     {         // If still connected, do work         DoWork();     }     else     {         // If the browser is not connected         // stop all response processing.         Response.End();     } } 

Please reply with your target app server stack so I can provide a more relevant example.

Regarding your 2nd alternative to use XHR to post client page unload events to the server, @Oded's comment about HTTP being stateless between requests is spot on. This is unlikely to work, especially in a farm with multiple servers.

like image 162
Steve Jansen Avatar answered Sep 28 '22 09:09

Steve Jansen