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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
48 static const int BAD_GATEWAY = 502; | 48 static const int BAD_GATEWAY = 502; |
49 static const int SERVICE_UNAVAILABLE = 503; | 49 static const int SERVICE_UNAVAILABLE = 503; |
50 static const int GATEWAY_TIMEOUT = 504; | 50 static const int GATEWAY_TIMEOUT = 504; |
51 static const int HTTP_VERSION_NOT_SUPPORTED = 505; | 51 static const int HTTP_VERSION_NOT_SUPPORTED = 505; |
52 // Client generated status code. | 52 // Client generated status code. |
53 static const int NETWORK_CONNECT_TIMEOUT_ERROR = 599; | 53 static const int NETWORK_CONNECT_TIMEOUT_ERROR = 599; |
54 } | 54 } |
55 | 55 |
56 | 56 |
57 /** | 57 /** |
58 * A server that delivers content, such as web pages, using | 58 * A server that delivers content, such as web pages, using the HTTP protocol. |
59 * the HTTP protocol. | |
60 * | 59 * |
61 * The [HttpServer] is a [Stream] of [HttpRequest]s. Each | 60 * The HttpServer is a [Stream] that provides [HttpRequest] objects. Each |
62 * [HttpRequest] has an associated [HttpResponse] object as its | 61 * HttpRequest has an associated [HttpResponse] object. |
63 * [HttpRequest.response] member, and the server responds to a request by | 62 * The server responds to a request by writing to that HttpResponse object. |
64 * writing to that [HttpResponse] object. | 63 * The following example shows how to bind an HttpServer to an IPv6 |
64 * [InternetAddress] on port 80 (the standard port for HTTP servers) | |
65 * and how to listen for requests. | |
66 * Port 80 is the default HTTP port. However, on most systems accessing | |
67 * this requires super-user privileges. For local testing consider | |
68 * using a non-reserved port (above 1023). | |
Anders Johnsen
2014/03/27 12:29:36
1023 is a funny number :) Maybe say 1024 and above
mem
2014/03/28 17:52:00
Done.
| |
65 * | 69 * |
66 * Incomplete requests where all or parts of the header is missing, are | 70 * import 'dart:io'; |
67 * ignored and no exceptions or [HttpRequest] objects are generated for them. | 71 * |
68 * Likewise, when writing to a [HttpResponse], any [Socket] exceptions are | 72 * main() { |
73 * HttpServer | |
74 * .bind(InternetAddress.ANY_IP_V6, 80) | |
75 * .then((server) { | |
76 * server.listen((HttpRequest request) { | |
77 * request.response.write('Hello, world!'); | |
78 * request.response.close(); | |
79 * }); | |
80 * }); | |
81 * } | |
82 * | |
83 * Incomplete requests, in which all or part of the header is missing, are | |
84 * ignored, and no exceptions or HttpRequest objects are generated for them. | |
85 * Likewise, when writing to an HttpResponse, any [Socket] exceptions are | |
69 * ignored and any future writes are ignored. | 86 * ignored and any future writes are ignored. |
70 * | 87 * |
71 * The [HttpRequest] exposes the request headers, and provides the request body, | 88 * The HttpRequest exposes the request headers and provides the request body, |
72 * if it exists, as a stream of data. If the body is unread, it'll be drained | 89 * if it exists, as a Stream of data. If the body is unread, it is drained |
73 * when the [HttpResponse] is being written to or closed. | 90 * when the server writes to the HttpResponse or closes it. |
74 * | 91 * |
75 * The following example shows how to bind a [HttpServer] to a IPv6 | 92 * ## Bind with a secure HTTPS connection |
76 * [InternetAddress] on port 80, and listening to requests. | |
77 * | 93 * |
78 * HttpServer.bind(InternetAddress.ANY_IP_V6, 80).then((server) { | 94 * Use [bindSecure] to create an HTTPS server. |
79 * server.listen((HttpRequest request) { | 95 * |
80 * // Handle requests. | 96 * The server presents a certificate to the client. In the following |
81 * }); | 97 * example, the certificate is named `localhost_cert` and comes from |
82 * }); | 98 * the database found in the `pkcert` directory |
Anders Johnsen
2014/03/27 12:29:36
. after directory.
mem
2014/03/28 17:52:00
Done.
| |
99 * This certificate is used by Dart for testing purposes. | |
Anders Johnsen
2014/03/27 12:29:36
I don't think we need this line. It should be obvi
mem
2014/03/28 17:52:00
Done.
| |
100 * | |
101 * import 'dart:io'; | |
102 * import "dart:isolate"; | |
103 * | |
104 * main() { | |
105 * var testPkcertDatabase = Platform.script.resolve('pkcert') | |
106 * .toFilePath(); | |
107 * SecureSocket.initialize(database: testPkcertDatabase, | |
108 * password: 'dartdart'); | |
109 * | |
110 * HttpServer | |
111 * .bindSecure(InternetAddress.ANY_IP_V6, | |
112 * 443, | |
113 * certificateName: 'localhost_cert') | |
114 * .then((server) { | |
115 * server.listen((HttpRequest request) { | |
116 * request.response.write('Hello, world!'); | |
117 * request.response.close(); | |
118 * }); | |
119 * }); | |
120 * } | |
121 * | |
122 * The certificate database is managed using the Mozilla certutil tool (see | |
123 * [NSS Tools certutil](https://developer.mozilla.org/en-US/docs/NSS/tools/NSS_T ools_certutil)). | |
124 * Dart uses the NSS library to handle SSL and Mozilla | |
Anders Johnsen
2014/03/27 12:29:36
I'm reading the 'and' wrong here. Would this be be
mem
2014/03/28 17:52:00
Done.
| |
125 * certutil must be used to manipulate the certificate database. | |
126 * | |
127 * ## Connect to a socket | |
Anders Johnsen
2014/03/27 12:29:36
a server socket
mem
2014/03/28 17:52:00
Done.
| |
128 * | |
129 * You can use the [listenOn] constructor to attach an HTTP server to | |
130 * a [ServerSocket]. | |
131 * | |
132 * import 'dart:io'; | |
133 * | |
134 * main() { | |
135 * ServerSocket.bind(InternetAddress.ANY_IP_V6, 80) | |
136 * .then((serverSocket) { | |
137 * HttpServer httpserver = new HttpServer.listenOn(serverSocket); | |
138 * serverSocket.listen((Socket socket) { | |
139 * socket.write('Hello, client.'); | |
140 * }); | |
141 * }); | |
142 * } | |
143 * | |
144 * ## Other resources | |
145 * | |
146 * * HttpServer is a Stream. Refer to the [Stream] class for information | |
147 * about the streaming qualities of an HttpServer. | |
Anders Johnsen
2014/03/27 12:29:36
We could consider to specify, that pausing the sub
mem
2014/03/28 17:52:00
Done.
| |
148 * | |
149 * * The [http_server](https://pub.dartlang.org/packages/http_server) | |
150 * package on pub.dartlang.org contains a set of high-level classes that, | |
151 * together with this class, makes it easy to provide content through HTTP | |
152 * servers. | |
83 */ | 153 */ |
84 abstract class HttpServer implements Stream<HttpRequest> { | 154 abstract class HttpServer implements Stream<HttpRequest> { |
85 /** | 155 /** |
86 * Set and get the default value of the `Server` header for all responses | 156 * Set and get the default value of the `Server` header for all responses |
87 * generated by this [HttpServer]. By default, it's disabled. | 157 * generated by this [HttpServer]. By default, it's disabled. |
88 * | 158 * |
89 * If the serverHeader is set to `null`, no default `Server` header will be | 159 * If the serverHeader is set to `null`, no default `Server` header will be |
90 * added to each response. | 160 * added to each response. |
91 */ | 161 */ |
92 String serverHeader; | 162 String serverHeader; |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
254 /** | 324 /** |
255 * Number of connections which are preparing to close. Note: These | 325 * Number of connections which are preparing to close. Note: These |
256 * connections are also part of the [:active:] count as they might | 326 * connections are also part of the [:active:] count as they might |
257 * still be sending data to the client before finally closing. | 327 * still be sending data to the client before finally closing. |
258 */ | 328 */ |
259 int closing = 0; | 329 int closing = 0; |
260 } | 330 } |
261 | 331 |
262 | 332 |
263 /** | 333 /** |
264 * Access to the HTTP headers for requests and responses. In some | 334 * Headers for HTTP requests and responses. |
265 * situations the headers will be immutable and the mutating methods | 335 * |
266 * will then throw exceptions. | 336 * In some situations the headers are immutable, |
Søren Gjesse
2014/03/27 10:09:55
We could go into the details on when headers are i
mem
2014/03/28 17:52:00
Done.
| |
337 * in which case the mutating methods throw exceptions. | |
267 * | 338 * |
268 * For all operations on HTTP headers the header name is | 339 * For all operations on HTTP headers the header name is |
269 * case-insensitive. | 340 * case-insensitive. |
341 * | |
342 * To set the value of a header use the `set()` method: | |
343 * | |
344 * request.headers.set(HttpHeaders.CACHE_CONTROL, | |
345 'max-age=3600, must-revalidate'); | |
346 * | |
347 * To retrieve the value of a header use the `value()` method: | |
348 * | |
349 * print(request.headers.value(HttpHeaders.USER_AGENT)); | |
350 * | |
351 * An HttpHeaders object holds a list of values for each name | |
352 * as the standard allows. In most cases a name holds only a single value, | |
353 * The most common mode of operation is to use `set()` for setting a value, | |
354 * and `value()` for retrieving a value. | |
270 */ | 355 */ |
271 abstract class HttpHeaders { | 356 abstract class HttpHeaders { |
272 static const ACCEPT = "accept"; | 357 static const ACCEPT = "accept"; |
273 static const ACCEPT_CHARSET = "accept-charset"; | 358 static const ACCEPT_CHARSET = "accept-charset"; |
274 static const ACCEPT_ENCODING = "accept-encoding"; | 359 static const ACCEPT_ENCODING = "accept-encoding"; |
275 static const ACCEPT_LANGUAGE = "accept-language"; | 360 static const ACCEPT_LANGUAGE = "accept-language"; |
276 static const ACCEPT_RANGES = "accept-ranges"; | 361 static const ACCEPT_RANGES = "accept-ranges"; |
277 static const AGE = "age"; | 362 static const AGE = "age"; |
278 static const ALLOW = "allow"; | 363 static const ALLOW = "allow"; |
279 static const AUTHORIZATION = "authorization"; | 364 static const AUTHORIZATION = "authorization"; |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
752 * which listens for HTTP requests on a specific host and port. | 837 * which listens for HTTP requests on a specific host and port. |
753 * For each request received, the HttpServer, which is a [Stream], | 838 * For each request received, the HttpServer, which is a [Stream], |
754 * generates an `HttpRequest` object and adds it to the stream. | 839 * generates an `HttpRequest` object and adds it to the stream. |
755 * | 840 * |
756 * An `HttpRequest` object delivers the body content of the request | 841 * An `HttpRequest` object delivers the body content of the request |
757 * as a stream of byte lists. | 842 * as a stream of byte lists. |
758 * The object also contains information about the request, | 843 * The object also contains information about the request, |
759 * such as the method, URI, and headers. | 844 * such as the method, URI, and headers. |
760 * | 845 * |
761 * In the following code, an HttpServer listens | 846 * In the following code, an HttpServer listens |
762 * for HTTP requests and, within the callback function, | 847 * for HTTP requests. When the server receives a request, |
763 * uses the `HttpRequest` object's `method` property to dispatch requests. | 848 * it uses the HttpRequest object's `method` property to dispatch requests. |
764 * | 849 * |
765 * final HOST = InternetAddress.LOOPBACK_IP_V4; | 850 * final HOST = InternetAddress.LOOPBACK_IP_V4; |
766 * final PORT = 4040; | 851 * final PORT = 80; |
767 * | 852 * |
768 * HttpServer.bind(HOST, PORT).then((_server) { | 853 * HttpServer.bind(HOST, PORT).then((_server) { |
769 * _server.listen((HttpRequest request) { | 854 * _server.listen((HttpRequest request) { |
770 * switch (request.method) { | 855 * switch (request.method) { |
771 * case 'GET': | 856 * case 'GET': |
772 * handleGetRequest(request); | 857 * handleGetRequest(request); |
773 * break; | 858 * break; |
774 * case 'POST': | 859 * case 'POST': |
775 * ... | 860 * ... |
776 * } | 861 * } |
777 * }, | 862 * }, |
778 * onError: handleError); // listen() failed. | 863 * onError: handleError); // listen() failed. |
779 * }).catchError(handleError); | 864 * }).catchError(handleError); |
780 * | 865 * |
781 * Listen to the `HttpRequest` stream to handle the | 866 * An HttpRequest object provides access to the associated [HttpResponse] |
782 * data and be notified once the entire body is received. | 867 * object through the response property. |
783 * An `HttpRequest` object contains an [HttpResponse] object, | 868 * The server writes its response to the body of the HttpResponse object. |
784 * to which the server can write its response. | 869 * For example, here's a function that responds to a request: |
785 * For example, here's a skeletal callback function | |
786 * that responds to a request: | |
787 * | 870 * |
788 * void handleGetRequest(HttpRequest req) { | 871 * void handleGetRequest(HttpRequest req) { |
789 * HttpResponse res = req.response; | 872 * HttpResponse res = req.response; |
790 * var body = []; | 873 * var body = []; |
791 * req.listen((List<int> buffer) => body.add(buffer), | 874 * req.listen((List<int> buffer) => body.add(buffer), |
792 * onDone: () { | 875 * onDone: () { |
793 * res.write('Received ${body.length} for request '); | 876 * res.write('Received ${body.length} for request '); |
794 * res.write(' ${req.method}: ${req.uri.path}'); | 877 * res.write(' ${req.method}: ${req.uri.path}'); |
795 * res.close(); | 878 * res.close(); |
796 * }, | 879 * }, |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
886 * | 969 * |
887 * If the [contentLength] of the body isn't 0, and the body isn't being read, | 970 * If the [contentLength] of the body isn't 0, and the body isn't being read, |
888 * any write calls on the [HttpResponse] automatically drain the request | 971 * any write calls on the [HttpResponse] automatically drain the request |
889 * body. | 972 * body. |
890 */ | 973 */ |
891 HttpResponse get response; | 974 HttpResponse get response; |
892 } | 975 } |
893 | 976 |
894 | 977 |
895 /** | 978 /** |
896 * An [HttpResponse] represents the headers and data to be returned to | 979 * An [HttpResponse] contains the headers and data returned |
Anders Johnsen
2014/03/27 12:29:36
contains is a dangerous words, as it makes it soun
mem
2014/03/28 17:52:00
Done.
| |
897 * a client in response to an HTTP request. | 980 * from the server to the client in response to an HTTP request. |
898 * | 981 * |
899 * This object has a number of properties for setting up the HTTP | 982 * Every HttpRequest object provides access to the associated [HttpResponse] |
900 * header of the response. When the header has been set up the methods | 983 * object through the response property. |
901 * from the [IOSink] can be used to write the actual body of the HTTP | 984 * The server sends its response to the client by writing to the |
902 * response. When one of the [IOSink] methods is used for the | 985 * body of the HttpResponse object. |
Anders Johnsen
2014/03/27 12:29:36
there is no 'body' object. maybe just say
wri
mem
2014/03/28 17:52:00
Done.
| |
903 * first time the request header is send. Calling any methods that | |
904 * will change the header after it is sent will throw an exception. | |
905 * | 986 * |
906 * When writing string data through the [IOSink] the encoding used | 987 * The HttpResponse object has a number of properties for setting up the HTTP |
907 * will be determined from the "charset" parameter of the | 988 * header of the response. This class implements [IOSink]. |
Anders Johnsen
2014/03/27 12:29:36
headers
Anders Johnsen
2014/03/27 12:29:36
Maybe move IOSink to the paragraph above, as it's
mem
2014/03/28 17:52:00
Done.
mem
2014/03/28 17:52:00
Done.
| |
989 * When the header has been set up, the methods | |
990 * from IOSink, such as `writeln()`, can be used to write | |
991 * the body of the HTTP response. | |
992 * Use the `close()` method to close the response and send it to the client. | |
993 * | |
994 * server.listen((HttpRequest request) { | |
995 * request.response.write('Hello, world!'); | |
996 * request.response.close(); | |
997 * }); | |
998 * | |
999 * When one of the IOSink methods is used for the | |
1000 * first time, the request header is sent. Calling any methods that | |
1001 * change the header after it is sent throws an exception. | |
1002 | |
1003 * When writing string data through the IOSink, the encoding used | |
1004 * is determined from the "charset" parameter of the | |
908 * "Content-Type" header. | 1005 * "Content-Type" header. |
909 * | 1006 * |
910 * HttpResponse response = ... | 1007 * HttpResponse response = ... |
911 * response.headers.contentType | 1008 * response.headers.contentType |
912 * = new ContentType("application", "json", charset: "utf-8"); | 1009 * = new ContentType("application", "json", charset: "utf-8"); |
913 * response.write(...); // Strings written will be UTF-8 encoded. | 1010 * response.write(...); // Strings written will be UTF-8 encoded. |
914 * | 1011 * |
915 * If no charset is provided the default of ISO-8859-1 (Latin 1) will | 1012 * If no charset is provided the default of ISO-8859-1 (Latin 1) will |
916 * be used. | 1013 * be used. |
917 * | 1014 * |
918 * HttpResponse response = ... | 1015 * HttpResponse response = ... |
919 * response.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); | 1016 * response.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); |
920 * response.write(...); // Strings written will be ISO-8859-1 encoded. | 1017 * response.write(...); // Strings written will be ISO-8859-1 encoded. |
921 * | 1018 * |
922 * If an unsupported encoding is used an exception will be thrown if | 1019 * An exception is thrown if you use an unsupported encoding and the |
923 * using one of the write methods taking a string. | 1020 * `write()` method being used takes a string parameter. |
Anders Johnsen
2014/03/27 12:29:36
This sentence is weird. write can only be called w
mem
2014/03/28 17:52:00
Done.
mem
2014/03/28 17:52:00
Done.
| |
924 */ | 1021 */ |
925 abstract class HttpResponse implements IOSink { | 1022 abstract class HttpResponse implements IOSink { |
926 // TODO(ajohnsen): Add documentation of how to pipe a file to the response. | 1023 // TODO(ajohnsen): Add documentation of how to pipe a file to the response. |
927 /** | 1024 /** |
928 * Gets and sets the content length of the response. If the size of | 1025 * Gets and sets the content length of the response. If the size of |
929 * the response is not known in advance set the content length to | 1026 * the response is not known in advance set the content length to |
930 * -1 - which is also the default if not set. | 1027 * -1 - which is also the default if not set. |
931 */ | 1028 */ |
932 int contentLength; | 1029 int contentLength; |
933 | 1030 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1004 * socket is not available. | 1101 * socket is not available. |
1005 */ | 1102 */ |
1006 HttpConnectionInfo get connectionInfo; | 1103 HttpConnectionInfo get connectionInfo; |
1007 } | 1104 } |
1008 | 1105 |
1009 | 1106 |
1010 /** | 1107 /** |
1011 * A client that receives content, such as web pages, from | 1108 * A client that receives content, such as web pages, from |
1012 * a server using the HTTP protocol. | 1109 * a server using the HTTP protocol. |
1013 * | 1110 * |
1014 * HttpClient contains a number of methods to send a HTTP request | 1111 * HttpClient contains a number of methods to send an [HttpClientRequest] |
1015 * to a HTTP server and receive a HTTP response back. | 1112 * to an Http server and receive an [HttpClientResponse] back. |
1113 * For example, you can use the [get], [getUrl], [post], and [postUrl] methods | |
1114 * for GET and POST requests, respectively. | |
1016 * | 1115 * |
1017 * This is a two-step process, triggered by two futures. When the | 1116 * ## Making a simple GET request: an example |
1018 * first future completes with a [HttpClientRequest] the underlying | |
1019 * network connection has been established, but no data has yet been | |
1020 * sent. The HTTP headers and body can be set on the request, and | |
1021 * [:close:] is called to sent it to the server. | |
1022 * | 1117 * |
1023 * The second future, which is returned by [:close:], completes with | 1118 * A `getUrl` request is a two-step process, triggered by two [Future]s. |
1024 * an [HttpClientResponse] object when the response is received from | 1119 * When the first future completes with a [HttpClientRequest], the underlying |
1025 * the server. This object contains the headers and body of the | 1120 * network connection has been established, but no data has been sent. |
1026 * response. | 1121 * In the callback function for the first future, the HTTP headers and body |
1027 | 1122 * can be set on the request. A call to the [close] method sends the |
1028 * The future for [HttpClientRequest] is created by methods such as | 1123 * request to the server. |
Anders Johnsen
2014/03/27 12:29:36
We send data as soon as the first either write or
mem
2014/03/28 17:52:00
Done.
| |
1029 * [getUrl] and [open]. | |
1030 * | 1124 * |
1031 * When the HTTP response is ready a [HttpClientResponse] object is | 1125 * When the HTTP response is received from the server, |
1032 * provided which provides access to the headers and body of the response. The | 1126 * the second future, which is returned by close, |
1033 * body is available as a stream implemented by [HttpClientResponse]. | 1127 * completes with an [HttpClientResponse] object. |
1034 * If a body is present, it must be read. Otherwise, it'll lead to a resource | 1128 * This object provides access to the headers and body of the response. |
1129 * The body is available as a stream implemented by HttpClientResponse. | |
1130 * If a body is present, it must be read. Otherwise, it leads to resource | |
1035 * leaks. Consider using [HttpClientResponse.drain] if the body is unused. | 1131 * leaks. Consider using [HttpClientResponse.drain] if the body is unused. |
1036 * | 1132 * |
1037 * HttpClient client = new HttpClient(); | 1133 * HttpClient client = new HttpClient(); |
1038 * client.getUrl(Uri.parse("http://www.example.com/")) | 1134 * client.getUrl(Uri.parse("http://www.example.com/")) |
1039 * .then((HttpClientRequest request) { | 1135 * .then((HttpClientRequest request) { |
1040 * // Prepare the request then call close on it to send it. | 1136 * // Prepare the request, then call close to send it. |
Søren Gjesse
2014/03/27 10:09:55
Maybe we should say what we mean with 'prepare the
mem
2014/03/28 17:52:00
Done.
| |
1137 * ... | |
1041 * return request.close(); | 1138 * return request.close(); |
1042 * }) | 1139 * }) |
1043 * .then((HttpClientResponse response) { | 1140 * .then((HttpClientResponse response) { |
1044 * // Process the response. | 1141 * // Process the response. |
1142 * ... | |
1045 * }); | 1143 * }); |
1046 * | 1144 * |
1047 * All [HttpClient] requests set the following header by default: | 1145 * The future for [HttpClientRequest] is created by methods such as |
1146 * [getUrl] and [open]. | |
1147 * | |
1148 * ## Headers | |
1149 * | |
1150 * All HttpClient requests set the following header by default: | |
1048 * | 1151 * |
1049 * Accept-Encoding: gzip | 1152 * Accept-Encoding: gzip |
1050 * | 1153 * |
1051 * This allows the HTTP server to use gzip compression for the body if | 1154 * This allows the HTTP server to use gzip compression for the body if |
1052 * possible. If this behavior is not desired set the | 1155 * possible. If this behavior is not desired set the |
1053 * "Accept-Encoding" header to something else. | 1156 * `Accept-Encoding` header to something else. |
Søren Gjesse
2014/03/27 10:09:55
Add:
To just turn off gzip compression of the res
mem
2014/03/28 17:52:00
Done.
| |
1054 * | 1157 * |
1055 * The [HttpClient] supports persistent connections and caches network | 1158 * ## Closing the connection |
Søren Gjesse
2014/03/27 10:09:55
connection -> HttpClient
mem
2014/03/28 17:52:00
Done.
| |
1056 * connections to reuse then for multiple requests whenever | 1159 * |
1160 * The HttpClient supports persistent connections and caches network | |
1161 * connections to reuse them for multiple requests whenever | |
1057 * possible. This means that network connections can be kept open for | 1162 * possible. This means that network connections can be kept open for |
1058 * some time after a request have completed. Use [:HttpClient.close:] | 1163 * some time after a request has completed. Use HttpClient.close |
1059 * to force shut down the [HttpClient] object and close the idle | 1164 * to force the HttpClient object to shut down and to close the idle |
1060 * network connections. | 1165 * network connections. |
1061 * | 1166 * |
1167 * ## Turning proxies on and off | |
1168 * | |
1062 * By default the HttpClient uses the proxy configuration available | 1169 * By default the HttpClient uses the proxy configuration available |
1063 * from the environment, see [findProxyFromEnvironment]. To turn off | 1170 * from the environment, see [findProxyFromEnvironment]. To turn off |
1064 * the use of proxies all together set the [findProxy] property to | 1171 * the use of proxies set the [findProxy] property to |
1065 * [:null:]. | 1172 * [:null:]. |
1066 * | 1173 * |
1067 * HttpClient client = new HttpClient(); | 1174 * HttpClient client = new HttpClient(); |
1068 * client.findProxy = null; | 1175 * client.findProxy = null; |
1069 */ | 1176 */ |
1070 abstract class HttpClient { | 1177 abstract class HttpClient { |
1071 static const int DEFAULT_HTTP_PORT = 80; | 1178 static const int DEFAULT_HTTP_PORT = 80; |
1072 static const int DEFAULT_HTTPS_PORT = 443; | 1179 static const int DEFAULT_HTTPS_PORT = 443; |
1073 | 1180 |
1074 /** | 1181 /** |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1339 * trying to establish a new connection after calling [shutdown] | 1446 * trying to establish a new connection after calling [shutdown] |
1340 * will throw an exception. | 1447 * will throw an exception. |
1341 */ | 1448 */ |
1342 void close({bool force: false}); | 1449 void close({bool force: false}); |
1343 } | 1450 } |
1344 | 1451 |
1345 | 1452 |
1346 /** | 1453 /** |
1347 * HTTP request for a client connection. | 1454 * HTTP request for a client connection. |
1348 * | 1455 * |
1349 * This object has a number of properties for setting up the HTTP | 1456 * To set up a request, set the headers using the headers property |
1350 * header of the request. When the header has been set up the methods | 1457 * provided in this class and write the data to the body of the request. |
1351 * from the [IOSink] can be used to write the actual body of the HTTP | 1458 * HttpClientRequest is an [IOSink]. Use the methods from IOSink, |
1352 * request. When one of the [IOSink] methods is used for the first | 1459 * such as writeCharCode(), to write the body of the HTTP |
1353 * time the request header is send. Calling any methods that will | 1460 * request. When one of the IOSink methods is used for the first |
1354 * change the header after it is sent will throw an exception. | 1461 * time, the request header is sent. Calling any methods that |
1462 * change the header after it is sent throws an exception. | |
1355 * | 1463 * |
1356 * When writing string data through the [IOSink] the | 1464 * When writing string data through the [IOSink] the |
1357 * encoding used will be determined from the "charset" parameter of | 1465 * encoding used is determined from the "charset" parameter of |
1358 * the "Content-Type" header. | 1466 * the "Content-Type" header. |
1359 * | 1467 * |
1360 * HttpClientRequest request = ... | 1468 * HttpClientRequest request = ... |
1361 * request.headers.contentType | 1469 * request.headers.contentType |
1362 * = new ContentType("application", "json", charset: "utf-8"); | 1470 * = new ContentType("application", "json", charset: "utf-8"); |
1363 * request.write(...); // Strings written will be UTF-8 encoded. | 1471 * request.write(...); // Strings written will be UTF-8 encoded. |
1364 * | 1472 * |
1365 * If no charset is provided the default of ISO-8859-1 (Latin 1) will | 1473 * If no charset is provided the default of ISO-8859-1 (Latin 1) is |
1366 * be used. | 1474 * be used. |
1367 * | 1475 * |
1368 * HttpClientRequest request = ... | 1476 * HttpClientRequest request = ... |
1369 * request.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); | 1477 * request.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); |
1370 * request.write(...); // Strings written will be ISO-8859-1 encoded. | 1478 * request.write(...); // Strings written will be ISO-8859-1 encoded. |
1371 * | 1479 * |
1372 * If an unsupported encoding is used an exception will be thrown if | 1480 * An exception is thrown if you use an unsupported encoding and the |
1373 * using one of the write methods taking a string. | 1481 * `write()` method being used takes a string parameter. |
1374 */ | 1482 */ |
1375 abstract class HttpClientRequest implements IOSink { | 1483 abstract class HttpClientRequest implements IOSink { |
1376 /** | 1484 /** |
1377 * Gets and sets the requested persistent connection state. | 1485 * Gets and sets the requested persistent connection state. |
1378 * | 1486 * |
1379 * The default value is [:true:]. | 1487 * The default value is [:true:]. |
1380 */ | 1488 */ |
1381 bool persistentConnection; | 1489 bool persistentConnection; |
1382 | 1490 |
1383 /** | 1491 /** |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1450 | 1558 |
1451 /** | 1559 /** |
1452 * Get information about the client connection. Returns [:null:] if the socket | 1560 * Get information about the client connection. Returns [:null:] if the socket |
1453 * is not available. | 1561 * is not available. |
1454 */ | 1562 */ |
1455 HttpConnectionInfo get connectionInfo; | 1563 HttpConnectionInfo get connectionInfo; |
1456 } | 1564 } |
1457 | 1565 |
1458 | 1566 |
1459 /** | 1567 /** |
1460 * HTTP response for a client connection. The [HttpClientResponse] is a | 1568 * HTTP response for a client connection. |
1461 * [Stream] of the body content of the response. Listen to the body to handle | 1569 |
1462 * the data and be notified once the entire body is received. | 1570 * The body of a [HttpClientResponse] object is a |
1571 * [Stream] of data from the server. Listen to the body to handle | |
1572 * the data and be notified when the entire body is received. | |
1573 * | |
1574 * new HttpClient().get('localhost', 80, '/file.txt') | |
1575 * .then((HttpClientRequeset request) => request.close()) | |
1576 * .then((HttpClientResponse response) { | |
1577 * response.transform(UTF8.decoder).listen((contents) { | |
1578 * // handle data | |
1579 * }); | |
1580 * }); | |
1463 */ | 1581 */ |
1464 abstract class HttpClientResponse implements Stream<List<int>> { | 1582 abstract class HttpClientResponse implements Stream<List<int>> { |
1465 /** | 1583 /** |
1466 * Returns the status code. | 1584 * Returns the status code. |
1467 */ | 1585 */ |
1468 int get statusCode; | 1586 int get statusCode; |
1469 | 1587 |
1470 /** | 1588 /** |
1471 * Returns the reason phrase associated with the status code. | 1589 * Returns the reason phrase associated with the status code. |
1472 */ | 1590 */ |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1643 class RedirectException implements HttpException { | 1761 class RedirectException implements HttpException { |
1644 final String message; | 1762 final String message; |
1645 final List<RedirectInfo> redirects; | 1763 final List<RedirectInfo> redirects; |
1646 | 1764 |
1647 const RedirectException(this.message, this.redirects); | 1765 const RedirectException(this.message, this.redirects); |
1648 | 1766 |
1649 String toString() => "RedirectException: $message"; | 1767 String toString() => "RedirectException: $message"; |
1650 | 1768 |
1651 Uri get uri => redirects.last.location; | 1769 Uri get uri => redirects.last.location; |
1652 } | 1770 } |
OLD | NEW |