OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 http_server; | 5 library http_server; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 import 'dart:isolate'; | 8 import 'dart:isolate'; |
9 import 'test_suite.dart'; // For TestUtils. | 9 import 'test_suite.dart'; // For TestUtils. |
10 // TODO(efortuna): Rewrite to not use the args library and simply take an | 10 // TODO(efortuna): Rewrite to not use the args library and simply take an |
(...skipping 10 matching lines...) Expand all Loading... | |
21 parser.addOption('crossOriginPort', abbr: 'c', | 21 parser.addOption('crossOriginPort', abbr: 'c', |
22 help: 'A different port that accepts request from the main server port.', | 22 help: 'A different port that accepts request from the main server port.', |
23 defaultsTo: '0'); | 23 defaultsTo: '0'); |
24 parser.addOption('mode', abbr: 'm', help: 'Testing mode.', | 24 parser.addOption('mode', abbr: 'm', help: 'Testing mode.', |
25 defaultsTo: 'release'); | 25 defaultsTo: 'release'); |
26 parser.addOption('arch', abbr: 'a', help: 'Testing architecture.', | 26 parser.addOption('arch', abbr: 'a', help: 'Testing architecture.', |
27 defaultsTo: 'ia32'); | 27 defaultsTo: 'ia32'); |
28 parser.addFlag('help', abbr: 'h', negatable: false, | 28 parser.addFlag('help', abbr: 'h', negatable: false, |
29 help: 'Print this usage information.'); | 29 help: 'Print this usage information.'); |
30 parser.addOption('package-root', help: 'The package root to use.'); | 30 parser.addOption('package-root', help: 'The package root to use.'); |
31 parser.addOption('network', help: 'The network interface to use.', | |
32 defaultsTo: '127.0.0.1'); | |
31 var args = parser.parse(new Options().arguments); | 33 var args = parser.parse(new Options().arguments); |
32 if (args['help']) { | 34 if (args['help']) { |
33 print(parser.getUsage()); | 35 print(parser.getUsage()); |
34 } else { | 36 } else { |
35 // Pretend we're running test.dart so that TestUtils doesn't get confused | 37 // Pretend we're running test.dart so that TestUtils doesn't get confused |
36 // about the "current directory." This is only used if we're trying to run | 38 // about the "current directory." This is only used if we're trying to run |
37 // this file independently for local testing. | 39 // this file independently for local testing. |
38 TestUtils.testScriptPath = new Path(new Options().script) | 40 TestUtils.testScriptPath = new Path(new Options().script) |
39 .directoryPath | 41 .directoryPath |
40 .join(new Path('../../test.dart')) | 42 .join(new Path('../../test.dart')) |
41 .canonicalize() | 43 .canonicalize() |
42 .toNativePath(); | 44 .toNativePath(); |
43 // Note: args['package-root'] is always the build directory. We have the | 45 // Note: args['package-root'] is always the build directory. We have the |
44 // implicit assumption that it contains the 'packages' subdirectory. | 46 // implicit assumption that it contains the 'packages' subdirectory. |
45 // TODO: We should probably rename 'package-root' to 'build-directory'. | 47 // TODO: We should probably rename 'package-root' to 'build-directory'. |
46 TestingServerRunner._packageRootDir = new Path(args['package-root']); | 48 TestingServerRunner._packageRootDir = new Path(args['package-root']); |
47 TestingServerRunner._buildDirectory = new Path(args['package-root']); | 49 TestingServerRunner._buildDirectory = new Path(args['package-root']); |
48 TestingServerRunner.startHttpServer('127.0.0.1', | 50 var network = args['network']; |
51 TestingServerRunner.startHttpServer(network, | |
49 port: int.parse(args['port'])); | 52 port: int.parse(args['port'])); |
50 print('Server listening on port ' | 53 print('Server listening on port ' |
51 '${TestingServerRunner.serverList[0].port}.'); | 54 '${TestingServerRunner.serverList[0].port}.'); |
52 TestingServerRunner.startHttpServer('127.0.0.1', | 55 TestingServerRunner.startHttpServer(network, |
53 allowedPort: TestingServerRunner.serverList[0].port, port: | 56 allowedPort: TestingServerRunner.serverList[0].port, port: |
54 int.parse(args['crossOriginPort'])); | 57 int.parse(args['crossOriginPort'])); |
55 print( | 58 print( |
56 'Server listening on port ${TestingServerRunner.serverList[1].port}.'); | 59 'Server listening on port ${TestingServerRunner.serverList[1].port}.'); |
57 } | 60 } |
58 } | 61 } |
59 /** | 62 /** |
60 * Runs a set of servers that are initialized specifically for the needs of our | 63 * Runs a set of servers that are initialized specifically for the needs of our |
61 * test framework, such as dealing with package-root. | 64 * test framework, such as dealing with package-root. |
62 */ | 65 */ |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 var requestPathStr = requestPath.toNativePath().substring( | 114 var requestPathStr = requestPath.toNativePath().substring( |
112 requestPath.toNativePath().indexOf(packagesDirName)); | 115 requestPath.toNativePath().indexOf(packagesDirName)); |
113 path = packageRootDir.append(requestPathStr); | 116 path = packageRootDir.append(requestPathStr); |
114 file = new File(path.toNativePath()); | 117 file = new File(path.toNativePath()); |
115 } | 118 } |
116 file.exists().then((exists) { | 119 file.exists().then((exists) { |
117 if (exists) { | 120 if (exists) { |
118 if (allowedPort != -1) { | 121 if (allowedPort != -1) { |
119 // Allow loading from localhost:$allowedPort in browsers. | 122 // Allow loading from localhost:$allowedPort in browsers. |
120 resp.headers.set("Access-Control-Allow-Origin", | 123 resp.headers.set("Access-Control-Allow-Origin", |
121 "http://127.0.0.1:$allowedPort"); | 124 request.headers['Origin']); |
kustermann
2013/02/12 19:00:48
I'm not so familiar with cross-domain requests, bu
blois
2013/02/12 20:31:12
Updated to use the hostname from the origin but to
| |
122 resp.headers.set('Access-Control-Allow-Credentials', 'true'); | 125 resp.headers.set('Access-Control-Allow-Credentials', 'true'); |
123 } else { | 126 } else { |
124 // No allowedPort specified. Allow from anywhere (but cross-origin | 127 // No allowedPort specified. Allow from anywhere (but cross-origin |
125 // requests *with credentials* will fail because you can't use "*"). | 128 // requests *with credentials* will fail because you can't use "*"). |
126 resp.headers.set("Access-Control-Allow-Origin", "*"); | 129 resp.headers.set("Access-Control-Allow-Origin", "*"); |
127 } | 130 } |
128 if (path.toNativePath().endsWith('.html')) { | 131 if (path.toNativePath().endsWith('.html')) { |
129 resp.headers.set('Content-Type', 'text/html'); | 132 resp.headers.set('Content-Type', 'text/html'); |
130 } else if (path.toNativePath().endsWith('.js')) { | 133 } else if (path.toNativePath().endsWith('.js')) { |
131 resp.headers.set('Content-Type', 'application/javascript'); | 134 resp.headers.set('Content-Type', 'application/javascript'); |
132 } else if (path.toNativePath().endsWith('.dart')) { | 135 } else if (path.toNativePath().endsWith('.dart')) { |
133 resp.headers.set('Content-Type', 'application/dart'); | 136 resp.headers.set('Content-Type', 'application/dart'); |
134 } | 137 } |
135 file.openInputStream().pipe(resp.outputStream); | 138 file.openInputStream().pipe(resp.outputStream); |
136 } else { | 139 } else { |
137 resp.statusCode = HttpStatus.NOT_FOUND; | 140 var directory = new Directory(path.toNativePath()); |
kustermann
2013/02/12 19:00:48
Since there is a 'new Directory.fromPath()' we sho
blois
2013/02/12 20:31:12
Done.
| |
138 try { | 141 directory.exists().then((exists) { |
139 resp.outputStream.close(); | 142 if (!exists) { |
140 } catch (e) { | 143 sendNotFound(resp); |
141 if (e is StreamException) { | |
142 print('Test http_server error closing the response stream: $e'); | |
143 } else { | 144 } else { |
144 throw e; | 145 sendDirectoryListing(directory, request, resp); |
145 } | 146 } |
146 } | 147 }); |
147 } | 148 } |
148 }); | 149 }); |
149 }; | 150 }; |
150 | 151 |
151 // Echos back the contents of the request as the response data. | 152 // Echos back the contents of the request as the response data. |
152 httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) { | 153 httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) { |
153 resp.headers.set("Access-Control-Allow-Origin", "*"); | 154 resp.headers.set("Access-Control-Allow-Origin", "*"); |
154 | 155 |
155 request.inputStream.pipe(resp.outputStream); | 156 request.inputStream.pipe(resp.outputStream); |
156 }); | 157 }); |
157 | 158 |
158 httpServer.listen(host, port); | 159 httpServer.listen(host, port); |
159 serverList.add(httpServer); | 160 serverList.add(httpServer); |
160 } | 161 } |
161 | 162 |
163 static void sendNotFound(HttpResponse response) { | |
164 response.statusCode = HttpStatus.NOT_FOUND; | |
165 try { | |
166 response.outputStream.close(); | |
167 } catch (e) { | |
168 if (e is StreamException) { | |
169 print('Test http_server error closing the response stream: $e'); | |
170 } else { | |
171 throw e; | |
172 } | |
173 } | |
174 } | |
175 | |
176 static void sendDirectoryListing(Directory directory, HttpRequest request, | |
177 HttpResponse response) { | |
178 var os = response.outputStream; | |
179 | |
180 var header = '''<!DOCTYPE html> | |
181 <html> | |
182 <head> | |
183 <title>${request.path}</title> | |
184 </head> | |
185 <body> | |
186 <code> | |
187 <div>${request.path}</div> | |
188 <hr/> | |
189 <ul>'''; | |
190 var footer = ''' | |
191 </ul> | |
192 </code> | |
193 </body> | |
194 </html>'''; | |
195 | |
196 os.writeString(header); | |
197 | |
198 var files = []; | |
199 | |
200 directory.list() | |
201 ..onFile = (filepath) { | |
202 files.add(filepath); | |
203 } | |
204 ..onDir = (dirpath) { | |
205 files.add('$dirpath/'); | |
kustermann
2013/02/12 19:00:48
AFAIK, this results in an entry in files with forw
blois
2013/02/12 20:31:12
Cleaned this up some- it's a bit odd as the '/' wa
| |
206 } | |
207 ..onDone = (_) { | |
208 files.sort(); | |
209 for (var file in files) { | |
210 var filename = new Path.raw(file).filename; | |
kustermann
2013/02/12 19:00:48
DirectoryLister gives AFAIK full native paths. So
blois
2013/02/12 20:31:12
Done.
| |
211 // no filename implies directory name. | |
212 if (filename.isEmpty) { | |
213 var dirname = new Path.raw(file).directoryPath.filename; | |
kustermann
2013/02/12 19:00:48
Same here.
blois
2013/02/12 20:31:12
Done.
| |
214 filename = '$dirname/'; | |
215 } | |
216 os.writeString( | |
217 '<li><a href="${request.path}$filename">$filename</a></li>'); | |
kustermann
2013/02/12 19:00:48
I think you implicitly assume that '${request.path
blois
2013/02/12 20:31:12
Yeah, and assuming that there is a trailing slash
| |
218 } | |
219 os.writeString(footer); | |
220 os.close(); | |
221 }; | |
222 } | |
223 | |
162 static terminateHttpServers() { | 224 static terminateHttpServers() { |
163 for (var server in serverList) server.close(); | 225 for (var server in serverList) server.close(); |
164 } | 226 } |
165 } | 227 } |
OLD | NEW |