1
0

QueryStringCollection.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 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 <[email protected]>
  36. * - Wictor Wilén (decode/encode functions) <[email protected]>
  37. * - Tim Coleman <[email protected]>
  38. * - Gonzalo Paniagua Javier <[email protected]>
  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 Private Methods
  58. private static string urlDecode (string s, Encoding encoding)
  59. {
  60. return s.IndexOfAny (new[] { '%', '+' }) > -1
  61. ? HttpUtility.UrlDecode (s, encoding)
  62. : s;
  63. }
  64. #endregion
  65. #region Public Methods
  66. public static QueryStringCollection Parse (string query)
  67. {
  68. return Parse (query, Encoding.UTF8);
  69. }
  70. public static QueryStringCollection Parse (string query, Encoding encoding)
  71. {
  72. if (query == null)
  73. return new QueryStringCollection (1);
  74. var len = query.Length;
  75. if (len == 0)
  76. return new QueryStringCollection (1);
  77. if (query == "?")
  78. return new QueryStringCollection (1);
  79. if (query[0] == '?')
  80. query = query.Substring (1);
  81. if (encoding == null)
  82. encoding = Encoding.UTF8;
  83. var ret = new QueryStringCollection ();
  84. var components = query.Split ('&');
  85. foreach (var component in components) {
  86. len = component.Length;
  87. if (len == 0)
  88. continue;
  89. if (component == "=")
  90. continue;
  91. var i = component.IndexOf ('=');
  92. if (i < 0) {
  93. ret.Add (null, urlDecode (component, encoding));
  94. continue;
  95. }
  96. if (i == 0) {
  97. ret.Add (null, urlDecode (component.Substring (1), encoding));
  98. continue;
  99. }
  100. var name = urlDecode (component.Substring (0, i), encoding);
  101. var start = i + 1;
  102. var val = start < len
  103. ? urlDecode (component.Substring (start), encoding)
  104. : String.Empty;
  105. ret.Add (name, val);
  106. }
  107. return ret;
  108. }
  109. public override string ToString ()
  110. {
  111. var buff = new StringBuilder ();
  112. foreach (var key in AllKeys)
  113. buff.AppendFormat ("{0}={1}&", key, this[key]);
  114. if (buff.Length > 0)
  115. buff.Length--;
  116. return buff.ToString ();
  117. }
  118. #endregion
  119. }
  120. }