| 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 List<Function> matchers = []; | |
| 25 List<Function> handlers = []; | |
| 26 | |
| 27 /** If set, serve up static files from this directory. */ | |
| 28 String staticFileDirectory; | |
| 29 | |
| 30 /* A common subset of all possible MIME types. */ | |
| 31 static const MIME_TYPES = const { | |
| 32 'json' : 'applicaton/json', | |
| 33 'js' : 'application/javascript', | |
| 34 'cgm' : 'image/cgm', | |
| 35 'g3fax': 'image/g3fax', | |
| 36 'gif' : 'image/gif', | |
| 37 'jpeg' : 'image/jpeg', | |
| 38 'jpg' : 'image/jpeg', | |
| 39 'png' : 'image/png', | |
| 40 'tif' : 'image/tiff', | |
| 41 'tiff' : 'image/tiff', | |
| 42 'ac3' : 'audio/ac3', | |
| 43 'mp3' : 'audio/mpeg', | |
| 44 'ogg' : 'audio/ogg', | |
| 45 'css' : 'text/css', | |
| 46 'csv' : 'text/csv', | |
| 47 'htm' : 'text/html', | |
| 48 'html' : 'text/html', | |
| 49 'txt' : 'text/plain', | |
| 50 'rtf' : 'text/rtf', | |
| 51 'mp4' : 'video/mp4', | |
| 52 'qt' : 'video/quicktime', | |
| 53 'vc1' : 'video/vc1' | |
| 54 }; | |
| 55 | |
| 56 HttpTestServer(int port, this.staticFileDirectory) { | |
| 57 HttpServer.bind("127.0.0.1", port).then((s) { | |
| 58 server = s; | |
| 59 print('Server listening on port $port'); | |
| 60 server.listen((HttpRequest request) { | |
| 61 for (var i = 0; i < matchers.length; i++) { | |
| 62 if (matchers[i](request)) { | |
| 63 handlers[i](request); | |
| 64 return; | |
| 65 } | |
| 66 } | |
| 67 HttpResponse response = request.response; | |
| 68 try { | |
| 69 if (staticFileDirectory != null) { | |
| 70 String fname = request.uri.path; | |
| 71 String path = '$staticFileDirectory$fname'; | |
| 72 File f = new File(path); | |
| 73 if (f.existsSync()) { | |
| 74 var p = path.substring(path.lastIndexOf('.') + 1).toLowerCase(); | |
| 75 if (MIME_TYPES.containsKey(p)) { | |
| 76 var ct = MIME_TYPES[p]; | |
| 77 var idx = ct.indexOf('/'); | |
| 78 response.headers.contentType = | |
| 79 new ContentType(ct.substring(0, idx), | |
| 80 ct.substring(idx + 1)); | |
| 81 } | |
| 82 response.addStream(f.openRead()).then((_) => response.close()); | |
| 83 } else { | |
| 84 response.statusCode = HttpStatus.NOT_FOUND; | |
| 85 response.reasonPhrase = '$path does not exist'; | |
| 86 response.close(); | |
| 87 } | |
| 88 } | |
| 89 } catch(e,s) { | |
| 90 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR; | |
| 91 response.reasonPhrase = "$e"; | |
| 92 response.write(s); | |
| 93 response.close(); | |
| 94 } | |
| 95 }); | |
| 96 }); | |
| 97 } | |
| 98 | |
| 99 void addHandler(Function matcher, Function handler) { | |
| 100 matchers.add(matcher); | |
| 101 handlers.add(handler); | |
| 102 } | |
| 103 | |
| 104 void close() { | |
| 105 server.close(); | |
| 106 } | |
| 107 } | |
| 108 | |
| OLD | NEW |