2 // tlssave.cs: Multi-sessions TLS/SSL Test Program which saves the URL to disk
3 // based on tlstest.cs, tlsmulti.cs and tlsasync.cs
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2004-2005 Novell (http://www.novell.com)
12 using System.Collections;
13 using System.Diagnostics;
14 using System.Globalization;
17 using System.Net.Sockets;
18 using System.Reflection;
19 using System.Security.Cryptography.X509Certificates;
21 using System.Threading;
23 using Mono.Security.Protocol.Tls;
27 static ArrayList handleList = new ArrayList ();
30 private HttpWebRequest request;
31 private ManualResetEvent handle;
32 private Stream stream;
33 private byte[] buffer;
34 private FileStream file;
36 public State (int id, HttpWebRequest req)
40 handle = new ManualResetEvent (false);
41 handleList.Add (handle);
48 public HttpWebRequest Request {
49 get { return request; }
52 public Stream Stream {
53 get { return stream; }
54 set { stream = value; }
57 public byte[] Buffer {
60 buffer = new byte [256]; // really small on purpose
68 file = new FileStream (id.ToString (), FileMode.Create);
73 public void Complete ()
80 static public void WaitAll ()
82 if (handleList.Count > 0) {
83 WaitHandle[] handles = (WaitHandle[]) handleList.ToArray (typeof (WaitHandle));
84 WaitHandle.WaitAll (handles);
90 public class SaveTest {
92 public static void Main (string[] args)
94 if (args.Length == 0) {
95 Console.WriteLine ("usage: mono tlssave.exe url1 [url ...]");
97 } else if (args.Length > 64) {
98 Console.WriteLine ("WaitHandle has a limit of 64 handles so you cannot process {0} URLs.", args.Length);
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.File.Write (state.Buffer, 0, length);
129 state.Stream.BeginRead (state.Buffer, 0, state.Buffer.Length, new AsyncCallback (StreamCallBack), state);
131 state.Stream.Close ();
132 Console.WriteLine ("END #{0}", state.Id);
137 public class TestCertificatePolicy : ICertificatePolicy {
139 public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate, WebRequest request, int error)
141 // whatever the reason we do not stop the SSL connection