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 字眼了, :)

读DataSnap源代码(二)的更多相关文章

  1. 读DataSnap源代码(五)

    function TDSHTTPWebDispatcher.DispatchRequest(Sender: TObject; Request: TWebRequest; Response: TWebR ...

  2. 读DataSnap源代码(一)

    Delphi的DataSnap用了一段时间了,但一直感觉有些地方还不够了解,所以花时间阅读了源代码,特作此烂笔头. Datasnap是在之前的WebBorker基础上搭建的,DataSnap向导自动生 ...

  3. 读DataSnap源代码(六)

    具体分析一下DataSanp App与Rest, WebBroker App的不同,先看TDSHTTPService. **************************************** ...

  4. 读DataSnap源代码(四)

    继续篇中的 function TCustomWebDispatcher.DispatchAction(Request: TWebRequest; Response: TWebResponse): Bo ...

  5. 读DataSnap源代码(三)

    function TWebRequestHandler.HandleRequest(Request: TWebRequest; Response: TWebResponse): Boolean; va ...

  6. session自己定义存储,怎样更好地进行session共享;读tomcat7源代码,org.apache.catalina.session.FileStore可知

    session自己定义存储.怎样更好地进行session共享: 读tomcat源代码,org.apache.catalina.session.FileStore可知 一.详见: 方法1 public ...

  7. 读Flask源代码学习Python--config原理

    读Flask源代码学习Python--config原理 个人学习笔记,水平有限.如果理解错误的地方,请大家指出来,谢谢!第一次写文章,发现好累--!. 起因   莫名其妙在第一份工作中使用了从来没有接 ...

  8. dotnet 读 WPF 源代码笔记 布局时 Arrange 如何影响元素渲染坐标

    大家是否好奇,在 WPF 里面,对 UIElement 重写 OnRender 方法进行渲染的内容,是如何受到上层容器控件的布局而进行坐标偏移.如有两个放入到 StackPanel 的自定义 UIEl ...

  9. DataSnap初步二

    转:https://blog.csdn.net/a00553344/article/details/51670486 1. 一个典型的DataSnap服务器至少需要三个控件: TDSServer: D ...

随机推荐

  1. HDU 1251 统计难题(字典树 裸题 链表做法)

    Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...

  2. 2.33 定位的坑:class属性有空格

    2.33 定位的坑:class属性有空格 前言有些class属性中间有空格,如果直接复制过来定位是会报错的InvalidSelectorException: Message:The given sel ...

  3. websocket js 代码样例

    function StartWebSocket(wsUri) { websocket = new WebSocket(wsUri); websocket.onopen = function(evt) ...

  4. Max(rowid)是否走B树索引

    Max(rowid)是否走B树索引 测试:SQL文本Max(rowid)执行计划是否走B树索引测试 一.猜测执行计划 当SQL语句中,执行max(rowid)时,执行计划猜测: A走B树索引全索引范围 ...

  5. OpenCV 自定义任意区域形状及计算平均值 方差

    opencv中有矩形的Rect函数.圆形的circl函数等,那么任意形状怎么取呢?方法1:点乘,将其形状与图像进行点乘,求其形状对应的图像形状:方法2:用findContours函数得对应的形状区域, ...

  6. vue 移动端的一些ui

    https://www.jianshu.com/p/b79b3b721cd5 Vant. YDUI. Vonic. buefy (bluma的ui) Vux

  7. HDU2029:Palindromes _easy version

    Problem Description "回文串"是一个正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.请写一个 ...

  8. 【HDOJ1598】【枚举+最小生成树】

    http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/1000 ...

  9. IDLE的使用

    为什么要用IDE? 到现在为止,我们也是写过代码的人啦,但你有没有发现,每次写代码要新建文件.写完保存时还要选择存放地点,执行时还要切换到命令行调用python解释器,好麻烦呀,能否一气呵成,让我简单 ...

  10. 《DSP using MATLAB》Problem 5.37

    证明过程: 代码: %% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...