public void AddHandler(HTTPDelegate HTTPDelegate, HTTPContentType HTTPContentType = null, HTTPAuthentication ContentTypeAuthentication = null, HTTPDelegate DefaultErrorHandler = null, URIReplacement AllowReplacement = URIReplacement.Fail) { ContentTypeNode _ContentTypeNode = null; if (HTTPContentType == null) { //RequestHandler = HTTPDelegate; //DefaultErrorHandler = DefaultErrorHandler; } else if (!_HTTPContentTypes.TryGetValue(HTTPContentType, out _ContentTypeNode)) { _ContentTypeNode = new ContentTypeNode(HTTPContentType, ContentTypeAuthentication, HTTPDelegate, DefaultErrorHandler, AllowReplacement); _HTTPContentTypes.Add(HTTPContentType, _ContentTypeNode); } else { if (_ContentTypeNode.AllowReplacement == URIReplacement.Allow) { _ContentTypeNode = new ContentTypeNode(HTTPContentType, ContentTypeAuthentication, HTTPDelegate, DefaultErrorHandler, AllowReplacement); _HTTPContentTypes[HTTPContentType] = _ContentTypeNode; } else if (_ContentTypeNode.AllowReplacement == URIReplacement.Ignore) { } else { throw new ArgumentException("Duplicate HTTP API definition!"); } } }
/// <summary> /// Return the best matching method handler for the given parameters. /// </summary> internal HTTPDelegate GetHandler(HTTPHostname Host, String URI, HTTPMethod HTTPMethod = null, Func <HTTPContentType[], HTTPContentType> HTTPContentTypeSelector = null, Action <IEnumerable <String> > ParsedURIParametersDelegate = null) { Host = Host ?? HTTPHostname.Any; URI = URI.IsNullOrEmpty() ? "/" : URI; HTTPMethod = HTTPMethod ?? HTTPMethod.GET; HTTPContentTypeSelector = HTTPContentTypeSelector ?? (v => HTTPContentType.HTML_UTF8); lock (Lock) { #region Get HostNode or "*" or fail HostnameNode _HostNode = null; if (!_HostnameNodes.TryGetValue(Host, out _HostNode)) { if (!_HostnameNodes.TryGetValue(HTTPHostname.Any, out _HostNode)) { return(null); } } //return GetErrorHandler(Host, URL, HTTPMethod, HTTPContentType, HTTPStatusCode.BadRequest); #endregion #region Try to find the best matching URLNode... var _RegexList = from __URLNode in _HostNode.URINodes.Values select new { URLNode = __URLNode, Regex = __URLNode.URIRegex }; var _AllTemplates = from _RegexTupel in _RegexList select new { URLNode = _RegexTupel.URLNode, Match = _RegexTupel.Regex.Match(URI) }; var _Matches = from _Match in _AllTemplates where _Match.Match.Success orderby 100 * _Match.URLNode.SortLength + _Match.URLNode.ParameterCount descending select new { URLNode = _Match.URLNode, Match = _Match.Match }; #endregion #region ...or return HostNode if (!_Matches.Any()) { //if (_HostNode.RequestHandler != null) // return _HostNode.RequestHandler; return(null); } #endregion HTTPMethodNode _HTTPMethodNode = null; ContentTypeNode _HTTPContentTypeNode = null; // Caused e.g. by the naming of the variables within the // URI templates, there could be multiple matches! //foreach (var _Match in _Matches) //{ // Use best matching URL Handler! var _Match2 = _Matches.First(); #region Copy MethodHandler Parameters var _Parameters = new List <String>(); for (var i = 1; i < _Match2.Match.Groups.Count; i++) { //_Parameters.Add(_Match2.Match.Groups[i].Value.RemoveLastSlash()); _Parameters.Add(_Match2.Match.Groups[i].Value); } var ParsedURIParametersDelegateLocal = ParsedURIParametersDelegate; if (ParsedURIParametersDelegateLocal != null) { ParsedURIParametersDelegate(_Parameters); } #endregion // If HTTPMethod was found... if (_Match2.URLNode.HTTPMethods.TryGetValue(HTTPMethod, out _HTTPMethodNode)) { var BestMatchingContentType = HTTPContentTypeSelector(_HTTPMethodNode.HTTPContentTypes.Keys.ToArray()); // Get HTTPContentTypeNode if (!_HTTPMethodNode.HTTPContentTypes.TryGetValue(BestMatchingContentType, out _HTTPContentTypeNode)) { return(_HTTPMethodNode.RequestHandler); } return(_HTTPContentTypeNode.RequestHandler); } //} // No HTTPMethod was found => return best matching URL Handler return(_Match2.URLNode.RequestHandler); //return GetErrorHandler(Host, URL, HTTPMethod, HTTPContentType, HTTPStatusCode.BadRequest); } }
public void AddHandler(HTTPDelegate HTTPDelegate, HTTPContentType HTTPContentType = null, HTTPAuthentication ContentTypeAuthentication = null, HTTPDelegate DefaultErrorHandler = null, URIReplacement AllowReplacement = URIReplacement.Fail) { ContentTypeNode _ContentTypeNode = null; if (HTTPContentType == null) { //RequestHandler = HTTPDelegate; //DefaultErrorHandler = DefaultErrorHandler; } else if (!_HTTPContentTypes.TryGetValue(HTTPContentType, out _ContentTypeNode)) { _ContentTypeNode = new ContentTypeNode(HTTPContentType, ContentTypeAuthentication, HTTPDelegate, DefaultErrorHandler, AllowReplacement); _HTTPContentTypes.Add(HTTPContentType, _ContentTypeNode); } else { if (_ContentTypeNode.AllowReplacement == URIReplacement.Allow) { _ContentTypeNode = new ContentTypeNode(HTTPContentType, ContentTypeAuthentication, HTTPDelegate, DefaultErrorHandler, AllowReplacement); _HTTPContentTypes[HTTPContentType] = _ContentTypeNode; } else if (_ContentTypeNode.AllowReplacement == URIReplacement.Ignore) { } else throw new ArgumentException("Duplicate HTTP API definition!"); } }