#1 2024-07-20 08:59:57

Flashcqxg
Member
Registered: 2018-01-11
Posts: 31

Two questions about THttpApiServer

The code is as follows:

fHttpServer := THttpApiServer.Create('');

fHttpServer.RegisterCompress(CompressGZip, 10);
fHttpServer.RegisterCompress(CompressDeflate, 10);

fHttpServer. AddUrl('', '8888',  False, '127.0.0.1');
// fHttpServer. AddUrl('', '8888', False, '*');
//fHttpServer.AddUrl('', '8888', False, '+');

1. Whether I use * or+, the following three methods cannot be accessed:
http://127.0.0.1:8888
http://localhost:8888
http://192.168.1.9:8888 (Local intranet IP address)

2. After using Register Compress, use Ctxt.SetOutFile send a static file to the browser,but that has not been compressed.

Thank you.

Offline

#2 2024-07-20 19:22:58

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

Re: Two questions about THttpApiServer

Did you register the URL at http.sys level?

Offline

#3 2024-07-20 22:48:23

Flashcqxg
Member
Registered: 2018-01-11
Posts: 31

Re: Two questions about THttpApiServer

How to register the URL as http.sys level? the code fHttpServer.add can't be registered?

Offline

#4 2024-07-21 08:57:23

Flashcqxg
Member
Registered: 2018-01-11
Posts: 31

Re: Two questions about THttpApiServer

The first problem has been solved. The second issue has not been resolved, as the output static HTML file is not compressed.

Offline

#5 2024-07-22 03:38:39

Flashcqxg
Member
Registered: 2018-01-11
Posts: 31

Re: Two questions about THttpApiServer

The static files is not compressed in the DoOnRequest:

function TGEApiServer.DoOnRequest(C: THttpServerRequestAbstract): cardinal;
var
  sFile: string;
begin

  var sUrl := UrlDecode(C.URL);
  sFile := ExtractFilePath(ParamStr(0)) + 'dist\' + sUrl;
  if FileExists(sFile) then
  begin
    Result := C.SetOutFile(sFile, False);
  end
  else if FileExists(sFile + '\index.html') then
  begin
    // eg:  http://17.0.0.1 = http://127.0.0.1/index.html
    sFile := sFile + '\index.html';
    Result := C.SetOutFile(sFile, False);
  end
  else if sUrl = '/login' then
  begin
    Result := C.SetOutFile('.\dist\index.html', False);
  end
  else
  begin
    Result := 200;
  end;
end;

Offline

#6 2024-07-22 06:19:13

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

Re: Two questions about THttpApiServer

Compression of files is not supported by the Windows API.
It can either return from memory or a file handle (see THttpChunkType).

So if you want compression, use SetOutContent(StringFromFile()).
Or even better, SetOutContent() with a cached value.

Offline

#7 2024-07-22 10:08:18

Flashcqxg
Member
Registered: 2018-01-11
Posts: 31

Re: Two questions about THttpApiServer

I am using SetOutContent(StringFromFile()),but it cannot output web pages properly.

Offline

#8 2024-07-22 12:54:45

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

Re: Two questions about THttpApiServer

Please refine what "it cannot output web pages properly" means.

Offline

#9 2024-07-22 12:58:51

Flashcqxg
Member
Registered: 2018-01-11
Posts: 31

Re: Two questions about THttpApiServer

What I mean is: I put some static files such as HTML, JS, CSS in the www directory and use THttpApiServer as a static web server. Currently, using SetOutContent (StringFromFile()) cannot achieve the goal.

Offline

#10 2024-07-22 14:13:04

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

Re: Two questions about THttpApiServer

Did you debug and see what is wrong?

How did you add the URI?
It sounds like if you don't understand what a http.sys domain is, and that you need to authorize the URI with admin rights at least once on the computer.

Offline

#11 2024-07-22 14:15:23

Flashcqxg
Member
Registered: 2018-01-11
Posts: 31

Re: Two questions about THttpApiServer

I just delphi the static files the DoOnRequest:

function TGEApiServer.DoOnRequest(C: THttpServerRequestAbstract): cardinal;
var
  sFile: string;
begin

  var sUrl := UrlDecode(C.URL);
  sFile := ExtractFilePath(ParamStr(0)) + 'dist\' + sUrl;
  if FileExists(sFile) then
  begin
    Result := C.SetOutFile(sFile, False);
  end
  else if FileExists(sFile + '\index.html') then
  begin
    // eg:  http://17.0.0.1 = http://127.0.0.1/index.html
    sFile := sFile + '\index.html';
    Result := C.SetOutFile(sFile, False);
  end
  else if sUrl = '/login' then
  begin
    Result := C.SetOutFile('.\dist\index.html', False);
  end
  else
  begin
    Result := 200;
  end;
end;

Offline

#12 2024-07-22 16:04:31

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

Re: Two questions about THttpApiServer

1) your code is unsafe because you can access any file on your system using ..\ patterns in the URI.

2) debug a little and see if it goes inside the method

3) http.sys needs to register the URI once with admin rights

Offline

#13 2024-07-22 23:30:15

Flashcqxg
Member
Registered: 2018-01-11
Posts: 31

Re: Two questions about THttpApiServer

Hi,
ab,What should safe code look like?and how to register the URI use code?
Oh, Mormot is so powerful, but its reference manual and demo are so few, making it difficult for beginners of Delphi to quickly get started.
Thanks for your help.

Offline

Board footer

Powered by FluxBB