Exemple #1
0
        public override SelectParameterResult TryCast(BindingData bindingData)
        {
            var parameterRequiringValidation = bindingData.parameterRequiringValidation;
            var key = this.GetKey(parameterRequiringValidation);

            return(bindingData.fetchBodyParam(parameterRequiringValidation,
                                              vCasted => SelectParameterResult.Body(vCasted, key, parameterRequiringValidation),
                                              why => SelectParameterResult.FailureBody(why, key, parameterRequiringValidation)));
        }
        public virtual SelectParameterResult TryCast(BindingData bindingData)
        {
            var parameterRequiringValidation = bindingData.parameterRequiringValidation;
            var key        = GetKey(parameterRequiringValidation);
            var fileResult = bindingData.fetchDefaultParam(parameterRequiringValidation,
                                                           (v) =>
            {
                return(SelectParameterResult.File(v, key, parameterRequiringValidation));
            },
                                                           (whyQuery) => SelectParameterResult.FailureFile(
                                                               $"Could not create value in file.",
                                                               key, parameterRequiringValidation));

            if (fileResult.valid)
            {
                return(fileResult);
            }

            var queryResult = bindingData.fetchQueryParam(parameterRequiringValidation,
                                                          (v) =>
            {
                if (fileResult.valid)
                {
                    fileResult.fromQuery = true;
                    return(fileResult);
                }
                return(SelectParameterResult.Query(v, key, parameterRequiringValidation));
            },
                                                          (whyQuery) =>
            {
                if (fileResult.valid)
                {
                    return(fileResult);
                }
                return(SelectParameterResult.FailureQuery(whyQuery, key, parameterRequiringValidation));
            });

            if (queryResult.valid)
            {
                return(queryResult);
            }

            return(bindingData.fetchBodyParam(parameterRequiringValidation,
                                              (v) =>
            {
                return SelectParameterResult.Body(v, key, parameterRequiringValidation);
            },
                                              (whyQuery) =>
            {
                if (queryResult.valid)
                {
                    return queryResult;
                }
                return SelectParameterResult.FailureBody(whyQuery, key, parameterRequiringValidation);
            }));
        }
Exemple #3
0
        public override SelectParameterResult TryCast(BindingData bindingData)
        {
            var request = bindingData.request;
            var parameterRequiringValidation = bindingData.parameterRequiringValidation;
            var bindType = parameterRequiringValidation.ParameterType;

            request.GetAbsoluteUri();
            if (typeof(MediaTypeHeaderValue) == bindType)
            {
                if (Content.IsNullOrWhiteSpace())
                {
                    return new SelectParameterResult
                           {
                               valid         = true,
                               fromBody      = false,
                               fromQuery     = false,
                               fromFile      = false,
                               key           = Content,
                               parameterInfo = parameterRequiringValidation,
                               value         = request.GetMediaType(),
                           }
                }
                ;

                return(bindingData.fetchBodyParam(
                           parameterRequiringValidation,
                           (value) =>
                {
                    return new SelectParameterResult
                    {
                        valid = true,
                        fromBody = false,
                        fromQuery = false,
                        fromFile = false,
                        key = Content,
                        parameterInfo = parameterRequiringValidation,
                        value = value,
                    };
                },
                           (why) =>
                {
                    return new SelectParameterResult
                    {
                        fromBody = false,
                        key = "",
                        fromQuery = false,
                        fromFile = false,
                        parameterInfo = parameterRequiringValidation,
                        valid = false,
                        failure = why,     // $"Cannot extract MediaTypeHeaderValue from non-multipart request.",
                    };
                }));
            }
            if (bindType.IsSubClassOfGeneric(typeof(IHttpHeaderValueCollection <>)))
            {
                if (bindType.GenericTypeArguments.First() == typeof(StringWithQualityHeaderValue))
                {
                    if (Content.HasBlackSpace())
                    {
                        return new SelectParameterResult
                               {
                                   key           = "",
                                   fromQuery     = false,
                                   fromBody      = false,
                                   fromFile      = false,
                                   parameterInfo = parameterRequiringValidation,
                                   valid         = false,
                                   failure       = "AcceptLanguage is not a content header.",
                               }
                    }
                    ;

                    var accepts   = request.GetAcceptLanguage().ToArray();
                    var valueCast = new HeaderValues <StringWithQualityHeaderValue>(accepts);
                    return(new SelectParameterResult
                    {
                        valid = true,
                        fromBody = false,
                        fromQuery = false,
                        fromFile = false,
                        key = default,