0

I want to know is it possible to stop connection if it wants to send large amount of data like big binary files to my http server? And if it is possible then how.

Imants Gulbis
  • 87
  • 1
  • 8
  • Duplicate of this? http://stackoverflow.com/questions/280684/size-of-uploaded-file. See for your answer the [1st answer there](http://stackoverflow.com/a/280702/1037511). (you can check the `Content-length`) – Rik Oct 10 '15 at 00:39
  • @Rik: assuming the client sends a `Content-Length` at all. It could use a `chunked` transfer encoding instead, or a MIME `multipart/form-data` post instead, both of which are self-terminating and do not use `Content-Length`. – Remy Lebeau Oct 10 '15 at 04:14
  • @RemyLebeau Yes, I know `Content-Length` is not required (it's also in that answer). That's why I linked to it. In that case you could just start reading and abort when the file gets too big. But your solution of sending a `411` is also (a nice) solution (unless you're dealing with a client-app which does do the chunked transfer encoding or MIME multipart/form-data post and can't change the way they send the file). – Rik Oct 10 '15 at 08:27
  • 1
    `TIdHTTPServer` does all of the reading, so in order to implement a read-and-abort approach, you need to create a custom `TStream` class that overrides `Write()` (or use `TIdEventStream` with an `OnWrite` event handler) to raise an exception when the limit is reached, then use the `TIdHTTPServer.OnCreatePostStream` event to provide an instance of that stream class during uploads. – Remy Lebeau Oct 10 '15 at 14:36
  • Ok this is actually very cool idea. I thing I will check Content-Length value and will implement my custom stream witch will block all attempts to upload more data than I allow – Imants Gulbis Oct 10 '15 at 16:02

1 Answers1

1

Yes, it is possible at times, but may not be possible all the time.

TIdHTTPServer has an OnHeadersAvailable event, which is triggered after the client's request headers have been read and before the request body is read. The event provides you access to the headers. You can check the value of the Content-Length header, and if it is not suitable for your needs then you can set the event's VContinueProcessing parameter to False to reject the request. If you want to customize the error message that is sent back to the client, use the OnHeadersBlocked event to provide your own ResponseNo, ResponseText, and ContentText values as needed.

Note that this only works if the client sends a Content-Length header. That is not strictly required, depending on the nature of the upload. The client could use a chunked transfer encoding, or a MIME multipart/form-data post, both of which are self-terminating formats that do not rely on the Content-Length header. If you want to insist that the client send a Content-Length header, you can reject the request with a 411 (Length Required) response code if the header is missing.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770