OLD | NEW |
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 #import('dart:io'); | 5 #import('dart:io'); |
6 | 6 |
7 class Conversation { | 7 class Conversation { |
8 HttpRequest request; | 8 HttpRequest request; |
9 HttpResponse response; | 9 HttpResponse response; |
10 | 10 |
(...skipping 25 matching lines...) Expand all Loading... |
36 } | 36 } |
37 | 37 |
38 handle() { | 38 handle() { |
39 String path = request.path; | 39 String path = request.path; |
40 if (path == '/') return redirect('/$landingPage'); | 40 if (path == '/') return redirect('/$landingPage'); |
41 if (path == '/favicon.ico') { | 41 if (path == '/favicon.ico') { |
42 path = '/lib/dartdoc/static/favicon.ico'; | 42 path = '/lib/dartdoc/static/favicon.ico'; |
43 } | 43 } |
44 if (path.contains('..') || path.contains('%')) return notFound(); | 44 if (path.contains('..') || path.contains('%')) return notFound(); |
45 var f = new File("./$path"); | 45 var f = new File("./$path"); |
46 f.exists((bool exists) { | 46 f.exists().then((bool exists) { |
47 if (!exists) return notFound(); | 47 if (!exists) return notFound(); |
48 if (path.endsWith('.dart')) { | 48 if (path.endsWith('.dart')) { |
49 response.headers.add(HttpHeaders.CONTENT_TYPE, "application/dart"); | 49 response.headers.add(HttpHeaders.CONTENT_TYPE, "application/dart"); |
50 } else if (path.endsWith('.ico')) { | 50 } else if (path.endsWith('.ico')) { |
51 response.headers.add(HttpHeaders.CONTENT_TYPE, "image/x-icon"); | 51 response.headers.add(HttpHeaders.CONTENT_TYPE, "image/x-icon"); |
52 } | 52 } |
53 f.openInputStream().pipe(response.outputStream); | 53 f.openInputStream().pipe(response.outputStream); |
54 onClosed(); | 54 onClosed(); |
55 }); | 55 }); |
56 } | 56 } |
57 | 57 |
58 static onRequest(HttpRequest request, HttpResponse response) { | 58 static onRequest(HttpRequest request, HttpResponse response) { |
59 new Conversation(request, response).handle(); | 59 new Conversation(request, response).handle(); |
60 } | 60 } |
61 } | 61 } |
62 | 62 |
63 main() { | 63 main() { |
64 List<String> arguments = new Options().arguments; | 64 List<String> arguments = new Options().arguments; |
65 if (arguments.length > 0) { | 65 if (arguments.length > 0) { |
66 Conversation.landingPage = arguments[0]; | 66 Conversation.landingPage = arguments[0]; |
67 } | 67 } |
68 var server = new HttpServer(); | 68 var server = new HttpServer(); |
69 var host = '127.0.0.1'; | 69 var host = '127.0.0.1'; |
70 var port = 8080; | 70 var port = 8080; |
71 server.listen(host, port); | 71 server.listen(host, port); |
72 print('HTTP server started on http://$host:$port/'); | 72 print('HTTP server started on http://$host:$port/'); |
73 server.defaultRequestHandler = Conversation.onRequest; | 73 server.defaultRequestHandler = Conversation.onRequest; |
74 } | 74 } |
OLD | NEW |