 |
Changeset 3459
- Timestamp:
- 04/24/08 22:27:25
(7 months ago)
- Author:
- kris
- Message:
updated HttpClient? to support keepalive, and added an example to HttpGet?
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r3250 |
r3459 |
|
| 81 | 81 | { |
|---|
| 82 | 82 | // callback for sending PUT content |
|---|
| 83 | | alias void delegate (IBuffer) Pump; |
|---|
| 84 | | |
|---|
| | 83 | alias void delegate (IBuffer) Pump; |
|---|
| | 84 | |
|---|
| 85 | 85 | // this is struct rather than typedef to avoid compiler bugs |
|---|
| 86 | 86 | private struct RequestMethod |
|---|
| … | … | |
| 109 | 109 | private bool doRedirect = true; |
|---|
| 110 | 110 | |
|---|
| | 111 | // attempt keepalive? |
|---|
| | 112 | private bool keepalive = false; |
|---|
| | 113 | |
|---|
| 111 | 114 | // limit the number of redirects, or catch circular redirects |
|---|
| 112 | 115 | private uint redirections, |
|---|
| 113 | 116 | redirectionLimit = 5; |
|---|
| 114 | 117 | |
|---|
| 115 | | // use HTTP v1.0? |
|---|
| 116 | | private static const char[] DefaultHttpVersion = "HTTP/1.0"; |
|---|
| | 118 | // the http version being sent with requests |
|---|
| | 119 | private char[] httpVersion; |
|---|
| | 120 | |
|---|
| | 121 | // http version id |
|---|
| | 122 | public enum Version {OnePointZero, OnePointOne}; |
|---|
| 117 | 123 | |
|---|
| 118 | 124 | // standard set of request methods ... |
|---|
| … | … | |
| 156 | 162 | |
|---|
| 157 | 163 | tmp = new Buffer (1024 * 4); |
|---|
| | 164 | output = new Buffer (1024 * 16); |
|---|
| | 165 | |
|---|
| 158 | 166 | paramsOut = new HttpParams (new Buffer (1024 * 4)); |
|---|
| 159 | 167 | headersOut = new HttpHeaders (new Buffer (1024 * 4)); |
|---|
| … | … | |
| 161 | 169 | |
|---|
| 162 | 170 | // decode the host name (may take a second or two) |
|---|
| 163 | | auto host = uri.getHost (); |
|---|
| | 171 | auto host = uri.getHost; |
|---|
| 164 | 172 | if (host) |
|---|
| 165 | 173 | address = new InternetAddress (host, uri.getValidPort); |
|---|
| 166 | 174 | else |
|---|
| 167 | 175 | responseLine.error ("invalid url provided to HttpClient ctor"); |
|---|
| | 176 | |
|---|
| | 177 | // default the http version to 1.0 |
|---|
| | 178 | setVersion (Version.OnePointZero); |
|---|
| 168 | 179 | } |
|---|
| 169 | 180 | |
|---|
| … | … | |
| 293 | 304 | |
|---|
| 294 | 305 | /*********************************************************************** |
|---|
| | 306 | |
|---|
| | 307 | Set the request method |
|---|
| | 308 | |
|---|
| | 309 | ***********************************************************************/ |
|---|
| | 310 | |
|---|
| | 311 | void setRequest (RequestMethod method) |
|---|
| | 312 | { |
|---|
| | 313 | this.method = method; |
|---|
| | 314 | } |
|---|
| | 315 | |
|---|
| | 316 | /*********************************************************************** |
|---|
| | 317 | |
|---|
| | 318 | Set the request version |
|---|
| | 319 | |
|---|
| | 320 | ***********************************************************************/ |
|---|
| | 321 | |
|---|
| | 322 | void setVersion (Version v) |
|---|
| | 323 | { |
|---|
| | 324 | static const char[][] versions = ["HTTP/1.0", "HTTP/1.1"]; |
|---|
| | 325 | |
|---|
| | 326 | httpVersion = versions[v]; |
|---|
| | 327 | } |
|---|
| | 328 | |
|---|
| | 329 | /*********************************************************************** |
|---|
| 295 | 330 | |
|---|
| 296 | 331 | enable/disable the internal redirection suppport |
|---|
| … | … | |
| 314 | 349 | } |
|---|
| 315 | 350 | |
|---|
| 316 | | /** |
|---|
| 317 | | * Deprecated: use setTimeout(TimeSpan) instead |
|---|
| 318 | | */ |
|---|
| 319 | | deprecated void setTimeout(double interval) |
|---|
| 320 | | { |
|---|
| 321 | | setTimeout(TimeSpan.interval(interval)); |
|---|
| 322 | | } |
|---|
| 323 | | |
|---|
| 324 | | /*********************************************************************** |
|---|
| 325 | | |
|---|
| 326 | | Overridable method to create a Socket. You may find a need |
|---|
| 327 | | to override this for some purpose; perhaps to add input or |
|---|
| 328 | | output filters. |
|---|
| 329 | | |
|---|
| 330 | | ***********************************************************************/ |
|---|
| 331 | | |
|---|
| 332 | | protected SocketConduit createSocket () |
|---|
| 333 | | { |
|---|
| 334 | | return new SocketConduit; |
|---|
| | 351 | /*********************************************************************** |
|---|
| | 352 | |
|---|
| | 353 | Control keepalive option |
|---|
| | 354 | |
|---|
| | 355 | ***********************************************************************/ |
|---|
| | 356 | |
|---|
| | 357 | void keepAlive (bool yes) |
|---|
| | 358 | { |
|---|
| | 359 | keepalive = yes; |
|---|
| 335 | 360 | } |
|---|
| 336 | 361 | |
|---|
| … | … | |
| 393 | 418 | responseLine.error ("too many redirections, or a circular redirection"); |
|---|
| 394 | 419 | |
|---|
| 395 | | // create socket, and connect it |
|---|
| 396 | | socket = createSocket; |
|---|
| 397 | | socket.setTimeout (timeout); |
|---|
| 398 | | socket.connect (address); |
|---|
| 399 | | |
|---|
| 400 | | // create buffers for input and output |
|---|
| | 420 | // new socket for each request? |
|---|
| | 421 | if (keepalive is false) |
|---|
| | 422 | close; |
|---|
| | 423 | |
|---|
| | 424 | // create socket and connect it. Retain prior socket if |
|---|
| | 425 | // not closed between calls |
|---|
| | 426 | if (socket is null) |
|---|
| | 427 | { |
|---|
| | 428 | socket = new SocketConduit; |
|---|
| | 429 | socket.setTimeout (timeout); |
|---|
| | 430 | socket.connect (address); |
|---|
| | 431 | } |
|---|
| | 432 | |
|---|
| | 433 | // setup buffers for input and output |
|---|
| | 434 | output.setConduit(socket); |
|---|
| 401 | 435 | if (input) |
|---|
| 402 | | input.clear, input.setConduit (socket); |
|---|
| | 436 | input.setConduit(socket).clear; |
|---|
| 403 | 437 | else |
|---|
| 404 | | input = new Buffer (socket); |
|---|
| 405 | | output = new Buffer (socket); |
|---|
| | 438 | input = new Buffer(socket); |
|---|
| 406 | 439 | |
|---|
| 407 | 440 | // save for read() method |
|---|
| … | … | |
| 413 | 446 | |
|---|
| 414 | 447 | // http/1.0 needs connection:close |
|---|
| 415 | | headersOut.add (HttpHeader.Connection, "close"); |
|---|
| | 448 | if (keepalive is false) |
|---|
| | 449 | headersOut.add (HttpHeader.Connection, "close"); |
|---|
| 416 | 450 | |
|---|
| 417 | 451 | // attach/extend query parameters if user has added some |
|---|
| … | … | |
| 429 | 463 | // should we emit query as part of request line? |
|---|
| 430 | 464 | if (query.length) |
|---|
| 431 | | if (method is Get) |
|---|
| | 465 | if (method != Post) |
|---|
| 432 | 466 | output ("?"), uri.encode (&output.consume, query, uri.IncQueryAll); |
|---|
| 433 | 467 | else |
|---|
| 434 | | if (method is Post && pump.funcptr is null) |
|---|
| | 468 | if (pump.funcptr is null) |
|---|
| 435 | 469 | { |
|---|
| 436 | 470 | // we're POSTing query text - add default info |
|---|
| … | … | |
| 443 | 477 | |
|---|
| 444 | 478 | // complete the request line, and emit headers too |
|---|
| 445 | | output (" ") (DefaultHttpVersion) (HttpConst.Eol); |
|---|
| | 479 | output (" ") (httpVersion) (HttpConst.Eol); |
|---|
| 446 | 480 | |
|---|
| 447 | 481 | headersOut.produce (&output.consume, HttpConst.Eol); |
|---|
| … | … | |
| 501 | 535 | auto host = uri.getHost(); |
|---|
| 502 | 536 | if (host) |
|---|
| 503 | | address = new InternetAddress (uri.getHost, uri.getValidPort); |
|---|
| | 537 | address = new InternetAddress (uri.getHost, uri.getValidPort); |
|---|
| 504 | 538 | else |
|---|
| 505 | 539 | responseLine.error ("redirect has invalid url: "~redirect); |
|---|
| r3160 |
r3459 |
|
| 83 | 83 | } |
|---|
| 84 | 84 | |
|---|
| | 85 | |
|---|
| | 86 | debug (HttpGet) |
|---|
| | 87 | { |
|---|
| | 88 | import tango.io.Console; |
|---|
| | 89 | |
|---|
| | 90 | void main() |
|---|
| | 91 | { |
|---|
| | 92 | // open a web-page for reading (see HttpPost for writing) |
|---|
| | 93 | auto page = new HttpGet ("http://www.digitalmars.com/d/intro.html"); |
|---|
| | 94 | |
|---|
| | 95 | // retrieve and flush display content |
|---|
| | 96 | Cout (cast(char[]) page.read) (); |
|---|
| | 97 | } |
|---|
| | 98 | } |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic