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)
8 // Marek Safar (marek.safar@gmail.com)
10 // Copyright 2003 Ximian, Inc. (http://www.ximian.com)
11 // Copyright 2007 Novell, Inc. (http://www.novell.com)
12 // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Collections;
37 using System.Collections.Generic;
38 using System.Collections.Specialized;
39 using System.Runtime.InteropServices;
40 using System.Runtime.Serialization;
43 // See RFC 2068 par 4.2 Message Headers
49 public class WebHeaderCollection : NameValueCollection, ISerializable {
51 internal enum HeaderInfo
58 static readonly bool[] allowed_chars = {
59 false, false, false, false, false, false, false, false, false, false, false, false, false, false,
60 false, false, false, false, false, false, false, false, false, false, false, false, false, false,
61 false, false, false, false, false, true, false, true, true, true, true, false, false, false, true,
62 true, false, true, true, false, true, true, true, true, true, true, true, true, true, true, false,
63 false, false, false, false, false, false, true, true, true, true, true, true, true, true, true,
64 true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
65 false, false, false, true, true, true, true, true, true, true, true, true, true, true, true, true,
66 true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
70 static readonly Dictionary<string, HeaderInfo> headers;
71 HeaderInfo? headerRestriction;
72 HeaderInfo? headerConsistency;
74 static WebHeaderCollection ()
76 headers = new Dictionary<string, HeaderInfo> (StringComparer.OrdinalIgnoreCase) {
77 { "Allow", HeaderInfo.MultiValue },
78 { "Accept", HeaderInfo.Request | HeaderInfo.MultiValue },
79 { "Accept-Charset", HeaderInfo.MultiValue },
80 { "Accept-Encoding", HeaderInfo.MultiValue },
81 { "Accept-Language", HeaderInfo.MultiValue },
82 { "Accept-Ranges", HeaderInfo.MultiValue },
83 { "Authorization", HeaderInfo.MultiValue },
84 { "Cache-Control", HeaderInfo.MultiValue },
85 { "Cookie", HeaderInfo.MultiValue },
86 { "Connection", HeaderInfo.Request | HeaderInfo.MultiValue },
87 { "Content-Encoding", HeaderInfo.MultiValue },
88 { "Content-Length", HeaderInfo.Request | HeaderInfo.Response },
89 { "Content-Type", HeaderInfo.Request },
90 { "Content-Language", HeaderInfo.MultiValue },
91 { "Date", HeaderInfo.Request },
92 { "Expect", HeaderInfo.Request | HeaderInfo.MultiValue},
93 { "Host", HeaderInfo.Request },
94 { "If-Match", HeaderInfo.MultiValue },
95 { "If-Modified-Since", HeaderInfo.Request },
96 { "If-None-Match", HeaderInfo.MultiValue },
97 { "Keep-Alive", HeaderInfo.Response },
98 { "Pragma", HeaderInfo.MultiValue },
99 { "Proxy-Authenticate", HeaderInfo.MultiValue },
100 { "Proxy-Authorization", HeaderInfo.MultiValue },
101 { "Proxy-Connection", HeaderInfo.Request | HeaderInfo.MultiValue },
102 { "Range", HeaderInfo.Request | HeaderInfo.MultiValue },
103 { "Referer", HeaderInfo.Request },
104 { "Set-Cookie", HeaderInfo.MultiValue },
105 { "Set-Cookie2", HeaderInfo.MultiValue },
106 { "TE", HeaderInfo.MultiValue },
107 { "Trailer", HeaderInfo.MultiValue },
108 { "Transfer-Encoding", HeaderInfo.Request | HeaderInfo.Response | HeaderInfo.MultiValue },
109 { "Upgrade", HeaderInfo.MultiValue },
110 { "User-Agent", HeaderInfo.Request },
111 { "Vary", HeaderInfo.MultiValue },
112 { "Via", HeaderInfo.MultiValue },
113 { "Warning", HeaderInfo.MultiValue },
114 { "WWW-Authenticate", HeaderInfo.Response | HeaderInfo. MultiValue }
120 public WebHeaderCollection ()
124 protected WebHeaderCollection (SerializationInfo serializationInfo,
125 StreamingContext streamingContext)
130 count = serializationInfo.GetInt32("Count");
131 for (int i = 0; i < count; i++)
132 this.Add (serializationInfo.GetString (i.ToString ()),
133 serializationInfo.GetString ((count + i).ToString ()));
134 } catch (SerializationException){
135 count = serializationInfo.GetInt32("count");
136 for (int i = 0; i < count; i++)
137 this.Add (serializationInfo.GetString ("k" + i),
138 serializationInfo.GetString ("v" + i));
143 internal WebHeaderCollection (HeaderInfo headerRestriction)
145 this.headerRestriction = headerRestriction;
150 public void Add (string header)
153 throw new ArgumentNullException ("header");
154 int pos = header.IndexOf (':');
156 throw new ArgumentException ("no colon found", "header");
158 this.Add (header.Substring (0, pos), header.Substring (pos + 1));
161 public override void Add (string name, string value)
164 throw new ArgumentNullException ("name");
166 CheckRestrictedHeader (name);
167 this.AddWithoutValidate (name, value);
170 protected void AddWithoutValidate (string headerName, string headerValue)
172 if (!IsHeaderName (headerName))
173 throw new ArgumentException ("invalid header name: " + headerName, "headerName");
174 if (headerValue == null)
175 headerValue = String.Empty;
177 headerValue = headerValue.Trim ();
178 if (!IsHeaderValue (headerValue))
179 throw new ArgumentException ("invalid header value: " + headerValue, "headerValue");
181 AddValue (headerName, headerValue);
184 internal void AddValue (string headerName, string headerValue)
186 base.Add (headerName, headerValue);
189 internal string [] GetValues_internal (string header, bool split)
192 throw new ArgumentNullException ("header");
194 string [] values = base.GetValues (header);
195 if (values == null || values.Length == 0)
198 if (split && IsMultiValue (header)) {
199 List<string> separated = null;
200 foreach (var value in values) {
201 if (value.IndexOf (',') < 0)
204 if (separated == null) {
205 separated = new List<string> (values.Length + 1);
206 foreach (var v in values) {
214 var slices = value.Split (',');
215 var slices_length = slices.Length;
216 if (value[value.Length - 1] == ',')
219 for (int i = 0; i < slices_length; ++i ) {
220 separated.Add (slices[i].Trim ());
224 if (separated != null)
225 return separated.ToArray ();
231 public override string [] GetValues (string header)
233 return GetValues_internal (header, true);
236 public override string[] GetValues (int index)
238 string[] values = base.GetValues (index);
240 if (values == null || values.Length == 0) {
247 public static bool IsRestricted (string headerName)
249 return IsRestricted (headerName, false);
252 public static bool IsRestricted (string headerName, bool response)
254 if (headerName == null)
255 throw new ArgumentNullException ("headerName");
257 if (headerName.Length == 0)
258 throw new ArgumentException ("empty string", "headerName");
260 if (!IsHeaderName (headerName))
261 throw new ArgumentException ("Invalid character in header");
264 if (!headers.TryGetValue (headerName, out info))
267 var flag = response ? HeaderInfo.Response : HeaderInfo.Request;
268 return (info & flag) != 0;
271 public override void OnDeserialization (object sender)
275 public override void Remove (string name)
278 throw new ArgumentNullException ("name");
280 CheckRestrictedHeader (name);
284 public override void Set (string name, string value)
287 throw new ArgumentNullException ("name");
288 if (!IsHeaderName (name))
289 throw new ArgumentException ("invalid header name");
291 value = String.Empty;
293 value = value.Trim ();
294 if (!IsHeaderValue (value))
295 throw new ArgumentException ("invalid header value");
297 CheckRestrictedHeader (name);
298 base.Set (name, value);
301 public byte[] ToByteArray ()
303 return Encoding.UTF8.GetBytes(ToString ());
306 internal string ToStringMultiValue ()
308 StringBuilder sb = new StringBuilder();
310 int count = base.Count;
311 for (int i = 0; i < count ; i++) {
312 string key = GetKey (i);
313 if (IsMultiValue (key)) {
314 foreach (string v in GetValues (i)) {
327 return sb.Append("\r\n").ToString();
330 public override string ToString ()
332 StringBuilder sb = new StringBuilder();
334 int count = base.Count;
335 for (int i = 0; i < count ; i++)
336 sb.Append (GetKey (i))
341 return sb.Append("\r\n").ToString();
344 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
345 StreamingContext streamingContext)
347 GetObjectData (serializationInfo, streamingContext);
350 public override void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
352 int count = base.Count;
353 serializationInfo.AddValue ("Count", count);
354 for (int i = 0; i < count; i++) {
355 serializationInfo.AddValue (i.ToString (), GetKey (i));
356 serializationInfo.AddValue ((count + i).ToString (), Get (i));
360 public override string[] AllKeys {
366 public override int Count {
372 public override KeysCollection Keys {
378 public override string Get (int index)
380 return base.Get (index);
383 public override string Get (string name)
385 return base.Get (name);
388 public override string GetKey (int index)
390 return base.GetKey (index);
393 public void Add (HttpRequestHeader header, string value)
395 Add (RequestHeaderToString (header), value);
398 public void Remove (HttpRequestHeader header)
400 Remove (RequestHeaderToString (header));
403 public void Set (HttpRequestHeader header, string value)
405 Set (RequestHeaderToString (header), value);
408 public void Add (HttpResponseHeader header, string value)
410 Add (ResponseHeaderToString (header), value);
413 public void Remove (HttpResponseHeader header)
415 Remove (ResponseHeaderToString (header));
418 public void Set (HttpResponseHeader header, string value)
420 Set (ResponseHeaderToString (header), value);
423 public string this [HttpRequestHeader header] {
425 return Get (RequestHeaderToString (header));
433 public string this [HttpResponseHeader header] {
435 return Get (ResponseHeaderToString (header));
443 public override void Clear ()
448 public override IEnumerator GetEnumerator ()
450 return base.GetEnumerator ();
455 // With this we don't check for invalid characters in header. See bug #55994.
456 internal void SetInternal (string header)
458 int pos = header.IndexOf (':');
460 throw new ArgumentException ("no colon found", "header");
462 SetInternal (header.Substring (0, pos), header.Substring (pos + 1));
465 internal void SetInternal (string name, string value)
468 value = String.Empty;
470 value = value.Trim ();
471 if (!IsHeaderValue (value))
472 throw new ArgumentException ("invalid header value");
474 if (IsMultiValue (name)) {
475 base.Add (name, value);
478 base.Set (name, value);
482 internal void RemoveAndAdd (string name, string value)
485 value = String.Empty;
487 value = value.Trim ();
490 base.Set (name, value);
493 internal void RemoveInternal (string name)
496 throw new ArgumentNullException ("name");
502 string RequestHeaderToString (HttpRequestHeader value)
504 CheckHeaderConsistency (HeaderInfo.Request);
507 case HttpRequestHeader.CacheControl:
508 return "Cache-Control";
509 case HttpRequestHeader.Connection:
511 case HttpRequestHeader.Date:
513 case HttpRequestHeader.KeepAlive:
515 case HttpRequestHeader.Pragma:
517 case HttpRequestHeader.Trailer:
519 case HttpRequestHeader.TransferEncoding:
520 return "Transfer-Encoding";
521 case HttpRequestHeader.Upgrade:
523 case HttpRequestHeader.Via:
525 case HttpRequestHeader.Warning:
527 case HttpRequestHeader.Allow:
529 case HttpRequestHeader.ContentLength:
530 return "Content-Length";
531 case HttpRequestHeader.ContentType:
532 return "Content-Type";
533 case HttpRequestHeader.ContentEncoding:
534 return "Content-Encoding";
535 case HttpRequestHeader.ContentLanguage:
536 return "Content-Language";
537 case HttpRequestHeader.ContentLocation:
538 return "Content-Location";
539 case HttpRequestHeader.ContentMd5:
540 return "Content-MD5";
541 case HttpRequestHeader.ContentRange:
542 return "Content-Range";
543 case HttpRequestHeader.Expires:
545 case HttpRequestHeader.LastModified:
546 return "Last-Modified";
547 case HttpRequestHeader.Accept:
549 case HttpRequestHeader.AcceptCharset:
550 return "Accept-Charset";
551 case HttpRequestHeader.AcceptEncoding:
552 return "Accept-Encoding";
553 case HttpRequestHeader.AcceptLanguage:
554 return "accept-language";
555 case HttpRequestHeader.Authorization:
556 return "Authorization";
557 case HttpRequestHeader.Cookie:
559 case HttpRequestHeader.Expect:
561 case HttpRequestHeader.From:
563 case HttpRequestHeader.Host:
565 case HttpRequestHeader.IfMatch:
567 case HttpRequestHeader.IfModifiedSince:
568 return "If-Modified-Since";
569 case HttpRequestHeader.IfNoneMatch:
570 return "If-None-Match";
571 case HttpRequestHeader.IfRange:
573 case HttpRequestHeader.IfUnmodifiedSince:
574 return "If-Unmodified-Since";
575 case HttpRequestHeader.MaxForwards:
576 return "Max-Forwards";
577 case HttpRequestHeader.ProxyAuthorization:
578 return "Proxy-Authorization";
579 case HttpRequestHeader.Referer:
581 case HttpRequestHeader.Range:
583 case HttpRequestHeader.Te:
585 case HttpRequestHeader.Translate:
587 case HttpRequestHeader.UserAgent:
590 throw new InvalidOperationException ();
594 string ResponseHeaderToString (HttpResponseHeader value)
596 CheckHeaderConsistency (HeaderInfo.Response);
599 case HttpResponseHeader.CacheControl:
600 return "Cache-Control";
601 case HttpResponseHeader.Connection:
603 case HttpResponseHeader.Date:
605 case HttpResponseHeader.KeepAlive:
607 case HttpResponseHeader.Pragma:
609 case HttpResponseHeader.Trailer:
611 case HttpResponseHeader.TransferEncoding:
612 return "Transfer-Encoding";
613 case HttpResponseHeader.Upgrade:
615 case HttpResponseHeader.Via:
617 case HttpResponseHeader.Warning:
619 case HttpResponseHeader.Allow:
621 case HttpResponseHeader.ContentLength:
622 return "Content-Length";
623 case HttpResponseHeader.ContentType:
624 return "Content-Type";
625 case HttpResponseHeader.ContentEncoding:
626 return "Content-Encoding";
627 case HttpResponseHeader.ContentLanguage:
628 return "Content-Language";
629 case HttpResponseHeader.ContentLocation:
630 return "Content-Location";
631 case HttpResponseHeader.ContentMd5:
632 return "Content-MD5";
633 case HttpResponseHeader.ContentRange:
634 return "Content-Range";
635 case HttpResponseHeader.Expires:
637 case HttpResponseHeader.LastModified:
638 return "Last-Modified";
639 case HttpResponseHeader.AcceptRanges:
640 return "Accept-Ranges";
641 case HttpResponseHeader.Age:
643 case HttpResponseHeader.ETag:
645 case HttpResponseHeader.Location:
647 case HttpResponseHeader.ProxyAuthenticate:
648 return "Proxy-Authenticate";
649 case HttpResponseHeader.RetryAfter:
650 return "Retry-After";
651 case HttpResponseHeader.Server:
653 case HttpResponseHeader.SetCookie:
655 case HttpResponseHeader.Vary:
657 case HttpResponseHeader.WwwAuthenticate:
658 return "WWW-Authenticate";
660 throw new InvalidOperationException ();
664 void CheckRestrictedHeader (string headerName)
666 if (!headerRestriction.HasValue)
670 if (!headers.TryGetValue (headerName, out info))
673 if ((info & headerRestriction.Value) != 0)
674 throw new ArgumentException ("This header must be modified with the appropiate property.");
677 void CheckHeaderConsistency (HeaderInfo value)
679 if (!headerConsistency.HasValue) {
680 headerConsistency = value;
684 if ((headerConsistency & value) == 0)
685 throw new InvalidOperationException ();
688 internal static bool IsMultiValue (string headerName)
690 if (headerName == null)
694 return headers.TryGetValue (headerName, out info) && (info & HeaderInfo.MultiValue) != 0;
697 internal static bool IsHeaderValue (string value)
699 // TEXT any 8 bit value except CTL's (0-31 and 127)
700 // but including \r\n space and \t
701 // after a newline at least one space or \t must follow
702 // certain header fields allow comments ()
704 int len = value.Length;
705 for (int i = 0; i < len; i++) {
709 if (c < 0x20 && (c != '\r' && c != '\n' && c != '\t'))
711 if (c == '\n' && ++i < len) {
713 if (c != ' ' && c != '\t')
721 internal static bool IsHeaderName (string name)
723 if (name == null || name.Length == 0)
726 int len = name.Length;
727 for (int i = 0; i < len; i++) {
729 if (c > 126 || !allowed_chars [c])