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