QueryStringCollection.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #region License
  2. /*
  3. * QueryStringCollection.cs
  4. *
  5. * This code is derived from HttpUtility.cs (System.Net) of Mono
  6. * (http://www.mono-project.com).
  7. *
  8. * The MIT License
  9. *
  10. * Copyright (c) 2005-2009 Novell, Inc. (http://www.novell.com)
  11. * Copyright (c) 2018-2022 sta.blockhead
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. * THE SOFTWARE.
  30. */
  31. #endregion
  32. #region Authors
  33. /*
  34. * Authors:
  35. * - Patrik Torstensson <Patrik.Torstensson@labs2.com>
  36. * - Wictor Wilén (decode/encode functions) <wictor@ibizkit.se>
  37. * - Tim Coleman <tim@timcoleman.com>
  38. * - Gonzalo Paniagua Javier <gonzalo@ximian.com>
  39. */
  40. #endregion
  41. using System;
  42. using System.Collections.Specialized;
  43. using System.Text;
  44. namespace WebSocketSharp.Net
  45. {
  46. internal sealed class QueryStringCollection : NameValueCollection
  47. {
  48. #region Public Constructors
  49. public QueryStringCollection ()
  50. {
  51. }
  52. public QueryStringCollection (int capacity)
  53. : base (capacity)
  54. {
  55. }
  56. #endregion
  57. #region Public Methods
  58. public static QueryStringCollection Parse (string query)
  59. {
  60. return Parse (query, Encoding.UTF8);
  61. }
  62. public static QueryStringCollection Parse (string query, Encoding encoding)
  63. {
  64. if (query == null)
  65. return new QueryStringCollection (1);
  66. if (query.Length == 0)
  67. return new QueryStringCollection (1);
  68. if (query == "?")
  69. return new QueryStringCollection (1);
  70. if (query[0] == '?')
  71. query = query.Substring (1);
  72. if (encoding == null)
  73. encoding = Encoding.UTF8;
  74. var ret = new QueryStringCollection ();
  75. foreach (var component in query.Split ('&')) {
  76. var len = component.Length;
  77. if (len == 0)
  78. continue;
  79. if (component == "=")
  80. continue;
  81. string name = null;
  82. string val = null;
  83. var idx = component.IndexOf ('=');
  84. if (idx < 0) {
  85. val = component.UrlDecode (encoding);
  86. }
  87. else if (idx == 0) {
  88. val = component.Substring (1).UrlDecode (encoding);
  89. }
  90. else {
  91. name = component.Substring (0, idx).UrlDecode (encoding);
  92. var start = idx + 1;
  93. val = start < len
  94. ? component.Substring (start).UrlDecode (encoding)
  95. : String.Empty;
  96. }
  97. ret.Add (name, val);
  98. }
  99. return ret;
  100. }
  101. public override string ToString ()
  102. {
  103. var buff = new StringBuilder ();
  104. foreach (var key in AllKeys)
  105. buff.AppendFormat ("{0}={1}&", key, this[key]);
  106. if (buff.Length > 0)
  107. buff.Length--;
  108. return buff.ToString ();
  109. }
  110. #endregion
  111. }
  112. }