Response with status code 406 (Not Acceptable).
Inheritance: Response
Esempio n. 1
0
        private Response ProcessAsNegotiator(object routeResult, NancyContext context)
        {
            context.WriteTraceLog(sb => sb.AppendLine("[DefaultRouteInvoker] Processing as negotiation"));

            var negotiator =
                GetNegotiator(routeResult, context);

            context.WriteTraceLog(sb =>
            {
                var allowableFormats = negotiator.NegotiationContext
                    .PermissableMediaRanges
                    .Select(mr => mr.ToString())
                    .Aggregate((t1, t2) => t1 + ", " + t2);

                var acceptFormants = context.Request.Headers["accept"]
                                                    .Aggregate((t1, t2) => t1 + ", " + t2);

                sb.AppendFormat("[DefaultRouteInvoker] Accept header: {0}\n", acceptFormants);
                sb.AppendFormat("[DefaultRouteInvoker] Acceptable media ranges: {0}\n", allowableFormats);
            });

            var compatibleHeaders =
                this.GetCompatibleHeaders(context, negotiator);

            if (!compatibleHeaders.Any())
            {
                context.WriteTraceLog(sb => sb.AppendLine("[DefaultRouteInvoker] Unable to negotiate response - no headers compatible"));

                return new NotAcceptableResponse();
            }

            var response =
                NegotiateResponse(compatibleHeaders, routeResult, negotiator, context);

            if (response == null)
            {
                context.WriteTraceLog(sb => sb.AppendLine("[DefaultRouteInvoker] Unable to negotiate response - no processors returned valid response"));

                response = new NotAcceptableResponse();
            }

            if (compatibleHeaders.Count() > 1)
            {
                response.WithHeader("Vary", "Accept");
            }

            AddLinkHeaders(context, compatibleHeaders, response);

            if (!(response is NotAcceptableResponse))
            {
                AddNegotiatedHeaders(negotiator, response);
            }

            return response;
        }
        private Response ProcessAsNegotiator(object routeResult, NancyContext context)
        {
            context.WriteTraceLog(sb => sb.AppendLine("[DefaultRouteInvoker] Processing as negotiation"));

            var negotiator =
                GetNegotiator(routeResult, context);

            var coercedAcceptHeaders = this.GetCoercedAcceptHeaders(context).ToArray();

            context.WriteTraceLog(sb =>
            {
                var allowableFormats = negotiator.NegotiationContext
                    .PermissableMediaRanges
                    .Select(mr => mr.ToString())
                    .Aggregate((t1, t2) => t1 + ", " + t2);

                var originalAccept = context.Request.Headers["accept"].Any()
                                         ? string.Join(", ", context.Request.Headers["accept"])
                                         : "None";

                var coercedAccept = coercedAcceptHeaders.Any() ?
                                        coercedAcceptHeaders.Select(h => h.Item1).Aggregate((t1, t2) => t1 + ", " + t2) :
                                        "None";

                sb.AppendFormat("[DefaultRouteInvoker] Original accept header: {0}\n", originalAccept);
                sb.AppendFormat("[DefaultRouteInvoker] Coerced accept header: {0}\n", coercedAccept);
                sb.AppendFormat("[DefaultRouteInvoker] Acceptable media ranges: {0}\n", allowableFormats);
            });

            var compatibleHeaders = this.GetCompatibleHeaders(coercedAcceptHeaders, context, negotiator);

            if (!compatibleHeaders.Any())
            {
                context.WriteTraceLog(sb => sb.AppendLine("[DefaultRouteInvoker] Unable to negotiate response - no headers compatible"));

                return new NotAcceptableResponse();
            }

            var response =
                NegotiateResponse(compatibleHeaders, routeResult, negotiator, context);

            if (response == null)
            {
                context.WriteTraceLog(sb => sb.AppendLine("[DefaultRouteInvoker] Unable to negotiate response - no processors returned valid response"));

                response = new NotAcceptableResponse();
            }

            response.WithHeader("Vary", "Accept");

            AddLinkHeaders(context, compatibleHeaders, response);

            if (!(response is NotAcceptableResponse))
            {
                AddNegotiatedHeaders(negotiator, response);
            }

            if (negotiator.NegotiationContext.StatusCode.HasValue)
            {
                response.StatusCode = negotiator.NegotiationContext.StatusCode.Value;
            }

            foreach (var cookie in negotiator.NegotiationContext.Cookies)
            {
                response.Cookies.Add(cookie);
            }

            return response;
        }