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 | 10 |
11 HttpServer startHttpServer(String host, [int allowedPort = -1]) { | 11 HttpServer _httpServer; |
| 12 |
| 13 void startHttpServer(String host, int port) { |
12 var basePath = TestUtils.dartDir(); | 14 var basePath = TestUtils.dartDir(); |
13 var httpServer = new HttpServer(); | 15 _httpServer = new HttpServer(); |
14 httpServer.onError = (e) { | 16 _httpServer.onError = (e) { |
15 // Consider errors in the builtin http server fatal. | 17 // Consider errors in the builtin http server fatal. |
16 // Intead of just throwing the exception we print | 18 // Intead of just throwing the exception we print |
17 // a message that makes it clearer what happened. | 19 // a message that makes it clearer what happened. |
18 print('Test http server error: $e'); | 20 print('Test http server error: $e'); |
19 exit(1); | 21 exit(1); |
20 }; | 22 }; |
21 httpServer.defaultRequestHandler = (request, resp) { | 23 _httpServer.defaultRequestHandler = (request, resp) { |
22 var requestPath = new Path(request.path).canonicalize(); | 24 var requestPath = new Path(request.path).canonicalize(); |
23 if (!requestPath.isAbsolute) { | 25 if (!requestPath.isAbsolute) { |
24 resp.statusCode = HttpStatus.NOT_FOUND; | 26 resp.statusCode = HttpStatus.NOT_FOUND; |
25 resp.outputStream.close(); | 27 resp.outputStream.close(); |
26 } else { | 28 } else { |
27 var path = basePath; | 29 var path = basePath; |
28 requestPath.segments().forEach((s) => path = path.append(s)); | 30 requestPath.segments().forEach((s) => path = path.append(s)); |
29 var file = new File(path.toNativePath()); | 31 var file = new File(path.toNativePath()); |
30 file.exists().then((exists) { | 32 file.exists().then((exists) { |
31 if (exists) { | 33 if (exists) { |
32 if (allowedPort != -1) { | 34 // Allow loading from localhost in browsers. |
33 // Allow loading from localhost:$allowedPort in browsers. | 35 resp.headers.set("Access-Control-Allow-Origin", "*"); |
34 resp.headers.set("Access-Control-Allow-Origin", | |
35 "http://127.0.0.1:$allowedPort"); | |
36 resp.headers.set('Access-Control-Allow-Credentials', 'true'); | |
37 } else { | |
38 // No allowedPort specified. Allow from anywhere (but cross-origin | |
39 // requests *with credentials* will fail because you can't use "*"). | |
40 resp.headers.set("Access-Control-Allow-Origin", "*"); | |
41 } | |
42 if (path.toNativePath().endsWith('.html')) { | |
43 resp.headers.set('Content-Type', 'text/html'); | |
44 } else if (path.toNativePath().endsWith('.js')) { | |
45 resp.headers.set('Content-Type', 'application/javascript'); | |
46 } else if (path.toNativePath().endsWith('.dart')) { | |
47 resp.headers.set('Content-Type', 'application/dart'); | |
48 } | |
49 file.openInputStream().pipe(resp.outputStream); | 36 file.openInputStream().pipe(resp.outputStream); |
50 } else { | 37 } else { |
51 resp.statusCode = HttpStatus.NOT_FOUND; | 38 resp.statusCode = HttpStatus.NOT_FOUND; |
52 resp.outputStream.close(); | 39 resp.outputStream.close(); |
53 } | 40 } |
54 }); | 41 }); |
55 } | 42 } |
56 }; | 43 }; |
57 | 44 |
58 // Echos back the contents of the request as the response data. | 45 // Echos back the contents of the request as the response data. |
59 httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) { | 46 _httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) { |
60 resp.headers.set("Access-Control-Allow-Origin", "*"); | 47 resp.headers.set("Access-Control-Allow-Origin", "*"); |
61 | 48 |
62 request.inputStream.pipe(resp.outputStream); | 49 request.inputStream.pipe(resp.outputStream); |
63 }); | 50 }); |
64 | 51 |
65 httpServer.listen(host, 0); | 52 _httpServer.listen(host, port); |
66 return httpServer; | |
67 } | 53 } |
68 | 54 |
69 terminateHttpServers(List<HttpServer> servers) { | 55 terminateHttpServer() { |
70 for (var server in servers) server.close(); | 56 if (_httpServer != null) _httpServer.close(); |
71 } | 57 } |
OLD | NEW |