You are not logged in.
Pages: 1
Got it. Thanks for your reply.
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?
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.
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.
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.
Which methods to call, in what order. What should I send there?
Just use the TJwtHS256 class from mormot.crypt.jwt.
How exactly do you show a simple example
Is it possible to ask to show an example relevant to Mormot2 for generating tokens and signing.
function TSimpleHttpAsyncServer.GenerateToken(const UserID: Integer): RawUTF8;
var
Claims: TJwtClaim;
JWT: RawUTF8;
begin
// Initialize the structure for storing data in the token
Claims.jrcJwtID := IntToStr(UserID);
Claims.jrcExpirationTime := DateTimeToUnixTime(Now + TOKEN_EXPIRATION_TIME);
// Generate a token and sign it using a secret key
JWT := JWTSign(TJWTAlgorithm.HS256, Claims, TOKEN_SECRET);
Result := JWT;
end;
Pages: 1