Changeset 88 for branches/DirectWeb/UHTTPServer.pas
- Timestamp:
- Dec 18, 2009, 12:21:39 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DirectWeb/UHTTPServer.pas
r87 r88 10 10 11 11 type 12 THTTPServer = class; 12 13 13 14 TQueryParameterList = class(TStringList) … … 28 29 Query: TQueryParameterList; 29 30 Path: string; 30 Host: string;31 31 Method: string; 32 32 Headers: TStringList; … … 47 47 end; 48 48 49 TRequestEvent = procedure(Request: THTTPRequest; Response: THTTPResponse) of object; 49 { THTTPHandlerData } 50 51 THTTPHandlerData = class 52 Server: THTTPServer; 53 Request: THTTPRequest; 54 Response: THTTPResponse; 55 SessionId: string; 56 Session: TStringList; 57 constructor Create; 58 destructor Destroy; override; 59 end; 60 61 TRequestEvent = procedure(HandlerData: THTTPHandlerData) of object; 50 62 51 63 TRequestHandler = class … … 63 75 { THTTPSession } 64 76 65 THTTPSession = class 66 Variables: TStringList; 67 procedure Load(Request: THTTPRequest); virtual; 68 procedure Save(Response: THTTPResponse); virtual; 77 { THTTPSessionStorage } 78 79 THTTPSessionStorage = class 80 procedure Load(HandlerData: THTTPHandlerData); virtual; 81 procedure Save(HandlerData: THTTPHandlerData); virtual; 69 82 constructor Create; virtual; 70 83 destructor Destroy; override; … … 76 89 private 77 90 procedure HandleClient(Sender: TObject); 78 procedure ErrorResponse( Request: THTTPRequest; Response: THTTPResponse);91 procedure ErrorResponse(HandlerData: THTTPHandlerData); 79 92 public 80 93 Name: string; … … 83 96 MaxConnection: Integer; 84 97 RequestHandlerList: TRequestHandlerList; 85 SessionHandler: THTTPSession; 86 procedure FileResponse(Request: THTTPRequest; 87 Response: THTTPResponse); 98 SessionStorage: THTTPSessionStorage; 99 procedure FileResponse(HandlerData: THTTPHandlerData); 88 100 constructor Create; 89 101 destructor Destroy; override; … … 100 112 LineIndex: Integer; 101 113 LineParts: array of string; 102 Request: THTTPRequest; 103 Response: THTTPResponse; 114 HandlerData: THTTPHandlerData; 104 115 I: Integer; 105 116 QueryString: string; 106 117 begin 107 118 with TTCPClientThread(Sender), Socket do begin 108 WriteLn('Client ' + IntToStr(Parent.IndexOf(Sender)) + ' connected from ' + GetRemoteSinIP); 119 WriteLn('Used thrads ' + IntToStr(Parent.ThreadPool.UsedCount) + '. Client connected from ' + GetRemoteSinIP); 120 121 HandlerData := THTTPHandlerData.Create; 122 with HandlerData do begin 123 Server := Self; 109 124 110 125 Response := THTTPResponse.Create; … … 128 143 LineParts[0] := Copy(LineParts[0], 1, Length(LineParts[0]) - 1); 129 144 Request.Headers.Values[LineParts[0]] := LineParts[1]; 130 WriteLn(Line);145 //WriteLn(Line); 131 146 end; 132 147 end; … … 138 153 Request.Cookies.Parse(Request.Headers.Values['Cookie']); 139 154 140 // Process session141 if Assigned(Session Handler) then142 Session Handler.Load(Request);155 // Load session variables 156 if Assigned(SessionStorage) then 157 SessionStorage.Load(HandlerData); 143 158 144 159 Response.Stream.Clear; … … 148 163 RequestHandler := RequestHandlerList.IndexOfName(Request.Path); 149 164 150 if Assigned(RequestHandler) then RequestHandler.Handler(Request, Response) 151 else ErrorResponse(Request, Response); 152 153 if Assigned(SessionHandler) then 154 SessionHandler.Save(Response); 165 if Assigned(RequestHandler) then RequestHandler.Handler(HandlerData) 166 else ErrorResponse(HandlerData); 167 168 // Store session variables 169 if Assigned(SessionStorage) then 170 SessionStorage.Save(HandlerData); 155 171 156 172 with Response do begin … … 167 183 // Send headers 168 184 for I := 0 to Headers.Count - 1 do begin 169 WriteLn(Headers.Names[I] + ': ' + Headers.ValueFromIndex[I] + #13#10);185 //WriteLn(Headers.Names[I] + ': ' + Headers.ValueFromIndex[I] + #13#10); 170 186 SendString(Headers.Names[I] + ': ' + Headers.ValueFromIndex[I] + #13#10); 171 187 end; … … 175 191 end; 176 192 177 Request.Destroy; 178 Response.Destroy; 179 end; 180 end; 181 182 procedure THTTPServer.ErrorResponse(Request: THTTPRequest; 183 Response: THTTPResponse); 184 begin 185 with Response.Stream do begin 193 Destroy; 194 end; 195 end; 196 end; 197 198 procedure THTTPServer.ErrorResponse(HandlerData: THTTPHandlerData); 199 begin 200 with HandlerData, Response.Stream do begin 186 201 WriteString('<html><body>Page ' + Request.Path + ' not found.</body></html>'); 187 202 end; 188 203 end; 189 204 190 procedure THTTPServer.FileResponse(Request: THTTPRequest; 191 Response: THTTPResponse); 205 procedure THTTPServer.FileResponse(HandlerData: THTTPHandlerData); 192 206 var 193 207 BinaryFile: TFileStream; 194 208 FileName: string; 195 209 begin 196 FileName := DocumentRoot + Request.Path; 197 if FileExists(FileName) then begin 198 Response.Headers.Values['Content-Type'] := GetMIMEType(Copy(ExtractFileExt(FileName), 2, 255)); 199 BinaryFile := TFileStream.Create(FileName, fmOpenRead); 200 Response.Stream.WriteStream(BinaryFile); 201 BinaryFile.Destroy; 202 end else 203 with Response.Stream do begin 204 WriteLn('File ' + Request.Path + ' not found.'); 205 WriteString('<html><body>File ' + Request.Path + ' not found.</body></html>'); 210 with HandlerData do begin 211 FileName := DocumentRoot + Request.Path; 212 if FileExists(FileName) then begin 213 Response.Headers.Values['Content-Type'] := GetMIMEType(Copy(ExtractFileExt(FileName), 2, 255)); 214 BinaryFile := TFileStream.Create(FileName, fmOpenRead); 215 Response.Stream.WriteStream(BinaryFile); 216 BinaryFile.Destroy; 217 end else 218 with Response.Stream do begin 219 WriteLn('File ' + Request.Path + ' not found.'); 220 WriteString('<html><body>File ' + Request.Path + ' not found.</body></html>'); 221 end; 206 222 end; 207 223 end; … … 329 345 end; 330 346 331 { THTTPSession } 332 333 procedure THTTPSession.Load(Request: THTTPRequest); 334 begin 335 336 end; 337 338 procedure THTTPSession.Save(Response: THTTPResponse); 339 begin 340 341 end; 342 343 constructor THTTPSession.Create; 344 begin 345 Variables := TStringList.Create; 346 end; 347 348 destructor THTTPSession.Destroy; 349 begin 350 Variables.Destroy; 347 { THTTPSessionStorage } 348 349 procedure THTTPSessionStorage.Load(HandlerData: THTTPHandlerData); 350 begin 351 352 end; 353 354 procedure THTTPSessionStorage.Save(HandlerData: THTTPHandlerData); 355 begin 356 357 end; 358 359 constructor THTTPSessionStorage.Create; 360 begin 361 362 end; 363 364 destructor THTTPSessionStorage.Destroy; 365 begin 366 inherited Destroy; 367 end; 368 369 { THTTPHandlerData } 370 371 constructor THTTPHandlerData.Create; 372 begin 373 Request := THTTPRequest.Create; 374 Response := THTTPResponse.Create; 375 Session := TStringList.Create; 376 end; 377 378 destructor THTTPHandlerData.Destroy; 379 begin 380 Request.Destroy; 381 Response.Destroy; 382 Session.Destroy; 351 383 inherited Destroy; 352 384 end;
Note:
See TracChangeset
for help on using the changeset viewer.