New tests.
[mono.git] / mcs / class / System / System.Net / HttpWebResponse.cs
index d4866e30e12a4a346bb27a48d12c71dbc0c082c2..83eef98ca697bca3ce61fcc93f523814d81b2a12 100644 (file)
@@ -36,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;
@@ -52,11 +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
@@ -70,15 +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);
                }
 
-#if NET_2_0
                [Obsolete ("Serialization is obsoleted for this type", false)]
-#endif
                protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
                {
                        SerializationInfo info = serializationInfo;
@@ -117,7 +135,8 @@ namespace System.Net
                }
                
                public string ContentEncoding {
-                       get { 
+                       get {
+                               CheckDisposed ();
                                string h = webHeaders ["Content-Encoding"];
                                return h != null ? h : "";
                        }
@@ -125,21 +144,14 @@ namespace System.Net
                
                public override long ContentLength {            
                        get {
-                               if (contentLength != -1)
-                                       return contentLength;
-
-                               try {
-                                       contentLength = (long) UInt64.Parse (webHeaders ["Content-Length"]); 
-                               } catch (Exception) {
-                                       return -1;
-                               }
-
                                return contentLength;
                        }
                }
                
                public override string ContentType {            
                        get {
+                               CheckDisposed ();
+
                                if (contentType == null)
                                        contentType = webHeaders ["Content-Type"];
 
@@ -148,23 +160,24 @@ namespace System.Net
                }
                
                public CookieCollection Cookies {
-                       get { 
+                       get {
+                               CheckDisposed ();
                                if (cookieCollection == null)
                                        cookieCollection = new CookieCollection ();
                                return cookieCollection;
                        }
                        set {
+                               CheckDisposed ();
                                cookieCollection = value;
                        }
                }
                
                public override WebHeaderCollection Headers {           
-                       get { 
+                       get {
                                return webHeaders; 
                        }
                }
 
-#if NET_2_0
                static Exception GetMustImplement ()
                {
                        return new NotImplementedException ();
@@ -177,10 +190,10 @@ namespace System.Net
                                throw GetMustImplement ();
                        }
                }
-#endif
                
                public DateTime LastModified {
                        get {
+                               CheckDisposed ();
                                try {
                                        string dtStr = webHeaders ["Last-Modified"];
                                        return MonoHttpDate.Parse (dtStr);
@@ -191,51 +204,51 @@ 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 { 
+                       get {
                                return statusCode; 
                        }
                }
                
                public string StatusDescription {
-                       get { 
+                       get {
+                               CheckDisposed ();
                                return statusDescription; 
                        }
                }
 
                // Methods
-#if !NET_2_0
-               public override int GetHashCode ()
-               {
-                       return base.GetHashCode ();
-               }
-#endif
                
                public string GetResponseHeader (string headerName)
                {
+                       CheckDisposed ();
                        string value = webHeaders [headerName];
                        return (value != null) ? value : "";
                }
@@ -268,11 +281,8 @@ namespace System.Net
                        GetObjectData (serializationInfo, streamingContext);
                }
 
-#if NET_2_0
-               protected override
-#endif
-               void GetObjectData (SerializationInfo serializationInfo,
-                                   StreamingContext streamingContext)
+               protected override void GetObjectData (SerializationInfo serializationInfo,
+                       StreamingContext streamingContext)
                {
                        SerializationInfo info = serializationInfo;
 
@@ -299,9 +309,6 @@ namespace System.Net
                        GC.SuppressFinalize (this);  
                }
 
-#if !NET_2_0
-               protected virtual
-#endif
                void Dispose (bool disposing) 
                {
                        if (this.disposed)
@@ -311,7 +318,6 @@ namespace System.Net
                        if (disposing) {
                                // release managed resources
                                uri = null;
-                               webHeaders = null;
                                cookieCollection = null;
                                method = null;
                                version = null;
@@ -381,11 +387,9 @@ namespace System.Net
                                        if (cookie.Domain == "")
                                                cookie.Domain = val;
                                        break;
-#if NET_2_0
                                case "HTTPONLY":
                                        cookie.HttpOnly = true;
                                        break;
-#endif
                                case "MAX-AGE": // RFC Style Set-Cookie2
                                        if (cookie.Expires == DateTime.MinValue) {
                                                try {
@@ -452,14 +456,8 @@ namespace System.Net
                                        DateTime cookieExpiresUtc = DateTime.ParseExact (value, cookieExpiresFormats [i], CultureInfo.InvariantCulture);
 
                                        //convert UTC/GMT time to local time
-#if NET_2_0
                                        cookieExpiresUtc = DateTime.SpecifyKind (cookieExpiresUtc, DateTimeKind.Utc);
                                        return TimeZone.CurrentTimeZone.ToLocalTime (cookieExpiresUtc);
-#else
-                                       //DateTime.Kind is only available on .NET 2.0, so do some calculation
-                                       TimeSpan localOffset = TimeZone.CurrentTimeZone.GetUtcOffset (cookieExpiresUtc.Date);
-                                       return cookieExpiresUtc.Add (localOffset);
-#endif
                                } catch {}
                        }