#1 2024-08-27 07:10:20

Terinel
Member
Registered: 2024-01-18
Posts: 8

Reception of files via http Post request does not work in multiple

Made a client-server application. The client sends files with an http Post request. The server accepts and saves files with a unique name in a special folder. But if you send a file from several machines, the message Socket error # 10061 Connection refused appears.

Offline

#2 2024-08-27 07:12:13

Terinel
Member
Registered: 2024-01-18
Posts: 8

Re: Reception of files via http Post request does not work in multiple

Here is the client code:

procedure TSendFile.SendFileToServer(const FileName: string; const ServerUrl: string);
var
  HTTP: TIdHTTP;
  Stream: TFileStream;
  Response: string;
begin
  HTTP := TIdHttp.Create(nil);
  try
    Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
    try
      HTTP.Request.ContentType := 'txt';
      HTTP.Request.ContentRangeStart := 0;
      HTTP.Request.ContentLength := Stream.Size;
      HTTP.Request.CharSet := 'utf-8';
      Response := HTTP.Post(ServerUrl, Stream);
      ShowMessage('Ответ сервера: ' + Response);
    finally
      Stream.Free;
    end;
  finally
    HTTP.Free;
  end;
end;


procedure TSendFile.ButtonSendClick(Sender: TObject);
var
  IdHTTP: TIdHTTP;
  FS: TFileStream;
  Bytes: TBytes;
  Base64String: string;
  JSON: TJSONObject;
  Response: string;
begin

SendFileToServer('C:\ubuntu-17.04-server-amd64.iso', '[url]http://192.168.0.63:8888/upload[/url]');


end;

end.

Last edited by Terinel (2024-08-29 06:26:33)

Offline

#3 2024-08-27 07:15:20

Terinel
Member
Registered: 2024-01-18
Posts: 8

Re: Reception of files via http Post request does not work in multiple

Here is the server code:

else if Ctxt.Url = '/upload' then
  begin
    try
      ContentType := SetContentTypeFromFileName(Ctxt.InContentType);
      Content := Ctxt.InContent;
      FileName := 'unknown' + Random(100).ToString;
      FileSize := Length(Content);

      UploadPath := CreateUploadFolder(Ctxt);
      try
      if not Length(Ctxt.InContent) = 0 then
          begin
            TSynLog.Add.Log(sllWarning, 'Client has not finished sending data, but the folder is being created', Self);
            ForceDirectories(UploadPath);
          end;
          SetLength(FileContentBytes, Length(Content));
          for u := 0 to Length(Content) - 1 do
          FileContentBytes u := Ord(Content u);
          try
            TSynLog.Add.Log(sllInfo, 'File processing started ' + Now.ToString + ' ' + FileName, Self);
            FProjectData.SaveToFile(FileName, content);
          except
            on E: Exception do
          begin
          Ctxt.OutContent := FormatUtf8('Error creating file: %s', [E.Message]);
          Ctxt.OutContentType := TEXT_CONTENT_TYPE;
          Result := HTTP_SERVERERROR;
          end;
        end;
        TSynLog.Add.Log(sllInfo, 'File processing completed ' + Now.ToString + ' ' + FileName, Self);
        Ctxt.OutContent := FormatUtf8('File uploaded successfully: %s (%d байт)', [FileName, FileSize]);
        Ctxt.OutContentType := TEXT_CONTENT_TYPE;
        Result := HTTP_SUCCESS;
      except
        on E: Exception do
        begin
          Ctxt.OutContent := FormatUtf8('Error creating file: %s', [E.Message]);
          Ctxt.OutContentType := TEXT_CONTENT_TYPE;
          Result := HTTP_SERVERERROR;
        end;
      end;
    except
        on E: Exception do
    end;
  end
  else
  begin
    Ctxt.RespStatus := HTTP_BADREQUEST;
    Result := HTTP_BADREQUEST;
  end;
end;

procedure TProjectData.SaveToFile(const pmcFileName: TFileName; Content: RawByteString);
begin
  FileFromString(Content, pmcFileName);
end;

...

constructor TSimpleHttpAsyncServer.Create;
begin
  inherited Create;
  fHttpServer := THttpAsyncServer.Create(
    '8888', nil, nil, '', SystemInfo.dwNumberOfProcessors + 1, 10000,
    [hsoNoXPoweredHeader,
     hsoNoStats,
     hsoHeadersInterning,
     hsoThreadSmooting
    //, hsoLogVerb4ose
    ]);
  //writeln('DropPriviledges=', DropPriviledges('abouchez'));
  fHttpServer.HttpQueueLength := 100000; // needed e.g. from wrk/ab benchmarks
  fHttpServer.OnRequest := DoOnRequest;
  fHttpServer.WaitStarted; // raise exception e.g. on binding issue
end;

...

var
  simpleServer: TSimpleHttpAsyncServer;
begin
  with TSynLog.Family do
    begin
      Level :=
        //LOG_VERBOSE +
        LOG_FILTER[lfErrors];
      //EchoToConsole := Level;
      PerThreadLog := ptIdentifiedInOneFile;
      HighResolutionTimestamp := true;
    end;

  simpleServer := TSimpleHttpAsyncServer.Create();
  try
    {$I-}
    writeln;
    TextColor(ccLightGreen);
    writeln(simpleServer.fHttpServer.ClassName, ' running on localhost:8888'#10);
    TextColor(ccWhite);
    writeln('try curl http://localhost:8888/echo'#10);
    TextColor(ccLightGray);
    writeln('Press [Enter] to quit'#10);
    TextColor(ccCyan);
    ConsoleWaitForEnterKey;
    writeln(ObjectToJson(simpleServer.fHttpServer, [woHumanReadable]));
    TextColor(ccLightGray);
    {$ifdef FPC_X64MM}
    WriteHeapStatus(' ', 16, 8, {compileflags=}true);
    {$endif FPC_X64MM}
  finally
    simpleServer.Free;
  end;

end.

Last edited by Terinel (2024-08-29 06:27:45)

Offline

#4 2024-08-27 15:09:32

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,655
Website

Re: Reception of files via http Post request does not work in multiple

Please follow the forum rules
and DO NOT put such code in the forum threads.

Offline

#5 2024-08-28 02:12:50

Terinel
Member
Registered: 2024-01-18
Posts: 8

Re: Reception of files via http Post request does not work in multiple

ab wrote:

Please follow the forum rules
and DO NOT put such code in the forum threads.

Very strange. But other forum members also post code. They are fine, they don't get any comments. Then make comments to others. And yet, regarding my question, will they answer me on the topic? Or is making a comment a way to avoid answering?

Offline

#6 2024-08-28 09:07:47

JD
Member
Registered: 2015-08-20
Posts: 118

Re: Reception of files via http Post request does not work in multiple

Terinel wrote:
ab wrote:

Please follow the forum rules
and DO NOT put such code in the forum threads.

Very strange. But other forum members also post code. They are fine, they don't get any comments. Then make comments to others. And yet, regarding my question, will they answer me on the topic? Or is making a comment a way to avoid answering?

@Terinel
You don't get it. If you post code at all, keep it very brief. I try to aim for approximately 10 lines or fewer; and if it is longer or for logs, I now use an online paste service like Paste.ee https://paste.ee/. There are many others like that available.

Apparently, it is necessary to do this in order not to overload this site's server.

Just my 2 cents.

Cheers,

JD

Last edited by JD (2024-08-28 09:15:08)

Offline

#7 2024-08-29 06:11:15

Terinel
Member
Registered: 2024-01-18
Posts: 8

Re: Reception of files via http Post request does not work in multiple

Got it. Thanks for your reply.

Offline

Board footer

Powered by FluxBB