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 // VMOptions= | 5 // VMOptions= |
6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
7 // VMOptions=--short_socket_write | 7 // VMOptions=--short_socket_write |
8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
9 | 9 |
10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
11 import 'dart:async'; | 11 import 'dart:async'; |
12 import 'dart:io'; | 12 import 'dart:io'; |
13 import 'dart:isolate'; | 13 import 'dart:isolate'; |
14 | 14 |
15 class IsolatedHttpServer { | 15 class IsolatedHttpServer { |
16 IsolatedHttpServer() | 16 IsolatedHttpServer() |
17 : _statusPort = new ReceivePort(), | 17 : _statusPort = new ReceivePort(), |
18 _serverPort = null { | 18 _serverPort = null; |
19 _serverPort = spawnFunction(startIsolatedHttpServer); | |
20 } | |
21 | 19 |
22 void setServerStartedHandler(void startedCallback(int port)) { | 20 void setServerStartedHandler(void startedCallback(int port)) { |
23 _startedCallback = startedCallback; | 21 _startedCallback = startedCallback; |
24 } | 22 } |
25 | 23 |
26 void start() { | 24 void start() { |
| 25 ReceivePort receivePort = new ReceivePort(); |
| 26 var remote = Isolate.spawn(startIsolatedHttpServer, receivePort.sendPort); |
| 27 receivePort.first.then((port) { |
| 28 _serverPort = port; |
| 29 |
| 30 // Send server start message to the server. |
| 31 var command = new IsolatedHttpServerCommand.start(); |
| 32 port.send([command, _statusPort.sendPort]); |
| 33 }); |
| 34 |
27 // Handle status messages from the server. | 35 // Handle status messages from the server. |
28 _statusPort.receive((var status, SendPort replyTo) { | 36 _statusPort.listen((var status) { |
29 if (status.isStarted) { | 37 if (status.isStarted) { |
30 _startedCallback(status.port); | 38 _startedCallback(status.port); |
31 } | 39 } |
32 }); | 40 }); |
33 | |
34 // Send server start message to the server. | |
35 var command = new IsolatedHttpServerCommand.start(); | |
36 _serverPort.send(command, _statusPort.toSendPort()); | |
37 } | 41 } |
38 | 42 |
39 void shutdown() { | 43 void shutdown() { |
40 // Send server stop message to the server. | 44 // Send server stop message to the server. |
41 _serverPort.send(new IsolatedHttpServerCommand.stop(), | 45 _serverPort.send([new IsolatedHttpServerCommand.stop(), |
42 _statusPort.toSendPort()); | 46 _statusPort.sendPort]); |
43 _statusPort.close(); | 47 _statusPort.close(); |
44 } | 48 } |
45 | 49 |
46 void chunkedEncoding() { | 50 void chunkedEncoding() { |
47 // Send chunked encoding message to the server. | 51 // Send chunked encoding message to the server. |
48 _serverPort.send( | 52 _serverPort.send([ |
49 new IsolatedHttpServerCommand.chunkedEncoding(), | 53 new IsolatedHttpServerCommand.chunkedEncoding(), |
50 _statusPort.toSendPort()); | 54 _statusPort.sendPort]); |
51 } | 55 } |
52 | 56 |
53 ReceivePort _statusPort; // Port for receiving messages from the server. | 57 ReceivePort _statusPort; // Port for receiving messages from the server. |
54 SendPort _serverPort; // Port for sending messages to the server. | 58 SendPort _serverPort; // Port for sending messages to the server. |
55 var _startedCallback; | 59 var _startedCallback; |
56 } | 60 } |
57 | 61 |
58 | 62 |
59 class IsolatedHttpServerCommand { | 63 class IsolatedHttpServerCommand { |
60 static const START = 0; | 64 static const START = 0; |
(...skipping 25 matching lines...) Expand all Loading... |
86 bool get isStopped => _state == STOPPED; | 90 bool get isStopped => _state == STOPPED; |
87 bool get isError => _state == ERROR; | 91 bool get isError => _state == ERROR; |
88 | 92 |
89 int get port => _port; | 93 int get port => _port; |
90 | 94 |
91 int _state; | 95 int _state; |
92 int _port; | 96 int _port; |
93 } | 97 } |
94 | 98 |
95 | 99 |
96 void startIsolatedHttpServer() { | 100 void startIsolatedHttpServer(SendPort replyTo) { |
97 var server = new TestServer(); | 101 var server = new TestServer(); |
98 server.init(); | 102 server.init(); |
99 port.receive(server.dispatch); | 103 replyTo.send(server.dispatchSendPort); |
100 } | 104 } |
101 | 105 |
102 | 106 |
103 class TestServer { | 107 class TestServer { |
104 // Return a 404. | 108 // Return a 404. |
105 void _notFoundHandler(HttpRequest request) { | 109 void _notFoundHandler(HttpRequest request) { |
106 var response = request.response; | 110 var response = request.response; |
107 response.statusCode = HttpStatus.NOT_FOUND; | 111 response.statusCode = HttpStatus.NOT_FOUND; |
108 response.headers.set("Content-Type", "text/html; charset=UTF-8"); | 112 response.headers.set("Content-Type", "text/html; charset=UTF-8"); |
109 response.outputStream.writeString("Page not found"); | 113 response.outputStream.writeString("Page not found"); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 void init() { | 198 void init() { |
195 // Setup request handlers. | 199 // Setup request handlers. |
196 _requestHandlers = new Map(); | 200 _requestHandlers = new Map(); |
197 _requestHandlers["/host"] = _hostHandler; | 201 _requestHandlers["/host"] = _hostHandler; |
198 _requestHandlers["/expires1"] = _expires1Handler; | 202 _requestHandlers["/expires1"] = _expires1Handler; |
199 _requestHandlers["/expires2"] = _expires2Handler; | 203 _requestHandlers["/expires2"] = _expires2Handler; |
200 _requestHandlers["/contenttype1"] = _contentType1Handler; | 204 _requestHandlers["/contenttype1"] = _contentType1Handler; |
201 _requestHandlers["/contenttype2"] = _contentType2Handler; | 205 _requestHandlers["/contenttype2"] = _contentType2Handler; |
202 _requestHandlers["/cookie1"] = _cookie1Handler; | 206 _requestHandlers["/cookie1"] = _cookie1Handler; |
203 _requestHandlers["/cookie2"] = _cookie2Handler; | 207 _requestHandlers["/cookie2"] = _cookie2Handler; |
| 208 _dispatchPort = new ReceivePort(); |
| 209 _dispatchPort.listen(dispatch); |
204 } | 210 } |
205 | 211 |
206 void dispatch(message, replyTo) { | 212 SendPort get dispatchSendPort => _dispatchPort.sendPort; |
207 if (message.isStart) { | 213 |
| 214 void dispatch(message) { |
| 215 IsolatedHttpServerCommand command = message[0]; |
| 216 SendPort replyTo = message[1]; |
| 217 if (command.isStart) { |
208 try { | 218 try { |
209 HttpServer.bind("127.0.0.1", 0).then((server) { | 219 HttpServer.bind("127.0.0.1", 0).then((server) { |
210 _server = server; | 220 _server = server; |
211 _server.listen(_requestReceivedHandler); | 221 _server.listen(_requestReceivedHandler); |
212 replyTo.send(new IsolatedHttpServerStatus.started(_server.port), | 222 replyTo.send(new IsolatedHttpServerStatus.started(_server.port), |
213 null); | 223 null); |
214 }); | 224 }); |
215 } catch (e) { | 225 } catch (e) { |
216 replyTo.send(new IsolatedHttpServerStatus.error(), null); | 226 replyTo.send(new IsolatedHttpServerStatus.error(), null); |
217 } | 227 } |
218 } else if (message.isStop) { | 228 } else if (command.isStop) { |
219 _server.close(); | 229 _server.close(); |
220 port.close(); | 230 _dispatchPort.close(); |
221 replyTo.send(new IsolatedHttpServerStatus.stopped(), null); | 231 replyTo.send(new IsolatedHttpServerStatus.stopped(), null); |
222 } else if (message.isChunkedEncoding) { | 232 } else if (command.isChunkedEncoding) { |
223 _chunkedEncoding = true; | 233 _chunkedEncoding = true; |
224 } | 234 } |
225 } | 235 } |
226 | 236 |
227 void _requestReceivedHandler(HttpRequest request) { | 237 void _requestReceivedHandler(HttpRequest request) { |
228 var requestHandler =_requestHandlers[request.uri.path]; | 238 var requestHandler =_requestHandlers[request.uri.path]; |
229 if (requestHandler != null) { | 239 if (requestHandler != null) { |
230 requestHandler(request); | 240 requestHandler(request); |
231 } else { | 241 } else { |
232 _notFoundHandler(request); | 242 _notFoundHandler(request); |
233 } | 243 } |
234 } | 244 } |
235 | 245 |
236 HttpServer _server; // HTTP server instance. | 246 HttpServer _server; // HTTP server instance. |
| 247 ReceivePort _dispatchPort; |
237 Map _requestHandlers; | 248 Map _requestHandlers; |
238 bool _chunkedEncoding = false; | 249 bool _chunkedEncoding = false; |
239 } | 250 } |
240 | 251 |
241 Future testHost() { | 252 Future testHost() { |
242 Completer completer = new Completer(); | 253 Completer completer = new Completer(); |
243 IsolatedHttpServer server = new IsolatedHttpServer(); | 254 IsolatedHttpServer server = new IsolatedHttpServer(); |
244 server.setServerStartedHandler((int port) { | 255 server.setServerStartedHandler((int port) { |
245 HttpClient httpClient = new HttpClient(); | 256 HttpClient httpClient = new HttpClient(); |
246 httpClient.get("127.0.0.1", port, "/host") | 257 httpClient.get("127.0.0.1", port, "/host") |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 | 432 |
422 void main() { | 433 void main() { |
423 testHost().then((_) { | 434 testHost().then((_) { |
424 return testExpires().then((_) { | 435 return testExpires().then((_) { |
425 return testContentType().then((_) { | 436 return testContentType().then((_) { |
426 return testCookies(); | 437 return testCookies(); |
427 }); | 438 }); |
428 }); | 439 }); |
429 }); | 440 }); |
430 } | 441 } |
OLD | NEW |