Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: runtime/bin/http.dart

Issue 10119005: Add HTTP handler registration to the HTTP server (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/bin/http_impl.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 /**
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 * function [matcher] is called with the request and should return
90 * [:true:] if the [handler] should handle the request. The first
91 * handler for which [matcher] returns [:true:] will be handed the
92 * request. The [handler] can be either an object implementing the
93 * [RequestHandler] interface or a function taking two arguments.
94 */
95 addRequestHandler(bool matcher(HttpRequest request), Object handler);
96
97 /**
98 * Sets the request handler. This request handler will be called if
99 * none of the request handlers registered by [addRequestHandler]
100 * matches the current request. If no default request handler is set
101 * the server will just respond with status code [:NOT_FOUND:] (404).
102 */
103 void set defaultRequestHandler(Object handler);
104
105 /**
79 * Stop server listening. 106 * Stop server listening.
80 */ 107 */
81 void close(); 108 void close();
82 109
83 /** 110 /**
84 * Returns the port that the server is listening on. This can be 111 * 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 112 * used to get the actual port used when a value of 0 for [port] is
86 * specified in the [listen] call. 113 * specified in the [listen] call.
87 */ 114 */
88 int get port(); 115 int get port();
89 116
90 /** 117 /**
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. 118 * Sets the error handler that is called when a connection error occurs.
97 */ 119 */
98 void set onError(void callback(Exception e)); 120 void set onError(void callback(Exception e));
99 } 121 }
100 122
101 123
102 /** 124 /**
103 * Access to the HTTP headers for requests and responses. In some 125 * Access to the HTTP headers for requests and responses. In some
104 * situations the headers will be imutable and the mutating methods 126 * situations the headers will be imutable and the mutating methods
105 * will then throw exceptions. 127 * will then throw exceptions.
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 */ 535 */
514 InputStream get inputStream(); 536 InputStream get inputStream();
515 } 537 }
516 538
517 539
518 class HttpException implements Exception { 540 class HttpException implements Exception {
519 const HttpException([String this.message = ""]); 541 const HttpException([String this.message = ""]);
520 String toString() => "HttpException: $message"; 542 String toString() => "HttpException: $message";
521 final String message; 543 final String message;
522 } 544 }
OLDNEW
« no previous file with comments | « no previous file | runtime/bin/http_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698