tags:

views:

89

answers:

2

I'm using the TWebModule component to write a web server application with Delphi. Clicking on the Actions property of the TWebModule a new action can be defined and a "OnAction" event handler created. For example:

procedure TMainWeb.MyAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  Response.Content := '<html><body>myvariable: '+request.queryfields.values['myvariable']+</body></html>';
end;

I have noticed non-parametered procedures can be called which have access to the TWebModule's Request, Response, and Handled parameters. For instance, I have successfully used the following instead of explicitly created action handlers:

procedure TMainWeb.WebModuleBeforeDispatch(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  if Pos('myaction.html',request.url)>0 then
    DoMyAction;
end;


procedure TMainWeb.DoMyAction;
begin
  Response.Content := '<html><body>myvariable: '+request.queryfields.values['myvariable']+</body></html>';

end;

Can I always be assured the references to Sender, Request, Response, and Handled I make in DoMyAction are the "correct" ones?

+3  A: 

No, you can't be assured of that in all cases and you're preparing a maintenance nightmare.

Why don't you create a DoMyAction that takes as parameters whatever you need inside from Request, Response and Handled?

With your example it would become:

procedure TMainWeb.WebModuleBeforeDispatch(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  if Pos('myaction.html',request.url)>0 then
  begin
    DoMyAction(Request, Response);
    Handled := True;
  end;
end;


procedure TMainWeb.DoMyAction(ARequest: TWebRequest; AResponse: TWebResponse);
begin
  AResponse.Content := '<html><body>myvariable: '+ARequest.queryfields.values['myvariable']+</body></html>';

end;
François
Thanks for the reply. Now that I know they aren't always necessarily the same I will create parameterized calls. Why then, are these visible (i.e. Request, Response) within the TWebModule class? It seems it could just lead people astray to think they are the same. Also, I have a couple web applications already released with a lot of situations like this embedded in them. But as far as I know there have been no problems. But then again maybe there are but I just haven't been made aware.
M Schenkel
+2  A: 

A TWebModule instance is created (or grabbed from a pre-allocated pool) for each request as it is processed. The Request and Response are available as properties on the instance. As long as you don't go trying to access another TWebModule instance, the Request/Response properties will be valid through out the lifetime of the request. If you call other methods on the TWebModule, you don't have to qualify their use. If you intend to have them accessible to other object methods or global procedures/functions, you need to pass them along as parameters.

Allen Bauer
How would you access another TWebModule instance? Is there some maintained list of TWebModules? Example: TWebModule(WebModulesList[iWebModule]).Request (note WebModulesList is something I construed). So in the call to TMainWeb.DoMyAction of my original question I can assume the references to Request, Response and Handled of TWebModule would be the same as if I had passed them as parameters? This tends to negates Francois's answer (with the assumptions of not accessing another TWebModule instance).
M Schenkel
Yes, there is an internal list of active and inactive web modules. When a request comes in, it grabs an inactive module from one list and adds it to the active list. If one isn't available, a new one is created. It is not advisable (nor is it particularly easy) at all to attempt to access another TWebModule instance. It may be in the middle of handling another request which would mean you'd stomp all over it, or at least get inconsistent data. François may have thought those properties were global in scope, in which case he'd be right. Request/Response are scoped to the web module.
Allen Bauer