| 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 /// | 9 /// |
| 10 /// The `dart:io` adapter supports request hijacking; see [Request.hijack]. | 10 /// The `dart:io` adapter supports request hijacking; see [Request.hijack]. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 requests.listen((request) => handleRequest(request, handler)); | 46 requests.listen((request) => handleRequest(request, handler)); |
| 47 }, (error, stackTrace) { | 47 }, (error, stackTrace) { |
| 48 _logError('Asynchronous error\n$error', stackTrace); | 48 _logError('Asynchronous error\n$error', stackTrace); |
| 49 }); | 49 }); |
| 50 } | 50 } |
| 51 | 51 |
| 52 /// Uses [handler] to handle [request]. | 52 /// Uses [handler] to handle [request]. |
| 53 /// | 53 /// |
| 54 /// Returns a [Future] which completes when the request has been handled. | 54 /// Returns a [Future] which completes when the request has been handled. |
| 55 Future handleRequest(HttpRequest request, Handler handler) { | 55 Future handleRequest(HttpRequest request, Handler handler) { |
| 56 var shelfRequest = _fromHttpRequest(request); | 56 var shelfRequest; |
| 57 try { |
| 58 shelfRequest = _fromHttpRequest(request); |
| 59 } catch (error, stackTrace) { |
| 60 var response = _logError('Error parsing request.\n$error', stackTrace); |
| 61 return _writeResponse(response, request.response); |
| 62 } |
| 57 | 63 |
| 58 // TODO(nweiz): abstract out hijack handling to make it easier to implement an | 64 // TODO(nweiz): abstract out hijack handling to make it easier to implement an |
| 59 // adapter. | 65 // adapter. |
| 60 return syncFuture(() => handler(shelfRequest)) | 66 return syncFuture(() => handler(shelfRequest)) |
| 61 .catchError((error, stackTrace) { | 67 .catchError((error, stackTrace) { |
| 62 if (error is HijackException) { | 68 if (error is HijackException) { |
| 63 // A HijackException should bypass the response-writing logic entirely. | 69 // A HijackException should bypass the response-writing logic entirely. |
| 64 if (!shelfRequest.canHijack) throw error; | 70 if (!shelfRequest.canHijack) throw error; |
| 65 | 71 |
| 66 // If the request wasn't hijacked, we shouldn't be seeing this exception. | 72 // If the request wasn't hijacked, we shouldn't be seeing this exception. |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 } | 144 } |
| 139 chain = chain | 145 chain = chain |
| 140 .foldFrames((frame) => frame.isCore || frame.package == 'shelf') | 146 .foldFrames((frame) => frame.isCore || frame.package == 'shelf') |
| 141 .terse; | 147 .terse; |
| 142 | 148 |
| 143 stderr.writeln('ERROR - ${new DateTime.now()}'); | 149 stderr.writeln('ERROR - ${new DateTime.now()}'); |
| 144 stderr.writeln(message); | 150 stderr.writeln(message); |
| 145 stderr.writeln(chain); | 151 stderr.writeln(chain); |
| 146 return new Response.internalServerError(); | 152 return new Response.internalServerError(); |
| 147 } | 153 } |
| OLD | NEW |