Search code examples
c#asp.netiisconnectionhttpmodule

Closing connection to browser early and returning respond status


I am implementing a custom IHttpModule in a project, which filter file uploading and provide upload status. I want the custom module to close the connection early (don't want to receive all the data the other side send, which is allowed to be big, save the incoming bandwidth).

Is it a possibility using HTTP protocol?

I tried sending "ContentLength: 0" and "Connection: close" header, flush the response, close the connection (HttpWorkerRequest.CloseConnection).

workerRequest.SendStatus(400, "Bad request");
workerRequest.SendKnownResponseHeader(HttpWorkerRequest.HeaderContentLength, "0");
workerRequest.SendKnownResponseHeader(HttpWorkerRequest.HeaderConnection, "close");
workerRequest.FlushResponse(false);
workerRequest.CloseConnection();
((HttpApplication)sender).CompleteRequest();

The browser/firefox response is displaying the "Connection reset" error message. Without any sign of receiving any data/header/status from the server.

Thanks in advance.


Solution

  • An http client expects response only after it has completed sending the data. You should modify the client to make multiple requests of small sizes so a response can be sent to it instead of having server close the connection (which will not work and isn't working as you've tested already).

    Check the http handler implemented in http://slfileupload.codeplex.com/ for a sample of how it is typically done.