(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Web / System.Web / HttpValueCollection.cs
blobe8ba4e3a0dbc4c754ed981a765d8f41e6513e6e2
1 //
2 // System.Web.HttpValueCollection
3 //
4 // Author:
5 // Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 // Gonzalo Paniagua (gonzalo@ximian.com)
7 //
9 //
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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.
29 using System;
30 using System.Collections.Specialized;
31 using System.Runtime.Serialization;
32 using System.Text;
33 using System.Web.Util;
35 namespace System.Web
37 [Serializable]
38 class HttpValueCollection : NameValueCollection
40 bool _bHeaders;
42 internal HttpValueCollection ()
44 _bHeaders = false;
47 internal HttpValueCollection (string sData)
49 FillFromQueryString (sData, Encoding.UTF8);
50 IsReadOnly = true;
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)
67 _bHeaders = true;
68 char [] arrSplitValue = new char [] {':'};
69 string sKey, sValue;
71 sKey = "";
72 sValue = "";
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
79 continue;
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();
89 continue;
90 } else if (arrKeyValue.Length == 2) {
91 if (arrKeyValue[0].Length == 0) {
92 sValue += arrKeyValue [1].Trim();
93 continue;
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)
120 _bHeaders = false;
121 if (sData == null || sData == "")
122 return;
124 string k, v;
125 int eq;
126 string [] arrValues = sData.Split (new char [] {'&'});
127 foreach (string sValue in arrValues) {
128 eq = sValue.IndexOf ('=');
130 if (eq == -1) {
131 k = sValue.Trim ();
132 if (decode)
133 k = HttpUtility.UrlDecode (k, encoding);
135 Add (k, String.Empty);
136 continue;
139 k = sValue.Substring (0, eq).Trim ();
140 v = String.Empty;
141 if (eq + 1 < sValue.Length) {
142 v = sValue.Substring (eq + 1).Trim ();
143 if (v.Length == 0)
144 v = String.Empty;
147 if (decode) {
148 k = HttpUtility.UrlDecode (k, encoding);
149 if (v.Length > 0)
150 v = HttpUtility.UrlDecode (v, encoding);
153 Add (k, v);
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 ()
169 IsReadOnly = true;
172 internal void MakeReadWrite ()
174 IsReadOnly = false;
177 internal void Merge (NameValueCollection oData)
179 foreach (string sKey in oData)
180 Add (sKey, oData [sKey]);
183 internal void Reset ()
185 Clear ();
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);
199 if (UrlEncode) {
200 // use encoding
201 result.Append (HttpUtility.UrlEncode (strKey, Encoding.UTF8));
202 result.Append (eq);
203 result.Append (HttpUtility.UrlEncode (Get (strKey), Encoding.UTF8));
204 } else {
205 result.Append (strKey);
206 result.Append (eq);
207 result.Append (Get (strKey));
211 return result.ToString ();
214 public override string ToString ()
216 return ToString (false);