2 // System.Net.WebHeaderCollection
\r
5 // Lawrence Pit (loz@cable.a2000.nl)
\r
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
\r
8 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
\r
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;
\r
34 using System.Collections.Specialized;
\r
35 using System.Runtime.InteropServices;
\r
36 using System.Runtime.Serialization;
\r
39 // See RFC 2068 par 4.2 Message Headers
\r
41 namespace System.Net
\r
45 public class WebHeaderCollection : NameValueCollection, ISerializable
\r
47 private static readonly Hashtable restricted;
\r
48 private static readonly Hashtable multiValue;
\r
49 private bool internallyCreated = false;
\r
51 // Static Initializer
\r
53 static WebHeaderCollection ()
\r
55 // the list of restricted header names as defined
\r
56 // by the ms.net spec
\r
57 restricted = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
\r
58 CaseInsensitiveComparer.Default);
\r
60 restricted.Add ("accept", true);
\r
61 restricted.Add ("connection", true);
\r
62 restricted.Add ("content-length", true);
\r
63 restricted.Add ("content-type", true);
\r
64 restricted.Add ("date", true);
\r
65 restricted.Add ("expect", true);
\r
66 restricted.Add ("host", true);
\r
67 restricted.Add ("range", true);
\r
68 restricted.Add ("referer", true);
\r
69 restricted.Add ("transfer-encoding", true);
\r
70 restricted.Add ("user-agent", true);
\r
72 // see par 14 of RFC 2068 to see which header names
\r
73 // accept multiple values each separated by a comma
\r
74 multiValue = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
\r
75 CaseInsensitiveComparer.Default);
\r
77 multiValue.Add ("accept", true);
\r
78 multiValue.Add ("accept-charset", true);
\r
79 multiValue.Add ("accept-encoding", true);
\r
80 multiValue.Add ("accept-language", true);
\r
81 multiValue.Add ("accept-ranges", true);
\r
82 multiValue.Add ("allow", true);
\r
83 multiValue.Add ("authorization", true);
\r
84 multiValue.Add ("cache-control", true);
\r
85 multiValue.Add ("connection", true);
\r
86 multiValue.Add ("content-encoding", true);
\r
87 multiValue.Add ("content-language", true);
\r
88 multiValue.Add ("expect", true);
\r
89 multiValue.Add ("if-match", true);
\r
90 multiValue.Add ("if-none-match", true);
\r
91 multiValue.Add ("proxy-authenticate", true);
\r
92 multiValue.Add ("public", true);
\r
93 multiValue.Add ("range", true);
\r
94 multiValue.Add ("transfer-encoding", true);
\r
95 multiValue.Add ("upgrade", true);
\r
96 multiValue.Add ("vary", true);
\r
97 multiValue.Add ("via", true);
\r
98 multiValue.Add ("warning", true);
\r
101 multiValue.Add ("set-cookie", true);
\r
102 multiValue.Add ("set-cookie2", true);
\r
107 public WebHeaderCollection () { }
\r
109 protected WebHeaderCollection (SerializationInfo serializationInfo,
\r
110 StreamingContext streamingContext)
\r
112 // TODO: test for compatibility with ms.net
\r
113 int count = serializationInfo.GetInt32("count");
\r
114 for (int i = 0; i < count; i++)
\r
115 this.Add (serializationInfo.GetString ("k" + i),
\r
116 serializationInfo.GetString ("v" + i));
\r
119 internal WebHeaderCollection (bool internallyCreated)
\r
121 this.internallyCreated = internallyCreated;
\r
126 public void Add (string header)
\r
128 if (header == null)
\r
129 throw new ArgumentNullException ("header");
\r
130 int pos = header.IndexOf (':');
\r
132 throw new ArgumentException ("no colon found", "header");
\r
133 this.Add (header.Substring (0, pos),
\r
134 header.Substring (pos + 1));
\r
137 public override void Add (string name, string value)
\r
140 throw new ArgumentNullException ("name");
\r
141 if (internallyCreated && IsRestricted (name))
\r
142 throw new ArgumentException ("restricted header");
\r
143 this.AddWithoutValidate (name, value);
\r
146 protected void AddWithoutValidate (string headerName, string headerValue)
\r
148 if (!IsHeaderName (headerName))
\r
149 throw new ArgumentException ("invalid header name: " + headerName, "headerName");
\r
150 if (headerValue == null)
\r
151 headerValue = String.Empty;
\r
153 headerValue = headerValue.Trim ();
\r
154 if (!IsHeaderValue (headerValue))
\r
155 throw new ArgumentException ("invalid header value: " + headerValue, "headerValue");
\r
156 base.Add (headerName, headerValue);
\r
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)
168 if (IsMultiValue (header)) {
169 values = GetMultipleValues (values);
175 static string [] GetMultipleValues (string [] values)
177 ArrayList mvalues = new ArrayList (values.Length);
178 StringBuilder sb = null;
179 for (int i = 0; i < values.Length; ++i) {
180 string val = values [i];
181 if (val.IndexOf (',') == -1) {
187 sb = new StringBuilder ();
190 for (int k = 0; k < val.Length; k++) {
194 } else if (!quote && c == ',') {
195 mvalues.Add (sb.ToString ().Trim ());
203 mvalues.Add (sb.ToString ().Trim ());
208 return (string []) mvalues.ToArray (typeof (string));
211 public static bool IsRestricted (string headerName)
\r
213 if (headerName == null)
\r
214 throw new ArgumentNullException ("headerName");
\r
216 if (headerName == "") // MS throw nullexception here!
\r
217 throw new ArgumentException ("empty string", "headerName");
\r
219 return restricted.ContainsKey (headerName);
\r
222 public override void OnDeserialization (object sender)
\r
226 public override void Remove (string name)
\r
229 throw new ArgumentNullException ("name");
\r
230 if (internallyCreated && IsRestricted (name))
\r
231 throw new ArgumentException ("restricted header");
\r
232 base.Remove (name);
\r
235 public override void Set (string name, string value)
\r
238 throw new ArgumentNullException ("name");
\r
239 if (internallyCreated && IsRestricted (name))
\r
240 throw new ArgumentException ("restricted header");
\r
241 if (!IsHeaderName (name))
\r
242 throw new ArgumentException ("invalid header name");
\r
244 value = String.Empty;
\r
246 value = value.Trim ();
\r
247 if (!IsHeaderValue (value))
\r
248 throw new ArgumentException ("invalid header value");
\r
249 base.Set (name, value);
\r
252 public byte[] ToByteArray ()
\r
254 return Encoding.UTF8.GetBytes(ToString ());
\r
257 public override string ToString ()
\r
259 StringBuilder sb = new StringBuilder();
\r
261 int count = base.Count;
\r
262 for (int i = 0; i < count ; i++)
\r
263 sb.Append (GetKey (i))
\r
268 return sb.Append("\r\n").ToString();
\r
271 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
\r
272 StreamingContext streamingContext)
\r
274 int count = base.Count;
\r
275 serializationInfo.AddValue ("count", count);
\r
276 for (int i = 0; i < count ; i++) {
\r
277 serializationInfo.AddValue ("k" + i, GetKey (i));
\r
278 serializationInfo.AddValue ("v" + i, Get (i));
\r
282 // Internal Methods
\r
284 // With this we don't check for invalid characters in header. See bug #55994.
\r
285 internal void SetInternal (string header)
\r
287 int pos = header.IndexOf (':');
\r
289 throw new ArgumentException ("no colon found", "header");
\r
291 SetInternal (header.Substring (0, pos), header.Substring (pos + 1));
\r
294 internal void SetInternal (string name, string value)
\r
297 value = String.Empty;
\r
299 value = value.Trim ();
\r
300 if (!IsHeaderValue (value))
\r
301 throw new ArgumentException ("invalid header value");
\r
303 if (IsMultiValue (name)) {
304 base.Add (name, value);
306 base.Remove (name);
\r
307 base.Set (name, value);
\r
311 internal void RemoveAndAdd (string name, string value)
314 value = String.Empty;
316 value = value.Trim ();
319 base.Set (name, value);
322 internal void RemoveInternal (string name)
\r
325 throw new ArgumentNullException ("name");
\r
326 base.Remove (name);
\r
331 internal static bool IsMultiValue (string headerName)
\r
333 if (headerName == null || headerName == "")
\r
336 return multiValue.ContainsKey (headerName);
\r
339 internal static bool IsHeaderValue (string value)
\r
341 // TEXT any 8 bit value except CTL's (0-31 and 127)
\r
342 // but including \r\n space and \t
\r
343 // after a newline at least one space or \t must follow
\r
344 // certain header fields allow comments ()
\r
346 int len = value.Length;
\r
347 for (int i = 0; i < len; i++) {
\r
348 char c = value [i];
\r
351 if (c < 0x20 && (c != '\r' && c != '\n' && c != '\t'))
\r
353 if (c == '\n' && ++i < len) {
\r
355 if (c != ' ' && c != '\t')
\r
363 internal static bool IsHeaderName (string name)
\r
365 // token = 1*<any CHAR except CTLs or tspecials>
\r
366 // tspecials = "(" | ")" | "<" | ">" | "@"
\r
367 // | "," | ";" | ":" | "\" | <">
\r
368 // | "/" | "[" | "]" | "?" | "="
\r
369 // | "{" | "}" | SP | HT
\r
371 if (name == null || name.Length == 0)
\r
374 int len = name.Length;
\r
375 for (int i = 0; i < len; i++) {
\r
377 if (c < 0x20 || c >= 0x7f)
\r
381 return name.IndexOfAny (tspecials) == -1;
\r
384 private static char [] tspecials =
\r
385 new char [] {'(', ')', '<', '>', '@',
\r
386 ',', ';', ':', '\\', '"',
\r
387 '/', '[', ']', '?', '=',
\r
388 '{', '}', ' ', '\t'};
\r