| 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:collection/wrappers.dart'; | 10 import 'package:collection/wrappers.dart'; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 /// This indicates that the request has succeeded. | 47 /// This indicates that the request has succeeded. |
| 48 /// | 48 /// |
| 49 /// [body] is the response body. It may be either a [String], a | 49 /// [body] is the response body. It may be either a [String], a |
| 50 /// [Stream<List<int>>], or `null` to indicate no body. If it's a [String], | 50 /// [Stream<List<int>>], or `null` to indicate no body. If it's a [String], |
| 51 /// [encoding] is used to encode it to a [Stream<List<int>>]. It defaults to | 51 /// [encoding] is used to encode it to a [Stream<List<int>>]. It defaults to |
| 52 /// UTF-8. | 52 /// UTF-8. |
| 53 /// | 53 /// |
| 54 /// If [encoding] is passed, the "encoding" field of the Content-Type header | 54 /// If [encoding] is passed, the "encoding" field of the Content-Type header |
| 55 /// in [headers] will be set appropriately. If there is no existing | 55 /// in [headers] will be set appropriately. If there is no existing |
| 56 /// Content-Type header, it will be set to "application/octet-stream". | 56 /// Content-Type header, it will be set to "application/octet-stream". |
| 57 Response.ok(body, {Map<String, String> headers, Encoding encoding}) | 57 Response.ok(body, {Map<String, String> headers, Encoding encoding, |
| 58 : this(200, body: body, headers: headers, encoding: encoding); | 58 Map<String, Object> context}) |
| 59 : this(200, body: body, headers: headers, encoding: encoding, |
| 60 context: context); |
| 59 | 61 |
| 60 /// Constructs a 301 Moved Permanently response. | 62 /// Constructs a 301 Moved Permanently response. |
| 61 /// | 63 /// |
| 62 /// This indicates that the requested resource has moved permanently to a new | 64 /// This indicates that the requested resource has moved permanently to a new |
| 63 /// URI. [location] is that URI; it can be either a [String] or a [Uri]. It's | 65 /// URI. [location] is that URI; it can be either a [String] or a [Uri]. It's |
| 64 /// automatically set as the Location header in [headers]. | 66 /// automatically set as the Location header in [headers]. |
| 65 /// | 67 /// |
| 66 /// [body] is the response body. It may be either a [String], a | 68 /// [body] is the response body. It may be either a [String], a |
| 67 /// [Stream<List<int>>], or `null` to indicate no body. If it's a [String], | 69 /// [Stream<List<int>>], or `null` to indicate no body. If it's a [String], |
| 68 /// [encoding] is used to encode it to a [Stream<List<int>>]. It defaults to | 70 /// [encoding] is used to encode it to a [Stream<List<int>>]. It defaults to |
| 69 /// UTF-8. | 71 /// UTF-8. |
| 70 /// | 72 /// |
| 71 /// If [encoding] is passed, the "encoding" field of the Content-Type header | 73 /// If [encoding] is passed, the "encoding" field of the Content-Type header |
| 72 /// in [headers] will be set appropriately. If there is no existing | 74 /// in [headers] will be set appropriately. If there is no existing |
| 73 /// Content-Type header, it will be set to "application/octet-stream". | 75 /// Content-Type header, it will be set to "application/octet-stream". |
| 74 Response.movedPermanently(location, {body, Map<String, String> headers, | 76 Response.movedPermanently(location, {body, Map<String, String> headers, |
| 75 Encoding encoding}) | 77 Encoding encoding, Map<String, Object> context}) |
| 76 : this._redirect(301, location, body, headers, encoding); | 78 : this._redirect(301, location, body, headers, encoding, |
| 79 context: context); |
| 77 | 80 |
| 78 /// Constructs a 302 Found response. | 81 /// Constructs a 302 Found response. |
| 79 /// | 82 /// |
| 80 /// This indicates that the requested resource has moved temporarily to a new | 83 /// This indicates that the requested resource has moved temporarily to a new |
| 81 /// URI. [location] is that URI; it can be either a [String] or a [Uri]. It's | 84 /// URI. [location] is that URI; it can be either a [String] or a [Uri]. It's |
| 82 /// automatically set as the Location header in [headers]. | 85 /// automatically set as the Location header in [headers]. |
| 83 /// | 86 /// |
| 84 /// [body] is the response body. It may be either a [String], a | 87 /// [body] is the response body. It may be either a [String], a |
| 85 /// [Stream<List<int>>], or `null` to indicate no body. If it's a [String], | 88 /// [Stream<List<int>>], or `null` to indicate no body. If it's a [String], |
| 86 /// [encoding] is used to encode it to a [Stream<List<int>>]. It defaults to | 89 /// [encoding] is used to encode it to a [Stream<List<int>>]. It defaults to |
| 87 /// UTF-8. | 90 /// UTF-8. |
| 88 /// | 91 /// |
| 89 /// If [encoding] is passed, the "encoding" field of the Content-Type header | 92 /// If [encoding] is passed, the "encoding" field of the Content-Type header |
| 90 /// in [headers] will be set appropriately. If there is no existing | 93 /// in [headers] will be set appropriately. If there is no existing |
| 91 /// Content-Type header, it will be set to "application/octet-stream". | 94 /// Content-Type header, it will be set to "application/octet-stream". |
| 92 Response.found(location, {body, Map<String, String> headers, | 95 Response.found(location, {body, Map<String, String> headers, |
| 93 Encoding encoding}) | 96 Encoding encoding, Map<String, Object> context}) |
| 94 : this._redirect(302, location, body, headers, encoding); | 97 : this._redirect(302, location, body, headers, encoding, |
| 98 context: context); |
| 95 | 99 |
| 96 /// Constructs a 303 See Other response. | 100 /// Constructs a 303 See Other response. |
| 97 /// | 101 /// |
| 98 /// This indicates that the response to the request should be retrieved using | 102 /// This indicates that the response to the request should be retrieved using |
| 99 /// a GET request to a new URI. [location] is that URI; it can be either a | 103 /// a GET request to a new URI. [location] is that URI; it can be either a |
| 100 /// [String] or a [Uri]. It's automatically set as the Location header in | 104 /// [String] or a [Uri]. It's automatically set as the Location header in |
| 101 /// [headers]. | 105 /// [headers]. |
| 102 /// | 106 /// |
| 103 /// [body] is the response body. It may be either a [String], a | 107 /// [body] is the response body. It may be either a [String], a |
| 104 /// [Stream<List<int>>], or `null` to indicate no body. If it's a [String], | 108 /// [Stream<List<int>>], or `null` to indicate no body. If it's a [String], |
| 105 /// [encoding] is used to encode it to a [Stream<List<int>>]. It defaults to | 109 /// [encoding] is used to encode it to a [Stream<List<int>>]. It defaults to |
| 106 /// UTF-8. | 110 /// UTF-8. |
| 107 /// | 111 /// |
| 108 /// If [encoding] is passed, the "encoding" field of the Content-Type header | 112 /// If [encoding] is passed, the "encoding" field of the Content-Type header |
| 109 /// in [headers] will be set appropriately. If there is no existing | 113 /// in [headers] will be set appropriately. If there is no existing |
| 110 /// Content-Type header, it will be set to "application/octet-stream". | 114 /// Content-Type header, it will be set to "application/octet-stream". |
| 111 Response.seeOther(location, {body, Map<String, String> headers, | 115 Response.seeOther(location, {body, Map<String, String> headers, |
| 112 Encoding encoding}) | 116 Encoding encoding, Map<String, Object> context}) |
| 113 : this._redirect(303, location, body, headers, encoding); | 117 : this._redirect(303, location, body, headers, encoding, |
| 118 context: context); |
| 114 | 119 |
| 115 /// Constructs a helper constructor for redirect responses. | 120 /// Constructs a helper constructor for redirect responses. |
| 116 Response._redirect(int statusCode, location, body, | 121 Response._redirect(int statusCode, location, body, |
| 117 Map<String, String> headers, Encoding encoding) | 122 Map<String, String> headers, Encoding encoding, |
| 123 { Map<String, Object> context }) |
| 118 : this(statusCode, | 124 : this(statusCode, |
| 119 body: body, | 125 body: body, |
| 120 encoding: encoding, | 126 encoding: encoding, |
| 121 headers: _addHeader( | 127 headers: _addHeader( |
| 122 headers, 'location', _locationToString(location))); | 128 headers, 'location', _locationToString(location)), |
| 129 context: context); |
| 123 | 130 |
| 124 /// Constructs a 304 Not Modified response. | 131 /// Constructs a 304 Not Modified response. |
| 125 /// | 132 /// |
| 126 /// This is used to respond to a conditional GET request that provided | 133 /// This is used to respond to a conditional GET request that provided |
| 127 /// information used to determine whether the requested resource has changed | 134 /// information used to determine whether the requested resource has changed |
| 128 /// since the last request. It indicates that the resource has not changed and | 135 /// since the last request. It indicates that the resource has not changed and |
| 129 /// the old value should be used. | 136 /// the old value should be used. |
| 130 Response.notModified({Map<String, String> headers}) | 137 Response.notModified({Map<String, String> headers, |
| 138 Map<String, Object> context}) |
| 131 : this(304, headers: _addHeader( | 139 : this(304, headers: _addHeader( |
| 132 headers, 'date', formatHttpDate(new DateTime.now()))); | 140 headers, 'date', formatHttpDate(new DateTime.now())), |
| 141 context: context); |
| 133 | 142 |
| 134 /// Constructs a 403 Forbidden response. | 143 /// Constructs a 403 Forbidden response. |
| 135 /// | 144 /// |
| 136 /// This indicates that the server is refusing to fulfill the request. | 145 /// This indicates that the server is refusing to fulfill the request. |
| 137 /// | 146 /// |
| 138 /// [body] is the response body. It may be either a [String], a | 147 /// [body] is the response body. It may be either a [String], a |
| 139 /// [Stream<List<int>>], or `null` to indicate no body. If it's a [String], | 148 /// [Stream<List<int>>], or `null` to indicate no body. If it's a [String], |
| 140 /// [encoding] is used to encode it to a [Stream<List<int>>]. It defaults to | 149 /// [encoding] is used to encode it to a [Stream<List<int>>]. It defaults to |
| 141 /// UTF-8. | 150 /// UTF-8. |
| 142 /// | 151 /// |
| 143 /// If [encoding] is passed, the "encoding" field of the Content-Type header | 152 /// If [encoding] is passed, the "encoding" field of the Content-Type header |
| 144 /// in [headers] will be set appropriately. If there is no existing | 153 /// in [headers] will be set appropriately. If there is no existing |
| 145 /// Content-Type header, it will be set to "application/octet-stream". | 154 /// Content-Type header, it will be set to "application/octet-stream". |
| 146 Response.forbidden(body, {Map<String, String> headers, | 155 Response.forbidden(body, {Map<String, String> headers, |
| 147 Encoding encoding}) | 156 Encoding encoding, Map<String, Object> context}) |
| 148 : this(403, body: body, headers: headers); | 157 : this(403, body: body, headers: headers, |
| 158 context: context); |
| 149 | 159 |
| 150 /// Constructs a 404 Not Found response. | 160 /// Constructs a 404 Not Found response. |
| 151 /// | 161 /// |
| 152 /// This indicates that the server didn't find any resource matching the | 162 /// This indicates that the server didn't find any resource matching the |
| 153 /// requested URI. | 163 /// requested URI. |
| 154 /// | 164 /// |
| 155 /// [body] is the response body. It may be either a [String], a | 165 /// [body] is the response body. It may be either a [String], a |
| 156 /// [Stream<List<int>>], or `null` to indicate no body. If it's a [String], | 166 /// [Stream<List<int>>], or `null` to indicate no body. If it's a [String], |
| 157 /// [encoding] is used to encode it to a [Stream<List<int>>]. It defaults to | 167 /// [encoding] is used to encode it to a [Stream<List<int>>]. It defaults to |
| 158 /// UTF-8. | 168 /// UTF-8. |
| 159 /// | 169 /// |
| 160 /// If [encoding] is passed, the "encoding" field of the Content-Type header | 170 /// If [encoding] is passed, the "encoding" field of the Content-Type header |
| 161 /// in [headers] will be set appropriately. If there is no existing | 171 /// in [headers] will be set appropriately. If there is no existing |
| 162 /// Content-Type header, it will be set to "application/octet-stream". | 172 /// Content-Type header, it will be set to "application/octet-stream". |
| 163 Response.notFound(body, {Map<String, String> headers, Encoding encoding}) | 173 Response.notFound(body, {Map<String, String> headers, Encoding encoding, |
| 164 : this(404, body: body, headers: headers); | 174 Map<String, Object> context}) |
| 175 : this(404, body: body, headers: headers, |
| 176 context: context); |
| 165 | 177 |
| 166 /// Constructs a 500 Internal Server Error response. | 178 /// Constructs a 500 Internal Server Error response. |
| 167 /// | 179 /// |
| 168 /// This indicates that the server had an internal error that prevented it | 180 /// This indicates that the server had an internal error that prevented it |
| 169 /// from fulfilling the request. | 181 /// from fulfilling the request. |
| 170 /// | 182 /// |
| 171 /// [body] is the response body. It may be either a [String], a | 183 /// [body] is the response body. It may be either a [String], a |
| 172 /// [Stream<List<int>>], or `null` to indicate no body. If it's `null` or not | 184 /// [Stream<List<int>>], or `null` to indicate no body. If it's `null` or not |
| 173 /// passed, a default error message is used. If it's a [String], [encoding] is | 185 /// passed, a default error message is used. If it's a [String], [encoding] is |
| 174 /// used to encode it to a [Stream<List<int>>]. It defaults to UTF-8. | 186 /// used to encode it to a [Stream<List<int>>]. It defaults to UTF-8. |
| 175 /// | 187 /// |
| 176 /// If [encoding] is passed, the "encoding" field of the Content-Type header | 188 /// If [encoding] is passed, the "encoding" field of the Content-Type header |
| 177 /// in [headers] will be set appropriately. If there is no existing | 189 /// in [headers] will be set appropriately. If there is no existing |
| 178 /// Content-Type header, it will be set to "application/octet-stream". | 190 /// Content-Type header, it will be set to "application/octet-stream". |
| 179 Response.internalServerError({body, Map<String, String> headers, | 191 Response.internalServerError({body, Map<String, String> headers, |
| 180 Encoding encoding}) | 192 Encoding encoding, Map<String, Object> context}) |
| 181 : this(500, | 193 : this(500, |
| 182 headers: body == null ? _adjust500Headers(headers) : headers, | 194 headers: body == null ? _adjust500Headers(headers) : headers, |
| 183 body: body == null ? 'Internal Server Error' : body); | 195 body: body == null ? 'Internal Server Error' : body, |
| 196 context: context); |
| 184 | 197 |
| 185 /// Constructs an HTTP response with the given [statusCode]. | 198 /// Constructs an HTTP response with the given [statusCode]. |
| 186 /// | 199 /// |
| 187 /// [statusCode] must be greater than or equal to 100. | 200 /// [statusCode] must be greater than or equal to 100. |
| 188 /// | 201 /// |
| 189 /// [body] is the response body. It may be either a [String], a | 202 /// [body] is the response body. It may be either a [String], a |
| 190 /// [Stream<List<int>>], or `null` to indicate no body. If it's `null` or not | 203 /// [Stream<List<int>>], or `null` to indicate no body. If it's `null` or not |
| 191 /// passed, a default error message is used. If it's a [String], [encoding] is | 204 /// passed, a default error message is used. If it's a [String], [encoding] is |
| 192 /// used to encode it to a [Stream<List<int>>]. It defaults to UTF-8. | 205 /// used to encode it to a [Stream<List<int>>]. It defaults to UTF-8. |
| 193 /// | 206 /// |
| 194 /// If [encoding] is passed, the "encoding" field of the Content-Type header | 207 /// If [encoding] is passed, the "encoding" field of the Content-Type header |
| 195 /// in [headers] will be set appropriately. If there is no existing | 208 /// in [headers] will be set appropriately. If there is no existing |
| 196 /// Content-Type header, it will be set to "application/octet-stream". | 209 /// Content-Type header, it will be set to "application/octet-stream". |
| 197 Response(this.statusCode, {body, Map<String, String> headers, | 210 Response(this.statusCode, {body, Map<String, String> headers, |
| 198 Encoding encoding}) | 211 Encoding encoding, |
| 212 Map<String, Object> context}) |
| 199 : super(_adjustHeaders(headers, encoding), | 213 : super(_adjustHeaders(headers, encoding), |
| 200 _bodyToStream(body, encoding)) { | 214 _bodyToStream(body, encoding), |
| 215 new UnmodifiableMapView(new Map.from( |
| 216 context != null ? context: {}))) { |
| 201 if (statusCode < 100) { | 217 if (statusCode < 100) { |
| 202 throw new ArgumentError("Invalid status code: $statusCode."); | 218 throw new ArgumentError("Invalid status code: $statusCode."); |
| 203 } | 219 } |
| 204 } | 220 } |
| 205 } | 221 } |
| 206 | 222 |
| 207 /// Converts [body] to a byte stream. | 223 /// Converts [body] to a byte stream. |
| 208 /// | 224 /// |
| 209 /// [body] may be either a [String], a [Stream<List<int>>], or `null`. If it's a | 225 /// [body] may be either a [String], a [Stream<List<int>>], or `null`. If it's a |
| 210 /// [String], [encoding] will be used to convert it to a [Stream<List<int>>]. | 226 /// [String], [encoding] will be used to convert it to a [Stream<List<int>>]. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 /// Converts [location], which may be a [String] or a [Uri], to a [String]. | 279 /// Converts [location], which may be a [String] or a [Uri], to a [String]. |
| 264 /// | 280 /// |
| 265 /// Throws an [ArgumentError] if [location] isn't a [String] or a [Uri]. | 281 /// Throws an [ArgumentError] if [location] isn't a [String] or a [Uri]. |
| 266 String _locationToString(location) { | 282 String _locationToString(location) { |
| 267 if (location is String) return location; | 283 if (location is String) return location; |
| 268 if (location is Uri) return location.toString(); | 284 if (location is Uri) return location.toString(); |
| 269 | 285 |
| 270 throw new ArgumentError('Response location must be a String or Uri, was ' | 286 throw new ArgumentError('Response location must be a String or Uri, was ' |
| 271 '"$location".'); | 287 '"$location".'); |
| 272 } | 288 } |
| OLD | NEW |