2003-10-10 Pedro Mart�nez Juli� <yoros@wanadoo.es>
[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                 Stream 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 (stream == null)
220                                 return Stream.Null;  
221                         if (0 == String.Compare (method, "HEAD", true)) // see par 4.3 & 9.4
222                                 return Stream.Null;  
223
224                         return stream;
225                 }
226                 
227                 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
228                                                   StreamingContext streamingContext)
229                 {
230                         SerializationInfo info = serializationInfo;
231
232                         info.AddValue ("uri", uri);
233                         info.AddValue ("contentLength", contentLength);
234                         info.AddValue ("contentType", contentType);
235                         info.AddValue ("method", method);
236                         info.AddValue ("statusDescription", statusDescription);
237                         info.AddValue ("cookieCollection", cookieCollection);
238                         info.AddValue ("version", version);
239                         info.AddValue ("statusCode", statusCode);
240                 }               
241
242
243                 // Cleaning up stuff
244
245                 public override void Close ()
246                 {
247                         ((IDisposable) this).Dispose ();
248                 }
249                 
250                 void IDisposable.Dispose ()
251                 {
252                         Dispose (true);
253                         GC.SuppressFinalize (this);  
254                 }
255                 
256                 protected virtual void Dispose (bool disposing) 
257                 {
258                         if (this.disposed)
259                                 return;
260                         this.disposed = true;
261                         
262                         if (disposing) {
263                                 // release managed resources
264                                 uri = null;
265                                 webHeaders = null;
266                                 cookieCollection = null;
267                                 method = null;
268                                 version = null;
269                                 statusDescription = null;
270                         }
271                         
272                         // release unmanaged resources
273                         Stream st = stream;
274                         stream = null;
275                         if (st != null)
276                                 st.Close ();
277                 }
278                 
279                 private void CheckDisposed () 
280                 {
281                         if (disposed)
282                                 throw new ObjectDisposedException (GetType ().FullName);
283                 }
284
285                 void FillCookies ()
286                 {
287                         if (webHeaders == null)
288                                 return;
289
290                         string val = webHeaders ["Set-Cookie"];
291                         if (val != null && val.Trim () != "")
292                                 SetCookie (val);
293
294                         val = webHeaders ["Set-Cookie2"];
295                         if (val != null && val.Trim () != "")
296                                 SetCookie2 (val);
297                 }
298                 
299                 static string [] SplitValue (string input)
300                 {
301                         string [] result = new string [2];
302                         int eq = input.IndexOf ('=');
303                         if (eq == -1) {
304                                 result [0] = "invalid";
305                         } else {
306                                 result [0] = input.Substring (0, eq).Trim ().ToUpper ();
307                                 result [1] = input.Substring (eq + 1);
308                         }
309                         
310                         return result;
311                 }
312                 
313                 [MonoTODO ("Parse dates")]
314                 void SetCookie (string cookie_str)
315                 {
316                         string[] parts = null;
317                         Collections.Queue options = null;
318                         Cookie cookie = null;
319
320                         options = new Collections.Queue (cookie_str.Split (';'));
321                         parts = SplitValue ((string) options.Dequeue()); // NAME=VALUE must be first
322
323                         cookie = new Cookie (parts[0], parts[1]);
324
325                         while (options.Count > 0) {
326                                 parts = SplitValue ((string) options.Dequeue());
327                                 switch (parts [0]) {
328                                         case "COMMENT":
329                                                 if (cookie.Comment == null)
330                                                         cookie.Comment = parts[1];
331                                         break;
332                                         case "COMMENTURL":
333                                                 if (cookie.CommentUri == null)
334                                                         cookie.CommentUri = new Uri(parts[1]);
335                                         break;
336                                         case "DISCARD":
337                                                 cookie.Discard = true;
338                                         break;
339                                         case "DOMAIN":
340                                                 if (cookie.Domain == "")
341                                                         cookie.Domain = parts[1];
342                                         break;
343                                         case "MAX-AGE": // RFC Style Set-Cookie2
344                                                 if (cookie.Expires == DateTime.MinValue)
345                                                         cookie.Expires = cookie.TimeStamp.AddSeconds (Int32.Parse (parts[1]));
346                                         break;
347                                         case "EXPIRES": // Netscape Style Set-Cookie
348                                                 if (cookie.Expires == DateTime.MinValue) {
349                                                         //FIXME: Does DateTime parse something like: "Sun, 17-Jan-2038 19:14:07 GMT"?
350                                                         //cookie.Expires = DateTime.ParseExact (parts[1]);
351                                                         cookie.Expires = DateTime.Now.AddDays (1);
352                                                 }
353                                         break;
354                                         case "PATH":
355                                                         cookie.Path = parts[1];
356                                         break;
357                                         case "PORT":
358                                                 if (cookie.Port == null)
359                                                         cookie.Port = parts[1];
360                                         break;
361                                         case "SECURE":
362                                                 cookie.Secure = true;
363                                         break;
364                                         case "VERSION":
365                                                 cookie.Version = Int32.Parse (parts[1]);
366                                         break;
367                                 } // switch
368                         } // while
369
370                         if (cookieCollection == null)
371                                 cookieCollection = new CookieCollection();
372
373                         if (cookie.Domain == "")
374                                 cookie.Domain = uri.Host;
375
376                         cookieCollection.Add (cookie);
377                 }
378
379                 void SetCookie2 (string cookies_str)
380                 {
381                         string [] cookies = cookies_str.Split (',');
382         
383                         foreach (string cookie_str in cookies)
384                                 SetCookie (cookie_str);
385                 }
386         }       
387 }
388