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
48 internal class WebHeaderCollection : NameValueCollection, ISerializable {
52 public class WebHeaderCollection : NameValueCollection, ISerializable {
55 internal enum HeaderInfo
62 static readonly bool[] allowed_chars = {
63 false, false, false, false, false, false, false, false, false, false, false, false, false, false,
64 false, false, false, false, false, false, false, false, false, false, false, false, false, false,
65 false, false, false, false, false, true, false, true, true, true, true, false, false, false, true,
66 true, false, true, true, false, true, true, true, true, true, true, true, true, true, true, false,
67 false, false, false, false, false, false, true, true, true, true, true, true, true, true, true,
68 true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
69 false, false, false, true, true, true, true, true, true, true, true, true, true, true, true, true,
70 true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
74 static readonly Dictionary<string, HeaderInfo> headers;
75 HeaderInfo? headerRestriction;
76 HeaderInfo? headerConsistency;
78 static WebHeaderCollection ()
80 headers = new Dictionary<string, HeaderInfo> (StringComparer.OrdinalIgnoreCase) {
81 { "Allow", HeaderInfo.MultiValue },
82 { "Accept", HeaderInfo.Request | HeaderInfo.MultiValue },
83 { "Accept-Charset", HeaderInfo.MultiValue },
84 { "Accept-Encoding", HeaderInfo.MultiValue },
85 { "Accept-Language", HeaderInfo.MultiValue },
86 { "Accept-Ranges", HeaderInfo.MultiValue },
87 { "Authorization", HeaderInfo.MultiValue },
88 { "Cache-Control", HeaderInfo.MultiValue },
89 { "Cookie", HeaderInfo.MultiValue },
90 { "Connection", HeaderInfo.Request | HeaderInfo.MultiValue },
91 { "Content-Encoding", HeaderInfo.MultiValue },
92 { "Content-Length", HeaderInfo.Request | HeaderInfo.Response },
93 { "Content-Type", HeaderInfo.Request },
94 { "Content-Language", HeaderInfo.MultiValue },
95 { "Date", HeaderInfo.Request },
96 { "Expect", HeaderInfo.Request | HeaderInfo.MultiValue},
97 { "Host", HeaderInfo.Request },
98 { "If-Match", HeaderInfo.MultiValue },
99 { "If-Modified-Since", HeaderInfo.Request },
100 { "If-None-Match", HeaderInfo.MultiValue },
101 { "Keep-Alive", HeaderInfo.Response },
102 { "Pragma", HeaderInfo.MultiValue },
103 { "Proxy-Authenticate", HeaderInfo.MultiValue },
104 { "Proxy-Authorization", HeaderInfo.MultiValue },
105 { "Proxy-Connection", HeaderInfo.Request | HeaderInfo.MultiValue },
106 { "Range", HeaderInfo.Request | HeaderInfo.MultiValue },
107 { "Referer", HeaderInfo.Request },
108 { "Set-Cookie", HeaderInfo.MultiValue },
109 { "Set-Cookie2", HeaderInfo.MultiValue },
110 { "TE", HeaderInfo.MultiValue },
111 { "Trailer", HeaderInfo.MultiValue },
112 { "Transfer-Encoding", HeaderInfo.Request | HeaderInfo.Response | HeaderInfo.MultiValue },
113 { "Upgrade", HeaderInfo.MultiValue },
114 { "User-Agent", HeaderInfo.Request },
115 { "Vary", HeaderInfo.MultiValue },
116 { "Via", HeaderInfo.MultiValue },
117 { "Warning", HeaderInfo.MultiValue },
118 { "WWW-Authenticate", HeaderInfo.Response | HeaderInfo. MultiValue }
124 public WebHeaderCollection ()
128 protected WebHeaderCollection (SerializationInfo serializationInfo,
129 StreamingContext streamingContext)
134 count = serializationInfo.GetInt32("Count");
135 for (int i = 0; i < count; i++)
136 this.Add (serializationInfo.GetString (i.ToString ()),
137 serializationInfo.GetString ((count + i).ToString ()));
138 } catch (SerializationException){
139 count = serializationInfo.GetInt32("count");
140 for (int i = 0; i < count; i++)
141 this.Add (serializationInfo.GetString ("k" + i),
142 serializationInfo.GetString ("v" + i));
147 internal WebHeaderCollection (HeaderInfo headerRestriction)
149 this.headerRestriction = headerRestriction;
154 public void Add (string header)
157 throw new ArgumentNullException ("header");
158 int pos = header.IndexOf (':');
160 throw new ArgumentException ("no colon found", "header");
162 this.Add (header.Substring (0, pos), header.Substring (pos + 1));
165 public override void Add (string name, string value)
168 throw new ArgumentNullException ("name");
170 CheckRestrictedHeader (name);
171 this.AddWithoutValidate (name, value);
174 protected void AddWithoutValidate (string headerName, string headerValue)
176 if (!IsHeaderName (headerName))
177 throw new ArgumentException ("invalid header name: " + headerName, "headerName");
178 if (headerValue == null)
179 headerValue = String.Empty;
181 headerValue = headerValue.Trim ();
182 if (!IsHeaderValue (headerValue))
183 throw new ArgumentException ("invalid header value: " + headerValue, "headerValue");
185 AddValue (headerName, headerValue);
188 internal void AddValue (string headerName, string headerValue)
190 base.Add (headerName, headerValue);
193 internal string [] GetValues_internal (string header, bool split)
196 throw new ArgumentNullException ("header");
198 string [] values = base.GetValues (header);
199 if (values == null || values.Length == 0)
202 if (split && IsMultiValue (header)) {
203 List<string> separated = null;
204 foreach (var value in values) {
205 if (value.IndexOf (',') < 0)
208 if (separated == null) {
209 separated = new List<string> (values.Length + 1);
210 foreach (var v in values) {
218 var slices = value.Split (',');
219 var slices_length = slices.Length;
220 if (value[value.Length - 1] == ',')
223 for (int i = 0; i < slices_length; ++i ) {
224 separated.Add (slices[i].Trim ());
228 if (separated != null)
229 return separated.ToArray ();
235 public override string [] GetValues (string header)
237 return GetValues_internal (header, true);
240 public override string[] GetValues (int index)
242 string[] values = base.GetValues (index);
244 if (values == null || values.Length == 0) {
251 public static bool IsRestricted (string headerName)
253 return IsRestricted (headerName, false);
256 public static bool IsRestricted (string headerName, bool response)
258 if (headerName == null)
259 throw new ArgumentNullException ("headerName");
261 if (headerName.Length == 0)
262 throw new ArgumentException ("empty string", "headerName");
264 if (!IsHeaderName (headerName))
265 throw new ArgumentException ("Invalid character in header");
268 if (!headers.TryGetValue (headerName, out info))
271 var flag = response ? HeaderInfo.Response : HeaderInfo.Request;
272 return (info & flag) != 0;
275 public override void OnDeserialization (object sender)
279 public override void Remove (string name)
282 throw new ArgumentNullException ("name");
284 CheckRestrictedHeader (name);
288 public override void Set (string name, string value)
291 throw new ArgumentNullException ("name");
292 if (!IsHeaderName (name))
293 throw new ArgumentException ("invalid header name");
295 value = String.Empty;
297 value = value.Trim ();
298 if (!IsHeaderValue (value))
299 throw new ArgumentException ("invalid header value");
301 CheckRestrictedHeader (name);
302 base.Set (name, value);
305 public byte[] ToByteArray ()
307 return Encoding.UTF8.GetBytes(ToString ());
310 internal string ToStringMultiValue ()
312 StringBuilder sb = new StringBuilder();
314 int count = base.Count;
315 for (int i = 0; i < count ; i++) {
316 string key = GetKey (i);
317 if (IsMultiValue (key)) {
318 foreach (string v in GetValues (i)) {
331 return sb.Append("\r\n").ToString();
334 public override string ToString ()
336 StringBuilder sb = new StringBuilder();
338 int count = base.Count;
339 for (int i = 0; i < count ; i++)
340 sb.Append (GetKey (i))
345 return sb.Append("\r\n").ToString();
348 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
349 StreamingContext streamingContext)
351 GetObjectData (serializationInfo, streamingContext);
354 public override void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
356 int count = base.Count;
357 serializationInfo.AddValue ("Count", count);
358 for (int i = 0; i < count; i++) {
359 serializationInfo.AddValue (i.ToString (), GetKey (i));
360 serializationInfo.AddValue ((count + i).ToString (), Get (i));
364 public override string[] AllKeys {
370 public override int Count {
376 public override KeysCollection Keys {
382 public override string Get (int index)
384 return base.Get (index);
387 public override string Get (string name)
389 return base.Get (name);
392 public override string GetKey (int index)
394 return base.GetKey (index);
397 public void Add (HttpRequestHeader header, string value)
399 Add (RequestHeaderToString (header), value);
402 public void Remove (HttpRequestHeader header)
404 Remove (RequestHeaderToString (header));
407 public void Set (HttpRequestHeader header, string value)
409 Set (RequestHeaderToString (header), value);
412 public void Add (HttpResponseHeader header, string value)
414 Add (ResponseHeaderToString (header), value);
417 public void Remove (HttpResponseHeader header)
419 Remove (ResponseHeaderToString (header));
422 public void Set (HttpResponseHeader header, string value)
424 Set (ResponseHeaderToString (header), value);
427 public string this [HttpRequestHeader header] {
429 return Get (RequestHeaderToString (header));
437 public string this [HttpResponseHeader header] {
439 return Get (ResponseHeaderToString (header));
447 public override void Clear ()
452 public override IEnumerator GetEnumerator ()
454 return base.GetEnumerator ();
459 // With this we don't check for invalid characters in header. See bug #55994.
460 internal void SetInternal (string header)
462 int pos = header.IndexOf (':');
464 throw new ArgumentException ("no colon found", "header");
466 SetInternal (header.Substring (0, pos), header.Substring (pos + 1));
469 internal void SetInternal (string name, string value)
472 value = String.Empty;
474 value = value.Trim ();
475 if (!IsHeaderValue (value))
476 throw new ArgumentException ("invalid header value");
478 if (IsMultiValue (name)) {
479 base.Add (name, value);
482 base.Set (name, value);
486 internal void RemoveAndAdd (string name, string value)
489 value = String.Empty;
491 value = value.Trim ();
494 base.Set (name, value);
497 internal void RemoveInternal (string name)
500 throw new ArgumentNullException ("name");
506 string RequestHeaderToString (HttpRequestHeader value)
508 CheckHeaderConsistency (HeaderInfo.Request);
511 case HttpRequestHeader.CacheControl:
512 return "Cache-Control";
513 case HttpRequestHeader.Connection:
515 case HttpRequestHeader.Date:
517 case HttpRequestHeader.KeepAlive:
519 case HttpRequestHeader.Pragma:
521 case HttpRequestHeader.Trailer:
523 case HttpRequestHeader.TransferEncoding:
524 return "Transfer-Encoding";
525 case HttpRequestHeader.Upgrade:
527 case HttpRequestHeader.Via:
529 case HttpRequestHeader.Warning:
531 case HttpRequestHeader.Allow:
533 case HttpRequestHeader.ContentLength:
534 return "Content-Length";
535 case HttpRequestHeader.ContentType:
536 return "Content-Type";
537 case HttpRequestHeader.ContentEncoding:
538 return "Content-Encoding";
539 case HttpRequestHeader.ContentLanguage:
540 return "Content-Language";
541 case HttpRequestHeader.ContentLocation:
542 return "Content-Location";
543 case HttpRequestHeader.ContentMd5:
544 return "Content-MD5";
545 case HttpRequestHeader.ContentRange:
546 return "Content-Range";
547 case HttpRequestHeader.Expires:
549 case HttpRequestHeader.LastModified:
550 return "Last-Modified";
551 case HttpRequestHeader.Accept:
553 case HttpRequestHeader.AcceptCharset:
554 return "Accept-Charset";
555 case HttpRequestHeader.AcceptEncoding:
556 return "Accept-Encoding";
557 case HttpRequestHeader.AcceptLanguage:
558 return "accept-language";
559 case HttpRequestHeader.Authorization:
560 return "Authorization";
561 case HttpRequestHeader.Cookie:
563 case HttpRequestHeader.Expect:
565 case HttpRequestHeader.From:
567 case HttpRequestHeader.Host:
569 case HttpRequestHeader.IfMatch:
571 case HttpRequestHeader.IfModifiedSince:
572 return "If-Modified-Since";
573 case HttpRequestHeader.IfNoneMatch:
574 return "If-None-Match";
575 case HttpRequestHeader.IfRange:
577 case HttpRequestHeader.IfUnmodifiedSince:
578 return "If-Unmodified-Since";
579 case HttpRequestHeader.MaxForwards:
580 return "Max-Forwards";
581 case HttpRequestHeader.ProxyAuthorization:
582 return "Proxy-Authorization";
583 case HttpRequestHeader.Referer:
585 case HttpRequestHeader.Range:
587 case HttpRequestHeader.Te:
589 case HttpRequestHeader.Translate:
591 case HttpRequestHeader.UserAgent:
594 throw new InvalidOperationException ();
598 string ResponseHeaderToString (HttpResponseHeader value)
600 CheckHeaderConsistency (HeaderInfo.Response);
603 case HttpResponseHeader.CacheControl:
604 return "Cache-Control";
605 case HttpResponseHeader.Connection:
607 case HttpResponseHeader.Date:
609 case HttpResponseHeader.KeepAlive:
611 case HttpResponseHeader.Pragma:
613 case HttpResponseHeader.Trailer:
615 case HttpResponseHeader.TransferEncoding:
616 return "Transfer-Encoding";
617 case HttpResponseHeader.Upgrade:
619 case HttpResponseHeader.Via:
621 case HttpResponseHeader.Warning:
623 case HttpResponseHeader.Allow:
625 case HttpResponseHeader.ContentLength:
626 return "Content-Length";
627 case HttpResponseHeader.ContentType:
628 return "Content-Type";
629 case HttpResponseHeader.ContentEncoding:
630 return "Content-Encoding";
631 case HttpResponseHeader.ContentLanguage:
632 return "Content-Language";
633 case HttpResponseHeader.ContentLocation:
634 return "Content-Location";
635 case HttpResponseHeader.ContentMd5:
636 return "Content-MD5";
637 case HttpResponseHeader.ContentRange:
638 return "Content-Range";
639 case HttpResponseHeader.Expires:
641 case HttpResponseHeader.LastModified:
642 return "Last-Modified";
643 case HttpResponseHeader.AcceptRanges:
644 return "Accept-Ranges";
645 case HttpResponseHeader.Age:
647 case HttpResponseHeader.ETag:
649 case HttpResponseHeader.Location:
651 case HttpResponseHeader.ProxyAuthenticate:
652 return "Proxy-Authenticate";
653 case HttpResponseHeader.RetryAfter:
654 return "Retry-After";
655 case HttpResponseHeader.Server:
657 case HttpResponseHeader.SetCookie:
659 case HttpResponseHeader.Vary:
661 case HttpResponseHeader.WwwAuthenticate:
662 return "WWW-Authenticate";
664 throw new InvalidOperationException ();
668 void CheckRestrictedHeader (string headerName)
670 if (!headerRestriction.HasValue)
674 if (!headers.TryGetValue (headerName, out info))
677 if ((info & headerRestriction.Value) != 0)
678 throw new ArgumentException ("This header must be modified with the appropiate property.");
681 void CheckHeaderConsistency (HeaderInfo value)
683 if (!headerConsistency.HasValue) {
684 headerConsistency = value;
688 if ((headerConsistency & value) == 0)
689 throw new InvalidOperationException ();
692 internal static bool IsMultiValue (string headerName)
694 if (headerName == null)
698 return headers.TryGetValue (headerName, out info) && (info & HeaderInfo.MultiValue) != 0;
701 internal static bool IsHeaderValue (string value)
703 // TEXT any 8 bit value except CTL's (0-31 and 127)
704 // but including \r\n space and \t
705 // after a newline at least one space or \t must follow
706 // certain header fields allow comments ()
708 int len = value.Length;
709 for (int i = 0; i < len; i++) {
713 if (c < 0x20 && (c != '\r' && c != '\n' && c != '\t'))
715 if (c == '\n' && ++i < len) {
717 if (c != ' ' && c != '\t')
725 internal static bool IsHeaderName (string name)
727 if (name == null || name.Length == 0)
730 int len = name.Length;
731 for (int i = 0; i < len; i++) {
733 if (c > 126 || !allowed_chars [c])