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

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

Issue 294123011: pkg/shelf: added `url` and `scriptName` named params to Request.change (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: doc comment tweak 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/shelf/CHANGELOG.md ('k') | pkg/shelf/lib/src/shelf_unmodifiable_map.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/shelf/lib/src/request.dart
diff --git a/pkg/shelf/lib/src/request.dart b/pkg/shelf/lib/src/request.dart
index 4a08468750a1d6a37f8a01fe2fdaa44984fefcbe..26fe011ff8d6b96135599d11574577556d4bdf79 100644
--- a/pkg/shelf/lib/src/request.dart
+++ b/pkg/shelf/lib/src/request.dart
@@ -134,6 +134,8 @@ class Request extends Message {
throw new ArgumentError('requstedUri must be an absolute URI.');
}
+ // TODO(kevmoo) if defined, check that scriptName is a fully-encoded, valid
+ // path component
if (this.scriptName.isNotEmpty && !this.scriptName.startsWith('/')) {
throw new ArgumentError('scriptName must be empty or start with "/".');
}
@@ -168,13 +170,33 @@ class Request extends Message {
///
/// All other context and header values from the [Request] will be included
/// in the copied [Request] unchanged.
- Request change({Map<String, String> headers, Map<String, Object> context}) {
+ ///
+ /// If [scriptName] is provided and [url] is not, [scriptName] must be a
+ /// prefix of [this.url]. [url] will default to [this.url] with this prefix
+ /// removed. Useful for routing middleware that sends requests to an inner
+ /// [Handler].
+ Request change({Map<String, String> headers, Map<String, Object> context,
+ String scriptName, Uri url}) {
headers = updateMap(this.headers, headers);
context = updateMap(this.context, context);
+ if (scriptName != null && url == null) {
+ var path = this.url.path;
+ if (path.startsWith(scriptName)) {
+ path = path.substring(scriptName.length);
+ url = new Uri(path: path, query: this.url.query);
+ } else {
+ throw new ArgumentError('If scriptName is provided without url, it must'
+ ' be a prefix of the existing url path.');
+ }
+ }
+
+ if (url == null) url = this.url;
+ if (scriptName == null) scriptName = this.scriptName;
+
return new Request(this.method, this.requestedUri,
- protocolVersion: this.protocolVersion, headers: headers, url: this.url,
- scriptName: this.scriptName, body: this.read(), context: context);
+ protocolVersion: this.protocolVersion, headers: headers, url: url,
+ scriptName: scriptName, body: this.read(), context: context);
}
/// Takes control of the underlying request socket.
@@ -233,8 +255,7 @@ class _OnHijack {
/// [ArgumentError].
Uri _computeUrl(Uri requestedUri, Uri url, String scriptName) {
if (url == null && scriptName == null) {
- return new Uri(path: requestedUri.path, query: requestedUri.query,
- fragment: requestedUri.fragment);
+ return new Uri(path: requestedUri.path, query: requestedUri.query);
}
if (url != null && scriptName != null) {
« no previous file with comments | « pkg/shelf/CHANGELOG.md ('k') | pkg/shelf/lib/src/shelf_unmodifiable_map.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698