2 // System.Net.WebHeaderCollection
\r
5 // Lawrence Pit (loz@cable.a2000.nl)
\r
9 using System.Collections;
\r
10 using System.Collections.Specialized;
\r
11 using System.Runtime.InteropServices;
\r
12 using System.Runtime.Serialization;
\r
15 // See RFC 2068 par 4.2 Message Headers
\r
17 namespace System.Net
\r
21 public class WebHeaderCollection : NameValueCollection, ISerializable
\r
23 private static readonly int [] restricted;
\r
24 private static readonly int [] multiValue;
\r
25 private bool internallyCreated = false;
\r
27 // Static Initializer
\r
29 static WebHeaderCollection ()
\r
31 // For performance reasons we initialize the following
\r
32 // tables by taking the hashcode of header names.
\r
33 // When you add a header make sure all characters are in
\r
36 // the list of restricted header names as defined
\r
37 // by the ms.net spec
\r
38 ArrayList a = new ArrayList ();
\r
39 a.Add ("accept".GetHashCode ());
\r
40 a.Add ("connection".GetHashCode ());
\r
41 a.Add ("content-length".GetHashCode ());
\r
42 a.Add ("content-type".GetHashCode ());
\r
43 a.Add ("date".GetHashCode ());
\r
44 a.Add ("expect".GetHashCode ()); // ??? What is this anyway?
\r
45 a.Add ("host".GetHashCode ());
\r
46 a.Add ("range".GetHashCode ());
\r
47 a.Add ("referer".GetHashCode ());
\r
48 a.Add ("transfer-encoding".GetHashCode ());
\r
49 a.Add ("user-agent".GetHashCode ());
\r
50 restricted = (int []) a.ToArray (typeof (int));
\r
52 // see par 14 of RFC 2068 to see which header names
\r
53 // accept multiple values each separated by a comma
\r
54 a = new ArrayList ();
\r
55 a.Add ("accept".GetHashCode ());
\r
56 a.Add ("accept-charset".GetHashCode ());
\r
57 a.Add ("accept-encoding".GetHashCode ());
\r
58 a.Add ("accept-language".GetHashCode ());
\r
59 a.Add ("accept-ranges".GetHashCode ());
\r
60 a.Add ("allow".GetHashCode ());
\r
61 a.Add ("authorization".GetHashCode ());
\r
62 a.Add ("cache-control".GetHashCode ());
\r
63 a.Add ("connection".GetHashCode ());
\r
64 a.Add ("content-encoding".GetHashCode ());
\r
65 a.Add ("content-language".GetHashCode ());
\r
66 a.Add ("expect".GetHashCode ());
\r
67 a.Add ("if-match".GetHashCode ());
\r
68 a.Add ("if-none-match".GetHashCode ());
\r
69 a.Add ("proxy-authenticate".GetHashCode ());
\r
70 a.Add ("public".GetHashCode ());
\r
71 a.Add ("range".GetHashCode ());
\r
72 a.Add ("transfer-encoding".GetHashCode ());
\r
73 a.Add ("upgrade".GetHashCode ());
\r
74 a.Add ("vary".GetHashCode ());
\r
75 a.Add ("via".GetHashCode ());
\r
76 a.Add ("warning".GetHashCode ());
\r
77 multiValue = (int []) a.ToArray (typeof (int));
\r
82 public WebHeaderCollection () { }
\r
84 protected WebHeaderCollection (SerializationInfo serializationInfo,
\r
85 StreamingContext streamingContext)
\r
87 // TODO: test for compatibility with ms.net
\r
88 int count = serializationInfo.GetInt32("count");
\r
89 for (int i = 0; i < count; i++)
\r
90 this.Add (serializationInfo.GetString ("k" + i),
\r
91 serializationInfo.GetString ("v" + i));
\r
94 internal WebHeaderCollection (bool dummy) : base ()
\r
96 this.internallyCreated = true;
\r
101 public void Add (string header)
\r
103 if (header == null)
\r
104 throw new ArgumentNullException ("header");
\r
105 int pos = header.IndexOf (':');
\r
107 throw new ArgumentException ("no colon found");
\r
108 this.Add (header.Substring (0, pos),
\r
109 header.Substring (pos + 1));
\r
112 public override void Add (string name, string value)
\r
115 throw new ArgumentNullException ("name");
\r
116 if (internallyCreated && IsRestricted (name))
\r
117 throw new ArgumentException ("restricted header");
\r
118 this.AddWithoutValidate (name, value);
\r
121 protected void AddWithoutValidate (string headerName, string headerValue)
\r
123 if (!IsHeaderName (headerName))
\r
124 throw new ArgumentException ("invalid header name");
\r
125 if (headerValue == null)
\r
126 headerValue = String.Empty;
\r
128 headerValue = headerValue.Trim ();
\r
129 if (!IsHeaderValue (headerValue))
\r
130 throw new ArgumentException ("invalid header value");
\r
131 base.Add (headerName, headerValue);
\r
134 public override string [] GetValues (string header)
\r
136 if (header == null)
\r
137 throw new ArgumentNullException ("header");
\r
138 string [] values = base.GetValues (header);
\r
139 if (values == null || values.Length == 0)
\r
141 if (!IsMultiValue (header))
\r
143 StringCollection col = new StringCollection ();
\r
144 for (int i = 0; i < values.Length; i++) {
\r
145 string [] s = values [i].Split (new char [] {','});
\r
146 for (int j = 0; j < s.Length; j++)
\r
147 s [j] = s [j].Trim ();
\r
150 values = new string [col.Count];
\r
151 col.CopyTo (values, 0);
\r
155 public static bool IsRestricted (string headerName)
\r
157 int hashCode = headerName.ToLower ().GetHashCode ();
\r
158 for (int i = 0; i < restricted.Length; i++)
\r
159 if (restricted [i] == hashCode)
\r
165 public override void OnDeserialization (object sender)
\r
167 // no idea what to do here... spec doesn't say much
\r
168 throw new NotImplementedException ();
\r
171 public override void Remove (string name)
\r
174 throw new ArgumentNullException ("name");
\r
175 if (internallyCreated && IsRestricted (name))
\r
176 throw new ArgumentException ("restricted header");
\r
177 base.Remove (name);
\r
180 public override void Set (string name, string value)
\r
183 throw new ArgumentNullException ("name");
\r
184 if (internallyCreated && IsRestricted (name))
\r
185 throw new ArgumentException ("restricted header");
\r
186 if (!IsHeaderName (name))
\r
187 throw new ArgumentException ("invalid header name");
\r
189 value = String.Empty;
\r
191 value = value.Trim ();
\r
192 if (!IsHeaderValue (value))
\r
193 throw new ArgumentException ("invalid header value");
\r
194 base.Set (name, value);
\r
197 public byte[] ToByteArray ()
\r
199 return Encoding.UTF8.GetBytes(ToString ());
\r
202 public override string ToString ()
\r
204 StringBuilder sb = new StringBuilder();
\r
206 int count = base.Count;
\r
207 for (int i = 0; i < count ; i++)
\r
208 sb.Append (GetKey (i))
\r
213 return sb.Append("\r\n").ToString();
\r
216 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
\r
217 StreamingContext streamingContext)
\r
219 int count = base.Count;
\r
220 serializationInfo.AddValue ("count", count);
\r
221 for (int i = 0; i < count ; i++) {
\r
222 serializationInfo.AddValue ("k" + i, GetKey (i));
\r
223 serializationInfo.AddValue ("v" + i, Get (i));
\r
227 // Internal Methods
\r
229 internal void SetInternal (string name, string value)
\r
232 value = String.Empty;
\r
234 value = value.Trim ();
\r
235 if (!IsHeaderValue (value))
\r
236 throw new ArgumentException ("invalid header value");
\r
237 base.Set (name, value);
\r
240 internal void RemoveInternal (string name)
\r
243 throw new ArgumentNullException ("name");
\r
244 base.Remove (name);
\r
249 private static bool IsMultiValue (string headerName)
\r
251 int hashCode = headerName.ToLower ().GetHashCode ();
\r
252 for (int i = 0; i < multiValue.Length; i++)
\r
253 if (multiValue [i] == hashCode)
\r
258 private bool IsHeaderValue (string value)
\r
260 // TEXT any 8 bit value except CTL's (0-31 and 127)
\r
261 // but including \r\n space and \t
\r
262 // after a newline at least one space or \t must follow
\r
263 // certain header fields allow comments ()
\r
265 int len = value.Length;
\r
266 for (int i = 0; i < len; i++) {
\r
267 char c = value [i];
\r
270 if (c < 0x20 && (c != '\r' && c != '\n' && c != '\t'))
\r
272 if (c == '\n' && ++i < len) {
\r
274 if (c != ' ' && c != '\t')
\r
282 private bool IsHeaderName (string name)
\r
284 // token = 1*<any CHAR except CTLs or tspecials>
\r
285 // tspecials = "(" | ")" | "<" | ">" | "@"
\r
286 // | "," | ";" | ":" | "\" | <">
\r
287 // | "/" | "[" | "]" | "?" | "="
\r
288 // | "{" | "}" | SP | HT
\r
290 if (name == null || name.Length == 0)
\r
293 int len = name.Length;
\r
294 for (int i = 0; i < len; i++) {
\r
296 if (c < 0x20 || c >= 0x7f)
\r
300 return name.IndexOfAny (tspecials) == -1;
\r
303 private static char [] tspecials =
\r
304 new char [] {'(', ')', '<', '>', '@',
\r
305 ',', ';', ':', '\\', '"',
\r
306 '/', '[', ']', '?', '=',
\r
307 '{', '}', ' ', '\t'};
\r