Ignore:
Timestamp:
Sep 9, 2022, 8:20:25 PM (2 years ago)
Author:
chronos
Message:
  • Modified: Removed TemplateGenerics package. Generics usage replaced by standard Generics.Collections.
Location:
trunk/Packages/CoolWeb/WebServer
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/CoolWeb/WebServer/UHTTPServer.pas

    r137 r138  
    44
    55uses
    6   Classes, SysUtils, UCommon, UMemoryStreamEx, UMIMEType,
    7   Synautil, SpecializedList, SpecializedDictionary, Syncobjs;
     6  Classes, SysUtils, UCommon, UMemoryStreamEx, UMIMEType, Synautil, Syncobjs,
     7  Generics.Collections, UGenerics;
    88
    99type
     
    7575  { TRequestHandlerList }
    7676
    77   TRequestHandlerList = class(TListObject)
     77  TRequestHandlerList = class(TObjectList<TRequestHandler>)
    7878    procedure Add(AName: string; AHandler: TRequestEvent);
    7979    function IndexOfName(AName: string): TRequestHandler;
     
    131131var
    132132  I: Integer;
     133  Item: TPair<string, string>;
    133134begin
    134135  with HandlerData, Response.Content do begin
     
    145146
    146147    WriteString('<h5>Request HTTP headers</h5>');
    147     for I := 0 to Request.Headers.Count - 1 do begin;
    148       with Request.Headers.Items[I] do
    149         WriteString(Key + ': ' + Value + '<br/>');
     148    for Item in Request.Headers do begin;
     149      WriteString(Item.Key + ': ' + Item.Value + '<br/>');
    150150    end;
    151151
    152152    WriteString('<h5>Request HTTP GET</h5>');
    153     for I := 0 to Request.Query.Count - 1 do begin
    154       with Request.Query.Items[I] do
    155       WriteString(Key + ': ' + Value + '<br/>');
     153    for Item in Request.Query do begin
     154      WriteString(Item.Key + ': ' + Item.Value + '<br/>');
    156155    end;
    157156
    158157    WriteString('<h5>Request HTTP cookies</h5>');
    159158    for I := 0 to Request.Cookies.Count - 1 do begin
    160       with Request.Cookies.Items[I] do
    161       WriteString(Key + ': ' + Value + '<br/>');
     159      WriteString(Item.Key + ': ' + Item.Value + '<br/>');
    162160    end;
    163161
     
    170168
    171169    WriteString('<h5>Request HTTP POST</h5>');
    172     for I := 0 to Request.Post.Count - 1 do begin
    173       with Request.Post.Items[I] do
    174       WriteString(Key + ': ' + Value + '<br/>');
     170    for Item in Request.Post do begin
     171      WriteString(Item.Key + ': ' + Item.Value + '<br/>');
    175172    end;
    176173
     
    181178    WriteString('<h5>Response HTTP headers</h5>');
    182179    with Response.Content do
    183     for I := 0 to Response.Headers.Count - 1 do begin
    184       with Response.Headers.Items[I] do
    185       WriteString(Key + ': ' + Value + '<br/>');
     180    for Item in Response.Headers do begin
     181      WriteString(Item.Key + ': ' + Item.Value + '<br/>');
    186182    end;
    187183
    188184    WriteString('<h5>Response HTTP cookies</h5>');
    189     for I := 0 to Response.Cookies.Count - 1 do begin;
    190       with Response.Cookies.Items[I] do
    191       WriteString(Key + ': ' + Value + '<br/>');
     185    for Item in Response.Cookies do begin;
     186      WriteString(Item.Key + ': ' + Item.Value + '<br/>');
    192187    end;
    193188  end;
     
    197192begin
    198193  with HandlerData, Response.Content do begin
    199     WriteString('<html><body>' + Format(SPageNotFound, [Request.Path.Implode('/', StrToStr)]) + '</body></html>');
     194    WriteString('<html><body>' + Format(SPageNotFound, [Implode('/', Request.Path)]) + '</body></html>');
    200195  end;
    201196end;
     
    218213begin
    219214  with HandlerData do begin
    220     FileName := DocumentRoot + DirectorySeparator + Request.Path.Implode('/', StrToStr);
     215    FileName := DocumentRoot + DirectorySeparator + Implode('/', Request.Path);
    221216    if FileExists(FileName) then begin
    222       Response.Headers.Values['Content-Type'] := GetMIMEType(Copy(ExtractFileExt(FileName), 2, 255));
     217      Response.Headers.Items['Content-Type'] := GetMIMEType(Copy(ExtractFileExt(FileName), 2, 255));
    223218      try
    224219        BinaryFile := TFileStream.Create(FileName, fmOpenRead);
     
    230225    with Response.Content do begin
    231226      //WriteLn(Format(SFileNotFound, [Request.Path.Implode('/', StrToStr)]));
    232       WriteString('<html><body>' + Format(SFileNotFound, [Request.Path.Implode('/', StrToStr)]) + '</body></html>');
     227      WriteString('<html><body>' + Format(SFileNotFound, [Implode('/', Request.Path)]) + '</body></html>');
    233228    end;
    234229  end;
     
    357352    Pair := TListString.Create;
    358353    Clear;
    359     Parts.Explode(Text, '&', StrToStr);
     354    Parts.Explode('&', Text);
    360355    for I := 0 to Parts.Count - 1 do begin
    361       Pair.Explode(Parts[I], '=', StrToStr);
     356      Pair.Explode('=', Parts[I]);
    362357      if Pair.Count >= 2 then
    363358        Add(Pair[0], Pair[1]);
     
    371366function TQueryParameterList.Syntetize: string;
    372367var
    373   I: Integer;
     368  Item: TPair<string, string>;
    374369begin
    375370  Result := '';
    376   for I := 0 to Count - 1 do
    377     Result := Result + '&amp;' + Keys[I] + '=' + Items[I].Value;
     371  for Item in Self do
     372    Result := Result + '&amp;' + Item.Key + '=' + Item.Value;
    378373  Result := Copy(Result, 6, Length(Result));
    379374end;
     
    391386    Pair := TListString.Create;
    392387    Clear;
    393     Parts.Explode(Text, ';', StrToStr);
     388    Parts.Explode(';', Text);
    394389    for I := 0 to Parts.Count - 1 do begin
    395       Pair.Explode(Parts[I], '=', StrToStr);
     390      Pair.Explode('=', Parts[I]);
    396391      if Pair.Count >= 2 then
    397392        Add(Trim(Pair[0]), Trim(Pair[1]));
     
    405400function TCookieList.Syntetize: string;
    406401var
    407   I: Integer;
     402  Item: TPair<string, string>;
    408403begin
    409404  Result := '';
    410   for I := 0 to Count - 1 do
    411     Result := Result + '; ' + Keys[I] + '=' + Items[I].Value;
     405  for Item in Self do
     406    Result := Result + '; ' + Item.Key + '=' + Item.Value;
    412407  Result := Copy(Result, 2, Length(Result));
    413408end;
  • trunk/Packages/CoolWeb/WebServer/UHTTPServerCGI.pas

    r137 r138  
    44
    55uses
    6   Classes, SysUtils, UHTTPServer, SpecializedList, IOStream;
     6  Classes, SysUtils, UHTTPServer, IOStream, Generics.Collections;
    77
    88type
     
    5454  Buffer: string;
    5555  Count: Integer;
     56  Item: TPair<string, string>;
    5657begin
    5758  HandlerData := THTTPHandlerData.Create;
     
    9697      EnvVars.Values['QUERY_STRING'] := Copy(EnvVars.Values['QUERY_STRING'], 1,
    9798        Length(EnvVars.Values['QUERY_STRING']) - 1);
    98     Request.Path.Explode(EnvVars.Values['QUERY_STRING'], '/', StrToStr);
     99    Request.Path.Explode('/', EnvVars.Values['QUERY_STRING']);
    99100    if Pos('?', EnvVars.Values['REQUEST_URI']) > 0 then begin
    100101      Request.Query.Parse(Copy(EnvVars.Values['REQUEST_URI'],
     
    129130    with Response do begin
    130131      // Generate cookies
    131       for I := 0 to Cookies.Count - 1 do
    132         Headers.Add('Set-Cookie', Cookies.Keys[I] + '=' + Cookies.Items[I].Value);
     132      for Item in Cookies do
     133        Headers.Add('Set-Cookie', Item.Key + '=' + Item.Value);
    133134        // + ';path=/;expires=' + RFC822DateTime(Now);
    134135
    135136      // Generate headers
    136       for I := 0 to Headers.Count - 1 do begin
    137         WriteLn(Headers.Keys[I] + ': ' + Headers.Items[I].Value);
     137      for Item in Headers do begin
     138        WriteLn(Item.Key + ': ' + Item.Value);
    138139      end;
    139140
  • trunk/Packages/CoolWeb/WebServer/UHTTPServerTCP.pas

    r137 r138  
    44
    55uses
    6   Classes, SysUtils, UHTTPServer, UTCPServer, SpecializedList, SynaUtil;
     6  Classes, SysUtils, UHTTPServer, UTCPServer, SynaUtil, Generics.Collections,
     7  UGenerics;
    78
    89type
     
    4243  I: Integer;
    4344  ContentLength: Integer;
     45  Value: string;
     46  Item: TPair<string, string>;
    4447begin
    4548  with TTCPClientThread(Sender), Socket do begin
     
    6063          WriteLn(IntToStr(Id) + ' ' + Line);
    6164          if (LineIndex = 0) then begin
    62             LineParts.Explode(Line, ' ', StrToStr);
     65            LineParts.Explode(' ', Line);
    6366            if (LineParts.Count >= 3) then begin
    6467              Request.Method := LineParts[0];
    6568              if Pos('?', LineParts[1]) > 0 then begin
    6669                Request.Query.Parse(Copy(LineParts[1], Pos('?', LineParts[1]) + 1, Length(LineParts[1])));
    67                 Request.Path.Explode(Copy(LineParts[1], 1, Pos('?', LineParts[1]) - 1), '/', StrToStr);
     70                Request.Path.Explode('/', Copy(LineParts[1], 1, Pos('?', LineParts[1]) - 1));
    6871              end else begin
    69                 Request.Path.Explode(LineParts[1], '/', StrToStr);
     72                Request.Path.Explode('/', LineParts[1]);
    7073                Request.Query.Clear;
    7174              end;
     
    7578            end;
    7679          end else begin
    77             LineParts.Explode(Line, ' ', StrToStr, 2);
     80            LineParts.Explode(' ', Line, 2);
    7881            if (LineParts.Count = 2) and (LineParts[0][Length(LineParts[0])] = ':') then begin
    7982              LineParts[0] := Copy(LineParts[0], 1, Length(LineParts[0]) - 1);
    8083              Request.Headers.Add(LineParts[0], LineParts[1]);
    81               //WriteLn(Line);
    8284            end;
    8385          end;
     
    8688
    8789        if Request.Method = 'POST' then begin
    88           ContentLength := StrToInt(Request.Headers.Values['Content-Length']);
     90          if Request.Headers.TryGetValue('Content-Length', Value) then
     91            ContentLength := StrToInt(Value);
    8992          SetLength(Line, ContentLength);
    9093          RecvBufferEx(PByte(Line), ContentLength, 1000);
     
    9699
    97100    // Process cookies
    98     if Request.Headers.SearchKey('Cookie') <> -1 then
    99       Request.Cookies.Parse(Request.Headers.Values['Cookie']);
     101    if Request.Headers.TryGetValue('Cookie', Value) then
     102      Request.Cookies.Parse(Value);
    100103
    101104    // Load session variables
     
    120123
    121124      // Handle cookies
    122       for I := 0 to Cookies.Count - 1 do
    123         Headers.Add('Set-Cookie', Cookies.Keys[I] + '=' + Cookies.Items[I].Value);
     125      for Item in Cookies do
     126        Headers.Add('Set-Cookie', Item.Key + '=' + Item.Value);
    124127        // + ';path=/;expires=' + RFC822DateTime(Now);
    125128
    126129      // Send headers
    127       for I := 0 to Headers.Count - 1 do begin
     130      for Item in Headers do begin
    128131        //WriteLn(Headers.Keys[I] + ': ' + Headers.Items[I].Value + #13#10);
    129         SendString(Headers.Keys[I] + ': ' + Headers.Items[I].Value + #13#10);
     132        SendString(Item.Key + ': ' + Item.Value + #13#10);
    130133      end;
    131134      SendString(#13#10);
  • trunk/Packages/CoolWeb/WebServer/UHTTPSessionFile.pas

    r137 r138  
    5555
    5656procedure THTTPSessionStorageFile.GetSessionId(HandlerData: THTTPHandlerData);
     57var
     58  Value: string;
    5759begin
    5860  with HandlerData do begin
    59     if Request.Cookies.SearchKey(SessionIdCookieName) <> -1 then begin
    60       SessionId := Request.Cookies.Values[SessionIdCookieName];
     61    if Request.Cookies.TryGetValue(SessionIdCookieName, Value) then begin
     62      SessionId := Value;
    6163    end else begin
    6264      SessionId := GetNewSessionId;
    63       Response.Cookies.Values[SessionIdCookieName] := SessionId;
     65      Response.Cookies.Items[SessionIdCookieName] := SessionId;
    6466    end;
    6567  end;
     
    9698    end else raise Exception.Create(SCantCreateSessionStorageDirectory);
    9799
    98     HandlerData.Response.Cookies.Values[SessionIdCookieName] := HandlerData.SessionId;
     100    HandlerData.Response.Cookies.Items[SessionIdCookieName] := HandlerData.SessionId;
    99101  finally
    100102    Lock.Release;
  • trunk/Packages/CoolWeb/WebServer/UHTTPSessionMySQL.pas

    r137 r138  
    6464
    6565procedure THTTPSessionStorageMySQL.GetSessionId(HandlerData: THTTPHandlerData);
     66var
     67  Value: string;
    6668begin
    6769  with HandlerData do begin
    68     if Request.Cookies.SearchKey(SessionIdCookieName) <> -1 then begin
    69       SessionId := Request.Cookies.Values[SessionIdCookieName];
     70    if Request.Cookies.TryGetValue(SessionIdCookieName, Value) then begin
     71      SessionId := Value;
    7072    end else begin
    7173      SessionId := GetNewSessionId;
     
    8789      HandlerData.SessionId + '"');
    8890    if DbRows.Count > 0 then begin
    89       HandlerData.Session.Text := DbRows[0].Values['Variables'];
     91      HandlerData.Session.Text := DbRows[0].Items['Variables'];
    9092    end else begin
    9193      HandlerData.SessionId := GetNewSessionId;
     
    114116    else Database.Query(DbRows2, 'INSERT INTO `HTTPSession` (`Time`,  `Variables`, `Identification`) VALUES (' +
    115117    'NOW(), "' + HandlerData.Session.Text + '", "' + HandlerData.SessionId + '")');
    116     HandlerData.Response.Cookies.Values[SessionIdCookieName] := HandlerData.SessionId;
     118    HandlerData.Response.Cookies.Items[SessionIdCookieName] := HandlerData.SessionId;
    117119  finally
    118120    DbRows2.Free;
Note: See TracChangeset for help on using the changeset viewer.