| 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('test_suite.dart'); // For TestUtils. | 9 #import('test_suite.dart'); // For TestUtils. |
| 9 | 10 |
| 10 HttpServer _httpServer; | 11 HttpServer _httpServer; |
| 11 | 12 |
| 12 startHttpServer(String host, int port) { | 13 // TODO(ager): Get rid of this when we get the Mac to behave. |
| 14 int _retries = 10; |
| 15 |
| 16 Future<bool> startHttpServer(String host, int port) { |
| 17 var completer = new Completer(); |
| 13 var basePath = TestUtils.dartDir(); | 18 var basePath = TestUtils.dartDir(); |
| 14 _httpServer = new HttpServer(); | 19 _httpServer = new HttpServer(); |
| 15 _httpServer.onError = (e) { | 20 _httpServer.onError = (e) { |
| 16 // Consider errors in the builtin http server fatal. | 21 // Consider errors in the builtin http server fatal. |
| 17 // Intead of just throwing the exception we print | 22 // Intead of just throwing the exception we print |
| 18 // a message that makes it clearer what happened. | 23 // a message that makes it clearer what happened. |
| 19 print('Test http server error: $e'); | 24 print('Test http server error: $e'); |
| 20 exit(1); | 25 exit(1); |
| 21 }; | 26 }; |
| 22 _httpServer.defaultRequestHandler = (request, resp) { | 27 _httpServer.defaultRequestHandler = (request, resp) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 33 // Allow loading from localhost in browsers. | 38 // Allow loading from localhost in browsers. |
| 34 resp.headers.set("Access-Control-Allow-Origin", "*"); | 39 resp.headers.set("Access-Control-Allow-Origin", "*"); |
| 35 file.openInputStream().pipe(resp.outputStream); | 40 file.openInputStream().pipe(resp.outputStream); |
| 36 } else { | 41 } else { |
| 37 resp.statusCode = HttpStatus.NOT_FOUND; | 42 resp.statusCode = HttpStatus.NOT_FOUND; |
| 38 resp.outputStream.close(); | 43 resp.outputStream.close(); |
| 39 } | 44 } |
| 40 }); | 45 }); |
| 41 } | 46 } |
| 42 }; | 47 }; |
| 43 _httpServer.listen(host, port); | 48 |
| 49 // TODO(ager): Get rid of this when we get the mac to behave. |
| 50 // Even though we have set the SO_REUSEADDR the mac is not |
| 51 // happy and gives us address alread in use errors. |
| 52 try { |
| 53 _httpServer.listen(host, port); |
| 54 completer.complete(true); |
| 55 } catch (e) { |
| 56 if (_retries-- == 0) { |
| 57 completer.completeException(e); |
| 58 } |
| 59 new Timer(1000, (t) { |
| 60 startHttpServer(host, port).then((r) => completer.complete(r)); |
| 61 }); |
| 62 } |
| 63 return completer.future; |
| 44 } | 64 } |
| 45 | 65 |
| 46 terminateHttpServer() { | 66 terminateHttpServer() { |
| 47 _httpServer.close(); | 67 _httpServer.close(); |
| 48 } | 68 } |
| OLD | NEW |