camenoj
camenoj

Reputation: 11

Synapse HTTPServ Delphi 7 Issue

I'm trying to do the following with the readily available Synapse HTTPServ example in Delphi 7.

1) Generate a simple HTML page with upload form field
2) When the user fills in the field and clicks the button, the name of the
       file is sent back to HTTPServ.
3) "get" the file and save locally
4) inspect the file
5) send back response with what was found

Sounds easy..on paper.

In the http.pas file in the TTCPHttpThrd.Execute procedure, it shows the following code.

//read request headers
If protocol <> '' then
Begin
  If pos('HTTP/', protocol) <> 1 Then Exit;
  If pos('HTTP/1.1', protocol) <> 1 Then close := true;
  Repeat
    s := sock.RecvString(Timeout);
    If sock.lasterror <> 0 Then Exit;
    If s <> '' Then Headers.add(s);
    If Pos('CONTENT-LENGTH:', Uppercase(s)) = 1 Then Size := StrToIntDef(SeparateRight(s, ' '), -1);
      // size ALWAYS returns 0 - CONTENT-LENGTH NOT IN HEADER!
    If Pos('CONNECTION: CLOSE', Uppercase(s)) = 1 Then close := true;
      // Present in header
  Until s = '';
End;

// since size ALWAYS = 0 this never gets executed to "get" the file..
InputData.Clear;
If size >= 0 Then
Begin
  InputData.SetSize(Size);
  x := Sock.RecvBufferEx(InputData.Memory, Size, Timeout);
  InputData.SetSize(x);
  If sock.lasterror <> 0 Then Exit;
End;
OutputData.Clear;
ResultCode := ProcessHttpRequest(method, uri);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ResultCode gets the result of the following function and according to the author comments, InputData should hold the uploaded file.

It doesn't - obviously because size ALWAYS = 0.

function TTCPHttpThrd.ProcessHttpRequest(Request, URI: string): integer;
var
  l: TStringlist;
begin
 // sample of precessing HTTP request:
 // InputData is uploaded document, headers is stringlist with request headers.
 // Request is type of request and URI is URI of request
 // OutputData is document with reply, headers is stringlist with reply headers.
 // Result is result code

Headers never contains the CONTENT-LENGTH field.

The form being used is about as simple as it can get:

<form name="getfile" action="http://127.0.0.1:80" method="get">
  <input type="file" name="uploadedfile" />
  <input type="submit" value="Submit" />
</form>

Any thoughts on why this might be?

Upvotes: 0

Views: 639

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595981

Your HTML webform cannot use method="get" to upload a file, it must use method="post" instead. An HTTP GET request does not carry any body data, but POST does.

Upvotes: 1

Related Questions