more executable bits
[mono.git] / mcs / class / Mono.Security / Test / tools / tlstest / tlssave.cs
1 //
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
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2004-2005 Novell (http://www.novell.com)
9 //
10
11 using System;
12 using System.Collections;
13 using System.Diagnostics;
14 using System.Globalization;
15 using System.IO;
16 using System.Net;
17 using System.Net.Sockets;
18 using System.Reflection;
19 using System.Security.Cryptography.X509Certificates;
20 using System.Text;
21 using System.Threading;
22
23 using Mono.Security.Protocol.Tls;
24
25 public class State {
26
27         static ArrayList handleList = new ArrayList ();
28
29         private int id;
30         private HttpWebRequest request;
31         private ManualResetEvent handle;
32         private Stream stream;
33         private byte[] buffer;
34         private FileStream file;
35
36         public State (int id, HttpWebRequest req)
37         {
38                 this.id = id;
39                 request = req;
40                 handle = new ManualResetEvent (false);
41                 handleList.Add (handle);
42         }
43
44         public int Id {
45                 get { return id; }
46         }
47
48         public HttpWebRequest Request {
49                 get { return request; }
50         }
51
52         public Stream Stream {
53                 get { return stream; }
54                 set { stream = value; }
55         }
56
57         public byte[] Buffer {
58                 get {
59                         if (buffer == null)
60                                 buffer = new byte [256]; // really small on purpose
61                         return buffer;
62                 }
63         }
64
65         public Stream File {
66                 get {
67                         if (file == null)
68                                 file = new FileStream (id.ToString (), FileMode.Create);
69                         return file;
70                 }
71         }
72
73         public void Complete ()
74         {
75                 if (file != null)
76                         file.Close ();
77                 handle.Set ();
78         }
79
80         static public void WaitAll ()
81         {
82                 if (handleList.Count > 0) {
83                         WaitHandle[] handles = (WaitHandle[]) handleList.ToArray (typeof (WaitHandle));
84                         WaitHandle.WaitAll (handles);
85                         handleList.Clear ();
86                 }
87         }
88 }
89
90 public class SaveTest {
91
92         public static void Main (string[] args) 
93         {
94                 if (args.Length == 0) {
95                         Console.WriteLine ("usage: mono tlssave.exe url1 [url ...]");
96                         return;
97                 } else if (args.Length > 64) {
98                         Console.WriteLine ("WaitHandle has a limit of 64 handles so you cannot process {0} URLs.", args.Length);
99                         return;
100                 }
101
102                 ServicePointManager.CertificatePolicy = new TestCertificatePolicy ();
103
104                 int id = 1;
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);
110                 }
111
112                 State.WaitAll ();
113         }
114
115         private static void ResponseCallback (IAsyncResult result)
116         {
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);
121         }
122
123         private static void StreamCallBack (IAsyncResult result)
124         {
125                 State state = ((State) result.AsyncState);
126                 int length = state.Stream.EndRead (result);
127                 if (length > 0) {
128                         state.File.Write (state.Buffer, 0, length);
129                         state.Stream.BeginRead (state.Buffer, 0, state.Buffer.Length, new AsyncCallback (StreamCallBack), state);
130                 } else {
131                         state.Stream.Close ();
132                         Console.WriteLine ("END #{0}", state.Id);
133                         state.Complete ();
134                 }
135         }
136
137         public class TestCertificatePolicy : ICertificatePolicy {
138
139                 public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate, WebRequest request, int error)
140                 {
141                         // whatever the reason we do not stop the SSL connection
142                         return true;
143                 }
144         }
145 }