| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, 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 /** | |
| 6 * HTTP status codes. | |
| 7 */ | |
| 8 abstract class HttpStatus { | |
| 9 static const int CONTINUE = 100; | |
| 10 static const int SWITCHING_PROTOCOLS = 101; | |
| 11 static const int OK = 200; | |
| 12 static const int CREATED = 201; | |
| 13 static const int ACCEPTED = 202; | |
| 14 static const int NON_AUTHORITATIVE_INFORMATION = 203; | |
| 15 static const int NO_CONTENT = 204; | |
| 16 static const int RESET_CONTENT = 205; | |
| 17 static const int PARTIAL_CONTENT = 206; | |
| 18 static const int MULTIPLE_CHOICES = 300; | |
| 19 static const int MOVED_PERMANENTLY = 301; | |
| 20 static const int FOUND = 302; | |
| 21 static const int MOVED_TEMPORARILY = 302; // Common alias for FOUND. | |
| 22 static const int SEE_OTHER = 303; | |
| 23 static const int NOT_MODIFIED = 304; | |
| 24 static const int USE_PROXY = 305; | |
| 25 static const int TEMPORARY_REDIRECT = 307; | |
| 26 static const int BAD_REQUEST = 400; | |
| 27 static const int UNAUTHORIZED = 401; | |
| 28 static const int PAYMENT_REQUIRED = 402; | |
| 29 static const int FORBIDDEN = 403; | |
| 30 static const int NOT_FOUND = 404; | |
| 31 static const int METHOD_NOT_ALLOWED = 405; | |
| 32 static const int NOT_ACCEPTABLE = 406; | |
| 33 static const int PROXY_AUTHENTICATION_REQUIRED = 407; | |
| 34 static const int REQUEST_TIMEOUT = 408; | |
| 35 static const int CONFLICT = 409; | |
| 36 static const int GONE = 410; | |
| 37 static const int LENGTH_REQUIRED = 411; | |
| 38 static const int PRECONDITION_FAILED = 412; | |
| 39 static const int REQUEST_ENTITY_TOO_LARGE = 413; | |
| 40 static const int REQUEST_URI_TOO_LONG = 414; | |
| 41 static const int UNSUPPORTED_MEDIA_TYPE = 415; | |
| 42 static const int REQUESTED_RANGE_NOT_SATISFIABLE = 416; | |
| 43 static const int EXPECTATION_FAILED = 417; | |
| 44 static const int INTERNAL_SERVER_ERROR = 500; | |
| 45 static const int NOT_IMPLEMENTED = 501; | |
| 46 static const int BAD_GATEWAY = 502; | |
| 47 static const int SERVICE_UNAVAILABLE = 503; | |
| 48 static const int GATEWAY_TIMEOUT = 504; | |
| 49 static const int HTTP_VERSION_NOT_SUPPORTED = 505; | |
| 50 // Client generated status code. | |
| 51 static const int NETWORK_CONNECT_TIMEOUT_ERROR = 599; | |
| 52 } | |
| 53 | |
| 54 | |
| 55 /** | |
| 56 * HTTP server. | |
| 57 */ | |
| 58 abstract class HttpServer { | |
| 59 factory HttpServer() => new _HttpServer(); | |
| 60 | |
| 61 /** | |
| 62 * Start listening for HTTP requests on the specified [host] and | |
| 63 * [port]. If a [port] of 0 is specified the server will choose an | |
| 64 * ephemeral port. The optional argument [backlog] can be used to | |
| 65 * specify the listen backlog for the underlying OS listen | |
| 66 * setup. See [addRequestHandler] and [defaultRequestHandler] for | |
| 67 * information on how incoming HTTP requests are handled. | |
| 68 */ | |
| 69 void listen(String host, int port, [int backlog]); | |
| 70 | |
| 71 /** | |
| 72 * Attach the HTTP server to an existing [:ServerSocket:]. If the | |
| 73 * [HttpServer] is closed, the [HttpServer] will just detach itself, | |
| 74 * and not close [serverSocket]. | |
| 75 */ | |
| 76 void listenOn(ServerSocket serverSocket); | |
| 77 | |
| 78 /** | |
| 79 * Adds a request handler to the list of request handlers. The | |
| 80 * function [matcher] is called with the request and must return | |
| 81 * [:true:] if the [handler] should handle the request. The first | |
| 82 * handler for which [matcher] returns [:true:] will be handed the | |
| 83 * request. | |
| 84 */ | |
| 85 addRequestHandler(bool matcher(HttpRequest request), | |
| 86 void handler(HttpRequest request, HttpResponse response)); | |
| 87 | |
| 88 /** | |
| 89 * Sets the default request handler. This request handler will be | |
| 90 * called if none of the request handlers registered by | |
| 91 * [addRequestHandler] matches the current request. If no default | |
| 92 * request handler is set the server will just respond with status | |
| 93 * code [:NOT_FOUND:] (404). | |
| 94 */ | |
| 95 void set defaultRequestHandler( | |
| 96 void handler(HttpRequest request, HttpResponse response)); | |
| 97 | |
| 98 /** | |
| 99 * Stop server listening. | |
| 100 */ | |
| 101 void close(); | |
| 102 | |
| 103 /** | |
| 104 * Returns the port that the server is listening on. This can be | |
| 105 * used to get the actual port used when a value of 0 for [port] is | |
| 106 * specified in the [listen] call. | |
| 107 */ | |
| 108 int get port; | |
| 109 | |
| 110 /** | |
| 111 * Sets the error handler that is called when a connection error occurs. | |
| 112 */ | |
| 113 void set onError(void callback(e)); | |
| 114 | |
| 115 /** | |
| 116 * Set the timeout, in seconds, for sessions of this HTTP server. Default | |
| 117 * is 20 minutes. | |
| 118 */ | |
| 119 int set sessionTimeout(int timeout); | |
| 120 } | |
| 121 | |
| 122 | |
| 123 /** | |
| 124 * Access to the HTTP headers for requests and responses. In some | |
| 125 * situations the headers will be imutable and the mutating methods | |
| 126 * will then throw exceptions. | |
| 127 * | |
| 128 * For all operation on HTTP headers the header name is | |
| 129 * case-insensitive. | |
| 130 */ | |
| 131 abstract class HttpHeaders { | |
| 132 static const ACCEPT = "Accept"; | |
| 133 static const ACCEPT_CHARSET = "Accept-Charset"; | |
| 134 static const ACCEPT_ENCODING = "Accept-Encoding"; | |
| 135 static const ACCEPT_LANGUAGE = "Accept-Language"; | |
| 136 static const ACCEPT_RANGES = "Accept-Ranges"; | |
| 137 static const AGE = "Age"; | |
| 138 static const ALLOW = "Allow"; | |
| 139 static const AUTHORIZATION = "Authorization"; | |
| 140 static const CACHE_CONTROL = "Cache-Control"; | |
| 141 static const CONNECTION = "Connection"; | |
| 142 static const CONTENT_ENCODING = "Content-Encoding"; | |
| 143 static const CONTENT_LANGUAGE = "Content-Language"; | |
| 144 static const CONTENT_LENGTH = "Content-Length"; | |
| 145 static const CONTENT_LOCATION = "Content-Location"; | |
| 146 static const CONTENT_MD5 = "Content-MD5"; | |
| 147 static const CONTENT_RANGE = "Content-Range"; | |
| 148 static const CONTENT_TYPE = "Content-Type"; | |
| 149 static const DATE = "Date"; | |
| 150 static const ETAG = "ETag"; | |
| 151 static const EXPECT = "Expect"; | |
| 152 static const EXPIRES = "Expires"; | |
| 153 static const FROM = "From"; | |
| 154 static const HOST = "Host"; | |
| 155 static const IF_MATCH = "If-Match"; | |
| 156 static const IF_MODIFIED_SINCE = "If-Modified-Since"; | |
| 157 static const IF_NONE_MATCH = "If-None-Match"; | |
| 158 static const IF_RANGE = "If-Range"; | |
| 159 static const IF_UNMODIFIED_SINCE = "If-Unmodified-Since"; | |
| 160 static const LAST_MODIFIED = "Last-Modified"; | |
| 161 static const LOCATION = "Location"; | |
| 162 static const MAX_FORWARDS = "Max-Forwards"; | |
| 163 static const PRAGMA = "Pragma"; | |
| 164 static const PROXY_AUTHENTICATE = "Proxy-Authenticate"; | |
| 165 static const PROXY_AUTHORIZATION = "Proxy-Authorization"; | |
| 166 static const RANGE = "Range"; | |
| 167 static const REFERER = "Referer"; | |
| 168 static const RETRY_AFTER = "Retry-After"; | |
| 169 static const SERVER = "Server"; | |
| 170 static const TE = "TE"; | |
| 171 static const TRAILER = "Trailer"; | |
| 172 static const TRANSFER_ENCODING = "Transfer-Encoding"; | |
| 173 static const UPGRADE = "Upgrade"; | |
| 174 static const USER_AGENT = "User-Agent"; | |
| 175 static const VARY = "Vary"; | |
| 176 static const VIA = "Via"; | |
| 177 static const WARNING = "Warning"; | |
| 178 static const WWW_AUTHENTICATE = "WWW-Authenticate"; | |
| 179 | |
| 180 static const GENERAL_HEADERS = const [CACHE_CONTROL, | |
| 181 CONNECTION, | |
| 182 DATE, | |
| 183 PRAGMA, | |
| 184 TRAILER, | |
| 185 TRANSFER_ENCODING, | |
| 186 UPGRADE, | |
| 187 VIA, | |
| 188 WARNING]; | |
| 189 | |
| 190 static const ENTITY_HEADERS = const [ALLOW, | |
| 191 CONTENT_ENCODING, | |
| 192 CONTENT_LANGUAGE, | |
| 193 CONTENT_LENGTH, | |
| 194 CONTENT_LOCATION, | |
| 195 CONTENT_MD5, | |
| 196 CONTENT_RANGE, | |
| 197 CONTENT_TYPE, | |
| 198 EXPIRES, | |
| 199 LAST_MODIFIED]; | |
| 200 | |
| 201 | |
| 202 static const RESPONSE_HEADERS = const [ACCEPT_RANGES, | |
| 203 AGE, | |
| 204 ETAG, | |
| 205 LOCATION, | |
| 206 PROXY_AUTHENTICATE, | |
| 207 RETRY_AFTER, | |
| 208 SERVER, | |
| 209 VARY, | |
| 210 WWW_AUTHENTICATE]; | |
| 211 | |
| 212 static const REQUEST_HEADERS = const [ACCEPT, | |
| 213 ACCEPT_CHARSET, | |
| 214 ACCEPT_ENCODING, | |
| 215 ACCEPT_LANGUAGE, | |
| 216 AUTHORIZATION, | |
| 217 EXPECT, | |
| 218 FROM, | |
| 219 HOST, | |
| 220 IF_MATCH, | |
| 221 IF_MODIFIED_SINCE, | |
| 222 IF_NONE_MATCH, | |
| 223 IF_RANGE, | |
| 224 IF_UNMODIFIED_SINCE, | |
| 225 MAX_FORWARDS, | |
| 226 PROXY_AUTHORIZATION, | |
| 227 RANGE, | |
| 228 REFERER, | |
| 229 TE, | |
| 230 USER_AGENT]; | |
| 231 | |
| 232 /** | |
| 233 * Returns the list of values for the header named [name]. If there | |
| 234 * is no headers with the provided name [:null:] will be returned. | |
| 235 */ | |
| 236 List<String> operator[](String name); | |
| 237 | |
| 238 /** | |
| 239 * Convenience method for the value for a single values header. If | |
| 240 * there is no header with the provided name [:null:] will be | |
| 241 * returned. If the header has more than one value an exception is | |
| 242 * thrown. | |
| 243 */ | |
| 244 String value(String name); | |
| 245 | |
| 246 /** | |
| 247 * Adds a header value. The header named [name] will have the value | |
| 248 * [value] added to its list of values. Some headers are single | |
| 249 * values and for these adding a value will replace the previous | |
| 250 * value. If the value is of type Date a HTTP date format will be | |
| 251 * applied. If the value is a [:List:] each element of the list will | |
| 252 * be added separately. For all other types the default [:toString:] | |
| 253 * method will be used. | |
| 254 */ | |
| 255 void add(String name, Object value); | |
| 256 | |
| 257 /** | |
| 258 * Sets a header. The header named [name] will have all its values | |
| 259 * cleared before the value [value] is added as its value. | |
| 260 */ | |
| 261 void set(String name, Object value); | |
| 262 | |
| 263 /** | |
| 264 * Removes a specific value for a header name. Some headers have | |
| 265 * system supplied values and for these the system supplied values | |
| 266 * will still be added to the collection of values for the header. | |
| 267 */ | |
| 268 void remove(String name, Object value); | |
| 269 | |
| 270 /** | |
| 271 * Remove all values for the specified header name. Some headers | |
| 272 * have system supplied values and for these the system supplied | |
| 273 * values will still be added to the collection of values for the | |
| 274 * header. | |
| 275 */ | |
| 276 void removeAll(String name); | |
| 277 | |
| 278 /** | |
| 279 * Enumerate the headers applying the function [f] to each | |
| 280 * header. The header name passed in [name] will be all lower | |
| 281 * case. | |
| 282 */ | |
| 283 void forEach(void f(String name, List<String> values)); | |
| 284 | |
| 285 /** | |
| 286 * Disable folding for the header named [name] when sending the HTTP | |
| 287 * header. By default, multiple header values are folded into a | |
| 288 * single header line by separating the values with commas. The | |
| 289 * Set-Cookie header has folding disabled by default. | |
| 290 */ | |
| 291 void noFolding(String name); | |
| 292 | |
| 293 /** | |
| 294 * Gets and sets the date. The value of this property will | |
| 295 * reflect the "Date" header. | |
| 296 */ | |
| 297 Date date; | |
| 298 | |
| 299 /** | |
| 300 * Gets and sets the expiry date. The value of this property will | |
| 301 * reflect the "Expires" header. | |
| 302 */ | |
| 303 Date expires; | |
| 304 | |
| 305 /** | |
| 306 * Gets and sets the 'if-modified-since' date. The value of this property will | |
| 307 * reflect the "if-modified-since" header. | |
| 308 */ | |
| 309 Date ifModifiedSince; | |
| 310 | |
| 311 /** | |
| 312 * Gets and sets the host part of the "Host" header for the | |
| 313 * connection. | |
| 314 */ | |
| 315 String host; | |
| 316 | |
| 317 /** | |
| 318 * Gets and sets the port part of the "Host" header for the | |
| 319 * connection. | |
| 320 */ | |
| 321 int port; | |
| 322 | |
| 323 /** | |
| 324 * Gets and sets the content type. Note that the content type in the | |
| 325 * header will only be updated if this field is set | |
| 326 * directly. Mutating the returned current value will have no | |
| 327 * effect. | |
| 328 */ | |
| 329 ContentType contentType; | |
| 330 } | |
| 331 | |
| 332 | |
| 333 /** | |
| 334 * Representation of a header value in the form: | |
| 335 * | |
| 336 * [:value; parameter1=value1; parameter2=value2:] | |
| 337 * | |
| 338 * [HeaderValue] can be used to conveniently build and parse header | |
| 339 * values on this form. | |
| 340 * | |
| 341 * To build an [:Accepts:] header with the value | |
| 342 * | |
| 343 * text/plain; q=0.3, text/html | |
| 344 * | |
| 345 * use code like this: | |
| 346 * | |
| 347 * HttpClientRequest request = ...; | |
| 348 * var v = new HeaderValue(); | |
| 349 * v.value = "text/plain"; | |
| 350 * v.parameters["q"] = "0.3" | |
| 351 * request.headers.add(HttpHeaders.ACCEPT, v); | |
| 352 * request.headers.add(HttpHeaders.ACCEPT, "text/html"); | |
| 353 * | |
| 354 * To parse the header values use the [:fromString:] constructor. | |
| 355 * | |
| 356 * HttpRequest request = ...; | |
| 357 * List<String> values = request.headers[HttpHeaders.ACCEPT]; | |
| 358 * values.forEach((value) { | |
| 359 * HeaderValue v = new HeaderValue.fromString(value); | |
| 360 * // Use v.value and v.parameters | |
| 361 * }); | |
| 362 */ | |
| 363 abstract class HeaderValue { | |
| 364 /** | |
| 365 * Creates a new header value object setting the value part. | |
| 366 */ | |
| 367 factory HeaderValue([String value = ""]) => new _HeaderValue(value); | |
| 368 | |
| 369 /** | |
| 370 * Creates a new header value object from parsing a header value | |
| 371 * string with both value and optional parameters. | |
| 372 */ | |
| 373 factory HeaderValue.fromString(String value, | |
| 374 {String parameterSeparator: ";"}) { | |
| 375 return new _HeaderValue.fromString( | |
| 376 value, parameterSeparator: parameterSeparator); | |
| 377 } | |
| 378 | |
| 379 /** | |
| 380 * Gets and sets the header value. | |
| 381 */ | |
| 382 String value; | |
| 383 | |
| 384 /** | |
| 385 * Gets the map of parameters. | |
| 386 */ | |
| 387 Map<String, String> get parameters; | |
| 388 | |
| 389 /** | |
| 390 * Returns the formatted string representation in the form: | |
| 391 * | |
| 392 * value; parameter1=value1; parameter2=value2 | |
| 393 */ | |
| 394 String toString(); | |
| 395 } | |
| 396 | |
| 397 abstract class HttpSession { | |
| 398 /** | |
| 399 * Get the id for the current session. | |
| 400 */ | |
| 401 String get id; | |
| 402 | |
| 403 /** | |
| 404 * Access the user-data associated with the session. | |
| 405 */ | |
| 406 Dynamic data; | |
| 407 | |
| 408 /** | |
| 409 * Destroy the session. This will terminate the session and any further | |
| 410 * connections with this id will be given a new id and session. | |
| 411 */ | |
| 412 void destroy(); | |
| 413 | |
| 414 /** | |
| 415 * Set a callback that will be called when the session is timed out. | |
| 416 */ | |
| 417 void set onTimeout(void callback()); | |
| 418 } | |
| 419 | |
| 420 | |
| 421 /** | |
| 422 * Representation of a content type. | |
| 423 */ | |
| 424 abstract class ContentType implements HeaderValue { | |
| 425 /** | |
| 426 * Creates a new content type object setting the primary type and | |
| 427 * sub type. | |
| 428 */ | |
| 429 factory ContentType([String primaryType = "", String subType = ""]) { | |
| 430 return new _ContentType(primaryType, subType); | |
| 431 } | |
| 432 | |
| 433 /** | |
| 434 * Creates a new content type object from parsing a Content-Type | |
| 435 * header value. As primary type, sub type and parameter names and | |
| 436 * values are not case sensitive all these values will be converted | |
| 437 * to lower case. Parsing this string | |
| 438 * | |
| 439 * text/html; charset=utf-8 | |
| 440 * | |
| 441 * will create a content type object with primary type [:text:], sub | |
| 442 * type [:html:] and parameter [:charset:] with value [:utf-8:]. | |
| 443 */ | |
| 444 factory ContentType.fromString(String value) { | |
| 445 return new _ContentType.fromString(value); | |
| 446 } | |
| 447 | |
| 448 /** | |
| 449 * Gets and sets the content type in the form "primaryType/subType". | |
| 450 */ | |
| 451 String value; | |
| 452 | |
| 453 /** | |
| 454 * Gets and sets the primary type. | |
| 455 */ | |
| 456 String primaryType; | |
| 457 | |
| 458 /** | |
| 459 * Gets and sets the sub type. | |
| 460 */ | |
| 461 String subType; | |
| 462 | |
| 463 /** | |
| 464 * Gets and sets the character set. | |
| 465 */ | |
| 466 String charset; | |
| 467 } | |
| 468 | |
| 469 | |
| 470 /** | |
| 471 * Representation of a cookie. For cookies received by the server as | |
| 472 * Cookie header values only [:name:] and [:value:] fields will be | |
| 473 * set. When building a cookie for the Set-Cookie header in the server | |
| 474 * and when receiving cookies in the client as Set-Cookie headers all | |
| 475 * fields can be used. | |
| 476 */ | |
| 477 abstract class Cookie { | |
| 478 /** | |
| 479 * Creates a new cookie optionally setting the name and value. | |
| 480 */ | |
| 481 factory Cookie([String name, String value]) => new _Cookie(name, value); | |
| 482 | |
| 483 /** | |
| 484 * Creates a new cookie by parsing a header value from a Set-Cookie | |
| 485 * header. | |
| 486 */ | |
| 487 factory Cookie.fromSetCookieValue(String value) { | |
| 488 return new _Cookie.fromSetCookieValue(value); | |
| 489 } | |
| 490 | |
| 491 /** | |
| 492 * Gets and sets the name. | |
| 493 */ | |
| 494 String name; | |
| 495 | |
| 496 /** | |
| 497 * Gets and sets the value. | |
| 498 */ | |
| 499 String value; | |
| 500 | |
| 501 /** | |
| 502 * Gets and sets the expiry date. | |
| 503 */ | |
| 504 Date expires; | |
| 505 | |
| 506 /** | |
| 507 * Gets and sets the max age. A value of [:0:] means delete cookie | |
| 508 * now. | |
| 509 */ | |
| 510 int maxAge; | |
| 511 | |
| 512 /** | |
| 513 * Gets and sets the domain. | |
| 514 */ | |
| 515 String domain; | |
| 516 | |
| 517 /** | |
| 518 * Gets and sets the path. | |
| 519 */ | |
| 520 String path; | |
| 521 | |
| 522 /** | |
| 523 * Gets and sets whether this cookie is secure. | |
| 524 */ | |
| 525 bool secure; | |
| 526 | |
| 527 /** | |
| 528 * Gets and sets whether this cookie is HTTP only. | |
| 529 */ | |
| 530 bool httpOnly; | |
| 531 | |
| 532 /** | |
| 533 * Returns the formatted string representation of the cookie. The | |
| 534 * string representation can be used for for setting the Cookie or | |
| 535 * Set-Cookie headers | |
| 536 */ | |
| 537 String toString(); | |
| 538 } | |
| 539 | |
| 540 | |
| 541 /** | |
| 542 * Http request delivered to the HTTP server callback. | |
| 543 */ | |
| 544 abstract class HttpRequest { | |
| 545 /** | |
| 546 * Returns the content length of the request body. If the size of | |
| 547 * the request body is not known in advance this -1. | |
| 548 */ | |
| 549 int get contentLength; | |
| 550 | |
| 551 /** | |
| 552 * Returns the persistent connection state signaled by the client. | |
| 553 */ | |
| 554 bool get persistentConnection; | |
| 555 | |
| 556 /** | |
| 557 * Returns the method for the request. | |
| 558 */ | |
| 559 String get method; | |
| 560 | |
| 561 /** | |
| 562 * Returns the URI for the request. | |
| 563 */ | |
| 564 String get uri; | |
| 565 | |
| 566 /** | |
| 567 * Returns the path part of the URI. | |
| 568 */ | |
| 569 String get path; | |
| 570 | |
| 571 /** | |
| 572 * Returns the query string. | |
| 573 */ | |
| 574 String get queryString; | |
| 575 | |
| 576 /** | |
| 577 * Returns the parsed query string. | |
| 578 */ | |
| 579 Map<String, String> get queryParameters; | |
| 580 | |
| 581 /** | |
| 582 * Returns the request headers. | |
| 583 */ | |
| 584 HttpHeaders get headers; | |
| 585 | |
| 586 /** | |
| 587 * Returns the cookies in the request (from the Cookie headers). | |
| 588 */ | |
| 589 List<Cookie> get cookies; | |
| 590 | |
| 591 /** | |
| 592 * Returns, or initialize, a session for the given request. If the session is | |
| 593 * being initialized by this call, [init] will be called with the | |
| 594 * newly create session. Here the [:HttpSession.data:] field can be set, if | |
| 595 * needed. | |
| 596 * See [:HttpServer.sessionTimeout:] on how to change default timeout. | |
| 597 */ | |
| 598 HttpSession session([init(HttpSession session)]); | |
| 599 | |
| 600 /** | |
| 601 * Returns the input stream for the request. This is used to read | |
| 602 * the request data. | |
| 603 */ | |
| 604 InputStream get inputStream; | |
| 605 | |
| 606 /** | |
| 607 * Returns the HTTP protocol version used in the request. This will | |
| 608 * be "1.0" or "1.1". | |
| 609 */ | |
| 610 String get protocolVersion; | |
| 611 | |
| 612 /** | |
| 613 * Get information about the client connection. Returns [null] if the socket | |
| 614 * isn't available. | |
| 615 */ | |
| 616 HttpConnectionInfo get connectionInfo; | |
| 617 } | |
| 618 | |
| 619 | |
| 620 /** | |
| 621 * HTTP response to be send back to the client. | |
| 622 */ | |
| 623 abstract class HttpResponse { | |
| 624 /** | |
| 625 * Gets and sets the content length of the response. If the size of | |
| 626 * the response is not known in advance set the content length to | |
| 627 * -1 - which is also the default if not set. | |
| 628 */ | |
| 629 int contentLength; | |
| 630 | |
| 631 /** | |
| 632 * Gets and sets the status code. Any integer value is accepted. For | |
| 633 * the official HTTP status codes use the fields from | |
| 634 * [HttpStatus]. If no status code is explicitly set the default | |
| 635 * value [HttpStatus.OK] is used. | |
| 636 */ | |
| 637 int statusCode; | |
| 638 | |
| 639 /** | |
| 640 * Gets and sets the reason phrase. If no reason phrase is explicitly | |
| 641 * set a default reason phrase is provided. | |
| 642 */ | |
| 643 String reasonPhrase; | |
| 644 | |
| 645 /** | |
| 646 * Gets and sets the persistent connection state. The initial value | |
| 647 * of this property is the persistent connection state from the | |
| 648 * request. | |
| 649 */ | |
| 650 bool persistentConnection; | |
| 651 | |
| 652 /** | |
| 653 * Returns the response headers. | |
| 654 */ | |
| 655 HttpHeaders get headers; | |
| 656 | |
| 657 /** | |
| 658 * Cookies to set in the client (in the Set-Cookie header). | |
| 659 */ | |
| 660 List<Cookie> get cookies; | |
| 661 | |
| 662 /** | |
| 663 * Returns the output stream for the response. This is used to write | |
| 664 * the response data. When all response data has been written close | |
| 665 * the stream to indicate the end of the response. | |
| 666 * | |
| 667 * When this is accessed for the first time the response header is | |
| 668 * send. Calling any methods that will change the header after | |
| 669 * having retrieved the output stream will throw an exception. | |
| 670 */ | |
| 671 OutputStream get outputStream; | |
| 672 | |
| 673 /** | |
| 674 * Detach the underlying socket from the HTTP server. When the | |
| 675 * socket is detached the HTTP server will no longer perform any | |
| 676 * operations on it. | |
| 677 * | |
| 678 * This is normally used when a HTTP upgrade request is received | |
| 679 * and the communication should continue with a different protocol. | |
| 680 */ | |
| 681 DetachedSocket detachSocket(); | |
| 682 | |
| 683 /** | |
| 684 * Get information about the client connection. Returns [null] if the socket | |
| 685 * isn't available. | |
| 686 */ | |
| 687 HttpConnectionInfo get connectionInfo; | |
| 688 } | |
| 689 | |
| 690 | |
| 691 /** | |
| 692 * HTTP client factory. The [HttpClient] handles all the sockets associated | |
| 693 * with the [HttpClientConnection]s and when the endpoint supports it, it will | |
| 694 * try to reuse opened sockets for several requests to support HTTP 1.1 | |
| 695 * persistent connections. This means that sockets will be kept open for some | |
| 696 * time after a requests have completed, unless HTTP procedures indicate that it | |
| 697 * must be closed as part of completing the request. Use [:HttpClient.shutdown:] | |
| 698 * to force close the idle sockets. | |
| 699 */ | |
| 700 abstract class HttpClient { | |
| 701 static const int DEFAULT_HTTP_PORT = 80; | |
| 702 | |
| 703 factory HttpClient() => new _HttpClient(); | |
| 704 | |
| 705 /** | |
| 706 * Opens a HTTP connection. The returned [HttpClientConnection] is | |
| 707 * used to register callbacks for asynchronous events on the HTTP | |
| 708 * connection. The "Host" header for the request will be set to the | |
| 709 * value [host]:[port]. This can be overridden through the | |
| 710 * HttpClientRequest interface before the request is sent. NOTE if | |
| 711 * [host] is an IP address this will still be set in the "Host" | |
| 712 * header. | |
| 713 */ | |
| 714 HttpClientConnection open(String method, String host, int port, String path); | |
| 715 | |
| 716 /** | |
| 717 * Opens a HTTP connection. The returned [HttpClientConnection] is | |
| 718 * used to register callbacks for asynchronous events on the HTTP | |
| 719 * connection. The "Host" header for the request will be set based | |
| 720 * the host and port specified in [url]. This can be overridden | |
| 721 * through the HttpClientRequest interface before the request is | |
| 722 * sent. NOTE if the host is specified as an IP address this will | |
| 723 * still be set in the "Host" header. | |
| 724 */ | |
| 725 HttpClientConnection openUrl(String method, Uri url); | |
| 726 | |
| 727 /** | |
| 728 * Opens a HTTP connection using the GET method. See [open] for | |
| 729 * details. Using this method to open a HTTP connection will set the | |
| 730 * content length to 0. | |
| 731 */ | |
| 732 HttpClientConnection get(String host, int port, String path); | |
| 733 | |
| 734 /** | |
| 735 * Opens a HTTP connection using the GET method. See [openUrl] for | |
| 736 * details. Using this method to open a HTTP connection will set the | |
| 737 * content length to 0. | |
| 738 */ | |
| 739 HttpClientConnection getUrl(Uri url); | |
| 740 | |
| 741 /** | |
| 742 * Opens a HTTP connection using the POST method. See [open] for details. | |
| 743 */ | |
| 744 HttpClientConnection post(String host, int port, String path); | |
| 745 | |
| 746 /** | |
| 747 * Opens a HTTP connection using the POST method. See [openUrl] for details. | |
| 748 */ | |
| 749 HttpClientConnection postUrl(Uri url); | |
| 750 | |
| 751 /** | |
| 752 * Sets the function to be called when a site is requesting | |
| 753 * authentication. The URL requested and the security realm from the | |
| 754 * server are passed in the arguments [url] and [realm]. | |
| 755 * | |
| 756 * The function returns a [Future] which should complete when the | |
| 757 * authentication has been resolved. If credentials cannot be | |
| 758 * provided the [Future] should complete with [false]. If | |
| 759 * credentials are available the function should add these using | |
| 760 * [addCredentials] before completing the [Future] with the value | |
| 761 * [true]. | |
| 762 * | |
| 763 * If the [Future] completes with true the request will be retried | |
| 764 * using the updated credentials. Otherwise response processing will | |
| 765 * continue normally. | |
| 766 */ | |
| 767 set authenticate(Future<bool> f(Uri url, String scheme, String realm)); | |
| 768 | |
| 769 /** | |
| 770 * Add credentials to be used for authorizing HTTP requests. | |
| 771 */ | |
| 772 void addCredentials(Uri url, String realm, HttpClientCredentials credentials); | |
| 773 | |
| 774 /** | |
| 775 * Sets the function used to resolve the proxy server to be used for | |
| 776 * opening a HTTP connection to the specified [url]. If this | |
| 777 * function is not set, direct connections will always be used. | |
| 778 * | |
| 779 * The string returned by [f] must be in the format used by browser | |
| 780 * PAC (proxy auto-config) scripts. That is either | |
| 781 * | |
| 782 * "DIRECT" | |
| 783 * | |
| 784 * for using a direct connection or | |
| 785 * | |
| 786 * "PROXY host:port" | |
| 787 * | |
| 788 * for using the proxy server [:host:] on port [:port:]. | |
| 789 * | |
| 790 * A configuration can contain several configuration elements | |
| 791 * separated by semicolons, e.g. | |
| 792 * | |
| 793 * "PROXY host:port; PROXY host2:port2; DIRECT" | |
| 794 */ | |
| 795 set findProxy(String f(Uri url)); | |
| 796 | |
| 797 /** | |
| 798 * Shutdown the HTTP client releasing all resources. | |
| 799 */ | |
| 800 void shutdown(); | |
| 801 } | |
| 802 | |
| 803 | |
| 804 /** | |
| 805 * A [HttpClientConnection] is returned by all [HttpClient] methods | |
| 806 * that initiate a connection to an HTTP server. The handlers will be | |
| 807 * called as the connection state progresses. | |
| 808 * | |
| 809 * The setting of all handlers is optional. If [onRequest] is not set | |
| 810 * the request will be send without any additional headers and an | |
| 811 * empty body. If [onResponse] is not set the response will be read | |
| 812 * and discarded. | |
| 813 */ | |
| 814 abstract class HttpClientConnection { | |
| 815 /** | |
| 816 * Sets the handler that is called when the connection is established. | |
| 817 */ | |
| 818 void set onRequest(void callback(HttpClientRequest request)); | |
| 819 | |
| 820 /** | |
| 821 * Sets callback to be called when the request has been send and | |
| 822 * the response is ready for processing. The callback is called when | |
| 823 * all headers of the response are received and data is ready to be | |
| 824 * received. | |
| 825 */ | |
| 826 void set onResponse(void callback(HttpClientResponse response)); | |
| 827 | |
| 828 /** | |
| 829 * Sets the handler that gets called if an error occurs while | |
| 830 * connecting or processing the HTTP request. | |
| 831 */ | |
| 832 void set onError(void callback(e)); | |
| 833 | |
| 834 /** | |
| 835 * Set this property to [:true:] if this connection should | |
| 836 * automatically follow redirects. The default is [:true:]. | |
| 837 */ | |
| 838 bool followRedirects; | |
| 839 | |
| 840 /** | |
| 841 * Set this property to the maximum number of redirects to follow | |
| 842 * when [followRedirects] is [:true:]. If this number is exceeded the | |
| 843 * [onError] callback will be called with a [RedirectLimitExceeded] | |
| 844 * exception. The default value is 5. | |
| 845 */ | |
| 846 int maxRedirects; | |
| 847 | |
| 848 /** | |
| 849 * Returns the series of redirects this connection has been through. | |
| 850 */ | |
| 851 List<RedirectInfo> get redirects; | |
| 852 | |
| 853 /** | |
| 854 * Redirect this connection to a new URL. The default value for | |
| 855 * [method] is the method for the current request. The default value | |
| 856 * for [url] is the value of the [:HttpStatus.LOCATION:] header of | |
| 857 * the current response. All body data must have been read from the | |
| 858 * current response before calling [redirect]. | |
| 859 */ | |
| 860 void redirect([String method, Uri url]); | |
| 861 | |
| 862 /** | |
| 863 * Detach the underlying socket from the HTTP client. When the | |
| 864 * socket is detached the HTTP client will no longer perform any | |
| 865 * operations on it. | |
| 866 * | |
| 867 * This is normally used when a HTTP upgrade is negotiated and the | |
| 868 * communication should continue with a different protocol. | |
| 869 */ | |
| 870 DetachedSocket detachSocket(); | |
| 871 | |
| 872 /** | |
| 873 * Get information about the client connection. Returns [null] if the socket | |
| 874 * isn't available. | |
| 875 */ | |
| 876 HttpConnectionInfo get connectionInfo; | |
| 877 } | |
| 878 | |
| 879 | |
| 880 /** | |
| 881 * HTTP request for a client connection. | |
| 882 */ | |
| 883 abstract class HttpClientRequest { | |
| 884 /** | |
| 885 * Gets and sets the content length of the request. If the size of | |
| 886 * the request is not known in advance set content length to -1, | |
| 887 * which is also the default. | |
| 888 */ | |
| 889 int contentLength; | |
| 890 | |
| 891 /** | |
| 892 * Returns the request headers. | |
| 893 */ | |
| 894 HttpHeaders get headers; | |
| 895 | |
| 896 /** | |
| 897 * Cookies to present to the server (in the Cookie header). | |
| 898 */ | |
| 899 List<Cookie> get cookies; | |
| 900 | |
| 901 /** | |
| 902 * Gets and sets the requested persistent connection state. | |
| 903 * The default value is [:true:]. | |
| 904 */ | |
| 905 bool persistentConnection; | |
| 906 | |
| 907 /** | |
| 908 * Returns the output stream for the request. This is used to write | |
| 909 * the request data. When all request data has been written close | |
| 910 * the stream to indicate the end of the request. | |
| 911 * | |
| 912 * When this is accessed for the first time the request header is | |
| 913 * send. Calling any methods that will change the header after | |
| 914 * having retrieved the output stream will throw an exception. | |
| 915 */ | |
| 916 OutputStream get outputStream; | |
| 917 } | |
| 918 | |
| 919 | |
| 920 /** | |
| 921 * HTTP response for a client connection. | |
| 922 */ | |
| 923 abstract class HttpClientResponse { | |
| 924 /** | |
| 925 * Returns the status code. | |
| 926 */ | |
| 927 int get statusCode; | |
| 928 | |
| 929 /** | |
| 930 * Returns the reason phrase associated with the status code. | |
| 931 */ | |
| 932 String get reasonPhrase; | |
| 933 | |
| 934 /** | |
| 935 * Returns the content length of the request body. If the size of | |
| 936 * the request body is not known in advance this -1. | |
| 937 */ | |
| 938 int get contentLength; | |
| 939 | |
| 940 /** | |
| 941 * Gets the persistent connection state returned by the server. | |
| 942 */ | |
| 943 bool get persistentConnection; | |
| 944 | |
| 945 /** | |
| 946 * Returns whether the status code is one of the normal redirect | |
| 947 * codes [:HttpStatus.MOVED_PERMANENTLY:], [:HttpStatus.FOUND:], | |
| 948 * [:HttpStatus.MOVED_TEMPORARILY:], [:HttpStatus.SEE_OTHER:] and | |
| 949 * [:HttpStatus.TEMPORARY_REDIRECT:]. | |
| 950 */ | |
| 951 bool get isRedirect; | |
| 952 | |
| 953 /** | |
| 954 * Returns the response headers. | |
| 955 */ | |
| 956 HttpHeaders get headers; | |
| 957 | |
| 958 /** | |
| 959 * Cookies set by the server (from the Set-Cookie header). | |
| 960 */ | |
| 961 List<Cookie> get cookies; | |
| 962 | |
| 963 /** | |
| 964 * Returns the input stream for the response. This is used to read | |
| 965 * the response data. | |
| 966 */ | |
| 967 InputStream get inputStream; | |
| 968 } | |
| 969 | |
| 970 | |
| 971 abstract class HttpClientCredentials { } | |
| 972 | |
| 973 | |
| 974 /** | |
| 975 * Represent credentials for basic authentication. | |
| 976 */ | |
| 977 abstract class HttpClientBasicCredentials extends HttpClientCredentials { | |
| 978 factory HttpClientBasicCredentials(String username, String password) => | |
| 979 new _HttpClientBasicCredentials(username, password); | |
| 980 } | |
| 981 | |
| 982 | |
| 983 /** | |
| 984 * Represent credentials for digest authentication. | |
| 985 */ | |
| 986 abstract class HttpClientDigestCredentials extends HttpClientCredentials { | |
| 987 factory HttpClientDigestCredentials(String username, String password) => | |
| 988 new _HttpClientDigestCredentials(username, password); | |
| 989 } | |
| 990 | |
| 991 | |
| 992 /** | |
| 993 * Connection information. | |
| 994 */ | |
| 995 abstract class HttpConnectionInfo { | |
| 996 String get remoteHost; | |
| 997 int get remotePort; | |
| 998 int get localPort; | |
| 999 } | |
| 1000 | |
| 1001 | |
| 1002 /** | |
| 1003 * Redirect information. | |
| 1004 */ | |
| 1005 abstract class RedirectInfo { | |
| 1006 /** | |
| 1007 * Returns the status code used for the redirect. | |
| 1008 */ | |
| 1009 int get statusCode; | |
| 1010 | |
| 1011 /** | |
| 1012 * Returns the method used for the redirect. | |
| 1013 */ | |
| 1014 String get method; | |
| 1015 | |
| 1016 /** | |
| 1017 * Returns the location for the redirect. | |
| 1018 */ | |
| 1019 Uri get location; | |
| 1020 } | |
| 1021 | |
| 1022 | |
| 1023 /** | |
| 1024 * When detaching a socket from either the [:HttpServer:] or the | |
| 1025 * [:HttpClient:] due to a HTTP connection upgrade there might be | |
| 1026 * unparsed data already read from the socket. This unparsed data | |
| 1027 * together with the detached socket is returned in an instance of | |
| 1028 * this class. | |
| 1029 */ | |
| 1030 abstract class DetachedSocket { | |
| 1031 Socket get socket; | |
| 1032 List<int> get unparsedData; | |
| 1033 } | |
| 1034 | |
| 1035 | |
| 1036 class HttpException implements Exception { | |
| 1037 const HttpException([String this.message = ""]); | |
| 1038 String toString() => "HttpException: $message"; | |
| 1039 final String message; | |
| 1040 } | |
| 1041 | |
| 1042 | |
| 1043 class RedirectException extends HttpException { | |
| 1044 const RedirectException(String message, | |
| 1045 List<RedirectInfo> this.redirects) : super(message); | |
| 1046 final List<RedirectInfo> redirects; | |
| 1047 } | |
| 1048 | |
| 1049 | |
| 1050 class RedirectLimitExceededException extends RedirectException { | |
| 1051 const RedirectLimitExceededException(List<RedirectInfo> redirects) | |
| 1052 : super("Redirect limit exceeded", redirects); | |
| 1053 } | |
| 1054 | |
| 1055 | |
| 1056 class RedirectLoopException extends RedirectException { | |
| 1057 const RedirectLoopException(List<RedirectInfo> redirects) | |
| 1058 : super("Redirect loop detected", redirects); | |
| 1059 } | |
| OLD | NEW |