| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 library safe_http_server; | 5 library safe_http_server; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; |
| 8 import 'dart:io'; | 9 import 'dart:io'; |
| 9 | 10 |
| 10 // TODO(nweiz): remove this when issue 9140 is fixed. | 11 // TODO(nweiz): remove this when issue 9140 is fixed. |
| 11 /// A wrapper around [HttpServer] that swallows errors caused by requests | 12 /// A wrapper around [HttpServer] that swallows errors caused by requests |
| 12 /// behaving badly. This provides the following guarantees: | 13 /// behaving badly. This provides the following guarantees: |
| 13 /// | 14 /// |
| 14 /// * The [SafeHttpServer.listen] onError callback will only emit server-wide | 15 /// * The [SafeHttpServer.listen] onError callback will only emit server-wide |
| 15 /// errors. It will not emit errors for requests that were unparseable or | 16 /// errors. It will not emit errors for requests that were unparseable or |
| 16 /// where the connection was closed too soon. | 17 /// where the connection was closed too soon. |
| 17 /// * [HttpResponse.done] will emit no errors. | 18 /// * [HttpResponse.done] will emit no errors. |
| 18 /// | 19 /// |
| 19 /// The [HttpRequest] data stream can still emit errors. | 20 /// The [HttpRequest] data stream can still emit errors. |
| 20 class SafeHttpServer extends StreamView<HttpRequest> implements HttpServer { | 21 class SafeHttpServer extends StreamView<HttpRequest> implements HttpServer { |
| 21 final HttpServer _inner; | 22 final HttpServer _inner; |
| 23 String serverHeader = 'SaftHttpServer'; |
| 24 Duration idleTimeout = const Duration(seconds: 120); |
| 22 | 25 |
| 23 static Future<SafeHttpServer> bind([String host = "localhost", | 26 static Future<SafeHttpServer> bind([String host = "localhost", |
| 24 int port = 0, int backlog = 0]) { | 27 int port = 0, int backlog = 0]) { |
| 25 return HttpServer.bind(host, port, backlog: backlog) | 28 return HttpServer.bind(host, port, backlog: backlog) |
| 26 .then((server) => new SafeHttpServer(server)); | 29 .then((server) => new SafeHttpServer(server)); |
| 27 } | 30 } |
| 28 | 31 |
| 29 SafeHttpServer(HttpServer server) | 32 SafeHttpServer(HttpServer server) |
| 30 : super(server), | 33 : super(server), |
| 31 _inner = server; | 34 _inner = server; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 Future<HttpResponse> get done { | 108 Future<HttpResponse> get done { |
| 106 if (_done == null) _done = _inner.done.catchError((_) {}); | 109 if (_done == null) _done = _inner.done.catchError((_) {}); |
| 107 return _done; | 110 return _done; |
| 108 } | 111 } |
| 109 | 112 |
| 110 int get contentLength => _inner.contentLength; | 113 int get contentLength => _inner.contentLength; |
| 111 set contentLength(int value) { | 114 set contentLength(int value) { |
| 112 _inner.contentLength = value; | 115 _inner.contentLength = value; |
| 113 } | 116 } |
| 114 | 117 |
| 118 Duration get deadline => _inner.deadline; |
| 119 void set deadline(Duration value) { |
| 120 _inner.deadline = value; |
| 121 } |
| 122 |
| 115 int get statusCode => _inner.statusCode; | 123 int get statusCode => _inner.statusCode; |
| 116 set statusCode(int value) { | 124 set statusCode(int value) { |
| 117 _inner.statusCode = value; | 125 _inner.statusCode = value; |
| 118 } | 126 } |
| 119 | 127 |
| 120 String get reasonPhrase => _inner.reasonPhrase; | 128 String get reasonPhrase => _inner.reasonPhrase; |
| 121 set reasonPhrase(String value) { | 129 set reasonPhrase(String value) { |
| 122 _inner.reasonPhrase = value; | 130 _inner.reasonPhrase = value; |
| 123 } | 131 } |
| 124 | 132 |
| 125 bool get persistentConnection => _inner.persistentConnection; | 133 bool get persistentConnection => _inner.persistentConnection; |
| 126 set persistentConnection(bool value) { | 134 set persistentConnection(bool value) { |
| 127 _inner.persistentConnection = value; | 135 _inner.persistentConnection = value; |
| 128 } | 136 } |
| 129 | 137 |
| 130 Encoding get encoding => _inner.encoding; | 138 Encoding get encoding => _inner.encoding; |
| 131 set encoding(Encoding value) { | 139 |
| 140 void set encoding(Encoding value) { |
| 132 _inner.encoding = value; | 141 _inner.encoding = value; |
| 133 } | 142 } |
| 134 | 143 |
| 135 HttpHeaders get headers => _inner.headers; | 144 HttpHeaders get headers => _inner.headers; |
| 136 List<Cookie> get cookies => _inner.cookies; | 145 List<Cookie> get cookies => _inner.cookies; |
| 137 Future<Socket> detachSocket() => _inner.detachSocket(); | 146 Future<Socket> detachSocket() => _inner.detachSocket(); |
| 138 HttpConnectionInfo get connectionInfo => _inner.connectionInfo; | 147 HttpConnectionInfo get connectionInfo => _inner.connectionInfo; |
| 139 void add(List<int> data) => _inner.add(data); | 148 void add(List<int> data) => _inner.add(data); |
| 140 Future<HttpResponse> addStream(Stream<List<int>> stream) => | 149 Future<HttpResponse> addStream(Stream<List<int>> stream) => |
| 141 _inner.addStream(stream); | 150 _inner.addStream(stream); |
| 142 Future close() => _inner.close(); | 151 Future close() => _inner.close(); |
| 143 Future flush() => _inner.flush(); | 152 Future flush() => _inner.flush(); |
| 144 void write(Object obj) => _inner.write(obj); | 153 void write(Object obj) => _inner.write(obj); |
| 145 void writeAll(Iterable objects, [String separator = ""]) => | 154 void writeAll(Iterable objects, [String separator = ""]) => |
| 146 _inner.writeAll(objects, separator); | 155 _inner.writeAll(objects, separator); |
| 147 void writeCharCode(int charCode) => _inner.writeCharCode(charCode); | 156 void writeCharCode(int charCode) => _inner.writeCharCode(charCode); |
| 148 void writeln([Object obj = ""]) => _inner.writeln(obj); | 157 void writeln([Object obj = ""]) => _inner.writeln(obj); |
| 149 void addError(error, [StackTrace stackTrace]) => | 158 void addError(error, [StackTrace stackTrace]) => |
| 150 _inner.addError(error, stackTrace); | 159 _inner.addError(error, stackTrace); |
| 160 Future redirect(Uri location, {int status: HttpStatus.MOVED_TEMPORARILY}) => |
| 161 _inner.redirect(location, status: status); |
| 151 } | 162 } |
| OLD | NEW |