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

Side by Side Diff: pkg/scheduled_test/lib/src/scheduled_server/safe_http_server.dart

Issue 134873005: Get rid of SafeHttpServer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 11 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 | « pkg/scheduled_test/lib/scheduled_server.dart ('k') | sdk/lib/_internal/pub/lib/src/oauth2.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
10 // TODO(nweiz): remove this when issue 9140 is fixed.
11 /// A wrapper around [HttpServer] that swallows errors caused by requests
12 /// behaving badly. This provides the following guarantees:
13 ///
14 /// * The [SafeHttpServer.listen] onError callback will only emit server-wide
15 /// errors. It will not emit errors for requests that were unparseable or
16 /// where the connection was closed too soon.
17 /// * [HttpResponse.done] will emit no errors.
18 ///
19 /// The [HttpRequest] data stream can still emit errors.
20 class SafeHttpServer extends StreamView<HttpRequest> implements HttpServer {
21 final HttpServer _inner;
22
23 static Future<SafeHttpServer> bind([String host = "localhost",
24 int port = 0, int backlog = 0]) {
25 return HttpServer.bind(host, port, backlog: backlog)
26 .then((server) => new SafeHttpServer(server));
27 }
28
29 SafeHttpServer(HttpServer server)
30 : super(server),
31 _inner = server;
32
33 Future close({bool force: false}) => _inner.close(force: force);
34
35 InternetAddress get address => _inner.address;
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 {Function onError, void onDone(),
46 bool cancelOnError: false}) {
47 var subscription;
48 subscription = super.listen((request) {
49 onData(new _HttpRequestWrapper(request));
50 }, onError: (error, stackTrace) {
51 // Ignore socket error 104, which is caused by a request being cancelled
52 // before it writes any headers. There's no reason to care about such
53 // requests.
54 if (error is SocketException && error.osError.errorCode == 104) return;
55 // Ignore any parsing errors, which come from malformed requests.
56 if (error is HttpException) return;
57 // Manually handle cancelOnError so the above (ignored) errors don't
58 // cause unsubscription.
59 if (cancelOnError) subscription.cancel();
60 if (onError != null) {
61 if (onError is ZoneBinaryCallback) {
62 onError(error, stackTrace);
63 } else {
64 onError(error);
65 }
66 }
67 }, onDone: onDone);
68 return subscription;
69 }
70 }
71
72 /// A wrapper around [HttpRequest] for the sole purpose of swallowing errors on
73 /// [HttpResponse.done].
74 class _HttpRequestWrapper extends StreamView<List<int>> implements HttpRequest {
75 final HttpRequest _inner;
76 final HttpResponse response;
77
78 _HttpRequestWrapper(HttpRequest inner)
79 : super(inner),
80 _inner = inner,
81 response = new _HttpResponseWrapper(inner.response);
82
83 int get contentLength => _inner.contentLength;
84 String get method => _inner.method;
85 Uri get uri => _inner.uri;
86 Uri get requestedUri => _inner.requestedUri;
87 HttpHeaders get headers => _inner.headers;
88 List<Cookie> get cookies => _inner.cookies;
89 bool get persistentConnection => _inner.persistentConnection;
90 X509Certificate get certificate => _inner.certificate;
91 HttpSession get session => _inner.session;
92 String get protocolVersion => _inner.protocolVersion;
93 HttpConnectionInfo get connectionInfo => _inner.connectionInfo;
94 }
95
96 /// A wrapper around [HttpResponse] for the sole purpose of swallowing errors on
97 /// [done].
98 class _HttpResponseWrapper implements HttpResponse {
99 final HttpResponse _inner;
100 Future<HttpResponse> _done;
101
102 _HttpResponseWrapper(this._inner);
103
104 /// Swallows all errors from writing to the response.
105 Future<HttpResponse> get done {
106 if (_done == null) _done = _inner.done.catchError((_) {});
107 return _done;
108 }
109
110 int get contentLength => _inner.contentLength;
111 set contentLength(int value) {
112 _inner.contentLength = value;
113 }
114
115 int get statusCode => _inner.statusCode;
116 set statusCode(int value) {
117 _inner.statusCode = value;
118 }
119
120 String get reasonPhrase => _inner.reasonPhrase;
121 set reasonPhrase(String value) {
122 _inner.reasonPhrase = value;
123 }
124
125 bool get persistentConnection => _inner.persistentConnection;
126 set persistentConnection(bool value) {
127 _inner.persistentConnection = value;
128 }
129
130 Encoding get encoding => _inner.encoding;
131 set encoding(Encoding value) {
132 _inner.encoding = value;
133 }
134
135 HttpHeaders get headers => _inner.headers;
136 List<Cookie> get cookies => _inner.cookies;
137 Future<Socket> detachSocket() => _inner.detachSocket();
138 HttpConnectionInfo get connectionInfo => _inner.connectionInfo;
139 void add(List<int> data) => _inner.add(data);
140 Future<HttpResponse> addStream(Stream<List<int>> stream) =>
141 _inner.addStream(stream);
142 Future close() => _inner.close();
143 Future flush() => _inner.flush();
144 void write(Object obj) => _inner.write(obj);
145 void writeAll(Iterable objects, [String separator = ""]) =>
146 _inner.writeAll(objects, separator);
147 void writeCharCode(int charCode) => _inner.writeCharCode(charCode);
148 void writeln([Object obj = ""]) => _inner.writeln(obj);
149 void addError(error, [StackTrace stackTrace]) =>
150 _inner.addError(error, stackTrace);
151 }
OLDNEW
« no previous file with comments | « pkg/scheduled_test/lib/scheduled_server.dart ('k') | sdk/lib/_internal/pub/lib/src/oauth2.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698