internal void Reconfigure(TrivialWebServer tws) { lock (this) { tws.ClearHandlers(); ConfigureProperties(); ConfigurePaths(tws); } }
internal TrivialWebServer Create() { lock (this) { ReloadConfig(); TrivialWebServer server = new TrivialWebServer(new IPEndPoint(IPAddress.Any, Port), LocalOnly); server.HandleConnectionBegin += new EventHandler(server_HandleConnectionBegin); ConfigureProperties(); ConfigurePaths(server); return(server); } }
private void ConfigurePaths(TrivialWebServer server) { foreach (XmlElement pathEl in _configRoot.SelectNodes("path")) { string pattern = XmlUtil.ReadString(pathEl, "@value", null); if (pattern != null) { pattern = "^" + Regex.Escape(pattern) + "$"; } else { pattern = XmlUtil.ReadString(pathEl, "@pattern", null); } if (pattern == null) { throw new ConfigurationException("Path element requires either 'value' or 'pattern'"); } HttpRequestPath path = new HttpRequestPath(); path.Register(new Regex(pattern), server); bool hasSeenHandler = false; XmlNode childNode = pathEl.FirstChild; while (childNode != null) { XmlElement childEl = childNode as XmlElement; if (childEl == null) { childNode = childNode.NextSibling; continue; } bool isFilter; switch (childEl.Name) { case "filter": isFilter = true; if (hasSeenHandler) { throw new ConfigurationException("All filters for a given path must come before all the handlers in that path"); } break; case "handler": isFilter = false; hasSeenHandler = true; break; default: throw new ConfigurationException("Element '" + childEl.Name + "' was not expected here"); } string clazz = XmlUtil.ReadString(childEl, "@className", null); Type filterType = Type.GetType(clazz, true); ConstructorInfo constructor = filterType.GetConstructor(new Type[] { typeof(XmlElement) }); if (constructor == null) { throw new ConfigurationException("Class " + filterType.FullName + " does not have a constructor that takes XmlElement"); } object filterOrHandler = constructor.Invoke(new object[] { childEl }); if (isFilter) { if (!(filterOrHandler is HttpRequestFilter)) { throw new ConfigurationException("Class " + filterType.FullName + " does not derive from HttpRequestFilter"); } path.AddFilter((HttpRequestFilter)filterOrHandler); } else { if (!(filterOrHandler is HttpRequestHandler)) { throw new ConfigurationException("Class " + filterType.FullName + " does not derive from HttpRequestHandler"); } path.AddHandler((HttpRequestHandler)filterOrHandler); } childNode = childNode.NextSibling; } } }