* Dns.cs: Reimplemented (simplified and fixed) asynchronous methods by
[mono.git] / mcs / class / System / System.Net / HttpWebResponse.cs
1 //\r
2 // System.Net.HttpWebResponse\r
3 //\r
4 // Author:\r
5 //   Lawrence Pit (loz@cable.a2000.nl)\r
6 //\r
7 \r
8 using System;\r
9 using System.IO;\r
10 using System.Runtime.Serialization;\r
11 \r
12 namespace System.Net \r
13 {\r
14         [Serializable]\r
15         public class HttpWebResponse : WebResponse, ISerializable, IDisposable\r
16         {\r
17                 private Uri uri;\r
18                 private WebHeaderCollection webHeaders;\r
19                 private CookieCollection cookieCollection = null;\r
20                 private string method = null;\r
21                 private Version version = null;\r
22                 private HttpStatusCode statusCode;\r
23                 private string statusDescription = null;\r
24 \r
25                 private Stream responseStream;          \r
26                 private bool disposed = false;\r
27                 \r
28                 // Constructors\r
29                 \r
30                 protected HttpWebResponse (Uri uri, Stream responseStream) \r
31                 { \r
32                         this.uri = uri;\r
33                         this.responseStream = responseStream;\r
34                 }\r
35                 \r
36                 protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)\r
37                 {\r
38                         throw new NotSupportedException ();\r
39                 }\r
40                 \r
41                 // Properties\r
42                 \r
43                 public string CharacterSet {\r
44                         // Content-Type   = "Content-Type" ":" media-type\r
45                         // media-type     = type "/" subtype *( ";" parameter )\r
46                         // parameter      = attribute "=" value\r
47                         // 3.7.1. default is ISO-8859-1\r
48                         get { \r
49                                 CheckDisposed ();\r
50                                 string contentType = ContentType;\r
51                                 if (contentType == null)\r
52                                         return "ISO-8859-1";\r
53                                 string val = contentType.ToLower ();                                    \r
54                                 int pos = val.IndexOf ("charset=");\r
55                                 if (pos == -1)\r
56                                         return "ISO-8859-1";\r
57                                 pos += 8;\r
58                                 int pos2 = val.IndexOf (';', pos);\r
59                                 return (pos2 == -1)\r
60                                      ? contentType.Substring (pos) \r
61                                      : contentType.Substring (pos, pos2 - pos);\r
62                         }\r
63                 }\r
64                 \r
65                 public string ContentEncoding {\r
66                         get { \r
67                                 CheckDisposed ();\r
68                                 return webHeaders ["Content-Encoding"]; \r
69                         }\r
70                 }\r
71                 \r
72                 public override long ContentLength {            \r
73                         get { \r
74                                 CheckDisposed ();\r
75                                 try {\r
76                                         return Int64.Parse (webHeaders ["Content-Length"]); \r
77                                 } catch (Exception) {\r
78                                         return -1;\r
79                                 }\r
80                         }\r
81                 }\r
82                 \r
83                 public override string ContentType {            \r
84                         get { \r
85                                 CheckDisposed ();\r
86                                 return webHeaders ["Content-Type"]; \r
87                         }\r
88                 }\r
89                 \r
90                 public CookieCollection Cookies {\r
91                         get { \r
92                                 CheckDisposed ();\r
93                                 \r
94                                 // LAMESPEC: a simple test reveal this always \r
95                                 // returns an empty collection. It is not filled \r
96                                 // with the values from the Set-Cookie or \r
97                                 // Set-Cookie2 response headers, which is a bit\r
98                                 // of a shame..\r
99                                 if (cookieCollection == null)\r
100                                         cookieCollection = new CookieCollection ();\r
101                                 return cookieCollection;\r
102                         }\r
103                         set {\r
104                                 CheckDisposed ();\r
105                                 // ?? don't understand how you can set cookies on a response.\r
106                                 throw new NotSupportedException ();\r
107                         }\r
108                 }\r
109                 \r
110                 public override WebHeaderCollection Headers {           \r
111                         get { \r
112                                 CheckDisposed ();\r
113                                 return webHeaders; \r
114                         }\r
115                 }\r
116                 \r
117                 public DateTime LastModified {\r
118                         get {\r
119                                 CheckDisposed ();\r
120                                 try {\r
121                                         string dtStr = webHeaders ["Last-Modified"];\r
122                                         // TODO: accept more than rfc1123 dates\r
123                                         DateTime dt = DateTime.ParseExact (dtStr, "r", null);\r
124                                         return dt;\r
125                                 } catch (Exception) {\r
126                                         return DateTime.Now;    \r
127                                 }\r
128                         }\r
129                 }\r
130                 \r
131                 public string Method {\r
132                         get { \r
133                                 CheckDisposed ();\r
134                                 return method; \r
135                         }\r
136                 }\r
137                 \r
138                 public Version ProtocolVersion {\r
139                         get { \r
140                                 CheckDisposed ();\r
141                                 return version; \r
142                         }\r
143                 }\r
144                 \r
145                 public override Uri ResponseUri {               \r
146                         get { \r
147                                 CheckDisposed ();\r
148                                 return uri; \r
149                         }\r
150                 }               \r
151                 \r
152                 public string Server {\r
153                         get { \r
154                                 CheckDisposed ();\r
155                                 return webHeaders ["Server"]; \r
156                         }\r
157                 }\r
158                 \r
159                 public HttpStatusCode StatusCode {\r
160                         get { \r
161                                 CheckDisposed ();\r
162                                 return statusCode; \r
163                         }\r
164                 }\r
165                 \r
166                 public string StatusDescription {\r
167                         get { \r
168                                 CheckDisposed ();\r
169                                 return statusDescription; \r
170                         }\r
171                 }\r
172 \r
173                 // Methods\r
174                 \r
175                 public override int GetHashCode ()\r
176                 {\r
177                         CheckDisposed ();\r
178                         return base.GetHashCode ();\r
179                 }\r
180                 \r
181                 public string GetResponseHeader (string headerName)\r
182                 {\r
183                         CheckDisposed ();\r
184                         return webHeaders [headerName];\r
185                 }\r
186                 \r
187                 [MonoTODO]\r
188                 public override Stream GetResponseStream ()\r
189                 {\r
190                         CheckDisposed ();\r
191                         throw new NotImplementedException ();\r
192                 }\r
193                 \r
194                 [MonoTODO]\r
195                 void ISerializable.GetObjectData (SerializationInfo serializationInfo,\r
196                                                   StreamingContext streamingContext)\r
197                 {\r
198                         CheckDisposed ();\r
199                         throw new NotImplementedException ();\r
200                 }               \r
201 \r
202 \r
203                 // Cleaning up stuff\r
204 \r
205                 ~HttpWebResponse ()\r
206                 {\r
207                         Dispose (false);\r
208                 }               \r
209                 \r
210                 public override void Close ()\r
211                 {\r
212                         ((IDisposable) this).Dispose ();\r
213                 }\r
214                 \r
215                 void IDisposable.Dispose ()\r
216                 {\r
217                         Dispose (true);\r
218                         \r
219                         // see spec, suppress finalization of this object.\r
220                         GC.SuppressFinalize (this);  \r
221                 }\r
222                 \r
223                 protected virtual void Dispose (bool disposing) \r
224                 {\r
225                         if (this.disposed)\r
226                                 return;\r
227                         this.disposed = true;\r
228                         \r
229                         if (disposing) {\r
230                                 // release managed resources\r
231                                 uri = null;\r
232                                 webHeaders = null;\r
233                                 cookieCollection = null;\r
234                                 method = null;\r
235                                 version = null;\r
236                                 // statusCode = null;\r
237                                 statusDescription = null;\r
238                         }\r
239                         \r
240                         // release unmanaged resources\r
241                         Stream stream = responseStream;\r
242                         responseStream = null;\r
243                         if (stream != null)\r
244                                 stream.Close ();                        \r
245                 }\r
246                 \r
247                 private void CheckDisposed () \r
248                 {\r
249                         if (disposed)\r
250                                 throw new ObjectDisposedException ("HttpWebResponse");\r
251                 }\r
252         }       \r
253 }