Ignore:
Timestamp:
Dec 21, 2009, 1:19:39 PM (15 years ago)
Author:
george
Message:
  • Upraveno: Třída TWebServer oddělena do samostatné jednotky.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DirectWeb/UWebServer.pas

    r89 r91  
    1818type
    1919
    20   { TDatabasePool }
    2120
    22   TDatabasePool = class(TThreadedPool)
    23   private
    24     FActive: Boolean;
    25     procedure SetActive(const AValue: Boolean);
    26   public
    27     property Active: Boolean read FActive write SetActive;
    28   public
    29     HostName: string;
    30     Schema: string;
    31     UserName: string;
    32     Password: string;
    33     constructor Create;
    34     destructor Destroy; override;
    35   end;
    3621
    3722  { TWebServer }
     
    3924  TWebServer = class
    4025  private
    41     SessionStorage: TFileHTTPSessionStorage;
    42     procedure ServerInfo(HandlerData: THTTPHandlerData);
    4326  public
    4427    HTTPServer: THTTPServer;
    45     DatabasePool: TDatabasePool;
    46 
    47     procedure ViewList(HandlerData: THTTPHandlerData);
    48     procedure ViewItem(HandlerData: THTTPHandlerData);
    49     procedure SendIndex(HandlerData: THTTPHandlerData);
    50     procedure WriteHeader(Stream: TMemoryStreamEx);
    51     procedure WriteFooter(Stream: TMemoryStreamEx);
    5228    constructor Create;
    5329    destructor Destroy; override;
    54     procedure LoadConfiguration;
    55     procedure Run;
    5630  end;
    5731
    5832implementation
    5933
    60 procedure TWebServer.LoadConfiguration;
    61 var
    62   Config: TXMLDocument;
    63   I1: Integer;
    64   I2: Integer;
    65   I3: Integer;
    66 begin
    67   if FileExists(ConfigFileName) then begin
    68     ReadXMLFile(Config, ConfigFileName);
    69     for I1 := 0 to Config.ChildNodes.Count - 1 do
    70     with Config.ChildNodes[I1] do begin
    71       if NodeName = 'configuration' then
    72       for I2 := 0 to ChildNodes.Count - 1 do
    73       with ChildNodes[I2] do begin
    74         if NodeName = 'database' then
    75         for I3 := 0 to ChildNodes.Count - 1 do
    76         with ChildNodes[I3] do begin
    77           if NodeName = 'hostname' then
    78             DatabasePool.HostName := TextContent;
    79           if NodeName = 'schema' then
    80             DatabasePool.Schema := TextContent;
    81           if NodeName = 'username' then
    82             DatabasePool.UserName := TextContent;
    83           if NodeName = 'password' then
    84             DatabasePool.Password := TextContent;
    85         end;
    86         if NodeName = 'server' then
    87         for I3 := 0 to ChildNodes.Count - 1 do
    88         with ChildNodes[I3] do begin
    89           if NodeName = 'address' then
    90             HTTPServer.Socket.Address := TextContent;
    91           if NodeName = 'port' then
    92             HTTPServer.Socket.Port := StrToInt(TextContent);
    93         end;
    94       end;
    95     end;
    96     Config.Destroy;
    97   end;
    98 end;
    99 
    100 procedure TWebServer.Run;
    101 var
    102   Command: string;
    103 begin
    104   LoadConfiguration;
    105   DatabasePool.Active := True;
    106   WriteLn('WoW hosting web server');
    107   with HTTPServer do begin
    108     Socket.Active := True;
    109     WriteLn('Accepting connections.');
    110     repeat
    111       Write('Server command: ');
    112       ReadLn(Command);
    113     until Command = 'quit';
    114     //WaitForFinish;
    115   end;
    116 end;
    117 
    118 procedure TWebServer.ViewList(HandlerData: THTTPHandlerData);
    119 var
    120   SqlDatabase: TSqlDatabase;
    121   DbRows: TDbRows;
    122   I, II: integer;
    123   OrderColumn: string;
    124   OrderDirection: string;
    125   Title: string;
    126   LinkQuery: TQueryParameterList;
    127   PageList: TPageList;
    128 begin
    129   SqlDatabase := TSqlDatabase(DatabasePool.Acquire);
    130   with HandlerData, Response, Stream, SqlDatabase do
    131   begin
    132     WriteHeader(Stream);
    133 
    134     WriteString('<div align="center">');
    135 
    136     // Prepare table paging
    137     DbRows := Query('SELECT COUNT(*) FROM ' + Request.Query.Values['Table']);
    138     PageList := TPageList.Create;
    139     PageList.HandlerData := HandlerData;
    140     with PageList do begin
    141       TotalCount := StrToInt(DbRows[0].ValuesAtIndex[0]);
    142       ItemPerPage := 20;
    143       NavigatorVisibleItems := 5;
    144       Process;
    145     end;
    146     DbRows.Destroy;
    147 
    148     //WriteString(Request.Query.Values['Table']);
    149     OrderColumn    := Request.Query.Values['OrderCol'];
    150     OrderDirection := Request.Query.Values['OrderDir'];
    151     if OrderDirection = '1' then
    152       OrderDirection := 'ASC'
    153     else
    154       OrderDirection := 'DESC';
    155     DbRows := Query('SELECT * FROM ' + Request.Query.Values['Table'] +
    156       ' ORDER BY ' + OrderColumn + ' ' + OrderDirection + PageList.SQLLimit);
    157 
    158     LinkQuery := TQueryParameterList.Create;
    159     LinkQuery.Assign(Request.Query);
    160 
    161     WriteString('<strong>Seznam typů karet</strong><br/>');
    162     WriteString(PageList.Output);
    163     WriteString('<table><tr>');
    164     if DbRows.Count > 0 then
    165       for I := 0 to DbRows[0].Count - 1 do
    166       begin
    167         Title := DbRows[0].Names[I];
    168         LinkQuery.Values['OrderCol'] := Title;
    169         if Title = OrderColumn then
    170           LinkQuery.Values['OrderDir'] :=
    171             IntToStr(1 - StrToInt(Request.Query.Values['OrderDir']))
    172         else
    173           LinkQuery.Values['OrderDir'] := Request.Query.Values['OrderDir'];
    174         Title := '<a href="?' + LinkQuery.Syntetize + '">' + Title + '</a>';
    175 
    176         WriteString('<th>' + Title + '</th>');
    177       end;
    178     WriteString('</tr>');
    179 
    180     LinkQuery.Destroy;
    181     for II := 0 to DbRows.Count - 1 do
    182     begin
    183       WriteString('<tr>');
    184       for I := 0 to DbRows[II].Count - 1 do
    185         WriteString('<td>' + DbRows[II].ValuesAtIndex[I] + '</td>');
    186       WriteString('</tr>');
    187     end;
    188     WriteString('</table>');
    189     WriteString(PageList.Output);
    190     WriteString('</div>');
    191 
    192     DbRows.Destroy;
    193     WriteFooter(Stream);
    194   end;
    195   PageList.Destroy;
    196   DatabasePool.Release(SqlDatabase);
    197 end;
    198 
    199 procedure TWebServer.ViewItem(HandlerData: THTTPHandlerData);
    200 var
    201   SqlDatabase: TSqlDatabase;
    202   DbRows: TDbRows;
    203   I, II: Integer;
    204   OrderColumn: string;
    205   OrderDirection: string;
    206   Title: string;
    207   LinkQuery: TQueryParameterList;
    208 begin
    209   SqlDatabase := TSqlDatabase(DatabasePool.Acquire);
    210   with HandlerData, Response, Stream, SqlDatabase do
    211   begin
    212     WriteHeader(Stream);
    213     DbRows := Query('SELECT * FROM ' + Request.Query.Values['Table'] +
    214       ' WHERE Id=' + Request.Query.Values['Id']);
    215     if DbRows.Count > 0 then begin
    216       WriteString('<strong>Zobrazení položky</strong>');
    217       WriteString('<table><tr><th>Vlastnost</th><th>Hodnota</th></tr>');
    218       for I := 0 to DbRows[0].Count - 1 do begin
    219         WriteString('<tr><td>' + DbRows[0].Names[I] + '</td><td>' +
    220           DbRows[0].ValuesAtIndex[I] + '</td></tr>');
    221       end;
    222     end else WriteString('Položka nenalezena.');
    223     WriteString('</table>');
    224     DbRows.Destroy;
    225     WriteFooter(Stream);
    226   end;
    227   DatabasePool.Release(SqlDatabase);
    228 end;
    229 
    230 procedure TWebServer.ServerInfo(HandlerData: THTTPHandlerData);
    231 var
    232   I: Integer;
    233 begin
    234   with HandlerData, Response.Stream do begin
    235     //Response.Cookies.Values['Test'] := 'Halo';
    236     //Response.Cookies.Values['Test2'] := 'Halo2';
    237 
    238     //HTTPServer.SessionHandler.Variables.Values['Session1'] := 'Value1';
    239     //HTTPServer.SessionHandler.Variables.Values['Session2'] := 'Value2';
    240 
    241     WriteString('<a href="?ServerInfo">Refresh</a>');
    242 
    243     WriteString('<h5>Request HTTP headers</h5>');
    244     for I := 0 to Request.Headers.Count - 1 do begin;
    245       WriteString(Request.Headers.Strings[I] + '<br/>');
    246     end;
    247 
    248     WriteString('<h5>Request HTTP cookies</h5>');
    249     for I := 0 to Request.Cookies.Count - 1 do begin;
    250       WriteString(Request.Cookies.Strings[I] + '<br/>');
    251     end;
    252 
    253     WriteString('<h5>Session variables</h5>');
    254     for I := 0 to Session.Count - 1 do begin;
    255       WriteString(Session.Strings[I] + '<br/>');
    256     end;
    257 
    258     WriteString('<h5>Response HTTP headers</h5>');
    259     with Response.Stream do
    260     for I := 0 to Response.Headers.Count - 1 do begin;
    261       WriteString(Response.Headers.Strings[I] + '<br/>');
    262     end;
    263   end;
    264 end;
    265 
    266 procedure TWebServer.SendIndex(HandlerData: THTTPHandlerData);
    267 begin
    268   with HandlerData, Response, Stream do
    269   begin
    270     WriteHeader(Stream);
    271     WriteString('Index');
    272     WriteFooter(Stream);
    273   end;
    274 end;
    275 
    276 procedure TWebServer.WriteHeader(Stream: TMemoryStreamEx);
    277 begin
    278   with Stream do
    279   begin
    280     WriteString('<?xml version="1.0" encoding="UTF-8"?>');
    281     WriteString('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">');
    282     WriteString('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs">');
    283     WriteString('<head>');
    284     WriteString('<title>');
    285     WriteString('Dispečink výtahů');
    286     WriteString('</title>');
    287     WriteString('<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8"/>');
    288     WriteString('<link rel="stylesheet" href="Style.css" type="text/css" media="all"/>');
    289     WriteString('<script type="text/javascript" src="Application/Style/Custom/Global.js">');
    290     WriteString('</script>');
    291     WriteString('<script type="text/javascript" src="Base/Style/jquery.js">');
    292     WriteString('</script>');
    293     WriteString('</head>');
    294     WriteString('<body>');
    295   end;
    296 end;
    297 
    298 procedure TWebServer.WriteFooter(Stream: TMemoryStreamEx);
    299 begin
    300   with Stream do
    301   begin
    302     WriteString('</body>');
    303     WriteString('</html>');
    304   end;
    305 end;
    306 
    30734constructor TWebServer.Create;
    30835begin
    30936  inherited Create;
    310   DatabasePool := TDatabasePool.Create;
    311   DatabasePool.TotalCount := 20;
    312 
    313   SessionStorage := TFileHTTPSessionStorage.Create;
    31437  HTTPServer := THTTPServer.Create;
    31538  with HTTPServer, Socket do begin
    316     SessionStorage := Self.SessionStorage;
    317     DocumentRoot := 'Data';
    318     with RequestHandlerList do begin
    319       Add('/', SendIndex);
    320       Add('/index.htm', SendIndex);
    321       Add('/index.html', SendIndex);
    322       Add('/logo.png', FileResponse);
    323       Add('/Style.css', FileResponse);
    324       Add('/ViewList', ViewList);
    325       Add('/ViewItem', ViewItem);
    326       Add('/ServerInfo', ServerInfo);
    327     end;
    32839  end;
    32940end;
     
    33142destructor TWebServer.Destroy;
    33243begin
    333   DatabasePool.Destroy;
    33444  HTTPServer.Destroy;
    335   SessionStorage.Destroy;
    33645  inherited Destroy;
    33746end;
    33847
    339 { TDatabasePool }
    340 
    341 procedure TDatabasePool.SetActive(const AValue: Boolean);
    342 var
    343   I: Integer;
    344 begin
    345   if not FActive and AValue then begin
    346     for I := 0 to TotalCount - 1 do
    347     with TThreadedPoolItem(Items[I]) do begin
    348       Item := TSqlDatabase.Create;
    349       with TSqlDatabase(Item) do begin
    350         HostName := Self.HostName;
    351         UserName := Self.UserName;
    352         Password := Self.Password;
    353         Database := Self.Schema;
    354         Connect;
    355       end;
    356     end;
    357   end else
    358   if FActive and not AValue then begin
    359 
    360   end;
    361   FActive := AValue;
    362 end;
    363 
    364 constructor TDatabasePool.Create;
    365 begin
    366   inherited;
    367 end;
    368 
    369 destructor TDatabasePool.Destroy;
    370 begin
    371   inherited Destroy;
    372 end;
    37348
    37449end.
Note: See TracChangeset for help on using the changeset viewer.