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 class TestServer { |
95 // Echo the request content back to the response. | 99 // Echo the request content back to the response. |
96 void _echoHandler(HttpRequest request, HttpResponse response) { | 100 void _echoHandler(HttpRequest request, HttpResponse response) { |
97 Expect.equals("POST", request.method); | 101 Expect.equals("POST", request.method); |
98 response.contentLength = request.contentLength; | 102 response.contentLength = request.contentLength; |
99 request.inputStream.pipe(response.outputStream); | 103 request.inputStream.pipe(response.outputStream); |
100 } | 104 } |
101 | 105 |
102 // Return a 404. | 106 // Return a 404. |
103 void _notFoundHandler(HttpRequest request, HttpResponse response) { | 107 void _notFoundHandler(HttpRequest request, HttpResponse response) { |
104 response.statusCode = HttpStatus.NOT_FOUND; | 108 response.statusCode = HttpStatus.NOT_FOUND; |
105 response.headers.set("Content-Type", "text/html; charset=UTF-8"); | 109 response.headers.set("Content-Type", "text/html; charset=UTF-8"); |
106 response.outputStream.writeString("Page not found"); | 110 response.outputStream.writeString("Page not found"); |
107 response.outputStream.close(); | 111 response.outputStream.close(); |
108 } | 112 } |
109 | 113 |
110 | 114 |
111 void main() { | 115 void init() { |
112 // Setup request handlers. | 116 // Setup request handlers. |
113 _requestHandlers = new Map(); | 117 _requestHandlers = new Map(); |
114 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { | 118 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { |
115 _echoHandler(request, response); | 119 _echoHandler(request, response); |
116 }; | 120 }; |
| 121 } |
117 | 122 |
118 this.port.receive((var message, SendPort replyTo) { | 123 void dispatch(message, SendPort replyTo) { |
119 if (message.isStart) { | 124 if (message.isStart) { |
120 _server = new HttpServer(); | 125 _server = new HttpServer(); |
121 try { | 126 try { |
122 _server.listen("127.0.0.1", 0); | 127 _server.listen("127.0.0.1", 0); |
123 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) { | 128 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) { |
124 _requestReceivedHandler(req, rsp); | 129 _requestReceivedHandler(req, rsp); |
125 }; | 130 }; |
126 replyTo.send(new TestServerStatus.started(_server.port), null); | 131 replyTo.send(new TestServerStatus.started(_server.port), null); |
127 } catch (var e) { | 132 } catch (var e) { |
128 replyTo.send(new TestServerStatus.error(), null); | 133 replyTo.send(new TestServerStatus.error(), null); |
129 } | |
130 } else if (message.isStop) { | |
131 _server.close(); | |
132 this.port.close(); | |
133 replyTo.send(new TestServerStatus.stopped(), null); | |
134 } else if (message.isChunkedEncoding) { | |
135 _chunkedEncoding = true; | |
136 } | 134 } |
137 }); | 135 } else if (message.isStop) { |
| 136 _server.close(); |
| 137 port.close(); |
| 138 replyTo.send(new TestServerStatus.stopped(), null); |
| 139 } else if (message.isChunkedEncoding) { |
| 140 _chunkedEncoding = true; |
| 141 } |
138 } | 142 } |
139 | 143 |
140 void _requestReceivedHandler(HttpRequest request, HttpResponse response) { | 144 void _requestReceivedHandler(HttpRequest request, HttpResponse response) { |
141 var requestHandler =_requestHandlers[request.path]; | 145 var requestHandler =_requestHandlers[request.path]; |
142 if (requestHandler != null) { | 146 if (requestHandler != null) { |
143 requestHandler(request, response); | 147 requestHandler(request, response); |
144 } else { | 148 } else { |
145 _notFoundHandler(request, response); | 149 _notFoundHandler(request, response); |
146 } | 150 } |
147 } | 151 } |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 } | 261 } |
258 testServerMain.start(); | 262 testServerMain.start(); |
259 } | 263 } |
260 | 264 |
261 void main() { | 265 void main() { |
262 testReadInto(true); | 266 testReadInto(true); |
263 testReadInto(false); | 267 testReadInto(false); |
264 testReadShort(true); | 268 testReadShort(true); |
265 testReadShort(false); | 269 testReadShort(false); |
266 } | 270 } |
OLD | NEW |