* HttpWebClientProtocol.cs: Add received cookies to cookieContainer when
[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 \r
38 namespace System.Web.Services.Protocols {\r
39         public abstract class HttpWebClientProtocol : WebClientProtocol {\r
40 \r
41                 #region Fields\r
42 \r
43                 bool allowAutoRedirect;\r
44                 X509CertificateCollection clientCertificates;\r
45                 CookieContainer cookieContainer;\r
46                 IWebProxy proxy;\r
47                 string userAgent;\r
48                 \r
49 #if NET_1_1\r
50                 bool _unsafeAuthenticated;\r
51 #endif\r
52                 #endregion\r
53 \r
54                 #region Constructors\r
55 \r
56                 protected HttpWebClientProtocol () \r
57                 {\r
58                         allowAutoRedirect = false;\r
59                         clientCertificates = null;\r
60                         cookieContainer = null;\r
61                         proxy = null; // FIXME\r
62                         userAgent = String.Format ("Mono Web Services Client Protocol {0}", Environment.Version);\r
63                 }\r
64                 \r
65                 #endregion // Constructors\r
66 \r
67                 #region Properties\r
68 \r
69                 [DefaultValue (false)]\r
70                 [WebServicesDescription ("Enable automatic handling of server redirects.")]\r
71                 public bool AllowAutoRedirect {\r
72                         get { return allowAutoRedirect; }\r
73                         set { allowAutoRedirect = value; }\r
74                 }\r
75 \r
76                 [Browsable (false)]\r
77                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]\r
78                 [WebServicesDescription ("The client certificates that will be sent to the server, if the server requests them.")]\r
79                 public X509CertificateCollection ClientCertificates {\r
80                         get {\r
81                                 if (clientCertificates == null)\r
82                                         clientCertificates = new X509CertificateCollection ();\r
83                                 return clientCertificates;\r
84                         }\r
85                 }\r
86 \r
87                 [DefaultValue (null)]\r
88                 [WebServicesDescription ("A container for all cookies received from servers in the current session.")]\r
89                 public CookieContainer CookieContainer {\r
90                         get { return cookieContainer; }\r
91                         set { cookieContainer = value; }\r
92                 }\r
93 \r
94                 [Browsable (false)]\r
95                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]\r
96                 public IWebProxy Proxy {\r
97                         get { return proxy; }\r
98                         set { proxy = value; }\r
99                 }\r
100 \r
101                 [WebServicesDescription ("Sets the user agent http header for the request.")]\r
102                 public string UserAgent {\r
103                         get { return userAgent; }\r
104                         set { userAgent = value; }\r
105                 }\r
106                 \r
107 #if NET_1_1\r
108                 public bool UnsafeAuthenticatedConnectionSharing\r
109                 {\r
110                         get { return _unsafeAuthenticated; }\r
111                         set { _unsafeAuthenticated = value; }\r
112                 }\r
113 #endif\r
114 \r
115                 #endregion // Properties\r
116 \r
117                 #region Methods\r
118 \r
119                 internal virtual void CheckForCookies (HttpWebResponse response)\r
120                 {\r
121                         CookieCollection cookies = response.Cookies;\r
122                         if (cookieContainer == null || cookies.Count == 0)\r
123                                 return;\r
124 \r
125                         CookieCollection coll = cookieContainer.GetCookies (uri);\r
126                         foreach (Cookie c in cookies) {\r
127                                 bool add = true;\r
128                                 foreach (Cookie prev in coll) {\r
129                                         if (c.Equals (prev)) {\r
130                                                 add = false;\r
131                                                 break;\r
132                                         }\r
133                                 }\r
134                                 if (add)\r
135                                         cookieContainer.Add (c);\r
136                         }\r
137                 }\r
138                 \r
139                 protected override WebRequest GetWebRequest (Uri uri)\r
140                 {\r
141                         WebRequest req = base.GetWebRequest (uri);\r
142                         HttpWebRequest request = req as HttpWebRequest;\r
143                         if (request == null)\r
144                                 return req;\r
145 \r
146                         request.AllowAutoRedirect = allowAutoRedirect;\r
147                         if (clientCertificates != null)\r
148                                 request.ClientCertificates.AddRange (clientCertificates);\r
149 \r
150                         request.CookieContainer = cookieContainer;\r
151                         if (proxy != null)\r
152                                 request.Proxy = proxy;\r
153 \r
154                         request.UserAgent = userAgent;\r
155 \r
156 #if NET_1_1\r
157                 //      request.UnsafeAuthenticatedConnectionSharing = _unsafeAuthenticated;\r
158 #endif\r
159 \r
160                         return request;\r
161                 }\r
162 \r
163                 protected override WebResponse GetWebResponse (WebRequest request)\r
164                 {\r
165                         WebResponse response = base.GetWebResponse (request);\r
166                         HttpWebResponse wr = response as HttpWebResponse;\r
167                         if (wr != null)\r
168                                 CheckForCookies (wr);\r
169                                 \r
170                         return response;\r
171                 }\r
172 \r
173                 protected override WebResponse GetWebResponse (WebRequest request, IAsyncResult result)\r
174                 {\r
175                         WebResponse response = base.GetWebResponse (request, result);\r
176                         HttpWebResponse wr = response as HttpWebResponse;\r
177                         if (wr != null)\r
178                                 CheckForCookies (wr);\r
179                                 \r
180                         return response;\r
181                 }\r
182 \r
183                 #endregion // Methods\r
184         }\r
185 }\r