Merge pull request #649 from DavidS/feature/implement-additional-reference-path
[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
46                 public WebRequestHandler ()
47                 {
48                         allowPipelining = true;
49                         authenticationLevel = AuthenticationLevel.MutualAuthRequested;
50                         cachePolicy = System.Net.WebRequest.DefaultCachePolicy;
51                         continueTimeout = TimeSpan.FromMilliseconds (350);
52                         impersonationLevel = TokenImpersonationLevel.Delegation;
53                         maxResponseHeadersLength = HttpWebRequest.DefaultMaximumResponseHeadersLength;
54                         readWriteTimeout = 300000;
55                         serverCertificateValidationCallback = null;
56                         unsafeAuthenticatedConnectionSharing = false;
57                 }
58
59                 public bool AllowPipelining {
60                         get { return allowPipelining; }
61                         set {
62                                 EnsureModifiability ();
63                                 allowPipelining = value;
64                         }
65                 }
66
67                 public RequestCachePolicy CachePolicy {
68                         get { return cachePolicy; }
69                         set {
70                                 EnsureModifiability ();
71                                 cachePolicy = value;
72                         }
73                 }
74
75                 public AuthenticationLevel AuthenticationLevel {
76                         get { return authenticationLevel; }
77                         set {
78                                 EnsureModifiability ();
79                                 authenticationLevel = value;
80                         }
81                 }
82
83                 [MonoTODO]
84                 public X509CertificateCollection ClientCertificates {
85                         get { throw new NotImplementedException (); }
86                 }
87
88                 [MonoTODO]
89                 public TimeSpan ContinueTimeout {
90                         get { return continueTimeout; }
91                         set {
92                                 EnsureModifiability ();
93                                 continueTimeout = value;
94                         }
95                 }
96
97                 public TokenImpersonationLevel ImpersonationLevel {
98                         get { return impersonationLevel; }
99                         set {
100                                 EnsureModifiability ();
101                                 impersonationLevel = value;
102                         }
103                 }
104
105                 public int MaxResponseHeadersLength {
106                         get { return maxResponseHeadersLength; }
107                         set {
108                                 EnsureModifiability ();
109                                 maxResponseHeadersLength = value;
110                         }
111                 }
112
113                 public int ReadWriteTimeout {
114                         get { return readWriteTimeout; }
115                         set {
116                                 EnsureModifiability ();
117                                 readWriteTimeout = value;
118                         }
119                 }
120
121                 [MonoTODO]
122                 public RemoteCertificateValidationCallback ServerCertificateValidationCallback {
123                         get { return serverCertificateValidationCallback; }
124                         set {
125                                 EnsureModifiability ();
126                                 serverCertificateValidationCallback = value;
127                         }
128                 }
129
130                 public bool UnsafeAuthenticatedConnectionSharing {
131                         get { return unsafeAuthenticatedConnectionSharing; }
132                         set {
133                                 EnsureModifiability ();
134                                 unsafeAuthenticatedConnectionSharing = value;
135                         }
136                 }
137
138                 internal override HttpWebRequest CreateWebRequest (HttpRequestMessage request)
139                 {
140                         HttpWebRequest wr = base.CreateWebRequest (request);
141
142                         wr.Pipelined = allowPipelining;
143                         wr.AuthenticationLevel = authenticationLevel;
144                         wr.CachePolicy = cachePolicy;
145                         wr.ImpersonationLevel = impersonationLevel;
146                         wr.MaximumResponseHeadersLength = maxResponseHeadersLength;
147                         wr.ReadWriteTimeout = readWriteTimeout;
148                         wr.UnsafeAuthenticatedConnectionSharing = unsafeAuthenticatedConnectionSharing;
149
150                         return wr;
151                 }
152         }
153 }
154