HttpHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Web;
  3. namespace OSHttpServer
  4. {
  5. /// <summary>
  6. /// Generic helper functions for HTTP
  7. /// </summary>
  8. public static class HttpHelper
  9. {
  10. /// <summary>
  11. /// An empty URI
  12. /// </summary>
  13. public static readonly Uri EmptyUri = new Uri("http://localhost/");
  14. /// <summary>
  15. /// Parses a query string.
  16. /// </summary>
  17. /// <param name="queryString">Query string (URI encoded)</param>
  18. /// <returns>A <see cref="HttpInput"/> object if successful; otherwise <see cref="HttpInput.Empty"/></returns>
  19. /// <exception cref="ArgumentNullException"><c>queryString</c> is null.</exception>
  20. /// <exception cref="FormatException">If string cannot be parsed.</exception>
  21. public static HttpInput ParseQueryString(string queryString)
  22. {
  23. if (queryString == null)
  24. throw new ArgumentNullException("queryString");
  25. if (queryString == string.Empty)
  26. return HttpInput.Empty;
  27. HttpInput input = new HttpInput("QueryString");
  28. queryString = queryString.TrimStart('?', '&');
  29. // a simple value.
  30. if (queryString.IndexOf("&") == -1 && !queryString.Contains("%3d") && !queryString.Contains("%3D") && !queryString.Contains("="))
  31. {
  32. input.Add(string.Empty, queryString);
  33. return input;
  34. }
  35. int state = 0;
  36. int startpos = 0;
  37. string name = null;
  38. for (int i = 0; i < queryString.Length; ++i)
  39. {
  40. int newIndexPos;
  41. if (state == 0 && IsEqual(queryString, ref i, out newIndexPos))
  42. {
  43. name = queryString.Substring(startpos, i - startpos);
  44. i = newIndexPos;
  45. startpos = i + 1;
  46. ++state;
  47. }
  48. else if (state == 1 && IsAmp(queryString, ref i, out newIndexPos))
  49. {
  50. Add(input, name, queryString.Substring(startpos, i - startpos));
  51. i = newIndexPos;
  52. startpos = i + 1;
  53. state = 0;
  54. name = null;
  55. }
  56. }
  57. if (state == 0 && !input.GetEnumerator().MoveNext())
  58. throw new FormatException("Not a valid query string: " + queryString);
  59. if (startpos <= queryString.Length)
  60. {
  61. if (name != null)
  62. Add(input, name, queryString.Substring(startpos, queryString.Length - startpos));
  63. else
  64. Add(input, string.Empty, queryString.Substring(startpos, queryString.Length - startpos));
  65. }
  66. return input;
  67. }
  68. private static bool IsEqual(string queryStr, ref int index, out int outIndex)
  69. {
  70. outIndex = index;
  71. if (queryStr[index] == '=')
  72. return true;
  73. if (queryStr[index] == '%' && queryStr.Length > index + 2 && queryStr[index + 1] == '3'
  74. && (queryStr[index + 2] == 'd' || queryStr[index + 2] == 'D'))
  75. {
  76. outIndex += 2;
  77. return true;
  78. }
  79. return false;
  80. }
  81. private static bool IsAmp(string queryStr, ref int index, out int outIndex)
  82. {
  83. outIndex = index;
  84. if (queryStr[index] == '%' && queryStr.Length > index + 2 && queryStr[index + 1] == '2' &&
  85. queryStr[index + 2] == '6')
  86. outIndex += 2;
  87. else if (queryStr[index] == '&')
  88. {
  89. if (queryStr.Length > index + 4
  90. && (queryStr[index + 1] == 'a' || queryStr[index + 1] == 'A')
  91. && (queryStr[index + 2] == 'm' || queryStr[index + 2] == 'M')
  92. && (queryStr[index + 3] == 'p' || queryStr[index + 3] == 'P')
  93. && queryStr[index + 4] == ';')
  94. outIndex += 4;
  95. }
  96. else
  97. return false;
  98. return true;
  99. }
  100. private static void Add(IHttpInput input, string name, string value)
  101. {
  102. input.Add(HttpUtility.UrlDecode(name), HttpUtility.UrlDecode(value));
  103. }
  104. }
  105. }