2 // tlsasync.cs: Multi-sessions TLS/SSL Test Program with async streams
3 // based on tlstest.cs and tlsmulti.cs
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2004-2005 Novell (http://www.novell.com)
12 using System.Collections;
13 using System.Globalization;
16 using System.Net.Sockets;
17 using System.Reflection;
18 using System.Security.Cryptography.X509Certificates;
20 using System.Threading;
22 using Mono.Security.Protocol.Tls;
26 static ArrayList handleList = new ArrayList ();
29 private HttpWebRequest request;
30 private ManualResetEvent handle;
31 private Stream stream;
32 private byte[] buffer;
33 private MemoryStream memory;
35 public State (int id, HttpWebRequest req)
39 handle = new ManualResetEvent (false);
40 handleList.Add (handle);
47 public HttpWebRequest Request {
48 get { return request; }
51 public Stream Stream {
52 get { return stream; }
53 set { stream = value; }
56 public byte[] Buffer {
59 buffer = new byte [256]; // really small on purpose
64 public Stream Memory {
67 memory = new MemoryStream ();
72 public void Complete ()
77 static public void WaitAll ()
79 if (handleList.Count > 0) {
80 WaitHandle[] handles = (WaitHandle[]) handleList.ToArray (typeof (WaitHandle));
81 WaitHandle.WaitAll (handles);
87 public class MultiTest {
91 public static void Main (string[] args)
93 if (args.Length == 0) {
94 Console.WriteLine ("usage: mono tlsaync.exe url1 [url ...]");
96 } else if (args.Length > 64) {
97 Console.WriteLine ("WaitHandle has a limit of 64 handles so you cannot process {0} URLs.", args.Length);
101 alone = (args.Length == 1);
102 ServicePointManager.CertificatePolicy = new TestCertificatePolicy ();
105 foreach (string url in args) {
106 Console.WriteLine ("GET #{0} at {1}", id, url);
107 HttpWebRequest wreq = (HttpWebRequest) WebRequest.Create (url);
108 State s = new State (id++, wreq);
109 wreq.BeginGetResponse (new AsyncCallback (ResponseCallback), s);
115 private static void ResponseCallback (IAsyncResult result)
117 State state = ((State) result.AsyncState);
118 HttpWebResponse response = (HttpWebResponse) state.Request.EndGetResponse (result);
119 state.Stream = response.GetResponseStream ();
120 state.Stream.BeginRead (state.Buffer, 0, state.Buffer.Length, new AsyncCallback (StreamCallBack), state);
123 private static void StreamCallBack (IAsyncResult result)
125 State state = ((State) result.AsyncState);
126 int length = state.Stream.EndRead (result);
128 state.Memory.Write (state.Buffer, 0, length);
129 state.Stream.BeginRead (state.Buffer, 0, state.Buffer.Length, new AsyncCallback (StreamCallBack), state);
131 state.Stream.Close ();
133 state.Memory.Position = 0;
134 StreamReader sr = new StreamReader (state.Memory, Encoding.UTF8);
135 Console.WriteLine (sr.ReadToEnd ());
137 Console.WriteLine ("END #{0}", state.Id);
142 public class TestCertificatePolicy : ICertificatePolicy {
144 public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate, WebRequest request, int error)
146 // whatever the reason we do not stop the SSL connection