HttpParam.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace OSHttpServer
  5. {
  6. /// <summary>
  7. /// Returns item either from a form or a query string (checks them in that order)
  8. /// </summary>
  9. public class HttpParam : IHttpInput
  10. {
  11. /// <summary> Representation of a non-initialized HttpParam </summary>
  12. public static readonly HttpParam Empty = new HttpParam(HttpInput.Empty, HttpInput.Empty);
  13. private IHttpInput m_form;
  14. private IHttpInput m_query;
  15. private List<HttpInputItem> _items = new List<HttpInputItem>();
  16. /// <summary>Initialises the class to hold a value either from a post request or a querystring request</summary>
  17. public HttpParam(IHttpInput form, IHttpInput query)
  18. {
  19. m_form = form;
  20. m_query = query;
  21. }
  22. #region IHttpInput Members
  23. /// <summary>
  24. /// The add method is not availible for HttpParam
  25. /// since HttpParam checks both Request.Form and Request.QueryString
  26. /// </summary>
  27. /// <param name="name">name identifying the value</param>
  28. /// <param name="value">value to add</param>
  29. /// <exception cref="NotImplementedException"></exception>
  30. [Obsolete("Not implemented for HttpParam")]
  31. public void Add(string name, string value)
  32. {
  33. throw new NotImplementedException();
  34. }
  35. /// <summary>
  36. /// Checks whether the form or querystring has the specified value
  37. /// </summary>
  38. /// <param name="name">Name, case sensitive</param>
  39. /// <returns>true if found; otherwise false.</returns>
  40. public bool Contains(string name)
  41. {
  42. return m_form.Contains(name) || m_query.Contains(name);
  43. }
  44. /// <summary>
  45. /// Fetch an item from the form or querystring (in that order).
  46. /// </summary>
  47. /// <param name="name"></param>
  48. /// <returns>Item if found; otherwise HttpInputItem.EmptyLanguageNode</returns>
  49. public HttpInputItem this[string name]
  50. {
  51. get
  52. {
  53. if (m_form[name] != HttpInputItem.Empty)
  54. return m_form[name];
  55. else
  56. return m_query[name];
  57. }
  58. }
  59. #endregion
  60. internal void SetQueryString(HttpInput query)
  61. {
  62. m_query = query;
  63. }
  64. internal void SetForm(HttpInput form)
  65. {
  66. m_form = form;
  67. }
  68. ///<summary>
  69. ///Returns an enumerator that iterates through the collection.
  70. ///</summary>
  71. ///
  72. ///<returns>
  73. ///A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
  74. ///</returns>
  75. ///<filterpriority>1</filterpriority>
  76. IEnumerator<HttpInputItem> IEnumerable<HttpInputItem>.GetEnumerator()
  77. {
  78. List<HttpInputItem> items = new List<HttpInputItem>(m_query);
  79. items.AddRange(m_form);
  80. return items.GetEnumerator();
  81. }
  82. #region IEnumerable Members
  83. ///<summary>
  84. ///Returns an enumerator that iterates through a collection.
  85. ///</summary>
  86. ///
  87. ///<returns>
  88. ///An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
  89. ///</returns>
  90. ///<filterpriority>2</filterpriority>
  91. public IEnumerator GetEnumerator()
  92. {
  93. List<HttpInputItem> items = new List<HttpInputItem>(m_query);
  94. items.AddRange(m_form);
  95. return items.GetEnumerator();
  96. }
  97. #endregion
  98. }
  99. }