| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of http_server; | 5 part of http_server; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * A [VirtualDirectory] can serve files and directory-listing from a root path, | 8 * A [VirtualDirectory] can serve files and directory-listing from a root path, |
| 9 * to [HttpRequest]s. | 9 * to [HttpRequest]s. |
| 10 * | 10 * |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 _errorCallback = callback; | 90 _errorCallback = callback; |
| 91 } | 91 } |
| 92 | 92 |
| 93 Future<FileSystemEntity> _locateResource(String path, | 93 Future<FileSystemEntity> _locateResource(String path, |
| 94 Iterator<String> segments) { | 94 Iterator<String> segments) { |
| 95 // Don't allow navigating up paths. | 95 // Don't allow navigating up paths. |
| 96 if (segments.current == "..") return new Future.value(null); | 96 if (segments.current == "..") return new Future.value(null); |
| 97 path = normalize(path); | 97 path = normalize(path); |
| 98 // If we jail to root, the relative path can never go up. | 98 // If we jail to root, the relative path can never go up. |
| 99 if (jailRoot && split(path).first == "..") return new Future.value(null); | 99 if (jailRoot && split(path).first == "..") return new Future.value(null); |
| 100 String fullPath() => join(root, path); | 100 String fullPath({bool endSlash: false}) { |
| 101 var p = join(root, path); |
| 102 if (endSlash && path != ".") p = "$p$separator"; |
| 103 return p; |
| 104 } |
| 101 return FileSystemEntity.type(fullPath(), followLinks: false) | 105 return FileSystemEntity.type(fullPath(), followLinks: false) |
| 102 .then((type) { | 106 .then((type) { |
| 103 switch (type) { | 107 switch (type) { |
| 104 case FileSystemEntityType.FILE: | 108 case FileSystemEntityType.FILE: |
| 105 if (segments.current == null) { | 109 if (segments.current == null) { |
| 106 return new File(fullPath()); | 110 return new File(fullPath()); |
| 107 } | 111 } |
| 108 break; | 112 break; |
| 109 | 113 |
| 110 case FileSystemEntityType.DIRECTORY: | 114 case FileSystemEntityType.DIRECTORY: |
| 111 if (segments.current == null) { | 115 if (segments.current == null) { |
| 112 if (allowDirectoryListing) { | 116 if (allowDirectoryListing) { |
| 113 return new Directory(fullPath()); | 117 return new Directory(fullPath(endSlash: true)); |
| 114 } | 118 } |
| 115 } else { | 119 } else { |
| 116 if (_invalidPathRegExp.hasMatch(segments.current)) break; | 120 if (_invalidPathRegExp.hasMatch(segments.current)) break; |
| 117 return _locateResource(join(path, segments.current), | 121 return _locateResource(join(path, segments.current), |
| 118 segments..moveNext()); | 122 segments..moveNext()); |
| 119 } | 123 } |
| 120 break; | 124 break; |
| 121 | 125 |
| 122 case FileSystemEntityType.LINK: | 126 case FileSystemEntityType.LINK: |
| 123 if (followLinks) { | 127 if (followLinks) { |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 | 381 |
| 378 Future close() => new Future.value(); | 382 Future close() => new Future.value(); |
| 379 | 383 |
| 380 void setMimeType(var bytes) { | 384 void setMimeType(var bytes) { |
| 381 var mimeType = lookupMimeType(path, headerBytes: bytes); | 385 var mimeType = lookupMimeType(path, headerBytes: bytes); |
| 382 if (mimeType != null) { | 386 if (mimeType != null) { |
| 383 response.headers.contentType = ContentType.parse(mimeType); | 387 response.headers.contentType = ContentType.parse(mimeType); |
| 384 } | 388 } |
| 385 } | 389 } |
| 386 } | 390 } |
| OLD | NEW |