Ignore:
Timestamp:
Dec 18, 2009, 12:21:39 PM (15 years ago)
Author:
george
Message:
  • Upraveno: Nedokončené přepracování správy vláken. Použit systém přidělování ze společné sady o dané velikosti namísto přidělování vláken ke každému požadavku samostatně. Vlákna jsou inicializována při spuštění aplikace, jsou opakovaně používána a uvolněna až při ukončení běhu aplikace.
  • Přidáno: Třída pro práci s opakovaně spustitelnými vlákny.
  • Přidáno: Třída přidělování objektů ze zásoby o dané velikosti.
  • Přidáno: Třída pro zobrazování stránkování seznamů položek.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DirectWeb/UHTTPServer.pas

    r87 r88  
    1010
    1111type
     12  THTTPServer = class;
    1213
    1314  TQueryParameterList = class(TStringList)
     
    2829    Query: TQueryParameterList;
    2930    Path: string;
    30     Host: string;
    3131    Method: string;
    3232    Headers: TStringList;
     
    4747  end;
    4848
    49   TRequestEvent = procedure(Request: THTTPRequest; Response: THTTPResponse) of object;
     49  { THTTPHandlerData }
     50
     51  THTTPHandlerData = class
     52    Server: THTTPServer;
     53    Request: THTTPRequest;
     54    Response: THTTPResponse;
     55    SessionId: string;
     56    Session: TStringList;
     57    constructor Create;
     58    destructor Destroy; override;
     59  end;
     60
     61  TRequestEvent = procedure(HandlerData: THTTPHandlerData) of object;
    5062
    5163  TRequestHandler = class
     
    6375  { THTTPSession }
    6476
    65   THTTPSession = class
    66     Variables: TStringList;
    67     procedure Load(Request: THTTPRequest); virtual;
    68     procedure Save(Response: THTTPResponse); virtual;
     77  { THTTPSessionStorage }
     78
     79  THTTPSessionStorage = class
     80    procedure Load(HandlerData: THTTPHandlerData); virtual;
     81    procedure Save(HandlerData: THTTPHandlerData); virtual;
    6982    constructor Create; virtual;
    7083    destructor Destroy; override;
     
    7689  private
    7790    procedure HandleClient(Sender: TObject);
    78     procedure ErrorResponse(Request: THTTPRequest; Response: THTTPResponse);
     91    procedure ErrorResponse(HandlerData: THTTPHandlerData);
    7992  public
    8093    Name: string;
     
    8396    MaxConnection: Integer;
    8497    RequestHandlerList: TRequestHandlerList;
    85     SessionHandler: THTTPSession;
    86     procedure FileResponse(Request: THTTPRequest;
    87       Response: THTTPResponse);
     98    SessionStorage: THTTPSessionStorage;
     99    procedure FileResponse(HandlerData: THTTPHandlerData);
    88100    constructor Create;
    89101    destructor Destroy; override;
     
    100112  LineIndex: Integer;
    101113  LineParts: array of string;
    102   Request: THTTPRequest;
    103   Response: THTTPResponse;
     114  HandlerData: THTTPHandlerData;
    104115  I: Integer;
    105116  QueryString: string;
    106117begin
    107118  with TTCPClientThread(Sender), Socket do begin
    108     WriteLn('Client ' + IntToStr(Parent.IndexOf(Sender)) + ' connected from ' + GetRemoteSinIP);
     119    WriteLn('Used thrads ' + IntToStr(Parent.ThreadPool.UsedCount) + '. Client connected from ' + GetRemoteSinIP);
     120
     121    HandlerData := THTTPHandlerData.Create;
     122    with HandlerData do begin
     123      Server := Self;
    109124
    110125    Response := THTTPResponse.Create;
     
    128143          LineParts[0] := Copy(LineParts[0], 1, Length(LineParts[0]) - 1);
    129144          Request.Headers.Values[LineParts[0]] := LineParts[1];
    130           WriteLn(Line);
     145          //WriteLn(Line);
    131146        end;
    132147      end;
     
    138153      Request.Cookies.Parse(Request.Headers.Values['Cookie']);
    139154
    140     // Process session
    141     if Assigned(SessionHandler) then
    142       SessionHandler.Load(Request);
     155    // Load session variables
     156    if Assigned(SessionStorage) then
     157      SessionStorage.Load(HandlerData);
    143158
    144159    Response.Stream.Clear;
     
    148163    RequestHandler := RequestHandlerList.IndexOfName(Request.Path);
    149164
    150     if Assigned(RequestHandler) then RequestHandler.Handler(Request, Response)
    151       else ErrorResponse(Request, Response);
    152 
    153     if Assigned(SessionHandler) then
    154       SessionHandler.Save(Response);
     165    if Assigned(RequestHandler) then RequestHandler.Handler(HandlerData)
     166      else ErrorResponse(HandlerData);
     167
     168    // Store session variables
     169    if Assigned(SessionStorage) then
     170      SessionStorage.Save(HandlerData);
    155171
    156172    with Response do begin
     
    167183      // Send headers
    168184      for I := 0 to Headers.Count - 1 do begin
    169         WriteLn(Headers.Names[I] + ': ' + Headers.ValueFromIndex[I] + #13#10);
     185        //WriteLn(Headers.Names[I] + ': ' + Headers.ValueFromIndex[I] + #13#10);
    170186        SendString(Headers.Names[I] + ': ' + Headers.ValueFromIndex[I] + #13#10);
    171187      end;
     
    175191    end;
    176192
    177     Request.Destroy;
    178     Response.Destroy;
    179   end;
    180 end;
    181 
    182 procedure THTTPServer.ErrorResponse(Request: THTTPRequest;
    183   Response: THTTPResponse);
    184 begin
    185   with Response.Stream do begin
     193      Destroy;
     194    end;
     195  end;
     196end;
     197
     198procedure THTTPServer.ErrorResponse(HandlerData: THTTPHandlerData);
     199begin
     200  with HandlerData, Response.Stream do begin
    186201    WriteString('<html><body>Page ' + Request.Path + ' not found.</body></html>');
    187202  end;
    188203end;
    189204
    190 procedure THTTPServer.FileResponse(Request: THTTPRequest;
    191   Response: THTTPResponse);
     205procedure THTTPServer.FileResponse(HandlerData: THTTPHandlerData);
    192206var
    193207  BinaryFile: TFileStream;
    194208  FileName: string;
    195209begin
    196   FileName := DocumentRoot + Request.Path;
    197   if FileExists(FileName) then begin
    198     Response.Headers.Values['Content-Type'] := GetMIMEType(Copy(ExtractFileExt(FileName), 2, 255));
    199     BinaryFile := TFileStream.Create(FileName, fmOpenRead);
    200     Response.Stream.WriteStream(BinaryFile);
    201     BinaryFile.Destroy;
    202   end else
    203   with Response.Stream do begin
    204     WriteLn('File ' + Request.Path + ' not found.');
    205     WriteString('<html><body>File ' + Request.Path + ' not found.</body></html>');
     210  with HandlerData do begin
     211    FileName := DocumentRoot + Request.Path;
     212    if FileExists(FileName) then begin
     213      Response.Headers.Values['Content-Type'] := GetMIMEType(Copy(ExtractFileExt(FileName), 2, 255));
     214      BinaryFile := TFileStream.Create(FileName, fmOpenRead);
     215      Response.Stream.WriteStream(BinaryFile);
     216      BinaryFile.Destroy;
     217    end else
     218    with Response.Stream do begin
     219      WriteLn('File ' + Request.Path + ' not found.');
     220      WriteString('<html><body>File ' + Request.Path + ' not found.</body></html>');
     221    end;
    206222  end;
    207223end;
     
    329345end;
    330346
    331 { THTTPSession }
    332 
    333 procedure THTTPSession.Load(Request: THTTPRequest);
    334 begin
    335 
    336 end;
    337 
    338 procedure THTTPSession.Save(Response: THTTPResponse);
    339 begin
    340 
    341 end;
    342 
    343 constructor THTTPSession.Create;
    344 begin
    345   Variables := TStringList.Create;
    346 end;
    347 
    348 destructor THTTPSession.Destroy;
    349 begin
    350   Variables.Destroy;
     347{ THTTPSessionStorage }
     348
     349procedure THTTPSessionStorage.Load(HandlerData: THTTPHandlerData);
     350begin
     351
     352end;
     353
     354procedure THTTPSessionStorage.Save(HandlerData: THTTPHandlerData);
     355begin
     356
     357end;
     358
     359constructor THTTPSessionStorage.Create;
     360begin
     361
     362end;
     363
     364destructor THTTPSessionStorage.Destroy;
     365begin
     366  inherited Destroy;
     367end;
     368
     369{ THTTPHandlerData }
     370
     371constructor THTTPHandlerData.Create;
     372begin
     373  Request := THTTPRequest.Create;
     374  Response := THTTPResponse.Create;
     375  Session := TStringList.Create;
     376end;
     377
     378destructor THTTPHandlerData.Destroy;
     379begin
     380  Request.Destroy;
     381  Response.Destroy;
     382  Session.Destroy;
    351383  inherited Destroy;
    352384end;
Note: See TracChangeset for help on using the changeset viewer.