读DataSnap源代码(三)

时间:2023-03-09 16:12:20
读DataSnap源代码(三)
 function TWebRequestHandler.HandleRequest(Request: TWebRequest;
Response: TWebResponse): Boolean;
var
I: Integer;
LWebModule: TComponent;
LWebAppServices: IWebAppServices;
LGetWebAppServices: IGetWebAppServices;
LComponent: TComponent;
begin
Result := False;
11 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;

第11行代码,先得到一个激活的WebModule,如何没有,就会创建一个再返回。

第14行到第26行代码,判断WebModule中是否实现了 IGetWebAppServices 接口的,具体做什么先略过。因为WebModule没有继承这个接口。

第27行,创建一个默认的WebAppServices

第32行,执行默认操作。

 function TDefaultWebAppServices.HandleRequest: Boolean;
begin
Result := InvokeDispatcher;
end; function TDefaultWebAppServices.InvokeDispatcher: Boolean;
begin
if RequestHandler <> nil then
begin
Result := RequestHandler.HandleRequest(Request, Response);
end
else
raise EWebBrokerException.CreateRes(@sNoDispatcherComponent);
end;

上面的RequestHandler是

property RequestHandler: IWebRequestHandler read GetRequestHandler;

function TDefaultWebAppServices.GetRequestHandler: IWebRequestHandler;
begin
if FRequestHandler = nil then
FRequestHandler := FindRequestHandler;
Result := FRequestHandler;
end; function TDefaultWebAppServices.FindRequestHandler: IWebRequestHandler;
var
Component: TComponent;
begin
Result := nil;
Component := FindWebDispatcher;
if Component <> nil then
if not Supports(Component, IWebRequestHandler, Result) then
Assert(False, 'Expect support for IWebRequestHandler'); { do not localize }
end; function TDefaultWebAppServices.FindWebDispatcher: TComponent;
var
  J: Integer;
begin
  Result := nil;
  if WebModule is TCustomWebDispatcher then
    Result := WebModule
  else
    for J := 0 to WebModule.ComponentCount - 1 do
      if WebModule.Components[J] is TCustomWebDispatcher then
      begin
        Result := WebModule.Components[J];
        break;
      end;
end;

上面红色标记的代码,最终会返回 WebModule.

TWebModule = class(TCustomWebDispatcher)

所以,上面绿色标记的代码,是调用TCustomWebDispatcher.HandleRequest方法, 内部代码如下:

function TCustomWebDispatcher.HandleRequest(
Request: TWebRequest; Response: TWebResponse): Boolean;
begin
FRequest := Request;
FResponse := Response;
Result := DispatchAction(Request, Response);
end;