program Project1;
{$APPTYPE GUI} {$R *.dres} uses
Vcl.Forms,
Web.WebReq,
IdHTTPWebBrokerBridge,
FormUnit1 in 'FormUnit1.pas' {Form1},
ServerMethodsUnit1 in 'ServerMethodsUnit1.pas',
WebModuleUnit1 in 'WebModuleUnit1.pas' {WebModule1: TWebModule}; {$R *.res} begin
{ 返回是什么,见下面的【绿色代码】 }
if WebRequestHandler <> nil then
WebRequestHandler.WebModuleClass := WebModuleClass;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
IdHTTPWebBrokerBridge单元中:
initialization
{** 挂钩 **}
2 WebReq.WebRequestHandlerProc := IdHTTPWebBrokerBridgeRequestHandler;
{$IFDEF HAS_CLASSVARS}
{$IFNDEF HAS_CLASSDESTRUCTOR}
finalization
FreeAndNil(TIdHTTPWebBrokerBridgeRequestHandler.FWebRequestHandler);
{$ENDIF}
{$ELSE}
finalization
FreeAndNil(IndyWebRequestHandler);
{$ENDIF}
WebRequestHandler是Web.WebReq中的一个方法:
function WebRequestHandler: TWebRequestHandler;
begin
if Assigned(WebRequestHandlerProc) then
Result := WebRequestHandlerProc /** 指向 IdHTTPWebBrokerBridgeRequestHandler 函数了 **/
else
Result := nil;
end;
IdHTTPWebBrokerBridgeRequestHandler的定义:
function IdHTTPWebBrokerBridgeRequestHandler: TWebRequestHandler;
begin
{$IFDEF HAS_CLASSVARS}
if not Assigned(TIdHTTPWebBrokerBridgeRequestHandler.FWebRequestHandler) then
TIdHTTPWebBrokerBridgeRequestHandler.FWebRequestHandler := TIdHTTPWebBrokerBridgeRequestHandler.Create(nil);
Result := TIdHTTPWebBrokerBridgeRequestHandler.FWebRequestHandler;
{$ELSE}
if not Assigned(IndyWebRequestHandler) then
IndyWebRequestHandler := TIdHTTPWebBrokerBridgeRequestHandler.Create(nil);
Result := IndyWebRequestHandler;
{$ENDIF}
end;
还记得(一)中的那个静态成员吗? 在第4行初始化了。
继续(一)中 Run 函数中的代码:
procedure TIdHTTPWebBrokerBridgeRequestHandler.Run(AThread: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
LRequest: TIdHTTPAppRequest;
LResponse: TIdHTTPAppResponse;
begin
try
LRequest := TIdHTTPAppRequest.Create(AThread, ARequestInfo, AResponseInfo);
try
LResponse := TIdHTTPAppResponse.Create(LRequest, AThread, ARequestInfo, AResponseInfo);
try
// WebBroker will free it and we cannot change this behaviour
AResponseInfo.FreeContentStream := False;
HandleRequest(LRequest, LResponse);
finally
FreeAndNil(LResponse);
end;
finally
FreeAndNil(LRequest);
end;
except
// Let Indy handle this exception
raise;
end;
end;
TIdHTTPWebBrokerBridgeRequestHandler并没有 override 父类(TWebRequestHandler)的HandleRequest方法. 看TWebRequestHandler.HandleRequest代码
function TWebRequestHandler.HandleRequest(Request: TWebRequest;
Response: TWebResponse): Boolean;
var
I: Integer;
LWebModule: TComponent;
LWebAppServices: IWebAppServices;
LGetWebAppServices: IGetWebAppServices;
LComponent: TComponent;
begin
Result := False;
LWebModule := ActivateWebModules;
if Assigned(LWebModule) then
try
try
if Supports(IInterface(LWebModule), IGetWebAppServices, LGetWebAppServices) then
LWebAppServices := LGetWebAppServices.GetWebAppServices;
if LWebAppServices = nil then
for I := to LWebModule.ComponentCount - do
begin
LComponent := LWebModule.Components[I];
if Supports(LComponent, IWebAppServices, LWebAppServices) then
if LWebAppServices.Active then
break
else
LWebAppServices := nil;
end;
if LWebAppServices = nil then
LWebAppServices := TDefaultWebAppServices.Create;
LWebAppServices.InitContext(LWebModule, Request, Response);
try
try
Result := LWebAppServices.HandleRequest;
except
ApplicationHandleException(LWebAppServices.ExceptionHandler);
end;
finally
LWebAppServices.FinishContext;
end;
if Result and not Response.Sent then
Response.SendResponse;
except
ApplicationHandleException(LWebAppServices.ExceptionHandler);
end;
finally
DeactivateWebModules(LWebModule);
end;
end;
在上面代码中,可以看到 WebModule 字眼了, :)