Chromium Code Reviews| 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 23 matching lines...) Expand all Loading... | |
| 34 } else { | 34 } else { |
| 35 // Pretend we're running test.dart so that TestUtils doesn't get confused | 35 // 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 | 36 // about the "current directory." This is only used if we're trying to run |
| 37 // this file independently for local testing. | 37 // this file independently for local testing. |
| 38 TestUtils.testScriptPath = new Path(new Options().script) | 38 TestUtils.testScriptPath = new Path(new Options().script) |
| 39 .directoryPath | 39 .directoryPath |
| 40 .join(new Path('../../test.dart')) | 40 .join(new Path('../../test.dart')) |
| 41 .canonicalize() | 41 .canonicalize() |
| 42 .toNativePath(); | 42 .toNativePath(); |
| 43 TestingServerRunner._packageRootDir = new Path(args['package-root']); | 43 TestingServerRunner._packageRootDir = new Path(args['package-root']); |
| 44 TestingServerRunner._buildDirectory = new Path(args['package-root']); | |
|
ricow1
2013/02/06 08:13:28
setting the _buildDirectory to the package root ca
kustermann
2013/02/06 16:33:51
Done.
| |
| 44 TestingServerRunner.startHttpServer('127.0.0.1', | 45 TestingServerRunner.startHttpServer('127.0.0.1', |
| 45 port: int.parse(args['port'])); | 46 port: int.parse(args['port'])); |
| 46 print('Server listening on port ' | 47 print('Server listening on port ' |
| 47 '${TestingServerRunner.serverList[0].port}.'); | 48 '${TestingServerRunner.serverList[0].port}.'); |
| 48 TestingServerRunner.startHttpServer('127.0.0.1', | 49 TestingServerRunner.startHttpServer('127.0.0.1', |
| 49 allowedPort: TestingServerRunner.serverList[0].port, port: | 50 allowedPort: TestingServerRunner.serverList[0].port, port: |
| 50 int.parse(args['crossOriginPort'])); | 51 int.parse(args['crossOriginPort'])); |
| 51 print( | 52 print( |
| 52 'Server listening on port ${TestingServerRunner.serverList[1].port}.'); | 53 'Server listening on port ${TestingServerRunner.serverList[1].port}.'); |
| 53 } | 54 } |
| 54 } | 55 } |
| 55 /** | 56 /** |
| 56 * Runs a set of servers that are initialized specifically for the needs of our | 57 * Runs a set of servers that are initialized specifically for the needs of our |
| 57 * test framework, such as dealing with package-root. | 58 * test framework, such as dealing with package-root. |
| 58 */ | 59 */ |
| 59 class TestingServerRunner { | 60 class TestingServerRunner { |
| 60 static List serverList = []; | 61 static List serverList = []; |
| 61 static Path _packageRootDir = null; | 62 static Path _packageRootDir = null; |
| 63 static Path _buildDirectory = null; | |
| 62 | 64 |
| 63 // Added as a getter so that the function will be called again each time the | 65 // Added as a getter so that the function will be called again each time the |
| 64 // default request handler closure is executed. | 66 // default request handler closure is executed. |
| 65 static Path get packageRootDir => _packageRootDir; | 67 static Path get packageRootDir => _packageRootDir; |
| 68 static Path get buildDirectory => _buildDirectory; | |
| 66 | 69 |
| 67 static setPackageRootDir(Map configuration) { | 70 static setPackageRootDir(Map configuration) { |
| 68 _packageRootDir = TestUtils.currentWorkingDirectory.join( | 71 _packageRootDir = TestUtils.currentWorkingDirectory.join( |
| 69 new Path(TestUtils.buildDir(configuration))); | 72 new Path(TestUtils.buildDir(configuration))); |
| 70 } | 73 } |
| 71 | 74 |
| 75 static setBuildDir(Map configuration) { | |
| 76 _buildDirectory = new Path(TestUtils.buildDir(configuration)); | |
| 77 } | |
| 78 | |
| 72 static startHttpServer(String host, {int allowedPort:-1, int port: 0}) { | 79 static startHttpServer(String host, {int allowedPort:-1, int port: 0}) { |
| 73 var basePath = TestUtils.dartDir(); | 80 var basePath = TestUtils.dartDir(); |
| 74 var httpServer = new HttpServer(); | 81 var httpServer = new HttpServer(); |
| 75 var packagesDirName = 'packages'; | 82 var packagesDirName = 'packages'; |
| 76 httpServer.onError = (e) { | 83 httpServer.onError = (e) { |
| 77 // TODO(ricow): Once we have a debug log we should write this out there. | 84 // TODO(ricow): Once we have a debug log we should write this out there. |
| 78 print('Test http server error: $e'); | 85 print('Test http server error: $e'); |
| 79 }; | 86 }; |
| 80 httpServer.defaultRequestHandler = (request, resp) { | 87 httpServer.defaultRequestHandler = (request, resp) { |
| 88 // TODO(kustermann,ricow): We could change this to the following scheme: | |
|
ricow1
2013/02/06 08:13:28
file a bug
kustermann
2013/02/06 16:33:51
Done.
| |
| 89 // http://host:port/root_dart/X -> $DartDir/X | |
| 90 // http://host:port/root_build/X -> $BuildDir/X | |
| 91 // http://host:port/root_packages/X -> $BuildDir/packages/X | |
| 92 | |
| 81 var requestPath = new Path(request.path.substring(1)).canonicalize(); | 93 var requestPath = new Path(request.path.substring(1)).canonicalize(); |
| 82 var path = basePath.join(requestPath); | 94 var path = basePath.join(requestPath); |
| 83 var file = new File(path.toNativePath()); | 95 var file = new File(path.toNativePath()); |
| 84 | 96 // Since the build directory may not be located directly beneath the dart |
| 97 // root directory (if we pass it in, e.g., for dartium testing) we serve | |
| 98 // files from the build directory explicitly. Please note that if | |
| 99 // buildDirectory has the same name as a directory inside the dart repo | |
| 100 // we will server files from the buildDirectory. | |
| 101 if (requestPath.toString().startsWith(buildDirectory.toString())) { | |
| 102 file = new File(requestPath.toNativePath()); | |
| 103 } | |
| 85 if (requestPath.segments().contains(packagesDirName)) { | 104 if (requestPath.segments().contains(packagesDirName)) { |
| 86 // Essentially implement the packages path rewriting, so we don't have | 105 // Essentially implement the packages path rewriting, so we don't have |
| 87 // to pass environment variables to the browsers. | 106 // to pass environment variables to the browsers. |
| 88 var requestPathStr = requestPath.toNativePath().substring( | 107 var requestPathStr = requestPath.toNativePath().substring( |
| 89 requestPath.toNativePath().indexOf(packagesDirName)); | 108 requestPath.toNativePath().indexOf(packagesDirName)); |
| 90 path = packageRootDir.append(requestPathStr); | 109 path = packageRootDir.append(requestPathStr); |
| 91 file = new File(path.toNativePath()); | 110 file = new File(path.toNativePath()); |
| 92 } | 111 } |
| 93 file.exists().then((exists) { | 112 file.exists().then((exists) { |
| 94 if (exists) { | 113 if (exists) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 }); | 152 }); |
| 134 | 153 |
| 135 httpServer.listen(host, port); | 154 httpServer.listen(host, port); |
| 136 serverList.add(httpServer); | 155 serverList.add(httpServer); |
| 137 } | 156 } |
| 138 | 157 |
| 139 static terminateHttpServers() { | 158 static terminateHttpServers() { |
| 140 for (var server in serverList) server.close(); | 159 for (var server in serverList) server.close(); |
| 141 } | 160 } |
| 142 } | 161 } |
| OLD | NEW |