| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart.io; | |
| 6 | |
| 7 /** | |
| 8 * HTTP status codes. | |
| 9 */ | |
| 10 abstract class HttpStatus { | |
| 11 static const int CONTINUE = 100; | |
| 12 static const int SWITCHING_PROTOCOLS = 101; | |
| 13 static const int OK = 200; | |
| 14 static const int CREATED = 201; | |
| 15 static const int ACCEPTED = 202; | |
| 16 static const int NON_AUTHORITATIVE_INFORMATION = 203; | |
| 17 static const int NO_CONTENT = 204; | |
| 18 static const int RESET_CONTENT = 205; | |
| 19 static const int PARTIAL_CONTENT = 206; | |
| 20 static const int MULTIPLE_CHOICES = 300; | |
| 21 static const int MOVED_PERMANENTLY = 301; | |
| 22 static const int FOUND = 302; | |
| 23 static const int MOVED_TEMPORARILY = 302; // Common alias for FOUND. | |
| 24 static const int SEE_OTHER = 303; | |
| 25 static const int NOT_MODIFIED = 304; | |
| 26 static const int USE_PROXY = 305; | |
| 27 static const int TEMPORARY_REDIRECT = 307; | |
| 28 static const int BAD_REQUEST = 400; | |
| 29 static const int UNAUTHORIZED = 401; | |
| 30 static const int PAYMENT_REQUIRED = 402; | |
| 31 static const int FORBIDDEN = 403; | |
| 32 static const int NOT_FOUND = 404; | |
| 33 static const int METHOD_NOT_ALLOWED = 405; | |
| 34 static const int NOT_ACCEPTABLE = 406; | |
| 35 static const int PROXY_AUTHENTICATION_REQUIRED = 407; | |
| 36 static const int REQUEST_TIMEOUT = 408; | |
| 37 static const int CONFLICT = 409; | |
| 38 static const int GONE = 410; | |
| 39 static const int LENGTH_REQUIRED = 411; | |
| 40 static const int PRECONDITION_FAILED = 412; | |
| 41 static const int REQUEST_ENTITY_TOO_LARGE = 413; | |
| 42 static const int REQUEST_URI_TOO_LONG = 414; | |
| 43 static const int UNSUPPORTED_MEDIA_TYPE = 415; | |
| 44 static const int REQUESTED_RANGE_NOT_SATISFIABLE = 416; | |
| 45 static const int EXPECTATION_FAILED = 417; | |
| 46 static const int INTERNAL_SERVER_ERROR = 500; | |
| 47 static const int NOT_IMPLEMENTED = 501; | |
| 48 static const int BAD_GATEWAY = 502; | |
| 49 static const int SERVICE_UNAVAILABLE = 503; | |
| 50 static const int GATEWAY_TIMEOUT = 504; | |
| 51 static const int HTTP_VERSION_NOT_SUPPORTED = 505; | |
| 52 // Client generated status code. | |
| 53 static const int NETWORK_CONNECT_TIMEOUT_ERROR = 599; | |
| 54 } | |
| 55 | |
| 56 | |
| 57 /** | |
| 58 * A server that delivers content, such as web pages, using the HTTP protocol. | |
| 59 * | |
| 60 * The HttpServer is a [Stream] that provides [HttpRequest] objects. Each | |
| 61 * HttpRequest has an associated [HttpResponse] object. | |
| 62 * The server responds to a request by 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 (1024 and above). | |
| 69 * | |
| 70 * import 'dart:io'; | |
| 71 * | |
| 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 | |
| 86 * ignored and any future writes are ignored. | |
| 87 * | |
| 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 | |
| 90 * when the server writes to the HttpResponse or closes it. | |
| 91 * | |
| 92 * ## Bind with a secure HTTPS connection | |
| 93 * | |
| 94 * Use [bindSecure] to create an HTTPS server. | |
| 95 * | |
| 96 * The server presents a certificate to the client. The certificate | |
| 97 * chain and the private key are set in the [SecurityContext] | |
| 98 * object that is passed to [bindSecure]. | |
| 99 * | |
| 100 * import 'dart:io'; | |
| 101 * import "dart:isolate"; | |
| 102 * | |
| 103 * main() { | |
| 104 * SecurityContext context = new SecurityContext(); | |
| 105 * var chain = | |
| 106 * Platform.script.resolve('certificates/server_chain.pem') | |
| 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'); | |
| 113 * | |
| 114 * HttpServer | |
| 115 * .bindSecure(InternetAddress.ANY_IP_V6, | |
| 116 * 443, | |
| 117 * context) | |
| 118 * .then((server) { | |
| 119 * server.listen((HttpRequest request) { | |
| 120 * request.response.write('Hello, world!'); | |
| 121 * request.response.close(); | |
| 122 * }); | |
| 123 * }); | |
| 124 * } | |
| 125 * | |
| 126 * The certificates and keys are PEM files, which can be created and | |
| 127 * managed with the tools in OpenSSL. | |
| 128 * | |
| 129 * ## Connect to a server socket | |
| 130 * | |
| 131 * You can use the [listenOn] constructor to attach an HTTP server to | |
| 132 * a [ServerSocket]. | |
| 133 * | |
| 134 * import 'dart:io'; | |
| 135 * | |
| 136 * main() { | |
| 137 * ServerSocket.bind(InternetAddress.ANY_IP_V6, 80) | |
| 138 * .then((serverSocket) { | |
| 139 * HttpServer httpserver = new HttpServer.listenOn(serverSocket); | |
| 140 * serverSocket.listen((Socket socket) { | |
| 141 * socket.write('Hello, client.'); | |
| 142 * }); | |
| 143 * }); | |
| 144 * } | |
| 145 * | |
| 146 * ## Other resources | |
| 147 * | |
| 148 * * HttpServer is a Stream. Refer to the [Stream] class for information | |
| 149 * about the streaming qualities of an HttpServer. | |
| 150 * Pausing the subscription of the stream, pauses at the OS level. | |
| 151 * | |
| 152 * * The [http_server](https://pub.dartlang.org/packages/http_server) | |
| 153 * package on pub.dartlang.org contains a set of high-level classes that, | |
| 154 * together with this class, makes it easy to provide content through HTTP | |
| 155 * servers. | |
| 156 */ | |
| 157 abstract class HttpServer implements Stream<HttpRequest> { | |
| 158 /** | |
| 159 * Get and set the default value of the `Server` header for all responses | |
| 160 * generated by this [HttpServer]. | |
| 161 * | |
| 162 * If [serverHeader] is `null`, no `Server` header will be added to each | |
| 163 * response. | |
| 164 * | |
| 165 * The default value is `null`. | |
| 166 */ | |
| 167 String serverHeader; | |
| 168 | |
| 169 /** | |
| 170 * Default set of headers added to all response objects. | |
| 171 * | |
| 172 * By default the following headers are in this set: | |
| 173 * | |
| 174 * Content-Type: text/plain; charset=utf-8 | |
| 175 * X-Frame-Options: SAMEORIGIN | |
| 176 * X-Content-Type-Options: nosniff | |
| 177 * X-XSS-Protection: 1; mode=block | |
| 178 * | |
| 179 * If the `Server` header is added here and the `serverHeader` is set as | |
| 180 * well then the value of `serverHeader` takes precedence. | |
| 181 */ | |
| 182 HttpHeaders get defaultResponseHeaders; | |
| 183 | |
| 184 /** | |
| 185 * Whether the [HttpServer] should compress the content, if possible. | |
| 186 * | |
| 187 * The content can only be compressed when the response is using | |
| 188 * chunked Transfer-Encoding and the incoming request has `gzip` | |
| 189 * as an accepted encoding in the Accept-Encoding header. | |
| 190 * | |
| 191 * The default value is `false` (compression disabled). | |
| 192 * To enable, set `autoCompress` to `true`. | |
| 193 */ | |
| 194 bool autoCompress; | |
| 195 | |
| 196 /** | |
| 197 * Get or set the timeout used for idle keep-alive connections. If no further | |
| 198 * request is seen within [idleTimeout] after the previous request was | |
| 199 * completed, the connection is dropped. | |
| 200 * | |
| 201 * Default is 120 seconds. | |
| 202 * | |
| 203 * Note that it may take up to `2 * idleTimeout` before a idle connection is | |
| 204 * aborted. | |
| 205 * | |
| 206 * To disable, set [idleTimeout] to `null`. | |
| 207 */ | |
| 208 Duration idleTimeout; | |
| 209 | |
| 210 | |
| 211 /** | |
| 212 * Starts listening for HTTP requests on the specified [address] and | |
| 213 * [port]. | |
| 214 * | |
| 215 * The [address] can either be a [String] or an | |
| 216 * [InternetAddress]. If [address] is a [String], [bind] will | |
| 217 * perform a [InternetAddress.lookup] and use the first value in the | |
| 218 * list. To listen on the loopback adapter, which will allow only | |
| 219 * incoming connections from the local host, use the value | |
| 220 * [InternetAddress.LOOPBACK_IP_V4] or | |
| 221 * [InternetAddress.LOOPBACK_IP_V6]. To allow for incoming | |
| 222 * connection from the network use either one of the values | |
| 223 * [InternetAddress.ANY_IP_V4] or [InternetAddress.ANY_IP_V6] to | |
| 224 * bind to all interfaces or the IP address of a specific interface. | |
| 225 * | |
| 226 * If an IP version 6 (IPv6) address is used, both IP version 6 | |
| 227 * (IPv6) and version 4 (IPv4) connections will be accepted. To | |
| 228 * restrict this to version 6 (IPv6) only, use [v6Only] to set | |
| 229 * version 6 only. | |
| 230 * | |
| 231 * If [port] has the value [:0:] an ephemeral port will be chosen by | |
| 232 * the system. The actual port used can be retrieved using the | |
| 233 * [port] getter. | |
| 234 * | |
| 235 * The optional argument [backlog] can be used to specify the listen | |
| 236 * backlog for the underlying OS listen setup. If [backlog] has the | |
| 237 * value of [:0:] (the default) a reasonable value will be chosen by | |
| 238 * the system. | |
| 239 * | |
| 240 * The optional argument [shared] specifies whether additional HttpServer | |
| 241 * objects can bind to the same combination of `address`, `port` and `v6Only`. | |
| 242 * If `shared` is `true` and more `HttpServer`s from this isolate or other | |
| 243 * isolates are bound to the port, then the incoming connections will be | |
| 244 * distributed among all the bound `HttpServer`s. Connections can be | |
| 245 * distributed over multiple isolates this way. | |
| 246 */ | |
| 247 static Future<HttpServer> bind(address, | |
| 248 int port, | |
| 249 {int backlog: 0, | |
| 250 bool v6Only: false, | |
| 251 bool shared: false}) | |
| 252 => _HttpServer.bind(address, port, backlog, v6Only, shared); | |
| 253 | |
| 254 /** | |
| 255 * The [address] can either be a [String] or an | |
| 256 * [InternetAddress]. If [address] is a [String], [bind] will | |
| 257 * perform a [InternetAddress.lookup] and use the first value in the | |
| 258 * list. To listen on the loopback adapter, which will allow only | |
| 259 * incoming connections from the local host, use the value | |
| 260 * [InternetAddress.LOOPBACK_IP_V4] or | |
| 261 * [InternetAddress.LOOPBACK_IP_V6]. To allow for incoming | |
| 262 * connection from the network use either one of the values | |
| 263 * [InternetAddress.ANY_IP_V4] or [InternetAddress.ANY_IP_V6] to | |
| 264 * bind to all interfaces or the IP address of a specific interface. | |
| 265 * | |
| 266 * If an IP version 6 (IPv6) address is used, both IP version 6 | |
| 267 * (IPv6) and version 4 (IPv4) connections will be accepted. To | |
| 268 * restrict this to version 6 (IPv6) only, use [v6Only] to set | |
| 269 * version 6 only. | |
| 270 * | |
| 271 * If [port] has the value [:0:] an ephemeral port will be chosen by | |
| 272 * the system. The actual port used can be retrieved using the | |
| 273 * [port] getter. | |
| 274 * | |
| 275 * The optional argument [backlog] can be used to specify the listen | |
| 276 * backlog for the underlying OS listen setup. If [backlog] has the | |
| 277 * value of [:0:] (the default) a reasonable value will be chosen by | |
| 278 * the system. | |
| 279 * | |
| 280 * If [requestClientCertificate] is true, the server will | |
| 281 * request clients to authenticate with a client certificate. | |
| 282 * The server will advertise the names of trusted issuers of client | |
| 283 * certificates, getting them from [context], where they have been | |
| 284 * set using [SecurityContext.setClientAuthorities]. | |
| 285 * | |
| 286 * The optional argument [shared] specifies whether additional HttpServer | |
| 287 * objects can bind to the same combination of `address`, `port` and `v6Only`. | |
| 288 * If `shared` is `true` and more `HttpServer`s from this isolate or other | |
| 289 * isolates are bound to the port, then the incoming connections will be | |
| 290 * distributed among all the bound `HttpServer`s. Connections can be | |
| 291 * distributed over multiple isolates this way. | |
| 292 */ | |
| 293 | |
| 294 static Future<HttpServer> bindSecure(address, | |
| 295 int port, | |
| 296 SecurityContext context, | |
| 297 {int backlog: 0, | |
| 298 bool v6Only: false, | |
| 299 bool requestClientCertificate: false, | |
| 300 bool shared: false}) | |
| 301 => _HttpServer.bindSecure(address, | |
| 302 port, | |
| 303 context, | |
| 304 backlog, | |
| 305 v6Only, | |
| 306 requestClientCertificate, | |
| 307 shared); | |
| 308 | |
| 309 /** | |
| 310 * Attaches the HTTP server to an existing [ServerSocket]. When the | |
| 311 * [HttpServer] is closed, the [HttpServer] will just detach itself, | |
| 312 * closing current connections but not closing [serverSocket]. | |
| 313 */ | |
| 314 factory HttpServer.listenOn(ServerSocket serverSocket) | |
| 315 => new _HttpServer.listenOn(serverSocket); | |
| 316 | |
| 317 /** | |
| 318 * Permanently stops this [HttpServer] from listening for new | |
| 319 * connections. This closes the [Stream] of [HttpRequest]s with a | |
| 320 * done event. The returned future completes when the server is | |
| 321 * stopped. For a server started using [bind] or [bindSecure] this | |
| 322 * means that the port listened on no longer in use. | |
| 323 * | |
| 324 * If [force] is `true`, active connections will be closed immediately. | |
| 325 */ | |
| 326 Future close({bool force: false}); | |
| 327 | |
| 328 /** | |
| 329 * Returns the port that the server is listening on. This can be | |
| 330 * used to get the actual port used when a value of 0 for [:port:] is | |
| 331 * specified in the [bind] or [bindSecure] call. | |
| 332 */ | |
| 333 int get port; | |
| 334 | |
| 335 /** | |
| 336 * Returns the address that the server is listening on. This can be | |
| 337 * used to get the actual address used, when the address is fetched by | |
| 338 * a lookup from a hostname. | |
| 339 */ | |
| 340 InternetAddress get address; | |
| 341 | |
| 342 /** | |
| 343 * Sets the timeout, in seconds, for sessions of this [HttpServer]. | |
| 344 * The default timeout is 20 minutes. | |
| 345 */ | |
| 346 set sessionTimeout(int timeout); | |
| 347 | |
| 348 /** | |
| 349 * Returns an [HttpConnectionsInfo] object summarizing the number of | |
| 350 * current connections handled by the server. | |
| 351 */ | |
| 352 HttpConnectionsInfo connectionsInfo(); | |
| 353 } | |
| 354 | |
| 355 | |
| 356 /** | |
| 357 * Summary statistics about an [HttpServer]s current socket connections. | |
| 358 */ | |
| 359 class HttpConnectionsInfo { | |
| 360 /** | |
| 361 * Total number of socket connections. | |
| 362 */ | |
| 363 int total = 0; | |
| 364 | |
| 365 /** | |
| 366 * Number of active connections where actual request/response | |
| 367 * processing is active. | |
| 368 */ | |
| 369 int active = 0; | |
| 370 | |
| 371 /** | |
| 372 * Number of idle connections held by clients as persistent connections. | |
| 373 */ | |
| 374 int idle = 0; | |
| 375 | |
| 376 /** | |
| 377 * Number of connections which are preparing to close. Note: These | |
| 378 * connections are also part of the [:active:] count as they might | |
| 379 * still be sending data to the client before finally closing. | |
| 380 */ | |
| 381 int closing = 0; | |
| 382 } | |
| 383 | |
| 384 | |
| 385 /** | |
| 386 * Headers for HTTP requests and responses. | |
| 387 * | |
| 388 * In some situations, headers are immutable: | |
| 389 * | |
| 390 * * HttpRequest and HttpClientResponse always have immutable headers. | |
| 391 * | |
| 392 * * HttpResponse and HttpClientRequest have immutable headers | |
| 393 * from the moment the body is written to. | |
| 394 * | |
| 395 * In these situations, the mutating methods throw exceptions. | |
| 396 * | |
| 397 * For all operations on HTTP headers the header name is | |
| 398 * case-insensitive. | |
| 399 * | |
| 400 * To set the value of a header use the `set()` method: | |
| 401 * | |
| 402 * request.headers.set(HttpHeaders.CACHE_CONTROL, | |
| 403 'max-age=3600, must-revalidate'); | |
| 404 * | |
| 405 * To retrieve the value of a header use the `value()` method: | |
| 406 * | |
| 407 * print(request.headers.value(HttpHeaders.USER_AGENT)); | |
| 408 * | |
| 409 * An HttpHeaders object holds a list of values for each name | |
| 410 * as the standard allows. In most cases a name holds only a single value, | |
| 411 * The most common mode of operation is to use `set()` for setting a value, | |
| 412 * and `value()` for retrieving a value. | |
| 413 */ | |
| 414 abstract class HttpHeaders { | |
| 415 static const ACCEPT = "accept"; | |
| 416 static const ACCEPT_CHARSET = "accept-charset"; | |
| 417 static const ACCEPT_ENCODING = "accept-encoding"; | |
| 418 static const ACCEPT_LANGUAGE = "accept-language"; | |
| 419 static const ACCEPT_RANGES = "accept-ranges"; | |
| 420 static const AGE = "age"; | |
| 421 static const ALLOW = "allow"; | |
| 422 static const AUTHORIZATION = "authorization"; | |
| 423 static const CACHE_CONTROL = "cache-control"; | |
| 424 static const CONNECTION = "connection"; | |
| 425 static const CONTENT_ENCODING = "content-encoding"; | |
| 426 static const CONTENT_LANGUAGE = "content-language"; | |
| 427 static const CONTENT_LENGTH = "content-length"; | |
| 428 static const CONTENT_LOCATION = "content-location"; | |
| 429 static const CONTENT_MD5 = "content-md5"; | |
| 430 static const CONTENT_RANGE = "content-range"; | |
| 431 static const CONTENT_TYPE = "content-type"; | |
| 432 static const DATE = "date"; | |
| 433 static const ETAG = "etag"; | |
| 434 static const EXPECT = "expect"; | |
| 435 static const EXPIRES = "expires"; | |
| 436 static const FROM = "from"; | |
| 437 static const HOST = "host"; | |
| 438 static const IF_MATCH = "if-match"; | |
| 439 static const IF_MODIFIED_SINCE = "if-modified-since"; | |
| 440 static const IF_NONE_MATCH = "if-none-match"; | |
| 441 static const IF_RANGE = "if-range"; | |
| 442 static const IF_UNMODIFIED_SINCE = "if-unmodified-since"; | |
| 443 static const LAST_MODIFIED = "last-modified"; | |
| 444 static const LOCATION = "location"; | |
| 445 static const MAX_FORWARDS = "max-forwards"; | |
| 446 static const PRAGMA = "pragma"; | |
| 447 static const PROXY_AUTHENTICATE = "proxy-authenticate"; | |
| 448 static const PROXY_AUTHORIZATION = "proxy-authorization"; | |
| 449 static const RANGE = "range"; | |
| 450 static const REFERER = "referer"; | |
| 451 static const RETRY_AFTER = "retry-after"; | |
| 452 static const SERVER = "server"; | |
| 453 static const TE = "te"; | |
| 454 static const TRAILER = "trailer"; | |
| 455 static const TRANSFER_ENCODING = "transfer-encoding"; | |
| 456 static const UPGRADE = "upgrade"; | |
| 457 static const USER_AGENT = "user-agent"; | |
| 458 static const VARY = "vary"; | |
| 459 static const VIA = "via"; | |
| 460 static const WARNING = "warning"; | |
| 461 static const WWW_AUTHENTICATE = "www-authenticate"; | |
| 462 | |
| 463 // Cookie headers from RFC 6265. | |
| 464 static const COOKIE = "cookie"; | |
| 465 static const SET_COOKIE = "set-cookie"; | |
| 466 | |
| 467 static const GENERAL_HEADERS = const [CACHE_CONTROL, | |
| 468 CONNECTION, | |
| 469 DATE, | |
| 470 PRAGMA, | |
| 471 TRAILER, | |
| 472 TRANSFER_ENCODING, | |
| 473 UPGRADE, | |
| 474 VIA, | |
| 475 WARNING]; | |
| 476 | |
| 477 static const ENTITY_HEADERS = const [ALLOW, | |
| 478 CONTENT_ENCODING, | |
| 479 CONTENT_LANGUAGE, | |
| 480 CONTENT_LENGTH, | |
| 481 CONTENT_LOCATION, | |
| 482 CONTENT_MD5, | |
| 483 CONTENT_RANGE, | |
| 484 CONTENT_TYPE, | |
| 485 EXPIRES, | |
| 486 LAST_MODIFIED]; | |
| 487 | |
| 488 | |
| 489 static const RESPONSE_HEADERS = const [ACCEPT_RANGES, | |
| 490 AGE, | |
| 491 ETAG, | |
| 492 LOCATION, | |
| 493 PROXY_AUTHENTICATE, | |
| 494 RETRY_AFTER, | |
| 495 SERVER, | |
| 496 VARY, | |
| 497 WWW_AUTHENTICATE]; | |
| 498 | |
| 499 static const REQUEST_HEADERS = const [ACCEPT, | |
| 500 ACCEPT_CHARSET, | |
| 501 ACCEPT_ENCODING, | |
| 502 ACCEPT_LANGUAGE, | |
| 503 AUTHORIZATION, | |
| 504 EXPECT, | |
| 505 FROM, | |
| 506 HOST, | |
| 507 IF_MATCH, | |
| 508 IF_MODIFIED_SINCE, | |
| 509 IF_NONE_MATCH, | |
| 510 IF_RANGE, | |
| 511 IF_UNMODIFIED_SINCE, | |
| 512 MAX_FORWARDS, | |
| 513 PROXY_AUTHORIZATION, | |
| 514 RANGE, | |
| 515 REFERER, | |
| 516 TE, | |
| 517 USER_AGENT]; | |
| 518 | |
| 519 /** | |
| 520 * Gets and sets the date. The value of this property will | |
| 521 * reflect the 'date' header. | |
| 522 */ | |
| 523 DateTime date; | |
| 524 | |
| 525 /** | |
| 526 * Gets and sets the expiry date. The value of this property will | |
| 527 * reflect the 'expires' header. | |
| 528 */ | |
| 529 DateTime expires; | |
| 530 | |
| 531 /** | |
| 532 * Gets and sets the "if-modified-since" date. The value of this property will | |
| 533 * reflect the "if-modified-since" header. | |
| 534 */ | |
| 535 DateTime ifModifiedSince; | |
| 536 | |
| 537 /** | |
| 538 * Gets and sets the host part of the 'host' header for the | |
| 539 * connection. | |
| 540 */ | |
| 541 String host; | |
| 542 | |
| 543 /** | |
| 544 * Gets and sets the port part of the 'host' header for the | |
| 545 * connection. | |
| 546 */ | |
| 547 int port; | |
| 548 | |
| 549 /** | |
| 550 * Gets and sets the content type. Note that the content type in the | |
| 551 * header will only be updated if this field is set | |
| 552 * directly. Mutating the returned current value will have no | |
| 553 * effect. | |
| 554 */ | |
| 555 ContentType contentType; | |
| 556 | |
| 557 /** | |
| 558 * Gets and sets the content length header value. | |
| 559 */ | |
| 560 int contentLength; | |
| 561 | |
| 562 /** | |
| 563 * Gets and sets the persistent connection header value. | |
| 564 */ | |
| 565 bool persistentConnection; | |
| 566 | |
| 567 /** | |
| 568 * Gets and sets the chunked transfer encoding header value. | |
| 569 */ | |
| 570 bool chunkedTransferEncoding; | |
| 571 | |
| 572 /** | |
| 573 * Returns the list of values for the header named [name]. If there | |
| 574 * is no header with the provided name, [:null:] will be returned. | |
| 575 */ | |
| 576 List<String> operator[](String name); | |
| 577 | |
| 578 /** | |
| 579 * Convenience method for the value for a single valued header. If | |
| 580 * there is no header with the provided name, [:null:] will be | |
| 581 * returned. If the header has more than one value an exception is | |
| 582 * thrown. | |
| 583 */ | |
| 584 String value(String name); | |
| 585 | |
| 586 /** | |
| 587 * Adds a header value. The header named [name] will have the value | |
| 588 * [value] added to its list of values. Some headers are single | |
| 589 * valued, and for these adding a value will replace the previous | |
| 590 * value. If the value is of type DateTime a HTTP date format will be | |
| 591 * applied. If the value is a [:List:] each element of the list will | |
| 592 * be added separately. For all other types the default [:toString:] | |
| 593 * method will be used. | |
| 594 */ | |
| 595 void add(String name, Object value); | |
| 596 | |
| 597 /** | |
| 598 * Sets a header. The header named [name] will have all its values | |
| 599 * cleared before the value [value] is added as its value. | |
| 600 */ | |
| 601 void set(String name, Object value); | |
| 602 | |
| 603 /** | |
| 604 * Removes a specific value for a header name. Some headers have | |
| 605 * system supplied values and for these the system supplied values | |
| 606 * will still be added to the collection of values for the header. | |
| 607 */ | |
| 608 void remove(String name, Object value); | |
| 609 | |
| 610 /** | |
| 611 * Removes all values for the specified header name. Some headers | |
| 612 * have system supplied values and for these the system supplied | |
| 613 * values will still be added to the collection of values for the | |
| 614 * header. | |
| 615 */ | |
| 616 void removeAll(String name); | |
| 617 | |
| 618 /** | |
| 619 * Enumerates the headers, applying the function [f] to each | |
| 620 * header. The header name passed in [:name:] will be all lower | |
| 621 * case. | |
| 622 */ | |
| 623 void forEach(void f(String name, List<String> values)); | |
| 624 | |
| 625 /** | |
| 626 * Disables folding for the header named [name] when sending the HTTP | |
| 627 * header. By default, multiple header values are folded into a | |
| 628 * single header line by separating the values with commas. The | |
| 629 * 'set-cookie' header has folding disabled by default. | |
| 630 */ | |
| 631 void noFolding(String name); | |
| 632 | |
| 633 /** | |
| 634 * Remove all headers. Some headers have system supplied values and | |
| 635 * for these the system supplied values will still be added to the | |
| 636 * collection of values for the header. | |
| 637 */ | |
| 638 void clear(); | |
| 639 } | |
| 640 | |
| 641 | |
| 642 /** | |
| 643 * Representation of a header value in the form: | |
| 644 * | |
| 645 * [:value; parameter1=value1; parameter2=value2:] | |
| 646 * | |
| 647 * [HeaderValue] can be used to conveniently build and parse header | |
| 648 * values on this form. | |
| 649 * | |
| 650 * To build an [:accepts:] header with the value | |
| 651 * | |
| 652 * text/plain; q=0.3, text/html | |
| 653 * | |
| 654 * use code like this: | |
| 655 * | |
| 656 * HttpClientRequest request = ...; | |
| 657 * var v = new HeaderValue("text/plain", {"q": "0.3"}); | |
| 658 * request.headers.add(HttpHeaders.ACCEPT, v); | |
| 659 * request.headers.add(HttpHeaders.ACCEPT, "text/html"); | |
| 660 * | |
| 661 * To parse the header values use the [:parse:] static method. | |
| 662 * | |
| 663 * HttpRequest request = ...; | |
| 664 * List<String> values = request.headers[HttpHeaders.ACCEPT]; | |
| 665 * values.forEach((value) { | |
| 666 * HeaderValue v = HeaderValue.parse(value); | |
| 667 * // Use v.value and v.parameters | |
| 668 * }); | |
| 669 * | |
| 670 * An instance of [HeaderValue] is immutable. | |
| 671 */ | |
| 672 abstract class HeaderValue { | |
| 673 /** | |
| 674 * Creates a new header value object setting the value and parameters. | |
| 675 */ | |
| 676 factory HeaderValue([String value = "", Map<String, String> parameters]) { | |
| 677 return new _HeaderValue(value, parameters); | |
| 678 } | |
| 679 | |
| 680 /** | |
| 681 * Creates a new header value object from parsing a header value | |
| 682 * string with both value and optional parameters. | |
| 683 */ | |
| 684 static HeaderValue parse(String value, | |
| 685 {String parameterSeparator: ";", | |
| 686 String valueSeparator: null, | |
| 687 bool preserveBackslash: false}) { | |
| 688 return _HeaderValue.parse(value, | |
| 689 parameterSeparator: parameterSeparator, | |
| 690 valueSeparator: valueSeparator, | |
| 691 preserveBackslash: preserveBackslash); | |
| 692 } | |
| 693 | |
| 694 /** | |
| 695 * Gets the header value. | |
| 696 */ | |
| 697 String get value; | |
| 698 | |
| 699 /** | |
| 700 * Gets the map of parameters. | |
| 701 * | |
| 702 * This map cannot be modified. invoking any operation which would | |
| 703 * modify the map will throw [UnsupportedError]. | |
| 704 */ | |
| 705 Map<String, String> get parameters; | |
| 706 | |
| 707 /** | |
| 708 * Returns the formatted string representation in the form: | |
| 709 * | |
| 710 * value; parameter1=value1; parameter2=value2 | |
| 711 */ | |
| 712 String toString(); | |
| 713 } | |
| 714 | |
| 715 abstract class HttpSession implements Map { | |
| 716 /** | |
| 717 * Gets the id for the current session. | |
| 718 */ | |
| 719 String get id; | |
| 720 | |
| 721 /** | |
| 722 * Destroys the session. This will terminate the session and any further | |
| 723 * connections with this id will be given a new id and session. | |
| 724 */ | |
| 725 void destroy(); | |
| 726 | |
| 727 /** | |
| 728 * Sets a callback that will be called when the session is timed out. | |
| 729 */ | |
| 730 void set onTimeout(void callback()); | |
| 731 | |
| 732 /** | |
| 733 * Is true if the session has not been sent to the client yet. | |
| 734 */ | |
| 735 bool get isNew; | |
| 736 } | |
| 737 | |
| 738 | |
| 739 /** | |
| 740 * Representation of a content type. An instance of [ContentType] is | |
| 741 * immutable. | |
| 742 */ | |
| 743 abstract class ContentType implements HeaderValue { | |
| 744 /** | |
| 745 * Content type for plain text using UTF-8 encoding. | |
| 746 * | |
| 747 * text/plain; charset=utf-8 | |
| 748 */ | |
| 749 static final TEXT = new ContentType("text", "plain", charset: "utf-8"); | |
| 750 | |
| 751 /** | |
| 752 * Content type for HTML using UTF-8 encoding. | |
| 753 * | |
| 754 * text/html; charset=utf-8 | |
| 755 */ | |
| 756 static final HTML = new ContentType("text", "html", charset: "utf-8"); | |
| 757 | |
| 758 /** | |
| 759 * Content type for JSON using UTF-8 encoding. | |
| 760 * | |
| 761 * application/json; charset=utf-8 | |
| 762 */ | |
| 763 static final JSON = new ContentType("application", "json", charset: "utf-8"); | |
| 764 | |
| 765 /** | |
| 766 * Content type for binary data. | |
| 767 * | |
| 768 * application/octet-stream | |
| 769 */ | |
| 770 static final BINARY = new ContentType("application", "octet-stream"); | |
| 771 | |
| 772 /** | |
| 773 * Creates a new content type object setting the primary type and | |
| 774 * sub type. The charset and additional parameters can also be set | |
| 775 * using [charset] and [parameters]. If charset is passed and | |
| 776 * [parameters] contains charset as well the passed [charset] will | |
| 777 * override the value in parameters. Keys passed in parameters will be | |
| 778 * converted to lower case. The `charset` entry, whether passed as `charset` | |
| 779 * or in `parameters`, will have its value converted to lower-case. | |
| 780 */ | |
| 781 factory ContentType(String primaryType, | |
| 782 String subType, | |
| 783 {String charset, Map<String, String> parameters}) { | |
| 784 return new _ContentType(primaryType, subType, charset, parameters); | |
| 785 } | |
| 786 | |
| 787 /** | |
| 788 * Creates a new content type object from parsing a Content-Type | |
| 789 * header value. As primary type, sub type and parameter names and | |
| 790 * values are not case sensitive all these values will be converted | |
| 791 * to lower case. Parsing this string | |
| 792 * | |
| 793 * text/html; charset=utf-8 | |
| 794 * | |
| 795 * will create a content type object with primary type [:text:], sub | |
| 796 * type [:html:] and parameter [:charset:] with value [:utf-8:]. | |
| 797 */ | |
| 798 static ContentType parse(String value) { | |
| 799 return _ContentType.parse(value); | |
| 800 } | |
| 801 | |
| 802 /** | |
| 803 * Gets the mime-type, without any parameters. | |
| 804 */ | |
| 805 String get mimeType; | |
| 806 | |
| 807 /** | |
| 808 * Gets the primary type. | |
| 809 */ | |
| 810 String get primaryType; | |
| 811 | |
| 812 /** | |
| 813 * Gets the sub type. | |
| 814 */ | |
| 815 String get subType; | |
| 816 | |
| 817 /** | |
| 818 * Gets the character set. | |
| 819 */ | |
| 820 String get charset; | |
| 821 } | |
| 822 | |
| 823 | |
| 824 /** | |
| 825 * Representation of a cookie. For cookies received by the server as | |
| 826 * Cookie header values only [:name:] and [:value:] fields will be | |
| 827 * set. When building a cookie for the 'set-cookie' header in the server | |
| 828 * and when receiving cookies in the client as 'set-cookie' headers all | |
| 829 * fields can be used. | |
| 830 */ | |
| 831 abstract class Cookie { | |
| 832 /** | |
| 833 * Gets and sets the name. | |
| 834 */ | |
| 835 String name; | |
| 836 | |
| 837 /** | |
| 838 * Gets and sets the value. | |
| 839 */ | |
| 840 String value; | |
| 841 | |
| 842 /** | |
| 843 * Gets and sets the expiry date. | |
| 844 */ | |
| 845 DateTime expires; | |
| 846 | |
| 847 /** | |
| 848 * Gets and sets the max age. A value of [:0:] means delete cookie | |
| 849 * now. | |
| 850 */ | |
| 851 int maxAge; | |
| 852 | |
| 853 /** | |
| 854 * Gets and sets the domain. | |
| 855 */ | |
| 856 String domain; | |
| 857 | |
| 858 /** | |
| 859 * Gets and sets the path. | |
| 860 */ | |
| 861 String path; | |
| 862 | |
| 863 /** | |
| 864 * Gets and sets whether this cookie is secure. | |
| 865 */ | |
| 866 bool secure; | |
| 867 | |
| 868 /** | |
| 869 * Gets and sets whether this cookie is HTTP only. | |
| 870 */ | |
| 871 bool httpOnly; | |
| 872 | |
| 873 /** | |
| 874 * Creates a new cookie optionally setting the name and value. | |
| 875 * | |
| 876 * By default the value of `httpOnly` will be set to `true`. | |
| 877 */ | |
| 878 factory Cookie([String name, String value]) => new _Cookie(name, value); | |
| 879 | |
| 880 /** | |
| 881 * Creates a new cookie by parsing a header value from a 'set-cookie' | |
| 882 * header. | |
| 883 */ | |
| 884 factory Cookie.fromSetCookieValue(String value) { | |
| 885 return new _Cookie.fromSetCookieValue(value); | |
| 886 } | |
| 887 | |
| 888 /** | |
| 889 * Returns the formatted string representation of the cookie. The | |
| 890 * string representation can be used for for setting the Cookie or | |
| 891 * 'set-cookie' headers | |
| 892 */ | |
| 893 String toString(); | |
| 894 } | |
| 895 | |
| 896 | |
| 897 /** | |
| 898 * A server-side object | |
| 899 * that contains the content of and information about an HTTP request. | |
| 900 * | |
| 901 * __Note__: Check out the | |
| 902 * [http_server](http://pub.dartlang.org/packages/http_server) | |
| 903 * package, which makes working with the low-level | |
| 904 * dart:io HTTP server subsystem easier. | |
| 905 * | |
| 906 * `HttpRequest` objects are generated by an [HttpServer], | |
| 907 * which listens for HTTP requests on a specific host and port. | |
| 908 * For each request received, the HttpServer, which is a [Stream], | |
| 909 * generates an `HttpRequest` object and adds it to the stream. | |
| 910 * | |
| 911 * An `HttpRequest` object delivers the body content of the request | |
| 912 * as a stream of byte lists. | |
| 913 * The object also contains information about the request, | |
| 914 * such as the method, URI, and headers. | |
| 915 * | |
| 916 * In the following code, an HttpServer listens | |
| 917 * for HTTP requests. When the server receives a request, | |
| 918 * it uses the HttpRequest object's `method` property to dispatch requests. | |
| 919 * | |
| 920 * final HOST = InternetAddress.LOOPBACK_IP_V4; | |
| 921 * final PORT = 80; | |
| 922 * | |
| 923 * HttpServer.bind(HOST, PORT).then((_server) { | |
| 924 * _server.listen((HttpRequest request) { | |
| 925 * switch (request.method) { | |
| 926 * case 'GET': | |
| 927 * handleGetRequest(request); | |
| 928 * break; | |
| 929 * case 'POST': | |
| 930 * ... | |
| 931 * } | |
| 932 * }, | |
| 933 * onError: handleError); // listen() failed. | |
| 934 * }).catchError(handleError); | |
| 935 * | |
| 936 * An HttpRequest object provides access to the associated [HttpResponse] | |
| 937 * object through the response property. | |
| 938 * The server writes its response to the body of the HttpResponse object. | |
| 939 * For example, here's a function that responds to a request: | |
| 940 * | |
| 941 * void handleGetRequest(HttpRequest req) { | |
| 942 * HttpResponse res = req.response; | |
| 943 * res.write('Received request ${req.method}: ${req.uri.path}'); | |
| 944 * res.close(); | |
| 945 * } | |
| 946 */ | |
| 947 abstract class HttpRequest implements Stream<List<int>> { | |
| 948 /** | |
| 949 * The content length of the request body. | |
| 950 * | |
| 951 * If the size of the request body is not known in advance, | |
| 952 * this value is -1. | |
| 953 */ | |
| 954 int get contentLength; | |
| 955 | |
| 956 /** | |
| 957 * The method, such as 'GET' or 'POST', for the request. | |
| 958 */ | |
| 959 String get method; | |
| 960 | |
| 961 /** | |
| 962 * The URI for the request. | |
| 963 * | |
| 964 * This provides access to the | |
| 965 * path and query string for the request. | |
| 966 */ | |
| 967 Uri get uri; | |
| 968 | |
| 969 /** | |
| 970 * The requested URI for the request. | |
| 971 * | |
| 972 * The returend URI is reconstructed by using http-header fields, to access | |
| 973 * otherwise lost information, e.g. host and scheme. | |
| 974 * | |
| 975 * To reconstruct the scheme, first 'X-Forwarded-Proto' is checked, and then | |
| 976 * falling back to server type. | |
| 977 * | |
| 978 * To reconstruct the host, fisrt 'X-Forwarded-Host' is checked, then 'Host' | |
| 979 * and finally calling back to server. | |
| 980 */ | |
| 981 Uri get requestedUri; | |
| 982 | |
| 983 /** | |
| 984 * The request headers. | |
| 985 * | |
| 986 * The returned [HttpHeaders] are immutable. | |
| 987 */ | |
| 988 HttpHeaders get headers; | |
| 989 | |
| 990 /** | |
| 991 * The cookies in the request, from the Cookie headers. | |
| 992 */ | |
| 993 List<Cookie> get cookies; | |
| 994 | |
| 995 /** | |
| 996 * The persistent connection state signaled by the client. | |
| 997 */ | |
| 998 bool get persistentConnection; | |
| 999 | |
| 1000 /** | |
| 1001 * The client certificate of the client making the request. | |
| 1002 * | |
| 1003 * This value is null if the connection is not a secure TLS or SSL connection, | |
| 1004 * or if the server does not request a client certificate, or if the client | |
| 1005 * does not provide one. | |
| 1006 */ | |
| 1007 X509Certificate get certificate; | |
| 1008 | |
| 1009 /** | |
| 1010 * The session for the given request. | |
| 1011 * | |
| 1012 * If the session is | |
| 1013 * being initialized by this call, [:isNew:] is true for the returned | |
| 1014 * session. | |
| 1015 * See [HttpServer.sessionTimeout] on how to change default timeout. | |
| 1016 */ | |
| 1017 HttpSession get session; | |
| 1018 | |
| 1019 /** | |
| 1020 * The HTTP protocol version used in the request, | |
| 1021 * either "1.0" or "1.1". | |
| 1022 */ | |
| 1023 String get protocolVersion; | |
| 1024 | |
| 1025 /** | |
| 1026 * Information about the client connection. | |
| 1027 * | |
| 1028 * Returns [:null:] if the socket is not available. | |
| 1029 */ | |
| 1030 HttpConnectionInfo get connectionInfo; | |
| 1031 | |
| 1032 /** | |
| 1033 * The [HttpResponse] object, used for sending back the response to the | |
| 1034 * client. | |
| 1035 * | |
| 1036 * If the [contentLength] of the body isn't 0, and the body isn't being read, | |
| 1037 * any write calls on the [HttpResponse] automatically drain the request | |
| 1038 * body. | |
| 1039 */ | |
| 1040 HttpResponse get response; | |
| 1041 } | |
| 1042 | |
| 1043 | |
| 1044 /** | |
| 1045 * An HTTP response, which returns the headers and data | |
| 1046 * from the server to the client in response to an HTTP request. | |
| 1047 * | |
| 1048 * Every HttpRequest object provides access to the associated [HttpResponse] | |
| 1049 * object through the `response` property. | |
| 1050 * The server sends its response to the client by writing to the | |
| 1051 * HttpResponse object. | |
| 1052 * | |
| 1053 * ## Writing the response | |
| 1054 * | |
| 1055 * This class implements [IOSink]. | |
| 1056 * After the header has been set up, the methods | |
| 1057 * from IOSink, such as `writeln()`, can be used to write | |
| 1058 * the body of the HTTP response. | |
| 1059 * Use the `close()` method to close the response and send it to the client. | |
| 1060 * | |
| 1061 * server.listen((HttpRequest request) { | |
| 1062 * request.response.write('Hello, world!'); | |
| 1063 * request.response.close(); | |
| 1064 * }); | |
| 1065 * | |
| 1066 * When one of the IOSink methods is used for the | |
| 1067 * first time, the request header is sent. Calling any methods that | |
| 1068 * change the header after it is sent throws an exception. | |
| 1069 * | |
| 1070 * ## Setting the headers | |
| 1071 * | |
| 1072 * The HttpResponse object has a number of properties for setting up | |
| 1073 * the HTTP headers of the response. | |
| 1074 * When writing string data through the IOSink, the encoding used | |
| 1075 * is determined from the "charset" parameter of the | |
| 1076 * "Content-Type" header. | |
| 1077 * | |
| 1078 * HttpResponse response = ... | |
| 1079 * response.headers.contentType | |
| 1080 * = new ContentType("application", "json", charset: "utf-8"); | |
| 1081 * response.write(...); // Strings written will be UTF-8 encoded. | |
| 1082 * | |
| 1083 * If no charset is provided the default of ISO-8859-1 (Latin 1) will | |
| 1084 * be used. | |
| 1085 * | |
| 1086 * HttpResponse response = ... | |
| 1087 * response.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); | |
| 1088 * response.write(...); // Strings written will be ISO-8859-1 encoded. | |
| 1089 * | |
| 1090 * An exception is thrown if you use the `write()` method | |
| 1091 * while an unsupported content-type is set. | |
| 1092 */ | |
| 1093 abstract class HttpResponse implements IOSink { | |
| 1094 // TODO(ajohnsen): Add documentation of how to pipe a file to the response. | |
| 1095 /** | |
| 1096 * Gets and sets the content length of the response. If the size of | |
| 1097 * the response is not known in advance set the content length to | |
| 1098 * -1 - which is also the default if not set. | |
| 1099 */ | |
| 1100 int contentLength; | |
| 1101 | |
| 1102 /** | |
| 1103 * Gets and sets the status code. Any integer value is accepted. For | |
| 1104 * the official HTTP status codes use the fields from | |
| 1105 * [HttpStatus]. If no status code is explicitly set the default | |
| 1106 * value [HttpStatus.OK] is used. | |
| 1107 * | |
| 1108 * The status code must be set before the body is written | |
| 1109 * to. Setting the status code after writing to the response body or | |
| 1110 * closing the response will throw a `StateError`. | |
| 1111 */ | |
| 1112 int statusCode; | |
| 1113 | |
| 1114 /** | |
| 1115 * Gets and sets the reason phrase. If no reason phrase is explicitly | |
| 1116 * set a default reason phrase is provided. | |
| 1117 * | |
| 1118 * The reason phrase must be set before the body is written | |
| 1119 * to. Setting the reason phrase after writing to the response body | |
| 1120 * or closing the response will throw a `StateError`. | |
| 1121 */ | |
| 1122 String reasonPhrase; | |
| 1123 | |
| 1124 /** | |
| 1125 * Gets and sets the persistent connection state. The initial value | |
| 1126 * of this property is the persistent connection state from the | |
| 1127 * request. | |
| 1128 */ | |
| 1129 bool persistentConnection; | |
| 1130 | |
| 1131 /** | |
| 1132 * Set and get the [deadline] for the response. The deadline is timed from the | |
| 1133 * time it's set. Setting a new deadline will override any previous deadline. | |
| 1134 * When a deadline is exceeded, the response will be closed and any further | |
| 1135 * data ignored. | |
| 1136 * | |
| 1137 * To disable a deadline, set the [deadline] to `null`. | |
| 1138 * | |
| 1139 * The [deadline] is `null` by default. | |
| 1140 */ | |
| 1141 Duration deadline; | |
| 1142 | |
| 1143 /** | |
| 1144 * Get or set if the [HttpResponse] should buffer output. | |
| 1145 * | |
| 1146 * Default value is `true`. | |
| 1147 * | |
| 1148 * __Note__: Disabling buffering of the output can result in very poor | |
| 1149 * performance, when writing many small chunks. | |
| 1150 */ | |
| 1151 bool bufferOutput; | |
| 1152 | |
| 1153 /** | |
| 1154 * Returns the response headers. | |
| 1155 * | |
| 1156 * The response headers can be modified until the response body is | |
| 1157 * written to or closed. After that they become immutable. | |
| 1158 */ | |
| 1159 HttpHeaders get headers; | |
| 1160 | |
| 1161 /** | |
| 1162 * Cookies to set in the client (in the 'set-cookie' header). | |
| 1163 */ | |
| 1164 List<Cookie> get cookies; | |
| 1165 | |
| 1166 /** | |
| 1167 * Respond with a redirect to [location]. | |
| 1168 * | |
| 1169 * The URI in [location] should be absolute, but there are no checks | |
| 1170 * to enforce that. | |
| 1171 * | |
| 1172 * By default the HTTP status code `HttpStatus.MOVED_TEMPORARILY` | |
| 1173 * (`302`) is used for the redirect, but an alternative one can be | |
| 1174 * specified using the [status] argument. | |
| 1175 * | |
| 1176 * This method will also call `close`, and the returned future is | |
| 1177 * the furure returned by `close`. | |
| 1178 */ | |
| 1179 Future redirect(Uri location, {int status: HttpStatus.MOVED_TEMPORARILY}); | |
| 1180 | |
| 1181 /** | |
| 1182 * Detaches the underlying socket from the HTTP server. When the | |
| 1183 * socket is detached the HTTP server will no longer perform any | |
| 1184 * operations on it. | |
| 1185 * | |
| 1186 * This is normally used when a HTTP upgrade request is received | |
| 1187 * and the communication should continue with a different protocol. | |
| 1188 * | |
| 1189 * If [writeHeaders] is `true`, the status line and [headers] will be written | |
| 1190 * to the socket before it's detached. If `false`, the socket is detached | |
| 1191 * immediately, without any data written to the socket. Default is `true`. | |
| 1192 */ | |
| 1193 Future<Socket> detachSocket({bool writeHeaders: true}); | |
| 1194 | |
| 1195 /** | |
| 1196 * Gets information about the client connection. Returns [:null:] if the | |
| 1197 * socket is not available. | |
| 1198 */ | |
| 1199 HttpConnectionInfo get connectionInfo; | |
| 1200 } | |
| 1201 | |
| 1202 | |
| 1203 /** | |
| 1204 * A client that receives content, such as web pages, from | |
| 1205 * a server using the HTTP protocol. | |
| 1206 * | |
| 1207 * HttpClient contains a number of methods to send an [HttpClientRequest] | |
| 1208 * to an Http server and receive an [HttpClientResponse] back. | |
| 1209 * For example, you can use the [get], [getUrl], [post], and [postUrl] methods | |
| 1210 * for GET and POST requests, respectively. | |
| 1211 * | |
| 1212 * ## Making a simple GET request: an example | |
| 1213 * | |
| 1214 * A `getUrl` request is a two-step process, triggered by two [Future]s. | |
| 1215 * When the first future completes with a [HttpClientRequest], the underlying | |
| 1216 * network connection has been established, but no data has been sent. | |
| 1217 * In the callback function for the first future, the HTTP headers and body | |
| 1218 * can be set on the request. Either the first write to the request object | |
| 1219 * or a call to [close] sends the request to the server. | |
| 1220 * | |
| 1221 * When the HTTP response is received from the server, | |
| 1222 * the second future, which is returned by close, | |
| 1223 * completes with an [HttpClientResponse] object. | |
| 1224 * This object provides access to the headers and body of the response. | |
| 1225 * The body is available as a stream implemented by HttpClientResponse. | |
| 1226 * If a body is present, it must be read. Otherwise, it leads to resource | |
| 1227 * leaks. Consider using [HttpClientResponse.drain] if the body is unused. | |
| 1228 * | |
| 1229 * HttpClient client = new HttpClient(); | |
| 1230 * client.getUrl(Uri.parse("http://www.example.com/")) | |
| 1231 * .then((HttpClientRequest request) { | |
| 1232 * // Optionally set up headers... | |
| 1233 * // Optionally write to the request object... | |
| 1234 * // Then call close. | |
| 1235 * ... | |
| 1236 * return request.close(); | |
| 1237 * }) | |
| 1238 * .then((HttpClientResponse response) { | |
| 1239 * // Process the response. | |
| 1240 * ... | |
| 1241 * }); | |
| 1242 * | |
| 1243 * The future for [HttpClientRequest] is created by methods such as | |
| 1244 * [getUrl] and [open]. | |
| 1245 * | |
| 1246 * ## HTTPS connections | |
| 1247 * | |
| 1248 * An HttpClient can make HTTPS requests, connecting to a server using | |
| 1249 * the TLS (SSL) secure networking protocol. Calling [getUrl] with an | |
| 1250 * https: scheme will work automatically, if the server's certificate is | |
| 1251 * signed by a root CA (certificate authority) on the default list of | |
| 1252 * well-known trusted CAs, compiled by Mozilla. | |
| 1253 * | |
| 1254 * To add a custom trusted certificate authority, or to send a client | |
| 1255 * certificate to servers that request one, pass a [SecurityContext] object | |
| 1256 * as the optional [context] argument to the `HttpClient` constructor. | |
| 1257 * The desired security options can be set on the [SecurityContext] object. | |
| 1258 * | |
| 1259 * ## Headers | |
| 1260 * | |
| 1261 * All HttpClient requests set the following header by default: | |
| 1262 * | |
| 1263 * Accept-Encoding: gzip | |
| 1264 * | |
| 1265 * This allows the HTTP server to use gzip compression for the body if | |
| 1266 * possible. If this behavior is not desired set the | |
| 1267 * `Accept-Encoding` header to something else. | |
| 1268 * To turn off gzip compression of the response, clear this header: | |
| 1269 * | |
| 1270 * request.headers.removeAll(HttpHeaders.ACCEPT_ENCODING) | |
| 1271 * | |
| 1272 * ## Closing the HttpClient | |
| 1273 * | |
| 1274 * The HttpClient supports persistent connections and caches network | |
| 1275 * connections to reuse them for multiple requests whenever | |
| 1276 * possible. This means that network connections can be kept open for | |
| 1277 * some time after a request has completed. Use HttpClient.close | |
| 1278 * to force the HttpClient object to shut down and to close the idle | |
| 1279 * network connections. | |
| 1280 * | |
| 1281 * ## Turning proxies on and off | |
| 1282 * | |
| 1283 * By default the HttpClient uses the proxy configuration available | |
| 1284 * from the environment, see [findProxyFromEnvironment]. To turn off | |
| 1285 * the use of proxies set the [findProxy] property to | |
| 1286 * [:null:]. | |
| 1287 * | |
| 1288 * HttpClient client = new HttpClient(); | |
| 1289 * client.findProxy = null; | |
| 1290 */ | |
| 1291 abstract class HttpClient { | |
| 1292 static const int DEFAULT_HTTP_PORT = 80; | |
| 1293 static const int DEFAULT_HTTPS_PORT = 443; | |
| 1294 | |
| 1295 /** | |
| 1296 * Get and set the idle timeout of non-active persistent (keep-alive) | |
| 1297 * connections. The default value is 15 seconds. | |
| 1298 */ | |
| 1299 Duration idleTimeout; | |
| 1300 | |
| 1301 /** | |
| 1302 * Get and set the maximum number of live connections, to a single host. | |
| 1303 * | |
| 1304 * Increasing this number may lower performance and take up unwanted | |
| 1305 * system resources. | |
| 1306 * | |
| 1307 * To disable, set to `null`. | |
| 1308 * | |
| 1309 * Default is `null`. | |
| 1310 */ | |
| 1311 int maxConnectionsPerHost; | |
| 1312 | |
| 1313 /** | |
| 1314 * Get and set whether the body of a response will be automatically | |
| 1315 * uncompressed. | |
| 1316 * | |
| 1317 * The body of an HTTP response can be compressed. In most | |
| 1318 * situations providing the un-compressed body is most | |
| 1319 * convenient. Therefore the default behavior is to un-compress the | |
| 1320 * body. However in some situations (e.g. implementing a transparent | |
| 1321 * proxy) keeping the uncompressed stream is required. | |
| 1322 * | |
| 1323 * NOTE: Headers in from the response is never modified. This means | |
| 1324 * that when automatic un-compression is turned on the value of the | |
| 1325 * header `Content-Length` will reflect the length of the original | |
| 1326 * compressed body. Likewise the header `Content-Encoding` will also | |
| 1327 * have the original value indicating compression. | |
| 1328 * | |
| 1329 * NOTE: Automatic un-compression is only performed if the | |
| 1330 * `Content-Encoding` header value is `gzip`. | |
| 1331 * | |
| 1332 * This value affects all responses produced by this client after the | |
| 1333 * value is changed. | |
| 1334 * | |
| 1335 * To disable, set to `false`. | |
| 1336 * | |
| 1337 * Default is `true`. | |
| 1338 */ | |
| 1339 bool autoUncompress; | |
| 1340 | |
| 1341 /** | |
| 1342 * Set and get the default value of the `User-Agent` header for all requests | |
| 1343 * generated by this [HttpClient]. The default value is | |
| 1344 * `Dart/<version> (dart:io)`. | |
| 1345 * | |
| 1346 * If the userAgent is set to `null`, no default `User-Agent` header will be | |
| 1347 * added to each request. | |
| 1348 */ | |
| 1349 String userAgent; | |
| 1350 | |
| 1351 factory HttpClient({SecurityContext context}) => new _HttpClient(context); | |
| 1352 | |
| 1353 /** | |
| 1354 * Opens a HTTP connection. | |
| 1355 * | |
| 1356 * The HTTP method to use is specified in [method], the server is | |
| 1357 * specified using [host] and [port], and the path (including | |
| 1358 * a possible query) is specified using [path]. | |
| 1359 * The path may also contain a URI fragment, which will be ignored. | |
| 1360 * | |
| 1361 * The `Host` header for the request will be set to the value | |
| 1362 * [host]:[port]. This can be overridden through the | |
| 1363 * [HttpClientRequest] interface before the request is sent. NOTE | |
| 1364 * if [host] is an IP address this will still be set in the `Host` | |
| 1365 * header. | |
| 1366 * | |
| 1367 * For additional information on the sequence of events during an | |
| 1368 * HTTP transaction, and the objects returned by the futures, see | |
| 1369 * the overall documentation for the class [HttpClient]. | |
| 1370 */ | |
| 1371 Future<HttpClientRequest> open(String method, | |
| 1372 String host, | |
| 1373 int port, | |
| 1374 String path); | |
| 1375 | |
| 1376 /** | |
| 1377 * Opens a HTTP connection. | |
| 1378 * | |
| 1379 * The HTTP method is specified in [method] and the URL to use in | |
| 1380 * [url]. | |
| 1381 * | |
| 1382 * The `Host` header for the request will be set to the value | |
| 1383 * [host]:[port]. This can be overridden through the | |
| 1384 * [HttpClientRequest] interface before the request is sent. NOTE | |
| 1385 * if [host] is an IP address this will still be set in the `Host` | |
| 1386 * header. | |
| 1387 * | |
| 1388 * For additional information on the sequence of events during an | |
| 1389 * HTTP transaction, and the objects returned by the futures, see | |
| 1390 * the overall documentation for the class [HttpClient]. | |
| 1391 */ | |
| 1392 Future<HttpClientRequest> openUrl(String method, Uri url); | |
| 1393 | |
| 1394 /** | |
| 1395 * Opens a HTTP connection using the GET method. | |
| 1396 * | |
| 1397 * The server is specified using [host] and [port], and the path | |
| 1398 * (including a possible query) is specified using | |
| 1399 * [path]. | |
| 1400 * | |
| 1401 * See [open] for details. | |
| 1402 */ | |
| 1403 Future<HttpClientRequest> get(String host, int port, String path); | |
| 1404 | |
| 1405 /** | |
| 1406 * Opens a HTTP connection using the GET method. | |
| 1407 * | |
| 1408 * The URL to use is specified in [url]. | |
| 1409 * | |
| 1410 * See [openUrl] for details. | |
| 1411 */ | |
| 1412 Future<HttpClientRequest> getUrl(Uri url); | |
| 1413 | |
| 1414 /** | |
| 1415 * Opens a HTTP connection using the POST method. | |
| 1416 * | |
| 1417 * The server is specified using [host] and [port], and the path | |
| 1418 * (including a possible query) is specified using | |
| 1419 * [path]. | |
| 1420 * | |
| 1421 * See [open] for details. | |
| 1422 */ | |
| 1423 Future<HttpClientRequest> post(String host, int port, String path); | |
| 1424 | |
| 1425 /** | |
| 1426 * Opens a HTTP connection using the POST method. | |
| 1427 * | |
| 1428 * The URL to use is specified in [url]. | |
| 1429 * | |
| 1430 * See [openUrl] for details. | |
| 1431 */ | |
| 1432 Future<HttpClientRequest> postUrl(Uri url); | |
| 1433 | |
| 1434 /** | |
| 1435 * Opens a HTTP connection using the PUT method. | |
| 1436 * | |
| 1437 * The server is specified using [host] and [port], and the path | |
| 1438 * (including a possible query) is specified using [path]. | |
| 1439 * | |
| 1440 * See [open] for details. | |
| 1441 */ | |
| 1442 Future<HttpClientRequest> put(String host, int port, String path); | |
| 1443 | |
| 1444 /** | |
| 1445 * Opens a HTTP connection using the PUT method. | |
| 1446 * | |
| 1447 * The URL to use is specified in [url]. | |
| 1448 * | |
| 1449 * See [openUrl] for details. | |
| 1450 */ | |
| 1451 Future<HttpClientRequest> putUrl(Uri url); | |
| 1452 | |
| 1453 /** | |
| 1454 * Opens a HTTP connection using the DELETE method. | |
| 1455 * | |
| 1456 * The server is specified using [host] and [port], and the path | |
| 1457 * (including s possible query) is specified using [path]. | |
| 1458 * | |
| 1459 * See [open] for details. | |
| 1460 */ | |
| 1461 Future<HttpClientRequest> delete(String host, int port, String path); | |
| 1462 | |
| 1463 /** | |
| 1464 * Opens a HTTP connection using the DELETE method. | |
| 1465 * | |
| 1466 * The URL to use is specified in [url]. | |
| 1467 * | |
| 1468 * See [openUrl] for details. | |
| 1469 */ | |
| 1470 Future<HttpClientRequest> deleteUrl(Uri url); | |
| 1471 | |
| 1472 /** | |
| 1473 * Opens a HTTP connection using the PATCH method. | |
| 1474 * | |
| 1475 * The server is specified using [host] and [port], and the path | |
| 1476 * (including a possible query) is specified using [path]. | |
| 1477 * | |
| 1478 * See [open] for details. | |
| 1479 */ | |
| 1480 Future<HttpClientRequest> patch(String host, int port, String path); | |
| 1481 | |
| 1482 /** | |
| 1483 * Opens a HTTP connection using the PATCH method. | |
| 1484 * | |
| 1485 * The URL to use is specified in [url]. | |
| 1486 * | |
| 1487 * See [openUrl] for details. | |
| 1488 */ | |
| 1489 Future<HttpClientRequest> patchUrl(Uri url); | |
| 1490 | |
| 1491 /** | |
| 1492 * Opens a HTTP connection using the HEAD method. | |
| 1493 * | |
| 1494 * The server is specified using [host] and [port], and the path | |
| 1495 * (including a possible query) is specified using [path]. | |
| 1496 * | |
| 1497 * See [open] for details. | |
| 1498 */ | |
| 1499 Future<HttpClientRequest> head(String host, int port, String path); | |
| 1500 | |
| 1501 /** | |
| 1502 * Opens a HTTP connection using the HEAD method. | |
| 1503 * | |
| 1504 * The URL to use is specified in [url]. | |
| 1505 * | |
| 1506 * See [openUrl] for details. | |
| 1507 */ | |
| 1508 Future<HttpClientRequest> headUrl(Uri url); | |
| 1509 | |
| 1510 /** | |
| 1511 * Sets the function to be called when a site is requesting | |
| 1512 * authentication. The URL requested and the security realm from the | |
| 1513 * server are passed in the arguments [url] and [realm]. | |
| 1514 * | |
| 1515 * The function returns a [Future] which should complete when the | |
| 1516 * authentication has been resolved. If credentials cannot be | |
| 1517 * provided the [Future] should complete with [:false:]. If | |
| 1518 * credentials are available the function should add these using | |
| 1519 * [addCredentials] before completing the [Future] with the value | |
| 1520 * [:true:]. | |
| 1521 * | |
| 1522 * If the [Future] completes with true the request will be retried | |
| 1523 * using the updated credentials. Otherwise response processing will | |
| 1524 * continue normally. | |
| 1525 */ | |
| 1526 set authenticate(Future<bool> f(Uri url, String scheme, String realm)); | |
| 1527 | |
| 1528 /** | |
| 1529 * Add credentials to be used for authorizing HTTP requests. | |
| 1530 */ | |
| 1531 void addCredentials(Uri url, String realm, HttpClientCredentials credentials); | |
| 1532 | |
| 1533 /** | |
| 1534 * Sets the function used to resolve the proxy server to be used for | |
| 1535 * opening a HTTP connection to the specified [url]. If this | |
| 1536 * function is not set, direct connections will always be used. | |
| 1537 * | |
| 1538 * The string returned by [f] must be in the format used by browser | |
| 1539 * PAC (proxy auto-config) scripts. That is either | |
| 1540 * | |
| 1541 * "DIRECT" | |
| 1542 * | |
| 1543 * for using a direct connection or | |
| 1544 * | |
| 1545 * "PROXY host:port" | |
| 1546 * | |
| 1547 * for using the proxy server [:host:] on port [:port:]. | |
| 1548 * | |
| 1549 * A configuration can contain several configuration elements | |
| 1550 * separated by semicolons, e.g. | |
| 1551 * | |
| 1552 * "PROXY host:port; PROXY host2:port2; DIRECT" | |
| 1553 * | |
| 1554 * The static function [findProxyFromEnvironment] on this class can | |
| 1555 * be used to implement proxy server resolving based on environment | |
| 1556 * variables. | |
| 1557 */ | |
| 1558 set findProxy(String f(Uri url)); | |
| 1559 | |
| 1560 /** | |
| 1561 * Function for resolving the proxy server to be used for a HTTP | |
| 1562 * connection from the proxy configuration specified through | |
| 1563 * environment variables. | |
| 1564 * | |
| 1565 * The following environment variables are taken into account: | |
| 1566 * | |
| 1567 * http_proxy | |
| 1568 * https_proxy | |
| 1569 * no_proxy | |
| 1570 * HTTP_PROXY | |
| 1571 * HTTPS_PROXY | |
| 1572 * NO_PROXY | |
| 1573 * | |
| 1574 * [:http_proxy:] and [:HTTP_PROXY:] specify the proxy server to use for | |
| 1575 * http:// urls. Use the format [:hostname:port:]. If no port is used a | |
| 1576 * default of 1080 will be used. If both are set the lower case one takes | |
| 1577 * precedence. | |
| 1578 * | |
| 1579 * [:https_proxy:] and [:HTTPS_PROXY:] specify the proxy server to use for | |
| 1580 * https:// urls. Use the format [:hostname:port:]. If no port is used a | |
| 1581 * default of 1080 will be used. If both are set the lower case one takes | |
| 1582 * precedence. | |
| 1583 * | |
| 1584 * [:no_proxy:] and [:NO_PROXY:] specify a comma separated list of | |
| 1585 * postfixes of hostnames for which not to use the proxy | |
| 1586 * server. E.g. the value "localhost,127.0.0.1" will make requests | |
| 1587 * to both "localhost" and "127.0.0.1" not use a proxy. If both are set | |
| 1588 * the lower case one takes precedence. | |
| 1589 * | |
| 1590 * To activate this way of resolving proxies assign this function to | |
| 1591 * the [findProxy] property on the [HttpClient]. | |
| 1592 * | |
| 1593 * HttpClient client = new HttpClient(); | |
| 1594 * client.findProxy = HttpClient.findProxyFromEnvironment; | |
| 1595 * | |
| 1596 * If you don't want to use the system environment you can use a | |
| 1597 * different one by wrapping the function. | |
| 1598 * | |
| 1599 * HttpClient client = new HttpClient(); | |
| 1600 * client.findProxy = (url) { | |
| 1601 * return HttpClient.findProxyFromEnvironment( | |
| 1602 * url, {"http_proxy": ..., "no_proxy": ...}); | |
| 1603 * } | |
| 1604 * | |
| 1605 * If a proxy requires authentication it is possible to configure | |
| 1606 * the username and password as well. Use the format | |
| 1607 * [:username:password@hostname:port:] to include the username and | |
| 1608 * password. Alternatively the API [addProxyCredentials] can be used | |
| 1609 * to set credentials for proxies which require authentication. | |
| 1610 */ | |
| 1611 static String findProxyFromEnvironment(Uri url, | |
| 1612 {Map<String, String> environment}) { | |
| 1613 return _HttpClient._findProxyFromEnvironment(url, environment); | |
| 1614 } | |
| 1615 | |
| 1616 /** | |
| 1617 * Sets the function to be called when a proxy is requesting | |
| 1618 * authentication. Information on the proxy in use and the security | |
| 1619 * realm for the authentication are passed in the arguments [host], | |
| 1620 * [port] and [realm]. | |
| 1621 * | |
| 1622 * The function returns a [Future] which should complete when the | |
| 1623 * authentication has been resolved. If credentials cannot be | |
| 1624 * provided the [Future] should complete with [:false:]. If | |
| 1625 * credentials are available the function should add these using | |
| 1626 * [addProxyCredentials] before completing the [Future] with the value | |
| 1627 * [:true:]. | |
| 1628 * | |
| 1629 * If the [Future] completes with [:true:] the request will be retried | |
| 1630 * using the updated credentials. Otherwise response processing will | |
| 1631 * continue normally. | |
| 1632 */ | |
| 1633 set authenticateProxy( | |
| 1634 Future<bool> f(String host, int port, String scheme, String realm)); | |
| 1635 | |
| 1636 /** | |
| 1637 * Add credentials to be used for authorizing HTTP proxies. | |
| 1638 */ | |
| 1639 void addProxyCredentials(String host, | |
| 1640 int port, | |
| 1641 String realm, | |
| 1642 HttpClientCredentials credentials); | |
| 1643 | |
| 1644 /** | |
| 1645 * Sets a callback that will decide whether to accept a secure connection | |
| 1646 * with a server certificate that cannot be authenticated by any of our | |
| 1647 * trusted root certificates. | |
| 1648 * | |
| 1649 * When an secure HTTP request if made, using this HttpClient, and the | |
| 1650 * server returns a server certificate that cannot be authenticated, the | |
| 1651 * callback is called asynchronously with the [X509Certificate] object and | |
| 1652 * the server's hostname and port. If the value of [badCertificateCallback] | |
| 1653 * is [:null:], the bad certificate is rejected, as if the callback | |
| 1654 * returned [:false:] | |
| 1655 * | |
| 1656 * If the callback returns true, the secure connection is accepted and the | |
| 1657 * [:Future<HttpClientRequest>:] that was returned from the call making the | |
| 1658 * request completes with a valid HttpRequest object. If the callback returns | |
| 1659 * false, the [:Future<HttpClientRequest>:] completes with an exception. | |
| 1660 * | |
| 1661 * If a bad certificate is received on a connection attempt, the library calls | |
| 1662 * the function that was the value of badCertificateCallback at the time | |
| 1663 * the request is made, even if the value of badCertificateCallback | |
| 1664 * has changed since then. | |
| 1665 */ | |
| 1666 set badCertificateCallback(bool callback(X509Certificate cert, | |
| 1667 String host, | |
| 1668 int port)); | |
| 1669 | |
| 1670 /** | |
| 1671 * Shutdown the HTTP client. If [force] is [:false:] (the default) | |
| 1672 * the [:HttpClient:] will be kept alive until all active | |
| 1673 * connections are done. If [force] is [:true:] any active | |
| 1674 * connections will be closed to immediately release all | |
| 1675 * resources. These closed connections will receive an [:onError:] | |
| 1676 * callback to indicate that the client was shutdown. In both cases | |
| 1677 * trying to establish a new connection after calling [shutdown] | |
| 1678 * will throw an exception. | |
| 1679 */ | |
| 1680 void close({bool force: false}); | |
| 1681 } | |
| 1682 | |
| 1683 | |
| 1684 /** | |
| 1685 * HTTP request for a client connection. | |
| 1686 * | |
| 1687 * To set up a request, set the headers using the headers property | |
| 1688 * provided in this class and write the data to the body of the request. | |
| 1689 * HttpClientRequest is an [IOSink]. Use the methods from IOSink, | |
| 1690 * such as writeCharCode(), to write the body of the HTTP | |
| 1691 * request. When one of the IOSink methods is used for the first | |
| 1692 * time, the request header is sent. Calling any methods that | |
| 1693 * change the header after it is sent throws an exception. | |
| 1694 * | |
| 1695 * When writing string data through the [IOSink] the | |
| 1696 * encoding used is determined from the "charset" parameter of | |
| 1697 * the "Content-Type" header. | |
| 1698 * | |
| 1699 * HttpClientRequest request = ... | |
| 1700 * request.headers.contentType | |
| 1701 * = new ContentType("application", "json", charset: "utf-8"); | |
| 1702 * request.write(...); // Strings written will be UTF-8 encoded. | |
| 1703 * | |
| 1704 * If no charset is provided the default of ISO-8859-1 (Latin 1) is | |
| 1705 * be used. | |
| 1706 * | |
| 1707 * HttpClientRequest request = ... | |
| 1708 * request.headers.add(HttpHeaders.CONTENT_TYPE, "text/plain"); | |
| 1709 * request.write(...); // Strings written will be ISO-8859-1 encoded. | |
| 1710 * | |
| 1711 * An exception is thrown if you use an unsupported encoding and the | |
| 1712 * `write()` method being used takes a string parameter. | |
| 1713 */ | |
| 1714 abstract class HttpClientRequest implements IOSink { | |
| 1715 /** | |
| 1716 * Gets and sets the requested persistent connection state. | |
| 1717 * | |
| 1718 * The default value is [:true:]. | |
| 1719 */ | |
| 1720 bool persistentConnection; | |
| 1721 | |
| 1722 /** | |
| 1723 * Set this property to [:true:] if this request should | |
| 1724 * automatically follow redirects. The default is [:true:]. | |
| 1725 * | |
| 1726 * Automatic redirect will only happen for "GET" and "HEAD" requests | |
| 1727 * and only for the status codes [:HttpHeaders.MOVED_PERMANENTLY:] | |
| 1728 * (301), [:HttpStatus.FOUND:] (302), | |
| 1729 * [:HttpStatus.MOVED_TEMPORARILY:] (302, alias for | |
| 1730 * [:HttpStatus.FOUND:]), [:HttpStatus.SEE_OTHER:] (303) and | |
| 1731 * [:HttpStatus.TEMPORARY_REDIRECT:] (307). For | |
| 1732 * [:HttpStatus.SEE_OTHER:] (303) autmatic redirect will also happen | |
| 1733 * for "POST" requests with the method changed to "GET" when | |
| 1734 * following the redirect. | |
| 1735 * | |
| 1736 * All headers added to the request will be added to the redirection | |
| 1737 * request(s). However, any body send with the request will not be | |
| 1738 * part of the redirection request(s). | |
| 1739 */ | |
| 1740 bool followRedirects; | |
| 1741 | |
| 1742 /** | |
| 1743 * Set this property to the maximum number of redirects to follow | |
| 1744 * when [followRedirects] is [:true:]. If this number is exceeded the | |
| 1745 * [onError] callback will be called with a [RedirectException]. | |
| 1746 * | |
| 1747 * The default value is 5. | |
| 1748 */ | |
| 1749 int maxRedirects; | |
| 1750 | |
| 1751 /** | |
| 1752 * The method of the request. | |
| 1753 */ | |
| 1754 String get method; | |
| 1755 | |
| 1756 /** | |
| 1757 * The uri of the request. | |
| 1758 */ | |
| 1759 Uri get uri; | |
| 1760 | |
| 1761 /** | |
| 1762 * Gets and sets the content length of the request. If the size of | |
| 1763 * the request is not known in advance set content length to -1, | |
| 1764 * which is also the default. | |
| 1765 */ | |
| 1766 int contentLength; | |
| 1767 | |
| 1768 /** | |
| 1769 * Get or set if the [HttpClientRequest] should buffer output. | |
| 1770 * | |
| 1771 * Default value is `true`. | |
| 1772 * | |
| 1773 * __Note__: Disabling buffering of the output can result in very poor | |
| 1774 * performance, when writing many small chunks. | |
| 1775 */ | |
| 1776 bool bufferOutput; | |
| 1777 | |
| 1778 /** | |
| 1779 * Returns the client request headers. | |
| 1780 * | |
| 1781 * The client request headers can be modified until the client | |
| 1782 * request body is written to or closed. After that they become | |
| 1783 * immutable. | |
| 1784 */ | |
| 1785 HttpHeaders get headers; | |
| 1786 | |
| 1787 /** | |
| 1788 * Cookies to present to the server (in the 'cookie' header). | |
| 1789 */ | |
| 1790 List<Cookie> get cookies; | |
| 1791 | |
| 1792 /** | |
| 1793 * A [HttpClientResponse] future that will complete once the response is | |
| 1794 * available. If an error occurs before the response is available, this | |
| 1795 * future will complete with an error. | |
| 1796 */ | |
| 1797 Future<HttpClientResponse> get done; | |
| 1798 | |
| 1799 /** | |
| 1800 * Close the request for input. Returns the value of [done]. | |
| 1801 */ | |
| 1802 Future<HttpClientResponse> close(); | |
| 1803 | |
| 1804 /** | |
| 1805 * Get information about the client connection. Returns [:null:] if the socket | |
| 1806 * is not available. | |
| 1807 */ | |
| 1808 HttpConnectionInfo get connectionInfo; | |
| 1809 } | |
| 1810 | |
| 1811 | |
| 1812 /** | |
| 1813 * HTTP response for a client connection. | |
| 1814 * | |
| 1815 * The body of a [HttpClientResponse] object is a | |
| 1816 * [Stream] of data from the server. Listen to the body to handle | |
| 1817 * the data and be notified when the entire body is received. | |
| 1818 * | |
| 1819 * new HttpClient().get('localhost', 80, '/file.txt') | |
| 1820 * .then((HttpClientRequest request) => request.close()) | |
| 1821 * .then((HttpClientResponse response) { | |
| 1822 * response.transform(UTF8.decoder).listen((contents) { | |
| 1823 * // handle data | |
| 1824 * }); | |
| 1825 * }); | |
| 1826 */ | |
| 1827 abstract class HttpClientResponse implements Stream<List<int>> { | |
| 1828 /** | |
| 1829 * Returns the status code. | |
| 1830 * | |
| 1831 * The status code must be set before the body is written | |
| 1832 * to. Setting the status code after writing to the body will throw | |
| 1833 * a `StateError`. | |
| 1834 */ | |
| 1835 int get statusCode; | |
| 1836 | |
| 1837 /** | |
| 1838 * Returns the reason phrase associated with the status code. | |
| 1839 * | |
| 1840 * The reason phrase must be set before the body is written | |
| 1841 * to. Setting the reason phrase after writing to the body will throw | |
| 1842 * a `StateError`. | |
| 1843 */ | |
| 1844 String get reasonPhrase; | |
| 1845 | |
| 1846 /** | |
| 1847 * Returns the content length of the response body. Returns -1 if the size of | |
| 1848 * the response body is not known in advance. | |
| 1849 * | |
| 1850 * If the content length needs to be set, it must be set before the | |
| 1851 * body is written to. Setting the reason phrase after writing to | |
| 1852 * the body will throw a `StateError`. | |
| 1853 */ | |
| 1854 int get contentLength; | |
| 1855 | |
| 1856 /** | |
| 1857 * Gets the persistent connection state returned by the server. | |
| 1858 * | |
| 1859 * if the persistent connection state needs to be set, it must be | |
| 1860 * set before the body is written to. Setting the reason phrase | |
| 1861 * after writing to the body will throw a `StateError`. | |
| 1862 */ | |
| 1863 bool get persistentConnection; | |
| 1864 | |
| 1865 /** | |
| 1866 * Returns whether the status code is one of the normal redirect | |
| 1867 * codes [HttpStatus.MOVED_PERMANENTLY], [HttpStatus.FOUND], | |
| 1868 * [HttpStatus.MOVED_TEMPORARILY], [HttpStatus.SEE_OTHER] and | |
| 1869 * [HttpStatus.TEMPORARY_REDIRECT]. | |
| 1870 */ | |
| 1871 bool get isRedirect; | |
| 1872 | |
| 1873 /** | |
| 1874 * Returns the series of redirects this connection has been through. The | |
| 1875 * list will be empty if no redirects were followed. [redirects] will be | |
| 1876 * updated both in the case of an automatic and a manual redirect. | |
| 1877 */ | |
| 1878 List<RedirectInfo> get redirects; | |
| 1879 | |
| 1880 /** | |
| 1881 * Redirects this connection to a new URL. The default value for | |
| 1882 * [method] is the method for the current request. The default value | |
| 1883 * for [url] is the value of the [HttpHeaders.LOCATION] header of | |
| 1884 * the current response. All body data must have been read from the | |
| 1885 * current response before calling [redirect]. | |
| 1886 * | |
| 1887 * All headers added to the request will be added to the redirection | |
| 1888 * request. However, any body sent with the request will not be | |
| 1889 * part of the redirection request. | |
| 1890 * | |
| 1891 * If [followLoops] is set to [:true:], redirect will follow the redirect, | |
| 1892 * even if the URL was already visited. The default value is [:false:]. | |
| 1893 * | |
| 1894 * [redirect] will ignore [maxRedirects] and will always perform the redirect. | |
| 1895 */ | |
| 1896 Future<HttpClientResponse> redirect([String method, | |
| 1897 Uri url, | |
| 1898 bool followLoops]); | |
| 1899 | |
| 1900 | |
| 1901 /** | |
| 1902 * Returns the client response headers. | |
| 1903 * | |
| 1904 * The client response headers are immutable. | |
| 1905 */ | |
| 1906 HttpHeaders get headers; | |
| 1907 | |
| 1908 /** | |
| 1909 * Detach the underlying socket from the HTTP client. When the | |
| 1910 * socket is detached the HTTP client will no longer perform any | |
| 1911 * operations on it. | |
| 1912 * | |
| 1913 * This is normally used when a HTTP upgrade is negotiated and the | |
| 1914 * communication should continue with a different protocol. | |
| 1915 */ | |
| 1916 Future<Socket> detachSocket(); | |
| 1917 | |
| 1918 /** | |
| 1919 * Cookies set by the server (from the 'set-cookie' header). | |
| 1920 */ | |
| 1921 List<Cookie> get cookies; | |
| 1922 | |
| 1923 /** | |
| 1924 * Returns the certificate of the HTTPS server providing the response. | |
| 1925 * Returns null if the connection is not a secure TLS or SSL connection. | |
| 1926 */ | |
| 1927 X509Certificate get certificate; | |
| 1928 | |
| 1929 /** | |
| 1930 * Gets information about the client connection. Returns [:null:] if the socke
t | |
| 1931 * is not available. | |
| 1932 */ | |
| 1933 HttpConnectionInfo get connectionInfo; | |
| 1934 } | |
| 1935 | |
| 1936 | |
| 1937 abstract class HttpClientCredentials { } | |
| 1938 | |
| 1939 | |
| 1940 /** | |
| 1941 * Represents credentials for basic authentication. | |
| 1942 */ | |
| 1943 abstract class HttpClientBasicCredentials extends HttpClientCredentials { | |
| 1944 factory HttpClientBasicCredentials(String username, String password) => | |
| 1945 new _HttpClientBasicCredentials(username, password); | |
| 1946 } | |
| 1947 | |
| 1948 | |
| 1949 /** | |
| 1950 * Represents credentials for digest authentication. Digest | |
| 1951 * authentication is only supported for servers using the MD5 | |
| 1952 * algorithm and quality of protection (qop) of either "none" or | |
| 1953 * "auth". | |
| 1954 */ | |
| 1955 abstract class HttpClientDigestCredentials extends HttpClientCredentials { | |
| 1956 factory HttpClientDigestCredentials(String username, String password) => | |
| 1957 new _HttpClientDigestCredentials(username, password); | |
| 1958 } | |
| 1959 | |
| 1960 | |
| 1961 /** | |
| 1962 * Information about an [HttpRequest], [HttpResponse], [HttpClientRequest], or | |
| 1963 * [HttpClientResponse] connection. | |
| 1964 */ | |
| 1965 abstract class HttpConnectionInfo { | |
| 1966 InternetAddress get remoteAddress; | |
| 1967 int get remotePort; | |
| 1968 int get localPort; | |
| 1969 } | |
| 1970 | |
| 1971 | |
| 1972 /** | |
| 1973 * Redirect information. | |
| 1974 */ | |
| 1975 abstract class RedirectInfo { | |
| 1976 /** | |
| 1977 * Returns the status code used for the redirect. | |
| 1978 */ | |
| 1979 int get statusCode; | |
| 1980 | |
| 1981 /** | |
| 1982 * Returns the method used for the redirect. | |
| 1983 */ | |
| 1984 String get method; | |
| 1985 | |
| 1986 /** | |
| 1987 * Returns the location for the redirect. | |
| 1988 */ | |
| 1989 Uri get location; | |
| 1990 } | |
| 1991 | |
| 1992 | |
| 1993 /** | |
| 1994 * When detaching a socket from either the [:HttpServer:] or the | |
| 1995 * [:HttpClient:] due to a HTTP connection upgrade there might be | |
| 1996 * unparsed data already read from the socket. This unparsed data | |
| 1997 * together with the detached socket is returned in an instance of | |
| 1998 * this class. | |
| 1999 */ | |
| 2000 abstract class DetachedSocket { | |
| 2001 Socket get socket; | |
| 2002 List<int> get unparsedData; | |
| 2003 } | |
| 2004 | |
| 2005 | |
| 2006 class HttpException implements IOException { | |
| 2007 final String message; | |
| 2008 final Uri uri; | |
| 2009 | |
| 2010 const HttpException(String this.message, {Uri this.uri}); | |
| 2011 | |
| 2012 String toString() { | |
| 2013 var b = new StringBuffer() | |
| 2014 ..write('HttpException: ') | |
| 2015 ..write(message); | |
| 2016 if (uri != null) { | |
| 2017 b.write(', uri = $uri'); | |
| 2018 } | |
| 2019 return b.toString(); | |
| 2020 } | |
| 2021 } | |
| 2022 | |
| 2023 | |
| 2024 class RedirectException implements HttpException { | |
| 2025 final String message; | |
| 2026 final List<RedirectInfo> redirects; | |
| 2027 | |
| 2028 const RedirectException(this.message, this.redirects); | |
| 2029 | |
| 2030 String toString() => "RedirectException: $message"; | |
| 2031 | |
| 2032 Uri get uri => redirects.last.location; | |
| 2033 } | |
| OLD | NEW |