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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 } | 80 } |
81 | 81 |
82 Future _writeResponse(Response response, HttpResponse httpResponse) { | 82 Future _writeResponse(Response response, HttpResponse httpResponse) { |
83 httpResponse.statusCode = response.statusCode; | 83 httpResponse.statusCode = response.statusCode; |
84 | 84 |
85 response.headers.forEach((header, value) { | 85 response.headers.forEach((header, value) { |
86 if (value == null) return; | 86 if (value == null) return; |
87 httpResponse.headers.set(header, value); | 87 httpResponse.headers.set(header, value); |
88 }); | 88 }); |
89 | 89 |
90 if (response.headers[HttpHeaders.SERVER] == null) { | |
91 var value = httpResponse.headers.value(HttpHeaders.SERVER); | |
92 httpResponse.headers.set(HttpHeaders.SERVER, '$value with Shelf'); | |
93 } | |
nweiz
2014/05/15 22:21:32
I still like the intent of this code, even if it d
| |
94 return httpResponse.addStream(response.read()) | 90 return httpResponse.addStream(response.read()) |
95 .then((_) => httpResponse.close()); | 91 .then((_) => httpResponse.close()); |
96 } | 92 } |
97 | 93 |
98 // TODO(kevmoo) A developer mode is needed to include error info in response | 94 // TODO(kevmoo) A developer mode is needed to include error info in response |
99 // TODO(kevmoo) Make error output plugable. stderr, logging, etc | 95 // TODO(kevmoo) Make error output plugable. stderr, logging, etc |
100 Response _logError(String message, [StackTrace stackTrace]) { | 96 Response _logError(String message, [StackTrace stackTrace]) { |
101 var chain = new Chain.current(); | 97 var chain = new Chain.current(); |
102 if (stackTrace != null) { | 98 if (stackTrace != null) { |
103 chain = new Chain.forTrace(stackTrace); | 99 chain = new Chain.forTrace(stackTrace); |
104 } | 100 } |
105 chain = chain | 101 chain = chain |
106 .foldFrames((frame) => frame.isCore || frame.package == 'shelf') | 102 .foldFrames((frame) => frame.isCore || frame.package == 'shelf') |
107 .terse; | 103 .terse; |
108 | 104 |
109 stderr.writeln('ERROR - ${new DateTime.now()}'); | 105 stderr.writeln('ERROR - ${new DateTime.now()}'); |
110 stderr.writeln(message); | 106 stderr.writeln(message); |
111 stderr.writeln(chain); | 107 stderr.writeln(chain); |
112 return new Response.internalServerError(); | 108 return new Response.internalServerError(); |
113 } | 109 } |
OLD | NEW |