Recent

Author Topic: Http Server  (Read 1709 times)

michoux

  • Full Member
  • ***
  • Posts: 112
Http Server
« on: December 05, 2022, 11:54:38 am »
Hello

I created a new HTTP Server Application in Lazarus on port 8080
When ever I try to reach it on localhost:8080  from browser I get following erro

************************************++
The application encountered the following error:

    Error: Not found
    Stack trace:
    $00438D3C
    $0043873C
    $00438D9E
    $0043234F
    $00432A47
    $0042BCA5
    $00436047
    $004359FF
    $00435B84
    $004219F1
    $00402B7F
    $7730FEF9
    $778C7BBE
    $778C7B8E
*******************************
« Last Edit: December 05, 2022, 12:27:02 pm by michoux »

michoux

  • Full Member
  • ***
  • Posts: 112
Re: Http Server
« Reply #1 on: December 05, 2022, 12:31:14 pm »
Somehow now server is working :)

I have following code, it is from tutorial of fpweb

Code: Pascal  [Select][+][-]
  1. procedure JSON(req: TRequest; res: TResponse);
  2. var
  3.   n: Integer;
  4.   f: TUploadedFile;
  5.   i: Integer;
  6. begin
  7.   n := req.Files.Count;
  8.   if n = 0 then
  9.     with res.Contents do
  10.     begin
  11.       Add('<form id="form" action="' + req.URI + '" method="POST" enctype="multipart/form-data">');
  12.       Add('<label for="name">Drag n drop or click to add file:</label>');
  13.       Add('<input type="file" name="input" />');
  14.       Add('<input type="submit" value="Send" />');
  15.       Add('</form>');
  16.     end
  17.   else
  18.   begin
  19.     f := req.Files[0];
  20.     res.Contents.LoadFromStream(f.Stream);
  21.   end;
  22.  
  23. end;                                      
  24.  
  25.  

I want to change this so that I receive file directly  with post
For example with curl.
c:\curl\bin\curl.exe -X GET --data-binary @c:\test.xml http://localhost:9080

Regards Mich

michoux

  • Full Member
  • ***
  • Posts: 112
Re: Http Server
« Reply #2 on: December 05, 2022, 12:37:50 pm »
Hi,

Ok I found out all is written in request.

just a simple code will output the file content posted with curl.
c:\curl\bin\curl.exe -X GET --data-binary @c:\live_timing\race.xml http://localhost:9080/


Code: Pascal  [Select][+][-]
  1. procedure JSON(req: TRequest; res: TResponse);
  2. var
  3.   n: Integer;
  4.   f: TUploadedFile;
  5.   i: Integer;
  6.   s:string;
  7. begin
  8.   s := req.Content;
  9.  
  10. writeln(s);
  11.   end;        
  12.  

michoux

  • Full Member
  • ***
  • Posts: 112
Re: Http Server
« Reply #3 on: December 05, 2022, 01:15:46 pm »
Now I have next question.

I make a form and I start a web server with a button.
form1.FPHttpServer2.Active:=true

This blocks my app totally.

PierceNg

  • Sr. Member
  • ****
  • Posts: 369
    • SamadhiWeb
Re: Http Server
« Reply #4 on: December 05, 2022, 01:36:33 pm »
Now I have next question.

I make a form and I start a web server with a button.
form1.FPHttpServer2.Active:=true

This blocks my app totally.

Run the HTTP server on a thread. Something like this:

Code: Pascal  [Select][+][-]
  1. type
  2.   TWebServerThread = class(TThread)
  3.     protected
  4.       procedure Execute; override;
  5.     public
  6.       constructor Create(CreateSuspended: boolean);
  7.   end;
  8.  
  9. constructor TWebServerThread.Create(CreateSuspended: boolean);
  10. begin
  11.   inherited Create(CreateSuspended);
  12.   FreeOnTerminate := true;
  13. end;
  14.  
  15. procedure TWebServerThread.Execute;
  16. begin
  17.   fphttpapp.Application.Run;
  18. end;

michoux

  • Full Member
  • ***
  • Posts: 112
Re: Http Server
« Reply #5 on: December 05, 2022, 01:59:15 pm »
Thanks.

How do I call TWebServerThread.Execute from my app?

PierceNg

  • Sr. Member
  • ****
  • Posts: 369
    • SamadhiWeb
Re: Http Server
« Reply #6 on: December 05, 2022, 02:11:01 pm »
How do I call TWebServerThread.Execute from my app?

Code: Pascal  [Select][+][-]
  1. begin
  2.   // Set up web server.
  3.   fphttpapp.Application.Port := 8000;
  4.   fphttpapp.Application.Threaded := true; // web server itself is multi-threaded
  5.   fphttpapp.Application.Initialize;
  6.   // Run the server in a thread.
  7.   // If you want to start the server on button press, then move next line to the button press handler.
  8.   TWebServerThread.Create(false); // false means the server thread runs immediately
  9.  
  10.   // Now run LCL application.
  11.   RequireDerivedFormResource:=True;
  12.   Forms.Application.Scaled:=True;
  13.   Forms.Application.Initialize;
  14.   Forms.Application.CreateForm(TForm1, Form1);
  15.   Forms.Application.Run;  
  16. end.

See https://wiki.freepascal.org/Multithreaded_Application_Tutorial for more info.

michoux

  • Full Member
  • ***
  • Posts: 112
Re: Http Server
« Reply #7 on: December 05, 2022, 03:42:02 pm »
Thank you very much.

I inserted form on which I have a report with lazReport.

After I get response from a webserver I want to call to open report, which is on form1 which I added.
I use
form1.frReport1.ShowReport;

This kills my app with SIGSERV ...
Is this related to thred?

PierceNg

  • Sr. Member
  • ****
  • Posts: 369
    • SamadhiWeb
Re: Http Server
« Reply #8 on: December 05, 2022, 04:01:16 pm »
Thank you very much.

I inserted form on which I have a report with lazReport.

After I get response from a webserver I want to call to open report, which is on form1 which I added.
I use
form1.frReport1.ShowReport;

This kills my app with SIGSERV ...
Is this related to thred?

Dunno. If you post a simplified but complete project that compiles and demonstrates the crash, then others will be better placed to help.

michoux

  • Full Member
  • ***
  • Posts: 112
Re: Http Server
« Reply #9 on: December 05, 2022, 04:33:44 pm »
Here is simple code.
I also have a form with lazreport on it this is all.
I just want to open report with

 form1.frReport1.LoadFromFile('test.lrf');
    form1.frReport1.ShowReport;

Code: Pascal  [Select][+][-]
  1.  
  2. program Project1;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. uses
  7.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  8.   cthreads,
  9.   {$ENDIF}{$ENDIF}
  10.   Classes,SysUtils, fphttpapp, httpdefs, httproute, fpjson, jsonparser, interfaces,forms,Unit1;
  11.  
  12. type
  13.   TWebServerThread = class(TThread)
  14.     protected
  15.       procedure Execute; override;
  16.     public
  17.       constructor Create(CreateSuspended: boolean);
  18.   end;
  19.  
  20.   constructor TWebServerThread.Create(CreateSuspended: boolean);
  21.   begin
  22.     inherited Create(CreateSuspended);
  23.     FreeOnTerminate := true;
  24.   end;
  25.  
  26.   procedure TWebServerThread.Execute;
  27.   begin
  28.     fphttpapp.Application.Run;
  29.   end;
  30.  
  31. procedure test(req: TRequest; res: TResponse);
  32. var
  33.   s:string;
  34.   rawJson: AnsiString;
  35.   people: TJSONArray;
  36.   person: TJSONObject;
  37.   personEnum: TJSONEnum;
  38. begin
  39.   // Get the JSON data
  40.     rawJson:=req.Content;
  41.      writeln(rawjson);
  42.  
  43.     form1.frReport1.LoadFromFile('test.lrf');
  44.     form1.frReport1.ShowReport;
  45. end;
  46.  
  47.  
  48.  
  49.  
  50. begin
  51.  
  52.  
  53.   fphttpapp.Application.Port := 8000;
  54.   HTTPRouter.RegisterRoute('/test', @test, true);
  55.   fphttpapp.Application.Threaded := true;
  56.   fphttpapp.Application.Initialize;
  57.   TWebServerThread.Create(false);
  58.  
  59.  
  60.   RequireDerivedFormResource:=True;
  61.   Forms.Application.Scaled:=True;
  62.   Forms.Application.Initialize;
  63.   Forms.Application.CreateForm(TForm1, Form1);
  64.   Forms.Application.Run;
  65.  
  66. end.
  67.  
  68.  
  69.  

PierceNg

  • Sr. Member
  • ****
  • Posts: 369
    • SamadhiWeb
Re: Http Server
« Reply #10 on: December 05, 2022, 04:46:46 pm »
Here is simple code.
I also have a form with lazreport on it this is all.
I just want to open report with

 form1.frReport1.LoadFromFile('test.lrf');
    form1.frReport1.ShowReport;

Code: Pascal  [Select][+][-]
  1.  
  2. program Project1;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. uses
  7.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  8.   cthreads,
  9.   {$ENDIF}{$ENDIF}
  10.   Classes,SysUtils, fphttpapp, httpdefs, httproute, fpjson, jsonparser, interfaces,forms,Unit1;
  11.  
  12. type
  13.   TWebServerThread = class(TThread)
  14.     protected
  15.       procedure Execute; override;
  16.     public
  17.       constructor Create(CreateSuspended: boolean);
  18.   end;
  19.  
  20.   constructor TWebServerThread.Create(CreateSuspended: boolean);
  21.   begin
  22.     inherited Create(CreateSuspended);
  23.     FreeOnTerminate := true;
  24.   end;
  25.  
  26.   procedure TWebServerThread.Execute;
  27.   begin
  28.     fphttpapp.Application.Run;
  29.   end;
  30.  
  31. procedure test(req: TRequest; res: TResponse);
  32. var
  33.   s:string;
  34.   rawJson: AnsiString;
  35.   people: TJSONArray;
  36.   person: TJSONObject;
  37.   personEnum: TJSONEnum;
  38. begin
  39.   // Get the JSON data
  40.     rawJson:=req.Content;
  41.      writeln(rawjson);
  42.  
  43.     form1.frReport1.LoadFromFile('test.lrf');
  44.     form1.frReport1.ShowReport;
  45. end;
  46.  
  47.  
  48.  
  49.  
  50. begin
  51.  
  52.  
  53.   fphttpapp.Application.Port := 8000;
  54.   HTTPRouter.RegisterRoute('/test', @test, true);
  55.   fphttpapp.Application.Threaded := true;
  56.   fphttpapp.Application.Initialize;
  57.   TWebServerThread.Create(false);
  58.  
  59.  
  60.   RequireDerivedFormResource:=True;
  61.   Forms.Application.Scaled:=True;
  62.   Forms.Application.Initialize;
  63.   Forms.Application.CreateForm(TForm1, Form1);
  64.   Forms.Application.Run;
  65.  
  66. end.
  67.  
  68.  
  69.  

According to the wiki page on multi-threaded programming I linked to earlier, section 'Lazarus Widgetset Interfaces':

Quote
Only one thread in an application should call LCL APIs, usually the main thread. Other threads can make use of the LCL through a number of indirect methods, ...


So your '/test' web handler shouldn't do LCL GUI things directly. Instead, it should save the data received, and somehow inform the main thread to process the saved data. See the wiki page for ideas.

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Http Server
« Reply #11 on: December 06, 2022, 05:20:26 am »
Hi michoux,

I think here's an example you are looking for.

https://www.freepascal.org/~michael/articles/webserver1/webserver1.pdf

You can also find the sources in the following list. Scroll down the list until you find webserver.

https://www.freepascal.org/~michael/articles/
« Last Edit: December 06, 2022, 05:23:05 am by egsuh »

michoux

  • Full Member
  • ***
  • Posts: 112
Re: Http Server
« Reply #12 on: December 06, 2022, 11:01:32 am »
Hi

Thank you for your help. I managed to get things going.
Now next step is to read some data and parse it.

I got following script which sends some data to serve.

Code: Pascal  [Select][+][-]
  1. <html>
  2.  
  3. <head>
  4.         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
  5. </head>
  6.  
  7. <body>
  8.         <h1>Localhost service call test</h1>
  9.         <label for="url-call">URL to call:</label>
  10.         <input style="width: 400px;" type="text" id="url-call" name="url-call" value="https://127.0.0.1:8000/sandbox"><br><br>
  11.         <a id="test-call" href="#">Call</a>
  12.  
  13.         <p id="odgovor"></p>
  14. </body>
  15.  
  16. <footer>
  17.         <script type="text/javascript">
  18.                 $("#test-call").click(function(){
  19.                         $.ajax({
  20.                                 type: 'POST',
  21.                                 url: $("#url-call").val(),
  22.                                 dataType: 'json',
  23.                                 data: {
  24.                                         "id": 10,
  25.                                         "title": "HP Pavilion 15-DK1056WM",
  26.                                         "description": "HP Pavilion 15-DK1056WM Gaming...",
  27.                                         "price": 1099,
  28.                                         "discountPercentage": 6.18,
  29.                                         "rating": 4.43,
  30.                                         "stock": 89,
  31.                                         "brand": "HP Pavilion",
  32.                                         "category": "laptops",
  33.                                         "thumbnail": "https://i.dummyjson.com/data/products/10/thumbnail.jpeg",
  34.                                         "images": [
  35.                                                 "https://i.dummyjson.com/data/products/10/1.jpg",
  36.                                                 "https://i.dummyjson.com/data/products/10/2.jpg",
  37.                                                 "https://i.dummyjson.com/data/products/10/3.jpg",
  38.                                                 "https://i.dummyjson.com/data/products/10/thumbnail.jpeg"
  39.                                         ]
  40.                                 },
  41.                                 success: function (r) {
  42.                                         $("#odgovor").html("Odgovor: <br> Success: <br> " + JSON.stringify(r, null, 2));
  43.                                 },
  44.                                 error: function (r) {
  45.                                         $("#odgovor").html("Odgovor: <br> Error: <br> " + JSON.stringify(r, null, 2));
  46.                                 }
  47.                         }, $(this));
  48.                 });
  49.         </script>
  50. </footer>
  51.  
  52. </html>
  53.  
  54.  


I get the data but it is is not in real json format it is some strange outpout.

Here is the received data.
"id=10&title=HP+Pavilion+15-DK1056WM&description=HP+Pavilion+15-DK1056WM+Gaming...&price=1099&discountPercentage=6.18&rating=4.43&stock=89&brand=HP+Pavilion&category=laptops&thumbnail=https%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2Fthumbnail.jpeg&images%5B%5D=https%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2F1.jpg&images%5B%5D=https%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2F2.jpg&images%5B%5D=https%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2F3.jpg&images%5B%5D=https%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2Fthumbnail.jpeg"

Regards Matija

michoux

  • Full Member
  • ***
  • Posts: 112
Re: Http Server
« Reply #13 on: December 06, 2022, 11:19:04 am »
I think above is problem with ajax code posting Json, I will try to fix this

 

TinyPortal © 2005-2018