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