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 | 5 |
6 import "dart:io"; | 6 import "dart:io"; |
7 | 7 |
8 main() { | 8 main() { |
9 bool serverOnClosed = false; | 9 bool serverOnClosed = false; |
10 bool clientOnClosed = false; | 10 bool clientOnClosed = false; |
11 bool requestOnClosed = false; | 11 bool requestOnClosed = false; |
12 | 12 |
13 HttpServer.bind("127.0.0.1", 0).then((server) { | 13 HttpServer.bind("127.0.0.1", 0).then((server) { |
14 var client = new HttpClient(); | 14 var client = new HttpClient(); |
15 | 15 |
16 checkDone() { | 16 checkDone() { |
17 if (serverOnClosed && clientOnClosed && requestOnClosed) { | 17 if (serverOnClosed && clientOnClosed && requestOnClosed) { |
18 server.close(); | 18 server.close(); |
19 client.close(); | 19 client.close(); |
20 } | 20 } |
21 } | 21 } |
22 | 22 |
23 server.listen((request) { | 23 server.listen((request) { |
24 request.listen( | 24 request.listen((_) {}, onDone: () { |
25 (_) {}, | 25 request.response.done.then((_) { |
26 onDone: () { | 26 serverOnClosed = true; |
27 request.response.done.then((_) { | 27 checkDone(); |
28 serverOnClosed = true; | 28 }); |
29 checkDone(); | 29 request.response.write("hello!"); |
30 }); | 30 request.response.close(); |
31 request.response.write("hello!"); | |
32 request.response.close(); | |
33 }); | |
34 }); | 31 }); |
| 32 }); |
35 | 33 |
36 client.postUrl(Uri.parse("http://127.0.0.1:${server.port}")) | 34 client |
| 35 .postUrl(Uri.parse("http://127.0.0.1:${server.port}")) |
37 .then((request) { | 36 .then((request) { |
38 request.contentLength = "hello!".length; | 37 request.contentLength = "hello!".length; |
39 request.done.then((_) { | 38 request.done.then((_) { |
40 clientOnClosed = true; | 39 clientOnClosed = true; |
41 checkDone(); | 40 checkDone(); |
42 }); | 41 }); |
43 request.write("hello!"); | 42 request.write("hello!"); |
44 return request.close(); | 43 return request.close(); |
45 }) | 44 }).then((response) { |
46 .then((response) { | 45 response.listen((_) {}, onDone: () { |
47 response.listen( | 46 requestOnClosed = true; |
48 (_) {}, | 47 checkDone(); |
49 onDone: () { | 48 }); |
50 requestOnClosed = true; | 49 }); |
51 checkDone(); | |
52 }); | |
53 }); | |
54 }); | 50 }); |
55 } | 51 } |
OLD | NEW |