OLD | NEW |
---|---|
(Empty) | |
1 #library('server'); | |
Siggi Cherem (dart-lang)
2012/09/20 23:55:41
+cpyright, +docs
gram
2012/09/21 00:25:46
Done.
| |
2 #import('dart:io'); | |
3 #import('../../pkg/args/lib/args.dart'); | |
4 | |
5 class HttpTestServer { | |
6 HttpServer server; | |
7 static Map mime = null; | |
Siggi Cherem (dart-lang)
2012/09/20 23:55:41
nit: rename to mime_types, possibly make it a stat
gram
2012/09/21 00:25:46
That would be nice but for some reason Map literal
Siggi Cherem (dart-lang)
2012/09/21 00:37:53
there are considered if you prefix them with 'cons
| |
8 | |
9 /** If set, serve up static files from this directory. */ | |
10 String staticFileDirectory; | |
11 | |
12 HttpTestServer(port, this.staticFileDirectory) { | |
13 if (mime == null) { | |
14 /* A common subset of all possible MIME types. */ | |
15 mime = { | |
16 'json' : 'applicaton/json', | |
Siggi Cherem (dart-lang)
2012/09/20 23:55:41
FYI - you could possibly read lots of mime types f
gram
2012/09/21 00:25:46
I could, but I think the list I have here is reaso
| |
17 'js' : 'application/javascript', | |
18 'cgm' : 'image/cgm', | |
19 'g3fax': 'image/g3fax', | |
20 'gif' : 'image/gif', | |
21 'jpeg' : 'image/jpeg', | |
22 'jpg': 'image/jpeg', | |
23 'png': 'image/png', | |
24 'tif' : 'image/tiff', | |
25 'tiff': 'image/tiff', | |
26 'ac3' : 'audio/ac3', | |
27 'mp3' : 'audio/mpeg', | |
28 'ogg' : 'audio/ogg', | |
29 'css' : 'text/css', | |
30 'csv' : 'text/csv', | |
31 'htm' : 'text/html', | |
32 'html' : 'text/html', | |
33 'txt' : 'text/plain', | |
34 'rtf' : 'text/rtf', | |
35 'mp4' : 'video/mp4', | |
36 'qt' : 'video/quicktime', | |
37 'vc1' : 'video/vc1' | |
38 }; | |
39 } | |
40 server = new HttpServer(); | |
41 server.listen("127.0.0.1", port); | |
42 server.onError = (e) { | |
43 }; | |
44 server.defaultRequestHandler = | |
45 (HttpRequest request, HttpResponse response) { | |
Siggi Cherem (dart-lang)
2012/09/20 23:55:41
I might prefer to move these as methods on this cl
gram
2012/09/21 00:25:46
I don't think so. It uses a factory constructor an
Siggi Cherem (dart-lang)
2012/09/21 00:37:53
seems small enough now, but if this grows, this mi
| |
46 try { | |
47 if (staticFileDirectory != null) { | |
48 String fname = request.path; | |
49 String path = '$staticFileDirectory$fname'; | |
50 File f = new File(path); | |
51 if (f.existsSync()) { | |
52 var p = path.substring(path.lastIndexOf('.')+1).toLowerCase(); | |
53 if (mime.containsKey(p)) { | |
54 var ct = mime[p]; | |
55 var idx = ct.indexOf('/'); | |
56 response.headers.contentType = | |
57 new ContentType(ct.substring(0, idx), ct.substring(idx+1)); | |
58 } | |
59 response.statusCode = HttpStatus.OK; | |
60 response.reasonPhrase = "OK"; | |
61 var content = f.readAsBytesSync(); | |
62 response.outputStream.write(content); | |
63 response.outputStream.close(); | |
64 } else { | |
65 response.statusCode = HttpStatus.NOT_FOUND; | |
66 response.reasonPhrase = "Not Found"; | |
67 response.outputStream.close(); | |
68 } | |
69 } | |
70 } catch(e,s) { | |
71 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR; | |
72 response.reasonPhrase = "$e"; | |
73 response.outputStream.close(); | |
74 } | |
75 }; | |
76 } | |
77 | |
78 void addHandler(Function matcher, handler) { | |
79 if (handler is Function) { | |
80 server.addRequestHandler(matcher, handler); | |
81 } else { | |
82 server.addRequestHandler(matcher, handler.onRequest); | |
83 } | |
84 } | |
85 | |
86 void close() { | |
87 server.close(); | |
88 } | |
89 } | |
90 | |
91 ArgParser getOptionParser() { | |
92 var parser = new ArgParser(); | |
93 parser.addOption('port', abbr: 'p', | |
94 help: 'Set the server listening port.', | |
95 defaultsTo: '80'); | |
96 | |
97 parser.addOption('root', abbr: 'r', | |
98 help: 'Set the directory for static files.', | |
99 defaultsTo: '/tmp'); | |
100 return parser; | |
101 } | |
102 | |
103 main() { | |
104 var optionsParser = getOptionParser(); | |
105 try { | |
106 var argResults = optionsParser.parse(new Options().arguments); | |
107 var server = new HttpTestServer( | |
108 int.parse(argResults['port']), | |
109 argResults['root']); | |
110 } catch (e) { | |
111 print(e); | |
112 print('Usage: http_server_test_runner.dart <options>'); | |
113 print(optionsParser.getUsage()); | |
114 } | |
115 } | |
OLD | NEW |