| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 part of vmservice; | 5 part of vmservice; |
| 6 | 6 |
| 7 class Server { | 7 class Server { |
| 8 int port; | 8 int port; |
| 9 static ContentType jsonContentType = ContentType.parse('application/json'); | 9 static ContentType jsonContentType = ContentType.parse('application/json'); |
| 10 final VmService service; | 10 final VmService service; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 request.response.close(); | 27 request.response.close(); |
| 28 return; | 28 return; |
| 29 } | 29 } |
| 30 | 30 |
| 31 var serviceRequest = new ServiceRequest(); | 31 var serviceRequest = new ServiceRequest(); |
| 32 var r = serviceRequest.parse(request.uri); | 32 var r = serviceRequest.parse(request.uri); |
| 33 if (!r) { | 33 if (!r) { |
| 34 // Did not understand the request uri. | 34 // Did not understand the request uri. |
| 35 serviceRequest.setErrorResponse('Invalid request uri: ${request.uri}'); | 35 serviceRequest.setErrorResponse('Invalid request uri: ${request.uri}'); |
| 36 } else { | 36 } else { |
| 37 r = service.runningIsolates.route(serviceRequest); | 37 var f = service.runningIsolates.route(serviceRequest); |
| 38 if (!r) { | 38 if (f != null) { |
| 39 f.then((_) { |
| 40 request.response.headers.contentType = jsonContentType; |
| 41 request.response.write(serviceRequest.response); |
| 42 request.response.close(); |
| 43 }).catchError((e) { }); |
| 44 return; |
| 45 } else { |
| 39 // Nothing responds to this type of request. | 46 // Nothing responds to this type of request. |
| 40 serviceRequest.setErrorResponse('No route for: $path'); | 47 serviceRequest.setErrorResponse('No route for: $path'); |
| 41 } | 48 } |
| 42 } | 49 } |
| 43 | 50 |
| 44 // Send response back over HTTP. | |
| 45 request.response.headers.contentType = jsonContentType; | 51 request.response.headers.contentType = jsonContentType; |
| 46 request.response.write(serviceRequest.response); | 52 request.response.write(serviceRequest.response); |
| 47 request.response.close(); | 53 request.response.close(); |
| 48 } | 54 } |
| 49 | 55 |
| 50 Future startServer() { | 56 Future startServer() { |
| 51 return HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port).then((s) { | 57 return HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port).then((s) { |
| 52 // Only display message when port is automatically selected. | 58 // Only display message when port is automatically selected. |
| 53 var display_message = (port == 0); | 59 var display_message = (port == 0); |
| 54 // Retrieve port. | 60 // Retrieve port. |
| 55 port = s.port; | 61 port = s.port; |
| 56 _server = s; | 62 _server = s; |
| 57 _server.listen(_requestHandler); | 63 _server.listen(_requestHandler); |
| 58 if (display_message) { | 64 if (display_message) { |
| 59 print('VmService listening on port $port'); | 65 print('VmService listening on port $port'); |
| 60 } | 66 } |
| 61 return s; | 67 return s; |
| 62 }); | 68 }); |
| 63 } | 69 } |
| 64 } | 70 } |
| 65 | |
| OLD | NEW |