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.method, request.requestedUri, |
77 '', request.protocolVersion, request.requestedUri, | 76 protocolVersion: request.protocolVersion, headers: headers, |
78 headers, body: request); | 77 body: request); |
79 } | 78 } |
80 | 79 |
81 Future _writeResponse(Response response, HttpResponse httpResponse) { | 80 Future _writeResponse(Response response, HttpResponse httpResponse) { |
82 httpResponse.statusCode = response.statusCode; | 81 httpResponse.statusCode = response.statusCode; |
83 | 82 |
84 response.headers.forEach((header, value) { | 83 response.headers.forEach((header, value) { |
85 if (value == null) return; | 84 if (value == null) return; |
86 httpResponse.headers.set(header, value); | 85 httpResponse.headers.set(header, value); |
87 }); | 86 }); |
88 | 87 |
89 if (response.headers[HttpHeaders.SERVER] == null) { | 88 if (response.headers[HttpHeaders.SERVER] == null) { |
90 var value = httpResponse.headers.value(HttpHeaders.SERVER); | 89 var value = httpResponse.headers.value(HttpHeaders.SERVER); |
91 httpResponse.headers.set(HttpHeaders.SERVER, '$value with Shelf'); | 90 httpResponse.headers.set(HttpHeaders.SERVER, '$value with Shelf'); |
92 } | 91 } |
93 return httpResponse.addStream(response.read()) | 92 return httpResponse.addStream(response.read()) |
94 .then((_) => httpResponse.close()); | 93 .then((_) => httpResponse.close()); |
95 } | 94 } |
96 | 95 |
97 // TODO(kevmoo) A developer mode is needed to include error info in response | 96 // TODO(kevmoo) A developer mode is needed to include error info in response |
98 // TODO(kevmoo) Make error output plugable. stderr, logging, etc | 97 // TODO(kevmoo) Make error output plugable. stderr, logging, etc |
99 Response _logError(String message) { | 98 Response _logError(String message) { |
100 stderr.writeln('ERROR - ${new DateTime.now()}'); | 99 stderr.writeln('ERROR - ${new DateTime.now()}'); |
101 stderr.writeln(message); | 100 stderr.writeln(message); |
102 return new Response.internalServerError(); | 101 return new Response.internalServerError(); |
103 } | 102 } |
OLD | NEW |