Recent

Author Topic: Limiting uploaded file size onto a web server  (Read 5164 times)

alex256

  • New Member
  • *
  • Posts: 24
Limiting uploaded file size onto a web server
« on: July 23, 2017, 09:35:21 pm »
Hello. I am wondering how to limit the size of file that is uploaded onto a web server (which is made using THTTPApplication)?

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Limiting uploaded file size onto a web server
« Reply #1 on: July 23, 2017, 09:53:02 pm »
Hello. I am wondering how to limit the size of file that is uploaded onto a web server (which is made using THTTPApplication)?
Keep account of buffers received...Disconnect....
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

alex256

  • New Member
  • *
  • Posts: 24
Re: Limiting uploaded file size onto a web server
« Reply #2 on: July 23, 2017, 09:56:38 pm »
Hello. I am wondering how to limit the size of file that is uploaded onto a web server (which is made using THTTPApplication)?
Keep account of buffers received...Disconnect....

Thanks, but how to do this?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Limiting uploaded file size onto a web server
« Reply #3 on: July 24, 2017, 06:17:11 am »
Quick glance at the code, there's no way to do that BEFORE accepting the whole multipart stream. Handle if afterwards instead, see TUploadedFile class members for inspiration.

alex256

  • New Member
  • *
  • Posts: 24
Re: Limiting uploaded file size onto a web server
« Reply #4 on: July 24, 2017, 11:51:25 am »
So, there is no way to do this without uploading the file to the server?  :(

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Limiting uploaded file size onto a web server
« Reply #5 on: July 24, 2017, 12:05:49 pm »
Quick glance at the code, there's no way to do that BEFORE accepting the whole multipart stream. Handle if afterwards instead, see TUploadedFile class members for inspiration.

What? Did you look at the inheritance? Socket level? Of course you can do that.... >:D

If the receiving socket receives too much, you quit. It is the socket that has to count.... not a higher level....
« Last Edit: July 24, 2017, 12:07:27 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Limiting uploaded file size onto a web server
« Reply #6 on: July 24, 2017, 12:13:16 pm »
So, there is no way to do this without uploading the file to the server?  :(
Well of course there is, by subclassing the relevant class(es) and handle that at the point you want. Somewhere in TRequest.ProcessMultiPart or TMimeItems.FormSplit. Normally we can only limit the whole request size (using web server feature in case of 3rd party) instead of just one field, because multipart data has no explicit size for each field (but can be calculated from boundary).

alex256

  • New Member
  • *
  • Posts: 24
Re: Limiting uploaded file size onto a web server
« Reply #7 on: July 24, 2017, 02:41:03 pm »
Thank you very much, I will try :)

alex256

  • New Member
  • *
  • Posts: 24
Re: Limiting uploaded file size onto a web server
« Reply #8 on: July 24, 2017, 06:26:58 pm »
Finally I got to make it work. I overloaded TMimeItems.CreateUploadFiles not to allow big files. But in TMimeItems.CreateUploadFiles they are already loaded into the memory. So I also added the request size limit inheriting many classes and overriding creation methods. The code is:

Code: Pascal  [Select][+][-]
  1. unit mycusthttp;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, custhttpapp, HTTPDefs, fphttp, fphttpserver, custweb, ssockets;
  9.  
  10. type
  11.  
  12.   { TMyMimeItems }
  13.  
  14.   TMyMimeItems = class(TMimeItems)
  15.   public
  16.     procedure CreateUploadFiles(Files: TUploadedFiles; Vars: TStrings); override;
  17.   end;
  18.  
  19.   { TMyHTTPConnection }
  20.  
  21.   TMyHTTPConnection = class(TFPHTTPConnection)
  22.   protected
  23.     procedure ReadRequestContent(ARequest: TFPHTTPConnectionRequest); override;
  24.   end;
  25.  
  26.   { TMyHttpServer }
  27.  
  28.   TMyHttpServer = class(TEmbeddedHttpServer)
  29.   protected
  30.     function CreateConnection(Data: TSocketStream): TFPHTTPConnection; override;
  31.   end;
  32.  
  33.   { TMyHTTPServerHandler }
  34.  
  35.   TMyHTTPServerHandler = class(TFPHTTPServerHandler)
  36.   protected
  37.     function CreateServer: TEmbeddedHttpServer; override;
  38.   end;
  39.  
  40.   { TMyCustomHTTPApplication }
  41.  
  42.   TMyCustomHTTPApplication = class(TCustomHTTPApplication)
  43.   protected
  44.     function InitializeWebHandler: TWebHandler; override;
  45.   end;
  46.  
  47. implementation
  48.  
  49. { TMyMimeItems }
  50.  
  51. procedure TMyMimeItems.CreateUploadFiles(Files: TUploadedFiles; Vars: TStrings);
  52. var
  53.   I: integer;
  54. begin
  55.   for I := 0 to Count - 1 do
  56.   begin
  57.     if (Items[I] as TMimeItem).DataSize > 1024 then
  58.     begin
  59.       Vars.Add('success=false');
  60.       Exit;
  61.     end;
  62.   end;
  63.   Vars.Add('success=true');
  64.   inherited CreateUploadFiles(Files, Vars);
  65. end;
  66.  
  67. { TMyHTTPConnection }
  68.  
  69. procedure TMyHTTPConnection.ReadRequestContent(ARequest: TFPHTTPConnectionRequest);
  70. var
  71.   L: integer;
  72. begin
  73.   L := ARequest.ContentLength;
  74.   if L > 65536 then
  75.     raise EFPHTTPError.Create('Request too big!');
  76.   inherited ReadRequestContent(ARequest);
  77. end;
  78.  
  79. { TMyHttpServer }
  80.  
  81. function TMyHttpServer.CreateConnection(Data: TSocketStream): TFPHTTPConnection;
  82. begin
  83.   Result := TMyHTTPConnection.Create(Self, Data);
  84. end;
  85.  
  86. { TMyHTTPServerHandler }
  87.  
  88. function TMyHTTPServerHandler.CreateServer: TEmbeddedHttpServer;
  89. begin
  90.   Result := TMyHttpServer.Create(Self);
  91. end;
  92.  
  93. { TMyCustomHTTPApplication }
  94.  
  95. function TMyCustomHTTPApplication.InitializeWebHandler: TWebHandler;
  96. begin
  97.   Result := TMyHTTPServerHandler.Create(Self);
  98. end;
  99.  
  100. initialization
  101.   MimeItemsClass := TMyMimeItems;
  102.  
  103. end.
  104.  

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: Limiting uploaded file size onto a web server
« Reply #9 on: July 24, 2017, 10:31:29 pm »
keep in mind that you cannot control how many bytes the client will send and most of the browsers and http libs will not check the socket state when sendind so even you close the socket gracefuly with shutdown the clent will hang by timeout
Speak postscript or die!
Translate to pdf and live!

 

TinyPortal © 2005-2018