Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * HTTP status codes. | 6 * HTTP status codes. |
| 7 */ | 7 */ |
| 8 interface HttpStatus { | 8 interface HttpStatus { |
| 9 static final int CONTINUE = 100; | 9 static final int CONTINUE = 100; |
| 10 static final int SWITCHING_PROTOCOLS = 101; | 10 static final int SWITCHING_PROTOCOLS = 101; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 void set onRequest(void callback(HttpRequest, HttpResponse)); | 93 void set onRequest(void callback(HttpRequest, HttpResponse)); |
| 94 | 94 |
| 95 /** | 95 /** |
| 96 * Sets the error handler that is called when a connection error occurs. | 96 * Sets the error handler that is called when a connection error occurs. |
| 97 */ | 97 */ |
| 98 void set onError(void callback(Exception e)); | 98 void set onError(void callback(Exception e)); |
| 99 } | 99 } |
| 100 | 100 |
| 101 | 101 |
| 102 /** | 102 /** |
| 103 * Access to the HTTP headers for requests and responses. In some | |
| 104 * situations the headers will be imutable and the mutating methods | |
| 105 * will then throw exceptions. | |
| 106 * | |
| 107 * For all operation on HTTP headers the header name is | |
| 108 * case-insensitive. | |
| 109 */ | |
| 110 interface HttpHeaders default _HttpHeaders { | |
|
Anders Johnsen
2012/04/13 11:47:53
Very neat, I like how you expose all normal header
Søren Gjesse
2012/04/16 11:37:16
They should, but maybe they should be part of the
| |
| 111 static final ACCEPT = "Accept"; | |
| 112 static final ACCEPT_CHARSET = "Accept-Charset"; | |
| 113 static final ACCEPT_ENCODING = "Accept-Encoding"; | |
| 114 static final ACCEPT_LANGUAGE = "Accept-Language"; | |
| 115 static final ACCEPT_RANGES = "Accept-Ranges"; | |
| 116 static final AGE = "Age"; | |
| 117 static final ALLOW = "Allow"; | |
| 118 static final AUTHORIZATION = "Authorization"; | |
| 119 static final CACHE_CONTROL = "Cache-Control"; | |
| 120 static final CONNECTION = "Connection"; | |
| 121 static final CONTENT_ENCODING = "Content-Encoding"; | |
| 122 static final CONTENT_LANGUAGE = "Content-Language"; | |
| 123 static final CONTENT_LENGTH = "Content-Length"; | |
| 124 static final CONTENT_LOCATION = "Content-Location"; | |
| 125 static final CONTENT_MD5 = "Content-MD5"; | |
| 126 static final CONTENT_RANGE = "Content-Range"; | |
| 127 static final CONTENT_TYPE = "Content-Type"; | |
| 128 static final DATE = "Date"; | |
| 129 static final ETAG = "ETag"; | |
| 130 static final EXPECT = "Expect"; | |
| 131 static final EXPIRES = "Expires"; | |
| 132 static final FROM = "From"; | |
| 133 static final HOST = "Host"; | |
| 134 static final IF_MATCH = "If-Match"; | |
| 135 static final IF_MODIFIED_SINCE = "If-Modified-Since"; | |
| 136 static final IF_NONE_MATCH = "If-None-Match"; | |
| 137 static final IF_RANGE = "If-Range"; | |
| 138 static final IF_UNMODIFIED_SINCE = "If-Unmodified-Since"; | |
| 139 static final LAST_MODIFIED = "Last-Modified"; | |
| 140 static final LOCATION = "Location"; | |
| 141 static final MAX_FORWARDS = "Max-Forwards"; | |
| 142 static final PRAGMA = "Pragma"; | |
| 143 static final PROXY_AUTHENTICATE = "Proxy-Authenticate"; | |
| 144 static final PROXY_AUTHORIZATION = "Proxy-Authorization"; | |
| 145 static final RANGE = "Range"; | |
| 146 static final REFERER = "Referer"; | |
| 147 static final RETRY_AFTER = "Retry-After"; | |
| 148 static final SERVER = "Server"; | |
| 149 static final TE = "TE"; | |
| 150 static final TRAILER = "Trailer"; | |
| 151 static final TRANSFER_ENCODING = "Transfer-Encoding"; | |
| 152 static final UPGRADE = "Upgrade"; | |
| 153 static final USER_AGENT = "User-Agent"; | |
| 154 static final VARY = "Vary"; | |
| 155 static final VIA = "Via"; | |
| 156 static final WARNING = "Warning"; | |
| 157 static final WWW_AUTHENTICATE = "WWW-Authenticate"; | |
| 158 | |
| 159 static final GENERAL_HEADERS = const [CACHE_CONTROL, | |
| 160 CONNECTION, | |
| 161 DATE, | |
| 162 PRAGMA, | |
| 163 TRAILER, | |
| 164 TRANSFER_ENCODING, | |
| 165 UPGRADE, | |
| 166 VIA, | |
| 167 WARNING]; | |
| 168 | |
| 169 static final ENTITY_HEADERS = const [ALLOW, | |
| 170 CONTENT-ENCODING, | |
| 171 CONTENT-LANGUAGE, | |
| 172 CONTENT-LENGTH, | |
| 173 CONTENT-LOCATION, | |
| 174 CONTENT-MD5, | |
| 175 CONTENT-RANGE, | |
| 176 CONTENT-TYPE, | |
| 177 EXPIRES, | |
| 178 LAST-MODIFIED]; | |
| 179 | |
| 180 | |
| 181 static final RESPONSE_HEADERS = const [ACCEPT-RANGES, | |
| 182 AGE, | |
| 183 ETAG, | |
| 184 LOCATION, | |
| 185 PROXY-AUTHENTICATE, | |
| 186 RETRY-AFTER, | |
| 187 SERVER, | |
| 188 VARY, | |
| 189 WWW-AUTHENTICATE]; | |
| 190 | |
| 191 static final REQUEST_HEADERS = const [ACCEPT, | |
| 192 ACCEPT-CHARSET, | |
| 193 ACCEPT-ENCODING, | |
| 194 ACCEPT-LANGUAGE, | |
| 195 AUTHORIZATION, | |
| 196 EXPECT, | |
| 197 FROM, | |
| 198 HOST, | |
| 199 IF-MATCH, | |
| 200 IF-MODIFIED-SINCE, | |
| 201 IF-NONE-MATCH, | |
| 202 IF-RANGE, | |
| 203 IF-UNMODIFIED-SINCE, | |
| 204 MAX-FORWARDS, | |
| 205 PROXY-AUTHORIZATION, | |
| 206 RANGE, | |
| 207 REFERER, | |
| 208 TE, | |
| 209 USER-AGENT]; | |
| 210 | |
| 211 /** | |
| 212 * Returns the list of values for the header named [name]. If there | |
| 213 * is no headers with the provided name null will be returned. | |
| 214 */ | |
| 215 List<String> operator[](String name); | |
|
Anders Johnsen
2012/04/13 11:47:53
Now we have no trivial way of getting the only hea
Søren Gjesse
2012/04/16 11:37:16
I agree, in the tests I had to use [0] in quite a
Anders Johnsen
2012/04/16 11:50:46
I think we should just do that, and throw an excep
Søren Gjesse
2012/04/16 12:17:31
Added method value for now.
| |
| 216 | |
| 217 /** | |
| 218 * Adds a header value. The header named [name] will have the value | |
| 219 * [value] added to its list of values. Some headers are single | |
| 220 * values and for these adding a value will replace the previous | |
| 221 * value. If the value is of type Date a HTTP date format will be | |
| 222 * applied to all other types the default toString method will be | |
| 223 * used. | |
| 224 */ | |
| 225 void add(String name, value); | |
|
Anders Johnsen
2012/04/13 11:47:53
Would it be better to say either var or Object? Ha
Søren Gjesse
2012/04/16 11:37:16
Added type Object
| |
| 226 | |
| 227 /** | |
| 228 * Sets a header. The header named [name] will have the value | |
| 229 * [value] set as its only value. All previous values in its list | |
| 230 * will be removed. Some headers have system supplied values and for | |
| 231 * these the system supplied values will still be added to the | |
| 232 * collection of values for the header. If the value is of type Date | |
| 233 * a HTTP date format will be applied to all other types the default | |
| 234 * toString method will be used. | |
| 235 */ | |
| 236 void set(String name, value); | |
|
Anders Johnsen
2012/04/13 11:47:53
Can value be a List?
Søren Gjesse
2012/04/16 11:37:16
That should be fine. It should then add each eleme
Anders Johnsen
2012/04/16 11:50:46
Ty!
| |
| 237 | |
| 238 /** | |
| 239 * Removes a specific value for a header name. Some headers have | |
| 240 * system supplied values and for these the system supplied values | |
| 241 * will still be added to the collection of values for the header. | |
| 242 */ | |
| 243 void remove(String name, value); | |
| 244 | |
| 245 /** | |
| 246 * Remove all values for the specified header name. Some headers | |
| 247 * have system supplied values and for these the system supplied | |
| 248 * values will still be added to the collection of values for the | |
| 249 * header. | |
| 250 */ | |
| 251 void removeAll(String name); | |
| 252 } | |
| 253 | |
| 254 | |
| 255 /** | |
| 103 * Http request delivered to the HTTP server callback. | 256 * Http request delivered to the HTTP server callback. |
| 104 */ | 257 */ |
| 105 interface HttpRequest default _HttpRequest { | 258 interface HttpRequest default _HttpRequest { |
| 106 /** | 259 /** |
| 107 * Returns the content length of the request body. If the size of | 260 * Returns the content length of the request body. If the size of |
| 108 * the request body is not known in advance this -1. | 261 * the request body is not known in advance this -1. |
| 109 */ | 262 */ |
| 110 int get contentLength(); | 263 int get contentLength(); |
| 111 | 264 |
| 112 /** | 265 /** |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 130 String get queryString(); | 283 String get queryString(); |
| 131 | 284 |
| 132 /** | 285 /** |
| 133 * Returns the parsed query string. | 286 * Returns the parsed query string. |
| 134 */ | 287 */ |
| 135 Map<String, String> get queryParameters(); | 288 Map<String, String> get queryParameters(); |
| 136 | 289 |
| 137 /** | 290 /** |
| 138 * Returns the request headers. | 291 * Returns the request headers. |
| 139 */ | 292 */ |
| 140 Map<String, String> get headers(); | 293 HttpHeaders get headers(); |
| 141 | 294 |
| 142 /** | 295 /** |
| 143 * Returns the input stream for the request. This is used to read | 296 * Returns the input stream for the request. This is used to read |
| 144 * the request data. | 297 * the request data. |
| 145 */ | 298 */ |
| 146 InputStream get inputStream(); | 299 InputStream get inputStream(); |
| 147 } | 300 } |
| 148 | 301 |
| 149 | 302 |
| 150 /** | 303 /** |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 171 */ | 324 */ |
| 172 String reasonPhrase; | 325 String reasonPhrase; |
| 173 | 326 |
| 174 /** | 327 /** |
| 175 * Gets and sets the expiry date. The value of this property will | 328 * Gets and sets the expiry date. The value of this property will |
| 176 * reflect the "Expires" header | 329 * reflect the "Expires" header |
| 177 */ | 330 */ |
| 178 Date expires; | 331 Date expires; |
| 179 | 332 |
| 180 /** | 333 /** |
| 181 * Sets a header on the response. NOTE: If the same header name is | |
| 182 * set more than once only the last value will be part of the | |
| 183 * response. | |
| 184 */ | |
| 185 void setHeader(String name, String value); | |
| 186 | |
| 187 /** | |
| 188 * Returns the response headers. | 334 * Returns the response headers. |
| 189 */ | 335 */ |
| 190 Map<String, String> get headers(); | 336 HttpHeaders get headers(); |
| 191 | 337 |
| 192 /** | 338 /** |
| 193 * Returns the output stream for the response. This is used to write | 339 * Returns the output stream for the response. This is used to write |
| 194 * the response data. When all response data has been written close | 340 * the response data. When all response data has been written close |
| 195 * the stream to indicate the end of the response. | 341 * the stream to indicate the end of the response. |
| 196 * | 342 * |
| 197 * When this is accessed for the first time the response header is | 343 * When this is accessed for the first time the response header is |
| 198 * send. Calling any methods that will change the header after | 344 * send. Calling any methods that will change the header after |
| 199 * having retrieved the output stream will throw an exception. | 345 * having retrieved the output stream will throw an exception. |
| 200 */ | 346 */ |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 310 String host; | 456 String host; |
| 311 | 457 |
| 312 /** | 458 /** |
| 313 * Gets and sets the port part of the "Host" header for the | 459 * Gets and sets the port part of the "Host" header for the |
| 314 * connection. By default this will be set to the value of the port | 460 * connection. By default this will be set to the value of the port |
| 315 * used when initiating the connection. | 461 * used when initiating the connection. |
| 316 */ | 462 */ |
| 317 int port; | 463 int port; |
| 318 | 464 |
| 319 /** | 465 /** |
| 320 * Sets a header on the request. NOTE: If the same header name is | |
| 321 * set more than once only the last value set will be part of the | |
| 322 * request. | |
| 323 */ | |
| 324 void setHeader(String name, String value); | |
| 325 | |
| 326 /** | |
| 327 * Returns the request headers. | 466 * Returns the request headers. |
| 328 */ | 467 */ |
| 329 Map<String, String> get headers(); | 468 HttpHeaders get headers(); |
| 330 | 469 |
| 331 /** | 470 /** |
| 332 * Returns the output stream for the request. This is used to write | 471 * Returns the output stream for the request. This is used to write |
| 333 * the request data. When all request data has been written close | 472 * the request data. When all request data has been written close |
| 334 * the stream to indicate the end of the request. | 473 * the stream to indicate the end of the request. |
| 335 * | 474 * |
| 336 * When this is accessed for the first time the request header is | 475 * When this is accessed for the first time the request header is |
| 337 * send. Calling any methods that will change the header after | 476 * send. Calling any methods that will change the header after |
| 338 * having retrieved the output stream will throw an exception. | 477 * having retrieved the output stream will throw an exception. |
| 339 */ | 478 */ |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 365 * Returns the date value for the "Expires" header. Returns null if | 504 * Returns the date value for the "Expires" header. Returns null if |
| 366 * the response has no "Expires" header. Throws a HttpException if | 505 * the response has no "Expires" header. Throws a HttpException if |
| 367 * the response has an "Expires" header which is not formatted as a | 506 * the response has an "Expires" header which is not formatted as a |
| 368 * valid HTTP date. | 507 * valid HTTP date. |
| 369 */ | 508 */ |
| 370 Date get expires(); | 509 Date get expires(); |
| 371 | 510 |
| 372 /** | 511 /** |
| 373 * Returns the response headers. | 512 * Returns the response headers. |
| 374 */ | 513 */ |
| 375 Map<String, String> get headers(); | 514 HttpHeaders get headers(); |
| 376 | 515 |
| 377 /** | 516 /** |
| 378 * Returns the input stream for the response. This is used to read | 517 * Returns the input stream for the response. This is used to read |
| 379 * the response data. | 518 * the response data. |
| 380 */ | 519 */ |
| 381 InputStream get inputStream(); | 520 InputStream get inputStream(); |
| 382 } | 521 } |
| 383 | 522 |
| 384 | 523 |
| 385 class HttpException implements Exception { | 524 class HttpException implements Exception { |
| 386 const HttpException([String this.message = ""]); | 525 const HttpException([String this.message = ""]); |
| 387 String toString() => "HttpException: $message"; | 526 String toString() => "HttpException: $message"; |
| 388 final String message; | 527 final String message; |
| 389 } | 528 } |
| OLD | NEW |