Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1099)

Unified Diff: pkg/shelf/lib/src/response.dart

Issue 596573002: pkg/shelf Include default bodies for 'Not Found' and 'Forbidden' Response (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: changelog tweaks and ready for publish Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/shelf/CHANGELOG.md ('k') | pkg/shelf/pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/shelf/lib/src/response.dart
diff --git a/pkg/shelf/lib/src/response.dart b/pkg/shelf/lib/src/response.dart
index c36c551a5a177ddcf6fae74d675e69af640b0e49..f95fdb77ff6839ad43e825d9bb7188dc8c0bdc9a 100644
--- a/pkg/shelf/lib/src/response.dart
+++ b/pkg/shelf/lib/src/response.dart
@@ -144,17 +144,19 @@ class Response extends Message {
///
/// This indicates that the server is refusing to fulfill the request.
///
- /// [body] is the response body. It may be either a [String], a
- /// [Stream<List<int>>], or `null` to indicate no body. If it's a [String],
- /// [encoding] is used to encode it to a [Stream<List<int>>]. It defaults to
- /// UTF-8.
+ /// [body] is the response body. It may be a [String], a [Stream<List<int>>],
+ /// or `null`. If it's a [String], [encoding] is used to encode it to a
+ /// [Stream<List<int>>]. The default encoding is UTF-8. If it's `null` or not
+ /// passed, a default error message is used.
///
/// If [encoding] is passed, the "encoding" field of the Content-Type header
/// in [headers] will be set appropriately. If there is no existing
/// Content-Type header, it will be set to "application/octet-stream".
Response.forbidden(body, {Map<String, String> headers,
Encoding encoding, Map<String, Object> context})
- : this(403, body: body, headers: headers,
+ : this(403,
+ headers: body == null ? _adjustErrorHeaders(headers) : headers,
+ body: body == null ? 'Forbidden' : body,
context: context);
/// Constructs a 404 Not Found response.
@@ -162,17 +164,19 @@ class Response extends Message {
/// This indicates that the server didn't find any resource matching the
/// requested URI.
///
- /// [body] is the response body. It may be either a [String], a
- /// [Stream<List<int>>], or `null` to indicate no body. If it's a [String],
- /// [encoding] is used to encode it to a [Stream<List<int>>]. It defaults to
- /// UTF-8.
+ /// [body] is the response body. It may be a [String], a [Stream<List<int>>],
+ /// or `null`. If it's a [String], [encoding] is used to encode it to a
+ /// [Stream<List<int>>]. The default encoding is UTF-8. If it's `null` or not
+ /// passed, a default error message is used.
///
/// If [encoding] is passed, the "encoding" field of the Content-Type header
/// in [headers] will be set appropriately. If there is no existing
/// Content-Type header, it will be set to "application/octet-stream".
Response.notFound(body, {Map<String, String> headers, Encoding encoding,
Map<String, Object> context})
- : this(404, body: body, headers: headers,
+ : this(404,
+ headers: body == null ? _adjustErrorHeaders(headers) : headers,
+ body: body == null ? 'Not Found' : body,
context: context);
/// Constructs a 500 Internal Server Error response.
@@ -180,10 +184,10 @@ class Response extends Message {
/// This indicates that the server had an internal error that prevented it
/// from fulfilling the request.
///
- /// [body] is the response body. It may be either a [String], a
- /// [Stream<List<int>>], or `null` to indicate no body. If it's `null` or not
- /// passed, a default error message is used. If it's a [String], [encoding] is
- /// used to encode it to a [Stream<List<int>>]. It defaults to UTF-8.
+ /// [body] is the response body. It may be a [String], a [Stream<List<int>>],
+ /// or `null`. If it's a [String], [encoding] is used to encode it to a
+ /// [Stream<List<int>>]. The default encoding is UTF-8. If it's `null` or not
+ /// passed, a default error message is used.
///
/// If [encoding] is passed, the "encoding" field of the Content-Type header
/// in [headers] will be set appropriately. If there is no existing
@@ -191,7 +195,7 @@ class Response extends Message {
Response.internalServerError({body, Map<String, String> headers,
Encoding encoding, Map<String, Object> context})
: this(500,
- headers: body == null ? _adjust500Headers(headers) : headers,
+ headers: body == null ? _adjustErrorHeaders(headers) : headers,
body: body == null ? 'Internal Server Error' : body,
context: context);
@@ -200,9 +204,9 @@ class Response extends Message {
/// [statusCode] must be greater than or equal to 100.
///
/// [body] is the response body. It may be either a [String], a
- /// [Stream<List<int>>], or `null` to indicate no body. If it's `null` or not
- /// passed, a default error message is used. If it's a [String], [encoding] is
- /// used to encode it to a [Stream<List<int>>]. It defaults to UTF-8.
+ /// [Stream<List<int>>], or `null` to indicate no body.
+ /// If it's a [String], [encoding] is used to encode it to a
+ /// [Stream<List<int>>]. The default encoding is UTF-8.
///
/// If [encoding] is passed, the "encoding" field of the Content-Type header
/// in [headers] will be set appropriately. If there is no existing
@@ -283,7 +287,7 @@ Map<String, String> _addHeader(Map<String, String> headers, String name,
///
/// Returns a new map without modifying [headers]. This is used to add
/// content-type information when creating a 500 response with a default body.
-Map<String, String> _adjust500Headers(Map<String, String> headers) {
+Map<String, String> _adjustErrorHeaders(Map<String, String> headers) {
if (headers == null || headers['content-type'] == null) {
return _addHeader(headers, 'content-type', 'text/plain');
}
« no previous file with comments | « pkg/shelf/CHANGELOG.md ('k') | pkg/shelf/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698