2 // System.Net.WebHeaderCollection
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Runtime.InteropServices;
36 using System.Runtime.Serialization;
39 // See RFC 2068 par 4.2 Message Headers
45 public class WebHeaderCollection : NameValueCollection, ISerializable
47 private static readonly Hashtable restricted;
48 private static readonly Hashtable multiValue;
49 private bool internallyCreated = false;
53 static WebHeaderCollection ()
55 // the list of restricted header names as defined
57 restricted = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
58 CaseInsensitiveComparer.Default);
60 restricted.Add ("accept", true);
61 restricted.Add ("connection", true);
62 restricted.Add ("content-length", true);
63 restricted.Add ("content-type", true);
64 restricted.Add ("date", true);
65 restricted.Add ("expect", true);
66 restricted.Add ("host", true);
67 restricted.Add ("if-modified-since", true);
68 restricted.Add ("range", true);
69 restricted.Add ("referer", true);
70 restricted.Add ("transfer-encoding", true);
71 restricted.Add ("user-agent", true);
73 // see par 14 of RFC 2068 to see which header names
74 // accept multiple values each separated by a comma
75 multiValue = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
76 CaseInsensitiveComparer.Default);
78 multiValue.Add ("accept", true);
79 multiValue.Add ("accept-charset", true);
80 multiValue.Add ("accept-encoding", true);
81 multiValue.Add ("accept-language", true);
82 multiValue.Add ("accept-ranges", true);
83 multiValue.Add ("allow", true);
84 multiValue.Add ("authorization", true);
85 multiValue.Add ("cache-control", true);
86 multiValue.Add ("connection", true);
87 multiValue.Add ("content-encoding", true);
88 multiValue.Add ("content-language", true);
89 multiValue.Add ("expect", true);
90 multiValue.Add ("if-match", true);
91 multiValue.Add ("if-none-match", true);
92 multiValue.Add ("proxy-authenticate", true);
93 multiValue.Add ("public", true);
94 multiValue.Add ("range", true);
95 multiValue.Add ("transfer-encoding", true);
96 multiValue.Add ("upgrade", true);
97 multiValue.Add ("vary", true);
98 multiValue.Add ("via", true);
99 multiValue.Add ("warning", true);
102 multiValue.Add ("set-cookie", true);
103 multiValue.Add ("set-cookie2", true);
108 public WebHeaderCollection () { }
110 protected WebHeaderCollection (SerializationInfo serializationInfo,
111 StreamingContext streamingContext)
113 // TODO: test for compatibility with ms.net
114 int count = serializationInfo.GetInt32("count");
115 for (int i = 0; i < count; i++)
116 this.Add (serializationInfo.GetString ("k" + i),
117 serializationInfo.GetString ("v" + i));
120 internal WebHeaderCollection (bool internallyCreated)
122 this.internallyCreated = internallyCreated;
127 public void Add (string header)
130 throw new ArgumentNullException ("header");
131 int pos = header.IndexOf (':');
133 throw new ArgumentException ("no colon found", "header");
134 this.Add (header.Substring (0, pos),
135 header.Substring (pos + 1));
138 public override void Add (string name, string value)
141 throw new ArgumentNullException ("name");
142 if (internallyCreated && IsRestricted (name))
143 throw new ArgumentException ("This header must be modified with the appropiate property.");
144 this.AddWithoutValidate (name, value);
147 protected void AddWithoutValidate (string headerName, string headerValue)
149 if (!IsHeaderName (headerName))
150 throw new ArgumentException ("invalid header name: " + headerName, "headerName");
151 if (headerValue == null)
152 headerValue = String.Empty;
154 headerValue = headerValue.Trim ();
155 if (!IsHeaderValue (headerValue))
156 throw new ArgumentException ("invalid header value: " + headerValue, "headerValue");
157 base.Add (headerName, headerValue);
160 public override string [] GetValues (string header)
163 throw new ArgumentNullException ("header");
165 string [] values = base.GetValues (header);
166 if (values == null || values.Length == 0)
170 if (IsMultiValue (header)) {
171 values = GetMultipleValues (values);
178 /* Now i wonder why this is here...
179 static string [] GetMultipleValues (string [] values)
181 ArrayList mvalues = new ArrayList (values.Length);
182 StringBuilder sb = null;
183 for (int i = 0; i < values.Length; ++i) {
184 string val = values [i];
185 if (val.IndexOf (',') == -1) {
191 sb = new StringBuilder ();
194 for (int k = 0; k < val.Length; k++) {
198 } else if (!quote && c == ',') {
199 mvalues.Add (sb.ToString ().Trim ());
207 mvalues.Add (sb.ToString ().Trim ());
212 return (string []) mvalues.ToArray (typeof (string));
216 public static bool IsRestricted (string headerName)
218 if (headerName == null)
219 throw new ArgumentNullException ("headerName");
221 if (headerName == "") // MS throw nullexception here!
222 throw new ArgumentException ("empty string", "headerName");
224 return restricted.ContainsKey (headerName);
227 public override void OnDeserialization (object sender)
231 public override void Remove (string name)
234 throw new ArgumentNullException ("name");
235 if (internallyCreated && IsRestricted (name))
236 throw new ArgumentException ("restricted header");
240 public override void Set (string name, string value)
243 throw new ArgumentNullException ("name");
244 if (internallyCreated && IsRestricted (name))
245 throw new ArgumentException ("restricted header");
246 if (!IsHeaderName (name))
247 throw new ArgumentException ("invalid header name");
249 value = String.Empty;
251 value = value.Trim ();
252 if (!IsHeaderValue (value))
253 throw new ArgumentException ("invalid header value");
254 base.Set (name, value);
257 public byte[] ToByteArray ()
259 return Encoding.UTF8.GetBytes(ToString ());
262 public override string ToString ()
264 StringBuilder sb = new StringBuilder();
266 int count = base.Count;
267 for (int i = 0; i < count ; i++)
268 sb.Append (GetKey (i))
273 return sb.Append("\r\n").ToString();
276 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
277 StreamingContext streamingContext)
279 int count = base.Count;
280 serializationInfo.AddValue ("count", count);
281 for (int i = 0; i < count ; i++) {
282 serializationInfo.AddValue ("k" + i, GetKey (i));
283 serializationInfo.AddValue ("v" + i, Get (i));
289 // With this we don't check for invalid characters in header. See bug #55994.
290 internal void SetInternal (string header)
292 int pos = header.IndexOf (':');
294 throw new ArgumentException ("no colon found", "header");
296 SetInternal (header.Substring (0, pos), header.Substring (pos + 1));
299 internal void SetInternal (string name, string value)
302 value = String.Empty;
304 value = value.Trim ();
305 if (!IsHeaderValue (value))
306 throw new ArgumentException ("invalid header value");
308 if (IsMultiValue (name)) {
309 base.Add (name, value);
312 base.Set (name, value);
316 internal void RemoveAndAdd (string name, string value)
319 value = String.Empty;
321 value = value.Trim ();
324 base.Set (name, value);
327 internal void RemoveInternal (string name)
330 throw new ArgumentNullException ("name");
336 internal static bool IsMultiValue (string headerName)
338 if (headerName == null || headerName == "")
341 return multiValue.ContainsKey (headerName);
344 internal static bool IsHeaderValue (string value)
346 // TEXT any 8 bit value except CTL's (0-31 and 127)
347 // but including \r\n space and \t
348 // after a newline at least one space or \t must follow
349 // certain header fields allow comments ()
351 int len = value.Length;
352 for (int i = 0; i < len; i++) {
356 if (c < 0x20 && (c != '\r' && c != '\n' && c != '\t'))
358 if (c == '\n' && ++i < len) {
360 if (c != ' ' && c != '\t')
368 internal static bool IsHeaderName (string name)
370 // token = 1*<any CHAR except CTLs or tspecials>
371 // tspecials = "(" | ")" | "<" | ">" | "@"
372 // | "," | ";" | ":" | "\" | <">
373 // | "/" | "[" | "]" | "?" | "="
374 // | "{" | "}" | SP | HT
376 if (name == null || name.Length == 0)
379 int len = name.Length;
380 for (int i = 0; i < len; i++) {
382 if (c < 0x20 || c >= 0x7f)
386 return name.IndexOfAny (tspecials) == -1;
389 private static char [] tspecials =
390 new char [] {'(', ')', '<', '>', '@',
391 ',', ';', ':', '\\', '"',
392 '/', '[', ']', '?', '=',
393 '{', '}', ' ', '\t'};