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 | 5 |
6 #import("dart:isolate"); | 6 #import("dart:isolate"); |
7 #import("dart:io"); | 7 #import("dart:io"); |
8 #import("dart:uri"); | |
8 | 9 |
9 void testHttp10Close(bool closeRequest) { | 10 void testHttp10Close(bool closeRequest) { |
10 HttpServer server = new HttpServer(); | 11 HttpServer server = new HttpServer(); |
11 server.listen("127.0.0.1", 0, backlog: 5); | 12 server.listen("127.0.0.1", 0, backlog: 5); |
12 | 13 |
13 Socket socket = new Socket("127.0.0.1", server.port); | 14 Socket socket = new Socket("127.0.0.1", server.port); |
14 socket.onConnect = () { | 15 socket.onConnect = () { |
15 List<int> buffer = new List<int>(1024); | 16 List<int> buffer = new List<int>(1024); |
16 socket.outputStream.writeString("GET / HTTP/1.0\r\n\r\n"); | 17 socket.outputStream.writeString("GET / HTTP/1.0\r\n\r\n"); |
17 if (closeRequest) socket.outputStream.close(); | 18 if (closeRequest) socket.outputStream.close(); |
(...skipping 16 matching lines...) Expand all Loading... | |
34 "GET / HTTP/1.1\r\nConnection: close\r\n\r\n"); | 35 "GET / HTTP/1.1\r\nConnection: close\r\n\r\n"); |
35 if (closeRequest) socket.outputStream.close(); | 36 if (closeRequest) socket.outputStream.close(); |
36 socket.onData = () => socket.readList(buffer, 0, buffer.length); | 37 socket.onData = () => socket.readList(buffer, 0, buffer.length); |
37 socket.onClosed = () { | 38 socket.onClosed = () { |
38 if (!closeRequest) socket.close(true); | 39 if (!closeRequest) socket.close(true); |
39 server.close(); | 40 server.close(); |
40 }; | 41 }; |
41 }; | 42 }; |
42 } | 43 } |
43 | 44 |
45 void testStreamResponse() { | |
46 var server = new HttpServer(); | |
47 server.listen("127.0.0.1", 0, backlog: 5); | |
48 server.defaultRequestHandler = (var request, var response) { | |
49 new Timer.repeating(10, (x) { | |
50 Date now = new Date.now(); | |
51 try { | |
52 response.outputStream.writeString( | |
53 'data:${now.millisecondsSinceEpoch}\n\n'); | |
54 } catch (e) { | |
55 x.cancel(); | |
56 server.close(); | |
57 } | |
58 }); | |
59 }; | |
60 | |
61 var client = new HttpClient(); | |
62 var connection = | |
63 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}")); | |
64 connection.onResponse = (resp) { | |
65 int bytes = 0; | |
66 resp.inputStream.onData = () { | |
67 bytes += resp.inputStream.read().length; | |
68 //print(bytes); | |
Mads Ager (google)
2012/11/20 08:04:28
Remove?
Søren Gjesse
2012/11/20 08:15:11
Done.
| |
69 if (bytes > 100) { | |
70 client.shutdown(); | |
71 } | |
72 }; | |
73 }; | |
74 } | |
75 | |
44 main() { | 76 main() { |
45 testHttp10Close(false); | 77 testHttp10Close(false); |
46 testHttp10Close(true); | 78 testHttp10Close(true); |
47 testHttp11Close(false); | 79 testHttp11Close(false); |
48 testHttp11Close(true); | 80 testHttp11Close(true); |
81 testStreamResponse(); | |
49 } | 82 } |
OLD | NEW |