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 ("if-modified-since", true);
\r
68 restricted.Add ("range", true);
\r
69 restricted.Add ("referer", true);
\r
70 restricted.Add ("transfer-encoding", true);
\r
71 restricted.Add ("user-agent", true);
\r
73 // see par 14 of RFC 2068 to see which header names
\r
74 // accept multiple values each separated by a comma
\r
75 multiValue = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
\r
76 CaseInsensitiveComparer.Default);
\r
78 multiValue.Add ("accept", true);
\r
79 multiValue.Add ("accept-charset", true);
\r
80 multiValue.Add ("accept-encoding", true);
\r
81 multiValue.Add ("accept-language", true);
\r
82 multiValue.Add ("accept-ranges", true);
\r
83 multiValue.Add ("allow", true);
\r
84 multiValue.Add ("authorization", true);
\r
85 multiValue.Add ("cache-control", true);
\r
86 multiValue.Add ("connection", true);
\r
87 multiValue.Add ("content-encoding", true);
\r
88 multiValue.Add ("content-language", true);
\r
89 multiValue.Add ("expect", true);
\r
90 multiValue.Add ("if-match", true);
\r
91 multiValue.Add ("if-none-match", true);
\r
92 multiValue.Add ("proxy-authenticate", true);
\r
93 multiValue.Add ("public", true);
\r
94 multiValue.Add ("range", true);
\r
95 multiValue.Add ("transfer-encoding", true);
\r
96 multiValue.Add ("upgrade", true);
\r
97 multiValue.Add ("vary", true);
\r
98 multiValue.Add ("via", true);
\r
99 multiValue.Add ("warning", true);
\r
102 multiValue.Add ("set-cookie", true);
\r
103 multiValue.Add ("set-cookie2", true);
\r
108 public WebHeaderCollection () { }
\r
110 protected WebHeaderCollection (SerializationInfo serializationInfo,
\r
111 StreamingContext streamingContext)
\r
113 // TODO: test for compatibility with ms.net
\r
114 int count = serializationInfo.GetInt32("count");
\r
115 for (int i = 0; i < count; i++)
\r
116 this.Add (serializationInfo.GetString ("k" + i),
\r
117 serializationInfo.GetString ("v" + i));
\r
120 internal WebHeaderCollection (bool internallyCreated)
\r
122 this.internallyCreated = internallyCreated;
\r
127 public void Add (string header)
\r
129 if (header == null)
\r
130 throw new ArgumentNullException ("header");
\r
131 int pos = header.IndexOf (':');
\r
133 throw new ArgumentException ("no colon found", "header");
\r
134 this.Add (header.Substring (0, pos),
\r
135 header.Substring (pos + 1));
\r
138 public override void Add (string name, string value)
\r
141 throw new ArgumentNullException ("name");
\r
142 if (internallyCreated && IsRestricted (name))
\r
143 throw new ArgumentException ("This header must be modified with the appropiate property.");
144 this.AddWithoutValidate (name, value);
\r
147 protected void AddWithoutValidate (string headerName, string headerValue)
\r
149 if (!IsHeaderName (headerName))
\r
150 throw new ArgumentException ("invalid header name: " + headerName, "headerName");
\r
151 if (headerValue == null)
\r
152 headerValue = String.Empty;
\r
154 headerValue = headerValue.Trim ();
\r
155 if (!IsHeaderValue (headerValue))
\r
156 throw new ArgumentException ("invalid header value: " + headerValue, "headerValue");
\r
157 base.Add (headerName, headerValue);
\r
160 public override string [] GetValues (string header)
163 throw new ArgumentNullException ("header");
165 string [] values = base.GetValues (header);
166 if (values == null || values.Length == 0)
170 if (IsMultiValue (header)) {
171 values = GetMultipleValues (values);
178 /* Now i wonder why this is here...
179 static string [] GetMultipleValues (string [] values)
181 ArrayList mvalues = new ArrayList (values.Length);
182 StringBuilder sb = null;
183 for (int i = 0; i < values.Length; ++i) {
184 string val = values [i];
185 if (val.IndexOf (',') == -1) {
191 sb = new StringBuilder ();
194 for (int k = 0; k < val.Length; k++) {
198 } else if (!quote && c == ',') {
199 mvalues.Add (sb.ToString ().Trim ());
207 mvalues.Add (sb.ToString ().Trim ());
212 return (string []) mvalues.ToArray (typeof (string));
216 public static bool IsRestricted (string headerName)
\r
218 if (headerName == null)
\r
219 throw new ArgumentNullException ("headerName");
\r
221 if (headerName == "") // MS throw nullexception here!
\r
222 throw new ArgumentException ("empty string", "headerName");
\r
224 return restricted.ContainsKey (headerName);
\r
227 public override void OnDeserialization (object sender)
\r
231 public override void Remove (string name)
\r
234 throw new ArgumentNullException ("name");
\r
235 if (internallyCreated && IsRestricted (name))
\r
236 throw new ArgumentException ("restricted header");
\r
237 base.Remove (name);
\r
240 public override void Set (string name, string value)
\r
243 throw new ArgumentNullException ("name");
\r
244 if (internallyCreated && IsRestricted (name))
\r
245 throw new ArgumentException ("restricted header");
\r
246 if (!IsHeaderName (name))
\r
247 throw new ArgumentException ("invalid header name");
\r
249 value = String.Empty;
\r
251 value = value.Trim ();
\r
252 if (!IsHeaderValue (value))
\r
253 throw new ArgumentException ("invalid header value");
\r
254 base.Set (name, value);
\r
257 public byte[] ToByteArray ()
\r
259 return Encoding.UTF8.GetBytes(ToString ());
\r
262 public override string ToString ()
\r
264 StringBuilder sb = new StringBuilder();
\r
266 int count = base.Count;
\r
267 for (int i = 0; i < count ; i++)
\r
268 sb.Append (GetKey (i))
\r
273 return sb.Append("\r\n").ToString();
\r
276 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
\r
277 StreamingContext streamingContext)
\r
279 int count = base.Count;
\r
280 serializationInfo.AddValue ("count", count);
\r
281 for (int i = 0; i < count ; i++) {
\r
282 serializationInfo.AddValue ("k" + i, GetKey (i));
\r
283 serializationInfo.AddValue ("v" + i, Get (i));
\r
287 // Internal Methods
\r
289 // With this we don't check for invalid characters in header. See bug #55994.
\r
290 internal void SetInternal (string header)
\r
292 int pos = header.IndexOf (':');
\r
294 throw new ArgumentException ("no colon found", "header");
\r
296 SetInternal (header.Substring (0, pos), header.Substring (pos + 1));
\r
299 internal void SetInternal (string name, string value)
\r
302 value = String.Empty;
\r
304 value = value.Trim ();
\r
305 if (!IsHeaderValue (value))
\r
306 throw new ArgumentException ("invalid header value");
\r
308 if (IsMultiValue (name)) {
309 base.Add (name, value);
311 base.Remove (name);
\r
312 base.Set (name, value);
\r
316 internal void RemoveAndAdd (string name, string value)
319 value = String.Empty;
321 value = value.Trim ();
324 base.Set (name, value);
327 internal void RemoveInternal (string name)
\r
330 throw new ArgumentNullException ("name");
\r
331 base.Remove (name);
\r
336 internal static bool IsMultiValue (string headerName)
\r
338 if (headerName == null || headerName == "")
\r
341 return multiValue.ContainsKey (headerName);
\r
344 internal static bool IsHeaderValue (string value)
\r
346 // TEXT any 8 bit value except CTL's (0-31 and 127)
\r
347 // but including \r\n space and \t
\r
348 // after a newline at least one space or \t must follow
\r
349 // certain header fields allow comments ()
\r
351 int len = value.Length;
\r
352 for (int i = 0; i < len; i++) {
\r
353 char c = value [i];
\r
356 if (c < 0x20 && (c != '\r' && c != '\n' && c != '\t'))
\r
358 if (c == '\n' && ++i < len) {
\r
360 if (c != ' ' && c != '\t')
\r
368 internal static bool IsHeaderName (string name)
\r
370 // token = 1*<any CHAR except CTLs or tspecials>
\r
371 // tspecials = "(" | ")" | "<" | ">" | "@"
\r
372 // | "," | ";" | ":" | "\" | <">
\r
373 // | "/" | "[" | "]" | "?" | "="
\r
374 // | "{" | "}" | SP | HT
\r
376 if (name == null || name.Length == 0)
\r
379 int len = name.Length;
\r
380 for (int i = 0; i < len; i++) {
\r
382 if (c < 0x20 || c >= 0x7f)
\r
386 return name.IndexOfAny (tspecials) == -1;
\r
389 private static char [] tspecials =
\r
390 new char [] {'(', ')', '<', '>', '@',
\r
391 ',', ';', ':', '\\', '"',
\r
392 '/', '[', ']', '?', '=',
\r
393 '{', '}', ' ', '\t'};
\r