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 ("range", true);
68 restricted
.Add ("referer", true);
69 restricted
.Add ("transfer-encoding", true);
70 restricted
.Add ("user-agent", true);
72 // see par 14 of RFC 2068 to see which header names
73 // accept multiple values each separated by a comma
74 multiValue
= new Hashtable (CaseInsensitiveHashCodeProvider
.Default
,
75 CaseInsensitiveComparer
.Default
);
77 multiValue
.Add ("accept", true);
78 multiValue
.Add ("accept-charset", true);
79 multiValue
.Add ("accept-encoding", true);
80 multiValue
.Add ("accept-language", true);
81 multiValue
.Add ("accept-ranges", true);
82 multiValue
.Add ("allow", true);
83 multiValue
.Add ("authorization", true);
84 multiValue
.Add ("cache-control", true);
85 multiValue
.Add ("connection", true);
86 multiValue
.Add ("content-encoding", true);
87 multiValue
.Add ("content-language", true);
88 multiValue
.Add ("expect", true);
89 multiValue
.Add ("if-match", true);
90 multiValue
.Add ("if-none-match", true);
91 multiValue
.Add ("proxy-authenticate", true);
92 multiValue
.Add ("public", true);
93 multiValue
.Add ("range", true);
94 multiValue
.Add ("transfer-encoding", true);
95 multiValue
.Add ("upgrade", true);
96 multiValue
.Add ("vary", true);
97 multiValue
.Add ("via", true);
98 multiValue
.Add ("warning", true);
101 multiValue
.Add ("set-cookie", true);
102 multiValue
.Add ("set-cookie2", true);
107 public WebHeaderCollection () { }
109 protected WebHeaderCollection (SerializationInfo serializationInfo
,
110 StreamingContext streamingContext
)
112 // TODO: test for compatibility with ms.net
113 int count
= serializationInfo
.GetInt32("count");
114 for (int i
= 0; i
< count
; i
++)
115 this.Add (serializationInfo
.GetString ("k" + i
),
116 serializationInfo
.GetString ("v" + i
));
119 internal WebHeaderCollection (bool internallyCreated
)
121 this.internallyCreated
= internallyCreated
;
126 public void Add (string header
)
129 throw new ArgumentNullException ("header");
130 int pos
= header
.IndexOf (':');
132 throw new ArgumentException ("no colon found", "header");
133 this.Add (header
.Substring (0, pos
),
134 header
.Substring (pos
+ 1));
137 public override void Add (string name
, string value)
140 throw new ArgumentNullException ("name");
141 if (internallyCreated
&& IsRestricted (name
))
142 throw new ArgumentException ("restricted header");
143 this.AddWithoutValidate (name
, value);
146 protected void AddWithoutValidate (string headerName
, string headerValue
)
148 if (!IsHeaderName (headerName
))
149 throw new ArgumentException ("invalid header name: " + headerName
, "headerName");
150 if (headerValue
== null)
151 headerValue
= String
.Empty
;
153 headerValue
= headerValue
.Trim ();
154 if (!IsHeaderValue (headerValue
))
155 throw new ArgumentException ("invalid header value: " + headerValue
, "headerValue");
156 base.Add (headerName
, headerValue
);
159 public override string [] GetValues (string header
)
162 throw new ArgumentNullException ("header");
164 string [] values
= base.GetValues (header
);
165 if (values
== null || values
.Length
== 0)
171 public static bool IsRestricted (string headerName
)
173 if (headerName
== null)
174 throw new ArgumentNullException ("headerName");
176 if (headerName
== "") // MS throw nullexception here!
177 throw new ArgumentException ("empty string", "headerName");
179 return restricted
.ContainsKey (headerName
);
182 public override void OnDeserialization (object sender
)
186 public override void Remove (string name
)
189 throw new ArgumentNullException ("name");
190 if (internallyCreated
&& IsRestricted (name
))
191 throw new ArgumentException ("restricted header");
195 public override void Set (string name
, string value)
198 throw new ArgumentNullException ("name");
199 if (internallyCreated
&& IsRestricted (name
))
200 throw new ArgumentException ("restricted header");
201 if (!IsHeaderName (name
))
202 throw new ArgumentException ("invalid header name");
204 value = String
.Empty
;
206 value = value.Trim ();
207 if (!IsHeaderValue (value))
208 throw new ArgumentException ("invalid header value");
209 base.Set (name
, value);
212 public byte[] ToByteArray ()
214 return Encoding
.UTF8
.GetBytes(ToString ());
217 public override string ToString ()
219 StringBuilder sb
= new StringBuilder();
221 int count
= base.Count
;
222 for (int i
= 0; i
< count
; i
++)
223 sb
.Append (GetKey (i
))
228 return sb
.Append("\r\n").ToString();
231 void ISerializable
.GetObjectData (SerializationInfo serializationInfo
,
232 StreamingContext streamingContext
)
234 int count
= base.Count
;
235 serializationInfo
.AddValue ("count", count
);
236 for (int i
= 0; i
< count
; i
++) {
237 serializationInfo
.AddValue ("k" + i
, GetKey (i
));
238 serializationInfo
.AddValue ("v" + i
, Get (i
));
244 // With this we don't check for invalid characters in header. See bug #55994.
245 internal void SetInternal (string header
)
247 int pos
= header
.IndexOf (':');
249 throw new ArgumentException ("no colon found", "header");
251 SetInternal (header
.Substring (0, pos
), header
.Substring (pos
+ 1));
254 internal void SetInternal (string name
, string value)
257 value = String
.Empty
;
259 value = value.Trim ();
260 if (!IsHeaderValue (value))
261 throw new ArgumentException ("invalid header value");
263 if (IsMultiValue (name
)) {
264 base.Add (name
, value);
267 base.Set (name
, value);
271 internal void RemoveAndAdd (string name
, string value)
274 value = String
.Empty
;
276 value = value.Trim ();
279 base.Set (name
, value);
282 internal void RemoveInternal (string name
)
285 throw new ArgumentNullException ("name");
291 internal static bool IsMultiValue (string headerName
)
293 if (headerName
== null || headerName
== "")
296 return multiValue
.ContainsKey (headerName
);
299 internal static bool IsHeaderValue (string value)
301 // TEXT any 8 bit value except CTL's (0-31 and 127)
302 // but including \r\n space and \t
303 // after a newline at least one space or \t must follow
304 // certain header fields allow comments ()
306 int len
= value.Length
;
307 for (int i
= 0; i
< len
; i
++) {
311 if (c
< 0x20 && (c
!= '\r' && c
!= '\n' && c
!= '\t'))
313 if (c
== '\n' && ++i
< len
) {
315 if (c
!= ' ' && c
!= '\t')
323 internal static bool IsHeaderName (string name
)
325 // token = 1*<any CHAR except CTLs or tspecials>
326 // tspecials = "(" | ")" | "<" | ">" | "@"
327 // | "," | ";" | ":" | "\" | <">
328 // | "/" | "[" | "]" | "?" | "="
329 // | "{" | "}" | SP | HT
331 if (name
== null || name
.Length
== 0)
334 int len
= name
.Length
;
335 for (int i
= 0; i
< len
; i
++) {
337 if (c
< 0x20 || c
>= 0x7f)
341 return name
.IndexOfAny (tspecials
) == -1;
344 private static char [] tspecials
=
345 new char [] {'(', ')', '<', '>', '@',
346 ',', ';', ':', '\\', '"',
347 '/', '[', ']', '?', '=',
348 '{', '}', ' ', '\t'};