Chromium Code Reviews| Index: tools/testing/dart/http_server.dart |
| diff --git a/tools/testing/dart/http_server.dart b/tools/testing/dart/http_server.dart |
| index 6966ab3fcf88f0925011bfe37b52070baad597e7..20c02b3c30ea99573b5a85ecc529f146b036c75d 100644 |
| --- a/tools/testing/dart/http_server.dart |
| +++ b/tools/testing/dart/http_server.dart |
| @@ -6,6 +6,7 @@ library http_server; |
| import 'dart:io'; |
| import 'dart:isolate'; |
| +import 'dart:uri'; |
| import 'test_suite.dart'; // For TestUtils. |
| // TODO(efortuna): Rewrite to not use the args library and simply take an |
| // expected number of arguments, so test.dart doesn't rely on the args library? |
| @@ -28,6 +29,8 @@ main() { |
| parser.addFlag('help', abbr: 'h', negatable: false, |
| help: 'Print this usage information.'); |
| parser.addOption('package-root', help: 'The package root to use.'); |
| + parser.addOption('network', help: 'The network interface to use.', |
| + defaultsTo: '127.0.0.1'); |
| var args = parser.parse(new Options().arguments); |
| if (args['help']) { |
| print(parser.getUsage()); |
| @@ -45,11 +48,12 @@ main() { |
| // TODO: We should probably rename 'package-root' to 'build-directory'. |
| TestingServerRunner._packageRootDir = new Path(args['package-root']); |
| TestingServerRunner._buildDirectory = new Path(args['package-root']); |
| - TestingServerRunner.startHttpServer('127.0.0.1', |
| + var network = args['network']; |
| + TestingServerRunner.startHttpServer(network, |
| port: int.parse(args['port'])); |
| print('Server listening on port ' |
| '${TestingServerRunner.serverList[0].port}.'); |
| - TestingServerRunner.startHttpServer('127.0.0.1', |
| + TestingServerRunner.startHttpServer(network, |
| allowedPort: TestingServerRunner.serverList[0].port, port: |
| int.parse(args['crossOriginPort'])); |
| print( |
| @@ -116,9 +120,11 @@ class TestingServerRunner { |
| file.exists().then((exists) { |
| if (exists) { |
| if (allowedPort != -1) { |
| - // Allow loading from localhost:$allowedPort in browsers. |
| - resp.headers.set("Access-Control-Allow-Origin", |
| - "http://127.0.0.1:$allowedPort"); |
| + var origin = new Uri(request.headers['Origin'][0]); |
|
kustermann
2013/02/12 21:03:44
The 'Origin' header may not be present on HttpRequ
blois
2013/02/12 23:06:56
Updated. I wasn't aware that this server was inten
|
| + // Allow loading from http://*:$allowedPort in browsers. |
| + var allowedOrigin = |
| + '${origin.scheme}://${origin.domain}:${allowedPort}'; |
| + resp.headers.set("Access-Control-Allow-Origin", allowedOrigin); |
| resp.headers.set('Access-Control-Allow-Credentials', 'true'); |
| } else { |
| // No allowedPort specified. Allow from anywhere (but cross-origin |
| @@ -134,16 +140,14 @@ class TestingServerRunner { |
| } |
| file.openInputStream().pipe(resp.outputStream); |
| } else { |
| - resp.statusCode = HttpStatus.NOT_FOUND; |
| - try { |
| - resp.outputStream.close(); |
| - } catch (e) { |
| - if (e is StreamException) { |
| - print('Test http_server error closing the response stream: $e'); |
| + var directory = new Directory.fromPath(path); |
| + directory.exists().then((exists) { |
| + if (!exists) { |
| + sendNotFound(resp); |
| } else { |
| - throw e; |
| + sendDirectoryListing(directory, request, resp); |
| } |
| - } |
| + }); |
| } |
| }); |
| }; |
| @@ -159,7 +163,77 @@ class TestingServerRunner { |
| serverList.add(httpServer); |
| } |
| + static void sendNotFound(HttpResponse response) { |
| + response.statusCode = HttpStatus.NOT_FOUND; |
| + try { |
| + response.outputStream.close(); |
| + } catch (e) { |
| + if (e is StreamException) { |
| + print('Test http_server error closing the response stream: $e'); |
| + } else { |
| + throw e; |
| + } |
| + } |
| + } |
| + |
| + static void sendDirectoryListing(Directory directory, HttpRequest request, |
|
Emily Fortuna
2013/02/12 22:21:24
can you add a little explanatory comment on this f
|
| + HttpResponse response) { |
| + var os = response.outputStream; |
|
kustermann
2013/02/12 21:03:44
You may want to set the content-type header to htm
Emily Fortuna
2013/02/12 22:21:24
+1 Chrome can handle it, but I've found DumpRender
blois
2013/02/12 23:06:56
Updated, though I've been using it OK on IE and iO
|
| + var header = '''<!DOCTYPE html> |
| + <html> |
| + <head> |
| + <title>${request.path}</title> |
| + </head> |
| + <body> |
| + <code> |
| + <div>${request.path}</div> |
| + <hr/> |
| + <ul>'''; |
| + var footer = ''' |
| + </ul> |
| + </code> |
| + </body> |
| + </html>'''; |
| + |
| + os.writeString(header); |
| + |
| + var entries = []; |
| + |
| + directory.list() |
| + ..onFile = (filepath) { |
| + var filename = new Path(filepath).filename; |
| + entries.add(new _Entry(filename, filename)); |
| + } |
| + ..onDir = (dirpath) { |
| + var filename = new Path(dirpath).filename; |
| + entries.add(new _Entry(filename, '$filename/')); |
| + } |
| + ..onDone = (_) { |
| + var requestPath = new Path.raw(request.path); |
| + entries.sort(); |
| + for (var entry in entries) { |
| + os.writeString( |
| + '<li><a href="${requestPath.append(entry.name)}">' |
| + '${entry.displayName}</a></li>'); |
| + } |
| + os.writeString(footer); |
| + os.close(); |
| + }; |
| + } |
| + |
| static terminateHttpServers() { |
| for (var server in serverList) server.close(); |
| } |
| } |
| + |
| +// Helper class for displaying directory listings. |
| +class _Entry { |
| + final String name; |
| + final String displayName; |
| + |
| + _Entry(this.name, this.displayName); |
| + |
| + int compareTo(_Entry other) { |
| + return name.compareTo(other.name); |
| + } |
| +} |