| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 /// A Shelf adapter for handling [HttpRequest] objects from `dart:io`. | 5 /// A Shelf adapter for handling [HttpRequest] objects from `dart:io`. |
| 6 /// | 6 /// |
| 7 /// One can provide an instance of [HttpServer] as the `requests` parameter in | 7 /// One can provide an instance of [HttpServer] as the `requests` parameter in |
| 8 /// [serveRequests]. | 8 /// [serveRequests]. |
| 9 library shelf.io; | 9 library shelf.io; |
| 10 | 10 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 return HttpServer.bind(address, port, backlog: backlog).then((server) { | 27 return HttpServer.bind(address, port, backlog: backlog).then((server) { |
| 28 serveRequests(server, handler); | 28 serveRequests(server, handler); |
| 29 return server; | 29 return server; |
| 30 }); | 30 }); |
| 31 } | 31 } |
| 32 | 32 |
| 33 /// Serve a [Stream] of [HttpRequest]s. | 33 /// Serve a [Stream] of [HttpRequest]s. |
| 34 /// | 34 /// |
| 35 /// [HttpServer] implements [Stream<HttpRequest>] so it can be passed directly | 35 /// [HttpServer] implements [Stream<HttpRequest>] so it can be passed directly |
| 36 /// to [serveRequests]. | 36 /// to [serveRequests]. |
| 37 /// |
| 38 /// Errors thrown by [handler] while serving a request will be printed to the |
| 39 /// console and cause a 500 response with no body. Errors thrown asynchronously |
| 40 /// by [handler] will be printed to the console or, if there's an active error |
| 41 /// zone, passed to that zone. |
| 37 void serveRequests(Stream<HttpRequest> requests, Handler handler) { | 42 void serveRequests(Stream<HttpRequest> requests, Handler handler) { |
| 38 requests.listen((request) => handleRequest(request, handler)); | 43 catchTopLevelErrors(() { |
| 44 requests.listen((request) => handleRequest(request, handler)); |
| 45 }, (error, stackTrace) { |
| 46 _logError('Asynchronous error\n$error', stackTrace); |
| 47 }); |
| 39 } | 48 } |
| 40 | 49 |
| 41 /// Uses [handler] to handle [request]. | 50 /// Uses [handler] to handle [request]. |
| 42 /// | 51 /// |
| 43 /// Returns a [Future] which completes when the request has been handled. | 52 /// Returns a [Future] which completes when the request has been handled. |
| 44 Future handleRequest(HttpRequest request, Handler handler) { | 53 Future handleRequest(HttpRequest request, Handler handler) { |
| 45 var shelfRequest = _fromHttpRequest(request); | 54 var shelfRequest = _fromHttpRequest(request); |
| 46 | 55 |
| 47 return syncFuture(() => handler(shelfRequest)) | 56 return syncFuture(() => handler(shelfRequest)) |
| 48 .catchError((error, stackTrace) { | 57 .catchError((error, stackTrace) { |
| 49 var chain = new Chain.current(); | 58 return _logError('Error thrown by handler\n$error', stackTrace); |
| 50 if (stackTrace != null) { | |
| 51 chain = new Chain.forTrace(stackTrace) | |
| 52 .foldFrames((frame) => frame.isCore || frame.package == 'shelf') | |
| 53 .terse; | |
| 54 } | |
| 55 | |
| 56 return _logError('Error thrown by handler\n$error\n$chain'); | |
| 57 }).then((response) { | 59 }).then((response) { |
| 58 if (response == null) { | 60 if (response == null) { |
| 59 response = _logError('null response from handler'); | 61 response = _logError('null response from handler'); |
| 60 } | 62 } |
| 61 | 63 |
| 62 return _writeResponse(response, request.response); | 64 return _writeResponse(response, request.response); |
| 63 }); | 65 }); |
| 64 } | 66 } |
| 65 | 67 |
| 66 /// Creates a new [Request] from the provided [HttpRequest]. | 68 /// Creates a new [Request] from the provided [HttpRequest]. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 88 if (response.headers[HttpHeaders.SERVER] == null) { | 90 if (response.headers[HttpHeaders.SERVER] == null) { |
| 89 var value = httpResponse.headers.value(HttpHeaders.SERVER); | 91 var value = httpResponse.headers.value(HttpHeaders.SERVER); |
| 90 httpResponse.headers.set(HttpHeaders.SERVER, '$value with Shelf'); | 92 httpResponse.headers.set(HttpHeaders.SERVER, '$value with Shelf'); |
| 91 } | 93 } |
| 92 return httpResponse.addStream(response.read()) | 94 return httpResponse.addStream(response.read()) |
| 93 .then((_) => httpResponse.close()); | 95 .then((_) => httpResponse.close()); |
| 94 } | 96 } |
| 95 | 97 |
| 96 // TODO(kevmoo) A developer mode is needed to include error info in response | 98 // TODO(kevmoo) A developer mode is needed to include error info in response |
| 97 // TODO(kevmoo) Make error output plugable. stderr, logging, etc | 99 // TODO(kevmoo) Make error output plugable. stderr, logging, etc |
| 98 Response _logError(String message) { | 100 Response _logError(String message, [StackTrace stackTrace]) { |
| 101 var chain = new Chain.current(); |
| 102 if (stackTrace != null) { |
| 103 chain = new Chain.forTrace(stackTrace); |
| 104 } |
| 105 chain = chain |
| 106 .foldFrames((frame) => frame.isCore || frame.package == 'shelf') |
| 107 .terse; |
| 108 |
| 99 stderr.writeln('ERROR - ${new DateTime.now()}'); | 109 stderr.writeln('ERROR - ${new DateTime.now()}'); |
| 100 stderr.writeln(message); | 110 stderr.writeln(message); |
| 111 stderr.writeln(chain); |
| 101 return new Response.internalServerError(); | 112 return new Response.internalServerError(); |
| 102 } | 113 } |
| OLD | NEW |