Redirecting to another action lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- egsuh · 2020-04-21 12:21 Is it possible to redirect to another action within fpweb web module? e.g.  I'd like to do, Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TwmDC1.DoSomethingRequest(Sender: TObject; ARequest: TRequest;    AResponse: TResponse; var Handled: Boolean);begin        // do something         AResponse.SendRedirect ('Respond');   // <-- Here, I want to redirect to another action, not url. end;   procedure TwmDC1.RespondRequest(Sender: TObject; ARequest: TRequest;   AResponse: TResponse; var Handled: Boolean);begin  AResponse.Content := OutTemplate.GetContent;  Handled := True;end;  Of course I can write Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   RespondRequest(Sender, ARequest,  AResponse, Handled);  instead of AResponse.SendRedirect. ---------------------------------------------------------------------- alantelles · 2020-04-23 16:55 I guess you already answered. ---------------------------------------------------------------------- devEric69 · 2020-05-29 08:17 Quote from: egsuh on April 21, 2020, 02:21:00 pmIs it possible to redirect to another action within fpweb web module? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TwmDC1.RespondRequest(Sender: TObject; ARequest: TRequest;   AResponse: TResponse; var Handled: Boolean);begin  AResponse.Content := OutTemplate.GetContent;  Handled := True;end;  Of course I can write Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   RespondRequest(Sender, ARequest,  AResponse, Handled);  instead of AResponse.SendRedirect. Thanks for the tip. For information, if the called RespondRequest is coded without calling ModuleTemplate.GetContent (ModuleTemplate.GetContent = request to parse the HTML file, and eventually replace its " transparent tags ", in the @viewXyz_onReplaceTag event), but rather by affecting directly the Content of AResponse, then we must call AResponse.SendContent in order to avoid returning into the calling onRequest. For example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure Tgo_uiCtrlFoo.uiActDoThis_onRequest(Sender: TObject; ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);begin    Handled:= true;       if something_is_not_ok then       uiAct403Forbidden_onRequest(Sender, ARequest, AResponse, Handled);    // if *only* something is ok, then i continue   ...\... end;        procedure Tgo_uiCtrlFoo.uiAct403Forbidden_onRequest(Sender: TObject; ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);begin  Handled:= True;  with AResponse do begin    Code:= 403;    CodeText:= 'Forbidden';    Content:= ''+                     ''+                   ''+                     'Forbidden'+                     ''+                   ''+                     '

403: URL forbidden.

'+                     ''+                     '';    Session.Terminate; // Here, I want too, to clear the "Pascal_SESSION" i.e. Self.Session    SendContent; //|--◉ I do not want to return into the caller (i.e. into uiActDoThis_onRequest)!  end;     end; ---------------------------------------------------------------------- egsuh · 2020-06-07 07:27 @devEric69 Thank you for your advice. I was not sure of the role of "SendResponse", but your explanation makes it clear. I'll test this myself. BTW, this is little bit weired that in Delphi, setting "Handled :- true" stops all following operations as far as I know. But in Lazarus, it does not. It's quite long ago when I wrote Delphi codes, so I do not want to check this again. Have you ever running FastCGI server? If you have much experience, please share it with me. My CGI server runs well, but fCGI seems to lag sometimes, so that the web-server sends errors, and resume after a few minutes. ---------------------------------------------------------------------- devEric69 · 2020-06-08 12:29 QuoteBTW, this is little bit weired that in Delphi, setting "Handled :- true" stops all following operations as far as I know. But in Lazarus, it does not. It's quite long ago when I wrote Delphi codes, so I do not want to check this again. Hi, Thank you @egsuh. I think like you. But don't have the precise explanation. The only thing I'm sure with the affectation "Handled:= true\false", is that when Handled == false, whereas the code is coming back again in the calling TWebAction that answers first because it was corresponding to the PATHINFO environment variable, or whereas it goes away i.e. it "shunts" its calling TWebAction because it meets SendContent; (Redirect; too, I guess???), then the HTML code sent back toward the browser is always empty, even if AResponse.Content has been affected. I, too, have read many examples that start mechanically with the line "Handled:= true;". And I have also reproduced it mechanically ("Monkey see, Monkey do"). I guess it's didactic. It's to prevent people like me from spending a whole day coding ... an empty answer in the browser . So, the example above, should be: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure Tgo_uiCtrlFoo.uiActDoThis_onRequest(Sender: TObject; ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);begin       if something_is_not_ok then       uiAct403Forbidden_onRequest(Sender, ARequest, AResponse, Handled);    // if *only* something is ok, then i continue   Handled:= true; // <==(!!!) more logical here, after any possible "shunt" of the callstack.   ...\...end;        procedure Tgo_uiCtrlFoo.uiAct403Forbidden_onRequest(Sender: TObject; ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);begin  Handled:= True;  with AResponse do begin    Code:= 403;    CodeText:= 'Forbidden';    Content:= ''+                     ''+                   ''+                     'Forbidden'+                     ''+                   ''+                     '

403: URL forbidden.

'+                     ''+                     '';    Session.Terminate; // Here, I want too, to clear the "Pascal_SESSION" i.e. Self.Session    SendContent; //|--◉ I do not want to return into the caller (i.e. into uiActDoThis_onRequest)!  end;     end; QuoteHave you ever running FastCGI server? If you have much experience, please share it with me. My CGI server runs well, but fCGI seems to lag sometimes, so that the web-server sends errors, and resume after a few minutes. No, sorry: I'm currently only writing a CGI application. If its loading in memory becomes problematic because there is too much traffic, then I'll can still migrate it to fast-CGI, before looking for other solutions. ---------------------------------------------------------------------- sash · 2020-06-08 16:37 The problem is that you're using wrong terms. In http, redirect is a response type "HTTP 302 Found" with new suggested url. While you (seems like) want to respond with content from another action. ---------------------------------------------------------------------- egsuh · 2020-06-09 04:04 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i The problem is that you're using wrong terms.In http, redirect is a response type "HTTP 302 Found" with new suggested url.While you (seems like) want to respond with content from another action. You are right in some sense. I can write full URL in the SendRedirect, like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i AResponse.SendRedirect(ARequest.host + ARequest.SCript + 'mynextaction');   But it looks like calling IIS (in Windows case) again, and I want to move to new action within my application. I've solved this issue by writing procedures, and passing AResponse as a parameter to the procedure. The content of AResponse is defined within the procedure. ---------------------------------------------------------------------- Leledumbo · 2020-06-13 19:04 Quote from: egsuh on June 09, 2020, 06:04:10 amYou are right in some sense. I can write full URL in the SendRedirect, like ... But it looks like calling IIS (in Windows case) again. Because that's essentially what a HTTP redirect is Your app responds to the request (from browser) with a HTTP redirect status (301/302/307) and a Location header. The browser then interprets the HTTP status and issue another request to the URL specified in Location header, which of course will call your app again. A more proper term for what you actually mean is delegation, not redirect, as it may happen internally within a request handling, transparent from the browser.