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.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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 /// which will be `false` for a hijacked request. The adapter may throw an | 113 /// which will be `false` for a hijacked request. The adapter may throw an |
114 /// error if a [HijackException] is received for a non-hijacked request, or if | 114 /// error if a [HijackException] is received for a non-hijacked request, or if |
115 /// no [HijackException] is received for a hijacked request. | 115 /// no [HijackException] is received for a hijacked request. |
116 /// | 116 /// |
117 /// See also [hijack]. | 117 /// See also [hijack]. |
118 // TODO(kevmoo) finish documenting the rest of the arguments. | 118 // TODO(kevmoo) finish documenting the rest of the arguments. |
119 Request(String method, Uri requestedUri, {String protocolVersion, | 119 Request(String method, Uri requestedUri, {String protocolVersion, |
120 Map<String, String> headers, Uri url, String scriptName, | 120 Map<String, String> headers, Uri url, String scriptName, |
121 Stream<List<int>> body, Map<String, Object> context, | 121 Stream<List<int>> body, Map<String, Object> context, |
122 OnHijackCallback onHijack}) | 122 OnHijackCallback onHijack}) |
123 : this._(method, requestedUri, protocolVersion: protocolVersion, | 123 : this._(method, requestedUri, |
124 headers: headers, url: url, scriptName: scriptName, | 124 protocolVersion: protocolVersion, |
125 body: body, context: context, | 125 headers: headers, |
126 onHijack: onHijack == null ? null : new _OnHijack(onHijack)); | 126 url: url, |
| 127 scriptName: scriptName, |
| 128 body: body, |
| 129 context: context, |
| 130 onHijack: onHijack == null ? null : new _OnHijack(onHijack)); |
127 | 131 |
128 /// This constructor has the same signature as [new Request] except that | 132 /// This constructor has the same signature as [new Request] except that |
129 /// accepts [onHijack] as [_OnHijack]. | 133 /// accepts [onHijack] as [_OnHijack]. |
130 /// | 134 /// |
131 /// Any [Request] created by calling [change] will pass [_onHijack] from the | 135 /// Any [Request] created by calling [change] will pass [_onHijack] from the |
132 /// source [Request] to ensure that [hijack] can only be called once, even | 136 /// source [Request] to ensure that [hijack] can only be called once, even |
133 /// from a changed [Request]. | 137 /// from a changed [Request]. |
134 Request._(this.method, Uri requestedUri, {String protocolVersion, | 138 Request._(this.method, Uri requestedUri, {String protocolVersion, |
135 Map<String, String> headers, Uri url, String scriptName, | 139 Map<String, String> headers, Uri url, String scriptName, |
136 Stream<List<int>> body, Map<String, Object> context, | 140 Stream<List<int>> body, Map<String, Object> context, _OnHijack onHijack}) |
137 _OnHijack onHijack}) | |
138 : this.requestedUri = requestedUri, | 141 : this.requestedUri = requestedUri, |
139 this.protocolVersion = protocolVersion == null ? | 142 this.protocolVersion = protocolVersion == null |
140 '1.1' : protocolVersion, | 143 ? '1.1' |
| 144 : protocolVersion, |
141 this.url = _computeUrl(requestedUri, url, scriptName), | 145 this.url = _computeUrl(requestedUri, url, scriptName), |
142 this.scriptName = _computeScriptName(requestedUri, url, scriptName), | 146 this.scriptName = _computeScriptName(requestedUri, url, scriptName), |
143 this._onHijack = onHijack, | 147 this._onHijack = onHijack, |
144 super(body == null ? new Stream.fromIterable([]) : body, | 148 super(body == null ? new Stream.fromIterable([]) : body, |
145 headers: headers, context: context) { | 149 headers: headers, context: context) { |
146 if (method.isEmpty) throw new ArgumentError('method cannot be empty.'); | 150 if (method.isEmpty) throw new ArgumentError('method cannot be empty.'); |
147 | 151 |
148 if (!requestedUri.isAbsolute) { | 152 if (!requestedUri.isAbsolute) { |
149 throw new ArgumentError('requstedUri must be an absolute URI.'); | 153 throw new ArgumentError('requstedUri must be an absolute URI.'); |
150 } | 154 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 /// [Request]. | 188 /// [Request]. |
185 /// | 189 /// |
186 /// All other context and header values from the [Request] will be included | 190 /// All other context and header values from the [Request] will be included |
187 /// in the copied [Request] unchanged. | 191 /// in the copied [Request] unchanged. |
188 /// | 192 /// |
189 /// If [scriptName] is provided and [url] is not, [scriptName] must be a | 193 /// If [scriptName] is provided and [url] is not, [scriptName] must be a |
190 /// prefix of [this.url]. [url] will default to [this.url] with this prefix | 194 /// prefix of [this.url]. [url] will default to [this.url] with this prefix |
191 /// removed. Useful for routing middleware that sends requests to an inner | 195 /// removed. Useful for routing middleware that sends requests to an inner |
192 /// [Handler]. | 196 /// [Handler]. |
193 Request change({Map<String, String> headers, Map<String, Object> context, | 197 Request change({Map<String, String> headers, Map<String, Object> context, |
194 String scriptName, Uri url}) { | 198 String scriptName, Uri url}) { |
195 headers = updateMap(this.headers, headers); | 199 headers = updateMap(this.headers, headers); |
196 context = updateMap(this.context, context); | 200 context = updateMap(this.context, context); |
197 | 201 |
198 if (scriptName != null && url == null) { | 202 if (scriptName != null && url == null) { |
199 var path = this.url.path; | 203 var path = this.url.path; |
200 if (path.startsWith(scriptName)) { | 204 if (path.startsWith(scriptName)) { |
201 path = path.substring(scriptName.length); | 205 path = path.substring(scriptName.length); |
202 url = new Uri(path: path, query: this.url.query); | 206 url = new Uri(path: path, query: this.url.query); |
203 } else { | 207 } else { |
204 throw new ArgumentError('If scriptName is provided without url, it must' | 208 throw new ArgumentError('If scriptName is provided without url, it must' |
205 ' be a prefix of the existing url path.'); | 209 ' be a prefix of the existing url path.'); |
206 } | 210 } |
207 } | 211 } |
208 | 212 |
209 if (url == null) url = this.url; | 213 if (url == null) url = this.url; |
210 if (scriptName == null) scriptName = this.scriptName; | 214 if (scriptName == null) scriptName = this.scriptName; |
211 | 215 |
212 return new Request._(this.method, this.requestedUri, | 216 return new Request._(this.method, this.requestedUri, |
213 protocolVersion: this.protocolVersion, headers: headers, url: url, | 217 protocolVersion: this.protocolVersion, |
214 scriptName: scriptName, body: this.read(), context: context, | 218 headers: headers, |
| 219 url: url, |
| 220 scriptName: scriptName, |
| 221 body: this.read(), |
| 222 context: context, |
215 onHijack: _onHijack); | 223 onHijack: _onHijack); |
216 } | 224 } |
217 | 225 |
218 /// Takes control of the underlying request socket. | 226 /// Takes control of the underlying request socket. |
219 /// | 227 /// |
220 /// Synchronously, this throws a [HijackException] that indicates to the | 228 /// Synchronously, this throws a [HijackException] that indicates to the |
221 /// adapter that it shouldn't emit a response itself. Asynchronously, | 229 /// adapter that it shouldn't emit a response itself. Asynchronously, |
222 /// [callback] is called with a [Stream<List<int>>] and | 230 /// [callback] is called with a [Stream<List<int>>] and |
223 /// [StreamSink<List<int>>], respectively, that provide access to the | 231 /// [StreamSink<List<int>>], respectively, that provide access to the |
224 /// underlying request socket. | 232 /// underlying request socket. |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 return ''; | 303 return ''; |
296 } | 304 } |
297 | 305 |
298 if (url != null && scriptName != null) { | 306 if (url != null && scriptName != null) { |
299 return scriptName; | 307 return scriptName; |
300 } | 308 } |
301 | 309 |
302 throw new ArgumentError( | 310 throw new ArgumentError( |
303 'url and scriptName must both be null or both be set.'); | 311 'url and scriptName must both be null or both be set.'); |
304 } | 312 } |
OLD | NEW |