* HttpWebClientProtocol.cs, Soap12FaultCodes.cs, SoapClientMessage.cs,
[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                 CookieCollection prevCookies;\r
50                 \r
51 #if NET_1_1\r
52                 bool _unsafeAuthenticated;\r
53 #endif\r
54                 #endregion\r
55 \r
56                 #region Constructors\r
57 \r
58                 protected HttpWebClientProtocol () \r
59                 {\r
60                         allowAutoRedirect = false;\r
61                         clientCertificates = null;\r
62                         cookieContainer = null;\r
63                         proxy = null; // FIXME\r
64                         userAgent = String.Format ("Mono Web Services Client Protocol {0}", Environment.Version);\r
65                 }\r
66                 \r
67                 #endregion // Constructors\r
68 \r
69                 #region Properties\r
70 \r
71                 [DefaultValue (false)]\r
72                 [WebServicesDescription ("Enable automatic handling of server redirects.")]\r
73                 public bool AllowAutoRedirect {\r
74                         get { return allowAutoRedirect; }\r
75                         set { allowAutoRedirect = value; }\r
76                 }\r
77 \r
78                 [Browsable (false)]\r
79                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]\r
80                 [WebServicesDescription ("The client certificates that will be sent to the server, if the server requests them.")]\r
81                 public X509CertificateCollection ClientCertificates {\r
82                         get {\r
83                                 if (clientCertificates == null)\r
84                                         clientCertificates = new X509CertificateCollection ();\r
85                                 return clientCertificates;\r
86                         }\r
87                 }\r
88 \r
89                 [DefaultValue (null)]\r
90                 [WebServicesDescription ("A container for all cookies received from servers in the current session.")]\r
91                 public CookieContainer CookieContainer {\r
92                         get { return cookieContainer; }\r
93                         set { cookieContainer = value; }\r
94                 }\r
95 \r
96                 [Browsable (false)]\r
97                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]\r
98                 public IWebProxy Proxy {\r
99                         get { return proxy; }\r
100                         set { proxy = value; }\r
101                 }\r
102 \r
103                 [WebServicesDescription ("Sets the user agent http header for the request.")]\r
104                 public string UserAgent {\r
105                         get { return userAgent; }\r
106                         set { userAgent = value; }\r
107                 }\r
108                 \r
109 #if NET_1_1\r
110                 public bool UnsafeAuthenticatedConnectionSharing\r
111                 {\r
112                         get { return _unsafeAuthenticated; }\r
113                         set { _unsafeAuthenticated = value; }\r
114                 }\r
115 #endif\r
116 \r
117                 #endregion // Properties\r
118 \r
119                 #region Methods\r
120 \r
121                 internal virtual void AddCookies (Uri uri)\r
122                 {\r
123                         if (cookieContainer == null)\r
124                                 cookieContainer = new CookieContainer ();\r
125 \r
126                         if (prevCookies == null || prevCookies.Count == 0)\r
127                                 return;\r
128 \r
129                         CookieCollection coll = cookieContainer.GetCookies (uri);\r
130                         foreach (Cookie prev in prevCookies) {\r
131                                 bool dont = false;\r
132                                 foreach (Cookie c in coll) {\r
133                                         if (c.Equals (prev)) {\r
134                                                 dont = true;\r
135                                                 break;\r
136                                         }\r
137                                 }\r
138 \r
139                                 if (dont == false)\r
140                                         cookieContainer.Add (prev);\r
141                         }\r
142                 }\r
143 \r
144                 internal virtual void CheckForCookies (HttpWebResponse response)\r
145                 {\r
146                         CookieCollection cookies = response.Cookies;\r
147                         if (cookies.Count == 0)\r
148                                 return;\r
149 \r
150                         if (prevCookies == null)\r
151                                 prevCookies = new CookieCollection ();\r
152 \r
153                         foreach (Cookie c in cookies)\r
154                                 prevCookies.Add (c);\r
155                 }\r
156                 \r
157                 protected override WebRequest GetWebRequest (Uri uri)\r
158                 {\r
159                         WebRequest req = base.GetWebRequest (uri);\r
160                         HttpWebRequest request = req as HttpWebRequest;\r
161                         if (request == null)\r
162                                 return req;\r
163 \r
164                         request.AllowAutoRedirect = allowAutoRedirect;\r
165                         if (clientCertificates != null)\r
166                                 request.ClientCertificates.AddRange (clientCertificates);\r
167 \r
168                         AddCookies (uri);\r
169                         request.CookieContainer = cookieContainer;\r
170                         if (proxy != null)\r
171                                 request.Proxy = proxy;\r
172 \r
173                         request.UserAgent = userAgent;\r
174 \r
175 #if NET_1_1\r
176                 //      request.UnsafeAuthenticatedConnectionSharing = _unsafeAuthenticated;\r
177 #endif\r
178 \r
179                         return request;\r
180                 }\r
181 \r
182                 protected override WebResponse GetWebResponse (WebRequest request)\r
183                 {\r
184                         WebResponse response = base.GetWebResponse (request);\r
185                         HttpWebResponse wr = response as HttpWebResponse;\r
186                         if (wr != null)\r
187                                 CheckForCookies (wr);\r
188                                 \r
189                         return response;\r
190                 }\r
191 \r
192                 protected override WebResponse GetWebResponse (WebRequest request, IAsyncResult result)\r
193                 {\r
194                         WebResponse response = base.GetWebResponse (request, result);\r
195                         HttpWebResponse wr = response as HttpWebResponse;\r
196                         if (wr != null)\r
197                                 CheckForCookies (wr);\r
198                                 \r
199                         return response;\r
200                 }\r
201                 \r
202 #if NET_2_0\r
203 \r
204                 [MonoTODO]\r
205                 protected void CancelAsync (object userState)\r
206                 {\r
207                         throw new NotImplementedException ();\r
208                 }\r
209 \r
210                 [MonoTODO]\r
211                 public static bool GenerateXmlMappings (Type type, ArrayList mapping)\r
212                 {\r
213                         throw new NotImplementedException ();\r
214                 }\r
215 \r
216                 [MonoTODO]\r
217                 public static Hashtable GenerateXmlMappings (Type[] types, ArrayList mapping)\r
218                 {\r
219                         throw new NotImplementedException ();\r
220                 }\r
221 #endif\r
222 \r
223                 #endregion // Methods\r
224         }\r
225 }\r