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

Side by Side Diff: utils/pub/safe_http_server.dart

Issue 12633015: Add a SafeHttpServer shim to work around issue 9140. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 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
OLDNEW
(Empty)
1 // Copyright (c) 2013, 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 safe_http_server;
6
7 import 'dart:async';
8 import 'dart:io';
9 import 'dart:uri';
10
11 // TODO(nweiz): remove this when issue 9140 is fixed.
12 /// A wrapper around [HttpServer] that swallows errors caused by requests
13 /// behaving badly. This provides the following guarantees:
14 ///
15 /// * The [SafeHttpServer.listen] onError callback will only emit server-wide
16 /// errors. It will not emit errors for requests that were unparseable or
17 /// where the connection was closed too soon.
18 /// * [HttpResponse.done] will emit no errors.
19 ///
20 /// The [HttpRequest] data stream can still emit errors.
21 class SafeHttpServer extends StreamView<HttpRequest> implements HttpServer {
22 final HttpServer _inner;
23
24 static Future<SafeHttpServer> bind([String host = "127.0.0.1",
25 int port = 0, int backlog = 0]) {
26 return HttpServer.bind(host, port, backlog)
27 .then((server) => new SafeHttpServer(server));
28 }
29
30 SafeHttpServer(HttpServer server)
31 : super(server),
32 _inner = server;
33
34 void close() => _inner.close();
35
36 int get port => _inner.port;
37
38 set sessionTimeout(int timeout) {
39 _inner.sessionTimeout = timeout;
40 }
41
42 HttpConnectionsInfo connectionsInfo() => _inner.connectionsInfo();
43
44 StreamSubscription<HttpRequest> listen(void onData(HttpRequest value),
45 {void onError(AsyncError error), void onDone(),
46 bool unsubscribeOnError: false}) {
47 var subscription;
48 subscription = super.listen((request) {
49 onData(new _HttpRequestWrapper(request));
50 }, onError: (e) {
51 var error = e.error;
52 // Ignore socket error 104, which is caused by a request being cancelled
53 // before it writes any headers. There's no reason to care about such
54 // requests.
55 if (error is SocketIOException && error.osError.errorCode == 104) return;
56 // Ignore any parsing errors, which come from malformed requests.
57 if (error is HttpParserException) return;
58 // Manually handle unsubscribeOnError so the above (ignored) errors don't
59 // cause unsubscription.
60 if (unsubscribeOnError) subscription.cancel();
61 if (onError != null) onError(e);
62 }, onDone: onDone);
63 return subscription;
64 }
65 }
66
67 /// A wrapper around [HttpRequest] for the sole purpose of swallowing errors on
68 /// [HttpResponse.done].
69 class _HttpRequestWrapper extends StreamView<List<int>> implements HttpRequest {
70 final HttpRequest _inner;
71 final HttpResponse response;
72
73 _HttpRequestWrapper(HttpRequest inner)
74 : super(inner),
75 _inner = inner,
76 response = new _HttpResponseWrapper(inner.response);
77
78 int get contentLength => _inner.contentLength;
79 String get method => _inner.method;
80 Uri get uri => _inner.uri;
81 Map<String, String> get queryParameters => _inner.queryParameters;
82 HttpHeaders get headers => _inner.headers;
83 List<Cookie> get cookies => _inner.cookies;
84 bool get persistentConnection => _inner.persistentConnection;
85 X509Certificate get certificate => _inner.certificate;
86 HttpSession get session => _inner.session;
87 String get protocolVersion => _inner.protocolVersion;
88 HttpConnectionInfo get connectionInfo => _inner.connectionInfo;
89 }
90
91 /// A wrapper around [HttpResponse] for the sole purpose of swallowing errors on
92 /// [done].
93 class _HttpResponseWrapper implements HttpResponse {
94 final HttpResponse _inner;
95 Future<HttpResponse> _done;
96
97 _HttpResponseWrapper(this._inner);
98
99 /// Swallows all errors from writing to the response.
100 Future<HttpResponse> get done {
101 if (_done == null) _done = _inner.done.catchError((_) {});
102 return _done;
103 }
104
105 int get contentLength => _inner.contentLength;
106 set contentLength(int value) {
107 _inner.contentLength = value;
108 }
109
110 int get statusCode => _inner.statusCode;
111 set statusCode(int value) {
112 _inner.statusCode = value;
113 }
114
115 String get reasonPhrase => _inner.reasonPhrase;
116 set reasonPhrase(String value) {
117 _inner.reasonPhrase = value;
118 }
119
120 bool get persistentConnection => _inner.persistentConnection;
121 set persistentConnection(bool value) {
122 _inner.persistentConnection = value;
123 }
124
125 Encoding get encoding => _inner.encoding;
126 set encoding(Encoding value) {
127 _inner.encoding = value;
128 }
129
130 HttpHeaders get headers => _inner.headers;
131 List<Cookie> get cookies => _inner.cookies;
132 Future<Socket> detachSocket() => _inner.detachSocket();
133 HttpConnectionInfo get connectionInfo => _inner.connectionInfo;
134 void writeBytes(List<int> data) => _inner.writeBytes(data);
135 Future<HttpResponse> consume(Stream<List<int>> stream) =>
136 _inner.consume(stream);
137 Future<HttpResponse> writeStream(Stream<List<int>> stream) =>
138 _inner.writeStream(stream);
139 void close() => _inner.close();
140 void write(Object obj) => _inner.write(obj);
141 void writeAll(Iterable objects) => _inner.writeAll(objects);
142 void writeCharCode(int charCode) => _inner.writeCharCode(charCode);
143 void writeln(Object obj) => _inner.writeln(obj);
144 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698