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

Unified Diff: pkg/http_server/lib/src/virtual_directory.dart

Issue 424063002: Add 'pathPrefix' optional argument to VirtualDirectory constructor. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Push version. Created 6 years, 5 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 | « no previous file | pkg/http_server/pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/http_server/lib/src/virtual_directory.dart
diff --git a/pkg/http_server/lib/src/virtual_directory.dart b/pkg/http_server/lib/src/virtual_directory.dart
index bf4e1a5a1010b099a52b843d0858e017cbcf1f02..6dfec5d9124d7644a9e4479edcf331e0833a7754 100644
--- a/pkg/http_server/lib/src/virtual_directory.dart
+++ b/pkg/http_server/lib/src/virtual_directory.dart
@@ -41,19 +41,36 @@ class VirtualDirectory {
*/
bool jailRoot = true;
+ final List<String> _pathPrefixSegments;
+
+
final RegExp _invalidPathRegExp = new RegExp("[\\\/\x00]");
_ErrorCallback _errorCallback;
_DirCallback _dirCallback;
+ static List<String> _parsePathPrefix(String pathPrefix) {
+ if (pathPrefix == null) return <String>[];
+ return new Uri(path: pathPrefix).pathSegments
+ .where((segment) => segment.isNotEmpty)
+ .toList();
+ }
+
/*
* Create a new [VirtualDirectory] for serving static file content of
* the path [root].
*
* The [root] is not required to exist. If the [root] doesn't exist at time of
- * a request, a 404 is generated.
+ * a request, a 404 response is generated.
+ *
+ * If [pathPrefix] is set, [pathPrefix] will indicate the expected path prefix
+ * of incoming requests. When locating the resource on disk, the prefix will
+ * be trimmed from the requests uri, before locating the actual resource.
+ * If the requests uri doesn't start with [pathPrefix], a 404 response is
+ * generated.
*/
- VirtualDirectory(this.root);
+ VirtualDirectory(this.root, {String pathPrefix})
+ : _pathPrefixSegments = _parsePathPrefix(pathPrefix);
/**
* Serve a [Stream] of [HttpRequest]s, in this [VirtualDirectory].
@@ -65,7 +82,14 @@ class VirtualDirectory {
* Serve a single [HttpRequest], in this [VirtualDirectory].
*/
Future serveRequest(HttpRequest request) {
- return _locateResource('.', request.uri.pathSegments.iterator..moveNext())
+ var iterator = request.uri.pathSegments.iterator;
+ for (var segment in _pathPrefixSegments) {
+ if (!iterator.moveNext() || iterator.current != segment) {
+ _serveErrorPage(HttpStatus.NOT_FOUND, request);
+ return request.response.done;
+ }
+ }
+ return _locateResource('.', iterator..moveNext())
.then((entity) {
if (entity is File) {
serveFile(entity, request);
« no previous file with comments | « no previous file | pkg/http_server/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698