1
0

OSHttpRequestPump.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. // #define DEBUGGING
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Collections.Specialized;
  31. using System.Diagnostics;
  32. using System.IO;
  33. using System.Net;
  34. using System.Reflection;
  35. using System.Text.RegularExpressions;
  36. using System.Threading;
  37. using log4net;
  38. using HttpServer;
  39. namespace OpenSim.Framework.Servers.HttpServer
  40. {
  41. /// <summary>
  42. /// An OSHttpRequestPump fetches incoming OSHttpRequest objects
  43. /// from the OSHttpRequestQueue and feeds them to all subscribed
  44. /// parties. Each OSHttpRequestPump encapsulates one thread to do
  45. /// the work and there is a fixed number of pumps for each
  46. /// OSHttpServer object.
  47. /// </summary>
  48. public class OSHttpRequestPump
  49. {
  50. private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. protected OSHttpServer _server;
  52. protected OSHttpRequestQueue _queue;
  53. protected Thread _engine;
  54. private int _id;
  55. public string EngineID
  56. {
  57. get { return String.Format("{0} pump {1}", _server.EngineID, _id); }
  58. }
  59. public OSHttpRequestPump(OSHttpServer server, OSHttpRequestQueue queue, int id)
  60. {
  61. _server = server;
  62. _queue = queue;
  63. _id = id;
  64. _engine = new Thread(new ThreadStart(Engine));
  65. _engine.Name = EngineID;
  66. _engine.IsBackground = true;
  67. _engine.Start();
  68. ThreadTracker.Add(_engine);
  69. }
  70. public static OSHttpRequestPump[] Pumps(OSHttpServer server, OSHttpRequestQueue queue, int poolSize)
  71. {
  72. OSHttpRequestPump[] pumps = new OSHttpRequestPump[poolSize];
  73. for (int i = 0; i < pumps.Length; i++)
  74. {
  75. pumps[i] = new OSHttpRequestPump(server, queue, i);
  76. }
  77. return pumps;
  78. }
  79. public void Start()
  80. {
  81. _engine = new Thread(new ThreadStart(Engine));
  82. _engine.Name = EngineID;
  83. _engine.IsBackground = true;
  84. _engine.Start();
  85. ThreadTracker.Add(_engine);
  86. }
  87. public void Engine()
  88. {
  89. OSHttpRequest req = null;
  90. while (true)
  91. {
  92. try
  93. {
  94. // dequeue an OSHttpRequest from OSHttpServer's
  95. // request queue
  96. req = _queue.Dequeue();
  97. // get a copy of the list of registered handlers
  98. List<OSHttpHandler> handlers = _server.OSHttpHandlers;
  99. // prune list and have it sorted from most
  100. // specific to least specific
  101. handlers = MatchHandlers(req, handlers);
  102. // process req: we try each handler in turn until
  103. // we are either out of handlers or get back a
  104. // Pass or Done
  105. OSHttpHandlerResult rc = OSHttpHandlerResult.Unprocessed;
  106. foreach (OSHttpHandler h in handlers)
  107. {
  108. rc = h.Process(req);
  109. // Pass: handler did not process the request,
  110. // try next handler
  111. if (OSHttpHandlerResult.Pass == rc) continue;
  112. // Handled: handler has processed the request
  113. if (OSHttpHandlerResult.Done == rc) break;
  114. // hmm, something went wrong
  115. throw new Exception(String.Format("[{0}] got unexpected OSHttpHandlerResult {1}", EngineID, rc));
  116. }
  117. if (OSHttpHandlerResult.Unprocessed == rc)
  118. {
  119. _log.InfoFormat("[{0}] OSHttpHandler: no handler registered for {1}", EngineID, req);
  120. // set up response header
  121. OSHttpResponse resp = new OSHttpResponse(req);
  122. resp.StatusCode = (int)OSHttpStatusCode.ClientErrorNotFound;
  123. resp.StatusDescription = String.Format("no handler on call for {0}", req);
  124. resp.ContentType = "text/html";
  125. // add explanatory message
  126. StreamWriter body = new StreamWriter(resp.Body);
  127. body.WriteLine("<html>");
  128. body.WriteLine("<header><title>Ooops...</title><header>");
  129. body.WriteLine(String.Format("<body><p>{0}</p></body>", resp.StatusDescription));
  130. body.WriteLine("</html>");
  131. body.Flush();
  132. // and ship it back
  133. resp.Send();
  134. }
  135. }
  136. catch (Exception e)
  137. {
  138. _log.DebugFormat("[{0}] OSHttpHandler problem: {1}", EngineID, e.ToString());
  139. _log.ErrorFormat("[{0}] OSHttpHandler problem: {1}", EngineID, e.Message);
  140. }
  141. }
  142. }
  143. protected List<OSHttpHandler> MatchHandlers(OSHttpRequest req, List<OSHttpHandler> handlers)
  144. {
  145. Dictionary<OSHttpHandler, int> scoredHandlers = new Dictionary<OSHttpHandler, int>();
  146. _log.DebugFormat("[{0}] MatchHandlers for {1}", EngineID, req);
  147. foreach (OSHttpHandler h in handlers)
  148. {
  149. // initial anchor
  150. scoredHandlers[h] = 0;
  151. // first, check whether IPEndPointWhitelist applies
  152. // and, if it does, whether client is on that white
  153. // list.
  154. if (null != h.IPEndPointWhitelist)
  155. {
  156. // TODO: following code requires code changes to
  157. // HttpServer.HttpRequest to become functional
  158. IPEndPoint remote = req.RemoteIPEndPoint;
  159. if (null != remote)
  160. {
  161. Match epm = h.IPEndPointWhitelist.Match(remote.ToString());
  162. if (!epm.Success)
  163. {
  164. scoredHandlers.Remove(h);
  165. continue;
  166. }
  167. }
  168. }
  169. if (null != h.Method)
  170. {
  171. Match m = h.Method.Match(req.HttpMethod);
  172. if (!m.Success)
  173. {
  174. scoredHandlers.Remove(h);
  175. continue;
  176. }
  177. scoredHandlers[h]++;
  178. }
  179. // whitelist ok, now check path
  180. if (null != h.Path)
  181. {
  182. Match m = h.Path.Match(req.RawUrl);
  183. if (!m.Success)
  184. {
  185. scoredHandlers.Remove(h);
  186. continue;
  187. }
  188. scoredHandlers[h] += m.ToString().Length;
  189. }
  190. // whitelist & path ok, now check query string
  191. if (null != h.Query)
  192. {
  193. int queriesMatch = MatchOnNameValueCollection(req.QueryString, h.Query);
  194. if (0 == queriesMatch)
  195. {
  196. _log.DebugFormat("[{0}] request {1}", EngineID, req);
  197. _log.DebugFormat("[{0}] dropping handler {1}", EngineID, h);
  198. scoredHandlers.Remove(h);
  199. continue;
  200. }
  201. scoredHandlers[h] += queriesMatch;
  202. }
  203. // whitelist, path, query string ok, now check headers
  204. if (null != h.Headers)
  205. {
  206. int headersMatch = MatchOnNameValueCollection(req.Headers, h.Headers);
  207. if (0 == headersMatch)
  208. {
  209. _log.DebugFormat("[{0}] request {1}", EngineID, req);
  210. _log.DebugFormat("[{0}] dropping handler {1}", EngineID, h);
  211. scoredHandlers.Remove(h);
  212. continue;
  213. }
  214. scoredHandlers[h] += headersMatch;
  215. }
  216. }
  217. List<OSHttpHandler> matchingHandlers = new List<OSHttpHandler>(scoredHandlers.Keys);
  218. matchingHandlers.Sort(delegate(OSHttpHandler x, OSHttpHandler y)
  219. {
  220. return scoredHandlers[x] - scoredHandlers[y];
  221. });
  222. LogDumpHandlerList(matchingHandlers);
  223. return matchingHandlers;
  224. }
  225. protected int MatchOnNameValueCollection(NameValueCollection collection, Dictionary<string, Regex> regexs)
  226. {
  227. int matched = 0;
  228. foreach (string tag in regexs.Keys)
  229. {
  230. // do we have a header "tag"?
  231. if (null == collection[tag])
  232. {
  233. return 0;
  234. }
  235. // does the content of collection[tag] match
  236. // the supplied regex?
  237. Match cm = regexs[tag].Match(collection[tag]);
  238. if (!cm.Success)
  239. {
  240. return 0;
  241. }
  242. // ok: matches
  243. matched++;
  244. continue;
  245. }
  246. return matched;
  247. }
  248. [ConditionalAttribute("DEBUGGING")]
  249. private void LogDumpHandlerList(List<OSHttpHandler> l)
  250. {
  251. _log.DebugFormat("[{0}] OSHttpHandlerList dump:", EngineID);
  252. foreach (OSHttpHandler h in l)
  253. _log.DebugFormat(" ", h.ToString());
  254. }
  255. }
  256. }