80c98d81f320e38703bb99b6e26a09e1d413b35f
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / HttpWebClientProtocol.cs
1 // \r
2 // System.Web.Services.Protocols.HttpWebClientProtocol.cs\r
3 //\r
4 // Author:\r
5 //   Tim Coleman (tim@timcoleman.com)\r
6 //\r
7 // Copyright (C) Tim Coleman, 2002\r
8 //\r
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 \r
31 using System;\r
32 using System.ComponentModel;\r
33 using System.Net;\r
34 using System.Security.Cryptography.X509Certificates;\r
35 using System.Threading;\r
36 using System.Web.Services;\r
37 using System.Collections;\r
38 \r
39 namespace System.Web.Services.Protocols {\r
40         public abstract class HttpWebClientProtocol : WebClientProtocol {\r
41 \r
42                 #region Fields\r
43 \r
44                 bool allowAutoRedirect;\r
45                 X509CertificateCollection clientCertificates;\r
46                 CookieContainer cookieContainer;\r
47                 IWebProxy proxy;\r
48                 string userAgent;\r
49                 \r
50 #if NET_1_1\r
51                 bool _unsafeAuthenticated;\r
52 #endif\r
53                 #endregion\r
54 \r
55                 #region Constructors\r
56 \r
57                 protected HttpWebClientProtocol () \r
58                 {\r
59                         allowAutoRedirect = false;\r
60                         clientCertificates = null;\r
61                         cookieContainer = null;\r
62                         proxy = null; // FIXME\r
63                         userAgent = String.Format ("Mono Web Services Client Protocol {0}", Environment.Version);\r
64                 }\r
65                 \r
66                 #endregion // Constructors\r
67 \r
68                 #region Properties\r
69 \r
70                 [DefaultValue (false)]\r
71                 [WebServicesDescription ("Enable automatic handling of server redirects.")]\r
72                 public bool AllowAutoRedirect {\r
73                         get { return allowAutoRedirect; }\r
74                         set { allowAutoRedirect = value; }\r
75                 }\r
76 \r
77                 [Browsable (false)]\r
78                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]\r
79                 [WebServicesDescription ("The client certificates that will be sent to the server, if the server requests them.")]\r
80                 public X509CertificateCollection ClientCertificates {\r
81                         get {\r
82                                 if (clientCertificates == null)\r
83                                         clientCertificates = new X509CertificateCollection ();\r
84                                 return clientCertificates;\r
85                         }\r
86                 }\r
87 \r
88                 [DefaultValue (null)]\r
89                 [WebServicesDescription ("A container for all cookies received from servers in the current session.")]\r
90                 public CookieContainer CookieContainer {\r
91                         get { return cookieContainer; }\r
92                         set { cookieContainer = value; }\r
93                 }\r
94 \r
95                 [Browsable (false)]\r
96                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]\r
97                 public IWebProxy Proxy {\r
98                         get { return proxy; }\r
99                         set { proxy = value; }\r
100                 }\r
101 \r
102                 [WebServicesDescription ("Sets the user agent http header for the request.")]\r
103                 public string UserAgent {\r
104                         get { return userAgent; }\r
105                         set { userAgent = value; }\r
106                 }\r
107                 \r
108 #if NET_1_1\r
109                 public bool UnsafeAuthenticatedConnectionSharing\r
110                 {\r
111                         get { return _unsafeAuthenticated; }\r
112                         set { _unsafeAuthenticated = value; }\r
113                 }\r
114 #endif\r
115 \r
116                 #endregion // Properties\r
117 \r
118                 #region Methods\r
119 \r
120                 internal virtual void CheckForCookies (HttpWebResponse response)\r
121                 {\r
122                         CookieCollection cookies = response.Cookies;\r
123                         if (cookieContainer == null || cookies.Count == 0)\r
124                                 return;\r
125 \r
126                         CookieCollection coll = cookieContainer.GetCookies (uri);\r
127                         foreach (Cookie c in cookies) {\r
128                                 bool add = true;\r
129                                 foreach (Cookie prev in coll) {\r
130                                         if (c.Equals (prev)) {\r
131                                                 add = false;\r
132                                                 break;\r
133                                         }\r
134                                 }\r
135                                 if (add)\r
136                                         cookieContainer.Add (c);\r
137                         }\r
138                 }\r
139                 \r
140                 protected override WebRequest GetWebRequest (Uri uri)\r
141                 {\r
142                         WebRequest req = base.GetWebRequest (uri);\r
143                         HttpWebRequest request = req as HttpWebRequest;\r
144                         if (request == null)\r
145                                 return req;\r
146 \r
147                         request.AllowAutoRedirect = allowAutoRedirect;\r
148                         if (clientCertificates != null)\r
149                                 request.ClientCertificates.AddRange (clientCertificates);\r
150 \r
151                         request.CookieContainer = cookieContainer;\r
152                         if (proxy != null)\r
153                                 request.Proxy = proxy;\r
154 \r
155                         request.UserAgent = userAgent;\r
156 \r
157 #if NET_1_1\r
158                 //      request.UnsafeAuthenticatedConnectionSharing = _unsafeAuthenticated;\r
159 #endif\r
160 \r
161                         return request;\r
162                 }\r
163 \r
164                 protected override WebResponse GetWebResponse (WebRequest request)\r
165                 {\r
166                         WebResponse response = base.GetWebResponse (request);\r
167                         HttpWebResponse wr = response as HttpWebResponse;\r
168                         if (wr != null)\r
169                                 CheckForCookies (wr);\r
170                                 \r
171                         return response;\r
172                 }\r
173 \r
174                 protected override WebResponse GetWebResponse (WebRequest request, IAsyncResult result)\r
175                 {\r
176                         WebResponse response = base.GetWebResponse (request, result);\r
177                         HttpWebResponse wr = response as HttpWebResponse;\r
178                         if (wr != null)\r
179                                 CheckForCookies (wr);\r
180                                 \r
181                         return response;\r
182                 }\r
183                 \r
184 #if NET_2_0\r
185 \r
186                 [MonoTODO]\r
187                 protected void CancelAsync (object userState)\r
188                 {\r
189                         throw new NotImplementedException ();\r
190                 }\r
191 \r
192                 [MonoTODO]\r
193                 public static bool GenerateXmlMappings (Type type, ArrayList mapping)\r
194                 {\r
195                         throw new NotImplementedException ();\r
196                 }\r
197 \r
198                 [MonoTODO]\r
199                 public static Hashtable GenerateXmlMappings (Type[] types, ArrayList mapping)\r
200                 {\r
201                         throw new NotImplementedException ();\r
202                 }\r
203 #endif\r
204 \r
205                 #endregion // Methods\r
206         }\r
207         \r
208 #if NET_2_0\r
209         internal class InvokeAsyncInfo\r
210         {\r
211                 public SynchronizationContext Context;\r
212                 public object UserState;\r
213                 public SendOrPostCallback Callback;\r
214                 \r
215                 public InvokeAsyncInfo (SendOrPostCallback callback, object userState)\r
216                 {\r
217                         Callback = callback;\r
218                         UserState = userState;\r
219                         Context = SynchronizationContext.Current;\r
220                 }\r
221         }\r
222 #endif\r
223 }\r