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

Side by Side Diff: pkg/http_server/lib/src/virtual_directory.dart

Issue 17544005: First push of a new 'http_server' dart package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 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
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of http_server;
6
7 /**
8 * A [VirtualDirectory] can serve files and directory-listing from a root path,
9 * to [HttpRequest]s.
10 *
11 * The [VirtualDirectory] providing secure handling of request uris and
12 * file-system links, correct mime-types and custom error pages.
13 */
14 abstract class VirtualDirectory {
15 final String root;
16
17 /**
18 * Set or get if the [VirtualDirectory] should list the content of
19 * directories.
20 */
21 bool allowDirectoryListing = false;
22
23 /**
24 * Set or get if the [VirtualDirectory] should follow links, that point
25 * to other resources within the [root] directory.
26 */
27 bool followLinks = true;
28
29 /*
30 * Create a new [VirtualDirectory] for serving static file content of
31 * the path [root].
32 *
33 * The [root] is not required to exist. If the [root] doesn't exist at time of
34 * a request, a 404 is generated.
35 */
36 factory VirtualDirectory(String root) => new _VirtualDirectory(root);
37
Søren Gjesse 2013/06/21 11:17:23 Dartdoc for serve and serveRequest
Anders Johnsen 2013/06/21 11:22:28 Done.
38 void serve(Stream<HttpRequest> requests);
39
40 void serveRequest(HttpRequest request);
41
42 /**
43 * Set the [callback] to override the error page handler. When [callback] is
44 * invoked, the [response] have the [HttpResponse.statusCode] set accordingly.
Søren Gjesse 2013/06/21 11:17:23 "the [response] have the [HttpResponse.statusCode]
Anders Johnsen 2013/06/21 11:22:28 Done.
45 */
46 void setErrorPageHandler(void callback(HttpResponse response));
47 }
48
49 class _VirtualDirectory implements VirtualDirectory {
50 final String root;
51
52 bool _allowDirectoryListing = false;
53 bool _followLinks = true;
54
55 _VirtualDirectory(this.root);
56
57 void serve(Stream<HttpRequest> requests) {
58 requests.listen(serveRequest);
59 }
60
61 void serveRequest(HttpRequest request) {
62 var path = new Path(request.uri.path).canonicalize();
63
64 if (!path.isAbsolute) {
Søren Gjesse 2013/06/21 11:17:23 Use HttpStatus constants
Anders Johnsen 2013/06/21 11:22:28 Done.
65 return _serveErrorPage(404, request);
66 }
67
68 _locateResource(new Path(root), path.segments())
69 .then((entity) {
70 if (entity == null) return _serveErrorPage(404, request);
71 if (entity is File) {
72 entity.openRead().pipe(request.response).catchError((_) {});
73 } else {
74 return _serveErrorPage(404, request);
75 }
76 print(entity);
77 });
78 }
79
80 Future<FileSystemEntity> _locateResource(Path path,
81 Iterable<String> segments) {
82 return FileSystemEntity.type(path.toString(), followLinks: false)
83 .then((type) {
84 switch (type) {
85 case FileSystemEntityType.FILE:
86 if (segments.isEmpty) return new File.fromPath(path);
87 break;
88
89 case FileSystemEntityType.DIRECTORY:
90 if (segments.isEmpty) {
91 if (_allowDirectoryListing) return new Directory.fromPath(path);
92 } else {
93 return _locateResource(path.append(segments.first),
94 segments.skip(1));
95 }
96 break;
97
98 case FileSystemEntityType.LINK:
99 if (followLinks) {
100 // TODO
101 }
102 break;
103
104 }
Søren Gjesse 2013/06/21 11:17:23 Comment on fall-through.
Anders Johnsen 2013/06/21 11:22:28 Done.
105 return null;
106 });
107 }
108
109 void _serveErrorPage(int error, HttpRequest request) {
110 request.response.statusCode = 404;
111 request.response.close();
112 }
113 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698