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 Resource 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 ServiceRequest serviceRequest = new ServiceRequest(); | |
32 bool 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 | |
51 Future startServer() { | |
52 return HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port).then((s) { | |
53 // Only display message when port is automatically selected. | |
54 bool display_message = port == 0; | |
siva
2013/07/19 17:41:16
slightly unreadable, maybe (port == 0);
Cutch
2013/07/19 18:15:02
Done.
| |
55 // Retrieve port. | |
56 port = s.port; | |
57 _server = s; | |
58 _server.listen(_requestHandler); | |
59 if (display_message) { | |
60 print('VmService listening on port $port'); | |
61 } | |
62 return s; | |
63 }); | |
64 } | |
65 } | |
OLD | NEW |