| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 if (response == null) { | 58 if (response == null) { |
| 59 response = _logError('null response from handler'); | 59 response = _logError('null response from handler'); |
| 60 } | 60 } |
| 61 | 61 |
| 62 return _writeResponse(response, request.response); | 62 return _writeResponse(response, request.response); |
| 63 }); | 63 }); |
| 64 } | 64 } |
| 65 | 65 |
| 66 /// Creates a new [Request] from the provided [HttpRequest]. | 66 /// Creates a new [Request] from the provided [HttpRequest]. |
| 67 Request _fromHttpRequest(HttpRequest request) { | 67 Request _fromHttpRequest(HttpRequest request) { |
| 68 //TODO(kevmoo): make headers case-insensitive | |
| 69 var headers = {}; | 68 var headers = {}; |
| 70 request.headers.forEach((k, v) { | 69 request.headers.forEach((k, v) { |
| 71 // Multiple header values are joined with commas. | 70 // Multiple header values are joined with commas. |
| 72 // See http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-21#page-22 | 71 // See http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-21#page-22 |
| 73 headers[k] = v.join(','); | 72 headers[k] = v.join(','); |
| 74 }); | 73 }); |
| 75 | 74 |
| 76 return new Request(request.uri.path, request.uri.query, request.method, | 75 return new Request(request.protocolVersion, request.method, headers, |
| 77 '', request.protocolVersion, request.requestedUri, | 76 request.requestedUri, body: request); |
| 78 headers, body: request); | |
| 79 } | 77 } |
| 80 | 78 |
| 81 Future _writeResponse(Response response, HttpResponse httpResponse) { | 79 Future _writeResponse(Response response, HttpResponse httpResponse) { |
| 82 httpResponse.statusCode = response.statusCode; | 80 httpResponse.statusCode = response.statusCode; |
| 83 | 81 |
| 84 response.headers.forEach((header, value) { | 82 response.headers.forEach((header, value) { |
| 85 if (value == null) return; | 83 if (value == null) return; |
| 86 httpResponse.headers.set(header, value); | 84 httpResponse.headers.set(header, value); |
| 87 }); | 85 }); |
| 88 | 86 |
| 89 if (response.headers[HttpHeaders.SERVER] == null) { | 87 if (response.headers[HttpHeaders.SERVER] == null) { |
| 90 var value = httpResponse.headers.value(HttpHeaders.SERVER); | 88 var value = httpResponse.headers.value(HttpHeaders.SERVER); |
| 91 httpResponse.headers.set(HttpHeaders.SERVER, '$value with Shelf'); | 89 httpResponse.headers.set(HttpHeaders.SERVER, '$value with Shelf'); |
| 92 } | 90 } |
| 93 return httpResponse.addStream(response.read()) | 91 return httpResponse.addStream(response.read()) |
| 94 .then((_) => httpResponse.close()); | 92 .then((_) => httpResponse.close()); |
| 95 } | 93 } |
| 96 | 94 |
| 97 // TODO(kevmoo) A developer mode is needed to include error info in response | 95 // TODO(kevmoo) A developer mode is needed to include error info in response |
| 98 // TODO(kevmoo) Make error output plugable. stderr, logging, etc | 96 // TODO(kevmoo) Make error output plugable. stderr, logging, etc |
| 99 Response _logError(String message) { | 97 Response _logError(String message) { |
| 100 stderr.writeln('ERROR - ${new DateTime.now()}'); | 98 stderr.writeln('ERROR - ${new DateTime.now()}'); |
| 101 stderr.writeln(message); | 99 stderr.writeln(message); |
| 102 return new Response.internalServerError(); | 100 return new Response.internalServerError(); |
| 103 } | 101 } |
| OLD | NEW |