New test.
[mono.git] / mcs / class / System / System.Net / HttpWebResponse.cs
index 7e1eccebbf8395a085caddba16131d20bab0eb9b..83eef98ca697bca3ce61fcc93f523814d81b2a12 100644 (file)
@@ -4,9 +4,11 @@
 // Authors:
 //     Lawrence Pit (loz@cable.a2000.nl)
 //     Gonzalo Paniagua Javier (gonzalo@ximian.com)
+//      Daniel Nauck    (dna(at)mono-project(dot)de)
 //
 // (c) 2002 Lawrence Pit
 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
+// (c) 2008 Daniel Nauck
 //
 
 //
@@ -34,15 +36,19 @@ using System;
 using System.Collections;
 using System.Globalization;
 using System.IO;
+using System.IO.Compression;
 using System.Net.Sockets;
 using System.Runtime.Serialization;
 using System.Text;
 
 namespace System.Net 
 {
+#if MOONLIGHT
+       internal class HttpWebResponse : WebResponse, ISerializable, IDisposable {
+#else
        [Serializable]
-       public class HttpWebResponse : WebResponse, ISerializable, IDisposable
-       {
+       public class HttpWebResponse : WebResponse, ISerializable, IDisposable {
+#endif
                Uri uri;
                WebHeaderCollection webHeaders;
                CookieCollection cookieCollection;
@@ -50,10 +56,11 @@ namespace System.Net
                Version version;
                HttpStatusCode statusCode;
                string statusDescription;
-               long contentLength = -1;
+               long contentLength;
                string contentType;
+               CookieContainer cookie_container;
 
-               bool disposed = false;
+               bool disposed;
                Stream stream;
                
                // Constructors
@@ -67,11 +74,29 @@ namespace System.Net
                        statusCode = (HttpStatusCode) data.StatusCode;
                        statusDescription = data.StatusDescription;
                        stream = data.stream;
+                       contentLength = -1;
+
+                       try {
+                               string cl = webHeaders ["Content-Length"];
+                               if (String.IsNullOrEmpty (cl) || !Int64.TryParse (cl, out contentLength))
+                                       contentLength = -1;
+                       } catch (Exception) {
+                               contentLength = -1;
+                       }
+
                        if (container != null) {
+                               this.cookie_container = container;      
                                FillCookies ();
                        }
+
+                       string content_encoding = webHeaders ["Content-Encoding"];
+                       if (content_encoding == "gzip" && (data.request.AutomaticDecompression & DecompressionMethods.GZip) != 0)
+                               stream = new GZipStream (stream, CompressionMode.Decompress);
+                       else if (content_encoding == "deflate" && (data.request.AutomaticDecompression & DecompressionMethods.Deflate) != 0)
+                               stream = new DeflateStream (stream, CompressionMode.Decompress);
                }
 
+               [Obsolete ("Serialization is obsoleted for this type", false)]
                protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
                {
                        SerializationInfo info = serializationInfo;
@@ -94,7 +119,6 @@ namespace System.Net
                        // parameter      = attribute "=" value
                        // 3.7.1. default is ISO-8859-1
                        get { 
-                               CheckDisposed ();
                                string contentType = ContentType;
                                if (contentType == null)
                                        return "ISO-8859-1";
@@ -111,7 +135,7 @@ namespace System.Net
                }
                
                public string ContentEncoding {
-                       get { 
+                       get {
                                CheckDisposed ();
                                string h = webHeaders ["Content-Encoding"];
                                return h != null ? h : "";
@@ -120,16 +144,6 @@ namespace System.Net
                
                public override long ContentLength {            
                        get {
-                               CheckDisposed ();
-                               if (contentLength != -1)
-                                       return contentLength;
-
-                               try {
-                                       contentLength = (long) UInt64.Parse (webHeaders ["Content-Length"]); 
-                               } catch (Exception) {
-                                       return -1;
-                               }
-
                                return contentLength;
                        }
                }
@@ -137,6 +151,7 @@ namespace System.Net
                public override string ContentType {            
                        get {
                                CheckDisposed ();
+
                                if (contentType == null)
                                        contentType = webHeaders ["Content-Type"];
 
@@ -145,9 +160,8 @@ namespace System.Net
                }
                
                public CookieCollection Cookies {
-                       get { 
+                       get {
                                CheckDisposed ();
-                               
                                if (cookieCollection == null)
                                        cookieCollection = new CookieCollection ();
                                return cookieCollection;
@@ -159,11 +173,23 @@ namespace System.Net
                }
                
                public override WebHeaderCollection Headers {           
-                       get { 
-                               CheckDisposed ();
+                       get {
                                return webHeaders; 
                        }
                }
+
+               static Exception GetMustImplement ()
+               {
+                       return new NotImplementedException ();
+               }
+               
+               [MonoTODO]
+               public override bool IsMutuallyAuthenticated
+               {
+                       get {
+                               throw GetMustImplement ();
+                       }
+               }
                
                public DateTime LastModified {
                        get {
@@ -178,55 +204,47 @@ namespace System.Net
                }
                
                public string Method {
-                       get { 
+                       get {
                                CheckDisposed ();
                                return method; 
                        }
                }
                
                public Version ProtocolVersion {
-                       get { 
+                       get {
                                CheckDisposed ();
                                return version; 
                        }
                }
                
                public override Uri ResponseUri {               
-                       get { 
+                       get {
                                CheckDisposed ();
                                return uri; 
                        }
                }               
                
                public string Server {
-                       get { 
+                       get {
                                CheckDisposed ();
                                return webHeaders ["Server"]; 
                        }
                }
                
                public HttpStatusCode StatusCode {
-                       get { 
-                               CheckDisposed ();
+                       get {
                                return statusCode; 
                        }
                }
                
                public string StatusDescription {
-                       get { 
+                       get {
                                CheckDisposed ();
                                return statusDescription; 
                        }
                }
 
                // Methods
-#if !NET_2_0
-               public override int GetHashCode ()
-               {
-                       CheckDisposed ();
-                       return base.GetHashCode ();
-               }
-#endif
                
                public string GetResponseHeader (string headerName)
                {
@@ -259,6 +277,12 @@ namespace System.Net
                
                void ISerializable.GetObjectData (SerializationInfo serializationInfo,
                                                  StreamingContext streamingContext)
+               {
+                       GetObjectData (serializationInfo, streamingContext);
+               }
+
+               protected override void GetObjectData (SerializationInfo serializationInfo,
+                       StreamingContext streamingContext)
                {
                        SerializationInfo info = serializationInfo;
 
@@ -270,8 +294,7 @@ namespace System.Net
                        info.AddValue ("cookieCollection", cookieCollection);
                        info.AddValue ("version", version);
                        info.AddValue ("statusCode", statusCode);
-               }               
-
+               }
 
                // Cleaning up stuff
 
@@ -286,9 +309,6 @@ namespace System.Net
                        GC.SuppressFinalize (this);  
                }
 
-#if !NET_2_0
-               protected virtual
-#endif
                void Dispose (bool disposing) 
                {
                        if (this.disposed)
@@ -298,7 +318,6 @@ namespace System.Net
                        if (disposing) {
                                // release managed resources
                                uri = null;
-                               webHeaders = null;
                                cookieCollection = null;
                                method = null;
                                version = null;
@@ -368,6 +387,9 @@ namespace System.Net
                                        if (cookie.Domain == "")
                                                cookie.Domain = val;
                                        break;
+                               case "HTTPONLY":
+                                       cookie.HttpOnly = true;
+                                       break;
                                case "MAX-AGE": // RFC Style Set-Cookie2
                                        if (cookie.Expires == DateTime.MinValue) {
                                                try {
@@ -378,17 +400,8 @@ namespace System.Net
                                case "EXPIRES": // Netscape Style Set-Cookie
                                        if (cookie.Expires != DateTime.MinValue)
                                                break;
-                                       try {
-                                               cookie.Expires = DateTime.ParseExact (val, "r", CultureInfo.InvariantCulture);
-                                       } catch {
-                                               try { 
-                                               cookie.Expires = DateTime.ParseExact (val,
-                                                               "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'",
-                                                               CultureInfo.InvariantCulture);
-                                               } catch {
-                                                       cookie.Expires = DateTime.Now.AddDays (1);
-                                               }
-                                       }
+
+                                       cookie.Expires = TryParseCookieExpires (val);
                                        break;
                                case "PATH":
                                        cookie.Path = val;
@@ -415,6 +428,8 @@ namespace System.Net
                                cookie.Domain = uri.Host;
 
                        cookieCollection.Add (cookie);
+                       if (cookie_container != null)
+                               cookie_container.Add (uri, cookie);
                }
 
                void SetCookie2 (string cookies_str)
@@ -424,6 +439,31 @@ namespace System.Net
                        foreach (string cookie_str in cookies)
                                SetCookie (cookie_str);
                }
+
+               string[] cookieExpiresFormats =
+                       new string[] { "r",
+                                       "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'",
+                                       "ddd, dd'-'MMM'-'yy HH':'mm':'ss 'GMT'" };
+
+               DateTime TryParseCookieExpires (string value)
+               {
+                       if (value == null || value.Length == 0)
+                               return DateTime.MinValue;
+
+                       for (int i = 0; i < cookieExpiresFormats.Length; i++)
+                       {
+                               try {
+                                       DateTime cookieExpiresUtc = DateTime.ParseExact (value, cookieExpiresFormats [i], CultureInfo.InvariantCulture);
+
+                                       //convert UTC/GMT time to local time
+                                       cookieExpiresUtc = DateTime.SpecifyKind (cookieExpiresUtc, DateTimeKind.Utc);
+                                       return TimeZone.CurrentTimeZone.ToLocalTime (cookieExpiresUtc);
+                               } catch {}
+                       }
+
+                       //If we can't parse Expires, use cookie as session cookie (expires is DateTime.MinValue)
+                       return DateTime.MinValue;
+               }
        }       
 
        class CookieParser {