Chromium Code Reviews| 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 library shelf.response; | 5 library shelf.response; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 | 9 |
| 10 import 'package:http_parser/http_parser.dart'; | 10 import 'package:http_parser/http_parser.dart'; |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 Response.notModified({Map<String, String> headers, | 137 Response.notModified({Map<String, String> headers, |
| 138 Map<String, Object> context}) | 138 Map<String, Object> context}) |
| 139 : this(304, headers: _addHeader( | 139 : this(304, headers: _addHeader( |
| 140 headers, 'date', formatHttpDate(new DateTime.now())), | 140 headers, 'date', formatHttpDate(new DateTime.now())), |
| 141 context: context); | 141 context: context); |
| 142 | 142 |
| 143 /// Constructs a 403 Forbidden response. | 143 /// Constructs a 403 Forbidden response. |
| 144 /// | 144 /// |
| 145 /// This indicates that the server is refusing to fulfill the request. | 145 /// This indicates that the server is refusing to fulfill the request. |
| 146 /// | 146 /// |
| 147 /// [body] is the response body. It may be either a [String], a | 147 /// [body] is the response body. It may be either a [String] or a |
| 148 /// [Stream<List<int>>], or `null` to indicate no body. If it's a [String], | 148 /// [Stream<List<int>>]. |
|
nweiz
2014/09/23 00:09:38
This should still include "or `null`".
kevmoo
2014/09/23 05:00:04
Done.
| |
| 149 /// [encoding] is used to encode it to a [Stream<List<int>>]. It defaults to | 149 /// If it's a [String], [encoding] is used to encode it to a |
| 150 /// UTF-8. | 150 /// [Stream<List<int>>]. The default encoding is UTF-8. |
| 151 /// If it's `null` or not passed, a default error message is used. | |
|
nweiz
2014/09/23 00:09:38
Nit: keep this text nicely line-wrapped.
kevmoo
2014/09/23 05:00:04
Done.
| |
| 151 /// | 152 /// |
| 152 /// If [encoding] is passed, the "encoding" field of the Content-Type header | 153 /// If [encoding] is passed, the "encoding" field of the Content-Type header |
| 153 /// in [headers] will be set appropriately. If there is no existing | 154 /// in [headers] will be set appropriately. If there is no existing |
| 154 /// Content-Type header, it will be set to "application/octet-stream". | 155 /// Content-Type header, it will be set to "application/octet-stream". |
| 155 Response.forbidden(body, {Map<String, String> headers, | 156 Response.forbidden(body, {Map<String, String> headers, |
| 156 Encoding encoding, Map<String, Object> context}) | 157 Encoding encoding, Map<String, Object> context}) |
| 157 : this(403, body: body, headers: headers, | 158 : this(403, |
| 159 headers: body == null ? _adjustErrorHeaders(headers) : headers, | |
| 160 body: body == null ? 'Forbidden' : body, | |
| 158 context: context); | 161 context: context); |
| 159 | 162 |
| 160 /// Constructs a 404 Not Found response. | 163 /// Constructs a 404 Not Found response. |
| 161 /// | 164 /// |
| 162 /// This indicates that the server didn't find any resource matching the | 165 /// This indicates that the server didn't find any resource matching the |
| 163 /// requested URI. | 166 /// requested URI. |
| 164 /// | 167 /// |
| 165 /// [body] is the response body. It may be either a [String], a | 168 /// [body] is the response body. It may be either a [String] or a |
| 166 /// [Stream<List<int>>], or `null` to indicate no body. If it's a [String], | 169 /// [Stream<List<int>>]. |
| 167 /// [encoding] is used to encode it to a [Stream<List<int>>]. It defaults to | 170 /// If it's a [String], [encoding] is used to encode it to a |
| 168 /// UTF-8. | 171 /// [Stream<List<int>>]. The default encoding is UTF-8. |
| 172 /// If it's `null` or not passed, a default error message is used. | |
| 169 /// | 173 /// |
| 170 /// If [encoding] is passed, the "encoding" field of the Content-Type header | 174 /// If [encoding] is passed, the "encoding" field of the Content-Type header |
| 171 /// in [headers] will be set appropriately. If there is no existing | 175 /// in [headers] will be set appropriately. If there is no existing |
| 172 /// Content-Type header, it will be set to "application/octet-stream". | 176 /// Content-Type header, it will be set to "application/octet-stream". |
| 173 Response.notFound(body, {Map<String, String> headers, Encoding encoding, | 177 Response.notFound(body, {Map<String, String> headers, Encoding encoding, |
| 174 Map<String, Object> context}) | 178 Map<String, Object> context}) |
| 175 : this(404, body: body, headers: headers, | 179 : this(404, |
| 180 headers: body == null ? _adjustErrorHeaders(headers) : headers, | |
| 181 body: body == null ? 'Not Found' : body, | |
| 176 context: context); | 182 context: context); |
| 177 | 183 |
| 178 /// Constructs a 500 Internal Server Error response. | 184 /// Constructs a 500 Internal Server Error response. |
| 179 /// | 185 /// |
| 180 /// This indicates that the server had an internal error that prevented it | 186 /// This indicates that the server had an internal error that prevented it |
| 181 /// from fulfilling the request. | 187 /// from fulfilling the request. |
| 182 /// | 188 /// |
| 183 /// [body] is the response body. It may be either a [String], a | 189 /// [body] is the response body. It may be either a [String] or a |
| 184 /// [Stream<List<int>>], or `null` to indicate no body. If it's `null` or not | 190 /// [Stream<List<int>>]. |
| 185 /// passed, a default error message is used. If it's a [String], [encoding] is | 191 /// If it's a [String], [encoding] is used to encode it to a |
| 186 /// used to encode it to a [Stream<List<int>>]. It defaults to UTF-8. | 192 /// [Stream<List<int>>]. The default encoding is UTF-8. |
| 193 /// If it's `null` or not passed, a default error message is used. | |
| 187 /// | 194 /// |
| 188 /// If [encoding] is passed, the "encoding" field of the Content-Type header | 195 /// If [encoding] is passed, the "encoding" field of the Content-Type header |
| 189 /// in [headers] will be set appropriately. If there is no existing | 196 /// in [headers] will be set appropriately. If there is no existing |
| 190 /// Content-Type header, it will be set to "application/octet-stream". | 197 /// Content-Type header, it will be set to "application/octet-stream". |
| 191 Response.internalServerError({body, Map<String, String> headers, | 198 Response.internalServerError({body, Map<String, String> headers, |
| 192 Encoding encoding, Map<String, Object> context}) | 199 Encoding encoding, Map<String, Object> context}) |
| 193 : this(500, | 200 : this(500, |
| 194 headers: body == null ? _adjust500Headers(headers) : headers, | 201 headers: body == null ? _adjustErrorHeaders(headers) : headers, |
| 195 body: body == null ? 'Internal Server Error' : body, | 202 body: body == null ? 'Internal Server Error' : body, |
| 196 context: context); | 203 context: context); |
| 197 | 204 |
| 198 /// Constructs an HTTP response with the given [statusCode]. | 205 /// Constructs an HTTP response with the given [statusCode]. |
| 199 /// | 206 /// |
| 200 /// [statusCode] must be greater than or equal to 100. | 207 /// [statusCode] must be greater than or equal to 100. |
| 201 /// | 208 /// |
| 202 /// [body] is the response body. It may be either a [String], a | 209 /// [body] is the response body. It may be either a [String], a |
| 203 /// [Stream<List<int>>], or `null` to indicate no body. If it's `null` or not | 210 /// [Stream<List<int>>], or `null` to indicate no body. |
| 204 /// passed, a default error message is used. If it's a [String], [encoding] is | 211 /// If it's a [String], [encoding] is used to encode it to a |
| 205 /// used to encode it to a [Stream<List<int>>]. It defaults to UTF-8. | 212 /// [Stream<List<int>>]. The default encoding is UTF-8. |
| 206 /// | 213 /// |
| 207 /// If [encoding] is passed, the "encoding" field of the Content-Type header | 214 /// If [encoding] is passed, the "encoding" field of the Content-Type header |
| 208 /// in [headers] will be set appropriately. If there is no existing | 215 /// in [headers] will be set appropriately. If there is no existing |
| 209 /// Content-Type header, it will be set to "application/octet-stream". | 216 /// Content-Type header, it will be set to "application/octet-stream". |
| 210 Response(this.statusCode, {body, Map<String, String> headers, | 217 Response(this.statusCode, {body, Map<String, String> headers, |
| 211 Encoding encoding, Map<String, Object> context}) | 218 Encoding encoding, Map<String, Object> context}) |
| 212 : super(_bodyToStream(body, encoding), | 219 : super(_bodyToStream(body, encoding), |
| 213 headers: _adjustHeaders(headers, encoding), | 220 headers: _adjustHeaders(headers, encoding), |
| 214 context: context) { | 221 context: context) { |
| 215 if (statusCode < 100) { | 222 if (statusCode < 100) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 276 String value) { | 283 String value) { |
| 277 headers = headers == null ? {} : new Map.from(headers); | 284 headers = headers == null ? {} : new Map.from(headers); |
| 278 headers[name] = value; | 285 headers[name] = value; |
| 279 return headers; | 286 return headers; |
| 280 } | 287 } |
| 281 | 288 |
| 282 /// Adds content-type information to [headers]. | 289 /// Adds content-type information to [headers]. |
| 283 /// | 290 /// |
| 284 /// Returns a new map without modifying [headers]. This is used to add | 291 /// Returns a new map without modifying [headers]. This is used to add |
| 285 /// content-type information when creating a 500 response with a default body. | 292 /// content-type information when creating a 500 response with a default body. |
| 286 Map<String, String> _adjust500Headers(Map<String, String> headers) { | 293 Map<String, String> _adjustErrorHeaders(Map<String, String> headers) { |
| 287 if (headers == null || headers['content-type'] == null) { | 294 if (headers == null || headers['content-type'] == null) { |
| 288 return _addHeader(headers, 'content-type', 'text/plain'); | 295 return _addHeader(headers, 'content-type', 'text/plain'); |
| 289 } | 296 } |
| 290 | 297 |
| 291 var contentType = new MediaType.parse(headers['content-type']) | 298 var contentType = new MediaType.parse(headers['content-type']) |
| 292 .change(mimeType: 'text/plain'); | 299 .change(mimeType: 'text/plain'); |
| 293 return _addHeader(headers, 'content-type', contentType.toString()); | 300 return _addHeader(headers, 'content-type', contentType.toString()); |
| 294 } | 301 } |
| 295 | 302 |
| 296 /// Converts [location], which may be a [String] or a [Uri], to a [String]. | 303 /// Converts [location], which may be a [String] or a [Uri], to a [String]. |
| 297 /// | 304 /// |
| 298 /// Throws an [ArgumentError] if [location] isn't a [String] or a [Uri]. | 305 /// Throws an [ArgumentError] if [location] isn't a [String] or a [Uri]. |
| 299 String _locationToString(location) { | 306 String _locationToString(location) { |
| 300 if (location is String) return location; | 307 if (location is String) return location; |
| 301 if (location is Uri) return location.toString(); | 308 if (location is Uri) return location.toString(); |
| 302 | 309 |
| 303 throw new ArgumentError('Response location must be a String or Uri, was ' | 310 throw new ArgumentError('Response location must be a String or Uri, was ' |
| 304 '"$location".'); | 311 '"$location".'); |
| 305 } | 312 } |
| OLD | NEW |