| 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) { | |
| 54 print(error); | |
| 55 if (stack != null) print(stack); | |
| 56 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR; | |
| 57 response.write(htmlInfo("Internal Server Error", | |
| 58 "Internal Server Error: $error\n$stack")); | |
| 59 response.close(); | |
| 60 } | |
| 61 | |
| 62 handleSocket() { | 55 handleSocket() { |
| 63 if (false && request.uri.path == '/ws/watch') { | 56 if (false && request.uri.path == '/ws/watch') { |
| 64 WebSocketTransformer.upgrade(request).then((WebSocket socket) { | 57 WebSocketTransformer.upgrade(request).then((WebSocket socket) { |
| 65 socket.add(JSON.encode({'create': []})); | 58 socket.add(JSON.encode({'create': []})); |
| 66 // WatchHandler handler = new WatchHandler(socket, files); | 59 // WatchHandler handler = new WatchHandler(socket, files); |
| 67 // handlers.add(handler); | 60 // handlers.add(handler); |
| 68 // socket.listen( | 61 // socket.listen( |
| 69 // handler.onData, cancelOnError: true, onDone: handler.onDone); | 62 // handler.onData, cancelOnError: true, onDone: handler.onDone); |
| 70 }); | 63 }); |
| 71 } else { | 64 } else { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 83 | 76 |
| 84 Uri uri = request.uri; | 77 Uri uri = request.uri; |
| 85 if (uri.path.endsWith('/')) { | 78 if (uri.path.endsWith('/')) { |
| 86 uri = uri.resolve('index.html'); | 79 uri = uri.resolve('index.html'); |
| 87 } | 80 } |
| 88 if (uri.path.contains('..') || uri.path.contains('%')) { | 81 if (uri.path.contains('..') || uri.path.contains('%')) { |
| 89 return notFound(uri.path); | 82 return notFound(uri.path); |
| 90 } | 83 } |
| 91 String path = uri.path; | 84 String path = uri.path; |
| 92 Uri root = documentRoot; | 85 Uri root = documentRoot; |
| 93 String dartType = 'application/dart'; | |
| 94 if (path.startsWith('${PACKAGES_PATH}/')) { | 86 if (path.startsWith('${PACKAGES_PATH}/')) { |
| 95 root = packageRoot; | 87 root = packageRoot; |
| 96 path = path.substring(PACKAGES_PATH.length); | 88 path = path.substring(PACKAGES_PATH.length); |
| 97 } | 89 } |
| 98 | 90 |
| 99 String filePath = root.resolve('.$path').toFilePath(); | 91 Uri resolvedRequest = root.resolve('.$path'); |
| 100 switch (request.method) { | 92 switch (request.method) { |
| 101 case 'GET': | 93 case 'GET': |
| 102 return handleGet(filePath, dartType); | 94 return handleGet(resolvedRequest); |
| 103 default: | 95 default: |
| 104 String method = const HtmlEscape().convert(request.method); | 96 String method = const HtmlEscape().convert(request.method); |
| 105 return badRequest("Unsupported method: '$method'"); | 97 return badRequest("Unsupported method: '$method'"); |
| 106 } | 98 } |
| 107 } | 99 } |
| 108 | 100 |
| 109 void handleGet(String path, String dartType) { | 101 void handleGet(Uri uri) { |
| 110 var f = new File(path); | 102 String path = uri.path; |
| 103 var f = new File.fromUri(uri); |
| 111 f.exists().then((bool exists) { | 104 f.exists().then((bool exists) { |
| 112 if (!exists) return notFound(request.uri); | 105 if (!exists) { |
| 106 if (path.endsWith('.dart.js')) { |
| 107 Uri dartScript = uri.resolve(path.substring(0, path.length - 3)); |
| 108 new File.fromUri(dartScript).exists().then((bool exists) { |
| 109 if (exists) { |
| 110 compileToJavaScript(dartScript); |
| 111 } else { |
| 112 notFound(request.uri); |
| 113 } |
| 114 }); |
| 115 return; |
| 116 } |
| 117 notFound(request.uri); |
| 118 return; |
| 119 } |
| 113 if (path.endsWith('.html')) { | 120 if (path.endsWith('.html')) { |
| 114 response.headers.set(CONTENT_TYPE, 'text/html'); | 121 response.headers.set(CONTENT_TYPE, 'text/html'); |
| 115 } else if (path.endsWith('.dart')) { | 122 } else if (path.endsWith('.dart')) { |
| 116 response.headers.set(CONTENT_TYPE, dartType); | 123 response.headers.set(CONTENT_TYPE, 'application/dart'); |
| 117 } else if (path.endsWith('.js')) { | 124 } else if (path.endsWith('.js')) { |
| 118 response.headers.set(CONTENT_TYPE, 'application/javascript'); | 125 response.headers.set(CONTENT_TYPE, 'application/javascript'); |
| 119 } else if (path.endsWith('.ico')) { | 126 } else if (path.endsWith('.ico')) { |
| 120 response.headers.set(CONTENT_TYPE, 'image/x-icon'); | 127 response.headers.set(CONTENT_TYPE, 'image/x-icon'); |
| 121 } else if (path.endsWith('.appcache')) { | 128 } else if (path.endsWith('.appcache')) { |
| 122 response.headers.set(CONTENT_TYPE, 'text/cache-manifest'); | 129 response.headers.set(CONTENT_TYPE, 'text/cache-manifest'); |
| 123 } | 130 } |
| 124 f.openRead().pipe(response).catchError(onError); | 131 f.openRead().pipe(response).catchError(onError); |
| 125 }); | 132 }); |
| 126 } | 133 } |
| 127 | 134 |
| 135 void compileToJavaScript(Uri dartScript) { |
| 136 Uri outputUri = request.uri; |
| 137 print("Compiling $dartScript to $outputUri"); |
| 138 // TODO(ahe): Implement this. |
| 139 notFound(request.uri); |
| 140 } |
| 141 |
| 128 static onRequest(HttpRequest request) { | 142 static onRequest(HttpRequest request) { |
| 129 Conversation conversation = new Conversation(request, request.response); | 143 Conversation conversation = new Conversation(request, request.response); |
| 130 if (WebSocketTransformer.isUpgradeRequest(request)) { | 144 if (WebSocketTransformer.isUpgradeRequest(request)) { |
| 131 conversation.handleSocket(); | 145 conversation.handleSocket(); |
| 132 } else { | 146 } else { |
| 133 conversation.handle(); | 147 conversation.handle(); |
| 134 } | 148 } |
| 135 } | 149 } |
| 136 | 150 |
| 137 static onError(error) { | 151 static onError(error) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 String host = options.host; | 187 String host = options.host; |
| 174 int port = options.port; | 188 int port = options.port; |
| 175 HttpServer.bind(host, port).then((HttpServer server) { | 189 HttpServer.bind(host, port).then((HttpServer server) { |
| 176 print('HTTP server started on http://$host:${server.port}/'); | 190 print('HTTP server started on http://$host:${server.port}/'); |
| 177 server.listen(Conversation.onRequest, onError: Conversation.onError); | 191 server.listen(Conversation.onRequest, onError: Conversation.onError); |
| 178 }).catchError((e) { | 192 }).catchError((e) { |
| 179 print("HttpServer.bind error: $e"); | 193 print("HttpServer.bind error: $e"); |
| 180 exit(1); | 194 exit(1); |
| 181 }); | 195 }); |
| 182 } | 196 } |
| OLD | NEW |