Changeset 86
- Timestamp:
- Dec 17, 2009, 8:25:15 AM (15 years ago)
- Location:
- branches/DirectWeb
- Files:
-
- 3 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 -
branches/DirectWeb/UWebServer.pas
r85 r86 23 23 function PagesList(URL: string; Page, TotalCount, CountPerPage: Integer 24 24 ): string; 25 procedure ServerInfo(Request: THTTPRequest; Response: THTTPResponse); 25 26 public 26 27 HTTPServer: THTTPServer; … … 144 145 OrderDirection: string; 145 146 Title: string; 146 LinkQuery: TQuery String;147 LinkQuery: TQueryParameterList; 147 148 begin 148 149 SqlDatabase := TSqlDatabase.Create; … … 165 166 ' ORDER BY ' + OrderColumn + ' ' + OrderDirection); 166 167 167 LinkQuery := TQuery String.Create;168 LinkQuery := TQueryParameterList.Create; 168 169 LinkQuery.Assign(Request.Query); 169 170 … … 209 210 OrderDirection: string; 210 211 Title: string; 211 LinkQuery: TQuery String;212 LinkQuery: TQueryParameterList; 212 213 begin 213 214 SqlDatabase := TSqlDatabase.Create; … … 237 238 end; 238 239 240 procedure TWebServer.ServerInfo(Request: THTTPRequest; Response: THTTPResponse); 241 var 242 I: Integer; 243 begin 244 with Response.Stream do begin 245 Response.Cookies.Values['Test'] := 'Halo'; 246 247 WriteString('<a href="?ServerInfo">Refresh</a>'); 248 249 WriteString('<h5>Request HTTP headers</h5>'); 250 251 for I := 0 to Request.Headers.Count - 1 do begin; 252 WriteString(Request.Headers.Strings[I] + '<br/>'); 253 end; 254 255 WriteString('<h5>Response HTTP headers</h5>'); 256 with Response.Stream do 257 for I := 0 to Response.Headers.Count - 1 do begin; 258 WriteString(Response.Headers.Strings[I] + '<br/>'); 259 end; 260 end; 261 end; 262 239 263 procedure TWebServer.SendIndex(Request: THTTPRequest; Response: THTTPResponse); 240 264 begin … … 297 321 Add('/ViewList', ViewList); 298 322 Add('/ViewItem', ViewItem); 323 Add('/ServerInfo', ServerInfo); 299 324 end; 300 325 end; -
branches/DirectWeb/WoWHostingWebServer.lpi
r85 r86 76 76 <IsPartOfProject Value="True"/> 77 77 <UnitName Value="UHTTPServer"/> 78 <CursorPos X=" 23" Y="153"/>79 <TopLine Value="1 41"/>78 <CursorPos X="37" Y="141"/> 79 <TopLine Value="123"/> 80 80 <EditorIndex Value="4"/> 81 81 <UsageCount Value="200"/> … … 84 84 <Unit6> 85 85 <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\classes\classesh.inc"/> 86 <CursorPos X="22" Y="1431"/> 87 <TopLine Value="1426"/> 86 <CursorPos X="14" Y="556"/> 87 <TopLine Value="539"/> 88 <EditorIndex Value="5"/> 88 89 <UsageCount Value="77"/> 90 <Loaded Value="True"/> 89 91 </Unit6> 90 92 <Unit7> … … 93 95 <CursorPos X="13" Y="1802"/> 94 96 <TopLine Value="1797"/> 95 <EditorIndex Value=" 8"/>97 <EditorIndex Value="9"/> 96 98 <UsageCount Value="100"/> 97 99 <Loaded Value="True"/> … … 113 115 <CursorPos X="45" Y="15"/> 114 116 <TopLine Value="1"/> 115 <EditorIndex Value=" 5"/>117 <EditorIndex Value="6"/> 116 118 <UsageCount Value="200"/> 117 119 <Loaded Value="True"/> … … 130 132 <CursorPos X="24" Y="61"/> 131 133 <TopLine Value="1"/> 132 <UsageCount Value="7 4"/>134 <UsageCount Value="76"/> 133 135 </Unit11> 134 136 <Unit12> 135 137 <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\classes\stringl.inc"/> 136 <CursorPos X=" 3" Y="193"/>137 <TopLine Value=" 186"/>138 <CursorPos X="41" Y="704"/> 139 <TopLine Value="690"/> 138 140 <UsageCount Value="11"/> 139 141 </Unit12> … … 150 152 <CursorPos X="3" Y="130"/> 151 153 <TopLine Value="121"/> 152 <EditorIndex Value=" 7"/>153 <UsageCount Value="7 3"/>154 <EditorIndex Value="8"/> 155 <UsageCount Value="75"/> 154 156 <Loaded Value="True"/> 155 157 </Unit14> … … 160 162 <CursorPos X="17" Y="41"/> 161 163 <TopLine Value="25"/> 162 <EditorIndex Value=" 6"/>163 <UsageCount Value="7 3"/>164 <EditorIndex Value="7"/> 165 <UsageCount Value="75"/> 164 166 <Loaded Value="True"/> 165 167 </Unit15> … … 170 172 <TopLine Value="330"/> 171 173 <EditorIndex Value="2"/> 172 <UsageCount Value="3 5"/>174 <UsageCount Value="36"/> 173 175 <Loaded Value="True"/> 174 176 </Unit16> … … 202 204 <IsPartOfProject Value="True"/> 203 205 <UnitName Value="UWebServer"/> 204 <CursorPos X="3 1" Y="295"/>205 <TopLine Value="2 77"/>206 <CursorPos X="35" Y="245"/> 207 <TopLine Value="231"/> 206 208 <EditorIndex Value="1"/> 207 <UsageCount Value="6 3"/>209 <UsageCount Value="65"/> 208 210 <Loaded Value="True"/> 209 211 </Unit21> … … 220 222 <CursorPos X="32" Y="11"/> 221 223 <TopLine Value="1"/> 222 <EditorIndex Value="1 0"/>223 <UsageCount Value="2 1"/>224 <EditorIndex Value="11"/> 225 <UsageCount Value="23"/> 224 226 <Loaded Value="True"/> 225 227 <SyntaxHighlighter Value="XML"/> … … 230 232 <CursorPos X="1" Y="8"/> 231 233 <TopLine Value="1"/> 232 <EditorIndex Value=" 9"/>233 <UsageCount Value="2 1"/>234 <EditorIndex Value="10"/> 235 <UsageCount Value="23"/> 234 236 <Loaded Value="True"/> 235 237 <SyntaxHighlighter Value="None"/> … … 238 240 <JumpHistory Count="30" HistoryIndex="29"> 239 241 <Position1> 240 <Filename Value="U WebServer.pas"/>241 <Caret Line=" 47" Column="16" TopLine="30"/>242 <Filename Value="UHTTPServer.pas"/> 243 <Caret Line="108" Column="1" TopLine="91"/> 242 244 </Position1> 243 245 <Position2> 244 <Filename Value="U WebServer.pas"/>245 <Caret Line=" 48" Column="39" TopLine="31"/>246 <Filename Value="UHTTPServer.pas"/> 247 <Caret Line="109" Column="1" TopLine="92"/> 246 248 </Position2> 247 249 <Position3> 248 <Filename Value="U WebServer.pas"/>249 <Caret Line="11 " Column="9" TopLine="1"/>250 <Filename Value="UHTTPServer.pas"/> 251 <Caret Line="111" Column="1" TopLine="94"/> 250 252 </Position3> 251 253 <Position4> 252 <Filename Value="U WebServer.pas"/>253 <Caret Line=" 36" Column="29" TopLine="7"/>254 <Filename Value="UHTTPServer.pas"/> 255 <Caret Line="122" Column="13" TopLine="106"/> 254 256 </Position4> 255 257 <Position5> 256 <Filename Value="U WebServer.pas"/>257 <Caret Line=" 35" Column="34" TopLine="19"/>258 <Filename Value="UHTTPServer.pas"/> 259 <Caret Line="85" Column="23" TopLine="68"/> 258 260 </Position5> 259 261 <Position6> 260 <Filename Value="U WebServer.pas"/>261 <Caret Line="2 90" Column="22" TopLine="261"/>262 <Filename Value="UHTTPServer.pas"/> 263 <Caret Line="21" Column="15" TopLine="1"/> 262 264 </Position6> 263 265 <Position7> 264 <Filename Value="U WebServer.pas"/>265 <Caret Line=" 272" Column="19" TopLine="255"/>266 <Filename Value="UHTTPServer.pas"/> 267 <Caret Line="44" Column="26" TopLine="16"/> 266 268 </Position7> 267 269 <Position8> 268 <Filename Value="U WebServer.pas"/>269 <Caret Line="2 75" Column="1" TopLine="259"/>270 <Filename Value="UHTTPServer.pas"/> 271 <Caret Line="282" Column="1" TopLine="259"/> 270 272 </Position8> 271 273 <Position9> 272 <Filename Value="U WebServer.pas"/>273 <Caret Line="2 72" Column="20" TopLine="262"/>274 <Filename Value="UHTTPServer.pas"/> 275 <Caret Line="249" Column="29" TopLine="232"/> 274 276 </Position9> 275 277 <Position10> 276 <Filename Value=" WoWHostingWebServer.lpr"/>277 <Caret Line="1 4" Column="1" TopLine="1"/>278 <Filename Value="UWebServer.pas"/> 279 <Caret Line="12" Column="22" TopLine="1"/> 278 280 </Position10> 279 281 <Position11> 280 <Filename Value=" WoWHostingWebServer.lpr"/>281 <Caret Line="1 8" Column="18" TopLine="1"/>282 <Filename Value="UWebServer.pas"/> 283 <Caret Line="147" Column="33" TopLine="130"/> 282 284 </Position11> 283 285 <Position12> 284 <Filename Value=" WoWHostingWebServer.lpr"/>285 <Caret Line=" 20" Column="11" TopLine="1"/>286 <Filename Value="UWebServer.pas"/> 287 <Caret Line="168" Column="37" TopLine="151"/> 286 288 </Position12> 287 289 <Position13> 288 <Filename Value=" WoWHostingWebServer.lpr"/>289 <Caret Line=" 21" Column="1" TopLine="1"/>290 <Filename Value="UHTTPServer.pas"/> 291 <Caret Line="121" Column="24" TopLine="98"/> 290 292 </Position13> 291 293 <Position14> 292 <Filename Value=" WoWHostingWebServer.lpr"/>293 <Caret Line=" 16" Column="31" TopLine="1"/>294 <Filename Value="UWebServer.pas"/> 295 <Caret Line="245" Column="35" TopLine="231"/> 294 296 </Position14> 295 297 <Position15> 296 <Filename Value=" WoWHostingWebServer.lpr"/>297 <Caret Line=" 18" Column="31" TopLine="1"/>298 <Filename Value="UHTTPServer.pas"/> 299 <Caret Line="289" Column="19" TopLine="271"/> 298 300 </Position15> 299 301 <Position16> 300 <Filename Value=" WoWHostingWebServer.lpr"/>301 <Caret Line=" 19" Column="19" TopLine="1"/>302 <Filename Value="UHTTPServer.pas"/> 303 <Caret Line="284" Column="1" TopLine="267"/> 302 304 </Position16> 303 305 <Position17> 304 <Filename Value=" WoWHostingWebServer.lpr"/>305 <Caret Line="2 3" Column="16" TopLine="1"/>306 <Filename Value="UHTTPServer.pas"/> 307 <Caret Line="285" Column="1" TopLine="268"/> 306 308 </Position17> 307 309 <Position18> 308 <Filename Value=" WoWHostingWebServer.lpr"/>309 <Caret Line="2 0" Column="1" TopLine="1"/>310 <Filename Value="UHTTPServer.pas"/> 311 <Caret Line="286" Column="1" TopLine="269"/> 310 312 </Position18> 311 313 <Position19> 312 <Filename Value="U TCPServer.pas"/>313 <Caret Line=" 42" Column="32" TopLine="25"/>314 <Filename Value="UHTTPServer.pas"/> 315 <Caret Line="287" Column="1" TopLine="270"/> 314 316 </Position19> 315 317 <Position20> 316 <Filename Value="U TCPServer.pas"/>317 <Caret Line=" 80" Column="10" TopLine="74"/>318 <Filename Value="UHTTPServer.pas"/> 319 <Caret Line="288" Column="1" TopLine="271"/> 318 320 </Position20> 319 321 <Position21> 320 <Filename Value=" ..\..\..\..\Knihovny\Free Pascal\synapse\blcksock.pas"/>321 <Caret Line="1 802" Column="13" TopLine="1797"/>322 <Filename Value="UHTTPServer.pas"/> 323 <Caret Line="121" Column="29" TopLine="91"/> 322 324 </Position21> 323 325 <Position22> 324 <Filename Value="U WebServer.pas"/>325 <Caret Line="1 52" Column="22" TopLine="139"/>326 <Filename Value="UHTTPServer.pas"/> 327 <Caret Line="119" Column="27" TopLine="121"/> 326 328 </Position22> 327 329 <Position23> 328 <Filename Value="U WebServer.pas"/>329 <Caret Line="1 33" Column="83" TopLine="116"/>330 <Filename Value="UHTTPServer.pas"/> 331 <Caret Line="140" Column="1" TopLine="123"/> 330 332 </Position23> 331 333 <Position24> 332 <Filename Value="U WebServer.pas"/>333 <Caret Line="1 54" Column="22" TopLine="135"/>334 <Filename Value="UHTTPServer.pas"/> 335 <Caret Line="141" Column="1" TopLine="124"/> 334 336 </Position24> 335 337 <Position25> 336 <Filename Value="U WebServer.pas"/>337 <Caret Line="1 33" Column="83" TopLine="116"/>338 <Filename Value="UHTTPServer.pas"/> 339 <Caret Line="142" Column="30" TopLine="122"/> 338 340 </Position25> 339 341 <Position26> 340 <Filename Value="U WebServer.pas"/>341 <Caret Line=" 211" Column="58" TopLine="197"/>342 <Filename Value="UHTTPServer.pas"/> 343 <Caret Line="146" Column="1" TopLine="129"/> 342 344 </Position26> 343 345 <Position27> 344 <Filename Value="U WebServer.pas"/>345 <Caret Line=" 51" Column="1" TopLine="34"/>346 <Filename Value="UHTTPServer.pas"/> 347 <Caret Line="147" Column="1" TopLine="130"/> 346 348 </Position27> 347 349 <Position28> 348 <Filename Value="U WebServer.pas"/>349 <Caret Line=" 68" Column="43" TopLine="132"/>350 <Filename Value="UHTTPServer.pas"/> 351 <Caret Line="148" Column="1" TopLine="131"/> 350 352 </Position28> 351 353 <Position29> 352 <Filename Value="U WebServer.pas"/>353 <Caret Line=" 296" Column="34" TopLine="277"/>354 <Filename Value="UHTTPServer.pas"/> 355 <Caret Line="149" Column="1" TopLine="132"/> 354 356 </Position29> 355 357 <Position30> 356 <Filename Value="U WebServer.pas"/>357 <Caret Line=" 295" Column="31" TopLine="277"/>358 <Filename Value="UHTTPServer.pas"/> 359 <Caret Line="152" Column="1" TopLine="135"/> 358 360 </Position30> 359 361 </JumpHistory> … … 374 376 </CompilerOptions> 375 377 <Debugging> 378 <BreakPoints Count="2"> 379 <Item1> 380 <Source Value="UHTTPServer.pas"/> 381 <Line Value="286"/> 382 </Item1> 383 <Item2> 384 <Source Value="UHTTPServer.pas"/> 385 <Line Value="140"/> 386 </Item2> 387 </BreakPoints> 376 388 <Exceptions Count="3"> 377 389 <Item1>
Note:
See TracChangeset
for help on using the changeset viewer.