Commit
[mono.git] / mcs / class / System / System.Net / HttpWebResponse.cs
index 76e588eac356953310dc2868a2b901b9bc3d5038..3dc8bb97ec1cee776c1679b473e099153dda62a5 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
 //
 
 //
@@ -52,7 +54,7 @@ namespace System.Net
                string statusDescription;
                long contentLength = -1;
                string contentType;
-               CookieContainer cookieContainer;
+               CookieContainer cookie_container;
 
                bool disposed = false;
                Stream stream;
@@ -69,14 +71,14 @@ namespace System.Net
                        statusDescription = data.StatusDescription;
                        stream = data.stream;
                        if (container != null) {
-                               this.cookieContainer = container;
+                               this.cookie_container = container;      
                                FillCookies ();
-                       } else if (webHeaders != null) {
-                               webHeaders.RemoveInternal ("Set-Cookie");
-                               webHeaders.RemoveInternal ("Set-Cookie2");
                        }
                }
 
+#if NET_2_0
+               [Obsolete ("Serialization is obsoleted for this type", false)]
+#endif
                protected HttpWebResponse (SerializationInfo serializationInfo, StreamingContext streamingContext)
                {
                        SerializationInfo info = serializationInfo;
@@ -118,7 +120,8 @@ namespace System.Net
                public string ContentEncoding {
                        get { 
                                CheckDisposed ();
-                               return webHeaders ["Content-Encoding"];
+                               string h = webHeaders ["Content-Encoding"];
+                               return h != null ? h : "";
                        }
                }
                
@@ -168,6 +171,21 @@ namespace System.Net
                                return webHeaders; 
                        }
                }
+
+#if NET_2_0
+               static Exception GetMustImplement ()
+               {
+                       return new NotImplementedException ();
+               }
+               
+               [MonoTODO]
+               public override bool IsMutuallyAuthenticated
+               {
+                       get {
+                               throw GetMustImplement ();
+                       }
+               }
+#endif
                
                public DateTime LastModified {
                        get {
@@ -263,6 +281,15 @@ namespace System.Net
                
                void ISerializable.GetObjectData (SerializationInfo serializationInfo,
                                                  StreamingContext streamingContext)
+               {
+                       GetObjectData (serializationInfo, streamingContext);
+               }
+
+#if NET_2_0
+               protected override
+#endif
+               void GetObjectData (SerializationInfo serializationInfo,
+                                   StreamingContext streamingContext)
                {
                        SerializationInfo info = serializationInfo;
 
@@ -274,8 +301,7 @@ namespace System.Net
                        info.AddValue ("cookieCollection", cookieCollection);
                        info.AddValue ("version", version);
                        info.AddValue ("statusCode", statusCode);
-               }               
-
+               }
 
                // Cleaning up stuff
 
@@ -372,24 +398,23 @@ 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)
-                                               cookie.Expires = cookie.TimeStamp.AddSeconds (Int32.Parse (val));
+                                       if (cookie.Expires == DateTime.MinValue) {
+                                               try {
+                                               cookie.Expires = cookie.TimeStamp.AddSeconds (UInt32.Parse (val));
+                                               } catch {}
+                                       }
                                        break;
                                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;
@@ -402,7 +427,9 @@ namespace System.Net
                                        cookie.Secure = true;
                                        break;
                                case "VERSION":
-                                       cookie.Version = Int32.Parse (val);
+                                       try {
+                                               cookie.Version = (int) UInt32.Parse (val);
+                                       } catch {}
                                        break;
                                }
                        }
@@ -414,8 +441,8 @@ namespace System.Net
                                cookie.Domain = uri.Host;
 
                        cookieCollection.Add (cookie);
-                       if (cookieContainer != null)
-                               cookieContainer.Add (uri, cookie);
+                       if (cookie_container != null)
+                               cookie_container.Add (uri, cookie);
                }
 
                void SetCookie2 (string cookies_str)
@@ -425,6 +452,37 @@ 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
+#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 {}
+                       }
+
+                       //If we can't parse Expires, use cookie as session cookie (expires is DateTime.MinValue)
+                       return DateTime.MinValue;
+               }
        }       
 
        class CookieParser {