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

Side by Side Diff: pkg/shelf/lib/src/request.dart

Issue 256753004: pkg/shelf: change helper method on Request and Response (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: user error Created 6 years, 7 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.request; 5 library shelf.request;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:http_parser/http_parser.dart'; 9 import 'package:http_parser/http_parser.dart';
10 10
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 Stream<List<int>> body, Map<String, Object> context}) 74 Stream<List<int>> body, Map<String, Object> context})
75 : this.requestedUri = requestedUri, 75 : this.requestedUri = requestedUri,
76 this.protocolVersion = protocolVersion == null ? 76 this.protocolVersion = protocolVersion == null ?
77 '1.1' : protocolVersion, 77 '1.1' : protocolVersion,
78 this.url = _computeUrl(requestedUri, url, scriptName), 78 this.url = _computeUrl(requestedUri, url, scriptName),
79 this.scriptName = _computeScriptName(requestedUri, url, scriptName), 79 this.scriptName = _computeScriptName(requestedUri, url, scriptName),
80 super(body == null ? new Stream.fromIterable([]) : body, 80 super(body == null ? new Stream.fromIterable([]) : body,
81 headers: headers, context: context) { 81 headers: headers, context: context) {
82 if (method.isEmpty) throw new ArgumentError('method cannot be empty.'); 82 if (method.isEmpty) throw new ArgumentError('method cannot be empty.');
83 83
84 // TODO(kevmoo) use isAbsolute property on Uri once Issue 18053 is fixed 84 if (!requestedUri.isAbsolute) {
85 if (requestedUri.scheme.isEmpty) {
86 throw new ArgumentError('requstedUri must be an absolute URI.'); 85 throw new ArgumentError('requstedUri must be an absolute URI.');
87 } 86 }
88 87
89 if (this.scriptName.isNotEmpty && !this.scriptName.startsWith('/')) { 88 if (this.scriptName.isNotEmpty && !this.scriptName.startsWith('/')) {
90 throw new ArgumentError('scriptName must be empty or start with "/".'); 89 throw new ArgumentError('scriptName must be empty or start with "/".');
91 } 90 }
92 91
93 if (this.scriptName == '/') { 92 if (this.scriptName == '/') {
94 throw new ArgumentError( 93 throw new ArgumentError(
95 'scriptName can never be "/". It should be empty instead.'); 94 'scriptName can never be "/". It should be empty instead.');
96 } 95 }
97 96
98 if (this.scriptName.endsWith('/')) { 97 if (this.scriptName.endsWith('/')) {
99 throw new ArgumentError('scriptName must not end with "/".'); 98 throw new ArgumentError('scriptName must not end with "/".');
100 } 99 }
101 100
102 if (this.url.path.isNotEmpty && !this.url.path.startsWith('/')) { 101 if (this.url.path.isNotEmpty && !this.url.path.startsWith('/')) {
103 throw new ArgumentError('url must be empty or start with "/".'); 102 throw new ArgumentError('url must be empty or start with "/".');
104 } 103 }
105 104
106 if (this.scriptName.isEmpty && this.url.path.isEmpty) { 105 if (this.scriptName.isEmpty && this.url.path.isEmpty) {
107 throw new ArgumentError('scriptName and url cannot both be empty.'); 106 throw new ArgumentError('scriptName and url cannot both be empty.');
108 } 107 }
109 } 108 }
109
110 /// Creates a new [Request] by copying existing values and applying specified
111 /// changes.
112 ///
113 /// New key-value pairs in [context] and [headers] will be added to the copied
114 /// [Request].
115 ///
116 /// If [context] or [headers] includes a key that already exists, the
117 /// key-value pair will replace the corresponding entry in the copied
118 /// [Request].
119 ///
120 /// All other context and header values from the [Request] will be included
121 /// in the copied [Request] unchanged.
122 Request change({Map<String, String> headers, Map<String, Object> context}) {
123 headers = updateMap(this.headers, headers);
124 context = updateMap(this.context, context);
125
126 return new Request(this.method, this.requestedUri,
127 protocolVersion: this.protocolVersion, headers: headers, url: this.url,
128 scriptName: this.scriptName, body: this.read(), context: context);
129 }
110 } 130 }
111 131
112 /// Computes `url` from the provided [Request] constructor arguments. 132 /// Computes `url` from the provided [Request] constructor arguments.
113 /// 133 ///
114 /// If [url] and [scriptName] are `null`, infer value from [requestedUrl], 134 /// If [url] and [scriptName] are `null`, infer value from [requestedUrl],
115 /// otherwise return [url]. 135 /// otherwise return [url].
116 /// 136 ///
117 /// If [url] is provided, but [scriptName] is omitted, throws an 137 /// If [url] is provided, but [scriptName] is omitted, throws an
118 /// [ArgumentError]. 138 /// [ArgumentError].
119 Uri _computeUrl(Uri requestedUri, Uri url, String scriptName) { 139 Uri _computeUrl(Uri requestedUri, Uri url, String scriptName) {
120 if (url == null && scriptName == null) { 140 if (url == null && scriptName == null) {
121 return new Uri(path: requestedUri.path, query: requestedUri.query, 141 return new Uri(path: requestedUri.path, query: requestedUri.query,
122 fragment: requestedUri.fragment); 142 fragment: requestedUri.fragment);
123 } 143 }
124 144
125 if (url != null && scriptName != null) { 145 if (url != null && scriptName != null) {
126 // TODO(kevmoo) use isAbsolute property on Uri once Issue 18053 is fixed
127 if (url.scheme.isNotEmpty) throw new ArgumentError('url must be relative.'); 146 if (url.scheme.isNotEmpty) throw new ArgumentError('url must be relative.');
128 return url; 147 return url;
129 } 148 }
130 149
131 throw new ArgumentError( 150 throw new ArgumentError(
132 'url and scriptName must both be null or both be set.'); 151 'url and scriptName must both be null or both be set.');
133 } 152 }
134 153
135 /// Computes `scriptName` from the provided [Request] constructor arguments. 154 /// Computes `scriptName` from the provided [Request] constructor arguments.
136 /// 155 ///
137 /// If [url] and [scriptName] are `null` it returns an empty string, otherwise 156 /// If [url] and [scriptName] are `null` it returns an empty string, otherwise
138 /// [scriptName] is returned. 157 /// [scriptName] is returned.
139 /// 158 ///
140 /// If [script] is provided, but [url] is omitted, throws an 159 /// If [script] is provided, but [url] is omitted, throws an
141 /// [ArgumentError]. 160 /// [ArgumentError].
142 String _computeScriptName(Uri requstedUri, Uri url, String scriptName) { 161 String _computeScriptName(Uri requstedUri, Uri url, String scriptName) {
143 if (url == null && scriptName == null) { 162 if (url == null && scriptName == null) {
144 return ''; 163 return '';
145 } 164 }
146 165
147 if (url != null && scriptName != null) { 166 if (url != null && scriptName != null) {
148 return scriptName; 167 return scriptName;
149 } 168 }
150 169
151 throw new ArgumentError( 170 throw new ArgumentError(
152 'url and scriptName must both be null or both be set.'); 171 'url and scriptName must both be null or both be set.');
153 } 172 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698