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 import "dart:async"; | 5 import "dart:async"; |
6 import "dart:io"; | 6 import "dart:io"; |
7 | 7 |
8 import "package:async_helper/async_helper.dart"; | 8 import "package:async_helper/async_helper.dart"; |
9 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
10 import "package:path/path.dart"; | 10 import "package:path/path.dart"; |
11 | 11 |
12 const HOST_NAME = "localhost"; | 12 const HOST_NAME = "localhost"; |
13 String localFile(path) => Platform.script.resolve(path).toFilePath(); | 13 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
14 | 14 |
15 SecurityContext serverContext = new SecurityContext() | 15 SecurityContext serverContext = new SecurityContext() |
16 ..useCertificateChain(localFile('certificates/server_chain.pem')) | 16 ..useCertificateChain(localFile('certificates/server_chain.pem')) |
17 ..usePrivateKey(localFile('certificates/server_key.pem'), | 17 ..usePrivateKey(localFile('certificates/server_key.pem'), |
18 password: 'dartdart'); | 18 password: 'dartdart'); |
19 // TODO: Specify which client certificate roots to trust. | 19 // TODO: Specify which client certificate roots to trust. |
20 | 20 |
21 SecurityContext clientContext = new SecurityContext() | 21 SecurityContext clientContext = new SecurityContext() |
22 ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')) | 22 ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')) |
23 // TODO: Set a client certificate here. | 23 // TODO: Set a client certificate here. |
24 ..useCertificateChain(localFile('certificates/server_chain.pem')) | 24 ..useCertificateChain(localFile('certificates/server_chain.pem')) |
25 ..usePrivateKey(localFile('certificates/server_key.pem'), | 25 ..usePrivateKey(localFile('certificates/server_key.pem'), |
26 password: 'dartdart'); | 26 password: 'dartdart'); |
27 | 27 |
28 void main() { | 28 void main() { |
29 asyncStart(); | 29 asyncStart(); |
30 HttpServer.bindSecure(HOST_NAME, | 30 HttpServer |
31 0, | 31 .bindSecure(HOST_NAME, 0, serverContext, |
32 serverContext, | 32 backlog: 5, requestClientCertificate: true) |
33 backlog: 5, | 33 .then((server) { |
34 requestClientCertificate: true).then((server) { | |
35 server.listen((HttpRequest request) { | 34 server.listen((HttpRequest request) { |
36 Expect.isNotNull(request.certificate); | 35 Expect.isNotNull(request.certificate); |
37 Expect.equals('CN=localhost', request.certificate.subject); | 36 Expect.equals('CN=localhost', request.certificate.subject); |
38 request.response.write("Hello"); | 37 request.response.write("Hello"); |
39 request.response.close(); | 38 request.response.close(); |
40 }); | 39 }); |
41 | 40 |
42 HttpClient client = new HttpClient(context: clientContext); | 41 HttpClient client = new HttpClient(context: clientContext); |
43 client.getUrl(Uri.parse("https://$HOST_NAME:${server.port}/")) | 42 client |
| 43 .getUrl(Uri.parse("https://$HOST_NAME:${server.port}/")) |
44 .then((request) => request.close()) | 44 .then((request) => request.close()) |
45 .then((response) { | 45 .then((response) { |
46 Expect.equals('CN=localhost', response.certificate.subject); | 46 Expect.equals('CN=localhost', response.certificate.subject); |
47 Expect.equals('CN=myauthority', response.certificate.issuer); | 47 Expect.equals('CN=myauthority', response.certificate.issuer); |
48 return response.fold(<int>[], | 48 return response.fold(<int>[], (message, data) => message..addAll(data)); |
49 (message, data) => message..addAll(data)); | 49 }).then((message) { |
50 }) | 50 String received = new String.fromCharCodes(message); |
51 .then((message) { | 51 Expect.equals(received, "Hello"); |
52 String received = new String.fromCharCodes(message); | 52 client.close(); |
53 Expect.equals(received, "Hello"); | 53 server.close(); |
54 client.close(); | 54 asyncEnd(); |
55 server.close(); | 55 }); |
56 asyncEnd(); | |
57 }); | |
58 }); | 56 }); |
59 } | 57 } |
OLD | NEW |