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. | |
57 */ | |
58 | |
59 interface RequestHandler { | |
60 void onRequest(HttpRequest request, HttpResponse response); | |
61 } | |
62 | |
63 | |
64 /** | |
65 * HTTP server. | 56 * HTTP server. |
66 */ | 57 */ |
67 interface HttpServer default _HttpServer { | 58 interface HttpServer default _HttpServer { |
68 HttpServer(); | 59 HttpServer(); |
69 | 60 |
70 /** | 61 /** |
71 * Start listening for HTTP requests on the specified [host] and | 62 * Start listening for HTTP requests on the specified [host] and |
72 * [port]. For each HTTP request the handler set through [onRequest] | 63 * [port]. For each HTTP request the handler set through [onRequest] |
73 * will be invoked. If a [port] of 0 is specified the server will | 64 * will be invoked. If a [port] of 0 is specified the server will |
74 * choose an ephemeral port. The optional argument [backlog] can be | 65 * choose an ephemeral port. The optional argument [backlog] can be |
75 * used to specify the listen backlog for the underlying OS listen | 66 * used to specify the listen backlog for the underlying OS listen |
76 * setup. | 67 * setup. |
77 */ | 68 */ |
78 void listen(String host, int port, [int backlog]); | 69 void listen(String host, int port, [int backlog]); |
79 | 70 |
80 /** | 71 /** |
81 * Attach the HTTP server to an existing ServerSocket. If the HttpServer is | 72 * Attach the HTTP server to an existing ServerSocket. If the HttpServer is |
82 * closed, the HttpServer will just detach itself, and not close | 73 * closed, the HttpServer will just detach itself, and not close |
83 * [serverSocket]. | 74 * [serverSocket]. |
84 */ | 75 */ |
85 void listenOn(ServerSocket serverSocket); | 76 void listenOn(ServerSocket serverSocket); |
86 | 77 |
87 /** | 78 /** |
88 * Adds a request handler to the list of request handlers. The | 79 * Adds a request handler to the list of request handlers. The |
89 * function [matcher] is called with the request and must return | 80 * function [matcher] is called with the request and must return |
90 * [:true:] if the [handler] should handle the request. The first | 81 * [:true:] if the [handler] should handle the request. The first |
91 * handler for which [matcher] returns [:true:] will be handed the | 82 * handler for which [matcher] returns [:true:] will be handed the |
92 * request. The [handler] can be either an object implementing the | 83 * request. |
93 * [RequestHandler] interface or a function taking two arguments. | |
94 */ | 84 */ |
95 addRequestHandler(bool matcher(HttpRequest request), Object handler); | 85 addRequestHandler(bool matcher(HttpRequest request), |
| 86 void handler(HttpRequest request, HttpResponse response)); |
96 | 87 |
97 /** | 88 /** |
98 * Sets the request handler. This request handler will be called if | 89 * Sets the default request handler. This request handler will be |
99 * none of the request handlers registered by [addRequestHandler] | 90 * called if none of the request handlers registered by |
100 * matches the current request. If no default request handler is set | 91 * [addRequestHandler] matches the current request. If no default |
101 * the server will just respond with status code [:NOT_FOUND:] (404). | 92 * request handler is set the server will just respond with status |
| 93 * code [:NOT_FOUND:] (404). |
102 */ | 94 */ |
103 void set defaultRequestHandler(Object handler); | 95 void set defaultRequestHandler( |
| 96 void handler(HttpRequest request, HttpResponse response)); |
104 | 97 |
105 /** | 98 /** |
106 * Stop server listening. | 99 * Stop server listening. |
107 */ | 100 */ |
108 void close(); | 101 void close(); |
109 | 102 |
110 /** | 103 /** |
111 * Returns the port that the server is listening on. This can be | 104 * Returns the port that the server is listening on. This can be |
112 * used to get the actual port used when a value of 0 for [port] is | 105 * used to get the actual port used when a value of 0 for [port] is |
113 * specified in the [listen] call. | 106 * specified in the [listen] call. |
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 */ | 528 */ |
536 InputStream get inputStream(); | 529 InputStream get inputStream(); |
537 } | 530 } |
538 | 531 |
539 | 532 |
540 class HttpException implements Exception { | 533 class HttpException implements Exception { |
541 const HttpException([String this.message = ""]); | 534 const HttpException([String this.message = ""]); |
542 String toString() => "HttpException: $message"; | 535 String toString() => "HttpException: $message"; |
543 final String message; | 536 final String message; |
544 } | 537 } |
OLD | NEW |