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 http_server; | |
| 6 import 'dart:io'; | |
| 7 import 'package:args/args.dart'; | |
| 8 | |
| 9 /** An options parser for the server. */ | |
| 10 ArgParser getOptionParser() { | |
| 11 var parser = new ArgParser(); | |
| 12 parser.addOption('port', abbr: 'p', | |
| 13 help: 'Set the server listening port.', | |
| 14 defaultsTo: '80'); | |
| 15 | |
| 16 parser.addOption('root', abbr: 'r', | |
| 17 help: 'Set the directory for static files.'); | |
| 18 return parser; | |
| 19 } | |
| 20 | |
| 21 /** A simple HTTP server. Currently handles serving static files. */ | |
| 22 class HttpTestServer { | |
| 23 HttpServer server; | |
| 24 | |
| 25 /** If set, serve up static files from this directory. */ | |
| 26 String staticFileDirectory; | |
| 27 | |
| 28 /* A common subset of all possible MIME types. */ | |
| 29 static const MIME_TYPES = const { | |
| 30 'json' : 'applicaton/json', | |
| 31 'js' : 'application/javascript', | |
| 32 'cgm' : 'image/cgm', | |
| 33 'g3fax': 'image/g3fax', | |
| 34 'gif' : 'image/gif', | |
| 35 'jpeg' : 'image/jpeg', | |
| 36 'jpg' : 'image/jpeg', | |
| 37 'png' : 'image/png', | |
| 38 'tif' : 'image/tiff', | |
| 39 'tiff' : 'image/tiff', | |
| 40 'ac3' : 'audio/ac3', | |
| 41 'mp3' : 'audio/mpeg', | |
| 42 'ogg' : 'audio/ogg', | |
| 43 'css' : 'text/css', | |
| 44 'csv' : 'text/csv', | |
| 45 'htm' : 'text/html', | |
| 46 'html' : 'text/html', | |
| 47 'txt' : 'text/plain', | |
| 48 'rtf' : 'text/rtf', | |
| 49 'mp4' : 'video/mp4', | |
| 50 'qt' : 'video/quicktime', | |
| 51 'vc1' : 'video/vc1' | |
| 52 }; | |
| 53 | |
| 54 HttpTestServer(port, this.staticFileDirectory) { | |
| 55 server = new HttpServer(); | |
| 56 server.listen("127.0.0.1", port); | |
| 57 server.onError = (e) { | |
| 58 }; | |
| 59 server.defaultRequestHandler = | |
| 60 (HttpRequest request, HttpResponse response) { | |
| 61 try { | |
| 62 if (staticFileDirectory != null) { | |
| 63 String fname = request.path; | |
| 64 String path = '$staticFileDirectory$fname'; | |
| 65 File f = new File(path); | |
| 66 if (f.existsSync()) { | |
| 67 var p = path.substring(path.lastIndexOf('.') + 1).toLowerCase(); | |
| 68 if (MIME_TYPES.containsKey(p)) { | |
| 69 var ct = MIME_TYPES[p]; | |
| 70 var idx = ct.indexOf('/'); | |
| 71 response.headers.contentType = | |
| 72 new ContentType(ct.substring(0, idx), | |
| 73 ct.substring(idx + 1)); | |
| 74 } | |
| 75 f.openInputStream().pipe(response.outputStream); | |
| 76 } else { | |
| 77 response.statusCode = HttpStatus.NOT_FOUND; | |
| 78 response.reasonPhrase = '$path does not exist'; | |
| 79 response.outputStream.close(); | |
| 80 } | |
| 81 } | |
| 82 } catch(e,s) { | |
| 83 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR; | |
| 84 response.reasonPhrase = "$e"; | |
| 85 response.outputStream.writeString(s.toString()); | |
| 86 response.outputStream.close(); | |
| 87 } | |
| 88 }; | |
| 89 } | |
| 90 | |
| 91 void addHandler(Function matcher, handler) { | |
| 92 if (handler is Function) { | |
| 93 server.addRequestHandler(matcher, handler); | |
| 94 } else { | |
| 95 server.addRequestHandler(matcher, handler.onRequest); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void close() { | |
| 100 server.close(); | |
| 101 } | |
| 102 } | |
| 103 | |
|
Siggi Cherem (dart-lang)
2012/10/10 17:51:32
remove empty line
gram
2012/10/17 00:03:28
Done.
| |
| OLD | NEW |