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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 * ignored and any future writes are ignored. | 86 * ignored and any future writes are ignored. |
87 * | 87 * |
88 * The HttpRequest exposes the request headers and provides the request body, | 88 * The HttpRequest exposes the request headers and provides the request body, |
89 * if it exists, as a Stream of data. If the body is unread, it is drained | 89 * if it exists, as a Stream of data. If the body is unread, it is drained |
90 * when the server writes to the HttpResponse or closes it. | 90 * when the server writes to the HttpResponse or closes it. |
91 * | 91 * |
92 * ## Bind with a secure HTTPS connection | 92 * ## Bind with a secure HTTPS connection |
93 * | 93 * |
94 * Use [bindSecure] to create an HTTPS server. | 94 * Use [bindSecure] to create an HTTPS server. |
95 * | 95 * |
96 * The server presents a certificate to the client. In the following | 96 * The server presents a certificate to the client. The certificate |
97 * example, the certificate is named `localhost_cert` and comes from | 97 * chain and the private key are set in the SecurityContext |
98 * the database found in the `pkcert` directory. | 98 * object that is passed to [bindSecure]. |
99 * | 99 * |
100 * import 'dart:io'; | 100 * import 'dart:io'; |
101 * import "dart:isolate"; | 101 * import "dart:isolate"; |
102 * | 102 * |
103 * main() { | 103 * main() { |
104 * var testPkcertDatabase = Platform.script.resolve('pkcert') | 104 * SecurityContext context = new SecurityContext(); |
105 * .toFilePath(); | 105 * var chain = |
106 * SecureSocket.initialize(database: testPkcertDatabase, | 106 * Platform.script.resolve('certificates/server_chain.pem') |
107 * password: 'dartdart'); | 107 * .toFilePath(); |
| 108 * var key = |
| 109 * Platform.script.resolve('certificates/server_key.pem') |
| 110 * .toFilePath(); |
| 111 * context.useCertificateChain(chain); |
| 112 * context.usePrivateKey(key, password: 'dartdart'); |
108 * | 113 * |
109 * HttpServer | 114 * HttpServer |
110 * .bindSecure(InternetAddress.ANY_IP_V6, | 115 * .bindSecure(InternetAddress.ANY_IP_V6, |
111 * 443, | 116 * 443, |
112 * certificateName: 'localhost_cert') | 117 * context) |
113 * .then((server) { | 118 * .then((server) { |
114 * server.listen((HttpRequest request) { | 119 * server.listen((HttpRequest request) { |
115 * request.response.write('Hello, world!'); | 120 * request.response.write('Hello, world!'); |
116 * request.response.close(); | 121 * request.response.close(); |
117 * }); | 122 * }); |
118 * }); | 123 * }); |
119 * } | 124 * } |
120 * | 125 * |
121 * The certificate database is managed using the Mozilla certutil tool (see | 126 * The certificates and keys are pem files, which can be created and |
122 * [NSS Tools certutil](https://developer.mozilla.org/en-US/docs/NSS/tools/NSS_T
ools_certutil)). | 127 * managed with the tools in OpenSSL and BoringSSL. |
123 * Dart uses the NSS library to handle SSL, and the Mozilla certutil | |
124 * must be used to manipulate the certificate database. | |
125 * | 128 * |
126 * ## Connect to a server socket | 129 * ## Connect to a server socket |
127 * | 130 * |
128 * You can use the [listenOn] constructor to attach an HTTP server to | 131 * You can use the [listenOn] constructor to attach an HTTP server to |
129 * a [ServerSocket]. | 132 * a [ServerSocket]. |
130 * | 133 * |
131 * import 'dart:io'; | 134 * import 'dart:io'; |
132 * | 135 * |
133 * main() { | 136 * main() { |
134 * ServerSocket.bind(InternetAddress.ANY_IP_V6, 80) | 137 * ServerSocket.bind(InternetAddress.ANY_IP_V6, 80) |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 * to the same `address`, `port` and `v6Only` combination is | 287 * to the same `address`, `port` and `v6Only` combination is |
285 * possible from the same Dart process. If `shared` is `true` and | 288 * possible from the same Dart process. If `shared` is `true` and |
286 * additional binds are performed, then the incoming connections | 289 * additional binds are performed, then the incoming connections |
287 * will be distributed between that set of `HttpServer`s. One way of | 290 * will be distributed between that set of `HttpServer`s. One way of |
288 * using this is to have number of isolates between which incoming | 291 * using this is to have number of isolates between which incoming |
289 * connections are distributed. | 292 * connections are distributed. |
290 */ | 293 */ |
291 | 294 |
292 static Future<HttpServer> bindSecure(address, | 295 static Future<HttpServer> bindSecure(address, |
293 int port, | 296 int port, |
| 297 SecurityContext context, |
294 {int backlog: 0, | 298 {int backlog: 0, |
295 bool v6Only: false, | 299 bool v6Only: false, |
296 String certificateName, | 300 String certificateName, |
297 bool requestClientCertificate: false, | 301 bool requestClientCertificate: false, |
298 bool shared: false}) | 302 bool shared: false}) |
299 => _HttpServer.bindSecure(address, | 303 => _HttpServer.bindSecure(address, |
300 port, | 304 port, |
| 305 context, |
301 backlog, | 306 backlog, |
302 v6Only, | 307 v6Only, |
303 certificateName, | 308 certificateName, |
304 requestClientCertificate, | 309 requestClientCertificate, |
305 shared); | 310 shared); |
306 | 311 |
307 /** | 312 /** |
308 * Attaches the HTTP server to an existing [ServerSocket]. When the | 313 * Attaches the HTTP server to an existing [ServerSocket]. When the |
309 * [HttpServer] is closed, the [HttpServer] will just detach itself, | 314 * [HttpServer] is closed, the [HttpServer] will just detach itself, |
310 * closing current connections but not closing [serverSocket]. | 315 * closing current connections but not closing [serverSocket]. |
(...skipping 1013 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1324 /** | 1329 /** |
1325 * Set and get the default value of the `User-Agent` header for all requests | 1330 * Set and get the default value of the `User-Agent` header for all requests |
1326 * generated by this [HttpClient]. The default value is | 1331 * generated by this [HttpClient]. The default value is |
1327 * `Dart/<version> (dart:io)`. | 1332 * `Dart/<version> (dart:io)`. |
1328 * | 1333 * |
1329 * If the userAgent is set to `null`, no default `User-Agent` header will be | 1334 * If the userAgent is set to `null`, no default `User-Agent` header will be |
1330 * added to each request. | 1335 * added to each request. |
1331 */ | 1336 */ |
1332 String userAgent; | 1337 String userAgent; |
1333 | 1338 |
1334 factory HttpClient() => new _HttpClient(); | 1339 factory HttpClient({SecurityContext context}) => new _HttpClient(context); |
1335 | 1340 |
1336 /** | 1341 /** |
1337 * Opens a HTTP connection. | 1342 * Opens a HTTP connection. |
1338 * | 1343 * |
1339 * The HTTP method to use is specified in [method], the server is | 1344 * The HTTP method to use is specified in [method], the server is |
1340 * specified using [host] and [port], and the path (including | 1345 * specified using [host] and [port], and the path (including |
1341 * a possible query) is specified using [path]. | 1346 * a possible query) is specified using [path]. |
1342 * The path may also contain a URI fragment, which will be ignored. | 1347 * The path may also contain a URI fragment, which will be ignored. |
1343 * | 1348 * |
1344 * The `Host` header for the request will be set to the value | 1349 * The `Host` header for the request will be set to the value |
(...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2007 class RedirectException implements HttpException { | 2012 class RedirectException implements HttpException { |
2008 final String message; | 2013 final String message; |
2009 final List<RedirectInfo> redirects; | 2014 final List<RedirectInfo> redirects; |
2010 | 2015 |
2011 const RedirectException(this.message, this.redirects); | 2016 const RedirectException(this.message, this.redirects); |
2012 | 2017 |
2013 String toString() => "RedirectException: $message"; | 2018 String toString() => "RedirectException: $message"; |
2014 | 2019 |
2015 Uri get uri => redirects.last.location; | 2020 Uri get uri => redirects.last.location; |
2016 } | 2021 } |
OLD | NEW |