Пример #1
0
        /// <summary>
        /// Limits the bandwith used by the subsequent stages in the owin pipeline.
        /// </summary>
        /// <param name="builder">The OWIN builder instance.</param>
        /// <param name="options">The max bandwith options.</param>
        /// <returns>The OWIN builder instance.</returns>
        /// <exception cref="System.ArgumentNullException">builder</exception>
        /// <exception cref="System.ArgumentNullException">options</exception>
        public static BuildFunc MaxBandwidth(this BuildFunc builder, MaxBandwidthOptions options)
        {
            builder.MustNotNull("builder");
            options.MustNotNull("options");

            builder(_ => MaxBandwidth(options));
            return builder;
        }
        /// <summary>
        /// Limits the bandwith used by the subsequent stages in the owin pipeline.
        /// </summary>
        /// <param name="builder">The IAppBuilder instance.</param>
        /// <param name="options">The max bandwith options.</param>
        /// <returns>The IAppBuilder instance.</returns>
        /// <exception cref="System.ArgumentNullException">builder</exception>
        public static IAppBuilder MaxBandwidth(this IAppBuilder builder, MaxBandwidthOptions options)
        {
            builder.MustNotNull("builder");
            options.MustNotNull("options");

            builder
                .UseOwin()
                .MaxBandwidth(options);
            return builder;
        }
Пример #3
0
        /// <summary>
        /// Limits the bandwith used by the subsequent stages in the owin pipeline.
        /// </summary>
        /// <param name="options">The max bandwidth options.</param>
        /// <returns>An OWIN middleware delegate.</returns>
        /// <exception cref="System.ArgumentNullException">options</exception>
        public static MidFunc MaxBandwidth(MaxBandwidthOptions options)
        {
            options.MustNotNull("options");

            return
                next =>
                env =>
                {
                    var context = new OwinContext(env);
                    Stream requestBodyStream = context.Request.Body ?? Stream.Null;
                    Stream responseBodyStream = context.Response.Body;
                    int maxBytesPerSecond = options.MaxBytesPerSecond;
                    if (maxBytesPerSecond < 0)
                    {
                        maxBytesPerSecond = 0;
                    }
                    options.Tracer.AsVerbose("Configure streams to be limited.");
                    context.Request.Body = new ThrottledStream(requestBodyStream, maxBytesPerSecond);
                    context.Response.Body = new ThrottledStream(responseBodyStream, maxBytesPerSecond);

                    //TODO consider SendFile interception
                    options.Tracer.AsVerbose("With configured limit forwarded.");
                    return next(env);
                };
        }