Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #library('server'); | |
| 6 #import('dart:io'); | |
| 7 #import('../../pkg/args/lib/args.dart'); | |
| 8 | |
| 9 /** A simple HTTP server. Currently handles serving static files. */ | |
| 10 class HttpTestServer { | |
| 11 HttpServer server; | |
| 12 | |
| 13 /** If set, serve up static files from this directory. */ | |
| 14 String staticFileDirectory; | |
| 15 | |
| 16 /* A common subset of all possible MIME types. */ | |
| 17 static const MIME_TYPES = const { | |
| 18 'json' : 'applicaton/json', | |
| 19 'js' : 'application/javascript', | |
| 20 'cgm' : 'image/cgm', | |
| 21 'g3fax': 'image/g3fax', | |
| 22 'gif' : 'image/gif', | |
| 23 'jpeg' : 'image/jpeg', | |
| 24 'jpg' : 'image/jpeg', | |
| 25 'png' : 'image/png', | |
| 26 'tif' : 'image/tiff', | |
| 27 'tiff' : 'image/tiff', | |
| 28 'ac3' : 'audio/ac3', | |
| 29 'mp3' : 'audio/mpeg', | |
| 30 'ogg' : 'audio/ogg', | |
| 31 'css' : 'text/css', | |
| 32 'csv' : 'text/csv', | |
| 33 'htm' : 'text/html', | |
| 34 'html' : 'text/html', | |
| 35 'txt' : 'text/plain', | |
| 36 'rtf' : 'text/rtf', | |
| 37 'mp4' : 'video/mp4', | |
| 38 'qt' : 'video/quicktime', | |
| 39 'vc1' : 'video/vc1' | |
| 40 }; | |
| 41 | |
| 42 HttpTestServer(port, this.staticFileDirectory) { | |
| 43 server = new HttpServer(); | |
| 44 server.listen("127.0.0.1", port); | |
| 45 server.onError = (e) { | |
| 46 }; | |
| 47 server.defaultRequestHandler = | |
| 48 (HttpRequest request, HttpResponse response) { | |
| 49 try { | |
| 50 if (staticFileDirectory != null) { | |
| 51 String fname = request.path; | |
| 52 String path = '$staticFileDirectory$fname'; | |
| 53 File f = new File(path); | |
| 54 if (f.existsSync()) { | |
| 55 var p = path.substring(path.lastIndexOf('.') + 1).toLowerCase(); | |
| 56 if (MIME_TYPES.containsKey(p)) { | |
| 57 var ct = MIME_TYPES[p]; | |
| 58 var idx = ct.indexOf('/'); | |
| 59 response.headers.contentType = | |
| 60 new ContentType(ct.substring(0, idx), | |
| 61 ct.substring(idx + 1)); | |
| 62 } | |
|
Søren Gjesse
2012/09/21 07:08:09
These two lines are not needed. The default status
| |
| 63 response.statusCode = HttpStatus.OK; | |
| 64 response.reasonPhrase = "OK"; | |
|
Søren Gjesse
2012/09/21 07:08:09
Here you can just do
f.openInputStream().pipe(r
| |
| 65 var content = f.readAsBytesSync(); | |
| 66 response.outputStream.write(content); | |
| 67 response.outputStream.close(); | |
| 68 } else { | |
| 69 response.statusCode = HttpStatus.NOT_FOUND; | |
|
Søren Gjesse
2012/09/21 07:08:09
No need to set reason phrase.
| |
| 70 response.reasonPhrase = "Not Found"; | |
| 71 response.outputStream.close(); | |
| 72 } | |
| 73 } | |
| 74 } catch(e,s) { | |
| 75 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR; | |
| 76 response.reasonPhrase = "$e"; | |
|
Søren Gjesse
2012/09/21 07:08:09
Maybe add
response.writeString(s.toString());
| |
| 77 response.outputStream.close(); | |
| 78 } | |
| 79 }; | |
| 80 } | |
| 81 | |
| 82 void addHandler(Function matcher, handler) { | |
| 83 if (handler is Function) { | |
| 84 server.addRequestHandler(matcher, handler); | |
| 85 } else { | |
| 86 server.addRequestHandler(matcher, handler.onRequest); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 void close() { | |
| 91 server.close(); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 ArgParser getOptionParser() { | |
| 96 var parser = new ArgParser(); | |
| 97 parser.addOption('port', abbr: 'p', | |
| 98 help: 'Set the server listening port.', | |
| 99 defaultsTo: '80'); | |
| 100 | |
| 101 parser.addOption('root', abbr: 'r', | |
| 102 help: 'Set the directory for static files.', | |
| 103 defaultsTo: '/tmp'); | |
| 104 return parser; | |
| 105 } | |
| 106 | |
| 107 main() { | |
| 108 var optionsParser = getOptionParser(); | |
| 109 try { | |
| 110 var argResults = optionsParser.parse(new Options().arguments); | |
| 111 var server = new HttpTestServer( | |
| 112 int.parse(argResults['port']), | |
| 113 argResults['root']); | |
| 114 } catch (e) { | |
| 115 print(e); | |
| 116 print('Usage: http_server_test_runner.dart <options>'); | |
| 117 print(optionsParser.getUsage()); | |
| 118 } | |
| 119 } | |
| OLD | NEW |