2 // System.Net.HttpWebResponse
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (c) 2002 Lawrence Pit
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Globalization;
37 using System.Net.Sockets;
38 using System.Runtime.Serialization;
44 public class HttpWebResponse : WebResponse, ISerializable, IDisposable
47 WebHeaderCollection webHeaders;
48 CookieCollection cookieCollection;
51 HttpStatusCode statusCode;
52 string statusDescription;
53 long contentLength = -1;
56 bool disposed = false;
61 internal HttpWebResponse (Uri uri, string method, WebConnectionData data, CookieContainer container)
65 webHeaders = data.Headers;
66 version = data.Version;
67 statusCode = (HttpStatusCode) data.StatusCode;
68 statusDescription = data.StatusDescription;
70 if (container != null) {
75 protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
77 SerializationInfo info = serializationInfo;
79 uri = (Uri) info.GetValue ("uri", typeof (Uri));
80 contentLength = info.GetInt64 ("contentLength");
81 contentType = info.GetString ("contentType");
82 method = info.GetString ("method");
83 statusDescription = info.GetString ("statusDescription");
84 cookieCollection = (CookieCollection) info.GetValue ("cookieCollection", typeof (CookieCollection));
85 version = (Version) info.GetValue ("version", typeof (Version));
86 statusCode = (HttpStatusCode) info.GetValue ("statusCode", typeof (HttpStatusCode));
91 public string CharacterSet {
92 // Content-Type = "Content-Type" ":" media-type
93 // media-type = type "/" subtype *( ";" parameter )
94 // parameter = attribute "=" value
95 // 3.7.1. default is ISO-8859-1
98 string contentType = ContentType;
99 if (contentType == null)
101 string val = contentType.ToLower ();
102 int pos = val.IndexOf ("charset=");
106 int pos2 = val.IndexOf (';', pos);
108 ? contentType.Substring (pos)
109 : contentType.Substring (pos, pos2 - pos);
113 public string ContentEncoding {
116 string h = webHeaders ["Content-Encoding"];
117 return h != null ? h : "";
121 public override long ContentLength {
124 if (contentLength != -1)
125 return contentLength;
128 contentLength = (long) UInt64.Parse (webHeaders ["Content-Length"]);
129 } catch (Exception) {
133 return contentLength;
137 public override string ContentType {
140 if (contentType == null)
141 contentType = webHeaders ["Content-Type"];
147 public CookieCollection Cookies {
151 if (cookieCollection == null)
152 cookieCollection = new CookieCollection ();
153 return cookieCollection;
157 cookieCollection = value;
161 public override WebHeaderCollection Headers {
168 public DateTime LastModified {
172 string dtStr = webHeaders ["Last-Modified"];
173 return MonoHttpDate.Parse (dtStr);
174 } catch (Exception) {
180 public string Method {
187 public Version ProtocolVersion {
194 public override Uri ResponseUri {
201 public string Server {
204 return webHeaders ["Server"];
208 public HttpStatusCode StatusCode {
215 public string StatusDescription {
218 return statusDescription;
224 public override int GetHashCode ()
227 return base.GetHashCode ();
231 public string GetResponseHeader (string headerName)
234 string value = webHeaders [headerName];
235 return (value != null) ? value : "";
238 internal void ReadAll ()
240 WebConnectionStream wce = stream as WebConnectionStream;
249 public override Stream GetResponseStream ()
254 if (0 == String.Compare (method, "HEAD", true)) // see par 4.3 & 9.4
260 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
261 StreamingContext streamingContext)
263 SerializationInfo info = serializationInfo;
265 info.AddValue ("uri", uri);
266 info.AddValue ("contentLength", contentLength);
267 info.AddValue ("contentType", contentType);
268 info.AddValue ("method", method);
269 info.AddValue ("statusDescription", statusDescription);
270 info.AddValue ("cookieCollection", cookieCollection);
271 info.AddValue ("version", version);
272 info.AddValue ("statusCode", statusCode);
278 public override void Close ()
280 ((IDisposable) this).Dispose ();
283 void IDisposable.Dispose ()
286 GC.SuppressFinalize (this);
292 void Dispose (bool disposing)
296 this.disposed = true;
299 // release managed resources
302 cookieCollection = null;
305 statusDescription = null;
308 // release unmanaged resources
315 private void CheckDisposed ()
318 throw new ObjectDisposedException (GetType ().FullName);
323 if (webHeaders == null)
326 string [] values = webHeaders.GetValues ("Set-Cookie");
327 if (values != null) {
328 foreach (string va in values)
332 values = webHeaders.GetValues ("Set-Cookie2");
333 if (values != null) {
334 foreach (string va in values)
339 void SetCookie (string header)
342 Cookie cookie = null;
343 CookieParser parser = new CookieParser (header);
345 while (parser.GetNextNameValue (out name, out val)) {
346 if ((name == null || name == "") && cookie == null)
349 if (cookie == null) {
350 cookie = new Cookie (name, val);
354 name = name.ToUpper ();
357 if (cookie.Comment == null)
358 cookie.Comment = val;
361 if (cookie.CommentUri == null)
362 cookie.CommentUri = new Uri (val);
365 cookie.Discard = true;
368 if (cookie.Domain == "")
371 case "MAX-AGE": // RFC Style Set-Cookie2
372 if (cookie.Expires == DateTime.MinValue) {
374 cookie.Expires = cookie.TimeStamp.AddSeconds (UInt32.Parse (val));
378 case "EXPIRES": // Netscape Style Set-Cookie
379 if (cookie.Expires != DateTime.MinValue)
382 cookie.Expires = DateTime.ParseExact (val, "r", CultureInfo.InvariantCulture);
385 cookie.Expires = DateTime.ParseExact (val,
386 "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'",
387 CultureInfo.InvariantCulture);
389 cookie.Expires = DateTime.Now.AddDays (1);
397 if (cookie.Port == null)
401 cookie.Secure = true;
405 cookie.Version = (int) UInt32.Parse (val);
411 if (cookieCollection == null)
412 cookieCollection = new CookieCollection ();
414 if (cookie.Domain == "")
415 cookie.Domain = uri.Host;
417 cookieCollection.Add (cookie);
420 void SetCookie2 (string cookies_str)
422 string [] cookies = cookies_str.Split (',');
424 foreach (string cookie_str in cookies)
425 SetCookie (cookie_str);
434 public CookieParser (string header) : this (header, 0)
438 public CookieParser (string header, int position)
440 this.header = header;
442 this.length = header.Length;
445 public bool GetNextNameValue (out string name, out string val)
453 name = GetCookieName ();
454 if (pos < header.Length && header [pos] == '=') {
456 val = GetCookieValue ();
459 if (pos < length && header [pos] == ';')
465 string GetCookieName ()
468 while (k < length && Char.IsWhiteSpace (header [k]))
472 while (k < length && header [k] != ';' && header [k] != '=')
476 return header.Substring (begin, k - begin).Trim ();
479 string GetCookieValue ()
485 while (k < length && Char.IsWhiteSpace (header [k]))
489 if (header [k] == '"'){
493 while (k < length && header [k] != '"')
496 for (j = k; j < length && header [j] != ';'; j++)
501 while (k < length && header [k] != ';')
506 return header.Substring (begin, k - begin).Trim ();