Ignore:
Timestamp:
Dec 17, 2009, 9:36:15 AM (15 years ago)
Author:
george
Message:
  • Přidáno: Podpora pro ukládání serverových proměnných HTTP sezení.
  • Upraveno: U funkce Explode lze nyní omezit počet výsledných částí řetězce.
Location:
branches/DirectWeb
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/DirectWeb

    • Property svn:ignore
      •  

        old new  
        33WoWHostingWebServer
        44Config.xml
         5Session
  • branches/DirectWeb/UHTTPServer.pas

    r86 r87  
    5959    procedure Add(AName: string; AHandler: TRequestEvent);
    6060    function IndexOfName(AName: string): TRequestHandler;
     61  end;
     62
     63  { THTTPSession }
     64
     65  THTTPSession = class
     66    Variables: TStringList;
     67    procedure Load(Request: THTTPRequest); virtual;
     68    procedure Save(Response: THTTPResponse); virtual;
     69    constructor Create; virtual;
     70    destructor Destroy; override;
    6171  end;
    6272
     
    7383    MaxConnection: Integer;
    7484    RequestHandlerList: TRequestHandlerList;
     85    SessionHandler: THTTPSession;
    7586    procedure FileResponse(Request: THTTPRequest;
    7687      Response: THTTPResponse);
     
    103114    repeat
    104115      Line := RecvString(10000);
    105       LineParts := Explode(' ', Line);
    106       if (LineIndex = 0) and (Length(LineParts) >= 3) then begin
    107         Request.Method := LineParts[0];
    108         if Pos('?', LineParts[1]) > 0 then begin
    109           Request.Path := Copy(LineParts[1], 1, Pos('?', LineParts[1]) - 1);
    110           Request.Query.Parse(Copy(LineParts[1], Pos('?', LineParts[1]) + 1, Length(LineParts[1])));
    111         end else Request.Path := LineParts[1];
    112       end else
    113       if (Length(LineParts) >= 2) and (LineParts[0][Length(LineParts[0])] = ':') then begin
    114         LineParts[0] := Copy(LineParts[0], 1, Length(LineParts[0]) - 1);
    115         Request.Headers.Values[LineParts[0]] := LineParts[1];
     116      if (LineIndex = 0) then begin
     117        LineParts := Explode(' ', Line);
     118        if (Length(LineParts) >= 3) then begin
     119          Request.Method := LineParts[0];
     120          if Pos('?', LineParts[1]) > 0 then begin
     121            Request.Path := Copy(LineParts[1], 1, Pos('?', LineParts[1]) - 1);
     122            Request.Query.Parse(Copy(LineParts[1], Pos('?', LineParts[1]) + 1, Length(LineParts[1])));
     123          end else Request.Path := LineParts[1];
     124        end;
     125      end else begin
     126        LineParts := Explode(' ', Line, 2);
     127        if (Length(LineParts) = 2) and (LineParts[0][Length(LineParts[0])] = ':') then begin
     128          LineParts[0] := Copy(LineParts[0], 1, Length(LineParts[0]) - 1);
     129          Request.Headers.Values[LineParts[0]] := LineParts[1];
     130          WriteLn(Line);
     131        end;
    116132      end;
    117133      Inc(LineIndex);
    118134    until Line = '';
    119135
    120     // Parse cookies
    121     if Request.Cookies.IndexOfName('Cookie') <> -1 then
    122     Request.Cookies.Parse(Request.Headers.Values['Cookie']);
     136    // Process cookies
     137    if Request.Headers.IndexOfName('Cookie') <> -1 then
     138      Request.Cookies.Parse(Request.Headers.Values['Cookie']);
     139
     140    // Process session
     141    if Assigned(SessionHandler) then
     142      SessionHandler.Load(Request);
    123143
    124144    Response.Stream.Clear;
     
    130150    if Assigned(RequestHandler) then RequestHandler.Handler(Request, Response)
    131151      else ErrorResponse(Request, Response);
     152
     153    if Assigned(SessionHandler) then
     154      SessionHandler.Save(Response);
    132155
    133156    with Response do begin
     
    139162      // Handle cookies
    140163      for I := 0 to Cookies.Count - 1 do
    141         Headers.Values['Set-Cookie'] := Cookies.Names[I] + '=' + Cookies.ValueFromIndex[I];
     164        Headers.Add('Set-Cookie' + Headers.NameValueSeparator + Cookies.Names[I] + '=' + Cookies.ValueFromIndex[I]);
    142165        // + ';path=/;expires=' + RFC822DateTime(Now);
    143166
    144167      // Send headers
    145       for I := 0 to Headers.Count - 1 do
     168      for I := 0 to Headers.Count - 1 do begin
     169        WriteLn(Headers.Names[I] + ': ' + Headers.ValueFromIndex[I] + #13#10);
    146170        SendString(Headers.Names[I] + ': ' + Headers.ValueFromIndex[I] + #13#10);
     171      end;
    147172      SendString(#13#10);
    148173      SendBuffer(Stream.Memory, Stream.Size);
     
    304329end;
    305330
     331{ THTTPSession }
     332
     333procedure THTTPSession.Load(Request: THTTPRequest);
     334begin
     335
     336end;
     337
     338procedure THTTPSession.Save(Response: THTTPResponse);
     339begin
     340
     341end;
     342
     343constructor THTTPSession.Create;
     344begin
     345  Variables := TStringList.Create;
     346end;
     347
     348destructor THTTPSession.Destroy;
     349begin
     350  Variables.Destroy;
     351  inherited Destroy;
     352end;
     353
    306354end.
    307355
Note: See TracChangeset for help on using the changeset viewer.