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 |
| 11 // expected number of arguments, so test.dart doesn't rely on the args library? | 11 // expected number of arguments, so test.dart doesn't rely on the args library? |
| 12 // See discussion on https://codereview.chromium.org/11931025/. | 12 // See discussion on https://codereview.chromium.org/11931025/. |
| 13 import 'vendored_pkg/args/args.dart'; | 13 import 'vendored_pkg/args/args.dart'; |
| 14 | 14 |
| 15 main() { | 15 main() { |
| 16 /** Convenience method for local testing. */ | 16 /** Convenience method for local testing. */ |
| 17 var parser = new ArgParser(); | 17 var parser = new ArgParser(); |
| 18 parser.addOption('port', abbr: 'p', | 18 parser.addOption('port', abbr: 'p', |
| 19 help: 'The main server port we wish to respond to requests.', | 19 help: 'The main server port we wish to respond to requests.', |
| 20 defaultsTo: '0'); | 20 defaultsTo: '0'); |
| 21 parser.addOption('crossOriginPort', abbr: 'c', | 21 parser.addOption('crossOriginPort', abbr: 'c', |
| 22 help: 'A different port that accepts request from the main server port.', | 22 help: 'A different port that accepts request from the main server port.', |
| 23 defaultsTo: '0'); | 23 defaultsTo: '0'); |
| 24 parser.addOption('mode', abbr: 'm', help: 'Testing mode.', | 24 parser.addOption('mode', abbr: 'm', help: 'Testing mode.', |
|
ahe
2013/02/01 10:28:59
Unused option.
| |
| 25 defaultsTo: 'release'); | 25 defaultsTo: 'release'); |
| 26 parser.addOption('arch', abbr: 'a', help: 'Testing architecture.', | 26 parser.addOption('arch', abbr: 'a', help: 'Testing architecture.', |
| 27 defaultsTo: 'ia32'); | 27 defaultsTo: 'ia32'); |
|
ahe
2013/02/01 10:28:59
Unused option.
| |
| 28 parser.addFlag('help', abbr: 'h', negatable: false, | 28 parser.addFlag('help', abbr: 'h', negatable: false, |
| 29 help: 'Print this usage information.'); | 29 help: 'Print this usage information.'); |
| 30 parser.addOption('package-root', help: 'The package root to use.'); | |
| 30 var args = parser.parse(new Options().arguments); | 31 var args = parser.parse(new Options().arguments); |
| 31 if (args['help']) { | 32 if (args['help']) { |
| 32 print(parser.getUsage()); | 33 print(parser.getUsage()); |
| 33 } else { | 34 } else { |
| 34 // 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 |
| 35 // 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 |
| 36 // this file independently for local testing. | 37 // this file independently for local testing. |
| 37 TestUtils.testScriptPath = new Path(new Options().script) | 38 TestUtils.testScriptPath = new Path(new Options().script) |
| 38 .directoryPath | 39 .directoryPath |
| 39 .join(new Path('../../test.dart')) | 40 .join(new Path('../../test.dart')) |
| 40 .canonicalize() | 41 .canonicalize() |
| 41 .toNativePath(); | 42 .toNativePath(); |
| 42 TestingServerRunner.setPackageRootDir({'mode': args['mode'], | 43 TestingServerRunner._packageRootDir = new Path(args['package-root']); |
| 43 'arch': args['arch'], 'system': Platform.operatingSystem, | |
| 44 'build_directory': ''}); | |
| 45 | |
| 46 TestingServerRunner.startHttpServer('127.0.0.1', | 44 TestingServerRunner.startHttpServer('127.0.0.1', |
| 47 port: int.parse(args['port'])); | 45 port: int.parse(args['port'])); |
| 48 print('Server listening on port ' | 46 print('Server listening on port ' |
| 49 '${TestingServerRunner.serverList[0].port}.'); | 47 '${TestingServerRunner.serverList[0].port}.'); |
| 50 TestingServerRunner.startHttpServer('127.0.0.1', | 48 TestingServerRunner.startHttpServer('127.0.0.1', |
| 51 allowedPort: TestingServerRunner.serverList[0].port, port: | 49 allowedPort: TestingServerRunner.serverList[0].port, port: |
| 52 int.parse(args['crossOriginPort'])); | 50 int.parse(args['crossOriginPort'])); |
| 53 print( | 51 print( |
| 54 'Server listening on port ${TestingServerRunner.serverList[1].port}.'); | 52 'Server listening on port ${TestingServerRunner.serverList[1].port}.'); |
| 55 } | 53 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 resp.outputStream.close(); | 116 resp.outputStream.close(); |
| 119 } catch (e) { | 117 } catch (e) { |
| 120 if (e is StreamException) { | 118 if (e is StreamException) { |
| 121 print('Test http_server error closing the response stream: $e'); | 119 print('Test http_server error closing the response stream: $e'); |
| 122 } else { | 120 } else { |
| 123 throw e; | 121 throw e; |
| 124 } | 122 } |
| 125 } | 123 } |
| 126 } | 124 } |
| 127 }); | 125 }); |
| 128 | |
| 129 }; | 126 }; |
| 130 | 127 |
| 131 // Echos back the contents of the request as the response data. | 128 // Echos back the contents of the request as the response data. |
| 132 httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) { | 129 httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) { |
| 133 resp.headers.set("Access-Control-Allow-Origin", "*"); | 130 resp.headers.set("Access-Control-Allow-Origin", "*"); |
| 134 | 131 |
| 135 request.inputStream.pipe(resp.outputStream); | 132 request.inputStream.pipe(resp.outputStream); |
| 136 }); | 133 }); |
| 137 | 134 |
| 138 httpServer.listen(host, port); | 135 httpServer.listen(host, port); |
| 139 serverList.add(httpServer); | 136 serverList.add(httpServer); |
| 140 } | 137 } |
| 141 | 138 |
| 142 static terminateHttpServers() { | 139 static terminateHttpServers() { |
| 143 for (var server in serverList) server.close(); | 140 for (var server in serverList) server.close(); |
| 144 } | 141 } |
| 145 } | 142 } |
| OLD | NEW |