• 读DataSnap源代码(二)


    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单元中:

     1 initialization
    {** 挂钩 **}

    2
    WebReq.WebRequestHandlerProc := IdHTTPWebBrokerBridgeRequestHandler;
    3 {$IFDEF HAS_CLASSVARS} 4 {$IFNDEF HAS_CLASSDESTRUCTOR} 5 finalization 6 FreeAndNil(TIdHTTPWebBrokerBridgeRequestHandler.FWebRequestHandler); 7 {$ENDIF} 8 {$ELSE} 9 finalization 10 FreeAndNil(IndyWebRequestHandler); 11 {$ENDIF}

    WebRequestHandler是Web.WebReq中的一个方法:

    1 function WebRequestHandler: TWebRequestHandler;
    2 begin
    3   if Assigned(WebRequestHandlerProc) then
    4     Result := WebRequestHandlerProc     /** 指向 IdHTTPWebBrokerBridgeRequestHandler 函数了 **/
    5   else
    6     Result := nil;
    7 end;

    IdHTTPWebBrokerBridgeRequestHandler的定义:

     1 function IdHTTPWebBrokerBridgeRequestHandler: TWebRequestHandler;
     2 begin
     3   {$IFDEF HAS_CLASSVARS}
     4   if not Assigned(TIdHTTPWebBrokerBridgeRequestHandler.FWebRequestHandler) then
     5     TIdHTTPWebBrokerBridgeRequestHandler.FWebRequestHandler := TIdHTTPWebBrokerBridgeRequestHandler.Create(nil);
     6   Result := TIdHTTPWebBrokerBridgeRequestHandler.FWebRequestHandler;
     7   {$ELSE}
     8   if not Assigned(IndyWebRequestHandler) then
     9     IndyWebRequestHandler := TIdHTTPWebBrokerBridgeRequestHandler.Create(nil);
    10   Result := IndyWebRequestHandler;
    11   {$ENDIF}
    12 end;

     还记得(一)中的那个静态成员吗? 在第4行初始化了。

    继续(一)中 Run 函数中的代码:

     1 procedure TIdHTTPWebBrokerBridgeRequestHandler.Run(AThread: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
     2 var
     3   LRequest: TIdHTTPAppRequest;
     4   LResponse: TIdHTTPAppResponse;
     5 begin
     6   try
     7     LRequest := TIdHTTPAppRequest.Create(AThread, ARequestInfo, AResponseInfo);
     8     try
     9       LResponse := TIdHTTPAppResponse.Create(LRequest, AThread, ARequestInfo, AResponseInfo);
    10       try
    11         // WebBroker will free it and we cannot change this behaviour
    12         AResponseInfo.FreeContentStream := False;
    13         HandleRequest(LRequest, LResponse);
    14       finally
    15         FreeAndNil(LResponse);
    16       end;
    17     finally
    18       FreeAndNil(LRequest);
    19     end;
    20   except
    21     // Let Indy handle this exception
    22     raise;
    23   end;
    24 end;

    TIdHTTPWebBrokerBridgeRequestHandler并没有 override 父类(TWebRequestHandler)的HandleRequest方法. 看TWebRequestHandler.HandleRequest代码

     1 function TWebRequestHandler.HandleRequest(Request: TWebRequest;
     2   Response: TWebResponse): Boolean;
     3 var
     4   I: Integer;
     5   LWebModule: TComponent;
     6   LWebAppServices: IWebAppServices;
     7   LGetWebAppServices: IGetWebAppServices;
     8   LComponent: TComponent;
     9 begin
    10   Result := False;
    11   LWebModule := ActivateWebModules;
    12   if Assigned(LWebModule) then
    13   try
    14     try
    15       if Supports(IInterface(LWebModule), IGetWebAppServices, LGetWebAppServices) then
    16         LWebAppServices := LGetWebAppServices.GetWebAppServices;
    17       if LWebAppServices = nil then
    18         for I := 0 to LWebModule.ComponentCount - 1 do
    19         begin
    20           LComponent := LWebModule.Components[I];
    21           if Supports(LComponent, IWebAppServices, LWebAppServices) then
    22             if LWebAppServices.Active then
    23               break
    24             else
    25               LWebAppServices := nil;
    26         end;
    27       if LWebAppServices = nil then
    28         LWebAppServices := TDefaultWebAppServices.Create;
    29       LWebAppServices.InitContext(LWebModule, Request, Response);
    30       try
    31         try
    32           Result := LWebAppServices.HandleRequest;
    33         except
    34           ApplicationHandleException(LWebAppServices.ExceptionHandler);
    35         end;
    36       finally
    37         LWebAppServices.FinishContext;
    38       end;
    39       if Result and not Response.Sent then
    40         Response.SendResponse;
    41     except
    42       ApplicationHandleException(LWebAppServices.ExceptionHandler);
    43     end;
    44   finally
    45     DeactivateWebModules(LWebModule);
    46   end;
    47 end;

    在上面代码中,可以看到 WebModule 字眼了, :)

  • 相关阅读:
    dos下 批处理 用 pause 可以在最后暂停 查看结果信息 build.bat
    flash jquery 调用摄像头 vue chrome49浏览器
    pandownload 百度网盘 下载
    webpack安装包的时候 1程序目录不要在C盘 2路径不要有中文 3用cnpm
    import * as tools from '@/libs/tools' 导入组件的时候 如果里面都是单独导入的,可以用 *加as起个别名使用
    markdown test
    谷歌浏览器 加安全地址 快捷方式加参数 chrome
    vue validate多表单验证思考 之前写过一个里外层,现在觉得不合适,应该平行的写,然后都给ret,最后判断ret 再做出反应,这样整体表单的所有验证就都报验证,然后最后提交的时候把组件内的对象合并到总的对象,再提交
    js引入的数组 会被页面缓存,如需要被强制性不缓存,请用function return 就ok了
    FiraCode 字体 => 箭头函数变成 整体 还有 等于 不等于
  • 原文地址:https://www.cnblogs.com/Jiaojiawang/p/4818506.html
Copyright © 2020-2023  润新知