Example #1
0
 public List <UriToActionMatch> GetAllUriMatches(Uri uri)
 {
     return(_uriRouter.GetAllUriMatches(uri));
 }
Example #2
0
        private static bool TryMatch(HttpContext context, IUriRouter uriRouter, bool logHttpRequests, IPAddress advertiseAsAddress, int advertiseAsPort)
        {
            var tcs        = new TaskCompletionSource <bool>();
            var httpEntity = new HttpEntity(context, logHttpRequests, advertiseAsAddress, advertiseAsPort,
                                            () => tcs.TrySetResult(true));

            var request = httpEntity.Request;

            try {
                var allMatches = uriRouter.GetAllUriMatches(request.Url);
                if (allMatches.Count == 0)
                {
                    NotFound(httpEntity);
                    return(false);
                }

                var allowedMethods = GetAllowedMethods(allMatches);

                if (request.HttpMethod.Equals(HttpMethod.Options, StringComparison.OrdinalIgnoreCase))
                {
                    RespondWithOptions(httpEntity, allowedMethods);
                    return(false);
                }

                var match = allMatches.LastOrDefault(
                    m => m.ControllerAction.HttpMethod.Equals(request.HttpMethod, StringComparison.OrdinalIgnoreCase));
                if (match == null)
                {
                    MethodNotAllowed(httpEntity, allowedMethods);
                    return(false);
                }

                ICodec requestCodec           = null;
                var    supportedRequestCodecs = match.ControllerAction.SupportedRequestCodecs;
                if (supportedRequestCodecs != null && supportedRequestCodecs.Length > 0)
                {
                    requestCodec = SelectRequestCodec(request.HttpMethod, request.ContentType, supportedRequestCodecs);
                    if (requestCodec == null)
                    {
                        BadContentType(httpEntity, "Invalid or missing Content-Type");
                        return(false);
                    }
                }

                ICodec responseCodec = SelectResponseCodec(request,
                                                           request.AcceptTypes,
                                                           match.ControllerAction.SupportedResponseCodecs,
                                                           match.ControllerAction.DefaultResponseCodec);
                if (responseCodec == null)
                {
                    BadCodec(httpEntity, "Requested URI is not available in requested format");
                    return(false);
                }
                try {
                    var manager =
                        httpEntity.CreateManager(requestCodec, responseCodec, allowedMethods, satisfied => { });
                    context.Items.Add(manager.GetType(), manager);
                    context.Items.Add(match.GetType(), match);
                    context.Items.Add(tcs.GetType(), tcs);
                    return(true);
                } catch (Exception exc) {
                    Log.Error(exc, "Error while handling HTTP request '{url}'.", request.Url);
                    InternalServerError(httpEntity);
                }
            } catch (Exception exc) {
                Log.Error(exc, "Unhandled exception while processing HTTP request at {url}.",
                          httpEntity.RequestedUrl);
                InternalServerError(httpEntity);
            }
            return(false);
        }
Example #3
0
        public void match_root()
        {
            var match = _router.GetAllUriMatches(Uri("/"));

            Assert.AreEqual(1, match.Count);
            Assert.AreEqual("/", match[0].ControllerAction.UriTemplate);
            Assert.AreEqual(HttpMethod.Get, match[0].ControllerAction.HttpMethod);
        }
Example #4
0
 public List <UriToActionMatch> GetAllUriMatches(Uri uri) => _uriRouter.GetAllUriMatches(uri);