| OLD | NEW |
| (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:convert'; | |
| 9 import 'dart:io'; | |
| 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 String serverHeader = 'SaftHttpServer'; | |
| 24 Duration idleTimeout = const Duration(seconds: 120); | |
| 25 | |
| 26 static Future<SafeHttpServer> bind([String host = "localhost", | |
| 27 int port = 0, int backlog = 0]) { | |
| 28 return HttpServer.bind(host, port, backlog: backlog) | |
| 29 .then((server) => new SafeHttpServer(server)); | |
| 30 } | |
| 31 | |
| 32 SafeHttpServer(HttpServer server) | |
| 33 : super(server), | |
| 34 _inner = server; | |
| 35 | |
| 36 Future close({bool force: false}) => _inner.close(force: force); | |
| 37 | |
| 38 InternetAddress get address => _inner.address; | |
| 39 int get port => _inner.port; | |
| 40 | |
| 41 set sessionTimeout(int timeout) { | |
| 42 _inner.sessionTimeout = timeout; | |
| 43 } | |
| 44 | |
| 45 HttpConnectionsInfo connectionsInfo() => _inner.connectionsInfo(); | |
| 46 | |
| 47 StreamSubscription<HttpRequest> listen(void onData(HttpRequest value), | |
| 48 {Function onError, void onDone(), | |
| 49 bool cancelOnError: false}) { | |
| 50 var subscription; | |
| 51 subscription = super.listen((request) { | |
| 52 onData(new _HttpRequestWrapper(request)); | |
| 53 }, onError: (error, StackTrace stackTrace) { | |
| 54 // Ignore socket error 104, which is caused by a request being cancelled | |
| 55 // before it writes any headers. There's no reason to care about such | |
| 56 // requests. | |
| 57 if (error is SocketException && error.osError.errorCode == 104) return; | |
| 58 // Ignore any parsing errors, which come from malformed requests. | |
| 59 if (error is HttpException) return; | |
| 60 // Manually handle cancelOnError so the above (ignored) errors don't | |
| 61 // cause unsubscription. | |
| 62 if (cancelOnError) subscription.cancel(); | |
| 63 if (onError != null) { | |
| 64 if (onError is ZoneBinaryCallback) { | |
| 65 onError(error, stackTrace); | |
| 66 } else { | |
| 67 onError(error); | |
| 68 } | |
| 69 } | |
| 70 }, onDone: onDone); | |
| 71 return subscription; | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 /// A wrapper around [HttpRequest] for the sole purpose of swallowing errors on | |
| 76 /// [HttpResponse.done]. | |
| 77 class _HttpRequestWrapper extends StreamView<List<int>> implements HttpRequest { | |
| 78 final HttpRequest _inner; | |
| 79 final HttpResponse response; | |
| 80 | |
| 81 _HttpRequestWrapper(HttpRequest inner) | |
| 82 : super(inner), | |
| 83 _inner = inner, | |
| 84 response = new _HttpResponseWrapper(inner.response); | |
| 85 | |
| 86 int get contentLength => _inner.contentLength; | |
| 87 String get method => _inner.method; | |
| 88 Uri get uri => _inner.uri; | |
| 89 Uri get requestedUri => _inner.requestedUri; | |
| 90 HttpHeaders get headers => _inner.headers; | |
| 91 List<Cookie> get cookies => _inner.cookies; | |
| 92 bool get persistentConnection => _inner.persistentConnection; | |
| 93 X509Certificate get certificate => _inner.certificate; | |
| 94 HttpSession get session => _inner.session; | |
| 95 String get protocolVersion => _inner.protocolVersion; | |
| 96 HttpConnectionInfo get connectionInfo => _inner.connectionInfo; | |
| 97 } | |
| 98 | |
| 99 /// A wrapper around [HttpResponse] for the sole purpose of swallowing errors on | |
| 100 /// [done]. | |
| 101 class _HttpResponseWrapper implements HttpResponse { | |
| 102 final HttpResponse _inner; | |
| 103 Future<HttpResponse> _done; | |
| 104 | |
| 105 _HttpResponseWrapper(this._inner); | |
| 106 | |
| 107 /// Swallows all errors from writing to the response. | |
| 108 Future<HttpResponse> get done { | |
| 109 if (_done == null) _done = _inner.done.catchError((_) {}); | |
| 110 return _done; | |
| 111 } | |
| 112 | |
| 113 int get contentLength => _inner.contentLength; | |
| 114 set contentLength(int value) { | |
| 115 _inner.contentLength = value; | |
| 116 } | |
| 117 | |
| 118 Duration get deadline => _inner.deadline; | |
| 119 void set deadline(Duration value) { | |
| 120 _inner.deadline = value; | |
| 121 } | |
| 122 | |
| 123 int get statusCode => _inner.statusCode; | |
| 124 set statusCode(int value) { | |
| 125 _inner.statusCode = value; | |
| 126 } | |
| 127 | |
| 128 String get reasonPhrase => _inner.reasonPhrase; | |
| 129 set reasonPhrase(String value) { | |
| 130 _inner.reasonPhrase = value; | |
| 131 } | |
| 132 | |
| 133 bool get persistentConnection => _inner.persistentConnection; | |
| 134 set persistentConnection(bool value) { | |
| 135 _inner.persistentConnection = value; | |
| 136 } | |
| 137 | |
| 138 Encoding get encoding => _inner.encoding; | |
| 139 | |
| 140 void set encoding(Encoding value) { | |
| 141 _inner.encoding = value; | |
| 142 } | |
| 143 | |
| 144 HttpHeaders get headers => _inner.headers; | |
| 145 List<Cookie> get cookies => _inner.cookies; | |
| 146 Future<Socket> detachSocket() => _inner.detachSocket(); | |
| 147 HttpConnectionInfo get connectionInfo => _inner.connectionInfo; | |
| 148 void add(List<int> data) => _inner.add(data); | |
| 149 Future<HttpResponse> addStream(Stream<List<int>> stream) => | |
| 150 _inner.addStream(stream); | |
| 151 Future close() => _inner.close(); | |
| 152 Future flush() => _inner.flush(); | |
| 153 void write(Object obj) => _inner.write(obj); | |
| 154 void writeAll(Iterable objects, [String separator = ""]) => | |
| 155 _inner.writeAll(objects, separator); | |
| 156 void writeCharCode(int charCode) => _inner.writeCharCode(charCode); | |
| 157 void writeln([Object obj = ""]) => _inner.writeln(obj); | |
| 158 void addError(error, [StackTrace stackTrace]) => | |
| 159 _inner.addError(error, stackTrace); | |
| 160 Future redirect(Uri location, {int status: HttpStatus.MOVED_TEMPORARILY}) => | |
| 161 _inner.redirect(location, status: status); | |
| 162 } | |
| OLD | NEW |