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

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

Issue 14297021: Move pub into sdk/lib/_internal. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Disallow package: imports of pub. Created 7 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 | « utils/pub/resource/certs/pkcs11.txt ('k') | utils/pub/sdk.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 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(error), void onDone(),
46 bool cancelOnError: false}) {
47 var subscription;
48 subscription = super.listen((request) {
49 onData(new _HttpRequestWrapper(request));
50 }, onError: (error) {
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 SocketIOException && error.osError.errorCode == 104) return;
55 // Ignore any parsing errors, which come from malformed requests.
56 if (error is HttpParserException) return;
57 // Manually handle cancelOnError so the above (ignored) errors don't
58 // cause unsubscription.
59 if (cancelOnError) subscription.cancel();
60 if (onError != null) onError(error);
61 }, onDone: onDone);
62 return subscription;
63 }
64 }
65
66 /// A wrapper around [HttpRequest] for the sole purpose of swallowing errors on
67 /// [HttpResponse.done].
68 class _HttpRequestWrapper extends StreamView<List<int>> implements HttpRequest {
69 final HttpRequest _inner;
70 final HttpResponse response;
71
72 _HttpRequestWrapper(HttpRequest inner)
73 : super(inner),
74 _inner = inner,
75 response = new _HttpResponseWrapper(inner.response);
76
77 int get contentLength => _inner.contentLength;
78 String get method => _inner.method;
79 Uri get uri => _inner.uri;
80 Map<String, String> get queryParameters => _inner.queryParameters;
81 HttpHeaders get headers => _inner.headers;
82 List<Cookie> get cookies => _inner.cookies;
83 bool get persistentConnection => _inner.persistentConnection;
84 X509Certificate get certificate => _inner.certificate;
85 HttpSession get session => _inner.session;
86 String get protocolVersion => _inner.protocolVersion;
87 HttpConnectionInfo get connectionInfo => _inner.connectionInfo;
88 }
89
90 /// A wrapper around [HttpResponse] for the sole purpose of swallowing errors on
91 /// [done].
92 class _HttpResponseWrapper implements HttpResponse {
93 final HttpResponse _inner;
94 Future<HttpResponse> _done;
95
96 _HttpResponseWrapper(this._inner);
97
98 /// Swallows all errors from writing to the response.
99 Future<HttpResponse> get done {
100 if (_done == null) _done = _inner.done.catchError((_) {});
101 return _done;
102 }
103
104 int get contentLength => _inner.contentLength;
105 set contentLength(int value) {
106 _inner.contentLength = value;
107 }
108
109 int get statusCode => _inner.statusCode;
110 set statusCode(int value) {
111 _inner.statusCode = value;
112 }
113
114 String get reasonPhrase => _inner.reasonPhrase;
115 set reasonPhrase(String value) {
116 _inner.reasonPhrase = value;
117 }
118
119 bool get persistentConnection => _inner.persistentConnection;
120 set persistentConnection(bool value) {
121 _inner.persistentConnection = value;
122 }
123
124 Encoding get encoding => _inner.encoding;
125 set encoding(Encoding value) {
126 _inner.encoding = value;
127 }
128
129 HttpHeaders get headers => _inner.headers;
130 List<Cookie> get cookies => _inner.cookies;
131 Future<Socket> detachSocket() => _inner.detachSocket();
132 HttpConnectionInfo get connectionInfo => _inner.connectionInfo;
133 void add(List<int> data) => _inner.add(data);
134 Future<HttpResponse> addStream(Stream<List<int>> stream) =>
135 _inner.addStream(stream);
136 Future close() => _inner.close();
137 void write(Object obj) => _inner.write(obj);
138 void writeAll(Iterable objects, [String separator = ""]) =>
139 _inner.writeAll(objects, separator);
140 void writeCharCode(int charCode) => _inner.writeCharCode(charCode);
141 void writeln([Object obj = ""]) => _inner.writeln(obj);
142 void addError(error) => _inner.addError(error);
143 }
OLDNEW
« no previous file with comments | « utils/pub/resource/certs/pkcs11.txt ('k') | utils/pub/sdk.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698