| 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('server'); | 5 #library('server'); |
| 6 #import('dart:io'); | 6 #import('dart:io'); |
| 7 #import('../../pkg/args/lib/args.dart'); | 7 #import('../../pkg/args/lib/args.dart'); |
| 8 | 8 |
| 9 /** A simple HTTP server. Currently handles serving static files. */ | 9 /** A simple HTTP server. Currently handles serving static files. */ |
| 10 class HttpTestServer { | 10 class HttpTestServer { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 File f = new File(path); | 53 File f = new File(path); |
| 54 if (f.existsSync()) { | 54 if (f.existsSync()) { |
| 55 var p = path.substring(path.lastIndexOf('.') + 1).toLowerCase(); | 55 var p = path.substring(path.lastIndexOf('.') + 1).toLowerCase(); |
| 56 if (MIME_TYPES.containsKey(p)) { | 56 if (MIME_TYPES.containsKey(p)) { |
| 57 var ct = MIME_TYPES[p]; | 57 var ct = MIME_TYPES[p]; |
| 58 var idx = ct.indexOf('/'); | 58 var idx = ct.indexOf('/'); |
| 59 response.headers.contentType = | 59 response.headers.contentType = |
| 60 new ContentType(ct.substring(0, idx), | 60 new ContentType(ct.substring(0, idx), |
| 61 ct.substring(idx + 1)); | 61 ct.substring(idx + 1)); |
| 62 } | 62 } |
| 63 response.statusCode = HttpStatus.OK; | 63 f.openInputStream().pipe(response.outputStream); |
| 64 response.reasonPhrase = "OK"; | |
| 65 var content = f.readAsBytesSync(); | |
| 66 response.outputStream.write(content); | |
| 67 response.outputStream.close(); | |
| 68 } else { | 64 } else { |
| 69 response.statusCode = HttpStatus.NOT_FOUND; | 65 response.statusCode = HttpStatus.NOT_FOUND; |
| 70 response.reasonPhrase = "Not Found"; | 66 response.reasonPhrase = '$path does not exist'; |
| 71 response.outputStream.close(); | 67 response.outputStream.close(); |
| 72 } | 68 } |
| 73 } | 69 } |
| 74 } catch(e,s) { | 70 } catch(e,s) { |
| 75 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR; | 71 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR; |
| 76 response.reasonPhrase = "$e"; | 72 response.reasonPhrase = "$e"; |
| 73 response.outputStream.writeString(s.toString()); |
| 77 response.outputStream.close(); | 74 response.outputStream.close(); |
| 78 } | 75 } |
| 79 }; | 76 }; |
| 80 } | 77 } |
| 81 | 78 |
| 82 void addHandler(Function matcher, handler) { | 79 void addHandler(Function matcher, handler) { |
| 83 if (handler is Function) { | 80 if (handler is Function) { |
| 84 server.addRequestHandler(matcher, handler); | 81 server.addRequestHandler(matcher, handler); |
| 85 } else { | 82 } else { |
| 86 server.addRequestHandler(matcher, handler.onRequest); | 83 server.addRequestHandler(matcher, handler.onRequest); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 110 var argResults = optionsParser.parse(new Options().arguments); | 107 var argResults = optionsParser.parse(new Options().arguments); |
| 111 var server = new HttpTestServer( | 108 var server = new HttpTestServer( |
| 112 int.parse(argResults['port']), | 109 int.parse(argResults['port']), |
| 113 argResults['root']); | 110 argResults['root']); |
| 114 } catch (e) { | 111 } catch (e) { |
| 115 print(e); | 112 print(e); |
| 116 print('Usage: http_server_test_runner.dart <options>'); | 113 print('Usage: http_server_test_runner.dart <options>'); |
| 117 print(optionsParser.getUsage()); | 114 print(optionsParser.getUsage()); |
| 118 } | 115 } |
| 119 } | 116 } |
| OLD | NEW |