Changeset 86 for branches/DirectWeb/UHTTPServer.pas
- Timestamp:
- Dec 17, 2009, 8:25:15 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DirectWeb/UHTTPServer.pas
r85 r86 6 6 7 7 uses 8 Classes, SysUtils, UTCPServer, UCommon, UMemoryStreamEx, UMIMEType; 8 Classes, SysUtils, UTCPServer, UCommon, UMemoryStreamEx, UMIMEType, 9 Synautil; 9 10 10 11 type 11 12 12 { TQueryString } 13 14 TQueryString = class(TStringList) 15 procedure Parse(QueryString: string); 13 TQueryParameterList = class(TStringList) 14 procedure Parse(Text: string); 16 15 function Syntetize: string; 17 16 end; 18 17 18 { TCookieList } 19 20 TCookieList = class(TStringList) 21 procedure Parse(Text: string); 22 function Syntetize: string; 23 end; 24 19 25 { THTTPRequest } 20 26 21 27 THTTPRequest = class 22 Query: TQuery String;28 Query: TQueryParameterList; 23 29 Path: string; 24 30 Host: string; 25 31 Method: string; 32 Headers: TStringList; 33 Cookies: TCookieList; 26 34 constructor Create; 27 35 destructor Destroy; override; … … 34 42 Stream: TMemoryStreamEx; 35 43 Headers: TStringList; 44 Cookies: TCookieList; 36 45 constructor Create; 37 46 destructor Destroy; override; … … 78 87 RequestHandler: TRequestHandler; 79 88 Line: string; 89 LineIndex: Integer; 80 90 LineParts: array of string; 81 91 Request: THTTPRequest; … … 90 100 Response.Headers.Values['Server'] := Name; 91 101 Request := THTTPRequest.Create; 102 LineIndex := 0; 92 103 repeat 93 104 Line := RecvString(10000); 94 105 LineParts := Explode(' ', Line); 95 if Length(LineParts) > 0then begin96 if (LineParts[0] = 'GET') or (LineParts[0] = 'HEAD') or97 (LineParts[0] = 'POST')then begin98 if Pos('?', LineParts[1]) > 0 then begin99 Request.Path := Copy(LineParts[1], 1, Pos('?', LineParts[1]) - 1);100 Request.Query.Parse(Copy(LineParts[1], Pos('?', LineParts[1]) + 1, Length(LineParts[1])));101 end else Request.Path := LineParts[1];102 Request.Method := LineParts[0];103 end else104 if LineParts[0] = 'Host:' then Request.Host:= LineParts[1];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]; 105 116 end; 117 Inc(LineIndex); 106 118 until Line = ''; 107 119 120 // Parse cookies 121 if Request.Cookies.IndexOfName('Cookie') <> -1 then 122 Request.Cookies.Parse(Request.Headers.Values['Cookie']); 123 108 124 Response.Stream.Clear; 125 Response.Headers.Values['Content-Type'] := 'text/html'; 126 109 127 WriteLn('Requested path: ' + Request.Path); 110 128 RequestHandler := RequestHandlerList.IndexOfName(Request.Path); … … 115 133 with Response do begin 116 134 SendString('HTTP/1.0 200 OK'#13#10); 117 SendString('Content-Length: ' + IntToStr(Stream.Size) + #13#10); 118 SendString('Connection: close'#13#10); 119 //SendString('Date: ' + RFC822DateTime(Now) + #13#10); 120 // Send additional headers 135 Headers.Values['Content-Length'] := IntToStr(Stream.Size); 136 Headers.Values['Connection'] := 'close'; 137 Headers.Values['Date'] := RFC822DateTime(Now); 138 139 // Handle cookies 140 for I := 0 to Cookies.Count - 1 do 141 Headers.Values['Set-Cookie'] := Cookies.Names[I] + '=' + Cookies.ValueFromIndex[I]; 142 // + ';path=/;expires=' + RFC822DateTime(Now); 143 144 // Send headers 121 145 for I := 0 to Headers.Count - 1 do 122 146 SendString(Headers.Names[I] + ': ' + Headers.ValueFromIndex[I] + #13#10); … … 180 204 begin 181 205 Stream := TMemoryStreamEx.Create; 206 Cookies := TCookieList.Create; 182 207 Headers := TStringList.Create; 183 Headers.Values['Content-Type'] := 'text/html';184 208 end; 185 209 … … 214 238 constructor THTTPRequest.Create; 215 239 begin 216 Query := TQueryString.Create; 240 Query := TQueryParameterList.Create; 241 Headers := TStringList.Create; 242 Cookies := TCookieList.Create; 217 243 end; 218 244 … … 220 246 begin 221 247 Query.Destroy; 248 Headers.Destroy; 222 249 inherited Destroy; 223 250 end; 224 251 225 { TQuery String}226 227 procedure TQuery String.Parse(QueryString: string);252 { TQueryParameterList } 253 254 procedure TQueryParameterList.Parse(Text: string); 228 255 var 229 256 I: Integer; … … 231 258 Pair: TArrayOfString; 232 259 begin 233 Parts := Explode('&', QueryString); 260 Clear; 261 Parts := Explode('&', Text); 234 262 for I := 0 to Length(Parts) - 1 do begin 235 263 Pair := Explode('=', Parts[I]); 236 Values[Pair[0]] := Pair[1]; 237 end; 238 end; 239 240 function TQueryString.Syntetize: string; 264 if Length(Pair) >= 2 then 265 Values[Pair[0]] := Pair[1]; 266 end; 267 end; 268 269 function TQueryParameterList.Syntetize: string; 241 270 var 242 271 I: Integer; … … 246 275 Result := Result + '&' + Names[I] + '=' + ValueFromIndex[I]; 247 276 Result := Copy(Result, 6, Length(Result)); 248 277 end; 278 279 { TCookieList } 280 281 procedure TCookieList.Parse(Text: string); 282 var 283 I: Integer; 284 Parts: TArrayOfString; 285 Pair: TArrayOfString; 286 begin 287 Clear; 288 Parts := Explode(';', Text); 289 for I := 0 to Length(Parts) - 1 do begin 290 Pair := Explode('=', Parts[I]); 291 if Length(Pair) >= 2 then 292 Values[Trim(Pair[0])] := Trim(Pair[1]); 293 end; 294 end; 295 296 function TCookieList.Syntetize: string; 297 var 298 I: Integer; 299 begin 300 Result := ''; 301 for I := 0 to Count - 1 do 302 Result := Result + '; ' + Names[I] + '=' + ValueFromIndex[I]; 303 Result := Copy(Result, 2, Length(Result)); 249 304 end; 250 305
Note:
See TracChangeset
for help on using the changeset viewer.