Changeset 139 for trunk/Packages/CoolWeb


Ignore:
Timestamp:
Sep 9, 2022, 9:41:42 PM (2 years ago)
Author:
chronos
Message:
  • Fixed: Better handle invalid not found pages including correct HTTP code.
Location:
trunk/Packages/CoolWeb/WebServer
Files:
3 edited

Legend:

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

    r138 r139  
    4343
    4444  THTTPResponse = class
     45    StatusCode: string;
    4546    ContentType: string;
    4647    Content: TMemoryStreamEx;
     
    123124  SPageNotFound = 'Page %s not found.';
    124125
     126const
     127  StatusCodeNotFound = '404 Not Found';
     128
    125129
    126130implementation
     
    134138begin
    135139  with HandlerData, Response.Content do begin
    136     //Response.Cookies.Values['Test'] := 'Halo';
    137     //Response.Cookies.Values['Test2'] := 'Halo2';
    138 
    139     //HTTPServer.SessionHandler.Variables.Values['Session1'] := 'Value1';
    140     //HTTPServer.SessionHandler.Variables.Values['Session2'] := 'Value2';
    141 
    142140    WriteString('<a href="?ServerInfo">Refresh</a>');
    143141
     
    192190begin
    193191  with HandlerData, Response.Content do begin
     192    Response.StatusCode := StatusCodeNotFound;
    194193    WriteString('<html><body>' + Format(SPageNotFound, [Implode('/', Request.Path)]) + '</body></html>');
    195194  end;
     
    224223    end else
    225224    with Response.Content do begin
    226       //WriteLn(Format(SFileNotFound, [Request.Path.Implode('/', StrToStr)]));
     225      Response.StatusCode := StatusCodeNotFound;
    227226      WriteString('<html><body>' + Format(SFileNotFound, [Implode('/', Request.Path)]) + '</body></html>');
    228227    end;
     
    248247procedure THTTPResponse.Assign(Source: THTTPResponse);
    249248begin
     249  StatusCode := Source.StatusCode;
    250250  Content.Assign(Source.Content);
    251251  ContentType := Source.ContentType;
     
    263263constructor THTTPResponse.Create;
    264264begin
     265  StatusCode := '200 OK';
    265266  Content := TMemoryStreamEx.Create;
    266267  Cookies := TCookieList.Create;
     
    291292begin
    292293  I := 0;
    293   while (I < Count) and (TRequestHandler(Items[I]).Name <> AName) do Inc(I);
    294   if I < Count then Result := TRequestHandler(Items[I]) else Result := nil;
     294  while (I < Count) and (Items[I].Name <> AName) do Inc(I);
     295  if I < Count then Result := Items[I]
     296    else Result := nil;
    295297end;
    296298
  • trunk/Packages/CoolWeb/WebServer/UHTTPServerTCP.pas

    r138 r139  
    117117
    118118    with Response do begin
    119       SendString('HTTP/1.0 200 OK'#13#10);
     119      SendString('HTTP/1.0 ' + StatusCode + #13#10);
    120120      Headers.Add('Content-Length', IntToStr(Content.Size));
    121121      Headers.Add('Connection', 'close');
  • trunk/Packages/CoolWeb/WebServer/UTurboPowerForm.pas

    r137 r139  
    2424    procedure AShowSourceExecute(Sender: TObject);
    2525    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    26   private
    27     { private declarations }
    2826  public
    2927    HtmlSource: string;
Note: See TracChangeset for help on using the changeset viewer.