[System] HttpListenerRequest: ignore bad cookies and keep request alive (#5657)
[mono.git] / mcs / class / System / System.Net / HttpWebResponse.cs
index 79e61a436249dc4f6653567b75d6080e397f4c72..5a32084972359603e7f55acefeefae274fc4477e 100644 (file)
@@ -86,10 +86,14 @@ namespace System.Net
                        }
 
                        string content_encoding = webHeaders ["Content-Encoding"];
-                       if (content_encoding == "gzip" && (data.request.AutomaticDecompression & DecompressionMethods.GZip) != 0)
+                       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)
+                               webHeaders.Remove (HttpRequestHeader.ContentEncoding);
+                       }
+                       else if (content_encoding == "deflate" && (data.request.AutomaticDecompression & DecompressionMethods.Deflate) != 0) {
                                stream = new DeflateStream (stream, CompressionMode.Decompress);
+                               webHeaders.Remove (HttpRequestHeader.ContentEncoding);
+                       }
                }
 
                [Obsolete ("Serialization is obsoleted for this type", false)]
@@ -106,7 +110,7 @@ namespace System.Net
                        version = (Version) info.GetValue ("version", typeof (Version));
                        statusCode = (HttpStatusCode) info.GetValue ("statusCode", typeof (HttpStatusCode));
                }
-               
+
                // Properties
                
                public string CharacterSet {
@@ -155,9 +159,7 @@ namespace System.Net
                        }
                }
                
-#if NET_4_5
                virtual
-#endif
                public CookieCollection Cookies {
                        get {
                                CheckDisposed ();
@@ -202,9 +204,7 @@ namespace System.Net
                        }
                }
                
-#if NET_4_5
                virtual
-#endif
                public string Method {
                        get {
                                CheckDisposed ();
@@ -229,22 +229,18 @@ namespace System.Net
                public string Server {
                        get {
                                CheckDisposed ();
-                               return webHeaders ["Server"]
+                               return webHeaders ["Server"] ?? "";
                        }
                }
                
-#if NET_4_5
                virtual
-#endif
                public HttpStatusCode StatusCode {
                        get {
                                return statusCode; 
                        }
                }
                
-#if NET_4_5
                virtual
-#endif
                public string StatusDescription {
                        get {
                                CheckDisposed ();
@@ -252,6 +248,12 @@ namespace System.Net
                        }
                }
 
+               public override bool SupportsHeaders {
+                       get {
+                               return true;
+                       }
+               }
+
                // Methods
                
                public string GetResponseHeader (string headerName)
@@ -321,20 +323,11 @@ namespace System.Net
                        Dispose (true);
                }
                
-#if NET_4_0
                protected override void Dispose (bool disposing)
                {
                        this.disposed = true;
                        base.Dispose (true);
                }
-#else
-               void Dispose (bool disposing) 
-               {
-                       this.disposed = true;
-                       if (disposing)
-                               Close ();
-               }
-#endif
                
                private void CheckDisposed () 
                {
@@ -351,46 +344,29 @@ namespace System.Net
                        // Don't terminate response reading on bad cookie value
                        //
                        string value;
+                       CookieCollection cookies = null;
                        try {
                                value = webHeaders.Get ("Set-Cookie");
-                               if (value != null && SetCookie (value))
-                                       return;
+                               if (value != null)
+                                       cookies = cookie_container.CookieCutter (uri, HttpKnownHeaderNames.SetCookie, value, false);
                        } catch {
                        }
 
                        try {
                                value = webHeaders.Get ("Set-Cookie2");
-                               if (value != null)
-                                       SetCookie (value);
-                       } catch {
-                       }
-               }
+                               if (value != null) {
+                                       var cookies2 = cookie_container.CookieCutter (uri, HttpKnownHeaderNames.SetCookie2, value, false);
 
-               bool SetCookie (string header)
-               {
-                       if (cookieCollection == null)
-                               cookieCollection = new CookieCollection ();
-
-                       bool at_least_one_set = false;
-                       var parser = new CookieParser (header);
-                       foreach (var cookie in parser.Parse ()) {
-                               if (cookie.Domain == "") {
-                                       cookie.Domain = uri.Host;
-                                       cookie.HasDomain = false;
-                               }
-
-                               if (cookie.HasDomain &&
-                                   !CookieContainer.CheckSameOrigin (uri, cookie.Domain))
-                                       continue;
-
-                               cookieCollection.Add (cookie);
-                               if (cookie_container != null) {
-                                       cookie_container.Add (uri, cookie);
-                                       at_least_one_set = true;
+                                       if (cookies != null && cookies.Count != 0) {
+                                               cookies.Add (cookies2);
+                                       } else {
+                                               cookies = cookies2;
+                                       }
                                }
+                       } catch {
                        }
 
-                       return at_least_one_set;
+                       this.cookieCollection = cookies;
                }
        }       
 }