31 using System.Collections.Generic;
32 using System.Collections.Specialized;
33 using System.Diagnostics;
36 using System.Reflection;
37 using System.Text.RegularExpressions;
38 using System.Threading;
42 namespace OpenSim.Framework.Servers.HttpServer
53 private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
61 public string EngineID
63 get {
return String.Format(
"{0} pump {1}", _server.EngineID, _id); }
72 _engine =
new Thread(
new ThreadStart(Engine));
73 _engine.IsBackground =
true;
75 _engine.Name = string.Format (
"Engine:{0}",EngineID);
77 ThreadTracker.Add(_engine);
83 for (
int i = 0; i < pumps.Length; i++)
93 _engine =
new Thread(
new ThreadStart(Engine));
94 _engine.IsBackground =
true;
96 _engine.Name = string.Format (
"Engine:{0}",EngineID);
98 ThreadTracker.Add(_engine);
111 req = _queue.Dequeue();
114 List<OSHttpHandler> handlers = _server.OSHttpHandlers;
118 handlers = MatchHandlers(req, handlers);
136 throw new Exception(String.Format(
"[{0}] got unexpected OSHttpHandlerResult {1}", EngineID, rc));
141 _log.InfoFormat(
"[{0}] OSHttpHandler: no handler registered for {1}", EngineID, req);
146 resp.StatusDescription = String.Format(
"no handler on call for {0}", req);
147 resp.ContentType =
"text/html";
150 StreamWriter body =
new StreamWriter(resp.Body);
151 body.WriteLine(
"<html>");
152 body.WriteLine(
"<header><title>Ooops...</title><header>");
153 body.WriteLine(String.Format(
"<body><p>{0}</p></body>", resp.StatusDescription));
154 body.WriteLine(
"</html>");
163 _log.DebugFormat(
"[{0}] OSHttpHandler problem: {1}", EngineID, e.ToString());
164 _log.ErrorFormat(
"[{0}] OSHttpHandler problem: {1}", EngineID, e.Message);
171 Dictionary<OSHttpHandler, int> scoredHandlers =
new Dictionary<OSHttpHandler, int>();
173 _log.DebugFormat(
"[{0}] MatchHandlers for {1}", EngineID, req);
177 scoredHandlers[h] = 0;
187 IPEndPoint remote = req.RemoteIPEndPoint;
190 Match epm = h.IPEndPointWhitelist.Match(remote.ToString());
193 scoredHandlers.Remove(h);
201 Match m = h.Method.Match(req.HttpMethod);
204 scoredHandlers.Remove(h);
213 Match m = h.Path.Match(req.RawUrl);
216 scoredHandlers.Remove(h);
219 scoredHandlers[h] += m.ToString().Length;
225 int queriesMatch = MatchOnNameValueCollection(req.QueryString, h.
Query);
226 if (0 == queriesMatch)
228 _log.DebugFormat(
"[{0}] request {1}", EngineID, req);
229 _log.DebugFormat(
"[{0}] dropping handler {1}", EngineID, h);
231 scoredHandlers.Remove(h);
234 scoredHandlers[h] += queriesMatch;
240 int headersMatch = MatchOnNameValueCollection(req.Headers, h.
Headers);
241 if (0 == headersMatch)
243 _log.DebugFormat(
"[{0}] request {1}", EngineID, req);
244 _log.DebugFormat(
"[{0}] dropping handler {1}", EngineID, h);
246 scoredHandlers.Remove(h);
249 scoredHandlers[h] += headersMatch;
253 List<OSHttpHandler> matchingHandlers =
new List<OSHttpHandler>(scoredHandlers.Keys);
256 return scoredHandlers[x] - scoredHandlers[y];
258 LogDumpHandlerList(matchingHandlers);
259 return matchingHandlers;
266 foreach (
string tag
in regexs.Keys)
269 if (null == collection[tag])
276 Match cm = regexs[tag].Match(collection[tag]);
290 [ConditionalAttribute(
"DEBUGGING")]
291 private void LogDumpHandlerList(List<OSHttpHandler> l)
293 _log.DebugFormat(
"[{0}] OSHttpHandlerList dump:", EngineID);
295 _log.DebugFormat(
" ", h.ToString());
OSHttpRequestQueue _queue
OSHttpRequestPump(OSHttpServer server, OSHttpRequestQueue queue, int id)
OSHttpResponse is the OpenSim representation of an HTTP response.
int MatchOnNameValueCollection(NameValueCollection collection, Dictionary< string, Regex > regexs)
virtual Dictionary< string, Regex > Query
Dictionary of (query name, regular expression) tuples, allowing us to match on URI query fields...
An OSHttpRequestPump fetches incoming OSHttpRequest objects from the OSHttpRequestQueue and feeds the...
virtual Regex Method
Regular expression used to match against method of the incoming HTTP request. If you want to match an...
virtual Regex Path
Regular expression used to match against path of the incoming HTTP request. If you want to match any ...
OSHttpServer provides an HTTP server bound to a specific port. When instantiated with just address an...
virtual Regex IPEndPointWhitelist
Dictionary of (header name, regular expression) tuples, allowing us to match on HTTP header fields...
OSHttpRequestQueues are used to hand over incoming HTTP requests to OSHttpRequestPump objects...
static OSHttpRequestPump[] Pumps(OSHttpServer server, OSHttpRequestQueue queue, int poolSize)
List< OSHttpHandler > MatchHandlers(OSHttpRequest req, List< OSHttpHandler > handlers)
virtual Dictionary< string, Regex > Headers
Dictionary of (header name, regular expression) tuples, allowing us to match on HTTP header fields...
OSHttpStatusCode
HTTP status codes (almost) as defined by W3C in http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html and IETF in http://tools.ietf.org/html/rfc6585