Recent

Author Topic: fpWeb CGI module.  (Read 769 times)

BSaidus

  • Sr. Member
  • ****
  • Posts: 495
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
fpWeb CGI module.
« on: April 20, 2023, 01:41:55 pm »
Hello.
I'm doing some tests to learn the wonderfull "fpWEB", and I wonder how to : after posting data to server (using GET methode) how to force browser to clear the string parameters to avoid the refresh.
here is the code.
Code: Pascal  [Select][+][-]
  1. // main datamodule
  2. procedure TWmUccenpf.DataModuleRequest(Sender: TObject; ARequest: TRequest;
  3.   AResponse: TResponse; var Handled: Boolean);
  4. var
  5.   lContent: String;
  6. begin
  7.   Handled := true;
  8.  
  9.   // See if the methode is post
  10.   if SysUtils.SameText(LowerCase(ARequest.Method ),'get' ) then
  11.   begin
  12.     tUcExec.fpLog('Methode GET invoked');   // just log a string
  13.     tUccenPf.fpProcessUccenPf(ARequest);    // do some saving data to file ( db )
  14.   end;
  15.  
  16.   // send content to browser 'html template from file'
  17.   AResponse.ContentType := 'text/html';
  18.   lContent := tUccenPf.fpProcessCountent();        // Get content from html tempplate
  19.   AResponse.Content := lContent;
  20.   AResponse.SendContent();
  21. end;  
  22.  

After returning, in the parameters on URL still exists
Code: Pascal  [Select][+][-]
  1. https://forum.lazarus.freepascal.org/index.php?action=post;board=38.0
  2. // how to redirect to
  3. https://forum.lazarus.freepascal.org/index.php
  4.  

So, Is there any way or technique to do this.
in PHP just this
Code: PHP  [Select][+][-]
  1. function fpTools_goto_url($AUrl) {
  2.   header("location: http://$AUrl") ;
  3.   exit();
  4. }
  5.  
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Leledumbo

  • Hero Member
  • *****
  • Posts: 8717
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: fpWeb CGI module.
« Reply #1 on: April 22, 2023, 02:35:36 am »
after posting data to server (using GET methode)
Wait, what? %)
how to force browser to clear the string parameters to avoid the refresh.
You typically don't for a GET request, but since you said:
in PHP just this
What you want to reply is a redirect response:
Code: Pascal  [Select][+][-]
  1. AResponse.SendRedirect('http://wherever.you/want/to/redirect/it/to');
  2.  

BSaidus

  • Sr. Member
  • ****
  • Posts: 495
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: fpWeb CGI module.
« Reply #2 on: April 22, 2023, 10:42:13 am »
Thank you @Leledumbo.
When I said posting data using GET methode I mean like this.
Code: Text  [Select][+][-]
  1.               <form name="fr_action1" id="fr_ction1" method="get" action="#">
  2.                  <button name="bt_ucccen1" id="bt_ucccen10" class="input-btn-green" type="submit" value="bt_save10"> Do action1</button>
  3.               </form>
  4.  
Yes, this methode
Code: Pascal  [Select][+][-]
  1. AResponse.SendRedirect('http://wherever.you/want/to/redirect/it/to');
  2.  
works well when using GET, and do not work when using POST.
Something else, this is a result of quering server variables using fpWeb. Do not you think that there is something messing .
Code: Pascal  [Select][+][-]
  1. procedure TFPWebModule1.DataModuleRequest(Sender: TObject; ARequest: TRequest;
  2.   AResponse: TResponse; var Handled: Boolean);
  3. var
  4.   lURL, lURI: String;
  5.   lContent: String;
  6. begin
  7.   Handled := true;
  8.   lURI := ARequest.URI;
  9.   lURL := ARequest.URL;
  10.  
  11.   lContent := LineEnding +
  12.   'ARequest.Accept               : ' + ARequest.Accept              + #10#13 +
  13.   'ARequest.AcceptCharset        : ' + ARequest.AcceptCharset       + #10#13 +
  14.   'ARequest.AcceptEncoding       : ' + ARequest.AcceptEncoding      + #10#13 +
  15.   'ARequest.AcceptLanguage       : ' + ARequest.AcceptLanguage      + #10#13 +
  16.   'ARequest.Authorization        : ' + ARequest.Authorization       + #10#13 +
  17.   'ARequest.Connection           : ' + ARequest.Connection          + #10#13 +
  18.   'ARequest.ContentEncoding      : ' + ARequest.ContentEncoding     + #10#13 +
  19.   'ARequest.ContentLanguage      : ' + ARequest.ContentLanguage     + #10#13 +
  20.   'ARequest.ContentLength        : ' + IntToStr(ARequest.ContentLength)       + #10#13 +
  21.   'ARequest.ContentType          : ' + ARequest.ContentType         + #10#13 +
  22.   'ARequest.Date                 : ' + ARequest.Date                + #10#13 +
  23.   'ARequest.Expires              : ' + ARequest.Expires             + #10#13 +
  24.   'ARequest.From                 : ' + ARequest.From                + #10#13 +
  25.   'ARequest.Host                 : ' + ARequest.Host                + #10#13 +
  26.   'ARequest.IfModifiedSince      : ' + ARequest.IfModifiedSince     + #10#13 +
  27.   'ARequest.LastModified         : ' + ARequest.LastModified        + #10#13 +
  28.   'ARequest.Location             : ' + ARequest.Location            + #10#13 +
  29.   'ARequest.Pragma               : ' + ARequest.Pragma              + #10#13 +
  30.   'ARequest.Referer              : ' + ARequest.Referer             + #10#13 +
  31.   'ARequest.RetryAfter           : ' + ARequest.RetryAfter          + #10#13 +
  32.   'ARequest.Server               : ' + ARequest.Server              + #10#13 +
  33.   'ARequest.UserAgent            : ' + ARequest.UserAgent           + #10#13 +
  34.   'ARequest.Warning              : ' + ARequest.Warning             + #10#13 +
  35.   'ARequest.WWWAuthenticate      : ' + ARequest.WWWAuthenticate     + #10#13 +
  36.   'ARequest.Via                  : ' + ARequest.Via                 + #10#13 +
  37.   'ARequest.HTTPAccept           : ' + ARequest.HTTPAccept          + #10#13 +
  38.   'ARequest.HTTPAcceptCharset    : ' + ARequest.HTTPAcceptCharset   + #10#13 +
  39.   'ARequest.HTTPAcceptEncoding   : ' + ARequest.HTTPAcceptEncoding  + #10#13 +
  40.   'ARequest.HTTPIfModifiedSince  : ' + ARequest.HTTPIfModifiedSince + #10#13 +
  41.   'ARequest.HTTPReferer          : ' + ARequest.HTTPReferer         + #10#13 +
  42.   'ARequest.HTTPUserAgent        : ' + ARequest.HTTPUserAgent       + #10#13 +
  43.   'ARequest.Cookie               : ' + ARequest.Cookie              + #10#13 +
  44.   'ARequest.SetCookie            : ' + ARequest.SetCookie           + #10#13 +
  45.   'ARequest.HTTPXRequestedWith   : ' + ARequest.HTTPXRequestedWith  + #10#13 +
  46.   'ARequest.HttpVersion          : ' + ARequest.HttpVersion         + #10#13 +
  47.   'ARequest.ProtocolVersion      : ' + ARequest.ProtocolVersion     + #10#13 +
  48.   'ARequest.PathInfo             : ' + ARequest.PathInfo            + #10#13 +
  49.   'ARequest.PathTranslated       : ' + ARequest.PathTranslated      + #10#13 +
  50.   'ARequest.RemoteAddress        : ' + ARequest.RemoteAddress       + #10#13 +
  51.   'ARequest.RemoteAddr           : ' + ARequest.RemoteAddr          + #10#13 +
  52.   'ARequest.RemoteHost           : ' + ARequest.RemoteHost          + #10#13 +
  53.   'ARequest.ScriptName           : ' + ARequest.ScriptName          + #10#13 +
  54.   'ARequest.ServerPort           : ' + IntToStr(ARequest.ServerPort)          + #10#13 +
  55.   'ARequest.Method               : ' + ARequest.Method              + #10#13 +
  56.   'ARequest.URL                  : ' + ARequest.URL                 + #10#13 +
  57.   'ARequest.Query                : ' + ARequest.Query               + #10#13 +
  58.   'ARequest.Content              : ' + ARequest.Content             + #10#13 ;
  59.  
  60.   fpLog(lContent);
  61.  
  62.  
  63.  
  64.  
  65. end;
  66.  
  67.  

RESULTS:
Code: Text  [Select][+][-]
  1. ARequest.Server               : localhost
  2. ARequest.ScriptName           : /fb1c/index.cgi
  3. ARequest.ServerPort           : 80
  4. ARequest.HttpVersion          :
  5. ARequest.ProtocolVersion      :
  6. ARequest.Accept               : text/html,application/xhtml xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
  7. ARequest.AcceptCharset        :
  8. ARequest.AcceptEncoding       : gzip, deflate, br
  9. ARequest.AcceptLanguage       : en-US,en;q=0.5
  10. ARequest.Authorization        :
  11. ARequest.Connection           : keep-alive
  12. ARequest.ContentEncoding      :
  13. ARequest.ContentLanguage      :
  14. ARequest.ContentLength        : 0
  15. ARequest.ContentType          :
  16. ARequest.Date                 :
  17. ARequest.Expires              :
  18. ARequest.From                 :
  19. ARequest.Host                 : localhost
  20. ARequest.IfModifiedSince      :
  21. ARequest.LastModified         :
  22. ARequest.Location             :
  23. ARequest.Pragma               :
  24. ARequest.Referer              :
  25. ARequest.RetryAfter           :
  26. ARequest.UserAgent            : Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0
  27. ARequest.Warning              :
  28. ARequest.WWWAuthenticate      :
  29. ARequest.Via                  :
  30. ARequest.HTTPAccept           : text/html,application/xhtml xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
  31. ARequest.HTTPAcceptCharset    :
  32. ARequest.HTTPAcceptEncoding   : gzip, deflate, br
  33. ARequest.HTTPIfModifiedSince  :
  34. ARequest.HTTPReferer          :
  35. ARequest.HTTPUserAgent        :
  36. ARequest.Cookie               :
  37. ARequest.SetCookie            :
  38. ARequest.HTTPXRequestedWith   :
  39. ARequest.HttpVersion          :
  40. ARequest.ProtocolVersion      :
  41. ARequest.PathInfo             :
  42. ARequest.PathTranslated       :
  43. ARequest.RemoteAddress        : 127.0.0.1
  44. ARequest.RemoteAddr           : 127.0.0.1
  45. ARequest.RemoteHost           :
  46. ARequest.Method               : GET
  47. ARequest.URL                  :
  48. ARequest.Query                :
  49. ARequest.Content              :
  50.  

PS: I'm using Lighttpd 1.4.49.1 on windows
PHP output this:
Code: Text  [Select][+][-]
  1. CONTENT_LENGTH  0
  2. DOCUMENT_ROOT   D:/UccenOS/UccenOS/Servers/lighttpd/htdocs
  3. GATEWAY_INTERFACE       CGI/1.1
  4. HTTP_ACCEPT     text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
  5. HTTP_ACCEPT_ENCODING    gzip, deflate, br
  6. HTTP_ACCEPT_LANGUAGE    en-US,en;q=0.5
  7. HTTP_CONNECTION         keep-alive
  8. HTTP_HOST       localhost
  9. HTTP_SEC_FETCH_DEST     document
  10. HTTP_SEC_FETCH_MODE     navigate
  11. HTTP_SEC_FETCH_SITE     none
  12. HTTP_SEC_FETCH_USER     ?1
  13. HTTP_UPGRADE_INSECURE_REQUESTS  1
  14. HTTP_USER_AGENT         Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0
  15. REDIRECT_STATUS         200
  16. REMOTE_ADDR     127.0.0.1
  17. REMOTE_PORT     51430
  18. REQUEST_METHOD  GET
  19. REQUEST_SCHEME  http
  20. REQUEST_URI     /fb1c/a.php
  21. SCRIPT_FILENAME         D:/UccenOS/UccenOS/Servers/lighttpd/htdocs/fb1c/a.php
  22. SCRIPT_NAME     /fb1c/a.php
  23. SERVER_ADDR     127.0.0.1
  24. SERVER_NAME     localhost
  25. SERVER_PORT     80
  26. SERVER_PROTOCOL         HTTP/1.1
  27. SERVER_SOFTWARE         lighttpd/1.4.49 (win32)
  28. SYSTEMROOT      C:\Windows
  29. WINDIR  C:\Windows
  30. PATH    D:\UccenOS\UccenOS\Servers\lighttpd
  31.  

« Last Edit: April 22, 2023, 10:49:10 am by BSaidus »
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Leledumbo

  • Hero Member
  • *****
  • Posts: 8717
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: fpWeb CGI module.
« Reply #3 on: April 23, 2023, 02:19:07 am »
Do not you think that there is something messing .
No, but also do note that not all headers are parsed and stored to its own property, as not all are standard.
Those non-standard headers can be queried with THTTPHeader.CustomHeaders, which you can also set as response headers, so you wish. Web servers like Lighttpd also tend to add their own headers, for identification inside the user program if necessary, which are some that you see.

BSaidus

  • Sr. Member
  • ****
  • Posts: 495
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: fpWeb CGI module.
« Reply #4 on: April 24, 2023, 09:32:06 am »
Hello @Leledumbo
A how about
Code: Pascal  [Select][+][-]
  1.     AResponse.SendRedirect('http://localhost/index.cgi');
  2.  
while using POST,    using
Code: Pascal  [Select][+][-]
  1. GET
[/color]it works but not with POST.
I bypassed this problem using this code.
Code: Pascal  [Select][+][-]
  1. class procedure tUccenPf.fpSendRedirect(AResponse: TResponse; AURL: String);
  2. begin
  3.   AResponse.SetCustomHeader('METHODE', 'GET');
  4.   AResponse.SendHeaders();
  5.   AResponse.SendRedirect( AURL );
  6. end;
  7.  
  8. { -------------------------------------------------------------------------------------- }
  9. { // Suppose that method is POST                                                         }
  10. { -------------------------------------------------------------------------------------- }
  11. class procedure tUccenPf.fpProcessUccenPf(ARequest: TRequest; AResponse: TResponse);
  12. var
  13.   lName: String;
  14.   lURLToRedirect: String;
  15. begin
  16.   lURLToRedirect := fpRequest2URL(ARequest); // Get the URL (http://localhost/index.cgi)
  17.   lName := ARequest.ContentFields.Values['bt_ucccen1'] ;
  18.   if System.Pos('bt_save0', lName) > 0 then
  19.   begin
  20.     tUcExec.fpLog('[bt_save0] saving general network configuration');
  21.     fpProcessDbParameters(ARequest);
  22.     Self.fpSendRedirect(AResponse, lURLToRedirect);
  23.     Exit;
  24.   end;
  25. end;
  26.  
I simply modify the POST =>(to) GET  in ARespnse Header.

lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Leledumbo

  • Hero Member
  • *****
  • Posts: 8717
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: fpWeb CGI module.
« Reply #5 on: April 24, 2023, 10:57:16 am »
Hello @Leledumbo
A how about
Code: Pascal  [Select][+][-]
  1.     AResponse.SendRedirect('http://localhost/index.cgi');
  2.  
while using POST,    using
Code: Pascal  [Select][+][-]
  1. GET
[/color]it works but not with POST.
SendRedirect sends 302/307, both of which cannot redirect a POST request, for a logical reason. 303 MIGHT be able to, but I haven't verified. My classic solution is to send a HTML containing JavaScript in its head that sets window.location, giving similar effect to instant redirection.
« Last Edit: April 24, 2023, 11:01:20 am by Leledumbo »

BSaidus

  • Sr. Member
  • ****
  • Posts: 495
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: fpWeb CGI module.
« Reply #6 on: April 24, 2023, 11:05:26 am »
My classic solution is to send a HTML containing JavaScript in its head that sets window.location, giving similar effect to instant redirection.
Thank you, I'll try it .
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

 

TinyPortal © 2005-2018