Recent

Author Topic: Any change to synapse - HttpPostFile suddenly does not work  (Read 564 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1196
Hi,
I have been using HttpPostFile quite reliably (for uploading dataset).  But suddenly it does not work.
I simplified things as follows.


Server side:


Code: Pascal  [Select][+][-]
  1. procedure TwmAQe.SetDataSetRequest(Sender: TObject; ARequest: TRequest;
  2.    AResponse: TResponse; var Handled: Boolean);
  3. begin
  4.    if ARequest.Files.Count >= 1 then begin
  5.      AResponse.Content := 'File found';
  6.      Handled := True;
  7.    end
  8.    else begin
  9.       AResponse.Content := 'No file attached';
  10.       Handled := True;
  11.    end;
  12. end;
  13.  

Client side:

Code: Pascal  [Select][+][-]
  1. function SetDataSet(querydef, fieldname, filename: string;DataSet:TDataSet): Boolean;
  2. var
  3.    turl: string = 'http://myurl/SetDataSet';
  4.    AStrm: TStream;
  5.    AStrl : TStringList;
  6. begin
  7.    // In original application, DataSet is saved to AStrm and uploaded. But I simplified.
  8.    Result:= false;
  9.    AStrl := TStringList.Create;
  10.    AStrm := TMemoryStream.Create;
  11.    // AStrm := TFileStream.Create ('d:\temp\temp.txt', fmOpenRead);
  12.    try
  13.       AStrm.Size := 0;  // This works, but not if I use TFileStream
  14.       Astrm.Position:= 0;
  15.       Result := HttpPostFile(tURL, fieldname, filename, AStrm, AStrl);
  16.    finally
  17.      AStrl.Free;
  18.      AStrm.Free;
  19.    end;
  20. end;
  21.  

If I define AStrm.Size := 0, then 'No file attached' is returned to AStrl. But when I use TFileStream then 500 error - the server even does not call SetDataSetRequest.

Webserver is running on Windows Server 2019 Standard, and client is run on Windows 11. 
Lazarus v2.2.4.

« Last Edit: March 25, 2023, 02:44:52 pm by egsuh »

Thaddy

  • Hero Member
  • *****
  • Posts: 13213
Re: Any change to synapse - HttpPostFile suddenly does not work
« Reply #1 on: March 25, 2023, 04:25:43 pm »
First determine the protocols used. Synapse is not very well maintained and older protocols are dropped by most server and client software. (SSL? or TLS 1.0? Those should not work, so then it works correct)
Synapse itself has not changed since many moons... But when used properly it should work.

Btw: fcl-web is closely related, maintained and usually even more reliable (and FPC standard).
« Last Edit: March 25, 2023, 04:30:36 pm by Thaddy »
I actually get compliments for being rude... (well, Dutch, but that is the same)

egsuh

  • Hero Member
  • *****
  • Posts: 1196
Re: Any change to synapse - HttpPostFile suddenly does not work
« Reply #2 on: March 25, 2023, 05:12:12 pm »
Thank you Thaddy. I thought of using THttpClient instead of synapse, and one problem was that I could not find way of uploading a stream, not a file. There must be a way but synapse was doing what is expected and is slightly faster than THttpClient. But speed is not a genuine issue. If uploading a memory stream instead of physical file then I’ll move to THttpClient.

Thaddy

  • Hero Member
  • *****
  • Posts: 13213
Re: Any change to synapse - HttpPostFile suddenly does not work
« Reply #3 on: March 25, 2023, 05:18:43 pm »
@egsuh

I will write you a small example, but not today (UTC +1 )
The ThttpClient is actually stream oriented, so you can also look at the examples.
I actually get compliments for being rude... (well, Dutch, but that is the same)

egsuh

  • Hero Member
  • *****
  • Posts: 1196
Re: Any change to synapse - HttpPostFile suddenly does not work
« Reply #4 on: March 27, 2023, 06:48:13 am »
My problem seems to be related with server side. It raises 500 error even tested with web browsers. What could be possible reasons? Failure to save files in the server, e.g. temporal file saving place is not allowed to write file, etc. --- But there were no problem and I did not change anything, except upgrading to Lazarus 2.2.4.

egsuh

  • Hero Member
  • *****
  • Posts: 1196
Re: Any change to synapse - HttpPostFile suddenly does not work
« Reply #5 on: March 27, 2023, 10:31:37 am »
My problem happens only with Windows Server 2019 server. When I tested it on my Windows 10 PC, there are no problems.

Thaddy

  • Hero Member
  • *****
  • Posts: 13213
Re: Any change to synapse - HttpPostFile suddenly does not work
« Reply #6 on: March 27, 2023, 10:32:00 am »
500 internal server error is usually caused by trying to connect with an insecure protocol.
The reason is always that, as can be seen that even browsers do not work.
You simply are using a wrong protocol. (maybe any ssl version or tls 1.0 )
It is not really an internal server error, it is used as a "catch all" for wrong protocols, so the server does not need to provide additional information. IOW the real information is dropped for security reasons.
I actually get compliments for being rude... (well, Dutch, but that is the same)

Thaddy

  • Hero Member
  • *****
  • Posts: 13213
Re: Any change to synapse - HttpPostFile suddenly does not work
« Reply #7 on: March 27, 2023, 10:33:51 am »
It should also happen with Windows 11 if 2019 server fails. That would also indicate legacy protocols.
I will test that against both while I am writing the promised example.
[edit]
Anything below tls 1.2 is dropped in newer Windows crypto. So it is indeed simply a protocol issue.
That means programmer error. Update your crypto libs. The pascal libraries are all OK.
See partially here: https://learn.microsoft.com/en-us/mem/configmgr/core/plan-design/security/enable-tls-1-2-client
« Last Edit: March 27, 2023, 10:46:43 am by Thaddy »
I actually get compliments for being rude... (well, Dutch, but that is the same)

egsuh

  • Hero Member
  • *****
  • Posts: 1196
Re: Any change to synapse - HttpPostFile suddenly does not work
« Reply #8 on: March 27, 2023, 11:39:13 am »
Thank you Thaddy.

Based on the link you presented, operating systems like Windows 10, Window Server 2019, Windows 11, etc. must be using TLS1.2.  Aren't they?

I found the reason. I opened a browser at the same machine as web server (Window Server 2019), and it displayed the error.

      Cannot create file 'c:\windows\temp\CGI00000.tmp',

which file already existed.

I deleted the CGI*****.tmp files and tried, and everything works fine. The numbers are added by 1 every time. 

Probably my re-starting of the server may have caused this problem, resetting the file index number to zero.

 

TinyPortal © 2005-2018