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 19 matching lines...) Expand all Loading... | |
| 30 static Uri packageRoot = Uri.base.resolve('packages/'); | 30 static Uri packageRoot = Uri.base.resolve('packages/'); |
| 31 | 31 |
| 32 Conversation(this.request, this.response); | 32 Conversation(this.request, this.response); |
| 33 | 33 |
| 34 onClosed(_) { | 34 onClosed(_) { |
| 35 if (response.statusCode == HttpStatus.OK) return; | 35 if (response.statusCode == HttpStatus.OK) return; |
| 36 print('Request for ${request.uri} ${response.statusCode}'); | 36 print('Request for ${request.uri} ${response.statusCode}'); |
| 37 } | 37 } |
| 38 | 38 |
| 39 notFound(path) { | 39 notFound(path) { |
| 40 response.headers.set(CONTENT_TYPE, 'text/html'); | |
| 40 response.statusCode = HttpStatus.NOT_FOUND; | 41 response.statusCode = HttpStatus.NOT_FOUND; |
| 41 response.write(htmlInfo('Not Found', | 42 response.write(htmlInfo('Not Found', |
| 42 'The file "$path" could not be found.')); | 43 'The file "$path" could not be found.')); |
| 43 response.close(); | 44 response.close(); |
| 44 } | 45 } |
| 45 | 46 |
| 46 badRequest(String problem) { | 47 badRequest(String problem) { |
| 48 response.headers.set(CONTENT_TYPE, 'text/html'); | |
| 47 response.statusCode = HttpStatus.BAD_REQUEST; | 49 response.statusCode = HttpStatus.BAD_REQUEST; |
| 48 response.write(htmlInfo("Bad request", | 50 response.write(htmlInfo("Bad request", |
| 49 "Bad request '${request.uri}': $problem")); | 51 "Bad request '${request.uri}': $problem")); |
| 50 response.close(); | 52 response.close(); |
| 51 } | 53 } |
| 52 | 54 |
| 53 internalError(error, stack) { | 55 internalError(error, stack) { |
| 54 print(error); | 56 print(error); |
| 55 if (stack != null) print(stack); | 57 if (stack != null) print(stack); |
| 58 try { | |
| 59 response.headers.set(CONTENT_TYPE, 'text/html'); | |
| 60 } catch (_) { | |
| 61 // Ignored. | |
|
kasperl
2015/01/19 11:21:04
What kind of exception can happen here and why are
ahe
2015/01/19 11:37:12
The kind of exception isn't specified. All that is
| |
| 62 } | |
| 56 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR; | 63 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR; |
| 57 response.write(htmlInfo("Internal Server Error", | 64 response.write(htmlInfo("Internal Server Error", |
| 58 "Internal Server Error: $error\n$stack")); | 65 "Internal Server Error: $error\n$stack")); |
| 59 response.close(); | 66 response.close(); |
| 60 } | 67 } |
| 61 | 68 |
| 62 handleSocket() { | 69 handleSocket() { |
| 63 if (false && request.uri.path == '/ws/watch') { | 70 if (false && request.uri.path == '/ws/watch') { |
| 64 WebSocketTransformer.upgrade(request).then((WebSocket socket) { | 71 WebSocketTransformer.upgrade(request).then((WebSocket socket) { |
| 65 socket.add(JSON.encode({'create': []})); | 72 socket.add(JSON.encode({'create': []})); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 83 | 90 |
| 84 Uri uri = request.uri; | 91 Uri uri = request.uri; |
| 85 if (uri.path.endsWith('/')) { | 92 if (uri.path.endsWith('/')) { |
| 86 uri = uri.resolve('index.html'); | 93 uri = uri.resolve('index.html'); |
| 87 } | 94 } |
| 88 if (uri.path.contains('..') || uri.path.contains('%')) { | 95 if (uri.path.contains('..') || uri.path.contains('%')) { |
| 89 return notFound(uri.path); | 96 return notFound(uri.path); |
| 90 } | 97 } |
| 91 String path = uri.path; | 98 String path = uri.path; |
| 92 Uri root = documentRoot; | 99 Uri root = documentRoot; |
| 93 String dartType = 'application/dart'; | |
| 94 if (path.startsWith('${PACKAGES_PATH}/')) { | 100 if (path.startsWith('${PACKAGES_PATH}/')) { |
| 95 root = packageRoot; | 101 root = packageRoot; |
| 96 path = path.substring(PACKAGES_PATH.length); | 102 path = path.substring(PACKAGES_PATH.length); |
| 97 } | 103 } |
| 98 | 104 |
| 99 String filePath = root.resolve('.$path').toFilePath(); | 105 Uri resolvedRequest = root.resolve('.$path'); |
| 100 switch (request.method) { | 106 switch (request.method) { |
| 101 case 'GET': | 107 case 'GET': |
| 102 return handleGet(filePath, dartType); | 108 return handleGet(resolvedRequest); |
| 103 default: | 109 default: |
| 104 String method = const HtmlEscape().convert(request.method); | 110 String method = const HtmlEscape().convert(request.method); |
| 105 return badRequest("Unsupported method: '$method'"); | 111 return badRequest("Unsupported method: '$method'"); |
| 106 } | 112 } |
| 107 } | 113 } |
| 108 | 114 |
| 109 void handleGet(String path, String dartType) { | 115 void handleGet(Uri uri) { |
| 110 var f = new File(path); | 116 String path = uri.path; |
| 117 var f = new File.fromUri(uri); | |
| 111 f.exists().then((bool exists) { | 118 f.exists().then((bool exists) { |
| 112 if (!exists) return notFound(request.uri); | 119 if (!exists) { |
| 120 if (path.endsWith('.dart.js')) { | |
| 121 Uri dartScript = uri.resolve(path.substring(0, path.length - 3)); | |
| 122 new File.fromUri(dartScript).exists().then((bool exists) { | |
| 123 if (exists) { | |
| 124 compileToJavaScript(dartScript); | |
| 125 } else { | |
| 126 notFound(request.uri); | |
| 127 } | |
| 128 }); | |
| 129 return; | |
| 130 } | |
| 131 notFound(request.uri); | |
| 132 return; | |
| 133 } | |
| 113 if (path.endsWith('.html')) { | 134 if (path.endsWith('.html')) { |
| 114 response.headers.set(CONTENT_TYPE, 'text/html'); | 135 response.headers.set(CONTENT_TYPE, 'text/html'); |
| 115 } else if (path.endsWith('.dart')) { | 136 } else if (path.endsWith('.dart')) { |
| 116 response.headers.set(CONTENT_TYPE, dartType); | 137 response.headers.set(CONTENT_TYPE, 'application/dart'); |
| 117 } else if (path.endsWith('.js')) { | 138 } else if (path.endsWith('.js')) { |
| 118 response.headers.set(CONTENT_TYPE, 'application/javascript'); | 139 response.headers.set(CONTENT_TYPE, 'application/javascript'); |
| 119 } else if (path.endsWith('.ico')) { | 140 } else if (path.endsWith('.ico')) { |
| 120 response.headers.set(CONTENT_TYPE, 'image/x-icon'); | 141 response.headers.set(CONTENT_TYPE, 'image/x-icon'); |
| 121 } else if (path.endsWith('.appcache')) { | 142 } else if (path.endsWith('.appcache')) { |
| 122 response.headers.set(CONTENT_TYPE, 'text/cache-manifest'); | 143 response.headers.set(CONTENT_TYPE, 'text/cache-manifest'); |
| 123 } | 144 } |
| 124 f.openRead().pipe(response).catchError(onError); | 145 f.openRead().pipe(response).catchError(onError); |
| 125 }); | 146 }); |
| 126 } | 147 } |
| 127 | 148 |
| 149 void compileToJavaScript(Uri dartScript) { | |
| 150 Uri outputUri = request.uri; | |
| 151 print("Compiling $dartScript to $outputUri"); | |
| 152 // TODO(ahe): Implement this. | |
| 153 notFound(request.uri); | |
| 154 } | |
| 155 | |
| 128 static onRequest(HttpRequest request) { | 156 static onRequest(HttpRequest request) { |
| 129 Conversation conversation = new Conversation(request, request.response); | 157 Conversation conversation = new Conversation(request, request.response); |
| 130 if (WebSocketTransformer.isUpgradeRequest(request)) { | 158 if (WebSocketTransformer.isUpgradeRequest(request)) { |
| 131 conversation.handleSocket(); | 159 conversation.handleSocket(); |
| 132 } else { | 160 } else { |
| 133 conversation.handle(); | 161 conversation.handle(); |
| 134 } | 162 } |
| 135 } | 163 } |
| 136 | 164 |
| 137 static onError(error) { | 165 static onError(error) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 String host = options.host; | 201 String host = options.host; |
| 174 int port = options.port; | 202 int port = options.port; |
| 175 HttpServer.bind(host, port).then((HttpServer server) { | 203 HttpServer.bind(host, port).then((HttpServer server) { |
| 176 print('HTTP server started on http://$host:${server.port}/'); | 204 print('HTTP server started on http://$host:${server.port}/'); |
| 177 server.listen(Conversation.onRequest, onError: Conversation.onError); | 205 server.listen(Conversation.onRequest, onError: Conversation.onError); |
| 178 }).catchError((e) { | 206 }).catchError((e) { |
| 179 print("HttpServer.bind error: $e"); | 207 print("HttpServer.bind error: $e"); |
| 180 exit(1); | 208 exit(1); |
| 181 }); | 209 }); |
| 182 } | 210 } |
| OLD | NEW |