OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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("dart:isolate"); | 10 #import("dart:isolate"); |
11 #import("dart:io"); | 11 #import("dart:io"); |
12 | 12 |
13 class TestServerMain { | 13 class TestServerMain { |
14 TestServerMain() | 14 TestServerMain() |
15 : _statusPort = new ReceivePort(), | 15 : _statusPort = new ReceivePort(), |
16 _serverPort = null { | 16 _serverPort = null { |
17 new TestServer().spawn().then((SendPort port) { | 17 _serverPort = spawnFunction(startTestServer); |
18 _serverPort = port; | |
19 }); | |
20 } | 18 } |
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() { |
27 // Handle status messages from the server. | 25 // Handle status messages from the server. |
28 _statusPort.receive((var status, SendPort replyTo) { | 26 _statusPort.receive((var status, SendPort replyTo) { |
29 if (status.isStarted) { | 27 if (status.isStarted) { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 bool get isStopped() => _state == STOPPED; | 82 bool get isStopped() => _state == STOPPED; |
85 bool get isError() => _state == ERROR; | 83 bool get isError() => _state == ERROR; |
86 | 84 |
87 int get port() => _port; | 85 int get port() => _port; |
88 | 86 |
89 int _state; | 87 int _state; |
90 int _port; | 88 int _port; |
91 } | 89 } |
92 | 90 |
93 | 91 |
94 class TestServer extends Isolate { | 92 void startTestServer() { |
| 93 var server = new TestServer(); |
| 94 server.init(); |
| 95 port.receive(server.dispatch); |
| 96 } |
| 97 |
| 98 |
| 99 class TestServer { |
95 // Return a 404. | 100 // Return a 404. |
96 void _notFoundHandler(HttpRequest request, HttpResponse response) { | 101 void _notFoundHandler(HttpRequest request, HttpResponse response) { |
97 response.statusCode = HttpStatus.NOT_FOUND; | 102 response.statusCode = HttpStatus.NOT_FOUND; |
98 response.headers.set("Content-Type", "text/html; charset=UTF-8"); | 103 response.headers.set("Content-Type", "text/html; charset=UTF-8"); |
99 response.outputStream.writeString("Page not found"); | 104 response.outputStream.writeString("Page not found"); |
100 response.outputStream.close(); | 105 response.outputStream.close(); |
101 } | 106 } |
102 | 107 |
103 // Check the "Host" header. | 108 // Check the "Host" header. |
104 void _hostHandler(HttpRequest request, HttpResponse response) { | 109 void _hostHandler(HttpRequest request, HttpResponse response) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 response.cookies.add(cookie2); | 171 response.cookies.add(cookie2); |
167 response.outputStream.close(); | 172 response.outputStream.close(); |
168 } | 173 } |
169 | 174 |
170 void _cookie2Handler(HttpRequest request, HttpResponse response) { | 175 void _cookie2Handler(HttpRequest request, HttpResponse response) { |
171 // Two cookies passed with this request. | 176 // Two cookies passed with this request. |
172 Expect.equals(2, request.cookies.length); | 177 Expect.equals(2, request.cookies.length); |
173 response.outputStream.close(); | 178 response.outputStream.close(); |
174 } | 179 } |
175 | 180 |
176 void main() { | 181 void init() { |
177 // Setup request handlers. | 182 // Setup request handlers. |
178 _requestHandlers = new Map(); | 183 _requestHandlers = new Map(); |
179 _requestHandlers["/host"] = | 184 _requestHandlers["/host"] = |
180 (HttpRequest request, HttpResponse response) { | 185 (HttpRequest request, HttpResponse response) { |
181 _hostHandler(request, response); | 186 _hostHandler(request, response); |
182 }; | 187 }; |
183 _requestHandlers["/expires1"] = | 188 _requestHandlers["/expires1"] = |
184 (HttpRequest request, HttpResponse response) { | 189 (HttpRequest request, HttpResponse response) { |
185 _expires1Handler(request, response); | 190 _expires1Handler(request, response); |
186 }; | 191 }; |
(...skipping 10 matching lines...) Expand all Loading... |
197 _contentType2Handler(request, response); | 202 _contentType2Handler(request, response); |
198 }; | 203 }; |
199 _requestHandlers["/cookie1"] = | 204 _requestHandlers["/cookie1"] = |
200 (HttpRequest request, HttpResponse response) { | 205 (HttpRequest request, HttpResponse response) { |
201 _cookie1Handler(request, response); | 206 _cookie1Handler(request, response); |
202 }; | 207 }; |
203 _requestHandlers["/cookie2"] = | 208 _requestHandlers["/cookie2"] = |
204 (HttpRequest request, HttpResponse response) { | 209 (HttpRequest request, HttpResponse response) { |
205 _cookie2Handler(request, response); | 210 _cookie2Handler(request, response); |
206 }; | 211 }; |
| 212 } |
207 | 213 |
208 this.port.receive((var message, SendPort replyTo) { | 214 void dispatch(message, replyTo) { |
209 if (message.isStart) { | 215 if (message.isStart) { |
210 _server = new HttpServer(); | 216 _server = new HttpServer(); |
211 try { | 217 try { |
212 _server.listen("127.0.0.1", 0); | 218 _server.listen("127.0.0.1", 0); |
213 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) { | 219 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) { |
214 _requestReceivedHandler(req, rsp); | 220 _requestReceivedHandler(req, rsp); |
215 }; | 221 }; |
216 replyTo.send(new TestServerStatus.started(_server.port), null); | 222 replyTo.send(new TestServerStatus.started(_server.port), null); |
217 } catch (var e) { | 223 } catch (var e) { |
218 replyTo.send(new TestServerStatus.error(), null); | 224 replyTo.send(new TestServerStatus.error(), null); |
219 } | |
220 } else if (message.isStop) { | |
221 _server.close(); | |
222 this.port.close(); | |
223 replyTo.send(new TestServerStatus.stopped(), null); | |
224 } else if (message.isChunkedEncoding) { | |
225 _chunkedEncoding = true; | |
226 } | 225 } |
227 }); | 226 } else if (message.isStop) { |
| 227 _server.close(); |
| 228 port.close(); |
| 229 replyTo.send(new TestServerStatus.stopped(), null); |
| 230 } else if (message.isChunkedEncoding) { |
| 231 _chunkedEncoding = true; |
| 232 } |
228 } | 233 } |
229 | 234 |
230 void _requestReceivedHandler(HttpRequest request, HttpResponse response) { | 235 void _requestReceivedHandler(HttpRequest request, HttpResponse response) { |
231 var requestHandler =_requestHandlers[request.path]; | 236 var requestHandler =_requestHandlers[request.path]; |
232 if (requestHandler != null) { | 237 if (requestHandler != null) { |
233 requestHandler(request, response); | 238 requestHandler(request, response); |
234 } else { | 239 } else { |
235 _notFoundHandler(request, response); | 240 _notFoundHandler(request, response); |
236 } | 241 } |
237 } | 242 } |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 }); | 404 }); |
400 testServerMain.start(); | 405 testServerMain.start(); |
401 } | 406 } |
402 | 407 |
403 void main() { | 408 void main() { |
404 testHost(); | 409 testHost(); |
405 testExpires(); | 410 testExpires(); |
406 testContentType(); | 411 testContentType(); |
407 testCookies(); | 412 testCookies(); |
408 } | 413 } |
OLD | NEW |