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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 static final int BAD_GATEWAY = 502; | 46 static final int BAD_GATEWAY = 502; |
| 47 static final int SERVICE_UNAVAILABLE = 503; | 47 static final int SERVICE_UNAVAILABLE = 503; |
| 48 static final int GATEWAY_TIMEOUT = 504; | 48 static final int GATEWAY_TIMEOUT = 504; |
| 49 static final int HTTP_VERSION_NOT_SUPPORTED = 505; | 49 static final int HTTP_VERSION_NOT_SUPPORTED = 505; |
| 50 // Client generated status code. | 50 // Client generated status code. |
| 51 static final int NETWORK_CONNECT_TIMEOUT_ERROR = 599; | 51 static final int NETWORK_CONNECT_TIMEOUT_ERROR = 599; |
| 52 } | 52 } |
| 53 | 53 |
| 54 | 54 |
| 55 /** | 55 /** |
| 56 * Interface to implement by HTTP request handler classes. | |
|
Mads Ager (google)
2012/04/18 12:22:08
to be implemented
| |
| 57 */ | |
| 58 | |
| 59 interface RequestHandler { | |
| 60 void onRequest(HttpRequest request, HttpResponse response); | |
| 61 } | |
| 62 | |
| 63 | |
| 64 /** | |
| 56 * HTTP server. | 65 * HTTP server. |
| 57 */ | 66 */ |
| 58 interface HttpServer default _HttpServer { | 67 interface HttpServer default _HttpServer { |
| 59 HttpServer(); | 68 HttpServer(); |
| 60 | 69 |
| 61 /** | 70 /** |
| 62 * Start listening for HTTP requests on the specified [host] and | 71 * Start listening for HTTP requests on the specified [host] and |
| 63 * [port]. For each HTTP request the handler set through [onRequest] | 72 * [port]. For each HTTP request the handler set through [onRequest] |
| 64 * will be invoked. If a [port] of 0 is specified the server will | 73 * will be invoked. If a [port] of 0 is specified the server will |
| 65 * choose an ephemeral port. The optional argument [backlog] can be | 74 * choose an ephemeral port. The optional argument [backlog] can be |
| 66 * used to specify the listen backlog for the underlying OS listen | 75 * used to specify the listen backlog for the underlying OS listen |
| 67 * setup. | 76 * setup. |
| 68 */ | 77 */ |
| 69 void listen(String host, int port, [int backlog]); | 78 void listen(String host, int port, [int backlog]); |
| 70 | 79 |
| 71 /** | 80 /** |
| 72 * Attach the HTTP server to an existing ServerSocket. If the HttpServer is | 81 * Attach the HTTP server to an existing ServerSocket. If the HttpServer is |
| 73 * closed, the HttpServer will just detach itself, and not close | 82 * closed, the HttpServer will just detach itself, and not close |
| 74 * [serverSocket]. | 83 * [serverSocket]. |
| 75 */ | 84 */ |
| 76 void listenOn(ServerSocket serverSocket); | 85 void listenOn(ServerSocket serverSocket); |
| 77 | 86 |
| 78 /** | 87 /** |
| 88 * Adds a request handler to the list of request handlers. The | |
| 89 * request path is matched against the specified pattern. The first | |
| 90 * handler for which pattern which matches will be handed the | |
|
Mads Ager (google)
2012/04/18 12:22:08
for which the pattern matches will be ...
| |
| 91 * request. The [handler] can be either an object implementing the | |
| 92 * [RequestHandler] interface or a function taking two arguments. | |
|
Mads Ager (google)
2012/04/18 12:22:08
Maybe spell out what the arguments are?
| |
| 93 */ | |
| 94 addRequestHandler(RegExp pattern, Object handler); | |
| 95 | |
| 96 /** | |
| 97 * Sets the request handler. This request handler will be called if | |
|
Mads Ager (google)
2012/04/18 12:22:08
Sets the default request handler.
| |
| 98 * none of the request handlers registered by [addRequestHandler] | |
| 99 * matches the current request. If no default request handler is set | |
|
Mads Ager (google)
2012/04/18 12:22:08
matches -> match
| |
| 100 * the server will just respond with status code [:NOT_FOUND:] (404). | |
| 101 */ | |
| 102 void set defaultRequestHandler(Object handler); | |
| 103 | |
| 104 /** | |
| 79 * Stop server listening. | 105 * Stop server listening. |
| 80 */ | 106 */ |
| 81 void close(); | 107 void close(); |
| 82 | 108 |
| 83 /** | 109 /** |
| 84 * Returns the port that the server is listening on. This can be | 110 * Returns the port that the server is listening on. This can be |
| 85 * used to get the actual port used when a value of 0 for [port] is | 111 * used to get the actual port used when a value of 0 for [port] is |
| 86 * specified in the [listen] call. | 112 * specified in the [listen] call. |
| 87 */ | 113 */ |
| 88 int get port(); | 114 int get port(); |
| 89 | 115 |
| 90 /** | 116 /** |
| 91 * Sets the handler that gets called when a new HTTP request is received. | |
| 92 */ | |
| 93 void set onRequest(void callback(HttpRequest, HttpResponse)); | |
| 94 | |
| 95 /** | |
| 96 * Sets the error handler that is called when a connection error occurs. | 117 * Sets the error handler that is called when a connection error occurs. |
| 97 */ | 118 */ |
| 98 void set onError(void callback(Exception e)); | 119 void set onError(void callback(Exception e)); |
| 99 } | 120 } |
| 100 | 121 |
| 101 | 122 |
| 102 /** | 123 /** |
| 103 * Access to the HTTP headers for requests and responses. In some | 124 * Access to the HTTP headers for requests and responses. In some |
| 104 * situations the headers will be imutable and the mutating methods | 125 * situations the headers will be imutable and the mutating methods |
| 105 * will then throw exceptions. | 126 * will then throw exceptions. |
| (...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 513 */ | 534 */ |
| 514 InputStream get inputStream(); | 535 InputStream get inputStream(); |
| 515 } | 536 } |
| 516 | 537 |
| 517 | 538 |
| 518 class HttpException implements Exception { | 539 class HttpException implements Exception { |
| 519 const HttpException([String this.message = ""]); | 540 const HttpException([String this.message = ""]); |
| 520 String toString() => "HttpException: $message"; | 541 String toString() => "HttpException: $message"; |
| 521 final String message; | 542 final String message; |
| 522 } | 543 } |
| OLD | NEW |