jjw

写给自己的博客。 记录学习的点滴以备查。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

读DataSnap源代码(四)

Posted on 2015-09-18 11:52  jjw  阅读(616)  评论(0编辑  收藏  举报

继续篇中的

 1 function TCustomWebDispatcher.DispatchAction(Request: TWebRequest;
 2   Response: TWebResponse): Boolean;
 3 var
 4   I: Integer;
 5   Action, Default: TWebActionItem;
 6   Dispatch: IWebDispatch;
 7 begin
 8   FRequest := Request;
 9   FResponse := Response;
10   I := 0;
11   Default := nil;
12   if Response.Sent then
13   begin
14     Result := True;
15     { Note that WebSnapSvr enabled apps have no way to mark response as sent }
16     Exit;
17   end;
18   Result := DoBeforeDispatch(Request, Response) or Response.Sent;
19   while not Result and (I < FActions.Count) do
20   begin
21     Action := FActions[I];
22     Result := Action.DispatchAction(Request, Response, False);
23     if Action.Default then Default := Action;
24     Inc(I);
25   end;
26   // Dispatch to self registering components
27   I := 0;
28   while not Result and (I < FDispatchList.Count) do
29   begin
30     if Supports(IInterface(FDispatchList.Items[I]), IWebDispatch, Dispatch) then
31     begin
32       Result := DispatchHandler(Self, Dispatch,
33         Request, Response, False);
34     end;
35     Inc(I);
36   end;
37 
38   if not Result and Assigned(Default) then
39     Result := Default.DispatchAction(Request, Response, True);
40   if Result and not Response.Sent then
41     Result := DoAfterDispatch(Request, Response);
42 
43 end;

在第26行代码之前,是执行WebModule中的Action, 然后是遍例WebModule上的Component, 只要支持IWebDispatch,都有机会处理WEB请求。

当然就包括了   TDSHTTPWebDispatcher = class(TDSHTTPServerTransport, IWebDispatch)。 继续代码:

 1 function DispatchHandler(Sender: TObject; Dispatch: IWebDispatch; Request: TWebRequest; Response: TWebResponse;
 2   DoDefault: Boolean): Boolean;
 3 begin
 4   Result := False;
 5   if (Dispatch.Enabled and ((Dispatch.MethodType = mtAny) or
 6     (Request.MethodType = Dispatch.MethodType)) and
 7     Dispatch.Mask.Matches(string(Request.InternalPathInfo))) then
 8   begin
 9     Result := Dispatch.DispatchRequest(Sender, Request, Response);
10   end;
11 end;
function TDSHTTPWebDispatcher.DispatchRequest(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse): Boolean;
begin
  try
    if Owner is TWebModule then
      DataSnapWebModule := TWebModule(Owner);
    try
      try
        RequiresServer;
        TDSHTTPServerWebBroker(Self.FHttpServer).DispatchDataSnap(Request, Response);
        Result := True;
      except
        on E: Exception do
        begin
          { Default to 500, like web services. }
          Response.StatusCode := 500;
          Result := True;
        end;
      end;
    except
      { Swallow any unexpected exception, it will bring down some web servers }
      Result := False;
    end;
  finally
    { Reset current DataSnapWebModule }
    DataSnapWebModule := nil;
  end;
end;

 至此,WEB请求转入到WebModule中的TDSHTTPWebDispatcher来处理了。