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 cookieContainer;
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.cookieContainer = container;
74 } else if (webHeaders != null) {
75 webHeaders.RemoveInternal ("Set-Cookie");
76 webHeaders.RemoveInternal ("Set-Cookie2");
80 protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
82 SerializationInfo info = serializationInfo;
84 uri = (Uri) info.GetValue ("uri", typeof (Uri));
85 contentLength = info.GetInt64 ("contentLength");
86 contentType = info.GetString ("contentType");
87 method = info.GetString ("method");
88 statusDescription = info.GetString ("statusDescription");
89 cookieCollection = (CookieCollection) info.GetValue ("cookieCollection", typeof (CookieCollection));
90 version = (Version) info.GetValue ("version", typeof (Version));
91 statusCode = (HttpStatusCode) info.GetValue ("statusCode", typeof (HttpStatusCode));
96 public string CharacterSet {
97 // Content-Type = "Content-Type" ":" media-type
98 // media-type = type "/" subtype *( ";" parameter )
99 // parameter = attribute "=" value
100 // 3.7.1. default is ISO-8859-1
103 string contentType = ContentType;
104 if (contentType == null)
106 string val = contentType.ToLower ();
107 int pos = val.IndexOf ("charset=");
111 int pos2 = val.IndexOf (';', pos);
113 ? contentType.Substring (pos)
114 : contentType.Substring (pos, pos2 - pos);
118 public string ContentEncoding {
121 return webHeaders ["Content-Encoding"];
125 public override long ContentLength {
128 if (contentLength != -1)
129 return contentLength;
132 contentLength = (long) UInt64.Parse (webHeaders ["Content-Length"]);
133 } catch (Exception) {
137 return contentLength;
141 public override string ContentType {
144 if (contentType == null)
145 contentType = webHeaders ["Content-Type"];
151 public CookieCollection Cookies {
155 if (cookieCollection == null)
156 cookieCollection = new CookieCollection ();
157 return cookieCollection;
161 cookieCollection = value;
165 public override WebHeaderCollection Headers {
172 public DateTime LastModified {
176 string dtStr = webHeaders ["Last-Modified"];
177 return MonoHttpDate.Parse (dtStr);
178 } catch (Exception) {
184 public string Method {
191 public Version ProtocolVersion {
198 public override Uri ResponseUri {
205 public string Server {
208 return webHeaders ["Server"];
212 public HttpStatusCode StatusCode {
219 public string StatusDescription {
222 return statusDescription;
228 public override int GetHashCode ()
231 return base.GetHashCode ();
234 public string GetResponseHeader (string headerName)
237 string value = webHeaders [headerName];
238 return (value != null) ? value : "";
241 internal void ReadAll ()
243 WebConnectionStream wce = stream as WebConnectionStream;
252 public override Stream GetResponseStream ()
257 if (0 == String.Compare (method, "HEAD", true)) // see par 4.3 & 9.4
263 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
264 StreamingContext streamingContext)
266 SerializationInfo info = serializationInfo;
268 info.AddValue ("uri", uri);
269 info.AddValue ("contentLength", contentLength);
270 info.AddValue ("contentType", contentType);
271 info.AddValue ("method", method);
272 info.AddValue ("statusDescription", statusDescription);
273 info.AddValue ("cookieCollection", cookieCollection);
274 info.AddValue ("version", version);
275 info.AddValue ("statusCode", statusCode);
281 public override void Close ()
283 ((IDisposable) this).Dispose ();
286 void IDisposable.Dispose ()
289 GC.SuppressFinalize (this);
292 protected virtual 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 == "")
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)
373 cookie.Expires = cookie.TimeStamp.AddSeconds (Int32.Parse (val));
375 case "EXPIRES": // Netscape Style Set-Cookie
376 if (cookie.Expires != DateTime.MinValue)
379 cookie.Expires = DateTime.ParseExact (val, "r", CultureInfo.InvariantCulture);
382 cookie.Expires = DateTime.ParseExact (val,
383 "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'",
384 CultureInfo.InvariantCulture);
386 cookie.Expires = DateTime.Now.AddDays (1);
394 if (cookie.Port == null)
398 cookie.Secure = true;
401 cookie.Version = Int32.Parse (val);
406 if (cookieCollection == null)
407 cookieCollection = new CookieCollection ();
409 if (cookie.Domain == "")
410 cookie.Domain = uri.Host;
412 cookieCollection.Add (cookie);
413 if (cookieContainer != null)
414 cookieContainer.Add (uri, cookie);
417 void SetCookie2 (string cookies_str)
419 string [] cookies = cookies_str.Split (',');
421 foreach (string cookie_str in cookies)
422 SetCookie (cookie_str);
431 public CookieParser (string header) : this (header, 0)
435 public CookieParser (string header, int position)
437 this.header = header;
439 this.length = header.Length;
442 public bool GetNextNameValue (out string name, out string val)
450 name = GetCookieName ();
451 if (header [pos] == '=') {
453 val = GetCookieValue ();
454 if (pos < length && header [pos] == ';')
461 string GetCookieName ()
464 while (k < length && Char.IsWhiteSpace (header [k]))
468 while (k < length && header [k] != ';' && header [k] != '=')
472 return header.Substring (begin, k - begin).Trim ();
475 string GetCookieValue ()
481 while (k < length && Char.IsWhiteSpace (header [k]))
485 if (header [k] == '"'){
489 while (k < length && header [k] != '"')
492 for (j = k; j < length && header [j] != ';'; j++)
497 while (k < length && header [k] != ';')
502 return header.Substring (begin, k - begin).Trim ();