| 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 _httpServer; | 11 HttpServer _httpServer; |
| 12 | 12 |
| 13 // TODO(ager): Get rid of this when we get the Mac to behave. | 13 void startHttpServer(String host, int port) { |
| 14 int _retries = 10; | |
| 15 | |
| 16 Future<bool> startHttpServer(String host, int port) { | |
| 17 var completer = new Completer(); | |
| 18 var basePath = TestUtils.dartDir(); | 14 var basePath = TestUtils.dartDir(); |
| 19 _httpServer = new HttpServer(); | 15 _httpServer = new HttpServer(); |
| 20 _httpServer.onError = (e) { | 16 _httpServer.onError = (e) { |
| 21 // Consider errors in the builtin http server fatal. | 17 // Consider errors in the builtin http server fatal. |
| 22 // Intead of just throwing the exception we print | 18 // Intead of just throwing the exception we print |
| 23 // a message that makes it clearer what happened. | 19 // a message that makes it clearer what happened. |
| 24 print('Test http server error: $e'); | 20 print('Test http server error: $e'); |
| 25 exit(1); | 21 exit(1); |
| 26 }; | 22 }; |
| 27 _httpServer.defaultRequestHandler = (request, resp) { | 23 _httpServer.defaultRequestHandler = (request, resp) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 39 resp.headers.set("Access-Control-Allow-Origin", "*"); | 35 resp.headers.set("Access-Control-Allow-Origin", "*"); |
| 40 file.openInputStream().pipe(resp.outputStream); | 36 file.openInputStream().pipe(resp.outputStream); |
| 41 } else { | 37 } else { |
| 42 resp.statusCode = HttpStatus.NOT_FOUND; | 38 resp.statusCode = HttpStatus.NOT_FOUND; |
| 43 resp.outputStream.close(); | 39 resp.outputStream.close(); |
| 44 } | 40 } |
| 45 }); | 41 }); |
| 46 } | 42 } |
| 47 }; | 43 }; |
| 48 | 44 |
| 49 // TODO(ager): Get rid of this when we get the mac to behave. | 45 _httpServer.listen(host, port); |
| 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; | |
| 64 } | 46 } |
| 65 | 47 |
| 66 terminateHttpServer() { | 48 terminateHttpServer() { |
| 67 _httpServer.close(); | 49 _httpServer.close(); |
| 68 } | 50 } |
| OLD | NEW |