OLD | NEW |
---|---|
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 670 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
681 * header of the response. When the header has been set up the methods | 681 * header of the response. When the header has been set up the methods |
682 * from the [IOSink] can be used to write the actual body of the HTTP | 682 * from the [IOSink] can be used to write the actual body of the HTTP |
683 * response. When one of the [IOSink] methods is used for the | 683 * response. When one of the [IOSink] methods is used for the |
684 * first time the request header is send. Calling any methods that | 684 * first time the request header is send. Calling any methods that |
685 * will change the header after it is sent will throw an exception. | 685 * will change the header after it is sent will throw an exception. |
686 * | 686 * |
687 * When writing string data through the [IOSink] the encoding used | 687 * When writing string data through the [IOSink] the encoding used |
688 * will be determined from the "charset" parameter of the | 688 * will be determined from the "charset" parameter of the |
689 * "Content-Type" header. | 689 * "Content-Type" header. |
690 * | 690 * |
691 * HttpResponse response = ... | 691 * HttpResponse response = ... |
692 * response.headers.contentType | 692 * response.headers.contentType |
693 * = new ContentType("application", "json", charset: "utf-8"); | 693 * = new ContentType("application", "json", charset: "utf-8"); |
694 * response.write(...); // Strings written will be UTF-8 encoded. | 694 * response.write(...); // Strings written will be UTF-8 encoded. |
695 * | 695 * |
696 * If no charset is provided the default of ISO-8859-1 (Latin 1) will | 696 * If no charset is provided the default of ISO-8859-1 (Latin 1) will |
697 * be used. | 697 * be used. |
698 * | 698 * |
699 * HttpResponse response = ... | 699 * HttpResponse response = ... |
700 * response.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); | 700 * response.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); |
701 * response.write(...); // Strings written will be ISO-8859-1 encoded. | 701 * response.write(...); // Strings written will be ISO-8859-1 encoded. |
702 * | 702 * |
703 * If an unsupported encoding is used an exception will be thrown if | 703 * If an unsupported encoding is used an exception will be thrown if |
704 * using one of the write methods taking a string. | 704 * using one of the write methods taking a string. |
705 */ | 705 */ |
706 abstract class HttpResponse implements IOSink<HttpResponse> { | 706 abstract class HttpResponse implements IOSink<HttpResponse> { |
707 // TODO(ajohnsen): Add documentation of how to pipe a file to the response. | 707 // TODO(ajohnsen): Add documentation of how to pipe a file to the response. |
708 /** | 708 /** |
709 * Gets and sets the content length of the response. If the size of | 709 * Gets and sets the content length of the response. If the size of |
710 * the response is not known in advance set the content length to | 710 * the response is not known in advance set the content length to |
711 * -1 - which is also the default if not set. | 711 * -1 - which is also the default if not set. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
755 | 755 |
756 /** | 756 /** |
757 * Gets information about the client connection. Returns [null] if the socket | 757 * Gets information about the client connection. Returns [null] if the socket |
758 * is not available. | 758 * is not available. |
759 */ | 759 */ |
760 HttpConnectionInfo get connectionInfo; | 760 HttpConnectionInfo get connectionInfo; |
761 } | 761 } |
762 | 762 |
763 | 763 |
764 /** | 764 /** |
765 * HTTP client factory. The [HttpClient] handles all the sockets associated | 765 * The [HttpClient] class implements the client side of the HTTP |
766 * with the [HttpClientConnection]s and when the endpoint supports it, it will | 766 * protocol. It contains a number of methods to send a HTTP request |
767 * try to reuse opened sockets for several requests to support HTTP 1.1 | 767 * to a HTTP server and receive a HTTP response back. |
768 * persistent connections. This means that sockets will be kept open for some | 768 * |
769 * time after a requests have completed, unless HTTP procedures indicate that it | 769 * This is a two-step process, triggered by two futures. When the |
770 * must be closed as part of completing the request. Use [:HttpClient.close:] | 770 * first future completes with a [HttpClientRequest] the underlying |
771 * to force close the idle sockets. | 771 * network connection has been established, but no data has yet been |
772 * sent. The HTTP headers and body can be set on the request, and | |
773 * [:close:] is called to sent it to the server. | |
774 * | |
775 * The second future, is returned by [:close:], completes with an | |
Bill Hesse
2013/04/12 08:26:00
future, which is returned ...
Søren Gjesse
2013/04/12 08:28:37
Done.
| |
776 * [HttpClientResponse] object when the response is received from the | |
777 * server. This object contains the headers and body of the response. | |
778 | |
779 * The future for [HttpClientRequest] is created by methods such as | |
780 * [getUrl] and [open]. | |
781 * | |
782 * When the HTTP response is ready a [HttpClientResponse] object is | |
783 * provided which provides access to the headers and body of the response. | |
784 * | |
785 * HttpClient client = new HttpClient(); | |
786 * client.getUrl(new Uri.fromString("http://www.example.com/")) | |
787 * .then((HttpClientRequest request) { | |
788 * // Prepare the request then call close on it to send it. | |
789 * return request.close(); | |
790 * }) | |
791 * .then((HttpClientResponse response) { | |
792 * // Process the response. | |
793 * }); | |
794 * | |
795 * All [HttpClient] requests set the following header by default: | |
796 * | |
797 * Accept-Encoding: gzip | |
798 * | |
799 * This allows the HTTP server to use gzip compression for the body if | |
800 * possible. If this behavior is not desired set the | |
801 * "Accept-Encoding" header to something else. | |
802 * | |
803 * The [HttpClient] supports persistent connections and caches network | |
804 * connections to reuse then for multiple requests whenever | |
805 * possible. This means that network connections can be kept open for | |
806 * some time after a request have completed. Use [:HttpClient.close:] | |
807 * to force shut down the [HttpClient] object and close the idle | |
808 * network connections. | |
772 */ | 809 */ |
773 abstract class HttpClient { | 810 abstract class HttpClient { |
774 static const int DEFAULT_HTTP_PORT = 80; | 811 static const int DEFAULT_HTTP_PORT = 80; |
775 static const int DEFAULT_HTTPS_PORT = 443; | 812 static const int DEFAULT_HTTPS_PORT = 443; |
776 | 813 |
777 factory HttpClient() => new _HttpClient(); | 814 factory HttpClient() => new _HttpClient(); |
778 | 815 |
779 /** | 816 /** |
780 * Opens a HTTP connection. The returned [HttpClientRequest] is used to | 817 * Opens a HTTP connection. The returned [HttpClientRequest] is used to |
781 * fill in the content of the request before sending it. The 'host' header for | 818 * fill in the content of the request before sending it. The 'host' header for |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
899 * | 936 * |
900 * [:no_proxy:] and [:NO_PROXY:] specify a comma separated list of | 937 * [:no_proxy:] and [:NO_PROXY:] specify a comma separated list of |
901 * postfixes of hostnames for which not to use the proxy | 938 * postfixes of hostnames for which not to use the proxy |
902 * server. E.g. the value "localhost,127.0.0.1" will make requests | 939 * server. E.g. the value "localhost,127.0.0.1" will make requests |
903 * to both "localhost" and "127.0.0.1" not use a proxy. If both are set | 940 * to both "localhost" and "127.0.0.1" not use a proxy. If both are set |
904 * the lower case one takes precedence. | 941 * the lower case one takes precedence. |
905 * | 942 * |
906 * To activate this way of resolving proxies assign this function to | 943 * To activate this way of resolving proxies assign this function to |
907 * the [findProxy] property on the [HttpClient]. | 944 * the [findProxy] property on the [HttpClient]. |
908 * | 945 * |
909 * HttpClient client = new HttpClient(); | 946 * HttpClient client = new HttpClient(); |
910 * client.findProxy = HttpClient.findProxyFromEnvironment; | 947 * client.findProxy = HttpClient.findProxyFromEnvironment; |
911 * | 948 * |
912 * If you don't want to use the system environment you can use a | 949 * If you don't want to use the system environment you can use a |
913 * different one by wrapping the function. | 950 * different one by wrapping the function. |
914 * | 951 * |
915 * HttpClient client = new HttpClient(); | 952 * HttpClient client = new HttpClient(); |
916 * client.findProxy = (url) { | 953 * client.findProxy = (url) { |
917 * return HttpClient.findProxyFromEnvironment( | 954 * return HttpClient.findProxyFromEnvironment( |
918 * url, {"http_proxy": ..., "no_proxy": ...}); | 955 * url, {"http_proxy": ..., "no_proxy": ...}); |
919 * } | 956 * } |
920 */ | 957 */ |
921 static String findProxyFromEnvironment(Uri url, | 958 static String findProxyFromEnvironment(Uri url, |
922 {Map<String, String> environment}) { | 959 {Map<String, String> environment}) { |
923 return _HttpClient._findProxyFromEnvironment(url, environment); | 960 return _HttpClient._findProxyFromEnvironment(url, environment); |
924 } | 961 } |
925 | 962 |
926 /** | 963 /** |
927 * Shutdown the HTTP client. If [force] is [:false:] (the default) | 964 * Shutdown the HTTP client. If [force] is [:false:] (the default) |
928 * the [:HttpClient:] will be kept alive until all active | 965 * the [:HttpClient:] will be kept alive until all active |
929 * connections are done. If [force] is [:true:] any active | 966 * connections are done. If [force] is [:true:] any active |
(...skipping 14 matching lines...) Expand all Loading... | |
944 * header of the request. When the header has been set up the methods | 981 * header of the request. When the header has been set up the methods |
945 * from the [IOSink] can be used to write the actual body of the HTTP | 982 * from the [IOSink] can be used to write the actual body of the HTTP |
946 * request. When one of the [IOSink] methods is used for the first | 983 * request. When one of the [IOSink] methods is used for the first |
947 * time the request header is send. Calling any methods that will | 984 * time the request header is send. Calling any methods that will |
948 * change the header after it is sent will throw an exception. | 985 * change the header after it is sent will throw an exception. |
949 * | 986 * |
950 * When writing string data through the [IOSink] the | 987 * When writing string data through the [IOSink] the |
951 * encoding used will be determined from the "charset" parameter of | 988 * encoding used will be determined from the "charset" parameter of |
952 * the "Content-Type" header. | 989 * the "Content-Type" header. |
953 * | 990 * |
954 * HttpClientRequest request = ... | 991 * HttpClientRequest request = ... |
955 * request.headers.contentType | 992 * request.headers.contentType |
956 * = new ContentType("application", "json", charset: "utf-8"); | 993 * = new ContentType("application", "json", charset: "utf-8"); |
957 * request.write(...); // Strings written will be UTF-8 encoded. | 994 * request.write(...); // Strings written will be UTF-8 encoded. |
958 * | 995 * |
959 * If no charset is provided the default of ISO-8859-1 (Latin 1) will | 996 * If no charset is provided the default of ISO-8859-1 (Latin 1) will |
960 * be used. | 997 * be used. |
961 * | 998 * |
962 * HttpClientRequest request = ... | 999 * HttpClientRequest request = ... |
963 * request.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); | 1000 * request.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); |
964 * request.write(...); // Strings written will be ISO-8859-1 encoded. | 1001 * request.write(...); // Strings written will be ISO-8859-1 encoded. |
965 * | 1002 * |
966 * If an unsupported encoding is used an exception will be thrown if | 1003 * If an unsupported encoding is used an exception will be thrown if |
967 * using one of the write methods taking a string. | 1004 * using one of the write methods taking a string. |
968 */ | 1005 */ |
969 abstract class HttpClientRequest | 1006 abstract class HttpClientRequest |
970 implements IOSink<HttpClientRequest> { | 1007 implements IOSink<HttpClientRequest> { |
971 /** | 1008 /** |
972 * Gets and sets the content length of the request. If the size of | 1009 * Gets and sets the content length of the request. If the size of |
973 * the request is not known in advance set content length to -1, | 1010 * the request is not known in advance set content length to -1, |
974 * which is also the default. | 1011 * which is also the default. |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1219 class RedirectLimitExceededException extends RedirectException { | 1256 class RedirectLimitExceededException extends RedirectException { |
1220 const RedirectLimitExceededException(List<RedirectInfo> redirects) | 1257 const RedirectLimitExceededException(List<RedirectInfo> redirects) |
1221 : super("Redirect limit exceeded", redirects); | 1258 : super("Redirect limit exceeded", redirects); |
1222 } | 1259 } |
1223 | 1260 |
1224 | 1261 |
1225 class RedirectLoopException extends RedirectException { | 1262 class RedirectLoopException extends RedirectException { |
1226 const RedirectLoopException(List<RedirectInfo> redirects) | 1263 const RedirectLoopException(List<RedirectInfo> redirects) |
1227 : super("Redirect loop detected", redirects); | 1264 : super("Redirect loop detected", redirects); |
1228 } | 1265 } |
OLD | NEW |