Recent

Author Topic: [AGREED] processing web forms using mod_pascal is OK, but file-upload is fail  (Read 703 times)

ADMGNS

  • New Member
  • *
  • Posts: 43
  • keep it simple and smart
hello

i,m trying to understand the behind of cgi mechanism from ground with mod_pascal.. i,ve challenged most of things, but, excep file upload..

how client side sends file data while uploading via web forms?? and how i can handle this streaming data using pascal (via mod_pascal)?

thanks
« Last Edit: June 11, 2025, 08:00:22 am by ADMGNS »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8817
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: processing web forms using mod_pascal is OK, but file-upload is fail
« Reply #1 on: June 09, 2025, 07:21:43 pm »
how client side sends file data while uploading via web forms??
Here's the partial raw data of such request:
Code: [Select]
Content-Type: multipart/form-data; boundary=---------------------------9051914041544843365972754266
Content-Length: somenumber

-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="file1"; filename="a.txt"
Content-Type: text/plain

Content of a.txt.

-----------------------------9051914041544843365972754266
The content of the uploaded file will be after the Content-Type and before the boundary, both of which have empty lines between the content.
and how i can handle this streaming data using pascal (via mod_pascal)?
Should be the same as in other fcl-web backends, read my tutorial.
Just ensure in either httpd.conf / apache2.conf (no idea what your Apache distribution uses), you have LimitRequestBody set to a reasonable number which can be inside <Directory>, <Location>, or <VirtualHost> block.
The default is 0 (unlimited), but this can vary from distribution to distribution. Sometimes it's implicitly overriden via mod_security, so be sure to check that one out.

zamronypj

  • Full Member
  • ***
  • Posts: 140
    • Fano Framework, Free Pascal web application framework
Re: processing web forms using mod_pascal is OK, but file-upload is fail
« Reply #2 on: June 10, 2025, 12:29:44 am »
To tell browser that you are sending file upload, add attribute enctype="multipart/form-data" 

Code: Text  [Select][+][-]
  1. <form enctype="multipart/form-data">
  2. ...
  3. </form>

To read uploaded file, you need to read request body from STDIN, but you need to parse multipart/form-data yourself and read binary data of file content. Read RFC 7578 https://datatracker.ietf.org/doc/html/rfc7578 for more information.

Code: Pascal  [Select][+][-]
  1. uses
  2.     sysutils;
  3.  
  4. var
  5.     contentLen : integer;
  6.     requestBody : string;
  7.     ch : char;
  8. begin
  9.     writeln('Request body:');
  10.     contentLen := strToInt(getEnvironmentVariable('CONTENT_LENGTH'));
  11.     if contentLen <> 0 then
  12.     begin
  13.         requestBody := '';
  14.         repeat
  15.             read(ch);
  16.             requestBody := requestBody + ch;
  17.             dec(contentLen);
  18.         until contentLen = 0;
  19.         writeln(requestBody);
  20.     end;
  21. end.
« Last Edit: June 10, 2025, 01:10:06 am by zamronypj »
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Apache module executes Pascal program like scripting language https://zamronypj.github.io/mod_pascal/
Github https://github.com/zamronypj

ADMGNS

  • New Member
  • *
  • Posts: 43
  • keep it simple and smart
Re: processing web forms using mod_pascal is OK, but file-upload is fail
« Reply #3 on: June 10, 2025, 04:04:41 am »
hello there

thank you for your kind replies..

"streaming data" means, when we trying upload very big files, is incoming data. i mean, does the client send partial data of file or whole file at once ?

regs

Leledumbo

  • Hero Member
  • *****
  • Posts: 8817
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: processing web forms using mod_pascal is OK, but file-upload is fail
« Reply #4 on: June 10, 2025, 07:19:19 am »
"streaming data" means, when we trying upload very big files, is incoming data. i mean, does the client send partial data of file or whole file at once ?
All options are possible, depending on the server. If they allow unlimited connection time, whole file at once is possible. Otherwise, there are three options for partial uploading:
  • Transfer-Encoding: chunked
  • S3 style multipart upload
  • WebSockets / gRPC
The first one is standardized. You just sent chunks until the closing chunk (0\r\n\r\n).

The second one is an application level protocol, which means the server must be coded specifically to handle this. The common flow is:
  • Client initiate request, supplying filename
  • Server responds with an ID
  • Client hits multiple requests, each representing a part of the file, supplying part number and the ID from the response
  • Server responds with another ID, usually an ETag, for each successfully uploaded part
  • Client completes the request by sending an XML request with a part number - ETag pair, for confirmation
  • Server constructs the complete file based on the XML information and send response with whatever considered useful
Of course you can omit the completion request, stating the number of parts at the initial request, or maybe change the XML to JSON, works, too. It's application level, meaning you're in control.

The third and the last one is like a pure socket, the protocol is bidirectional so through the same "tunnel" you can pass data as you wish continuously. Each has different implementation, though, so feel free to read and understand how each works.

ADMGNS

  • New Member
  • *
  • Posts: 43
  • keep it simple and smart
Re: processing web forms using mod_pascal is OK, but file-upload is fail
« Reply #5 on: June 11, 2025, 02:00:52 am »
thank you for your informative and clarify reply (or may i say response ;)

i,m always wondering how some file-sharing sites implement these mechanisms to upload very big files, for example, megaupload, wetransfer, etc.. because, in my experiments, upload operations fail and raise timeout error generally at server-side or sometimes at client-side..

so.. i must study hard and deeply a bit on kind topics and some details..;) the fact that, in order to fully learn something, you have to sit down and do it all over again from ground.. on other ways, it always remains unexpected gray zones..

regs

Leledumbo

  • Hero Member
  • *****
  • Posts: 8817
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: processing web forms using mod_pascal is OK, but file-upload is fail
« Reply #6 on: June 13, 2025, 04:28:13 am »
i,m always wondering how some file-sharing sites implement these mechanisms to upload very big files, for example, megaupload, wetransfer, etc.. because, in my experiments, upload operations fail and raise timeout error generally at server-side or sometimes at client-side..
You can always inspect what happens under the hood by opening your browser's developer tools, navigate to the Network tab, then do the big file upload. The request will be logged where you can inspect what's being sent and received.

 

TinyPortal © 2005-2018