| 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 #library("http"); | |
| 6 #import("dart:io"); | |
| 7 #source("http_impl.dart"); | |
| 8 #source("../../runtime/bin/buffer_list.dart"); | |
| 9 #source("../../runtime/bin/stream_util.dart"); | |
| 10 | |
| 11 /** | |
| 12 * HTTP status codes. | |
| 13 */ | |
| 14 interface HttpStatus { | |
| 15 static final int CONTINUE = 100; | |
| 16 static final int SWITCHING_PROTOCOLS = 101; | |
| 17 static final int OK = 200; | |
| 18 static final int CREATED = 201; | |
| 19 static final int ACCEPTED = 202; | |
| 20 static final int NON_AUTHORITATIVE_INFORMATION = 203; | |
| 21 static final int NO_CONTENT = 204; | |
| 22 static final int RESET_CONTENT = 205; | |
| 23 static final int PARTIAL_CONTENT = 206; | |
| 24 static final int MULTIPLE_CHOICES = 300; | |
| 25 static final int MOVED_PERMANENTLY = 301; | |
| 26 static final int FOUND = 302; | |
| 27 static final int MOVED_TEMPORARILY = 302; // Common alias for FOUND. | |
| 28 static final int SEE_OTHER = 303; | |
| 29 static final int NOT_MODIFIED = 304; | |
| 30 static final int USE_PROXY = 305; | |
| 31 static final int TEMPORARY_REDIRECT = 307; | |
| 32 static final int BAD_REQUEST = 400; | |
| 33 static final int UNAUTHORIZED = 401; | |
| 34 static final int PAYMENT_REQUIRED = 402; | |
| 35 static final int FORBIDDEN = 403; | |
| 36 static final int NOT_FOUND = 404; | |
| 37 static final int METHOD_NOT_ALLOWED = 405; | |
| 38 static final int NOT_ACCEPTABLE = 406; | |
| 39 static final int PROXY_AUTHENTICATION_REQUIRED = 407; | |
| 40 static final int REQUEST_TIMEOUT = 408; | |
| 41 static final int CONFLICT = 409; | |
| 42 static final int GONE = 410; | |
| 43 static final int LENGTH_REQUIRED = 411; | |
| 44 static final int PRECONDITION_FAILED = 412; | |
| 45 static final int REQUEST_ENTITY_TOO_LARGE = 413; | |
| 46 static final int REQUEST_URI_TOO_LONG = 414; | |
| 47 static final int UNSUPPORTED_MEDIA_TYPE = 415; | |
| 48 static final int REQUESTED_RANGE_NOT_SATISFIABLE = 416; | |
| 49 static final int EXPECTATION_FAILED = 417; | |
| 50 static final int INTERNAL_SERVER_ERROR = 500; | |
| 51 static final int NOT_IMPLEMENTED = 501; | |
| 52 static final int BAD_GATEWAY = 502; | |
| 53 static final int SERVICE_UNAVAILABLE = 503; | |
| 54 static final int GATEWAY_TIMEOUT = 504; | |
| 55 static final int HTTP_VERSION_NOT_SUPPORTED = 505; | |
| 56 // Client generated status code. | |
| 57 static final int NETWORK_CONNECT_TIMEOUT_ERROR = 599; | |
| 58 } | |
| 59 | |
| 60 | |
| 61 /** | |
| 62 * HTTP server. | |
| 63 */ | |
| 64 interface HttpServer default _HttpServer { | |
| 65 HttpServer(); | |
| 66 | |
| 67 /** | |
| 68 * Start listening for HTTP requests on the specified [host] and | |
| 69 * [port]. For each HTTP request the handler set through | |
| 70 * [requestHandler] will be invoked. If a [port] of 0 is specified | |
| 71 * the server will choose an ephemeral port. The optional argument | |
| 72 * [backlog] can be used to specify the listen backlog for the | |
| 73 * underlying OS listen setup. | |
| 74 */ | |
| 75 void listen(String host, int port, [int backlog]); | |
| 76 | |
| 77 /** | |
| 78 * Stop server listening. | |
| 79 */ | |
| 80 void close(); | |
| 81 | |
| 82 /** | |
| 83 * Returns the port that the server is listening on. This can be | |
| 84 * used to get the actual port used when a value of 0 for [port] is | |
| 85 * specified in the [listen] call. | |
| 86 */ | |
| 87 int get port(); | |
| 88 | |
| 89 /** | |
| 90 * Sets the handler that gets called when a new HTTP request is received. | |
| 91 */ | |
| 92 void set requestHandler(void handler(HttpRequest, HttpResponse)); | |
| 93 | |
| 94 /** | |
| 95 * Sets the error handler that is called when a connection error occurs. | |
| 96 */ | |
| 97 void set errorHandler(void handler(String errorMessage)); | |
| 98 } | |
| 99 | |
| 100 | |
| 101 /** | |
| 102 * Http request delivered to the HTTP server callback. | |
| 103 */ | |
| 104 interface HttpRequest default _HttpRequest { | |
| 105 /** | |
| 106 * Returns the content length of the request body. If the size of | |
| 107 * the request body is not known in advance this -1. | |
| 108 */ | |
| 109 int get contentLength(); | |
| 110 | |
| 111 /** | |
| 112 * Returns the keep alive state of the connection. | |
| 113 */ | |
| 114 bool get keepAlive(); | |
| 115 | |
| 116 /** | |
| 117 * Returns the method for the request. | |
| 118 */ | |
| 119 String get method(); | |
| 120 | |
| 121 /** | |
| 122 * Returns the URI for the request. | |
| 123 */ | |
| 124 String get uri(); | |
| 125 | |
| 126 /** | |
| 127 * Returns the path part of the URI. | |
| 128 */ | |
| 129 String get path(); | |
| 130 | |
| 131 /** | |
| 132 * Returns the query string. | |
| 133 */ | |
| 134 String get queryString(); | |
| 135 | |
| 136 /** | |
| 137 * Returns the parsed query string. | |
| 138 */ | |
| 139 Map<String, String> get queryParameters(); | |
| 140 | |
| 141 /** | |
| 142 * Returns the request headers. | |
| 143 */ | |
| 144 Map<String, String> get headers(); | |
| 145 | |
| 146 /** | |
| 147 * Returns the input stream for the request. This is used to read | |
| 148 * the request data. | |
| 149 */ | |
| 150 InputStream get inputStream(); | |
| 151 } | |
| 152 | |
| 153 | |
| 154 /** | |
| 155 * HTTP response to be send back to the client. | |
| 156 */ | |
| 157 interface HttpResponse default _HttpResponse { | |
| 158 /** | |
| 159 * Gets and sets the content length of the response. If the size of | |
| 160 * the response is not known in advance set the content length to | |
| 161 * -1 - which is also the default if not set. | |
| 162 */ | |
| 163 void set contentLength(int contentLength); | |
| 164 int get contentLength(); | |
| 165 | |
| 166 /** | |
| 167 * Gets and sets the keep alive state of the connection. If the | |
| 168 * associated request have a keep alive state of false setting keep | |
| 169 * alive to true will have no effect. | |
| 170 */ | |
| 171 void set keepAlive(bool keepAlive); | |
| 172 bool get keepAlive(); | |
| 173 | |
| 174 /** | |
| 175 * Sets a header on the response. NOTE: If the same header name is | |
| 176 * set more than once only the last value will be part of the | |
| 177 * response. | |
| 178 */ | |
| 179 void setHeader(String name, String value); | |
| 180 | |
| 181 /** | |
| 182 * Returns the output stream for the response. This is used to write | |
| 183 * the response data. When all response data has been written close | |
| 184 * the stream to indicate the end of the response. | |
| 185 * | |
| 186 * When this is accessed for the first time the response header is | |
| 187 * send. Calling any methods that will change the header after | |
| 188 * having retrieved the output stream will throw an exception. | |
| 189 */ | |
| 190 OutputStream get outputStream(); | |
| 191 | |
| 192 /** | |
| 193 * Write string data to the response. The string characters will be | |
| 194 * encoded using UFT-8. This is a temporary convenience method as | |
| 195 * long as the OutputStream interface does not have a writeString | |
| 196 * method. | |
| 197 */ | |
| 198 bool writeString(String string); | |
| 199 } | |
| 200 | |
| 201 | |
| 202 /** | |
| 203 * HTTP client factory. | |
| 204 */ | |
| 205 interface HttpClient default _HttpClient { | |
| 206 HttpClient(); | |
| 207 | |
| 208 /** | |
| 209 * Opens a HTTP connection. The returned [HttpClientConnection] is | |
| 210 * used to register handlers for asynchronous events on a Http | |
| 211 * connection. | |
| 212 */ | |
| 213 HttpClientConnection open(String method, String host, int port, String path); | |
| 214 | |
| 215 /** | |
| 216 * Opens a HTTP connection using the GET method. | |
| 217 */ | |
| 218 HttpClientConnection get(String host, int port, String path); | |
| 219 | |
| 220 /** | |
| 221 * Opens a HTTP connection using the POST method. | |
| 222 */ | |
| 223 HttpClientConnection post(String host, int port, String path); | |
| 224 | |
| 225 /** | |
| 226 * Shutdown the HTTP client releasing all resources. | |
| 227 */ | |
| 228 void shutdown(); | |
| 229 } | |
| 230 | |
| 231 | |
| 232 /** | |
| 233 * A [HttpClientConnection] is returned by all [HttpClient] methods | |
| 234 * that initiate a connection to an HTTP server. The handlers will be | |
| 235 * called as the connection state progresses. | |
| 236 * | |
| 237 * The setting of all handlers is optional. If the [requestHandler] is | |
| 238 * not set the request will be send without any additional headers and | |
| 239 * an empty body. If the [responseHandler] is not set the response | |
| 240 * will be read and discarded. | |
| 241 */ | |
| 242 interface HttpClientConnection { | |
| 243 /** | |
| 244 * Sets the handler that is called when the connection is established. | |
| 245 */ | |
| 246 void set requestHandler(void handler(HttpClientRequest request)); | |
| 247 | |
| 248 /** | |
| 249 * Sets callback to be called when the request has been send and | |
| 250 * the response is ready for processing. The callback is called when | |
| 251 * all headers of the response are received and data is ready to be | |
| 252 * received. | |
| 253 */ | |
| 254 void set responseHandler(void handler(HttpClientResponse response)); | |
| 255 | |
| 256 /** | |
| 257 * Sets the handler that gets called if an error occurs while | |
| 258 * processing the HTTP request. | |
| 259 */ | |
| 260 void set errorHandler(void handler(HttpException e)); | |
| 261 } | |
| 262 | |
| 263 | |
| 264 /** | |
| 265 * HTTP request for a client connection. | |
| 266 */ | |
| 267 interface HttpClientRequest default _HttpClientRequest { | |
| 268 /** | |
| 269 * Gets and sets the content length of the request. If the size of | |
| 270 * the request is not known in advance set content length to -1, | |
| 271 * which is also the default. | |
| 272 */ | |
| 273 void set contentLength(int contentLength); | |
| 274 int get contentLength(); | |
| 275 | |
| 276 /** | |
| 277 * Gets and sets the keep alive state of the connection. | |
| 278 */ | |
| 279 void set keepAlive(bool keepAlive); | |
| 280 bool get keepAlive(); | |
| 281 | |
| 282 /** | |
| 283 * Sets a header on the request. NOTE: If the same header name is | |
| 284 * set more than once only the last value set will be part of the | |
| 285 * request. | |
| 286 */ | |
| 287 void setHeader(String name, String value); | |
| 288 | |
| 289 /** | |
| 290 * Returns the output stream for the request. This is used to write | |
| 291 * the request data. When all request data has been written close | |
| 292 * the stream to indicate the end of the request. | |
| 293 * | |
| 294 * When this is accessed for the first time the request header is | |
| 295 * send. Calling any methods that will change the header after | |
| 296 * having retrieved the output stream will throw an exception. | |
| 297 */ | |
| 298 OutputStream get outputStream(); | |
| 299 | |
| 300 /** | |
| 301 * Write string data to the request. The string characters will be | |
| 302 * encoded using UFT-8. This is a temporary convenience method as | |
| 303 * long as the OutputStream interface does not have a writeString | |
| 304 * method. | |
| 305 */ | |
| 306 bool writeString(String string); | |
| 307 } | |
| 308 | |
| 309 | |
| 310 /** | |
| 311 * HTTP response for a client connection. | |
| 312 */ | |
| 313 interface HttpClientResponse default _HttpClientResponse { | |
| 314 /** | |
| 315 * Returns the status code. | |
| 316 */ | |
| 317 int get statusCode(); | |
| 318 | |
| 319 /** | |
| 320 * Returns the reason phrase associated with the status code. | |
| 321 */ | |
| 322 String get reasonPhrase(); | |
| 323 | |
| 324 /** | |
| 325 * Returns the content length of the request body. If the size of | |
| 326 * the request body is not known in advance this -1. | |
| 327 */ | |
| 328 int get contentLength(); | |
| 329 | |
| 330 /** | |
| 331 * Returns the keep alive state of the connection. | |
| 332 */ | |
| 333 bool get keepAlive(); | |
| 334 | |
| 335 /** | |
| 336 * Returns the response headers. | |
| 337 */ | |
| 338 Map get headers(); | |
| 339 | |
| 340 /** | |
| 341 * Returns the input stream for the response. This is used to read | |
| 342 * the response data. | |
| 343 */ | |
| 344 InputStream get inputStream(); | |
| 345 } | |
| 346 | |
| 347 | |
| 348 class HttpException implements Exception { | |
| 349 const HttpException([String this.message = ""]); | |
| 350 String toString() => "HttpException: $message"; | |
| 351 final String message; | |
| 352 } | |
| OLD | NEW |