Chromium Code Reviews| 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 library dart2js_incremental.server; | 5 library dart2js_incremental.server; |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 | 8 |
| 9 import 'dart:async' show | 9 import 'dart:async' show |
| 10 Future, | 10 Future, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 | 46 |
| 47 Future badRequest(String problem) { | 47 Future badRequest(String problem) { |
| 48 response | 48 response |
| 49 ..headers.set(CONTENT_TYPE, 'text/html') | 49 ..headers.set(CONTENT_TYPE, 'text/html') |
| 50 ..statusCode = HttpStatus.BAD_REQUEST | 50 ..statusCode = HttpStatus.BAD_REQUEST |
| 51 ..write( | 51 ..write( |
| 52 htmlInfo("Bad request", "Bad request '${request.uri}': $problem")); | 52 htmlInfo("Bad request", "Bad request '${request.uri}': $problem")); |
| 53 return response.close(); | 53 return response.close(); |
| 54 } | 54 } |
| 55 | 55 |
| 56 Future handleSocket() { | 56 Future handleSocket() async { |
| 57 if (false && request.uri.path == '/ws/watch') { | 57 if (false && request.uri.path == '/ws/watch') { |
| 58 return WebSocketTransformer.upgrade(request).then((WebSocket socket) { | 58 WebSocket socket = await WebSocketTransformer.upgrade(request); |
| 59 socket.add(JSON.encode({'create': []})); | 59 socket.add(JSON.encode({'create': []})); |
| 60 // WatchHandler handler = new WatchHandler(socket, files); | 60 // WatchHandler handler = new WatchHandler(socket, files); |
| 61 // handlers.add(handler); | 61 // handlers.add(handler); |
| 62 // socket.listen( | 62 // socket.listen( |
| 63 // handler.onData, cancelOnError: true, onDone: handler.onDone); | 63 // handler.onData, cancelOnError: true, onDone: handler.onDone); |
| 64 }); | |
| 65 } else { | 64 } else { |
| 66 response.done | 65 response.done |
| 67 .then(onClosed) | 66 .then(onClosed) |
| 68 .catchError(onError); | 67 .catchError(onError); |
| 69 return notFound(request.uri); | 68 return await notFound(request.uri); |
|
kasperl
2015/01/19 14:22:59
I don't think you have to await here. The futures
ahe
2015/01/19 14:52:37
(We discussed this in person, attempting to summar
| |
| 70 } | 69 } |
| 71 } | 70 } |
| 72 | 71 |
| 73 Future handle() { | 72 Future handle() { |
| 74 response.done | 73 response.done |
| 75 .then(onClosed) | 74 .then(onClosed) |
| 76 .catchError(onError); | 75 .catchError(onError); |
| 77 | 76 |
| 78 Uri uri = request.uri; | 77 Uri uri = request.uri; |
| 79 if (uri.path.endsWith('/')) { | 78 if (uri.path.endsWith('/')) { |
| 80 uri = uri.resolve('index.html'); | 79 uri = uri.resolve('index.html'); |
| 81 } | 80 } |
| 82 if (uri.path.contains('..') || uri.path.contains('%')) { | 81 if (uri.path.contains('..') || uri.path.contains('%')) { |
| 83 return notFound(uri); | 82 return notFound(uri); |
| 84 } | 83 } |
| 85 String path = uri.path; | 84 String path = uri.path; |
| 86 Uri root = documentRoot; | 85 Uri root = documentRoot; |
| 87 if (path.startsWith('${PACKAGES_PATH}/')) { | 86 if (path.startsWith('${PACKAGES_PATH}/')) { |
| 88 root = packageRoot; | 87 root = packageRoot; |
| 89 path = path.substring(PACKAGES_PATH.length); | 88 path = path.substring(PACKAGES_PATH.length); |
| 90 } | 89 } |
| 91 | 90 |
| 92 Uri resolvedRequest = root.resolve('.$path'); | 91 Uri resolvedRequest = root.resolve('.$path'); |
| 93 switch (request.method) { | 92 switch (request.method) { |
| 94 case 'GET': | 93 case 'GET': |
| 95 return handleGet(resolvedRequest); | 94 return handleGet(resolvedRequest); |
| 96 default: | 95 default: |
| 97 String method = const HtmlEscape().convert(request.method); | 96 String method = const HtmlEscape().convert(request.method); |
| 98 return badRequest("Unsupported method: '$method'"); | 97 return badRequest("Unsupported method: '$method'"); |
| 99 } | 98 } |
| 100 } | 99 } |
| 101 | 100 |
| 102 Future handleGet(Uri uri) { | 101 Future handleGet(Uri uri) async { |
| 103 String path = uri.path; | 102 String path = uri.path; |
| 104 var f = new File.fromUri(uri); | 103 var f = new File.fromUri(uri); |
| 105 return f.exists().then((bool exists) { | 104 if (!await f.exists()) { |
| 106 if (!exists) { | 105 if (path.endsWith('.dart.js')) { |
| 107 if (path.endsWith('.dart.js')) { | 106 Uri dartScript = uri.resolve(path.substring(0, path.length - 3)); |
| 108 Uri dartScript = uri.resolve(path.substring(0, path.length - 3)); | 107 if (await new File.fromUri(dartScript).exists()) { |
| 109 return new File.fromUri(dartScript).exists().then((bool exists) { | 108 return await compileToJavaScript(dartScript); |
| 110 if (exists) { | |
| 111 return compileToJavaScript(dartScript); | |
| 112 } else { | |
| 113 return notFound(request.uri); | |
| 114 } | |
| 115 }); | |
| 116 } | 109 } |
| 117 return notFound(request.uri); | |
| 118 } | 110 } |
| 119 if (path.endsWith('.html')) { | 111 return await notFound(request.uri); |
| 120 response.headers.set(CONTENT_TYPE, 'text/html'); | 112 } else if (path.endsWith('.html')) { |
| 121 } else if (path.endsWith('.dart')) { | 113 response.headers.set(CONTENT_TYPE, 'text/html'); |
| 122 response.headers.set(CONTENT_TYPE, 'application/dart'); | 114 } else if (path.endsWith('.dart')) { |
| 123 } else if (path.endsWith('.js')) { | 115 response.headers.set(CONTENT_TYPE, 'application/dart'); |
| 124 response.headers.set(CONTENT_TYPE, 'application/javascript'); | 116 } else if (path.endsWith('.js')) { |
| 125 } else if (path.endsWith('.ico')) { | 117 response.headers.set(CONTENT_TYPE, 'application/javascript'); |
| 126 response.headers.set(CONTENT_TYPE, 'image/x-icon'); | 118 } else if (path.endsWith('.ico')) { |
| 127 } else if (path.endsWith('.appcache')) { | 119 response.headers.set(CONTENT_TYPE, 'image/x-icon'); |
| 128 response.headers.set(CONTENT_TYPE, 'text/cache-manifest'); | 120 } else if (path.endsWith('.appcache')) { |
| 129 } | 121 response.headers.set(CONTENT_TYPE, 'text/cache-manifest'); |
| 130 return f.openRead().pipe(response); | 122 } |
| 131 }); | 123 return await f.openRead().pipe(response); |
| 132 } | 124 } |
| 133 | 125 |
| 134 Future compileToJavaScript(Uri dartScript) { | 126 Future compileToJavaScript(Uri dartScript) { |
| 135 Uri outputUri = request.uri; | 127 Uri outputUri = request.uri; |
| 136 print("Compiling $dartScript to $outputUri"); | 128 print("Compiling $dartScript to $outputUri"); |
| 137 // TODO(ahe): Implement this. | 129 // TODO(ahe): Implement this. |
| 138 throw new UnimplementedError("compileToJavaScript"); | 130 throw new UnimplementedError("compileToJavaScript"); |
| 139 return notFound(request.uri); | 131 return notFound(request.uri); |
| 140 } | 132 } |
| 141 | 133 |
| 142 Future dispatch() { | 134 Future dispatch() async { |
| 143 return new Future.sync(() { | 135 try { |
| 144 return WebSocketTransformer.isUpgradeRequest(request) | 136 return await WebSocketTransformer.isUpgradeRequest(request) |
|
kasperl
2015/01/19 14:22:59
Here you definitely need the await.
ahe
2015/01/19 14:52:37
Acknowledged.
| |
| 145 ? handleSocket() | 137 ? handleSocket() |
| 146 : handle(); | 138 : handle(); |
| 147 }).catchError(onError); | 139 } catch (e, s) { |
| 140 onError(e, s); | |
| 141 } | |
| 148 } | 142 } |
| 149 | 143 |
| 150 static Future onRequest(HttpRequest request) { | 144 static Future onRequest(HttpRequest request) async { |
| 151 HttpResponse response = request.response; | 145 HttpResponse response = request.response; |
| 152 return | 146 try { |
| 153 new Future.sync(() => new Conversation(request, response).dispatch()) | 147 return await new Conversation(request, response).dispatch(); |
| 154 .catchError((error, [stack]) { | 148 } catch (e, s) { |
| 155 onStaticError(error, stack); | 149 try { |
| 156 return | 150 onStaticError(e, s); |
| 157 new Future.sync(() => response.close()).catchError(onStaticError); | 151 return await response.close(); |
| 158 }); | 152 } catch (e, s) { |
| 153 onStaticError(e, s); | |
| 154 } | |
| 155 } | |
| 159 } | 156 } |
| 160 | 157 |
| 161 void onError(error, [stack]) { | 158 Future onError(error, [stack]) async { |
| 162 onStaticError(error, stack); | 159 try { |
| 163 new Future.sync(() => response.close()).catchError(onStaticError); | 160 onStaticError(error, stack); |
| 161 return await response.close(); | |
| 162 } catch (e, s) { | |
| 163 onStaticError(e, s); | |
| 164 } | |
| 164 } | 165 } |
| 165 | 166 |
| 166 static void onStaticError(error, [stack]) { | 167 static void onStaticError(error, [stack]) { |
| 167 if (error is HttpException) { | 168 if (error is HttpException) { |
| 168 print('Error: ${error.message}'); | 169 print('Error: ${error.message}'); |
| 169 } else { | 170 } else { |
| 170 print('Error: ${error}'); | 171 print('Error: ${error}'); |
| 171 } | 172 } |
| 172 if (stack != null) { | 173 if (stack != null) { |
| 173 print(stack); | 174 print(stack); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 186 </head> | 187 </head> |
| 187 <body> | 188 <body> |
| 188 <h1>$title</h1> | 189 <h1>$title</h1> |
| 189 <p style='white-space:pre'>$text</p> | 190 <p style='white-space:pre'>$text</p> |
| 190 </body> | 191 </body> |
| 191 </html> | 192 </html> |
| 192 """; | 193 """; |
| 193 } | 194 } |
| 194 } | 195 } |
| 195 | 196 |
| 196 main(List<String> arguments) { | 197 main(List<String> arguments) async { |
| 197 Options options = Options.parse(arguments); | 198 Options options = Options.parse(arguments); |
| 198 if (options == null) { | 199 if (options == null) { |
| 199 exit(1); | 200 exit(1); |
| 200 } | 201 } |
| 201 if (!options.arguments.isEmpty) { | 202 if (!options.arguments.isEmpty) { |
| 202 Conversation.documentRoot = Uri.base.resolve(options.arguments.single); | 203 Conversation.documentRoot = Uri.base.resolve(options.arguments.single); |
| 203 } | 204 } |
| 204 Conversation.packageRoot = options.packageRoot; | 205 Conversation.packageRoot = options.packageRoot; |
| 205 String host = options.host; | 206 String host = options.host; |
| 206 int port = options.port; | 207 int port = options.port; |
| 207 HttpServer.bind(host, port).then((HttpServer server) { | 208 try { |
| 209 HttpServer server = await HttpServer.bind(host, port); | |
| 208 print('HTTP server started on http://$host:${server.port}/'); | 210 print('HTTP server started on http://$host:${server.port}/'); |
| 209 server.listen(Conversation.onRequest, onError: Conversation.onStaticError); | 211 server.listen(Conversation.onRequest, onError: Conversation.onStaticError); |
| 210 }).catchError((e) { | 212 } catch (e) { |
| 211 print("HttpServer.bind error: $e"); | 213 print("HttpServer.bind error: $e"); |
| 212 exit(1); | 214 exit(1); |
| 213 }); | 215 }; |
| 214 } | 216 } |
| OLD | NEW |