Ignore:
Timestamp:
Jul 20, 2011, 11:42:23 PM (13 years ago)
Author:
george
Message:
  • Modified: Enhanced TWebApp class.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Network/CoolWeb/WebServer/UWebApp.pas

    r255 r256  
    11unit UWebApp;
    22
    3 {$mode objfpc}{$H+}
     3{$mode Delphi}{$H+}
    44
    55interface
    66
    77uses
    8   Classes, SysUtils, CustApp, SpecializedList, UWebPage;
     8  Classes, SysUtils, CustApp, SpecializedList, UWebPage, UHTTPSessionFile,
     9  UHTTPServer, UHTTPServerCGI;
    910
    1011type
    1112  TRegistredPage = class
    12     Path: string;
     13    Name: string;
    1314    Page: TWebPage;
     15  end;
     16
     17  { TRegistredPageList }
     18
     19  TRegistredPageList = class(TListObject)
     20    function FindByName(Name: string): TRegistredPage;
    1421  end;
    1522
     
    1825  TWebApp = class(TCustomApplication)
    1926  private
     27    FOnInitialize: TNotifyEvent;
    2028    procedure DoRun; override;
     29    procedure HTTPServerRequest(HandlerData: THTTPHandlerData);
    2130  public
    22     Pages: TListObject;
     31    Pages: TRegistredPageList;
     32    HTTPServer: THTTPServer;
     33    HTTPSessionStorageFile: THTTPSessionStorageFile;
    2334    procedure RegisterPage(PageClass: TWebPageClass; out Reference; Path: string);
    2435    constructor Create(AOwner: TComponent); override;
    2536    destructor Destroy; override;
     37    property OnInitialize: TNotifyEvent read FOnInitialize write FOnInitialize;
    2638  end;
    2739
     
    2941procedure Register;
    3042
     43var
     44  Application: TWebApp;
     45
    3146
    3247implementation
     48
     49resourcestring
     50  SPageNotFound = 'Page not found';
     51
    3352
    3453procedure Register;
     
    3756end;
    3857
     58{ TRegistredPageList }
     59
     60function TRegistredPageList.FindByName(Name: string): TRegistredPage;
     61var
     62  I: Integer;
     63begin
     64  I := 0;
     65  while (I < Count) and (TRegistredPage(Items[I]).Name <> Name) do Inc(I);
     66  if I < Count then Result := TRegistredPage(Items[I])
     67    else Result := nil;
     68end;
     69
    3970{ TWebApp }
    4071
    4172procedure TWebApp.DoRun;
    4273begin
     74  if Assigned(FOnInitialize) then FOnInitialize(Self);
     75  HTTPServer.Run;
    4376  Terminate;
    4477end;
     
    5386//  NewPage.Page := PageClass.Create(Self);
    5487  NewPage.Page := PageClass.Create(nil);
    55   NewPage.Path := Path;
     88  NewPage.Name := Path;
    5689  TWebPage(Reference) := NewPage.Page;
     90end;
     91
     92procedure TWebApp.HTTPServerRequest(HandlerData: THTTPHandlerData);
     93var
     94  Page: TRegistredPage;
     95  PageName: string;
     96begin
     97  with HandlerData do begin
     98    //Request.QueryParts.Count := 2;
     99    //Request.QueryParts[0] := 'uzivatel';
     100    //Request.QueryParts[1] := 'prihlaseni';
     101
     102    if Request.QueryParts.Count > 0 then PageName := Request.QueryParts[0]
     103      else PageName := '';
     104    Page := Pages.FindByName(PageName);
     105    if Assigned(Page) then begin
     106      Page.Page.OnProduce(HandlerData);
     107    end else Response.Stream.WriteString(SPageNotFound);
     108  end;
    57109end;
    58110
     
    60112begin
    61113  inherited Create(AOwner);
    62   Pages := TListObject.Create;
     114  Pages := TRegistredPageList.Create;
     115  HTTPServer := THTTPServerCGI.Create(nil);
     116  HTTPServer.OnRequest := HTTPServerRequest;
    63117end;
    64118
     
    66120begin
    67121  Pages.Free;
     122  HTTPServer.Free;
    68123  inherited Destroy;
    69124end;
    70125
     126
     127initialization
     128
     129Application := TWebApp.Create(nil);
     130
     131
     132finalization
     133
     134Application.Free;
     135
    71136end.
    72137
Note: See TracChangeset for help on using the changeset viewer.