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..86dd0d2fa9193899cb0e372b6ee6be14637835f0 100644 |
--- a/tools/testing/dart/http_server.dart |
+++ b/tools/testing/dart/http_server.dart |
@@ -28,6 +28,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 +47,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( |
@@ -118,7 +121,7 @@ class TestingServerRunner { |
if (allowedPort != -1) { |
// Allow loading from localhost:$allowedPort in browsers. |
resp.headers.set("Access-Control-Allow-Origin", |
- "http://127.0.0.1:$allowedPort"); |
+ 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
|
resp.headers.set('Access-Control-Allow-Credentials', 'true'); |
} else { |
// No allowedPort specified. Allow from anywhere (but cross-origin |
@@ -134,16 +137,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(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.
|
+ directory.exists().then((exists) { |
+ if (!exists) { |
+ sendNotFound(resp); |
} else { |
- throw e; |
+ sendDirectoryListing(directory, request, resp); |
} |
- } |
+ }); |
} |
}); |
}; |
@@ -159,6 +160,67 @@ 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, |
+ HttpResponse response) { |
+ var os = response.outputStream; |
+ |
+ 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 files = []; |
+ |
+ directory.list() |
+ ..onFile = (filepath) { |
+ files.add(filepath); |
+ } |
+ ..onDir = (dirpath) { |
+ 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
|
+ } |
+ ..onDone = (_) { |
+ files.sort(); |
+ for (var file in files) { |
+ 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.
|
+ // no filename implies directory name. |
+ if (filename.isEmpty) { |
+ 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.
|
+ filename = '$dirname/'; |
+ } |
+ os.writeString( |
+ '<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
|
+ } |
+ os.writeString(footer); |
+ os.close(); |
+ }; |
+ } |
+ |
static terminateHttpServers() { |
for (var server in serverList) server.close(); |
} |