bb4393d9e419c1aa91bf1830c15284f978ae46e6
[mono.git] / mcs / class / System / System.Net / HttpWebResponse.cs
1 //
2 // System.Net.HttpWebResponse
3 //
4 // Authors:
5 //      Lawrence Pit (loz@cable.a2000.nl)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (c) 2002 Lawrence Pit
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 //
11
12 using System;
13 using System.IO;
14 using System.Net.Sockets;
15 using System.Runtime.Serialization;
16 using System.Text;
17
18 namespace System.Net 
19 {
20         [Serializable]
21         public class HttpWebResponse : WebResponse, ISerializable, IDisposable
22         {
23                 Uri uri;
24                 WebHeaderCollection webHeaders;
25                 CookieCollection cookieCollection;
26                 string method;
27                 Version version;
28                 HttpStatusCode statusCode;
29                 string statusDescription;
30                 long contentLength = -1;
31                 string contentType;
32
33                 bool disposed = false;
34                 WebConnectionStream stream;
35                 
36                 // Constructors
37                 
38                 internal HttpWebResponse (Uri uri, string method, WebConnectionData data, bool cookiesSet)
39                 {
40                         this.uri = uri;
41                         this.method = method;
42                         webHeaders = data.Headers;
43                         version = data.Version;
44                         statusCode = (HttpStatusCode) data.StatusCode;
45                         statusDescription = data.StatusDescription;
46                         stream = data.stream;
47                         if (cookiesSet) {
48                                 FillCookies ();
49                         } else if (webHeaders != null) {
50                                 webHeaders.RemoveInternal ("Set-Cookie");
51                                 webHeaders.RemoveInternal ("Set-Cookie2");
52                         }
53                 }
54
55                 protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
56                 {
57                         SerializationInfo info = serializationInfo;
58
59                         uri = (Uri) info.GetValue ("uri", typeof (Uri));
60                         contentLength = info.GetInt64 ("contentLength");
61                         contentType = info.GetString ("contentType");
62                         method = info.GetString ("method");
63                         statusDescription = info.GetString ("statusDescription");
64                         cookieCollection = (CookieCollection) info.GetValue ("cookieCollection", typeof (CookieCollection));
65                         version = (Version) info.GetValue ("version", typeof (Version));
66                         statusCode = (HttpStatusCode) info.GetValue ("statusCode", typeof (HttpStatusCode));
67                 }
68                 
69                 // Properties
70                 
71                 public string CharacterSet {
72                         // Content-Type   = "Content-Type" ":" media-type
73                         // media-type     = type "/" subtype *( ";" parameter )
74                         // parameter      = attribute "=" value
75                         // 3.7.1. default is ISO-8859-1
76                         get { 
77                                 CheckDisposed ();
78                                 string contentType = ContentType;
79                                 if (contentType == null)
80                                         return "ISO-8859-1";
81                                 string val = contentType.ToLower ();                                    
82                                 int pos = val.IndexOf ("charset=");
83                                 if (pos == -1)
84                                         return "ISO-8859-1";
85                                 pos += 8;
86                                 int pos2 = val.IndexOf (';', pos);
87                                 return (pos2 == -1)
88                                      ? contentType.Substring (pos) 
89                                      : contentType.Substring (pos, pos2 - pos);
90                         }
91                 }
92                 
93                 public string ContentEncoding {
94                         get { 
95                                 CheckDisposed ();
96                                 return webHeaders ["Content-Encoding"];
97                         }
98                 }
99                 
100                 public override long ContentLength {            
101                         get {
102                                 CheckDisposed ();
103                                 if (contentLength != -1)
104                                         return contentLength;
105
106                                 try {
107                                         contentLength = (long) UInt64.Parse (webHeaders ["Content-Length"]); 
108                                 } catch (Exception) {
109                                         return -1;
110                                 }
111
112                                 return contentLength;
113                         }
114                 }
115                 
116                 public override string ContentType {            
117                         get {
118                                 CheckDisposed ();
119                                 if (contentType == null)
120                                         contentType = webHeaders ["Content-Type"];
121
122                                 return contentType;
123                         }
124                 }
125                 
126                 public CookieCollection Cookies {
127                         get { 
128                                 CheckDisposed ();
129                                 
130                                 if (cookieCollection == null)
131                                         cookieCollection = new CookieCollection ();
132                                 return cookieCollection;
133                         }
134                         set {
135                                 CheckDisposed ();
136                                 cookieCollection = value;
137                         }
138                 }
139                 
140                 public override WebHeaderCollection Headers {           
141                         get { 
142                                 CheckDisposed ();
143                                 return webHeaders; 
144                         }
145                 }
146                 
147                 public DateTime LastModified {
148                         get {
149                                 CheckDisposed ();
150                                 try {
151                                         string dtStr = webHeaders ["Last-Modified"];
152                                         return MonoHttpDate.Parse (dtStr);
153                                 } catch (Exception) {
154                                         return DateTime.Now;    
155                                 }
156                         }
157                 }
158                 
159                 public string Method {
160                         get { 
161                                 CheckDisposed ();
162                                 return method; 
163                         }
164                 }
165                 
166                 public Version ProtocolVersion {
167                         get { 
168                                 CheckDisposed ();
169                                 return version; 
170                         }
171                 }
172                 
173                 public override Uri ResponseUri {               
174                         get { 
175                                 CheckDisposed ();
176                                 return uri; 
177                         }
178                 }               
179                 
180                 public string Server {
181                         get { 
182                                 CheckDisposed ();
183                                 return webHeaders ["Server"]; 
184                         }
185                 }
186                 
187                 public HttpStatusCode StatusCode {
188                         get { 
189                                 CheckDisposed ();
190                                 return statusCode; 
191                         }
192                 }
193                 
194                 public string StatusDescription {
195                         get { 
196                                 CheckDisposed ();
197                                 return statusDescription; 
198                         }
199                 }
200
201                 // Methods
202                 
203                 public override int GetHashCode ()
204                 {
205                         CheckDisposed ();
206                         return base.GetHashCode ();
207                 }
208                 
209                 public string GetResponseHeader (string headerName)
210                 {
211                         CheckDisposed ();
212                         string value = webHeaders [headerName];
213                         return (value != null) ? value : "";
214                 }
215                 
216                 public override Stream GetResponseStream ()
217                 {
218                         CheckDisposed ();
219                         if (0 == String.Compare (method, "HEAD", true)) // see par 4.3 & 9.4
220                                 return Stream.Null;  
221
222                         return stream;
223                 }
224                 
225                 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
226                                                   StreamingContext streamingContext)
227                 {
228                         SerializationInfo info = serializationInfo;
229
230                         info.AddValue ("uri", uri);
231                         info.AddValue ("contentLength", contentLength);
232                         info.AddValue ("contentType", contentType);
233                         info.AddValue ("method", method);
234                         info.AddValue ("statusDescription", statusDescription);
235                         info.AddValue ("cookieCollection", cookieCollection);
236                         info.AddValue ("version", version);
237                         info.AddValue ("statusCode", statusCode);
238                 }               
239
240
241                 // Cleaning up stuff
242
243                 public override void Close ()
244                 {
245                         ((IDisposable) this).Dispose ();
246                 }
247                 
248                 void IDisposable.Dispose ()
249                 {
250                         Dispose (true);
251                         GC.SuppressFinalize (this);  
252                 }
253                 
254                 protected virtual void Dispose (bool disposing) 
255                 {
256                         if (this.disposed)
257                                 return;
258                         this.disposed = true;
259                         
260                         if (disposing) {
261                                 // release managed resources
262                                 uri = null;
263                                 webHeaders = null;
264                                 cookieCollection = null;
265                                 method = null;
266                                 version = null;
267                                 statusDescription = null;
268                         }
269                         
270                         // release unmanaged resources
271                         Stream st = stream;
272                         stream = null;
273                         if (st != null)
274                                 st.Close ();
275                 }
276                 
277                 private void CheckDisposed () 
278                 {
279                         if (disposed)
280                                 throw new ObjectDisposedException (GetType ().FullName);
281                 }
282
283                 void FillCookies ()
284                 {
285                         if (webHeaders == null)
286                                 return;
287
288                         string val = webHeaders ["Set-Cookie"];
289                         if (val != null && val.Trim () != "")
290                                 SetCookie (val);
291
292                         val = webHeaders ["Set-Cookie2"];
293                         if (val != null && val.Trim () != "")
294                                 SetCookie2 (val);
295                 }
296                 
297                 static string [] SplitValue (string input)
298                 {
299                         string [] result = new string [2];
300                         int eq = input.IndexOf ('=');
301                         if (eq == -1) {
302                                 result [0] = "invalid";
303                         } else {
304                                 result [0] = input.Substring (0, eq).Trim ().ToUpper ();
305                                 result [1] = input.Substring (eq + 1);
306                         }
307                         
308                         return result;
309                 }
310                 
311                 [MonoTODO ("Parse dates")]
312                 void SetCookie (string cookie_str)
313                 {
314                         string[] parts = null;
315                         Collections.Queue options = null;
316                         Cookie cookie = null;
317
318                         options = new Collections.Queue (cookie_str.Split (';'));
319                         parts = SplitValue ((string) options.Dequeue()); // NAME=VALUE must be first
320
321                         cookie = new Cookie (parts[0], parts[1]);
322
323                         while (options.Count > 0) {
324                                 parts = SplitValue ((string) options.Dequeue());
325                                 switch (parts [0]) {
326                                         case "COMMENT":
327                                                 if (cookie.Comment == null)
328                                                         cookie.Comment = parts[1];
329                                         break;
330                                         case "COMMENTURL":
331                                                 if (cookie.CommentUri == null)
332                                                         cookie.CommentUri = new Uri(parts[1]);
333                                         break;
334                                         case "DISCARD":
335                                                 cookie.Discard = true;
336                                         break;
337                                         case "DOMAIN":
338                                                 if (cookie.Domain == "")
339                                                         cookie.Domain = parts[1];
340                                         break;
341                                         case "MAX-AGE": // RFC Style Set-Cookie2
342                                                 if (cookie.Expires == DateTime.MinValue)
343                                                         cookie.Expires = cookie.TimeStamp.AddSeconds (Int32.Parse (parts[1]));
344                                         break;
345                                         case "EXPIRES": // Netscape Style Set-Cookie
346                                                 if (cookie.Expires == DateTime.MinValue) {
347                                                         //FIXME: Does DateTime parse something like: "Sun, 17-Jan-2038 19:14:07 GMT"?
348                                                         //cookie.Expires = DateTime.ParseExact (parts[1]);
349                                                         cookie.Expires = DateTime.Now.AddDays (1);
350                                                 }
351                                         break;
352                                         case "PATH":
353                                                         cookie.Path = parts[1];
354                                         break;
355                                         case "PORT":
356                                                 if (cookie.Port == null)
357                                                         cookie.Port = parts[1];
358                                         break;
359                                         case "SECURE":
360                                                 cookie.Secure = true;
361                                         break;
362                                         case "VERSION":
363                                                 cookie.Version = Int32.Parse (parts[1]);
364                                         break;
365                                 } // switch
366                         } // while
367
368                         if (cookieCollection == null)
369                                 cookieCollection = new CookieCollection();
370
371                         if (cookie.Domain == "")
372                                 cookie.Domain = uri.Host;
373
374                         cookieCollection.Add (cookie);
375                 }
376
377                 void SetCookie2 (string cookies_str)
378                 {
379                         string [] cookies = cookies_str.Split (',');
380         
381                         foreach (string cookie_str in cookies)
382                                 SetCookie (cookie_str);
383                 }
384         }       
385 }
386