Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: sdk/lib/io/http.dart

Issue 124753002: Code cleanup (mostly io lib and some http lib). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Merge to head. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.io; 5 part of dart.io;
6 6
7 /** 7 /**
8 * HTTP status codes. 8 * HTTP status codes.
9 */ 9 */
10 abstract class HttpStatus { 10 abstract class HttpStatus {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 * [InternetAddress] on port 80, and listening to requests. 76 * [InternetAddress] on port 80, and listening to requests.
77 * 77 *
78 * HttpServer.bind(InternetAddress.ANY_IP_V6, 80).then((server) { 78 * HttpServer.bind(InternetAddress.ANY_IP_V6, 80).then((server) {
79 * server.listen((HttpRequest request) { 79 * server.listen((HttpRequest request) {
80 * // Handle requests. 80 * // Handle requests.
81 * }); 81 * });
82 * }); 82 * });
83 */ 83 */
84 abstract class HttpServer implements Stream<HttpRequest> { 84 abstract class HttpServer implements Stream<HttpRequest> {
85 /** 85 /**
86 * Set and get the default value of the `Server` header for all responses
87 * generated by this [HttpServer]. The default value is
88 * `Dart/<version> (dart:io)`.
89 *
90 * If the serverHeader is set to `null`, no default `Server` header will be
91 * added to each response.
92 */
93 String serverHeader;
94
95 /**
96 * Get or set the timeout used for idle keep-alive connections. If no further
97 * request is seen within [idleTimeout] after the previous request was
98 * completed, the connection is dropped.
99 *
100 * Default is 120 seconds.
101 *
102 * To disable, set [idleTimeout] to `null`.
103 */
104 Duration idleTimeout;
105
106 /**
86 * Starts listening for HTTP requests on the specified [address] and 107 * Starts listening for HTTP requests on the specified [address] and
87 * [port]. 108 * [port].
88 * 109 *
89 * The [address] can either be a [String] or an 110 * The [address] can either be a [String] or an
90 * [InternetAddress]. If [address] is a [String], [bind] will 111 * [InternetAddress]. If [address] is a [String], [bind] will
91 * perform a [InternetAddress.lookup] and use the first value in the 112 * perform a [InternetAddress.lookup] and use the first value in the
92 * list. To listen on the loopback adapter, which will allow only 113 * list. To listen on the loopback adapter, which will allow only
93 * incoming connections from the local host, use the value 114 * incoming connections from the local host, use the value
94 * [InternetAddress.LOOPBACK_IP_V4] or 115 * [InternetAddress.LOOPBACK_IP_V4] or
95 * [InternetAddress.LOOPBACK_IP_V6]. To allow for incoming 116 * [InternetAddress.LOOPBACK_IP_V6]. To allow for incoming
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 * Sets the timeout, in seconds, for sessions of this [HttpServer]. 219 * Sets the timeout, in seconds, for sessions of this [HttpServer].
199 * The default timeout is 20 minutes. 220 * The default timeout is 20 minutes.
200 */ 221 */
201 set sessionTimeout(int timeout); 222 set sessionTimeout(int timeout);
202 223
203 /** 224 /**
204 * Returns an [HttpConnectionsInfo] object summarizing the number of 225 * Returns an [HttpConnectionsInfo] object summarizing the number of
205 * current connections handled by the server. 226 * current connections handled by the server.
206 */ 227 */
207 HttpConnectionsInfo connectionsInfo(); 228 HttpConnectionsInfo connectionsInfo();
208
209 /**
210 * Set and get the default value of the `Server` header for all responses
211 * generated by this [HttpServer]. The default value is
212 * `Dart/<version> (dart:io)`.
213 *
214 * If the serverHeader is set to `null`, no default `Server` header will be
215 * added to each response.
216 */
217 String serverHeader;
218
219 /**
220 * Get or set the timeout used for idle keep-alive connections. If no further
221 * request is seen within [idleTimeout] after the previous request was
222 * completed, the connection is droped.
223 *
224 * Default is 120 seconds.
225 *
226 * To disable, set [idleTimeout] to `null`.
227 */
228 Duration idleTimeout;
229 } 229 }
230 230
231 231
232 /** 232 /**
233 * Summary statistics about an [HttpServer]s current socket connections. 233 * Summary statistics about an [HttpServer]s current socket connections.
234 */ 234 */
235 class HttpConnectionsInfo { 235 class HttpConnectionsInfo {
236 /** 236 /**
237 * Total number of socket connections. 237 * Total number of socket connections.
238 */ 238 */
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 IF_RANGE, 365 IF_RANGE,
366 IF_UNMODIFIED_SINCE, 366 IF_UNMODIFIED_SINCE,
367 MAX_FORWARDS, 367 MAX_FORWARDS,
368 PROXY_AUTHORIZATION, 368 PROXY_AUTHORIZATION,
369 RANGE, 369 RANGE,
370 REFERER, 370 REFERER,
371 TE, 371 TE,
372 USER_AGENT]; 372 USER_AGENT];
373 373
374 /** 374 /**
375 * Gets and sets the date. The value of this property will
376 * reflect the 'date' header.
377 */
378 DateTime date;
379
380 /**
381 * Gets and sets the expiry date. The value of this property will
382 * reflect the 'expires' header.
383 */
384 DateTime expires;
385
386 /**
387 * Gets and sets the "if-modified-since" date. The value of this property will
388 * reflect the "if-modified-since" header.
389 */
390 DateTime ifModifiedSince;
391
392 /**
393 * Gets and sets the host part of the 'host' header for the
394 * connection.
395 */
396 String host;
397
398 /**
399 * Gets and sets the port part of the 'host' header for the
400 * connection.
401 */
402 int port;
403
404 /**
405 * Gets and sets the content type. Note that the content type in the
406 * header will only be updated if this field is set
407 * directly. Mutating the returned current value will have no
408 * effect.
409 */
410 ContentType contentType;
411
412 /**
413 * Gets and sets the content length header value.
414 */
415 int contentLength;
416
417 /**
418 * Gets and sets the persistent connection header value.
419 */
420 bool persistentConnection;
421
422 /**
423 * Gets and sets the chunked transfer encoding header value.
424 */
425 bool chunkedTransferEncoding;
426
427 /**
375 * Returns the list of values for the header named [name]. If there 428 * Returns the list of values for the header named [name]. If there
376 * is no header with the provided name, [:null:] will be returned. 429 * is no header with the provided name, [:null:] will be returned.
377 */ 430 */
378 List<String> operator[](String name); 431 List<String> operator[](String name);
379 432
380 /** 433 /**
381 * Convenience method for the value for a single valued header. If 434 * Convenience method for the value for a single valued header. If
382 * there is no header with the provided name, [:null:] will be 435 * there is no header with the provided name, [:null:] will be
383 * returned. If the header has more than one value an exception is 436 * returned. If the header has more than one value an exception is
384 * thrown. 437 * thrown.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 */ 477 */
425 void forEach(void f(String name, List<String> values)); 478 void forEach(void f(String name, List<String> values));
426 479
427 /** 480 /**
428 * Disables folding for the header named [name] when sending the HTTP 481 * Disables folding for the header named [name] when sending the HTTP
429 * header. By default, multiple header values are folded into a 482 * header. By default, multiple header values are folded into a
430 * single header line by separating the values with commas. The 483 * single header line by separating the values with commas. The
431 * 'set-cookie' header has folding disabled by default. 484 * 'set-cookie' header has folding disabled by default.
432 */ 485 */
433 void noFolding(String name); 486 void noFolding(String name);
434
435 /**
436 * Gets and sets the date. The value of this property will
437 * reflect the 'date' header.
438 */
439 DateTime date;
440
441 /**
442 * Gets and sets the expiry date. The value of this property will
443 * reflect the 'expires' header.
444 */
445 DateTime expires;
446
447 /**
448 * Gets and sets the "if-modified-since" date. The value of this property will
449 * reflect the "if-modified-since" header.
450 */
451 DateTime ifModifiedSince;
452
453 /**
454 * Gets and sets the host part of the 'host' header for the
455 * connection.
456 */
457 String host;
458
459 /**
460 * Gets and sets the port part of the 'host' header for the
461 * connection.
462 */
463 int port;
464
465 /**
466 * Gets and sets the content type. Note that the content type in the
467 * header will only be updated if this field is set
468 * directly. Mutating the returned current value will have no
469 * effect.
470 */
471 ContentType contentType;
472
473 /**
474 * Gets and sets the content length header value.
475 */
476 int contentLength;
477
478 /**
479 * Gets and sets the persistent connection header value.
480 */
481 bool persistentConnection;
482
483 /**
484 * Gets and sets the chunked transfer encoding header value.
485 */
486 bool chunkedTransferEncoding;
487 } 487 }
488 488
489 489
490 /** 490 /**
491 * Representation of a header value in the form: 491 * Representation of a header value in the form:
492 * 492 *
493 * [:value; parameter1=value1; parameter2=value2:] 493 * [:value; parameter1=value1; parameter2=value2:]
494 * 494 *
495 * [HeaderValue] can be used to conveniently build and parse header 495 * [HeaderValue] can be used to conveniently build and parse header
496 * values on this form. 496 * values on this form.
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
640 640
641 /** 641 /**
642 * Representation of a cookie. For cookies received by the server as 642 * Representation of a cookie. For cookies received by the server as
643 * Cookie header values only [:name:] and [:value:] fields will be 643 * Cookie header values only [:name:] and [:value:] fields will be
644 * set. When building a cookie for the 'set-cookie' header in the server 644 * set. When building a cookie for the 'set-cookie' header in the server
645 * and when receiving cookies in the client as 'set-cookie' headers all 645 * and when receiving cookies in the client as 'set-cookie' headers all
646 * fields can be used. 646 * fields can be used.
647 */ 647 */
648 abstract class Cookie { 648 abstract class Cookie {
649 /** 649 /**
650 * Creates a new cookie optionally setting the name and value.
651 */
652 factory Cookie([String name, String value]) => new _Cookie(name, value);
653
654 /**
655 * Creates a new cookie by parsing a header value from a 'set-cookie'
656 * header.
657 */
658 factory Cookie.fromSetCookieValue(String value) {
659 return new _Cookie.fromSetCookieValue(value);
660 }
661
662 /**
663 * Gets and sets the name. 650 * Gets and sets the name.
664 */ 651 */
665 String name; 652 String name;
666 653
667 /** 654 /**
668 * Gets and sets the value. 655 * Gets and sets the value.
669 */ 656 */
670 String value; 657 String value;
671 658
672 /** 659 /**
(...skipping 21 matching lines...) Expand all
694 * Gets and sets whether this cookie is secure. 681 * Gets and sets whether this cookie is secure.
695 */ 682 */
696 bool secure; 683 bool secure;
697 684
698 /** 685 /**
699 * Gets and sets whether this cookie is HTTP only. 686 * Gets and sets whether this cookie is HTTP only.
700 */ 687 */
701 bool httpOnly; 688 bool httpOnly;
702 689
703 /** 690 /**
691 * Creates a new cookie optionally setting the name and value.
692 */
693 factory Cookie([String name, String value]) => new _Cookie(name, value);
694
695 /**
696 * Creates a new cookie by parsing a header value from a 'set-cookie'
697 * header.
698 */
699 factory Cookie.fromSetCookieValue(String value) {
700 return new _Cookie.fromSetCookieValue(value);
701 }
702
703 /**
704 * Returns the formatted string representation of the cookie. The 704 * Returns the formatted string representation of the cookie. The
705 * string representation can be used for for setting the Cookie or 705 * string representation can be used for for setting the Cookie or
706 * 'set-cookie' headers 706 * 'set-cookie' headers
707 */ 707 */
708 String toString(); 708 String toString();
709 } 709 }
710 710
711 711
712 /** 712 /**
713 * A server-side object 713 * A server-side object
(...skipping 17 matching lines...) Expand all
731 * In the following code, an HttpServer listens 731 * In the following code, an HttpServer listens
732 * for HTTP requests and, within the callback function, 732 * for HTTP requests and, within the callback function,
733 * uses the HttpRequest object's `method` property to dispatch requests. 733 * uses the HttpRequest object's `method` property to dispatch requests.
734 * 734 *
735 * final HOST = InternetAddress.LOOPBACK_IP_V4; 735 * final HOST = InternetAddress.LOOPBACK_IP_V4;
736 * final PORT = 4040; 736 * final PORT = 4040;
737 * 737 *
738 * HttpServer.bind(HOST, PORT).then((_server) { 738 * HttpServer.bind(HOST, PORT).then((_server) {
739 * _server.listen((HttpRequest request) { 739 * _server.listen((HttpRequest request) {
740 * switch (request.method) { 740 * switch (request.method) {
741 * case 'GET': 741 * case 'GET':
742 * handleGetRequest(request); 742 * handleGetRequest(request);
743 * break; 743 * break;
744 * case 'POST': 744 * case 'POST':
745 * ... 745 * ...
746 * } 746 * }
747 * }, 747 * },
748 * onError: handleError); // listen() failed. 748 * onError: handleError); // listen() failed.
749 * }).catchError(handleError); 749 * }).catchError(handleError);
750 * 750 *
751 * Listen to the HttpRequest stream to handle the 751 * Listen to the HttpRequest stream to handle the
752 * data and be notified once the entire body is received. 752 * data and be notified once the entire body is received.
753 * An HttpRequest object contains an [HttpResponse] object, 753 * An HttpRequest object contains an [HttpResponse] object,
754 * to which the server can write its response. 754 * to which the server can write its response.
755 * For example, here's a skeletal callback function 755 * For example, here's a skeletal callback function
756 * that responds to a request: 756 * that responds to a request:
757 * 757 *
758 * void handleGetRequest(HttpRequest req) { 758 * void handleGetRequest(HttpRequest req) {
759 * HttpResponse res = req.response; 759 * HttpResponse res = req.response;
760 * var body = []; 760 * var body = [];
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
817 * The session for the given request (read-only). 817 * The session for the given request (read-only).
818 * 818 *
819 * If the session is 819 * If the session is
820 * being initialized by this call, [:isNew:] is true for the returned 820 * being initialized by this call, [:isNew:] is true for the returned
821 * session. 821 * session.
822 * See [HttpServer.sessionTimeout] on how to change default timeout. 822 * See [HttpServer.sessionTimeout] on how to change default timeout.
823 */ 823 */
824 HttpSession get session; 824 HttpSession get session;
825 825
826 /** 826 /**
827 * The HTTP protocol version used in the request, 827 * The HTTP protocol version used in the request,
828 * either "1.0" or "1.1" (read-only). 828 * either "1.0" or "1.1" (read-only).
829 */ 829 */
830 String get protocolVersion; 830 String get protocolVersion;
831 831
832 /** 832 /**
833 * Information about the client connection (read-only). 833 * Information about the client connection (read-only).
834 * 834 *
835 * Returns [null] if the socket is not available. 835 * Returns [null] if the socket is not available.
836 */ 836 */
837 HttpConnectionInfo get connectionInfo; 837 HttpConnectionInfo get connectionInfo;
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after
1323 * 1323 *
1324 * HttpClientRequest request = ... 1324 * HttpClientRequest request = ...
1325 * request.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); 1325 * request.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain");
1326 * request.write(...); // Strings written will be ISO-8859-1 encoded. 1326 * request.write(...); // Strings written will be ISO-8859-1 encoded.
1327 * 1327 *
1328 * If an unsupported encoding is used an exception will be thrown if 1328 * If an unsupported encoding is used an exception will be thrown if
1329 * using one of the write methods taking a string. 1329 * using one of the write methods taking a string.
1330 */ 1330 */
1331 abstract class HttpClientRequest implements IOSink { 1331 abstract class HttpClientRequest implements IOSink {
1332 /** 1332 /**
1333 * Gets and sets the requested persistent connection state.
1334 * The default value is [:true:].
1335 */
1336 bool persistentConnection;
1337
1338 /**
1339 * Set this property to [:true:] if this request should
1340 * automatically follow redirects. The default is [:true:].
1341 *
1342 * Automatic redirect will only happen for "GET" and "HEAD" requests
1343 * and only for the status codes [:HttpHeaders.MOVED_PERMANENTLY:]
1344 * (301), [:HttpStatus.FOUND:] (302),
1345 * [:HttpStatus.MOVED_TEMPORARILY:] (302, alias for
1346 * [:HttpStatus.FOUND:]), [:HttpStatus.SEE_OTHER:] (303) and
1347 * [:HttpStatus.TEMPORARY_REDIRECT:] (307). For
1348 * [:HttpStatus.SEE_OTHER:] (303) autmatic redirect will also happen
1349 * for "POST" requests with the method changed to "GET" when
1350 * following the redirect.
1351 *
1352 * All headers added to the request will be added to the redirection
1353 * request(s). However, any body send with the request will not be
1354 * part of the redirection request(s).
1355 */
1356 bool followRedirects;
1357
1358 /**
1359 * Set this property to the maximum number of redirects to follow
1360 * when [followRedirects] is [:true:]. If this number is exceeded the
1361 * [onError] callback will be called with a [RedirectException].
1362 *
1363 * The default value is 5.
1364 */
1365 int maxRedirects;
1366
1367 /**
1333 * The method of the request. 1368 * The method of the request.
1334 */ 1369 */
1335 String get method; 1370 String get method;
1336 1371
1337 /** 1372 /**
1338 * The uri of the request. 1373 * The uri of the request.
1339 */ 1374 */
1340 Uri get uri; 1375 Uri get uri;
1341 1376
1342 /** 1377 /**
1343 * Gets and sets the content length of the request. If the size of 1378 * Gets and sets the content length of the request. If the size of
1344 * the request is not known in advance set content length to -1, 1379 * the request is not known in advance set content length to -1,
1345 * which is also the default. 1380 * which is also the default.
1346 */ 1381 */
1347 int contentLength; 1382 int contentLength;
1348 1383
1349 /** 1384 /**
1350 * Returns the request headers. 1385 * Returns the request headers.
1351 */ 1386 */
1352 HttpHeaders get headers; 1387 HttpHeaders get headers;
1353 1388
1354 /** 1389 /**
1355 * Cookies to present to the server (in the 'cookie' header). 1390 * Cookies to present to the server (in the 'cookie' header).
1356 */ 1391 */
1357 List<Cookie> get cookies; 1392 List<Cookie> get cookies;
1358 1393
1359 /** 1394 /**
1360 * Gets and sets the requested persistent connection state.
1361 * The default value is [:true:].
1362 */
1363 bool persistentConnection;
1364
1365 /**
1366 * A [HttpClientResponse] future that will complete once the response is 1395 * A [HttpClientResponse] future that will complete once the response is
1367 * available. If an error occurs before the response is available, this 1396 * available. If an error occurs before the response is available, this
1368 * future will complete with an error. 1397 * future will complete with an error.
1369 */ 1398 */
1370 Future<HttpClientResponse> get done; 1399 Future<HttpClientResponse> get done;
1371 1400
1372 /** 1401 /**
1373 * Close the request for input. Returns the value of [done]. 1402 * Close the request for input. Returns the value of [done].
1374 */ 1403 */
1375 Future<HttpClientResponse> close(); 1404 Future<HttpClientResponse> close();
1376 1405
1377 /** 1406 /**
1378 * Set this property to [:true:] if this request should
1379 * automatically follow redirects. The default is [:true:].
1380 *
1381 * Automatic redirect will only happen for "GET" and "HEAD" requests
1382 * and only for the status codes [:HttpHeaders.MOVED_PERMANENTLY:]
1383 * (301), [:HttpStatus.FOUND:] (302),
1384 * [:HttpStatus.MOVED_TEMPORARILY:] (302, alias for
1385 * [:HttpStatus.FOUND:]), [:HttpStatus.SEE_OTHER:] (303) and
1386 * [:HttpStatus.TEMPORARY_REDIRECT:] (307). For
1387 * [:HttpStatus.SEE_OTHER:] (303) autmatic redirect will also happen
1388 * for "POST" requests with the method changed to "GET" when
1389 * following the redirect.
1390 *
1391 * All headers added to the request will be added to the redirection
1392 * request(s). However, any body send with the request will not be
1393 * part of the redirection request(s).
1394 */
1395 bool followRedirects;
1396
1397 /**
1398 * Set this property to the maximum number of redirects to follow
1399 * when [followRedirects] is [:true:]. If this number is exceeded the
1400 * [onError] callback will be called with a [RedirectException].
1401 *
1402 * The default value is 5.
1403 */
1404 int maxRedirects;
1405
1406 /**
1407 * Get information about the client connection. Returns [null] if the socket 1407 * Get information about the client connection. Returns [null] if the socket
1408 * is not available. 1408 * is not available.
1409 */ 1409 */
1410 HttpConnectionInfo get connectionInfo; 1410 HttpConnectionInfo get connectionInfo;
1411 } 1411 }
1412 1412
1413 1413
1414 /** 1414 /**
1415 * HTTP response for a client connection. The [HttpClientResponse] is a 1415 * HTTP response for a client connection. The [HttpClientResponse] is a
1416 * [Stream] of the body content of the response. Listen to the body to handle 1416 * [Stream] of the body content of the response. Listen to the body to handle
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
1577 } 1577 }
1578 1578
1579 1579
1580 class HttpException implements IOException { 1580 class HttpException implements IOException {
1581 final String message; 1581 final String message;
1582 final Uri uri; 1582 final Uri uri;
1583 1583
1584 const HttpException(String this.message, {Uri this.uri}); 1584 const HttpException(String this.message, {Uri this.uri});
1585 1585
1586 String toString() { 1586 String toString() {
1587 var b = new StringBuffer(); 1587 var b = new StringBuffer()
1588 b.write('HttpException: '); 1588 ..write('HttpException: ')
1589 b.write(message); 1589 ..write(message);
1590 if (uri != null) { 1590 if (uri != null) {
1591 b.write(', uri = $uri'); 1591 b.write(', uri = $uri');
1592 } 1592 }
1593 return b.toString(); 1593 return b.toString();
1594 } 1594 }
1595 } 1595 }
1596 1596
1597 1597
1598 class RedirectException implements HttpException { 1598 class RedirectException implements HttpException {
1599 final String message; 1599 final String message;
1600 final List<RedirectInfo> redirects; 1600 final List<RedirectInfo> redirects;
1601 1601
1602 const RedirectException(String this.message, 1602 const RedirectException(this.message, this.redirects);
1603 List<RedirectInfo> this.redirects);
1604 1603
1605 String toString() => "RedirectException: $message"; 1604 String toString() => "RedirectException: $message";
1606 1605
1607 Uri get uri => redirects.last.location; 1606 Uri get uri => redirects.last.location;
1608 } 1607 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698