2 // System.Net.WebHeaderCollection
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Miguel de Icaza (miguel@novell.com)
9 // Copyright 2003 Ximian, Inc. (http://www.ximian.com)
10 // Copyright 2007 Novell, Inc. (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections;
36 using System.Collections.Specialized;
37 using System.Runtime.InteropServices;
38 using System.Runtime.Serialization;
41 // See RFC 2068 par 4.2 Message Headers
47 public class WebHeaderCollection : NameValueCollection, ISerializable
49 private static readonly Hashtable restricted;
50 private static readonly Hashtable multiValue;
51 private bool internallyCreated = false;
55 static WebHeaderCollection ()
57 // the list of restricted header names as defined
59 restricted = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
60 CaseInsensitiveComparer.Default);
62 restricted.Add ("accept", true);
63 restricted.Add ("connection", true);
64 restricted.Add ("content-length", true);
65 restricted.Add ("content-type", true);
66 restricted.Add ("date", true);
67 restricted.Add ("expect", true);
68 restricted.Add ("host", true);
69 restricted.Add ("if-modified-since", true);
70 restricted.Add ("range", true);
71 restricted.Add ("referer", true);
72 restricted.Add ("transfer-encoding", true);
73 restricted.Add ("user-agent", true);
75 // see par 14 of RFC 2068 to see which header names
76 // accept multiple values each separated by a comma
77 multiValue = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
78 CaseInsensitiveComparer.Default);
80 multiValue.Add ("accept", true);
81 multiValue.Add ("accept-charset", true);
82 multiValue.Add ("accept-encoding", true);
83 multiValue.Add ("accept-language", true);
84 multiValue.Add ("accept-ranges", true);
85 multiValue.Add ("allow", true);
86 multiValue.Add ("authorization", true);
87 multiValue.Add ("cache-control", true);
88 multiValue.Add ("connection", true);
89 multiValue.Add ("content-encoding", true);
90 multiValue.Add ("content-language", true);
91 multiValue.Add ("expect", true);
92 multiValue.Add ("if-match", true);
93 multiValue.Add ("if-none-match", true);
94 multiValue.Add ("proxy-authenticate", true);
95 multiValue.Add ("public", true);
96 multiValue.Add ("range", true);
97 multiValue.Add ("transfer-encoding", true);
98 multiValue.Add ("upgrade", true);
99 multiValue.Add ("vary", true);
100 multiValue.Add ("via", true);
101 multiValue.Add ("warning", true);
102 multiValue.Add ("www-authenticate", true);
105 multiValue.Add ("set-cookie", true);
106 multiValue.Add ("set-cookie2", true);
111 public WebHeaderCollection () { }
113 protected WebHeaderCollection (SerializationInfo serializationInfo,
114 StreamingContext streamingContext)
116 int count = serializationInfo.GetInt32("Count");
117 for (int i = 0; i < count; i++)
118 this.Add (serializationInfo.GetString (i.ToString ()),
119 serializationInfo.GetString ((count + i).ToString ()));
122 internal WebHeaderCollection (bool internallyCreated)
124 this.internallyCreated = internallyCreated;
129 public void Add (string header)
132 throw new ArgumentNullException ("header");
133 int pos = header.IndexOf (':');
135 throw new ArgumentException ("no colon found", "header");
136 this.Add (header.Substring (0, pos),
137 header.Substring (pos + 1));
140 public override void Add (string name, string value)
143 throw new ArgumentNullException ("name");
144 if (internallyCreated && IsRestricted (name))
145 throw new ArgumentException ("This header must be modified with the appropiate property.");
146 this.AddWithoutValidate (name, value);
149 protected void AddWithoutValidate (string headerName, string headerValue)
151 if (!IsHeaderName (headerName))
152 throw new ArgumentException ("invalid header name: " + headerName, "headerName");
153 if (headerValue == null)
154 headerValue = String.Empty;
156 headerValue = headerValue.Trim ();
157 if (!IsHeaderValue (headerValue))
158 throw new ArgumentException ("invalid header value: " + headerValue, "headerValue");
159 base.Add (headerName, headerValue);
162 public override string [] GetValues (string header)
165 throw new ArgumentNullException ("header");
167 string [] values = base.GetValues (header);
168 if (values == null || values.Length == 0)
172 if (IsMultiValue (header)) {
173 values = GetMultipleValues (values);
180 /* Now i wonder why this is here...
181 static string [] GetMultipleValues (string [] values)
183 ArrayList mvalues = new ArrayList (values.Length);
184 StringBuilder sb = null;
185 for (int i = 0; i < values.Length; ++i) {
186 string val = values [i];
187 if (val.IndexOf (',') == -1) {
193 sb = new StringBuilder ();
196 for (int k = 0; k < val.Length; k++) {
200 } else if (!quote && c == ',') {
201 mvalues.Add (sb.ToString ().Trim ());
209 mvalues.Add (sb.ToString ().Trim ());
214 return (string []) mvalues.ToArray (typeof (string));
218 public static bool IsRestricted (string headerName)
220 if (headerName == null)
221 throw new ArgumentNullException ("headerName");
223 if (headerName == "") // MS throw nullexception here!
224 throw new ArgumentException ("empty string", "headerName");
226 return restricted.ContainsKey (headerName);
229 public override void OnDeserialization (object sender)
233 public override void Remove (string name)
236 throw new ArgumentNullException ("name");
237 if (internallyCreated && IsRestricted (name))
238 throw new ArgumentException ("restricted header");
242 public override void Set (string name, string value)
245 throw new ArgumentNullException ("name");
246 if (internallyCreated && IsRestricted (name))
247 throw new ArgumentException ("restricted header");
248 if (!IsHeaderName (name))
249 throw new ArgumentException ("invalid header name");
251 value = String.Empty;
253 value = value.Trim ();
254 if (!IsHeaderValue (value))
255 throw new ArgumentException ("invalid header value");
256 base.Set (name, value);
259 public byte[] ToByteArray ()
261 return Encoding.UTF8.GetBytes(ToString ());
264 public override string ToString ()
266 StringBuilder sb = new StringBuilder();
268 int count = base.Count;
269 for (int i = 0; i < count ; i++)
270 sb.Append (GetKey (i))
275 return sb.Append("\r\n").ToString();
278 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
279 StreamingContext streamingContext)
281 int count = base.Count;
282 serializationInfo.AddValue ("Count", count);
283 for (int i = 0; i < count; i++) {
284 serializationInfo.AddValue (i.ToString (), GetKey (i));
285 serializationInfo.AddValue ((count + i).ToString (), Get (i));
290 string RequestHeaderToString (HttpRequestHeader value)
293 case HttpRequestHeader.CacheControl:
294 return "cache-control";
295 case HttpRequestHeader.Connection:
297 case HttpRequestHeader.Date:
299 case HttpRequestHeader.KeepAlive:
301 case HttpRequestHeader.Pragma:
303 case HttpRequestHeader.Trailer:
305 case HttpRequestHeader.TransferEncoding:
306 return "transfer-encoding";
307 case HttpRequestHeader.Upgrade:
309 case HttpRequestHeader.Via:
311 case HttpRequestHeader.Warning:
313 case HttpRequestHeader.Allow:
315 case HttpRequestHeader.ContentLength:
316 return "content-length";
317 case HttpRequestHeader.ContentType:
318 return "content-type";
319 case HttpRequestHeader.ContentEncoding:
320 return "content-encoding";
321 case HttpRequestHeader.ContentLanguage:
322 return "content-language";
323 case HttpRequestHeader.ContentLocation:
324 return "content-location";
325 case HttpRequestHeader.ContentMd5:
326 return "content-md5";
327 case HttpRequestHeader.ContentRange:
328 return "content-range";
329 case HttpRequestHeader.Expires:
331 case HttpRequestHeader.LastModified:
332 return "last-modified";
333 case HttpRequestHeader.Accept:
335 case HttpRequestHeader.AcceptCharset:
336 return "accept-charset";
337 case HttpRequestHeader.AcceptEncoding:
338 return "accept-encoding";
339 case HttpRequestHeader.AcceptLanguage:
340 return "accept-language";
341 case HttpRequestHeader.Authorization:
342 return "authorization";
343 case HttpRequestHeader.Cookie:
345 case HttpRequestHeader.Expect:
347 case HttpRequestHeader.From:
349 case HttpRequestHeader.Host:
351 case HttpRequestHeader.IfMatch:
353 case HttpRequestHeader.IfModifiedSince:
354 return "if-modified-since";
355 case HttpRequestHeader.IfNoneMatch:
356 return "if-none-match";
357 case HttpRequestHeader.IfRange:
359 case HttpRequestHeader.IfUnmodifiedSince:
360 return "if-unmodified-since";
361 case HttpRequestHeader.MaxForwards:
362 return "max-forwards";
363 case HttpRequestHeader.ProxyAuthorization:
364 return "proxy-authorization";
365 case HttpRequestHeader.Referer:
367 case HttpRequestHeader.Range:
369 case HttpRequestHeader.Te:
371 case HttpRequestHeader.Translate:
373 case HttpRequestHeader.UserAgent:
376 throw new InvalidOperationException ();
380 public string this[HttpRequestHeader hrh]
383 return Get (RequestHeaderToString (hrh));
387 Add (RequestHeaderToString (hrh), value);
391 string ResponseHeaderToString (HttpResponseHeader value)
394 case HttpResponseHeader.CacheControl:
395 return "cache-control";
396 case HttpResponseHeader.Connection:
398 case HttpResponseHeader.Date:
400 case HttpResponseHeader.KeepAlive:
402 case HttpResponseHeader.Pragma:
404 case HttpResponseHeader.Trailer:
406 case HttpResponseHeader.TransferEncoding:
407 return "transfer-encoding";
408 case HttpResponseHeader.Upgrade:
410 case HttpResponseHeader.Via:
412 case HttpResponseHeader.Warning:
414 case HttpResponseHeader.Allow:
416 case HttpResponseHeader.ContentLength:
417 return "content-length";
418 case HttpResponseHeader.ContentType:
419 return "content-type";
420 case HttpResponseHeader.ContentEncoding:
421 return "content-encoding";
422 case HttpResponseHeader.ContentLanguage:
423 return "content-language";
424 case HttpResponseHeader.ContentLocation:
425 return "content-location";
426 case HttpResponseHeader.ContentMd5:
427 return "content-md5";
428 case HttpResponseHeader.ContentRange:
429 return "content-range";
430 case HttpResponseHeader.Expires:
432 case HttpResponseHeader.LastModified:
433 return "last-modified";
434 case HttpResponseHeader.AcceptRanges:
435 return "accept-ranges";
436 case HttpResponseHeader.Age:
438 case HttpResponseHeader.ETag:
440 case HttpResponseHeader.Location:
442 case HttpResponseHeader.ProxyAuthenticate:
443 return "proxy-authenticate";
444 case HttpResponseHeader.RetryAfter:
446 case HttpResponseHeader.Server:
448 case HttpResponseHeader.SetCookie:
450 case HttpResponseHeader.Vary:
452 case HttpResponseHeader.WwwAuthenticate:
453 return "www-authenticate";
455 throw new InvalidOperationException ();
458 public string this[HttpResponseHeader hrh]
462 return Get (ResponseHeaderToString (hrh));
467 Add (ResponseHeaderToString (hrh), value);
474 // With this we don't check for invalid characters in header. See bug #55994.
475 internal void SetInternal (string header)
477 int pos = header.IndexOf (':');
479 throw new ArgumentException ("no colon found", "header");
481 SetInternal (header.Substring (0, pos), header.Substring (pos + 1));
484 internal void SetInternal (string name, string value)
487 value = String.Empty;
489 value = value.Trim ();
490 if (!IsHeaderValue (value))
491 throw new ArgumentException ("invalid header value");
493 if (IsMultiValue (name)) {
494 base.Add (name, value);
497 base.Set (name, value);
501 internal void RemoveAndAdd (string name, string value)
504 value = String.Empty;
506 value = value.Trim ();
509 base.Set (name, value);
512 internal void RemoveInternal (string name)
515 throw new ArgumentNullException ("name");
521 internal static bool IsMultiValue (string headerName)
523 if (headerName == null || headerName == "")
526 return multiValue.ContainsKey (headerName);
529 internal static bool IsHeaderValue (string value)
531 // TEXT any 8 bit value except CTL's (0-31 and 127)
532 // but including \r\n space and \t
533 // after a newline at least one space or \t must follow
534 // certain header fields allow comments ()
536 int len = value.Length;
537 for (int i = 0; i < len; i++) {
541 if (c < 0x20 && (c != '\r' && c != '\n' && c != '\t'))
543 if (c == '\n' && ++i < len) {
545 if (c != ' ' && c != '\t')
553 internal static bool IsHeaderName (string name)
555 // token = 1*<any CHAR except CTLs or tspecials>
556 // tspecials = "(" | ")" | "<" | ">" | "@"
557 // | "," | ";" | ":" | "\" | <">
558 // | "/" | "[" | "]" | "?" | "="
559 // | "{" | "}" | SP | HT
561 if (name == null || name.Length == 0)
564 int len = name.Length;
565 for (int i = 0; i < len; i++) {
567 if (c < 0x20 || c >= 0x7f)
571 return name.IndexOfAny (tspecials) == -1;
574 private static char [] tspecials =
575 new char [] {'(', ')', '<', '>', '@',
576 ',', ';', ':', '\\', '"',
577 '/', '[', ']', '?', '=',
578 '{', '}', ' ', '\t'};