コード例 #1
0
ファイル: Limits.cs プロジェクト: khellang/LimitsMiddleware
        /// <summary>
        /// Limits the length of the URL.
        /// </summary>
        /// <param name="builder">The OWIN builder instance.</param>
        /// <param name="options">The max url length options.</param>
        /// <returns>The OWIN builder instance.</returns>
        /// <exception cref="System.ArgumentNullException">builder</exception>
        /// <exception cref="System.ArgumentNullException">options</exception>
        public static BuildFunc MaxUrlLength(this BuildFunc builder, MaxUrlLengthOptions options)
        {
            builder.MustNotNull("builder");
            options.MustNotNull("options");

            builder(_ => MaxUrlLength(options));
            return builder;
        }
コード例 #2
0
        /// <summary>
        /// Limits the length of the URL.
        /// </summary>
        /// <param name="builder">The IAppBuilder instance.</param>
        /// <param name="options">The max url length options.</param>
        /// <returns>The IAppBuilder instance.</returns>
        /// <exception cref="System.ArgumentNullException">builder</exception>
        public static IAppBuilder MaxUrlLength(this IAppBuilder builder, MaxUrlLengthOptions options)
        {
            builder.MustNotNull("builder");
            options.MustNotNull("options");

            builder
                .UseOwin()
                .MaxUrlLength(options);
            return builder;
        }
コード例 #3
0
ファイル: Limits.cs プロジェクト: khellang/LimitsMiddleware
        /// <summary>
        /// Limits the length of the request content.
        /// </summary>
        /// <param name="options">The max request content lenght options.</param>
        /// <returns>An OWIN middleware delegate.</returns>
        /// <exception cref="System.ArgumentNullException">options</exception>
        public static MidFunc MaxUrlLength(MaxUrlLengthOptions options)
        {
            options.MustNotNull("options");

            return
                next =>
                env =>
                {
                    var context = new OwinContext(env);
                    int maxUrlLength = options.MaxUrlLength;
                    string unescapedUri = Uri.UnescapeDataString(context.Request.Uri.AbsoluteUri);

                    options.Tracer.AsVerbose("Checking request url length.");
                    if (unescapedUri.Length > maxUrlLength)
                    {
                        options.Tracer.AsInfo(
                            "Url \"{0}\"(Length: {2}) exceeds allowed length of {1}. Request rejected.",
                            unescapedUri,
                            maxUrlLength,
                            unescapedUri.Length);
                        context.Response.StatusCode = 414;
                        context.Response.ReasonPhrase = options.LimitReachedReasonPhrase(context.Response.StatusCode);
                        return Task.FromResult(0);
                    }
                    options.Tracer.AsVerbose("Check passed. Request forwarded.");
                    return next(env);
                };
        }