New tests.
[mono.git] / mcs / class / System / System.Net / WebHeaderCollection.cs
index 6a58fc90c6640404ad84f2e24ecd68d1f2e89b2a..16f77533a40dfc0c95c8c2420a44d110c8f28723 100644 (file)
@@ -4,10 +4,12 @@
 // Authors:
 //     Lawrence Pit (loz@cable.a2000.nl)
 //     Gonzalo Paniagua Javier (gonzalo@ximian.com)
+//      Miguel de Icaza (miguel@novell.com)
+//
+// Copyright 2003 Ximian, Inc. (http://www.ximian.com)
+// Copyright 2007 Novell, Inc. (http://www.novell.com)
 //
-// (c) 2003 Ximian, Inc. (http://www.ximian.com)
 //
-
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -31,6 +33,7 @@
 
 using System;
 using System.Collections;
+using System.Collections.Generic;
 using System.Collections.Specialized;
 using System.Runtime.InteropServices;
 using System.Runtime.Serialization;
@@ -40,12 +43,16 @@ using System.Text;
     
 namespace System.Net 
 {
+#if MOONLIGHT
+       internal class WebHeaderCollection : NameValueCollection, ISerializable {
+#else
        [Serializable]
        [ComVisible(true)]
-       public class WebHeaderCollection : NameValueCollection, ISerializable
-       {
+       public class WebHeaderCollection : NameValueCollection, ISerializable {
+#endif
                private static readonly Hashtable restricted;
                private static readonly Hashtable multiValue;
+               static readonly Dictionary<string, bool> restricted_response;
                private bool internallyCreated = false;
                
                // Static Initializer
@@ -54,8 +61,8 @@ namespace System.Net
                {
                        // the list of restricted header names as defined 
                        // by the ms.net spec
-                       restricted = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
-                                                   CaseInsensitiveComparer.Default);
+                       restricted = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
+                                                   CaseInsensitiveComparer.DefaultInvariant);
 
                        restricted.Add ("accept", true);
                        restricted.Add ("connection", true);
@@ -69,11 +76,18 @@ namespace System.Net
                        restricted.Add ("referer", true);
                        restricted.Add ("transfer-encoding", true);
                        restricted.Add ("user-agent", true);                    
-                       
+                       restricted.Add ("proxy-connection", true);                      
+
+                       //
+                       restricted_response = new Dictionary<string, bool> (StringComparer.InvariantCultureIgnoreCase);
+                       restricted_response.Add ("Content-Length", true);
+                       restricted_response.Add ("Transfer-Encoding", true);
+                       restricted_response.Add ("WWW-Authenticate", true);
+
                        // see par 14 of RFC 2068 to see which header names
                        // accept multiple values each separated by a comma
-                       multiValue = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
-                                                   CaseInsensitiveComparer.Default);
+                       multiValue = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
+                                                   CaseInsensitiveComparer.DefaultInvariant);
 
                        multiValue.Add ("accept", true);
                        multiValue.Add ("accept-charset", true);
@@ -97,6 +111,7 @@ namespace System.Net
                        multiValue.Add ("vary", true);
                        multiValue.Add ("via", true);
                        multiValue.Add ("warning", true);
+                       multiValue.Add ("www-authenticate", true);
 
                        // Extra
                        multiValue.Add ("set-cookie", true);
@@ -110,11 +125,20 @@ namespace System.Net
                protected WebHeaderCollection (SerializationInfo serializationInfo, 
                                               StreamingContext streamingContext)
                {
-                       // TODO: test for compatibility with ms.net
-                       int count = serializationInfo.GetInt32("count");
-                       for (int i = 0; i < count; i++) 
-                               this.Add (serializationInfo.GetString ("k" + i),
-                                         serializationInfo.GetString ("v" + i));
+                       int count;
+
+                       try {
+                               count = serializationInfo.GetInt32("Count");
+                               for (int i = 0; i < count; i++) 
+                                       this.Add (serializationInfo.GetString (i.ToString ()),
+                                                 serializationInfo.GetString ((count + i).ToString ()));
+                       } catch (SerializationException){
+                               count = serializationInfo.GetInt32("count");
+                               for (int i = 0; i < count; i++) 
+                                       this.Add (serializationInfo.GetString ("k" + i),
+                                                 serializationInfo.GetString ("v" + i));
+                       }
+                       
                }
                
                internal WebHeaderCollection (bool internallyCreated)
@@ -175,6 +199,16 @@ namespace System.Net
                        return values;
                }
 
+               public override string[] GetValues (int index)
+               {
+                       string[] values = base.GetValues (index);
+                       if (values == null || values.Length == 0) {
+                               return(null);
+                       }
+                       
+                       return(values);
+               }
+
                /* Now i wonder why this is here...
                static string [] GetMultipleValues (string [] values)
                {
@@ -221,6 +255,23 @@ namespace System.Net
                        if (headerName == "") // MS throw nullexception here!
                                throw new ArgumentException ("empty string", "headerName");
 
+                       if (!IsHeaderName (headerName))
+                               throw new ArgumentException ("Invalid character in header");
+
+                       return restricted.ContainsKey (headerName);
+               }
+
+               public static bool IsRestricted (string headerName, bool response)
+               {
+                       if (String.IsNullOrEmpty (headerName))
+                               throw new ArgumentNullException ("headerName");
+
+                       if (!IsHeaderName (headerName))
+                               throw new ArgumentException ("Invalid character in header");
+
+
+                       if (response)
+                               return restricted_response.ContainsKey (headerName);
                        return restricted.ContainsKey (headerName);
                }
 
@@ -272,18 +323,282 @@ namespace System.Net
                                  
                        return sb.Append("\r\n").ToString();
                }
-               
+#if !TARGET_JVM
                void ISerializable.GetObjectData (SerializationInfo serializationInfo,
-                                                 StreamingContext streamingContext)
+                                                 StreamingContext streamingContext)
+               {
+                       GetObjectData (serializationInfo, streamingContext);
+               }
+#endif
+               public override void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
                {
                        int count = base.Count;
-                       serializationInfo.AddValue ("count", count);
-                       for (int i = 0; i < count ; i++) {
-                               serializationInfo.AddValue ("k" + i, GetKey (i));
-                               serializationInfo.AddValue ("v" + i, Get (i));
+                       serializationInfo.AddValue ("Count", count);
+                       for (int i = 0; i < count; i++) {
+                               serializationInfo.AddValue (i.ToString (), GetKey (i));
+                               serializationInfo.AddValue ((count + i).ToString (), Get (i));
+                       }
+               }
+
+               public override string[] AllKeys
+               {
+                       get {
+                               return(base.AllKeys);
+                       }
+               }
+               
+               public override int Count 
+               {
+                       get {
+                               return(base.Count);
+                       }
+               }
+
+               public override KeysCollection Keys
+               {
+                       get {
+                               return(base.Keys);
                        }
                }
+
+               public override string Get (int index)
+               {
+                       return(base.Get (index));
+               }
+               
+               public override string Get (string name)
+               {
+                       return(base.Get (name));
+               }
                
+               public override string GetKey (int index)
+               {
+                       return(base.GetKey (index));
+               }
+
+               public void Add (HttpRequestHeader header, string value)
+               {
+                       Add (RequestHeaderToString (header), value);
+               }
+
+               public void Remove (HttpRequestHeader header)
+               {
+                       Remove (RequestHeaderToString (header));
+               }
+
+               public void Set (HttpRequestHeader header, string value)
+               {
+                       Set (RequestHeaderToString (header), value);
+               }
+
+               public void Add (HttpResponseHeader header, string value)
+               {
+                       Add (ResponseHeaderToString (header), value);
+               }
+
+               public void Remove (HttpResponseHeader header)
+               {
+                       Remove (ResponseHeaderToString (header));
+               }
+
+               public void Set (HttpResponseHeader header, string value)
+               {
+                       Set (ResponseHeaderToString (header), value);
+               }
+
+               string RequestHeaderToString (HttpRequestHeader value)
+               {
+                       switch (value){
+                       case HttpRequestHeader.CacheControl:
+                               return "Cache-Control";
+                       case HttpRequestHeader.Connection:
+                               return "Connection";
+                       case HttpRequestHeader.Date:
+                               return "Date";
+                       case HttpRequestHeader.KeepAlive:
+                               return "Keep-Alive";
+                       case HttpRequestHeader.Pragma:
+                               return "Pragma";
+                       case HttpRequestHeader.Trailer:
+                               return "Trailer";
+                       case HttpRequestHeader.TransferEncoding:
+                               return "Transfer-Encoding";
+                       case HttpRequestHeader.Upgrade:
+                               return "Upgrade";
+                       case HttpRequestHeader.Via:
+                               return "Via";
+                       case HttpRequestHeader.Warning:
+                               return "Warning";
+                       case HttpRequestHeader.Allow:
+                               return "Allow";
+                       case HttpRequestHeader.ContentLength:
+                               return "Content-Length";
+                       case HttpRequestHeader.ContentType:
+                               return "Content-Type";
+                       case HttpRequestHeader.ContentEncoding:
+                               return "Content-Encoding";
+                       case HttpRequestHeader.ContentLanguage:
+                               return "Content-Language";
+                       case HttpRequestHeader.ContentLocation:
+                               return "Content-Location";
+                       case HttpRequestHeader.ContentMd5:
+                               return "Content-MD5";
+                       case HttpRequestHeader.ContentRange:
+                               return "Content-Range";
+                       case HttpRequestHeader.Expires:
+                               return "Expires";
+                       case HttpRequestHeader.LastModified:
+                               return "Last-Modified";
+                       case HttpRequestHeader.Accept:
+                               return "Accept";
+                       case HttpRequestHeader.AcceptCharset:
+                               return "Accept-Charset";
+                       case HttpRequestHeader.AcceptEncoding:
+                               return "Accept-Encoding";
+                       case HttpRequestHeader.AcceptLanguage:
+                               return "accept-language";
+                       case HttpRequestHeader.Authorization:
+                               return "Authorization";
+                       case HttpRequestHeader.Cookie:
+                               return "Cookie";
+                       case HttpRequestHeader.Expect:
+                               return "Expect";
+                       case HttpRequestHeader.From:
+                               return "From";
+                       case HttpRequestHeader.Host:
+                               return "Host";
+                       case HttpRequestHeader.IfMatch:
+                               return "If-Match";
+                       case HttpRequestHeader.IfModifiedSince:
+                               return "If-Modified-Since";
+                       case HttpRequestHeader.IfNoneMatch:
+                               return "If-None-Match";
+                       case HttpRequestHeader.IfRange:
+                               return "If-Range";
+                       case HttpRequestHeader.IfUnmodifiedSince:
+                               return "If-Unmodified-Since";
+                       case HttpRequestHeader.MaxForwards:
+                               return "Max-Forwards";
+                       case HttpRequestHeader.ProxyAuthorization:
+                               return "Proxy-Authorization";
+                       case HttpRequestHeader.Referer:
+                               return "Referer";
+                       case HttpRequestHeader.Range:
+                               return "Range";
+                       case HttpRequestHeader.Te:
+                               return "TE";
+                       case HttpRequestHeader.Translate:
+                               return "Translate";
+                       case HttpRequestHeader.UserAgent:
+                               return "User-Agent";
+                       default:
+                               throw new InvalidOperationException ();
+                       }
+               }
+               
+               
+               public string this[HttpRequestHeader hrh]
+               {
+                       get {
+                               return Get (RequestHeaderToString (hrh));
+                       }
+                       
+                       set {
+                               Add (RequestHeaderToString (hrh), value);
+                       }
+               }
+
+               string ResponseHeaderToString (HttpResponseHeader value)
+               {
+                       switch (value){
+                       case HttpResponseHeader.CacheControl:
+                               return "Cache-Control";
+                       case HttpResponseHeader.Connection:
+                               return "Connection";
+                       case HttpResponseHeader.Date:
+                               return "Date";
+                       case HttpResponseHeader.KeepAlive:
+                               return "Keep-Alive";
+                       case HttpResponseHeader.Pragma:
+                               return "Pragma";
+                       case HttpResponseHeader.Trailer:
+                               return "Trailer";
+                       case HttpResponseHeader.TransferEncoding:
+                               return "Transfer-Encoding";
+                       case HttpResponseHeader.Upgrade:
+                               return "Upgrade";
+                       case HttpResponseHeader.Via:
+                               return "Via";
+                       case HttpResponseHeader.Warning:
+                               return "Warning";
+                       case HttpResponseHeader.Allow:
+                               return "Allow";
+                       case HttpResponseHeader.ContentLength:
+                               return "Content-Length";
+                       case HttpResponseHeader.ContentType:
+                               return "Content-Type";
+                       case HttpResponseHeader.ContentEncoding:
+                               return "Content-Encoding";
+                       case HttpResponseHeader.ContentLanguage:
+                               return "Content-Language";
+                       case HttpResponseHeader.ContentLocation:
+                               return "Content-Location";
+                       case HttpResponseHeader.ContentMd5:
+                               return "Content-MD5";
+                       case HttpResponseHeader.ContentRange:
+                               return "Content-Range";
+                       case HttpResponseHeader.Expires:
+                               return "Expires";
+                       case HttpResponseHeader.LastModified:
+                               return "Last-Modified";
+                       case HttpResponseHeader.AcceptRanges:
+                               return "Accept-Ranges";
+                       case HttpResponseHeader.Age:
+                               return "Age";
+                       case HttpResponseHeader.ETag:
+                               return "ETag";
+                       case HttpResponseHeader.Location:
+                               return "Location";
+                       case HttpResponseHeader.ProxyAuthenticate:
+                               return "Proxy-Authenticate";
+                       case HttpResponseHeader.RetryAfter:
+                               return "Retry-After";
+                       case HttpResponseHeader.Server:
+                               return "Server";
+                       case HttpResponseHeader.SetCookie:
+                               return "Set-Cookie";
+                       case HttpResponseHeader.Vary:
+                               return "Vary";
+                       case HttpResponseHeader.WwwAuthenticate:
+                               return "WWW-Authenticate";
+                       default:
+                               throw new InvalidOperationException ();
+                       }
+               }
+               public string this[HttpResponseHeader hrh]
+               {
+                       get
+                       {
+                               return Get (ResponseHeaderToString (hrh));
+                       }
+
+                       set
+                       {
+                               Add (ResponseHeaderToString (hrh), value);
+                       }
+               }
+
+               public override void Clear ()
+               {
+                       base.Clear ();
+               }
+
+
+               public override IEnumerator GetEnumerator ()
+               {
+                       return(base.GetEnumerator ());
+               }
+
                // Internal Methods
                
                // With this we don't check for invalid characters in header. See bug #55994.
@@ -367,31 +682,31 @@ namespace System.Net
                
                internal static bool IsHeaderName (string name)
                {
-                       // token          = 1*<any CHAR except CTLs or tspecials>
-                       // tspecials      = "(" | ")" | "<" | ">" | "@"
-                       //                | "," | ";" | ":" | "\" | <">
-                       //                | "/" | "[" | "]" | "?" | "="
-                       //                | "{" | "}" | SP | HT
-                       
                        if (name == null || name.Length == 0)
                                return false;
 
                        int len = name.Length;
                        for (int i = 0; i < len; i++) {                 
                                char c = name [i];
-                               if (c < 0x20 || c >= 0x7f)
+                               if (c > 126 || !allowed_chars [(int) c])
                                        return false;
                        }
                        
-                       return name.IndexOfAny (tspecials) == -1;
+                       return true;
                }
 
-               private static char [] tspecials = 
-                               new char [] {'(', ')', '<', '>', '@',
-                                            ',', ';', ':', '\\', '"',
-                                            '/', '[', ']', '?', '=',
-                                            '{', '}', ' ', '\t'};
-                                                       
+               static bool [] allowed_chars = new bool [126] {
+                       false, false, false, false, false, false, false, false, false, false, false, false, false, false,
+                       false, false, false, false, false, false, false, false, false, false, false, false, false, false,
+                       false, false, false, false, false, true, false, true, true, true, true, false, false, false, true,
+                       true, false, true, true, false, true, true, true, true, true, true, true, true, true, true, false,
+                       false, false, false, false, false, false, true, true, true, true, true, true, true, true, true,
+                       true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
+                       false, false, false, true, true, true, true, true, true, true, true, true, true, true, true, true,
+                       true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
+                       false, true, false
+                       };
        }
 }
 
+