Recent

Author Topic: Any access to TRequest in webmodule outside of Action event handlers  (Read 971 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1493
Hello,

Within webmodule's action event handlers, we can access request values.
Can I access the TRequest object outside, as in following example?  Adding dummy parameter is simple like
 
   function TwmAQs.NextText(ARequest: TRequest; i:index): string;

but I'd like to know there are any way to access the Request without using the parameters.

Code: Pascal  [Select][+][-]
  1. procedure TwmAQs.DataModuleRequest(Sender: TObject; ARequest: TRequest;
  2.    AResponse: TResponse; var Handled: Boolean);
  3. begin
  4.      // this is easy
  5.      AResponse.content := ARequest.host;
  6.      Handled := false;
  7.  
  8.     // I'd like to use in following way
  9.      AResponse.content := NextText(5);
  10.      Handled := true;
  11. end;
  12.  
  13.  
  14. function TwmAQs.NextText(i:index): string;
  15. begin
  16.       // How can I access request here?
  17.       Result := Request.Host + '_' + IntTostr(i);
  18. end;
  19.  
  20.  


rvk

  • Hero Member
  • *****
  • Posts: 6585
Re: Any access to TRequest in webmodule outside of Action event handlers
« Reply #1 on: October 23, 2024, 04:28:52 pm »
but I'd like to know there are any way to access the Request without using the parameters.
No, I don't think this is possible without hacking DoHandleRequest.
The variable passed on there are local. So no way to get to them.

But why would you want to?
Adding ARequest to TwmAQs.NextText is the easiest way.

egsuh

  • Hero Member
  • *****
  • Posts: 1493
Re: Any access to TRequest in webmodule outside of Action event handlers
« Reply #2 on: October 24, 2024, 03:12:05 am »
Simply I wanted to know...  I needed one method that process the same work required from many actions of fp webmodule, which will be put in private section.

Simply adding both parameters of TRequest and TResponse will let me achieve my purpose.

     
      procedure TWebModule1.SendMyResponse(req: TRequest; res: TResponse);
      begin
           if req.host = 'localhost' then...   
      end;

 

I can access the request within this method in the above way.   Even, I can do with TResponse only.

     
      procedure TWebModule1.SendMyResponse(res: TResponse);
      begin
           if res.Request.host = 'localhost' then...       
      end;



rvk

  • Hero Member
  • *****
  • Posts: 6585
Re: Any access to TRequest in webmodule outside of Action event handlers
« Reply #3 on: October 24, 2024, 09:31:38 am »
Simply I wanted to know...  I needed one method that process the same work required from many actions of fp webmodule, which will be put in private section.
Of course, you can also put the TRequest and TResponse parameters in DataModuleRequest into a private variable after which those are accessible from the other actions.

But from the code you showed, just calling one single TwmAQs.NextText, passing the variable would be the easiest.

Warfley

  • Hero Member
  • *****
  • Posts: 1762
Re: Any access to TRequest in webmodule outside of Action event handlers
« Reply #4 on: October 24, 2024, 01:55:50 pm »
There are multiple ways of doing that:
The best way is to pass the request as parameter. It's only valid within the call to the handle event, so using it as a Parameter ensures that it's lifetime is always respected
Another good way is to make NextText a nested Funktion of the handler method. Nested functions have access to the parameters (and locals defined before the nested function) of the parent. This also enforces lifetime as it's only available within the function.

Then there is the slightly worse option to put it into a thread local global variable. This way it's a global variable accessible by all functions, but localized to a thread. It works but is bad because now you don't enforce scoping and lifetime.

Lastly you can do the worst option to put the request as a class field or a global variable. This also grants access to all functions, and has the same issues that thread local global have, plus additionally if you have multi threaded request handling they override each others requests, forcing you to stick to single threaded Webservers.

egsuh

  • Hero Member
  • *****
  • Posts: 1493
Re: Any access to TRequest in webmodule outside of Action event handlers
« Reply #5 on: October 25, 2024, 03:07:45 am »
@Warfley,

Thank you for your kind advice.

I was just wondering whether there is a way to access it directly from webmodule itself.. like a hidden variable or property, i.e. outside the event handler.

The reason I need this is to separate a function/procedure shared by several (action) event handlers.
As you told, passing TRequest and TResponse as parameters is good enough, and not a problem at all.

Warfley

  • Hero Member
  • *****
  • Posts: 1762
Re: Any access to TRequest in webmodule outside of Action event handlers
« Reply #6 on: October 25, 2024, 04:16:52 pm »
This would work if there was an instantiation of the module for each request, but afaik it's not the case, so threading will come in your way.

I know that giant function signatures are annoying, but it's the safest way

 

TinyPortal © 2005-2018