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, bool cookiesSet)
65 webHeaders = data.Headers;
66 version = data.Version;
67 statusCode = (HttpStatusCode) data.StatusCode;
68 statusDescription = data.StatusDescription;
72 } else if (webHeaders != null) {
73 webHeaders.RemoveInternal ("Set-Cookie");
74 webHeaders.RemoveInternal ("Set-Cookie2");
78 protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
80 SerializationInfo info = serializationInfo;
82 uri = (Uri) info.GetValue ("uri", typeof (Uri));
83 contentLength = info.GetInt64 ("contentLength");
84 contentType = info.GetString ("contentType");
85 method = info.GetString ("method");
86 statusDescription = info.GetString ("statusDescription");
87 cookieCollection = (CookieCollection) info.GetValue ("cookieCollection", typeof (CookieCollection));
88 version = (Version) info.GetValue ("version", typeof (Version));
89 statusCode = (HttpStatusCode) info.GetValue ("statusCode", typeof (HttpStatusCode));
94 public string CharacterSet {
95 // Content-Type = "Content-Type" ":" media-type
96 // media-type = type "/" subtype *( ";" parameter )
97 // parameter = attribute "=" value
98 // 3.7.1. default is ISO-8859-1
101 string contentType = ContentType;
102 if (contentType == null)
104 string val = contentType.ToLower ();
105 int pos = val.IndexOf ("charset=");
109 int pos2 = val.IndexOf (';', pos);
111 ? contentType.Substring (pos)
112 : contentType.Substring (pos, pos2 - pos);
116 public string ContentEncoding {
119 return webHeaders ["Content-Encoding"];
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 ();
232 public string GetResponseHeader (string headerName)
235 string value = webHeaders [headerName];
236 return (value != null) ? value : "";
239 internal void ReadAll ()
241 WebConnectionStream wce = stream as WebConnectionStream;
250 public override Stream GetResponseStream ()
255 if (0 == String.Compare (method, "HEAD", true)) // see par 4.3 & 9.4
261 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
262 StreamingContext streamingContext)
264 SerializationInfo info = serializationInfo;
266 info.AddValue ("uri", uri);
267 info.AddValue ("contentLength", contentLength);
268 info.AddValue ("contentType", contentType);
269 info.AddValue ("method", method);
270 info.AddValue ("statusDescription", statusDescription);
271 info.AddValue ("cookieCollection", cookieCollection);
272 info.AddValue ("version", version);
273 info.AddValue ("statusCode", statusCode);
279 public override void Close ()
281 ((IDisposable) this).Dispose ();
284 void IDisposable.Dispose ()
287 GC.SuppressFinalize (this);
290 protected virtual void Dispose (bool disposing)
294 this.disposed = true;
297 // release managed resources
300 cookieCollection = null;
303 statusDescription = null;
306 // release unmanaged resources
313 private void CheckDisposed ()
316 throw new ObjectDisposedException (GetType ().FullName);
321 if (webHeaders == null)
324 string [] values = webHeaders.GetValues ("Set-Cookie");
325 if (values != null) {
326 foreach (string va in values)
330 values = webHeaders.GetValues ("Set-Cookie2");
331 if (values != null) {
332 foreach (string va in values)
337 void SetCookie (string header)
340 Cookie cookie = null;
341 CookieParser parser = new CookieParser (header);
343 while (parser.GetNextNameValue (out name, out val)) {
344 if (name == null || name == "")
347 if (cookie == null) {
348 cookie = new Cookie (name, val);
352 name = name.ToUpper ();
355 if (cookie.Comment == null)
356 cookie.Comment = val;
359 if (cookie.CommentUri == null)
360 cookie.CommentUri = new Uri (val);
363 cookie.Discard = true;
366 if (cookie.Domain == "")
369 case "MAX-AGE": // RFC Style Set-Cookie2
370 if (cookie.Expires == DateTime.MinValue)
371 cookie.Expires = cookie.TimeStamp.AddSeconds (Int32.Parse (val));
373 case "EXPIRES": // Netscape Style Set-Cookie
374 if (cookie.Expires != DateTime.MinValue)
377 cookie.Expires = DateTime.ParseExact (val, "r", CultureInfo.InvariantCulture);
380 cookie.Expires = DateTime.ParseExact (val,
381 "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'",
382 CultureInfo.InvariantCulture);
384 cookie.Expires = DateTime.Now.AddDays (1);
392 if (cookie.Port == null)
396 cookie.Secure = true;
399 cookie.Version = Int32.Parse (val);
404 if (cookieCollection == null)
405 cookieCollection = new CookieCollection ();
407 if (cookie.Domain == "")
408 cookie.Domain = uri.Host;
410 cookieCollection.Add (cookie);
413 void SetCookie2 (string cookies_str)
415 string [] cookies = cookies_str.Split (',');
417 foreach (string cookie_str in cookies)
418 SetCookie (cookie_str);
427 public CookieParser (string header) : this (header, 0)
431 public CookieParser (string header, int position)
433 this.header = header;
435 this.length = header.Length;
438 public bool GetNextNameValue (out string name, out string val)
446 name = GetCookieName ();
447 if (header [pos] == '=') {
449 val = GetCookieValue ();
450 if (pos < length && header [pos] == ';')
457 string GetCookieName ()
460 while (k < length && Char.IsWhiteSpace (header [k]))
464 while (k < length && header [k] != ';' && header [k] != '=')
468 return header.Substring (begin, k - begin).Trim ();
471 string GetCookieValue ()
477 while (k < length && Char.IsWhiteSpace (header [k]))
481 while (k < length && header [k] != ';')
485 return header.Substring (begin, k - begin).Trim ();