Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: runtime/bin/vmservice/server.dart

Issue 308673012: Redo LocationManager to use HTML5 History API (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_io; 5 part of vmservice_io;
6 6
7 class WebSocketClient extends Client { 7 class WebSocketClient extends Client {
8 static const int PARSE_ERROR_CODE = 4000; 8 static const int PARSE_ERROR_CODE = 4000;
9 static const int BINARY_MESSAGE_ERROR_CODE = 4001; 9 static const int BINARY_MESSAGE_ERROR_CODE = 4001;
10 static const int NOT_MAP_ERROR_CODE = 4002; 10 static const int NOT_MAP_ERROR_CODE = 4002;
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 dynamic toJson() { 73 dynamic toJson() {
74 Map map = super.toJson(); 74 Map map = super.toJson();
75 map['type'] = 'HttpRequestClient'; 75 map['type'] = 'HttpRequestClient';
76 map['request'] = '$request'; 76 map['request'] = '$request';
77 return map; 77 return map;
78 } 78 }
79 } 79 }
80 80
81 class Server { 81 class Server {
82 static const WEBSOCKET_PATH = '/ws'; 82 static const WEBSOCKET_PATH = '/ws';
83 String defaultPath = '/index.html'; 83 String observatoryPath = '/index.html';
84 final String ip; 84 final String ip;
85 int port; 85 int port;
86 86
87 final VMService service; 87 final VMService service;
88 HttpServer _server; 88 HttpServer _server;
89 89
90 Server(this.service, this.ip, this.port); 90 Server(this.service, this.ip, this.port);
91 91
92 bool _shouldServeObservatory(HttpRequest request) {
93 if (request.headers['observatory'] != null) {
94 // Request is already coming from Observatory.
95 return false;
96 }
97 // TODO(johnmccutchan): Test with obscure browsers.
98 if (request.headers.value(HttpHeaders.USER_AGENT).contains('Mozilla')) {
99 // Request is coming from a browser but not Observatory application.
100 // Serve Observatory and let the Observatory make the real request.
101 return true;
102 }
103 // All other user agents are assumed to be textual.
104 return false;
105 }
106
92 void _requestHandler(HttpRequest request) { 107 void _requestHandler(HttpRequest request) {
93 // Allow cross origin requests. 108 // Allow cross origin requests with 'observatory' header.
94 request.response.headers.add('Access-Control-Allow-Origin', '*'); 109 request.response.headers.add('Access-Control-Allow-Origin', '*');
110 request.response.headers.add('Access-Control-Allow-Headers', 'observatory');
95 111
96 final String path = 112 if (request.method != 'GET') {
97 request.uri.path == '/' ? defaultPath : request.uri.path; 113 // Not a GET request. Do nothing.
98
99 var resource = Resource.resources[path];
100 if (resource != null) {
101 // Serving up a static resource (e.g. .css, .html, .png).
102 request.response.headers.contentType =
103 ContentType.parse(resource.mimeType);
104 request.response.add(resource.data);
105 request.response.close(); 114 request.response.close();
106 return; 115 return;
107 } 116 }
108 117
118 final String path =
119 request.uri.path == '/' ? observatoryPath : request.uri.path;
120
109 if (path == WEBSOCKET_PATH) { 121 if (path == WEBSOCKET_PATH) {
110 WebSocketTransformer.upgrade(request).then((WebSocket webSocket) { 122 WebSocketTransformer.upgrade(request).then((WebSocket webSocket) {
111 new WebSocketClient(webSocket, service); 123 new WebSocketClient(webSocket, service);
112 }); 124 });
113 return; 125 return;
114 } 126 }
115 127
128 var resource = Resource.resources[path];
129 if (resource == null && _shouldServeObservatory(request)) {
130 resource = Resource.resources[observatoryPath];
131 assert(resource != null);
132 }
133 if (resource != null) {
134 // Serving up a static resource (e.g. .css, .html, .png).
135 request.response.headers.contentType =
136 ContentType.parse(resource.mimeType);
137 request.response.add(resource.data);
138 request.response.close();
139 return;
140 }
116 var message = new Message.fromUri(request.uri); 141 var message = new Message.fromUri(request.uri);
117 var client = new HttpRequestClient(request, service); 142 var client = new HttpRequestClient(request, service);
118 client.onMessage(null, message); 143 client.onMessage(null, message);
119 } 144 }
120 145
121 Future startServer() { 146 Future startServer() {
122 return HttpServer.bind(ip, port).then((s) { 147 return HttpServer.bind(ip, port).then((s) {
123 // Only display message when port is automatically selected. 148 // Only display message when port is automatically selected.
124 var display_message = (ip != '127.0.0.1' || port != 8181); 149 var display_message = (ip != '127.0.0.1' || port != 8181);
125 // Retrieve port. 150 // Retrieve port.
126 port = s.port; 151 port = s.port;
127 _server = s; 152 _server = s;
128 _server.listen(_requestHandler); 153 _server.listen(_requestHandler);
129 if (display_message) { 154 if (display_message) {
130 print('Observatory listening on http://$ip:$port'); 155 print('Observatory listening on http://$ip:$port');
131 } 156 }
132 return s; 157 return s;
133 }); 158 });
134 } 159 }
135 } 160 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698