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;
55 CookieContainer cookie_container;
57 bool disposed = false;
62 internal HttpWebResponse (Uri uri, string method, WebConnectionData data, CookieContainer container)
66 webHeaders = data.Headers;
67 version = data.Version;
68 statusCode = (HttpStatusCode) data.StatusCode;
69 statusDescription = data.StatusDescription;
71 if (container != null) {
72 this.cookie_container = container;
77 protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
79 SerializationInfo info = serializationInfo;
81 uri = (Uri) info.GetValue ("uri", typeof (Uri));
82 contentLength = info.GetInt64 ("contentLength");
83 contentType = info.GetString ("contentType");
84 method = info.GetString ("method");
85 statusDescription = info.GetString ("statusDescription");
86 cookieCollection = (CookieCollection) info.GetValue ("cookieCollection", typeof (CookieCollection));
87 version = (Version) info.GetValue ("version", typeof (Version));
88 statusCode = (HttpStatusCode) info.GetValue ("statusCode", typeof (HttpStatusCode));
93 public string CharacterSet {
94 // Content-Type = "Content-Type" ":" media-type
95 // media-type = type "/" subtype *( ";" parameter )
96 // parameter = attribute "=" value
97 // 3.7.1. default is ISO-8859-1
100 string contentType = ContentType;
101 if (contentType == null)
103 string val = contentType.ToLower ();
104 int pos = val.IndexOf ("charset=");
108 int pos2 = val.IndexOf (';', pos);
110 ? contentType.Substring (pos)
111 : contentType.Substring (pos, pos2 - pos);
115 public string ContentEncoding {
118 string h = webHeaders ["Content-Encoding"];
119 return h != null ? h : "";
123 public override long ContentLength {
126 if (contentLength != -1)
127 return contentLength;
130 contentLength = (long) UInt64.Parse (webHeaders ["Content-Length"]);
131 } catch (Exception) {
135 return contentLength;
139 public override string ContentType {
142 if (contentType == null)
143 contentType = webHeaders ["Content-Type"];
149 public CookieCollection Cookies {
153 if (cookieCollection == null)
154 cookieCollection = new CookieCollection ();
155 return cookieCollection;
159 cookieCollection = value;
163 public override WebHeaderCollection Headers {
170 public DateTime LastModified {
174 string dtStr = webHeaders ["Last-Modified"];
175 return MonoHttpDate.Parse (dtStr);
176 } catch (Exception) {
182 public string Method {
189 public Version ProtocolVersion {
196 public override Uri ResponseUri {
203 public string Server {
206 return webHeaders ["Server"];
210 public HttpStatusCode StatusCode {
217 public string StatusDescription {
220 return statusDescription;
226 public override int GetHashCode ()
229 return base.GetHashCode ();
233 public string GetResponseHeader (string headerName)
236 string value = webHeaders [headerName];
237 return (value != null) ? value : "";
240 internal void ReadAll ()
242 WebConnectionStream wce = stream as WebConnectionStream;
251 public override Stream GetResponseStream ()
256 if (0 == String.Compare (method, "HEAD", true)) // see par 4.3 & 9.4
262 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
263 StreamingContext streamingContext)
265 SerializationInfo info = serializationInfo;
267 info.AddValue ("uri", uri);
268 info.AddValue ("contentLength", contentLength);
269 info.AddValue ("contentType", contentType);
270 info.AddValue ("method", method);
271 info.AddValue ("statusDescription", statusDescription);
272 info.AddValue ("cookieCollection", cookieCollection);
273 info.AddValue ("version", version);
274 info.AddValue ("statusCode", statusCode);
280 public override void Close ()
282 ((IDisposable) this).Dispose ();
285 void IDisposable.Dispose ()
288 GC.SuppressFinalize (this);
294 void Dispose (bool disposing)
298 this.disposed = true;
301 // release managed resources
304 cookieCollection = null;
307 statusDescription = null;
310 // release unmanaged resources
317 private void CheckDisposed ()
320 throw new ObjectDisposedException (GetType ().FullName);
325 if (webHeaders == null)
328 string [] values = webHeaders.GetValues ("Set-Cookie");
329 if (values != null) {
330 foreach (string va in values)
334 values = webHeaders.GetValues ("Set-Cookie2");
335 if (values != null) {
336 foreach (string va in values)
341 void SetCookie (string header)
344 Cookie cookie = null;
345 CookieParser parser = new CookieParser (header);
347 while (parser.GetNextNameValue (out name, out val)) {
348 if ((name == null || name == "") && cookie == null)
351 if (cookie == null) {
352 cookie = new Cookie (name, val);
356 name = name.ToUpper ();
359 if (cookie.Comment == null)
360 cookie.Comment = val;
363 if (cookie.CommentUri == null)
364 cookie.CommentUri = new Uri (val);
367 cookie.Discard = true;
370 if (cookie.Domain == "")
373 case "MAX-AGE": // RFC Style Set-Cookie2
374 if (cookie.Expires == DateTime.MinValue) {
376 cookie.Expires = cookie.TimeStamp.AddSeconds (UInt32.Parse (val));
380 case "EXPIRES": // Netscape Style Set-Cookie
381 if (cookie.Expires != DateTime.MinValue)
384 cookie.Expires = DateTime.ParseExact (val, "r", CultureInfo.InvariantCulture);
387 cookie.Expires = DateTime.ParseExact (val,
388 "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'",
389 CultureInfo.InvariantCulture);
391 cookie.Expires = DateTime.Now.AddDays (1);
399 if (cookie.Port == null)
403 cookie.Secure = true;
407 cookie.Version = (int) UInt32.Parse (val);
413 if (cookieCollection == null)
414 cookieCollection = new CookieCollection ();
416 if (cookie.Domain == "")
417 cookie.Domain = uri.Host;
419 cookieCollection.Add (cookie);
420 if (cookie_container != null)
421 cookie_container.Add (uri, cookie);
424 void SetCookie2 (string cookies_str)
426 string [] cookies = cookies_str.Split (',');
428 foreach (string cookie_str in cookies)
429 SetCookie (cookie_str);
438 public CookieParser (string header) : this (header, 0)
442 public CookieParser (string header, int position)
444 this.header = header;
446 this.length = header.Length;
449 public bool GetNextNameValue (out string name, out string val)
457 name = GetCookieName ();
458 if (pos < header.Length && header [pos] == '=') {
460 val = GetCookieValue ();
463 if (pos < length && header [pos] == ';')
469 string GetCookieName ()
472 while (k < length && Char.IsWhiteSpace (header [k]))
476 while (k < length && header [k] != ';' && header [k] != '=')
480 return header.Substring (begin, k - begin).Trim ();
483 string GetCookieValue ()
489 while (k < length && Char.IsWhiteSpace (header [k]))
493 if (header [k] == '"'){
497 while (k < length && header [k] != '"')
500 for (j = k; j < length && header [j] != ';'; j++)
505 while (k < length && header [k] != ';')
510 return header.Substring (begin, k - begin).Trim ();