Changeset 376 for Network


Ignore:
Timestamp:
Jun 18, 2012, 10:42:17 PM (12 years ago)
Author:
chronos
Message:
  • Modified: QueryParts renamed to Path as TListString type.
Location:
Network/CoolWeb
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • Network/CoolWeb/Common/UMemoryStreamEx.pas

    r94 r376  
    4646    function Sum: Byte;
    4747    procedure FillByte(Data: Byte; Count: Integer);
     48    procedure Assign(Source: TMemoryStreamEx);
    4849    constructor Create;
    4950    property Endianness: TEndianness read FEndianness write SetEndianness;
     
    204205end;
    205206
     207procedure TMemoryStreamEx.Assign(Source: TMemoryStreamEx);
     208var
     209  OldPosition: Int64;
     210begin
     211  FEndianness := Source.FEndianness;
     212  SwapData := Source.SwapData;
     213  OldPosition := Source.Position;
     214  Clear;
     215  Source.Position := 0;
     216  CopyFrom(Source, Source.Size);
     217  Position := OldPosition;
     218  Source.Position := OldPosition;
     219end;
     220
    206221function TMemoryStreamEx.ReadWord: Word;
    207222begin
  • Network/CoolWeb/WebServer/UHTTPServer.pas

    r311 r376  
    77uses
    88  Classes, SysUtils, UTCPServer, UCommon, UMemoryStreamEx, UMIMEType,
    9   Synautil, SpecializedList;
     9  Synautil, SpecializedList, SpecializedDictionary;
    1010
    1111type
     
    3131    Content: TMemoryStreamEx;
    3232    Query: TQueryParameterList;
    33     QueryParts: TListString;
    34     Path: string;
     33    Path: TListString;
    3534    Method: string;
    36     Headers: TStringList;
     35    Headers: TDictionaryStringString;
    3736    Cookies: TCookieList;
    3837    Post: TQueryParameterList;
     38    procedure Assign(Source: THTTPRequest);
    3939    procedure Clear;
    4040    constructor Create;
     
    4747    ContentType: string;
    4848    Content: TMemoryStreamEx;
    49     Headers: TStringList;
     49    Headers: TDictionaryStringString;
    5050    Cookies: TCookieList;
     51    procedure Assign(Source: THTTPResponse);
    5152    procedure Clear;
    5253    constructor Create;
     
    6263    SessionId: string;
    6364    Session: TStringList;
    64     constructor Create;
     65    procedure Assign(Source: THTTPHandlerData);
     66    constructor Create; virtual;
    6567    destructor Destroy; override;
    6668  end;
     
    145147    WriteString('<h5>Request HTTP headers</h5>');
    146148    for I := 0 to Request.Headers.Count - 1 do begin;
    147       WriteString(Request.Headers.Strings[I] + '<br/>');
     149      WriteString(Request.Headers.Items[I].Value + '<br/>');
    148150    end;
    149151
     
    171173    with Response.Content do
    172174    for I := 0 to Response.Headers.Count - 1 do begin;
    173       WriteString(Response.Headers.Strings[I] + '<br/>');
     175      WriteString(Response.Headers.Items[I].Value + '<br/>');
    174176    end;
    175177
     
    184186begin
    185187  with HandlerData, Response.Content do begin
    186     WriteString('<html><body>' + Format(SPageNotFound, [Request.Path]) + '</body></html>');
     188    WriteString('<html><body>' + Format(SPageNotFound, [Request.Path.Implode('/', StrToStr)]) + '</body></html>');
    187189  end;
    188190end;
     
    206208begin
    207209  with HandlerData do begin
    208     FileName := DocumentRoot + Request.Path;
     210    FileName := DocumentRoot + DirectorySeparator + Request.Path.Implode('/', StrToStr);
    209211    if FileExists(FileName) then begin
    210212      Response.Headers.Values['Content-Type'] := GetMIMEType(Copy(ExtractFileExt(FileName), 2, 255));
    211       BinaryFile := TFileStream.Create(FileName, fmOpenRead);
    212       Response.Content.WriteStream(BinaryFile, BinaryFile.Size);
    213       BinaryFile.Destroy;
     213      try
     214        BinaryFile := TFileStream.Create(FileName, fmOpenRead);
     215        Response.Content.WriteStream(BinaryFile, BinaryFile.Size);
     216      finally
     217        BinaryFile.Free;
     218      end;
    214219    end else
    215220    with Response.Content do begin
    216       WriteLn(Format(SFileNotFound, [Request.Path]));
    217       WriteString('<html><body>' + Format(SFileNotFound, [Request.Path]) + '</body></html>');
     221      WriteLn(Format(SFileNotFound, [Request.Path.Implode('/', StrToStr)]));
     222      WriteString('<html><body>' + Format(SFileNotFound, [Request.Path.Implode('/', StrToStr)]) + '</body></html>');
    218223    end;
    219224  end;
     
    234239{ THTTPResponse }
    235240
     241procedure THTTPResponse.Assign(Source: THTTPResponse);
     242begin
     243  Content.Assign(Source.Content);
     244  ContentType := Source.ContentType;
     245  Cookies.Assign(Source.Cookies);
     246  Headers.Assign(Source.Headers);
     247end;
     248
    236249procedure THTTPResponse.Clear;
    237250begin
     
    245258  Content := TMemoryStreamEx.Create;
    246259  Cookies := TCookieList.Create;
    247   Headers := TStringList.Create;
     260  Headers := TDictionaryStringString.Create;
    248261end;
    249262
     
    277290{ THTTPRequest }
    278291
     292procedure THTTPRequest.Assign(Source: THTTPRequest);
     293begin
     294  Content.Assign(Source.Content);
     295  ContentType := Source.ContentType;
     296  Cookies.Assign(Source.Cookies);
     297  Headers.Assign(Source.Headers);
     298  Post.Assign(Source.Post);
     299  Method := Source.Method;
     300  Query.Assign(Source.Query);
     301  Path.Assign(Source.Path);
     302end;
     303
    279304procedure THTTPRequest.Clear;
    280305begin
    281306  Post.Clear;
    282307  Content.Clear;
    283   QueryParts.Clear;
     308  Path.Clear;
    284309  Cookies.Clear;
    285310  Headers.Clear;
     
    291316  Post := TQueryParameterList.Create;
    292317  Query := TQueryParameterList.Create;
    293   QueryParts := TListString.Create;
    294   Headers := TStringList.Create;
     318  Path := TListString.Create;
     319  Headers := TDictionaryStringString.Create;
    295320  Cookies := TCookieList.Create;
    296321  Content := TMemoryStreamEx.Create;
     
    302327  Post.Free;
    303328  Query.Free;
    304   QueryParts.Free;
     329  Path.Free;
    305330  Headers.Free;
    306331  Cookies.Free;
     
    400425
    401426{ THTTPHandlerData }
     427
     428procedure THTTPHandlerData.Assign(Source: THTTPHandlerData);
     429begin
     430  Request.Assign(Source.Request);
     431  Response.Assign(Source.Response);
     432  Session.Assign(Source.Session);
     433  Server := Source.Server;
     434  SessionId := Source.SessionId;
     435end;
    402436
    403437constructor THTTPHandlerData.Create;
  • Network/CoolWeb/WebServer/UHTTPServerCGI.pas

    r311 r376  
    9999      EnvVars.Values['QUERY_STRING'] := Copy(EnvVars.Values['QUERY_STRING'], 1,
    100100        Length(EnvVars.Values['QUERY_STRING']) - 1);
    101     Request.QueryParts.Explode(EnvVars.Values['QUERY_STRING'], '/', StrToStr);
    102     if Pos('?', EnvVars.Values['REQUEST_URI']) > 0 then
     101    Request.Path.Explode(EnvVars.Values['QUERY_STRING'], '/', StrToStr);
     102    if Pos('?', EnvVars.Values['REQUEST_URI']) > 0 then begin
    103103      Request.Query.Parse(Copy(EnvVars.Values['REQUEST_URI'],
    104104        Pos('?', EnvVars.Values['REQUEST_URI']) + 1,
    105105        Length(EnvVars.Values['REQUEST_URI'])));
     106    end;
    106107
    107108    // Load session variables
     
    119120
    120121    Response.Content.Clear;
    121     Response.Headers.Values['Content-type'] := 'text/html';
     122    Response.Headers.Add('Content-type', 'text/html');
    122123
    123124    // Execute content handler
     
    132133      // Generate cookies
    133134      for I := 0 to Cookies.Count - 1 do
    134         Headers.Add('Set-Cookie' + Headers.NameValueSeparator + Cookies.Names[I] + '=' + Cookies.ValueFromIndex[I]);
     135        Headers.Add('Set-Cookie', Cookies.Names[I] + '=' + Cookies.ValueFromIndex[I]);
    135136        // + ';path=/;expires=' + RFC822DateTime(Now);
    136137
    137138      // Generate headers
    138139      for I := 0 to Headers.Count - 1 do begin
    139         WriteLn(Headers.Names[I] + ': ' + Headers.ValueFromIndex[I]);
     140        WriteLn(Headers.Keys[I] + ': ' + Headers.Items[I].Value);
    140141      end;
    141142
  • Network/CoolWeb/WebServer/UHTTPServerTCP.pas

    r311 r376  
    5151
    5252      Response := THTTPResponse.Create;
    53       Response.Headers.Values['Server'] := Name;
     53      Response.Headers.Add('Server', Name);
    5454      Request := THTTPRequest.Create;
    5555      LineIndex := 0;
     
    6363              Request.Method := LineParts[0];
    6464              if Pos('?', LineParts[1]) > 0 then begin
    65                 Request.Path := Copy(LineParts[1], 1, Pos('?', LineParts[1]) - 1);
    6665                Request.Query.Parse(Copy(LineParts[1], Pos('?', LineParts[1]) + 1, Length(LineParts[1])));
    67               end else Request.Path := LineParts[1];
     66                Request.Path.Explode(Copy(LineParts[1], 1, Pos('?', LineParts[1]) - 1), '/', StrToStr);
     67              end else begin
     68                Request.Path.Explode(LineParts[1], '/', StrToStr);
     69                Request.Query.Clear;
     70              end;
    6871            end;
    6972          end else begin
     
    7174            if (LineParts.Count = 2) and (LineParts[0][Length(LineParts[0])] = ':') then begin
    7275              LineParts[0] := Copy(LineParts[0], 1, Length(LineParts[0]) - 1);
    73               Request.Headers.Values[LineParts[0]] := LineParts[1];
     76              Request.Headers.Add(LineParts[0], LineParts[1]);
    7477              //WriteLn(Line);
    7578            end;
     
    8285
    8386    // Process cookies
    84     if Request.Headers.IndexOfName('Cookie') <> -1 then
     87    if Request.Headers.SearchKey('Cookie') <> -1 then
    8588      Request.Cookies.Parse(Request.Headers.Values['Cookie']);
    8689
     
    9093
    9194    Response.Content.Clear;
    92     Response.Headers.Values['Content-Type'] := 'text/html';
     95    Response.Headers.Add('Content-Type', 'text/html');
    9396
    9497    if Assigned(OnRequest) then OnRequest(HandlerData)
     
    101104    with Response do begin
    102105      SendString('HTTP/1.0 200 OK'#13#10);
    103       Headers.Values['Content-Length'] := IntToStr(Content.Size);
    104       Headers.Values['Connection'] := 'close';
    105       Headers.Values['Date'] := RFC822DateTime(Now);
     106      Headers.Add('Content-Length', IntToStr(Content.Size));
     107      Headers.Add('Connection', 'close');
     108      Headers.Add('Date', RFC822DateTime(Now));
    106109
    107110      // Handle cookies
    108111      for I := 0 to Cookies.Count - 1 do
    109         Headers.Add('Set-Cookie' + Headers.NameValueSeparator + Cookies.Names[I] + '=' + Cookies.ValueFromIndex[I]);
     112        Headers.Add('Set-Cookie', Cookies.Names[I] + '=' + Cookies.ValueFromIndex[I]);
    110113        // + ';path=/;expires=' + RFC822DateTime(Now);
    111114
    112115      // Send headers
    113116      for I := 0 to Headers.Count - 1 do begin
    114         //WriteLn(Headers.Names[I] + ': ' + Headers.ValueFromIndex[I] + #13#10);
    115         SendString(Headers.Names[I] + ': ' + Headers.ValueFromIndex[I] + #13#10);
     117        WriteLn(Headers.Keys[I] + ': ' + Headers.Items[I].Value + #13#10);
     118        SendString(Headers.Keys[I] + ': ' + Headers.Items[I].Value + #13#10);
    116119      end;
    117120      SendString(#13#10);
  • Network/CoolWeb/WebServer/UHTTPServerTurboPower.pas

    r357 r376  
    4747    Response.Content.Position := 0;
    4848    FormWebBrowser.HtmlSource := Response.Content.ReadString;
    49     //FormWebBrowser.IpHtmlPanel1.SetHtmlFromStr(FormWebBrowser.HtmlSource);
     49    FormWebBrowser.IpHtmlPanel1.SetHtmlFromStr(FormWebBrowser.HtmlSource);
    5050  finally
    5151    Free;
  • Network/CoolWeb/WebServer/UWebApp.pas

    r375 r376  
    125125begin
    126126  with HandlerData do begin
    127     if Request.QueryParts.Count > 0 then PageName := Request.QueryParts[0]
     127    if Request.Path.Count > 0 then PageName := Request.Path[0]
    128128      else PageName := '';
    129129    Page := FindByName(PageName);
  • Network/CoolWeb/WebServer/UWebPage.pas

    r254 r376  
    1010type
    1111  TOnProduceEvent = procedure(HandlerData: THTTPHandlerData) of object;
     12
    1213
    1314  { TWebPage }
Note: See TracChangeset for help on using the changeset viewer.