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 import '../../../pkg/args/lib/args.dart'; | |
11 import '../../../pkg/path/lib/path.dart' as pathLib; | |
10 | 12 |
11 HttpServer _httpServer; | 13 main() { |
Emily Fortuna
2013/01/10 02:07:10
Q: What happened to this file?
A: The main issue
ahe
2013/01/10 09:27:24
Great idea!
| |
14 /** Convenience method for local testing. */ | |
15 var parser = new ArgParser(); | |
16 parser.addOption('port', abbr: 'p', | |
17 help: 'The main server port we wish to respond to requests.', | |
18 defaultsTo: '0'); | |
19 parser.addOption('crossOriginPort', abbr: 'c', | |
20 help: 'A different port that accepts request from the main server port.', | |
21 defaultsTo: '0'); | |
22 parser.addFlag('help', abbr: 'h', negatable: false, | |
23 help: 'Print this usage information.'); | |
24 var args = parser.parse(new Options().arguments); | |
25 if (args['help']) { | |
26 print(parser.getUsage()); | |
27 } else { | |
28 // Pretend we're running test.dart so that TestUtils doesn't get confused | |
29 // about the "current directory." This is only used if we're trying to run | |
30 // this file independently for local testing. | |
31 TestUtils.testScriptPath = 'tools/test.dart'; | |
32 TestingServerRunner.setPackageRootDir({'mode': 'release'}); | |
12 | 33 |
13 void startHttpServer(String host, int port) { | 34 TestingServerRunner.startHttpServer('127.0.0.1', |
14 var basePath = TestUtils.dartDir(); | 35 port: int.parse(args['port'])); |
15 _httpServer = new HttpServer(); | 36 print( |
16 _httpServer.onError = (e) { | 37 'Server listening on port ${TestingServerRunner.serverList[0].port}.'); |
17 // Consider errors in the builtin http server fatal. | 38 TestingServerRunner.startHttpServer('127.0.0.1', |
18 // Intead of just throwing the exception we print | 39 allowedPort: TestingServerRunner.serverList[0].port, port: |
19 // a message that makes it clearer what happened. | 40 int.parse(args['crossOriginPort'])); |
20 print('Test http server error: $e'); | 41 print( |
21 exit(1); | 42 'Server listening on port ${TestingServerRunner.serverList[1].port}.'); |
22 }; | 43 } |
23 _httpServer.defaultRequestHandler = (request, resp) { | 44 } |
24 var requestPath = new Path(request.path).canonicalize(); | 45 /** |
25 if (!requestPath.isAbsolute) { | 46 * Runs a set of servers that are initialized specifically the needs for our |
26 resp.statusCode = HttpStatus.NOT_FOUND; | 47 * test framework, such as dealing with package-root. |
27 resp.outputStream.close(); | 48 */ |
28 } else { | 49 class TestingServerRunner { |
29 var path = basePath; | 50 static List serverList = []; |
30 requestPath.segments().forEach((s) => path = path.append(s)); | 51 static String _packageRootDir = null; |
31 var file = new File(path.toNativePath()); | 52 |
53 // Added as a getter so that the function will be called again each time the | |
54 // default request handler closure is executed. | |
55 static String get packageRootDir => _packageRootDir; | |
56 | |
57 static setPackageRootDir(Map configuration) { | |
58 _packageRootDir = pathLib.join(TestUtils.currentWorkingDirectory.toString(), | |
59 TestUtils.buildDir(configuration)); | |
60 } | |
61 | |
62 static startHttpServer(String host, {int allowedPort:-1, int port: 0}) { | |
63 var basePath = TestUtils.dartDir().toString(); | |
64 var httpServer = new HttpServer(); | |
65 var packagesDirName = 'packages'; | |
66 httpServer.onError = (e) { | |
67 // Consider errors in the builtin http server fatal. | |
68 // Intead of just throwing the exception we print | |
69 // a message that makes it clearer what happened. | |
70 print('Test http server error: $e'); | |
71 exit(1); | |
72 }; | |
73 httpServer.defaultRequestHandler = (request, resp) { | |
74 // Remove the initial slash in the request, since it's not an absolute | |
Mads Ager (google)
2013/01/10 09:18:55
I don't think this will work on Windows. I patched
Bob Nystrom
2013/01/10 19:36:43
Any details here? If there's bugs in the path pack
Mads Ager (google)
2013/01/11 10:11:03
This is not a problem with the pathos library but
Bob Nystrom
2013/01/11 16:27:42
Yes, that's exactly why Builder exists. Instead of
| |
75 // url. | |
76 var requestPath = pathLib.normalize(request.path).substring(1); | |
77 var path = pathLib.join(basePath, requestPath); | |
78 var file = new File(path); | |
79 | |
80 if (requestPath.toString().contains(packagesDirName)) { | |
Bob Nystrom
2013/01/10 19:36:43
This will do the wrong thing if you have a directo
Emily Fortuna
2013/01/11 02:57:12
Done.
| |
81 // Essentially implement the packages path rewriting, so we don't have | |
82 // to pass environment variables to the browsers. | |
83 requestPath = requestPath.substring(requestPath.indexOf(packagesDirName) ); | |
Bob Nystrom
2013/01/10 19:36:43
Long line.
Emily Fortuna
2013/01/11 02:57:12
Done.
| |
84 path = pathLib.join(packageRootDir, requestPath); | |
85 file = new File(path); | |
86 } | |
32 file.exists().then((exists) { | 87 file.exists().then((exists) { |
33 if (exists) { | 88 if (exists) { |
34 // Allow loading from localhost in browsers. | 89 if (allowedPort != -1) { |
35 resp.headers.set("Access-Control-Allow-Origin", "*"); | 90 // Allow loading from localhost:$allowedPort in browsers. |
91 resp.headers.set("Access-Control-Allow-Origin", | |
92 "http://127.0.0.1:$allowedPort"); | |
93 resp.headers.set('Access-Control-Allow-Credentials', 'true'); | |
94 } else { | |
95 // No allowedPort specified. Allow from anywhere (but cross-origin | |
96 // requests *with credentials* will fail because you can't use "*"). | |
97 resp.headers.set("Access-Control-Allow-Origin", "*"); | |
98 } | |
99 if (path.endsWith('.html')) { | |
100 resp.headers.set('Content-Type', 'text/html'); | |
101 } else if (path.endsWith('.js')) { | |
102 resp.headers.set('Content-Type', 'application/javascript'); | |
103 } else if (path.endsWith('.dart')) { | |
104 resp.headers.set('Content-Type', 'application/dart'); | |
105 } | |
36 file.openInputStream().pipe(resp.outputStream); | 106 file.openInputStream().pipe(resp.outputStream); |
37 } else { | 107 } else { |
38 resp.statusCode = HttpStatus.NOT_FOUND; | 108 resp.statusCode = HttpStatus.NOT_FOUND; |
39 resp.outputStream.close(); | 109 try { |
110 resp.outputStream.close(); | |
111 } catch (e) { | |
112 if (e is StreamException) { | |
113 print('Test http_server error closing the response stream: $e'); | |
114 } else { | |
115 throw e; | |
116 } | |
117 } | |
40 } | 118 } |
41 }); | 119 }); |
42 } | |
43 }; | |
44 | 120 |
45 // Echos back the contents of the request as the response data. | 121 }; |
46 _httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) { | |
47 resp.headers.set("Access-Control-Allow-Origin", "*"); | |
48 | 122 |
49 request.inputStream.pipe(resp.outputStream); | 123 // Echos back the contents of the request as the response data. |
50 }); | 124 httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) { |
125 resp.headers.set("Access-Control-Allow-Origin", "*"); | |
51 | 126 |
52 _httpServer.listen(host, port); | 127 request.inputStream.pipe(resp.outputStream); |
128 }); | |
129 | |
130 httpServer.listen(host, port); | |
131 serverList.add(httpServer); | |
132 } | |
133 | |
134 static terminateHttpServers() { | |
135 for (var server in serverList) server.close(); | |
136 } | |
53 } | 137 } |
54 | |
55 terminateHttpServer() { | |
56 if (_httpServer != null) _httpServer.close(); | |
57 } | |
OLD | NEW |