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 // 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 // OtherResources=certificates/server_chain.pem | 9 // OtherResources=certificates/server_chain.pem |
10 // OtherResources=certificates/server_key.pem | 10 // OtherResources=certificates/server_key.pem |
11 // OtherResources=certificates/trusted_certs.pem | 11 // OtherResources=certificates/trusted_certs.pem |
12 | 12 |
13 import "package:expect/expect.dart"; | 13 import "package:expect/expect.dart"; |
14 import "package:path/path.dart"; | 14 import "package:path/path.dart"; |
15 import "dart:async"; | 15 import "dart:async"; |
16 import "dart:io"; | 16 import "dart:io"; |
17 import "dart:isolate"; | 17 import "dart:isolate"; |
18 | 18 |
19 String localFile(path) => Platform.script.resolve(path).toFilePath(); | 19 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
20 | 20 |
21 SecurityContext serverContext = new SecurityContext() | 21 SecurityContext serverContext = new SecurityContext() |
22 ..useCertificateChain(localFile('certificates/server_chain.pem')) | 22 ..useCertificateChain(localFile('certificates/server_chain.pem')) |
23 ..usePrivateKey(localFile('certificates/server_key.pem'), | 23 ..usePrivateKey(localFile('certificates/server_key.pem'), |
24 password: 'dartdart'); | 24 password: 'dartdart'); |
25 | 25 |
26 SecurityContext clientContext = new SecurityContext() | 26 SecurityContext clientContext = new SecurityContext() |
27 ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')); | 27 ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')); |
28 | 28 |
29 main() async { | 29 main() async { |
30 List<int> message = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n".codeUnits; | 30 List<int> message = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n".codeUnits; |
31 int written = 0; | 31 int written = 0; |
32 List<int> body = <int>[]; | 32 List<int> body = <int>[]; |
33 var server = await HttpServer.bindSecure( | 33 var server = |
34 "localhost", | 34 await HttpServer.bindSecure("localhost", 0, serverContext, backlog: 5); |
35 0, | |
36 serverContext, | |
37 backlog: 5); | |
38 server.listen((HttpRequest request) async { | 35 server.listen((HttpRequest request) async { |
39 await request.drain(); | 36 await request.drain(); |
40 request.response.contentLength = 100; | 37 request.response.contentLength = 100; |
41 for (int i = 0; i < 10; i++) { | 38 for (int i = 0; i < 10; i++) { |
42 request.response.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | 39 request.response.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
43 } | 40 } |
44 request.response.close(); | 41 request.response.close(); |
45 }); | 42 }); |
46 var socket = await RawSecureSocket.connect("localhost", | 43 var socket = await RawSecureSocket.connect("localhost", server.port, |
47 server.port, | 44 context: clientContext); |
48 context: clientContext); | |
49 socket.listen((RawSocketEvent event) { | 45 socket.listen((RawSocketEvent event) { |
50 switch (event) { | 46 switch (event) { |
51 case RawSocketEvent.READ: | 47 case RawSocketEvent.READ: |
52 body.addAll(socket.read()); | 48 body.addAll(socket.read()); |
53 break; | 49 break; |
54 case RawSocketEvent.WRITE: | 50 case RawSocketEvent.WRITE: |
55 written += | 51 written += socket.write(message, written, message.length - written); |
56 socket.write(message, written, message.length - written); | |
57 if (written < message.length) { | 52 if (written < message.length) { |
58 socket.writeEventsEnabled = true; | 53 socket.writeEventsEnabled = true; |
59 } else { | 54 } else { |
60 socket.shutdown(SocketDirection.SEND); | 55 socket.shutdown(SocketDirection.SEND); |
61 } | 56 } |
62 break; | 57 break; |
63 case RawSocketEvent.READ_CLOSED: | 58 case RawSocketEvent.READ_CLOSED: |
64 Expect.isTrue(body.length > 100, "$body\n${body.length}"); | 59 Expect.isTrue(body.length > 100, "$body\n${body.length}"); |
65 Expect.equals(72, body[0]); | 60 Expect.equals(72, body[0]); |
66 Expect.equals(9, body[body.length - 1]); | 61 Expect.equals(9, body[body.length - 1]); |
67 server.close(); | 62 server.close(); |
68 break; | 63 break; |
69 default: throw "Unexpected event $event"; | 64 default: |
| 65 throw "Unexpected event $event"; |
70 } | 66 } |
71 }, onError: (e, trace) { | 67 }, onError: (e, trace) { |
72 String msg = "onError handler of RawSecureSocket stream hit $e"; | 68 String msg = "onError handler of RawSecureSocket stream hit $e"; |
73 if (trace != null) msg += "\nStackTrace: $trace"; | 69 if (trace != null) msg += "\nStackTrace: $trace"; |
74 Expect.fail(msg); | 70 Expect.fail(msg); |
75 }); | 71 }); |
76 } | 72 } |
OLD | NEW |