Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1119)

Side by Side Diff: utils/testrunner/http_server.dart

Issue 14247033: Updated testrunner: (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 http_server; 5 library http_server;
6 import 'dart:io'; 6 import 'dart:io';
7 import 'package:args/args.dart'; 7 import 'package:args/args.dart';
8 8
9 /** An options parser for the server. */ 9 /** An options parser for the server. */
10 ArgParser getOptionParser() { 10 ArgParser getOptionParser() {
11 var parser = new ArgParser(); 11 var parser = new ArgParser();
12 parser.addOption('port', abbr: 'p', 12 parser.addOption('port', abbr: 'p',
13 help: 'Set the server listening port.', 13 help: 'Set the server listening port.',
14 defaultsTo: '80'); 14 defaultsTo: '80');
15 15
16 parser.addOption('root', abbr: 'r', 16 parser.addOption('root', abbr: 'r',
17 help: 'Set the directory for static files.'); 17 help: 'Set the directory for static files.');
18 return parser; 18 return parser;
19 } 19 }
20 20
21 /** A simple HTTP server. Currently handles serving static files. */ 21 /** A simple HTTP server. Currently handles serving static files. */
22 class HttpTestServer { 22 class HttpTestServer {
23 HttpServer server; 23 HttpServer server;
24 List<Function> matchers = [];
25 List<Function> handlers = [];
24 26
25 /** If set, serve up static files from this directory. */ 27 /** If set, serve up static files from this directory. */
26 String staticFileDirectory; 28 String staticFileDirectory;
27 29
28 /* A common subset of all possible MIME types. */ 30 /* A common subset of all possible MIME types. */
29 static const MIME_TYPES = const { 31 static const MIME_TYPES = const {
30 'json' : 'applicaton/json', 32 'json' : 'applicaton/json',
31 'js' : 'application/javascript', 33 'js' : 'application/javascript',
32 'cgm' : 'image/cgm', 34 'cgm' : 'image/cgm',
33 'g3fax': 'image/g3fax', 35 'g3fax': 'image/g3fax',
(...skipping 11 matching lines...) Expand all
45 'htm' : 'text/html', 47 'htm' : 'text/html',
46 'html' : 'text/html', 48 'html' : 'text/html',
47 'txt' : 'text/plain', 49 'txt' : 'text/plain',
48 'rtf' : 'text/rtf', 50 'rtf' : 'text/rtf',
49 'mp4' : 'video/mp4', 51 'mp4' : 'video/mp4',
50 'qt' : 'video/quicktime', 52 'qt' : 'video/quicktime',
51 'vc1' : 'video/vc1' 53 'vc1' : 'video/vc1'
52 }; 54 };
53 55
54 HttpTestServer(int port, this.staticFileDirectory) { 56 HttpTestServer(int port, this.staticFileDirectory) {
55 server = new HttpServer(); 57 HttpServer.bind("127.0.0.1", port).then((s) {
56 try { 58 server = s;
57 server.listen("127.0.0.1", port);
58 print('Server listening on port $port'); 59 print('Server listening on port $port');
59 } catch (e) { 60 server.listen((HttpRequest request) {
60 print('Server listen on port $port failed'); 61 for (var i = 0; i < matchers.length; i++) {
61 throw e; 62 if (matchers[i](request)) {
62 } 63 handlers[i](request);
63 server.onError = (e) { 64 return;
64 }; 65 }
65 server.defaultRequestHandler = 66 }
66 (HttpRequest request, HttpResponse response) { 67 HttpResponse response = request.response;
67 try { 68 try {
68 if (staticFileDirectory != null) { 69 if (staticFileDirectory != null) {
69 String fname = request.path; 70 String fname = request.uri.path;
70 String path = '$staticFileDirectory$fname'; 71 String path = '$staticFileDirectory$fname';
71 File f = new File(path); 72 File f = new File(path);
72 if (f.existsSync()) { 73 if (f.existsSync()) {
73 var p = path.substring(path.lastIndexOf('.') + 1).toLowerCase(); 74 var p = path.substring(path.lastIndexOf('.') + 1).toLowerCase();
74 if (MIME_TYPES.containsKey(p)) { 75 if (MIME_TYPES.containsKey(p)) {
75 var ct = MIME_TYPES[p]; 76 var ct = MIME_TYPES[p];
76 var idx = ct.indexOf('/'); 77 var idx = ct.indexOf('/');
77 response.headers.contentType = 78 response.headers.contentType =
78 new ContentType(ct.substring(0, idx), 79 new ContentType(ct.substring(0, idx),
79 ct.substring(idx + 1)); 80 ct.substring(idx + 1));
80 } 81 }
81 f.openInputStream().pipe(response.outputStream); 82 response.addStream(f.openRead()).then((_) => response.close());
82 } else { 83 } else {
83 response.statusCode = HttpStatus.NOT_FOUND; 84 response.statusCode = HttpStatus.NOT_FOUND;
84 response.reasonPhrase = '$path does not exist'; 85 response.reasonPhrase = '$path does not exist';
85 response.outputStream.close(); 86 response.close();
86 } 87 }
87 } 88 }
88 } catch(e,s) { 89 } catch(e,s) {
89 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR; 90 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
90 response.reasonPhrase = "$e"; 91 response.reasonPhrase = "$e";
91 response.outputStream.writeString(s.toString()); 92 response.write(s);
92 response.outputStream.close(); 93 response.close();
93 } 94 }
94 }; 95 });
96 });
95 } 97 }
96 98
97 void addHandler(Function matcher, handler) { 99 void addHandler(Function matcher, Function handler) {
98 if (handler is Function) { 100 matchers.add(matcher);
99 server.addRequestHandler(matcher, handler); 101 handlers.add(handler);
100 } else {
101 server.addRequestHandler(matcher, handler.onRequest);
102 }
103 } 102 }
104 103
105 void close() { 104 void close() {
106 server.close(); 105 server.close();
107 } 106 }
108 } 107 }
108
OLDNEW
« no previous file with comments | « no previous file | utils/testrunner/layout_test_controller.dart » ('j') | utils/testrunner/pipeline_utils.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698