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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fa3b1af8bdfe4862044ebdedd8a7b1e4a1813792 |
| --- /dev/null |
| +++ b/tools/testing/dart/http_server.dart |
| @@ -0,0 +1,44 @@ |
| +#library('http_server'); |
| + |
| +#import('dart:io'); |
| +#import('test_suite.dart'); // For TestUtils. |
| + |
| +HttpServer _httpServer; |
| + |
| +startHttpServer(String host, int port) { |
| + var basePath = TestUtils.dartDir(); |
| + _httpServer = new HttpServer(); |
| + _httpServer.listen(host, port); |
| + _httpServer.onError = (e) { |
| + // Consider errors in the builtin http server fatal. |
| + // Intead of just throwing the exception we print |
| + // a message that makes it clearer what happened. |
| + print('Test http server error: $e'); |
| + exit(1); |
| + }; |
| + _httpServer.defaultRequestHandler = (request, resp) { |
| + var requestPath = new Path(request.path).canonicalize(); |
| + if (!requestPath.isAbsolute) { |
| + resp.statusCode = HttpStatus.NOT_FOUND; |
| + resp.outputStream.close(); |
| + } else { |
| + var path = basePath; |
| + requestPath.segments().forEach((s) => path = path.append(s)); |
| + var file = new File(path.toNativePath()); |
| + file.exists().then((exists) { |
| + if (exists) { |
|
Søren Gjesse
2012/10/04 10:34:24
How about adding a Content-Type based on the file
Mads Ager (google)
2012/10/04 10:41:42
Yeah, let's add that when we need it. I would like
|
| + // Allow loading from localhost in browsers. |
| + resp.headers.set("Access-Control-Allow-Origin", "*"); |
| + file.openInputStream().pipe(resp.outputStream); |
| + } else { |
| + resp.statusCode = HttpStatus.NOT_FOUND; |
| + resp.outputStream.close(); |
| + } |
| + }); |
| + } |
| + }; |
| +} |
| + |
| +terminateHttpServer() { |
| + _httpServer.close(); |
| +} |