**** Merged from MCS ****
[mono-project.git] / mcs / class / System / System.Net / WebHeaderCollection.cs
blob4d3c29db3cd42e663a8df9dcc8855fdd9c83f6a6
1 //
2 // System.Net.WebHeaderCollection
3 //
4 // Authors:
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
9 //
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:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
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.
32 using System;
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Runtime.InteropServices;
36 using System.Runtime.Serialization;
37 using System.Text;
39 // See RFC 2068 par 4.2 Message Headers
41 namespace System.Net
43 [Serializable]
44 [ComVisible(true)]
45 public class WebHeaderCollection : NameValueCollection, ISerializable
47 private static readonly Hashtable restricted;
48 private static readonly Hashtable multiValue;
49 private bool internallyCreated = false;
51 // Static Initializer
53 static WebHeaderCollection ()
55 // the list of restricted header names as defined
56 // by the ms.net spec
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);
100 // Extra
101 multiValue.Add ("set-cookie", true);
102 multiValue.Add ("set-cookie2", true);
105 // Constructors
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;
124 // Methods
126 public void Add (string header)
128 if (header == null)
129 throw new ArgumentNullException ("header");
130 int pos = header.IndexOf (':');
131 if (pos == -1)
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)
139 if (name == null)
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;
152 else
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)
161 if (header == null)
162 throw new ArgumentNullException ("header");
164 string [] values = base.GetValues (header);
165 if (values == null || values.Length == 0)
166 return null;
168 return values;
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)
188 if (name == null)
189 throw new ArgumentNullException ("name");
190 if (internallyCreated && IsRestricted (name))
191 throw new ArgumentException ("restricted header");
192 base.Remove (name);
195 public override void Set (string name, string value)
197 if (name == null)
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");
203 if (value == null)
204 value = String.Empty;
205 else
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))
224 .Append (": ")
225 .Append (Get (i))
226 .Append ("\r\n");
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));
242 // Internal Methods
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 (':');
248 if (pos == -1)
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)
256 if (value == null)
257 value = String.Empty;
258 else
259 value = value.Trim ();
260 if (!IsHeaderValue (value))
261 throw new ArgumentException ("invalid header value");
263 if (IsMultiValue (name)) {
264 base.Add (name, value);
265 } else {
266 base.Remove (name);
267 base.Set (name, value);
271 internal void RemoveAndAdd (string name, string value)
273 if (value == null)
274 value = String.Empty;
275 else
276 value = value.Trim ();
278 base.Remove (name);
279 base.Set (name, value);
282 internal void RemoveInternal (string name)
284 if (name == null)
285 throw new ArgumentNullException ("name");
286 base.Remove (name);
289 // Private Methods
291 internal static bool IsMultiValue (string headerName)
293 if (headerName == null || headerName == "")
294 return false;
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++) {
308 char c = value [i];
309 if (c == 127)
310 return false;
311 if (c < 0x20 && (c != '\r' && c != '\n' && c != '\t'))
312 return false;
313 if (c == '\n' && ++i < len) {
314 c = value [i];
315 if (c != ' ' && c != '\t')
316 return false;
320 return true;
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)
332 return false;
334 int len = name.Length;
335 for (int i = 0; i < len; i++) {
336 char c = name [i];
337 if (c < 0x20 || c >= 0x7f)
338 return false;
341 return name.IndexOfAny (tspecials) == -1;
344 private static char [] tspecials =
345 new char [] {'(', ')', '<', '>', '@',
346 ',', ';', ':', '\\', '"',
347 '/', '[', ']', '?', '=',
348 '{', '}', ' ', '\t'};