This project has moved and is read-only. For the latest updates, please go here.

how to abort a request (so that its body is not sent)

Oct 9, 2015 at 1:31 PM
Edited Oct 9, 2015 at 1:32 PM
Hi All,

A bit of context:

My service exposes an http/https endpoint. Users can issue POST requests to this endpoint and upload their payload there (let's say each request is around 4MB).


Sometimes I would like to drop requests based on http headers (e.g. a user breached quota, so he/she is not allowed to upload any more). And here is my key requirement: in this case I would like my servers not to receive these 4MB of payload (no traffic sent to their network adapters).


Is there any way I can achieve this with Katana?

I know that I can do this with raw HTTP Server API (http.sys), e.g. like this:
HttpListenerContext context = Listener.GetContext();
// some logic here

Unfortunately I can't see similar Abort() call in Katana.

Also, maybe you can suggest any other way of achieving my goal?

What I've already tried:

I've already tried various approaches but none of them works:
  • 100-continue header (this is fully optional optimization)
  • sending a response with failure status code and closing connection ("Connection" header)
  • closing all streams etc.
An all cases entire POST body was uploaded to my server. The only solution that works (connection is closed without uploading the payload) is with Abort(), but this is no available in Katana :(

Oct 12, 2015 at 12:49 PM
ok, it seems I found a solution (I skipped null checks, etc. for readability):
            HttpRequestMessage requestMessage = ...
            OwinContext owinContext = requestMessage.Properties["MS_OwinContext"] as OwinContext;
            HttpListenerContext httpListenerContext = owinContext.Environment["System.Net.HttpListenerContext"] as HttpListenerContext;
Oct 12, 2015 at 4:06 PM
That should work. If you control the client then there's one other thing you could try. The problem is that per the protocol the client is honor bound to send you what they said they would. E.g. If they said content-length 4mb then they need to send that. However some clients are clever enough to send transfer-encoding chunked instead, and then if they get back a 4xx+ status code instead of a 100 they can just send a chunked terminator without the whole body.