Changeset 138 for trunk/Packages/CoolWeb/WebServer
- Timestamp:
- Sep 9, 2022, 8:20:25 PM (2 years ago)
- Location:
- trunk/Packages/CoolWeb/WebServer
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/CoolWeb/WebServer/UHTTPServer.pas
r137 r138 4 4 5 5 uses 6 Classes, SysUtils, UCommon, UMemoryStreamEx, UMIMEType, 7 Synautil, SpecializedList, SpecializedDictionary, Syncobjs;6 Classes, SysUtils, UCommon, UMemoryStreamEx, UMIMEType, Synautil, Syncobjs, 7 Generics.Collections, UGenerics; 8 8 9 9 type … … 75 75 { TRequestHandlerList } 76 76 77 TRequestHandlerList = class(T ListObject)77 TRequestHandlerList = class(TObjectList<TRequestHandler>) 78 78 procedure Add(AName: string; AHandler: TRequestEvent); 79 79 function IndexOfName(AName: string): TRequestHandler; … … 131 131 var 132 132 I: Integer; 133 Item: TPair<string, string>; 133 134 begin 134 135 with HandlerData, Response.Content do begin … … 145 146 146 147 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/>'); 150 150 end; 151 151 152 152 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/>'); 156 155 end; 157 156 158 157 WriteString('<h5>Request HTTP cookies</h5>'); 159 158 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/>'); 162 160 end; 163 161 … … 170 168 171 169 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/>'); 175 172 end; 176 173 … … 181 178 WriteString('<h5>Response HTTP headers</h5>'); 182 179 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/>'); 186 182 end; 187 183 188 184 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/>'); 192 187 end; 193 188 end; … … 197 192 begin 198 193 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>'); 200 195 end; 201 196 end; … … 218 213 begin 219 214 with HandlerData do begin 220 FileName := DocumentRoot + DirectorySeparator + Request.Path.Implode('/', StrToStr);215 FileName := DocumentRoot + DirectorySeparator + Implode('/', Request.Path); 221 216 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)); 223 218 try 224 219 BinaryFile := TFileStream.Create(FileName, fmOpenRead); … … 230 225 with Response.Content do begin 231 226 //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>'); 233 228 end; 234 229 end; … … 357 352 Pair := TListString.Create; 358 353 Clear; 359 Parts.Explode( Text, '&', StrToStr);354 Parts.Explode('&', Text); 360 355 for I := 0 to Parts.Count - 1 do begin 361 Pair.Explode( Parts[I], '=', StrToStr);356 Pair.Explode('=', Parts[I]); 362 357 if Pair.Count >= 2 then 363 358 Add(Pair[0], Pair[1]); … … 371 366 function TQueryParameterList.Syntetize: string; 372 367 var 373 I : Integer;368 Item: TPair<string, string>; 374 369 begin 375 370 Result := ''; 376 for I := 0 to Count - 1do377 Result := Result + '&' + Keys[I] + '=' + Items[I].Value;371 for Item in Self do 372 Result := Result + '&' + Item.Key + '=' + Item.Value; 378 373 Result := Copy(Result, 6, Length(Result)); 379 374 end; … … 391 386 Pair := TListString.Create; 392 387 Clear; 393 Parts.Explode( Text, ';', StrToStr);388 Parts.Explode(';', Text); 394 389 for I := 0 to Parts.Count - 1 do begin 395 Pair.Explode( Parts[I], '=', StrToStr);390 Pair.Explode('=', Parts[I]); 396 391 if Pair.Count >= 2 then 397 392 Add(Trim(Pair[0]), Trim(Pair[1])); … … 405 400 function TCookieList.Syntetize: string; 406 401 var 407 I : Integer;402 Item: TPair<string, string>; 408 403 begin 409 404 Result := ''; 410 for I := 0 to Count - 1do411 Result := Result + '; ' + Keys[I] + '=' + Items[I].Value;405 for Item in Self do 406 Result := Result + '; ' + Item.Key + '=' + Item.Value; 412 407 Result := Copy(Result, 2, Length(Result)); 413 408 end; -
trunk/Packages/CoolWeb/WebServer/UHTTPServerCGI.pas
r137 r138 4 4 5 5 uses 6 Classes, SysUtils, UHTTPServer, SpecializedList, IOStream;6 Classes, SysUtils, UHTTPServer, IOStream, Generics.Collections; 7 7 8 8 type … … 54 54 Buffer: string; 55 55 Count: Integer; 56 Item: TPair<string, string>; 56 57 begin 57 58 HandlerData := THTTPHandlerData.Create; … … 96 97 EnvVars.Values['QUERY_STRING'] := Copy(EnvVars.Values['QUERY_STRING'], 1, 97 98 Length(EnvVars.Values['QUERY_STRING']) - 1); 98 Request.Path.Explode( EnvVars.Values['QUERY_STRING'], '/', StrToStr);99 Request.Path.Explode('/', EnvVars.Values['QUERY_STRING']); 99 100 if Pos('?', EnvVars.Values['REQUEST_URI']) > 0 then begin 100 101 Request.Query.Parse(Copy(EnvVars.Values['REQUEST_URI'], … … 129 130 with Response do begin 130 131 // Generate cookies 131 for I := 0 to Cookies.Count - 1do132 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); 133 134 // + ';path=/;expires=' + RFC822DateTime(Now); 134 135 135 136 // Generate headers 136 for I := 0 to Headers.Count - 1do begin137 WriteLn( Headers.Keys[I] + ': ' + Headers.Items[I].Value);137 for Item in Headers do begin 138 WriteLn(Item.Key + ': ' + Item.Value); 138 139 end; 139 140 -
trunk/Packages/CoolWeb/WebServer/UHTTPServerTCP.pas
r137 r138 4 4 5 5 uses 6 Classes, SysUtils, UHTTPServer, UTCPServer, SpecializedList, SynaUtil; 6 Classes, SysUtils, UHTTPServer, UTCPServer, SynaUtil, Generics.Collections, 7 UGenerics; 7 8 8 9 type … … 42 43 I: Integer; 43 44 ContentLength: Integer; 45 Value: string; 46 Item: TPair<string, string>; 44 47 begin 45 48 with TTCPClientThread(Sender), Socket do begin … … 60 63 WriteLn(IntToStr(Id) + ' ' + Line); 61 64 if (LineIndex = 0) then begin 62 LineParts.Explode( Line, ' ', StrToStr);65 LineParts.Explode(' ', Line); 63 66 if (LineParts.Count >= 3) then begin 64 67 Request.Method := LineParts[0]; 65 68 if Pos('?', LineParts[1]) > 0 then begin 66 69 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)); 68 71 end else begin 69 Request.Path.Explode( LineParts[1], '/', StrToStr);72 Request.Path.Explode('/', LineParts[1]); 70 73 Request.Query.Clear; 71 74 end; … … 75 78 end; 76 79 end else begin 77 LineParts.Explode( Line, ' ', StrToStr, 2);80 LineParts.Explode(' ', Line, 2); 78 81 if (LineParts.Count = 2) and (LineParts[0][Length(LineParts[0])] = ':') then begin 79 82 LineParts[0] := Copy(LineParts[0], 1, Length(LineParts[0]) - 1); 80 83 Request.Headers.Add(LineParts[0], LineParts[1]); 81 //WriteLn(Line);82 84 end; 83 85 end; … … 86 88 87 89 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); 89 92 SetLength(Line, ContentLength); 90 93 RecvBufferEx(PByte(Line), ContentLength, 1000); … … 96 99 97 100 // Process cookies 98 if Request.Headers. SearchKey('Cookie') <> -1then99 Request.Cookies.Parse( Request.Headers.Values['Cookie']);101 if Request.Headers.TryGetValue('Cookie', Value) then 102 Request.Cookies.Parse(Value); 100 103 101 104 // Load session variables … … 120 123 121 124 // Handle cookies 122 for I := 0 to Cookies.Count - 1do123 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); 124 127 // + ';path=/;expires=' + RFC822DateTime(Now); 125 128 126 129 // Send headers 127 for I := 0 to Headers.Count - 1do begin130 for Item in Headers do begin 128 131 //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); 130 133 end; 131 134 SendString(#13#10); -
trunk/Packages/CoolWeb/WebServer/UHTTPSessionFile.pas
r137 r138 55 55 56 56 procedure THTTPSessionStorageFile.GetSessionId(HandlerData: THTTPHandlerData); 57 var 58 Value: string; 57 59 begin 58 60 with HandlerData do begin 59 if Request.Cookies. SearchKey(SessionIdCookieName) <> -1then begin60 SessionId := Request.Cookies.Values[SessionIdCookieName];61 if Request.Cookies.TryGetValue(SessionIdCookieName, Value) then begin 62 SessionId := Value; 61 63 end else begin 62 64 SessionId := GetNewSessionId; 63 Response.Cookies. Values[SessionIdCookieName] := SessionId;65 Response.Cookies.Items[SessionIdCookieName] := SessionId; 64 66 end; 65 67 end; … … 96 98 end else raise Exception.Create(SCantCreateSessionStorageDirectory); 97 99 98 HandlerData.Response.Cookies. Values[SessionIdCookieName] := HandlerData.SessionId;100 HandlerData.Response.Cookies.Items[SessionIdCookieName] := HandlerData.SessionId; 99 101 finally 100 102 Lock.Release; -
trunk/Packages/CoolWeb/WebServer/UHTTPSessionMySQL.pas
r137 r138 64 64 65 65 procedure THTTPSessionStorageMySQL.GetSessionId(HandlerData: THTTPHandlerData); 66 var 67 Value: string; 66 68 begin 67 69 with HandlerData do begin 68 if Request.Cookies. SearchKey(SessionIdCookieName) <> -1then begin69 SessionId := Request.Cookies.Values[SessionIdCookieName];70 if Request.Cookies.TryGetValue(SessionIdCookieName, Value) then begin 71 SessionId := Value; 70 72 end else begin 71 73 SessionId := GetNewSessionId; … … 87 89 HandlerData.SessionId + '"'); 88 90 if DbRows.Count > 0 then begin 89 HandlerData.Session.Text := DbRows[0]. Values['Variables'];91 HandlerData.Session.Text := DbRows[0].Items['Variables']; 90 92 end else begin 91 93 HandlerData.SessionId := GetNewSessionId; … … 114 116 else Database.Query(DbRows2, 'INSERT INTO `HTTPSession` (`Time`, `Variables`, `Identification`) VALUES (' + 115 117 'NOW(), "' + HandlerData.Session.Text + '", "' + HandlerData.SessionId + '")'); 116 HandlerData.Response.Cookies. Values[SessionIdCookieName] := HandlerData.SessionId;118 HandlerData.Response.Cookies.Items[SessionIdCookieName] := HandlerData.SessionId; 117 119 finally 118 120 DbRows2.Free;
Note:
See TracChangeset
for help on using the changeset viewer.