2 // System.Web.HttpValueCollection
5 // Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 // Gonzalo Paniagua (gonzalo@ximian.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System
.Collections
.Specialized
;
31 using System
.Runtime
.Serialization
;
33 using System
.Web
.Util
;
38 class HttpValueCollection
: NameValueCollection
42 internal HttpValueCollection ()
47 internal HttpValueCollection (string sData
)
49 FillFromQueryString (sData
, Encoding
.UTF8
);
53 internal HttpValueCollection(string sData
, bool ReadOnly
, Encoding encoding
)
55 FillFromQueryString (sData
, encoding
);
56 IsReadOnly
= ReadOnly
;
59 protected HttpValueCollection (SerializationInfo info
, StreamingContext context
)
60 : base (info
, context
)
64 // string = header1: value1\r\nheader2: value2
65 internal void FillFromHeaders (string sHeaders
, Encoding encoding
)
68 char [] arrSplitValue
= new char [] {':'}
;
74 string [] arrValues
= sHeaders
.Split (new char [] {'\r', '\n'}
);
75 foreach (string sLine
in arrValues
) {
76 string [] arrKeyValue
= sLine
.Split (arrSplitValue
);
77 if (arrKeyValue
.Length
== 1 && arrKeyValue
[0].Length
== 0) {
78 // Empty \r or \n is ignored
82 if (arrKeyValue
[0] != sKey
&& sKey
.Length
> 0) {
83 Add (HttpUtility
.UrlDecode (sKey
, encoding
),
84 HttpUtility
.UrlDecode (sValue
, encoding
));
87 if (arrKeyValue
.Length
== 1) {
88 sValue
+= "\r\n" + arrKeyValue
[0].Trim();
90 } else if (arrKeyValue
.Length
== 2) {
91 if (arrKeyValue
[0].Length
== 0) {
92 sValue
+= arrKeyValue
[1].Trim();
96 sKey
= arrKeyValue
[0].Trim();
97 sValue
= arrKeyValue
[1].Trim();
101 if (sKey
.Length
> 0) {
102 Add (HttpUtility
.UrlDecode (sKey
, encoding
),
103 HttpUtility
.UrlDecode (sValue
, encoding
));
107 internal void FillFromHeaders (string sData
)
109 FillFromHeaders (sData
, Encoding
.UTF8
);
112 // String = test=aaa&kalle=nisse
113 internal void FillFromQueryString (string sData
, Encoding encoding
)
115 FillFromQueryString (sData
, encoding
, true);
118 void FillFromQueryString (string sData
, Encoding encoding
, bool decode
)
121 if (sData
== null || sData
== "")
126 string [] arrValues
= sData
.Split (new char [] {'&'}
);
127 foreach (string sValue
in arrValues
) {
128 eq
= sValue
.IndexOf ('=');
133 k
= HttpUtility
.UrlDecode (k
, encoding
);
135 Add (k
, String
.Empty
);
139 k
= sValue
.Substring (0, eq
).Trim ();
141 if (eq
+ 1 < sValue
.Length
) {
142 v
= sValue
.Substring (eq
+ 1).Trim ();
148 k
= HttpUtility
.UrlDecode (k
, encoding
);
150 v
= HttpUtility
.UrlDecode (v
, encoding
);
157 internal void FillFromQueryString (string sData
)
159 FillFromQueryString (sData
, Encoding
.UTF8
);
162 internal void FillFromCookieString (string sData
)
164 FillFromQueryString (sData
, Encoding
.UTF8
, false);
167 internal void MakeReadOnly ()
172 internal void MakeReadWrite ()
177 internal void Merge (NameValueCollection oData
)
179 foreach (string sKey
in oData
)
180 Add (sKey
, oData
[sKey
]);
183 internal void Reset ()
188 internal string ToString (bool UrlEncode
)
190 StringBuilder result
= new StringBuilder ();
191 string eq
= (_bHeaders
? ":" : "=");
192 string separator
= (_bHeaders
? "\r\n" : "&");
194 foreach (string strKey
in AllKeys
) {
196 if (result
.Length
> 0)
197 result
.Append (separator
);
201 result
.Append (HttpUtility
.UrlEncode (strKey
, Encoding
.UTF8
));
203 result
.Append (HttpUtility
.UrlEncode (Get (strKey
), Encoding
.UTF8
));
205 result
.Append (strKey
);
207 result
.Append (Get (strKey
));
211 return result
.ToString ();
214 public override string ToString ()
216 return ToString (false);