Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WebHeaderCollection.cs « System.Net « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5b4b2217943e3efe3ff50a63210783d0c1e48046 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//
// System.Net.WebHeaderCollection
//
// Author:
//   Lawrence Pit (loz@cable.a2000.nl)
//

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Text;
    
// See RFC 2068 par 4.2 Message Headers
    
namespace System.Net 
{
	[Serializable]
	[ComVisible(true)]
	public class WebHeaderCollection : NameValueCollection, ISerializable
	{
		private static readonly int [] restricted;
		private static readonly int [] multiValue;
		private bool internallyCreated = false;
		
		// Static Initializer
		
		static WebHeaderCollection () 
		{
			// For performance reasons we initialize the following
			// tables by taking the hashcode of header names.
			// When you add a header make sure all characters are in 
			// lowercase.
			
			// the list of restricted header names as defined 
			// by the ms.net spec
			ArrayList a = new ArrayList ();
			a.Add ("accept".GetHashCode ());
			a.Add ("connection".GetHashCode ());
			a.Add ("content-length".GetHashCode ());
			a.Add ("content-type".GetHashCode ());
			a.Add ("date".GetHashCode ());
			a.Add ("expect".GetHashCode ());    // ??? What is this anyway?
			a.Add ("host".GetHashCode ());
			a.Add ("range".GetHashCode ());
			a.Add ("referer".GetHashCode ());
			a.Add ("transfer-encoding".GetHashCode ());
			a.Add ("user-agent".GetHashCode ());			
			restricted = (int []) a.ToArray (typeof (int));
			
			// see par 14 of RFC 2068 to see which header names
			// accept multiple values each separated by a comma
			a = new ArrayList ();
			a.Add ("accept".GetHashCode ());
			a.Add ("accept-charset".GetHashCode ());
			a.Add ("accept-encoding".GetHashCode ());
			a.Add ("accept-language".GetHashCode ());
			a.Add ("accept-ranges".GetHashCode ());
			a.Add ("allow".GetHashCode ());
			a.Add ("authorization".GetHashCode ());
			a.Add ("cache-control".GetHashCode ());
			a.Add ("connection".GetHashCode ());
			a.Add ("content-encoding".GetHashCode ());
			a.Add ("content-language".GetHashCode ());			
			a.Add ("expect".GetHashCode ());		
			a.Add ("if-match".GetHashCode ());
			a.Add ("if-none-match".GetHashCode ());
			a.Add ("proxy-authenticate".GetHashCode ());
			a.Add ("public".GetHashCode ());			
			a.Add ("range".GetHashCode ());
			a.Add ("transfer-encoding".GetHashCode ());
			a.Add ("upgrade".GetHashCode ());
			a.Add ("vary".GetHashCode ());
			a.Add ("via".GetHashCode ());
			a.Add ("warning".GetHashCode ());
			multiValue = (int []) a.ToArray (typeof (int));
		}
		
		// Constructors
		
		public WebHeaderCollection () {	}	
		
		protected WebHeaderCollection (SerializationInfo serializationInfo, 
					       StreamingContext streamingContext)
		{
			// TODO: test for compatibility with ms.net
			int count = serializationInfo.GetInt32("count");
			for (int i = 0; i < count; i++) 
				this.Add (serializationInfo.GetString ("k" + i),
					  serializationInfo.GetString ("v" + i));
		}
		
		internal WebHeaderCollection (bool dummy) : base ()
		{	
			this.internallyCreated = true;
		}		
		
		// Methods
		
		public void Add (string header)
		{
			if (header == null)
				throw new ArgumentNullException ("header");
			int pos = header.IndexOf (':');
			if (pos == -1)
				throw new ArgumentException ("no colon found");				
			this.Add (header.Substring (0, pos), 
				  header.Substring (pos + 1));
		}
		
		public override void Add (string name, string value)
		{
			if (name == null)
				throw new ArgumentNullException ("name");
			if (internallyCreated && IsRestricted (name))
				throw new ArgumentException ("restricted header");
			this.AddWithoutValidate (name, value);
		}

		protected void AddWithoutValidate (string headerName, string headerValue)
		{
			if (!IsHeaderName (headerName))
				throw new ArgumentException ("invalid header name");
			if (headerValue == null)
				headerValue = String.Empty;
			else
				headerValue = headerValue.Trim ();
			if (!IsHeaderValue (headerValue))
				throw new ArgumentException ("invalid header value");
			base.Add (headerName, headerValue);			
		}
		
		public override string [] GetValues (string header)
		{
			if (header == null)
				throw new ArgumentNullException ("header");
			string [] values = base.GetValues (header);
			if (values == null || values.Length == 0) 
				return null;
			if (!IsMultiValue (header))
				return values;
			StringCollection col = new StringCollection ();
			for (int i = 0; i < values.Length; i++) {
				string [] s = values [i].Split (new char [] {','});
				for (int j = 0; j < s.Length; j++) 
					s [j] = s [j].Trim ();
				col.AddRange (s);
			}
			values = new string [col.Count];
			col.CopyTo (values, 0);
			return values;
		}

		public static bool IsRestricted (string headerName)
		{
			int hashCode = headerName.ToLower ().GetHashCode ();
			for (int i = 0; i < restricted.Length; i++) 
				if (restricted [i] == hashCode)
					return true;
			return false;
		}

		[MonoTODO]
		public override void OnDeserialization (object sender)
		{
			// no idea what to do here... spec doesn't say much
			throw new NotImplementedException ();
		}

		public override void Remove (string name)
		{
			if (name == null)
				throw new ArgumentNullException ("name");
			if (internallyCreated && IsRestricted (name))
				throw new ArgumentException ("restricted header");
			base.Remove (name);
		}

		public override void Set (string name, string value)
		{
			if (name == null)
				throw new ArgumentNullException ("name");
			if (internallyCreated && IsRestricted (name))
				throw new ArgumentException ("restricted header");
			if (!IsHeaderName (name))
				throw new ArgumentException ("invalid header name");
			if (value == null)
				value = String.Empty;
			else
				value = value.Trim ();
			if (!IsHeaderValue (value))
				throw new ArgumentException ("invalid header value");
			base.Set (name, value);			
		}

		public byte[] ToByteArray ()
		{
			return Encoding.UTF8.GetBytes(ToString ());
		}

		public override string ToString ()
		{
			StringBuilder sb = new StringBuilder();

			int count = base.Count;
			for (int i = 0; i < count ; i++)
				sb.Append (GetKey (i))
				  .Append (": ")
				  .Append (Get (i))
				  .Append ("\r\n");
				  
			return sb.Append("\r\n").ToString();
		}
		
		void ISerializable.GetObjectData (SerializationInfo serializationInfo,
		   				  StreamingContext streamingContext)
		{
			int count = base.Count;
			serializationInfo.AddValue ("count", count);
			for (int i = 0; i < count ; i++) {
				serializationInfo.AddValue ("k" + i, GetKey (i));
				serializationInfo.AddValue ("v" + i, Get (i));
			}
		}
		
		// Internal Methods
		
		internal void SetInternal (string name, string value)
		{
			if (value == null)
				value = String.Empty;
			else
				value = value.Trim ();
			if (!IsHeaderValue (value))
				throw new ArgumentException ("invalid header value");
			base.Set (name, value);	
		}
		
		internal void RemoveInternal (string name)
		{
			if (name == null)
				throw new ArgumentNullException ("name");
			base.Remove (name);
		}		
		
		// Private Methods
		
		private static bool IsMultiValue (string headerName)
		{
			int hashCode = headerName.ToLower ().GetHashCode ();
			for (int i = 0; i < multiValue.Length; i++) 
				if (multiValue [i] == hashCode)
					return true;
			return false;
		}		
		
		private bool IsHeaderValue (string value)
		{
			// TEXT any 8 bit value except CTL's (0-31 and 127)
			//      but including \r\n space and \t
			//      after a newline at least one space or \t must follow
			//      certain header fields allow comments ()
				
			int len = value.Length;
			for (int i = 0; i < len; i++) {			
				char c = value [i];
				if (c == 127)
					return false;
				if (c < 0x20 && (c != '\r' && c != '\n' && c != '\t'))
					return false;
				if (c == '\n' && ++i < len) {
					c = value [i];
					if (c != ' ' && c != '\t')
						return false;
				}
			}
			
			return true;
		}
		
		private bool IsHeaderName (string name)
		{
			// token          = 1*<any CHAR except CTLs or tspecials>
			// tspecials      = "(" | ")" | "<" | ">" | "@"
			//                | "," | ";" | ":" | "\" | <">
			//                | "/" | "[" | "]" | "?" | "="
			//                | "{" | "}" | SP | HT
			
			if (name == null || name.Length == 0)
				return false;

			int len = name.Length;
			for (int i = 0; i < len; i++) {			
				char c = name [i];
				if (c < 0x20 || c >= 0x7f)
					return false;
			}
			
			return name.IndexOfAny (tspecials) == -1;
		}

		private static char [] tspecials = 
				new char [] {'(', ')', '<', '>', '@',
					     ',', ';', ':', '\\', '"',
					     '/', '[', ']', '?', '=',
					     '{', '}', ' ', '\t'};
							
	}
}