// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Net.Http; using System.Web; using System.Web.Http.Hosting; namespace System.Web.Http.WebHost { /// /// Provides an implementation of suited for use /// in an ASP.NET environment which provides direct support for input and output buffering. /// public class WebHostBufferPolicySelector : IHostBufferPolicySelector { /// /// Determines whether the host should buffer the entity body when processing a request with content. /// /// The host-specific context. In this case, it is an instance /// of . /// true if buffering should be used; otherwise a streamed request should be used. public virtual bool UseBufferedInputStream(object hostContext) { if (hostContext == null) { throw Error.ArgumentNull("hostContext"); } return true; } /// /// Determines whether the host should buffer the entity body. /// /// The response for which to determine /// whether host output buffering should be used for the response entity body. /// true if buffering should be used; otherwise a streamed response should be used. public virtual bool UseBufferedOutputStream(HttpResponseMessage response) { if (response == null) { throw Error.ArgumentNull("response"); } // Any HttpContent that knows its length is presumably already buffered internally. HttpContent content = response.Content; if (content != null) { long? contentLength = content.Headers.ContentLength; if (contentLength.HasValue && contentLength.Value >= 0) { return false; } // Content length is null or -1 (meaning not known). // Buffer any HttpContent except StreamContent and PushStreamContent return !(content is StreamContent || content is PushStreamContent); } return false; } } }