OSHttpHandler.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Text.RegularExpressions;
  31. namespace OpenSim.Framework.Servers.HttpServer
  32. {
  33. /// <sumary>
  34. /// Any OSHttpHandler must return one of the following results:
  35. /// <list type = "table">
  36. /// <listheader>
  37. /// <term>result code</term>
  38. /// <description>meaning</description>
  39. /// </listheader>
  40. /// <item>
  41. /// <term>Pass</term>
  42. /// <description>handler did not process the request</request>
  43. /// </item>
  44. /// <item>
  45. /// <term>Done</term>
  46. /// <description>handler did process the request, OSHttpServer
  47. /// can clean up and close the request</request>
  48. /// </item>
  49. /// </list>
  50. /// </summary>
  51. public enum OSHttpHandlerResult
  52. {
  53. Unprocessed,
  54. Pass,
  55. Done,
  56. }
  57. /// <summary>
  58. /// An OSHttpHandler that matches on the "content-type" header can
  59. /// supply an OSHttpContentTypeChecker delegate which will be
  60. /// invoked by the request matcher in OSHttpRequestPump.
  61. /// </summary>
  62. /// <returns>true if the handler is interested in the content;
  63. /// false otherwise</returns>
  64. public delegate bool OSHttpContentTypeChecker(OSHttpRequest req);
  65. public abstract class OSHttpHandler
  66. {
  67. /// <summary>
  68. /// Regular expression used to match against method of
  69. /// the incoming HTTP request. If you want to match any string
  70. /// either use '.*' or null. To match on the empty string use
  71. /// '^$'.
  72. /// </summary>
  73. public virtual Regex Method
  74. {
  75. get { return _method; }
  76. }
  77. protected Regex _method;
  78. /// <summary>
  79. /// Regular expression used to match against path of the
  80. /// incoming HTTP request. If you want to match any string
  81. /// either use '.*' or null. To match on the emtpy string use
  82. /// '^$'.
  83. /// </summary>
  84. public virtual Regex Path
  85. {
  86. get { return _path; }
  87. }
  88. protected Regex _path;
  89. /// <summary>
  90. /// Dictionary of (query name, regular expression) tuples,
  91. /// allowing us to match on URI query fields.
  92. /// </summary>
  93. public virtual Dictionary<string, Regex> Query
  94. {
  95. get { return _query; }
  96. }
  97. protected Dictionary<string, Regex> _query;
  98. /// <summary>
  99. /// Dictionary of (header name, regular expression) tuples,
  100. /// allowing us to match on HTTP header fields.
  101. /// </summary>
  102. public virtual Dictionary<string, Regex> Headers
  103. {
  104. get { return _headers; }
  105. }
  106. protected Dictionary<string, Regex> _headers;
  107. /// <summary>
  108. /// Dictionary of (header name, regular expression) tuples,
  109. /// allowing us to match on HTTP header fields.
  110. /// </summary>
  111. /// <remarks>
  112. /// This feature is currently not implemented as it requires
  113. /// (trivial) changes to HttpServer.HttpListener that have not
  114. /// been implemented.
  115. /// </remarks>
  116. public virtual Regex IPEndPointWhitelist
  117. {
  118. get { return _ipEndPointRegex; }
  119. }
  120. protected Regex _ipEndPointRegex;
  121. /// <summary>
  122. /// Base class constructor.
  123. /// </summary>
  124. /// <param name="path">null or path regex</param>
  125. /// <param name="headers">null or dictionary of header
  126. /// regexs</param>
  127. /// <param name="contentType">null or content type
  128. /// regex</param>
  129. /// <param name="whitelist">null or IP address regex</param>
  130. public OSHttpHandler(Regex method, Regex path, Dictionary<string, Regex> query,
  131. Dictionary<string, Regex> headers, Regex contentType, Regex whitelist)
  132. {
  133. _method = method;
  134. _path = path;
  135. _query = query;
  136. _ipEndPointRegex = whitelist;
  137. if (null == _headers && null != contentType)
  138. {
  139. _headers = new Dictionary<string, Regex>();
  140. _headers.Add("content-type", contentType);
  141. }
  142. }
  143. /// <summary>
  144. /// Process an incoming OSHttpRequest that matched our
  145. /// requirements.
  146. /// </summary>
  147. /// <returns>
  148. /// OSHttpHandlerResult.Pass if we are after all not
  149. /// interested in the request; OSHttpHandlerResult.Done if we
  150. /// did process the request.
  151. /// </returns>
  152. public abstract OSHttpHandlerResult Process(OSHttpRequest request);
  153. public override string ToString()
  154. {
  155. StringWriter sw = new StringWriter();
  156. sw.WriteLine("{0}", base.ToString());
  157. sw.WriteLine(" method regex {0}", null == Method ? "null" : Method.ToString());
  158. sw.WriteLine(" path regex {0}", null == Path ? "null": Path.ToString());
  159. foreach (string tag in Headers.Keys)
  160. {
  161. sw.WriteLine(" header {0} : {1}", tag, Headers[tag].ToString());
  162. }
  163. sw.WriteLine(" IP whitelist {0}", null == IPEndPointWhitelist ? "null" : IPEndPointWhitelist.ToString());
  164. sw.WriteLine();
  165. sw.Close();
  166. return sw.ToString();
  167. }
  168. }
  169. }