Ejemplo n.º 1
0
        /// <inheritdoc />
        protected override async Task OnRequestAsync(IHttpContext context)
        {
            var result = await _resolvers.ResolveAsync(context).ConfigureAwait(false);

            switch (result)
            {
            case RouteResolutionResult.RouteNotMatched:
            case RouteResolutionResult.NoHandlerSuccessful:
                await OnPathNotFoundAsync(context).ConfigureAwait(false);

                break;

            case RouteResolutionResult.NoHandlerSelected:
                await OnMethodNotAllowedAsync(context).ConfigureAwait(false);

                break;

            case RouteResolutionResult.Success:
                return;

            default:
                SelfCheck.Fail($"Internal error: unknown route resolution result {result}.");
                return;
            }
        }
        /// <summary>
        /// Asynchronously parses a request body in <c>application/x-www-form-urlencoded</c> format.
        /// </summary>
        /// <param name="this">The <see cref="IHttpContext"/> on which this method is called.</param>
        /// <returns>A <see cref="Task{TResult}">Task</see>, representing the ongoing operation,
        /// whose result will be a read-only <see cref="NameValueCollection"/>of form field names and values.</returns>
        /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception>
        /// <remarks>
        /// <para>This method may safely be called more than once for the same <see cref="IHttpContext"/>:
        /// it will return the same collection instead of trying to parse the request body again.</para>
        /// </remarks>
        public static async Task <NameValueCollection> GetRequestFormDataAsync(this IHttpContext @this)
        {
            if ([email protected](FormDataKey, out var previousResult))
            {
                NameValueCollection result;
                try
                {
                    using (var reader = @this.OpenRequestText())
                    {
                        result = UrlEncodedDataParser.Parse(await reader.ReadToEndAsync().ConfigureAwait(false), false);
                    }
                }
                catch (Exception e)
                {
                    @this.Items[FormDataKey] = e;
                    throw;
                }

                @this.Items[FormDataKey] = result;
                return(result);
            }

            switch (previousResult)
            {
            case NameValueCollection collection:
                return(collection);

            case Exception exception:
                ExceptionDispatchInfo.Capture(exception).Throw();
                return(null);

            case null:
                SelfCheck.Fail($"Previous result of {nameof(HttpContextExtensions)}.{nameof(GetRequestFormDataAsync)} is null.");
                return(null);

            default:
                SelfCheck.Fail($"Previous result of {nameof(HttpContextExtensions)}.{nameof(GetRequestFormDataAsync)} is of unexpected type {previousResult.GetType().FullName}");
                return(null);
            }
        }
        /// <summary>
        /// Parses a request URL query. Note that this is different from getting the <see cref="IHttpRequest.QueryString"/> property,
        /// in that fields without an equal sign are treated as if they have an empty value, instead of their keys being grouped
        /// as values of the <c>null</c> key.
        /// </summary>
        /// <param name="this">The <see cref="IHttpContext"/> on which this method is called.</param>
        /// <returns>A read-only <see cref="NameValueCollection"/>.</returns>
        /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception>
        /// <remarks>
        /// <para>This method may safely be called more than once for the same <see cref="IHttpContext"/>:
        /// it will return the same collection instead of trying to parse the request body again.</para>
        /// </remarks>
        public static NameValueCollection GetRequestQueryData(this IHttpContext @this)
        {
            if ([email protected](QueryDataKey, out var previousResult))
            {
                NameValueCollection result;
                try
                {
                    result = UrlEncodedDataParser.Parse(@this.Request.Url.Query, false);
                }
                catch (Exception e)
                {
                    @this.Items[FormDataKey] = e;
                    throw;
                }

                @this.Items[FormDataKey] = result;
                return(result);
            }

            switch (previousResult)
            {
            case NameValueCollection collection:
                return(collection);

            case Exception exception:
                ExceptionDispatchInfo.Capture(exception).Throw();
                return(null);

            case null:
                SelfCheck.Fail($"Previous result of {nameof(HttpContextExtensions)}.{nameof(GetRequestQueryData)} is null.");
                return(null);

            default:
                SelfCheck.Fail($"Previous result of {nameof(HttpContextExtensions)}.{nameof(GetRequestQueryData)} is of unexpected type {previousResult.GetType().FullName}");
                return(null);
            }
        }