Recent

Author Topic: TFPHTTPServer in a thread  (Read 7087 times)

euMesmo

  • New Member
  • *
  • Posts: 12
Re: TFPHTTPServer in a thread
« Reply #15 on: July 18, 2019, 12:43:24 pm »
Thank you for your interest rvk, but that is not the problem.

I will try to explain. My application creates web pages (html+css+js) based on parameters entered by the user. To preview the application creates all the necessary files in a temporary folder and then the application opens the html file in the browser that the system has by default. The problem is that several of the contents it creates cannot be seen in the browser in local (cross-origin error) by using "file:///..." being necessary "http://".

In case the computer has installed a web server (WAMP type) the application creates the files in a temporary folder of the folder to which the "localhost" points and then opens the browser with the url of the localhost and the html created.

I would like the application to have an integrated web server so that the user does not have to install any WAMP type server and that when the preview is made it opens in the browser under an http server.

When I saw the rvk code I thought it was what I was looking for but my problem is that I don't know in which path I should create the files to call localhost:8001 from the system's default browser.

I hope I have explained.
Thank you for your time.

rvk

  • Hero Member
  • *****
  • Posts: 3842
Re: TFPHTTPServer in a thread
« Reply #16 on: July 18, 2019, 12:58:38 pm »
I would like the application to have an integrated web server so that the user does not have to install any WAMP type server and that when the preview is made it opens in the browser under an http server.

When I saw the rvk code I thought it was what I was looking for but my problem is that I don't know in which path I should create the files to call localhost:8001 from the system's default browser.
Ok, that makes it clearer.
The code in this topic was for a specific different problem.

Did you look at some other examples of TFPHTTPServer yet.
For example: lazarus_directory\fpc\3.0.4\source\packages\fcl-web\examples\httpserver
(sample source also here: https://github.com/alrieckert/freepascal/blob/master/packages/fcl-web/examples/httpserver/simplehttpserver.pas)

You can see that with Serv.Port:=8080; you can set the port to use (i.e. http://localhost:8080 in this line)
In TTestHTTPServer.HandleRequest() you see that the requested file (in ARequest.Url) is read from the BaseDir (which you can set with Serv.BaseDir:=ExtractFilePath(ParamStr(0)); )

So simplehttpserver.pas should have everything you need to supply a default browser with a complete website on an alternate port (other than 80).

lucamar

  • Hero Member
  • *****
  • Posts: 2126
Re: TFPHTTPServer in a thread
« Reply #17 on: July 18, 2019, 03:48:19 pm »
You can use rvk's code without problems (I just tested it).

Just add some code to the OnRequest method (in the main form) to handle returning the files from wherever you put them.

This is an extremely simple example which just tries to find the file(s) in the current directory, but nothing prevents you from serving them from anywhere else. In that case, though, you will have to deal with the paths in the request by yourself--but that's not cumbersome or difficult:

Code: Pascal  [Select]
  1. procedure TForm1.OnRequest(Sender: TObject;
  2.   var ARequest: TFPHTTPConnectionRequest;
  3.   var AResponse: TFPHTTPConnectionResponse);
  4. var
  5.   AFilename: String;
  6. begin
  7.   AFilename := ARequest.URI;
  8.   {Get rid of the starting bars so we won't serve from '/'
  9.    Theres should be only one, but just in case ...}
  10.   while AFilename.StartsWith('/') or AFilename.StartsWith('.') do
  11.     Delete(AFilename, 1, 1);
  12.   {Alternatively, one could just do:
  13.     AFilename := GeCurrentDir + AFilename;
  14.   or whatever the web-root directory is,
  15.   but that's not very secure}
  16.  
  17.   { If we find the file, serve it; if not, fawn about it}
  18.   if FileExists(AFilename) then begin
  19.     AResponse.Code := 200;
  20.     AResponse.Content := ReadFileToString(AFilename);
  21.   end else begin
  22.     AResponse.Code := 404;
  23.     AResponse.Content := 'Sorry, don''t have any "' + AFilename +'"';
  24.   end;
  25. end;

With my test program running ((it incorporates the example code) if, for example, I ask in the browser for:
  http://localhost:8081/unit1.pas
it shows the source for the unit.
« Last Edit: July 18, 2019, 03:51:21 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

euMesmo

  • New Member
  • *
  • Posts: 12
Re: TFPHTTPServer in a thread
« Reply #18 on: July 18, 2019, 09:42:32 pm »
Many thanks to rvk and lucamar, your comments have been very helpful.

euMesmo

  • New Member
  • *
  • Posts: 12
Re: TFPHTTPServer in a thread
« Reply #19 on: July 27, 2019, 03:51:42 pm »
I've been testing rvk's code by adding lucamar's code but I can't get it to work properly.

Everything works fine if you open simple files like plain text or static html (without css or js).

As you can see in the attachment, if I run the server in "start", and, in the browser I open (http://localhost:8001/01taller/01taller.htm) an html containing css and javascript seems not to load the styles of the file css. However, javascript seems to work correctly.

I added to the lucamar code the line "Arequest.ProtocolVersion:='HTTP/1.1';" because it returned an error message "Missing HTTP protocol version in request".

I have tried several things, such as adding mimetypes but I can't move forward. Can you help me?

Thank you very much.

rvk

  • Hero Member
  • *****
  • Posts: 3842
Re: TFPHTTPServer in a thread
« Reply #20 on: July 28, 2019, 01:00:52 am »
I can confirm there is something really weird going on in fphttpserver.

You should have seen the .css request but instead you get the "no HTTP/" exception.

Tracing though it, I see that for the .css request, the function TFPHTTPConnection.ReadString() is failing. It returns an empty first line for the received headers (resulting in the "no HTTP/" exception). The FBuffer does contain the correct headers with the request for the .css.

Exact tracing resulted in the line
Code: Pascal  [Select]
  1. if Length(FBuffer)=0 then
  2.   FillBuffer;
If the highlighted line is executed, the Result in the debugger becomes inaccessible. The later Result:=Result+xx then results in an empty string.

Are we dealing with another case of de-referenced (result)string here?

lucamar

  • Hero Member
  • *****
  • Posts: 2126
Re: TFPHTTPServer in a thread
« Reply #21 on: July 28, 2019, 03:06:23 am »
I can't replicate. Both my old test and your program (after correcting the main program to "use" cthreads) work as they should and serve all files.

This is your program output (in the memo):
01taller/01taller.htm
01taller/01tallerProble_resources/css/ardoraTest.css
01taller/01tallerProble_resources/js/jquery.js
01taller/01tallerProble_resources/js/jquery-ui.min.js
01taller/01tallerProble_resources/js/jquery.ui.touch-punch.min.js
01taller/01tallerProble_resources/js/ardoraTestCFG.js
01taller/01tallerProble_resources/js/ardoraScorm.js
01taller/01tallerProble_resources/js/ardoraTest.js
01taller/01tallerProble_resources/js/ardoraTab.js

This on an Ubuntu 12.04 box with Laz/FPC 2.0.2/3.0.4, if it matters. Oh! I tested only with one browser: Midori (WebKit-based).

ETA: Tested using wget (which is much more picky) to "mirror" the page and it also "downloads" the page, style-sheet and scripts without problems. Curious, isn't it?
« Last Edit: July 28, 2019, 03:12:26 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

rvk

  • Hero Member
  • *****
  • Posts: 3842
Re: TFPHTTPServer in a thread
« Reply #22 on: July 28, 2019, 08:58:23 am »
I can replicate the problem on Wondows 10 with (a few weeks old) Lazarus/FPC trunk.

I'll try stable/release and latest trunk today.

But I've seen a similar problem a few weeks ago with a dereferenced string.
I'll also try to chance it to shortstring {$H-} and propper buffer to see if it resolves it.
(Shortstrings are not reference counted)


euMesmo

  • New Member
  • *
  • Posts: 12
Re: TFPHTTPServer in a thread
« Reply #23 on: July 30, 2019, 01:40:26 pm »
I have tested W7, W10 and UbuntuMate18 with Lazarus 2.0.0, firefox and chrome and the same thing happens in all of them.

I understand this is some kind of fpweb bug.

julkas

  • Sr. Member
  • ****
  • Posts: 430
  • KISS principle / Lazarus 2.0.0 / FPC 3.0.4
Re: TFPHTTPServer in a thread
« Reply #24 on: July 30, 2019, 03:00:39 pm »
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

julkas

  • Sr. Member
  • ****
  • Posts: 430
  • KISS principle / Lazarus 2.0.0 / FPC 3.0.4
Re: TFPHTTPServer in a thread
« Reply #25 on: July 30, 2019, 05:09:58 pm »
I have tested Lazarus HTTP Web server example. It's not working for me  >:D
« Last Edit: July 30, 2019, 05:15:32 pm by julkas »
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

Thaddy

  • Hero Member
  • *****
  • Posts: 9278
Re: TFPHTTPServer in a thread
« Reply #26 on: July 30, 2019, 05:31:50 pm »
remove wmecho from the uses clause.
also related to equus asinus.

julkas

  • Sr. Member
  • ****
  • Posts: 430
  • KISS principle / Lazarus 2.0.0 / FPC 3.0.4
Re: TFPHTTPServer in a thread
« Reply #27 on: July 30, 2019, 06:11:58 pm »
remove wmecho from the uses clause.
@Thaddy Can you explain, why removing wmecho from uses clause resolves problem?
Who have successfully tested this Lazarus example?
« Last Edit: July 30, 2019, 06:18:14 pm by julkas »
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

Thaddy

  • Hero Member
  • *****
  • Posts: 9278
Re: TFPHTTPServer in a thread
« Reply #28 on: July 30, 2019, 07:20:10 pm »
Because wmecho is not needed and makes it fail to compile because it can not be found.
After that, try to start the server and ask for a page (must exist) like index.html.
Works.
also related to equus asinus.

rvk

  • Hero Member
  • *****
  • Posts: 3842
Re: TFPHTTPServer in a thread
« Reply #29 on: July 30, 2019, 07:23:40 pm »
Works.
Did you try the example of TS?
Did you try a bigger website with .css-files?

It definitely fails in TFPHTTPConnection.ReadString() like a mentioned for the .css request.