updated client certificates to handle non-manual case
[mono.git] / mcs / class / System.Net.Http.WebRequest / System.Net.Http.WebRequest / WebRequestHandler.cs
1 //
2 // WebRequestHandler.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2013 Xamarin Inc. (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26 using System;
27 using System.Net.Cache;
28 using System.Net.Security;
29 using System.Security.Principal;
30 using System.Security.Cryptography.X509Certificates;
31
32 namespace System.Net.Http
33 {
34         public class WebRequestHandler : HttpClientHandler
35         {
36                 bool allowPipelining;
37                 RequestCachePolicy cachePolicy;
38                 AuthenticationLevel authenticationLevel;
39                 TimeSpan continueTimeout;
40                 TokenImpersonationLevel impersonationLevel;
41                 int maxResponseHeadersLength;
42                 int readWriteTimeout;
43                 RemoteCertificateValidationCallback serverCertificateValidationCallback;
44                 bool unsafeAuthenticatedConnectionSharing;
45                 X509CertificateCollection clientCertificates;
46
47                 public WebRequestHandler ()
48                 {
49                         allowPipelining = true;
50                         authenticationLevel = AuthenticationLevel.MutualAuthRequested;
51                         cachePolicy = System.Net.WebRequest.DefaultCachePolicy;
52                         continueTimeout = TimeSpan.FromMilliseconds (350);
53                         impersonationLevel = TokenImpersonationLevel.Delegation;
54                         maxResponseHeadersLength = HttpWebRequest.DefaultMaximumResponseHeadersLength;
55                         readWriteTimeout = 300000;
56                         serverCertificateValidationCallback = null;
57                         unsafeAuthenticatedConnectionSharing = false;
58                         clientCertificates = new X509CertificateCollection();
59                 }
60
61                 public bool AllowPipelining {
62                         get { return allowPipelining; }
63                         set {
64                                 EnsureModifiability ();
65                                 allowPipelining = value;
66                         }
67                 }
68
69                 public RequestCachePolicy CachePolicy {
70                         get { return cachePolicy; }
71                         set {
72                                 EnsureModifiability ();
73                                 cachePolicy = value;
74                         }
75                 }
76
77                 public AuthenticationLevel AuthenticationLevel {
78                         get { return authenticationLevel; }
79                         set {
80                                 EnsureModifiability ();
81                                 authenticationLevel = value;
82                         }
83                 }
84
85                 public X509CertificateCollection ClientCertificates {
86                         get {
87                                 if (this.ClientCertificateOptions != ClientCertificateOption.Manual) {
88                                         throw new InvalidOperationException("The ClientCertificateOptions property must be set to 'Manual' to use this property.");
89                                 }
90                                 
91                                 return clientCertificates;
92                         }
93                 }
94
95                 [MonoTODO]
96                 public TimeSpan ContinueTimeout {
97                         get { return continueTimeout; }
98                         set {
99                                 EnsureModifiability ();
100                                 continueTimeout = value;
101                         }
102                 }
103
104                 public TokenImpersonationLevel ImpersonationLevel {
105                         get { return impersonationLevel; }
106                         set {
107                                 EnsureModifiability ();
108                                 impersonationLevel = value;
109                         }
110                 }
111
112                 public int MaxResponseHeadersLength {
113                         get { return maxResponseHeadersLength; }
114                         set {
115                                 EnsureModifiability ();
116                                 maxResponseHeadersLength = value;
117                         }
118                 }
119
120                 public int ReadWriteTimeout {
121                         get { return readWriteTimeout; }
122                         set {
123                                 EnsureModifiability ();
124                                 readWriteTimeout = value;
125                         }
126                 }
127
128                 public RemoteCertificateValidationCallback ServerCertificateValidationCallback {
129                         get { return serverCertificateValidationCallback; }
130                         set {
131                                 EnsureModifiability ();
132                                 serverCertificateValidationCallback = value;
133                         }
134                 }
135
136                 public bool UnsafeAuthenticatedConnectionSharing {
137                         get { return unsafeAuthenticatedConnectionSharing; }
138                         set {
139                                 EnsureModifiability ();
140                                 unsafeAuthenticatedConnectionSharing = value;
141                         }
142                 }
143
144                 internal override HttpWebRequest CreateWebRequest (HttpRequestMessage request)
145                 {
146                         HttpWebRequest wr = base.CreateWebRequest (request);
147
148                         wr.Pipelined = allowPipelining;
149                         wr.AuthenticationLevel = authenticationLevel;
150                         wr.CachePolicy = cachePolicy;
151                         wr.ImpersonationLevel = impersonationLevel;
152                         wr.MaximumResponseHeadersLength = maxResponseHeadersLength;
153                         wr.ReadWriteTimeout = readWriteTimeout;
154                         wr.UnsafeAuthenticatedConnectionSharing = unsafeAuthenticatedConnectionSharing;
155                         wr.ServerCertificateValidationCallback = serverCertificateValidationCallback;
156                         
157                         if (this.ClientCertificateOptions == ClientCertificateOption.Manual) {
158                                 wr.ClientCertificates = clientCertificates;
159                         }
160
161                         return wr;
162                 }
163         }
164 }
165