OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 part of vmservice; |
| 6 |
| 7 class Server { |
| 8 int port; |
| 9 static ContentType jsonContentType = ContentType.parse('application/json'); |
| 10 final VmService service; |
| 11 HttpServer _server; |
| 12 |
| 13 Server(this.service, this.port); |
| 14 |
| 15 void _requestHandler(HttpRequest request) { |
| 16 // Allow cross origin requests. |
| 17 request.response.headers.add('Access-Control-Allow-Origin', '*'); |
| 18 |
| 19 final String path = |
| 20 request.uri.path == '/' ? '/index.html' : request.uri.path; |
| 21 |
| 22 var resource = Resource.resources[path]; |
| 23 if (resource != null) { |
| 24 // Serving up a static resource (e.g. .css, .html, .png). |
| 25 request.response.headers.contentType = ContentType.parse(mimeType); |
| 26 request.response.add(resource.data); |
| 27 request.response.close(); |
| 28 return; |
| 29 } |
| 30 |
| 31 var serviceRequest = new ServiceRequest(); |
| 32 var r = serviceRequest.parse(request.uri); |
| 33 if (!r) { |
| 34 // Did not understand the request uri. |
| 35 serviceRequest.setErrorResponse('Invalid request uri: ${request.uri}'); |
| 36 } else { |
| 37 r = service.runningIsolates.route(serviceRequest); |
| 38 if (!r) { |
| 39 // Nothing responds to this type of request. |
| 40 serviceRequest.setErrorResponse('No route for: $path'); |
| 41 } |
| 42 } |
| 43 |
| 44 // Send response back over HTTP. |
| 45 request.response.headers.contentType = jsonContentType; |
| 46 request.response.write(serviceRequest.response); |
| 47 request.response.close(); |
| 48 } |
| 49 |
| 50 Future startServer() { |
| 51 return HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port).then((s) { |
| 52 // Only display message when port is automatically selected. |
| 53 var display_message = (port == 0); |
| 54 // Retrieve port. |
| 55 port = s.port; |
| 56 _server = s; |
| 57 _server.listen(_requestHandler); |
| 58 if (display_message) { |
| 59 print('VmService listening on port $port'); |
| 60 } |
| 61 return s; |
| 62 }); |
| 63 } |
| 64 } |
| 65 |
OLD | NEW |