src/packages/EmbedIO/HttpRequestExtensions.cs
/// <summary>
/// <para>Attempts to proactively negotiate a compression method for a response,
/// based on a request's <c>Accept-Encoding</c> header (or lack of it).</para>
/// </summary>
/// <param name="this">The <see cref="IHttpRequest"/> on which this method is called.</param>
/// <param name="preferCompression"><see langword="true"/> if sending compressed data is preferred over
/// sending non-compressed data; otherwise, <see langword="false"/>.</param>
/// <param name="compressionMethod">When this method returns, the compression method to use for the response,
/// if content negotiation is successful. This parameter is passed uninitialized.</param>
/// <param name="prepareResponse">When this method returns, a callback that prepares data in an <see cref="IHttpResponse"/>
/// according to the result of content negotiation. This parameter is passed uninitialized.</param>
/// <returns><see langword="true"/> if content negotiation is successful;
/// otherwise, <see langword="false"/>.</returns>
/// <remarks>
/// <para>If this method returns <see langword="true"/>, the <paramref name="prepareResponse"/> callback
/// will set appropriate response headers to reflect the results of content negotiation.</para>
/// <para>If this method returns <see langword="false"/>, the <paramref name="prepareResponse"/> callback
/// will throw a <see cref="HttpNotAcceptableException"/> to send a <c>406 Not Acceptable</c> response
/// with the <c>Vary</c> header set to <c>Accept-Encoding</c>,
/// so that the client may know the reason why the request has been rejected.</para>
/// <para>If <paramref name="this"/> has no<c>Accept-Encoding</c> header, this method
/// always returns <see langword="true"/> and sets <paramref name="compressionMethod"/>
/// to <see cref="CompressionMethod.None"/>.</para>
/// </remarks>
/// <seealso cref="HttpNotAcceptableException(string)"/>
Attempts to proactively negotiate a compression method for a response,
based on a request's Accept-Encoding
Attempts to proactively negotiate a compression method for a response based on a request ' s Accept - Encoding
public static bool TryNegotiateContentEncoding(
            this IHttpRequest @this,
            bool preferCompression,
            out CompressionMethod compressionMethod,
            out Action<IHttpResponse> prepareResponse)
        {
            var acceptedEncodings = new QValueList(true, @this.Headers.GetValues(HttpHeaderNames.AcceptEncoding));
            if (!acceptedEncodings.TryNegotiateContentEncoding(preferCompression, out compressionMethod, out var compressionMethodName))
            {
                prepareResponse = r => throw HttpException.NotAcceptable(HttpHeaderNames.AcceptEncoding);
                return false;
            }
            prepareResponse = r =>
            {
                r.Headers.Add(HttpHeaderNames.Vary, HttpHeaderNames.AcceptEncoding);
                r.Headers.Set(HttpHeaderNames.ContentEncoding, compressionMethodName);
            };
            return true;
        }
public static bool TryNegotiateContentEncoding ( this IHttpRequest @this , bool preferCompression , out CompressionMethod compressionMethod , out Action < IHttpResponse > prepareResponse ) { var acceptedEncodings = new QValueList ( true , @this . Headers . GetValues ( HttpHeaderNames . AcceptEncoding ) ) ; if ( ! acceptedEncodings . TryNegotiateContentEncoding ( preferCompression , out compressionMethod , out var compressionMethodName ) ) { prepareResponse = r => throw HttpException . NotAcceptable ( HttpHeaderNames . AcceptEncoding ) ; return false ; } prepareResponse = r => { r . Headers . Add ( HttpHeaderNames . Vary , HttpHeaderNames . AcceptEncoding ) ; r . Headers . Set ( HttpHeaderNames . ContentEncoding , compressionMethodName ) ; } ; return true ; }
Attempts to proactively negotiate a compression method for a response,
based on a request's Accept-Encoding
Attempts to proactively negotiate a compression method for a response based on a request ' s Accept - Encoding
if content negotiation is successful;
otherwise, .
if content negotiation is successful ; otherwise .
IHttpRequest
this
preferCompression
bool
if sending compressed data is preferred over
sending non-compressed data; otherwise, .
if sending compressed data is preferred over sending non - compressed data ; otherwise .
CompressionMethod
out
Action<IHttpResponse>
out
this
The  on which this method is called.
The on which this method is called .
compressionMethod
When this method returns, the compression method to use for the response,
if content negotiation is successful. This parameter is passed uninitialized.
When this method returns the compression method to use for the response if content negotiation is successful . This parameter is passed uninitialized .
prepareResponse
When this method returns, a callback that prepares data in an
according to the result of content negotiation. This parameter is passed uninitialized.
When this method returns a callback that prepares data in an according to the result of content negotiation . This parameter is passed uninitialized .
remarks
If this method returns , the  callback
will set appropriate response headers to reflect the results of content negotiation.If this method returns , the  callback
will throw a  to send a 406 Not AcceptableVaryAccept-EncodingIf  has noAccept-Encoding and sets
to .
If this method returns the callback will set appropriate response headers to reflect the results of content negotiation . If this method returns the callback will throw a to send a 406 Not AcceptableVaryAccept - EncodingIf has noAccept - Encoding and sets to .
seealso
None