Unosquare
    Show / Hide Table of Contents

    Class HttpRequestExtensions

    Provides extension methods for types implementing IHttpRequest.

    Inheritance
    Object
    HttpRequestExtensions
    Inherited Members
    Object.ToString()
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Namespace: EmbedIO
    Syntax
    public static class HttpRequestExtensions

    Methods

    CheckIfModifiedSince(IHttpRequest, DateTime, out Boolean)

    Checks whether an If-Modified-Since header exists in a request and, if so, whether its value is a date and time more recent or equal to a given .

    See RFC7232, Section 3.3 for a normative reference.

    Declaration
    public static bool CheckIfModifiedSince(this IHttpRequest this, DateTime lastModifiedUtc, out bool headerExists)
    Parameters
    Type Name Description
    IHttpRequest this

    The IHttpRequest on which this method is called.

    DateTime lastModifiedUtc

    A date and time value, in Coordinated Universal Time, expressing the last time a resource was modified.

    Boolean headerExists

    When this method returns, a value that indicates whether an If-Modified-Since header is present in this, regardless of the method's return value. This parameter is passed uninitialized.

    Returns
    Type Description
    Boolean

    true if an If-Modified-Since header is present in this and its value is a date and time more recent or equal to lastModifiedUtc; false otherwise.

    CheckIfNoneMatch(IHttpRequest, String, out Boolean)

    Checks whether an If-None-Match header exists in a request and, if so, whether it contains a given entity tag.

    See RFC7232, Section 3.2 for a normative reference; however, see the Remarks section for more information about the RFC compliance of this method.

    Declaration
    public static bool CheckIfNoneMatch(this IHttpRequest this, string entityTag, out bool headerExists)
    Parameters
    Type Name Description
    IHttpRequest this

    The IHttpRequest on which this method is called.

    String entityTag

    The entity tag.

    Boolean headerExists

    When this method returns, a value that indicates whether an If-None-Match header is present in this, regardless of the method's return value. This parameter is passed uninitialized.

    Returns
    Type Description
    Boolean

    true if an If-None-Match header is present in this and one of the entity tags listed in it is equal to entityTag; false otherwise.

    Remarks

    RFC7232, Section 3.2 states that a weak comparison function (as defined in RFC7232, Section 2.3.2) must be used for If-None-Match. That would mean parsing every entity tag, at least minimally, to determine whether it is a "weak" or "strong" tag. Since EmbedIO currently generates only "strong" tags, this method uses the default string comparer instead.

    The behavior of this method is thus not, strictly speaking, RFC7232-compliant; it works, though, with entity tags generated by EmbedIO.

    IsRangeRequest(IHttpRequest, Int64, String, DateTime, out Int64, out Int64)

    Checks whether a Range header exists in a request and, if so, determines whether it is possible to send a 206 Partial Content response.

    See RFC7233 for a normative reference; however, see the Remarks section for more information about the RFC compliance of this method.

    Declaration
    public static bool IsRangeRequest(this IHttpRequest this, long contentLength, string entityTag, DateTime lastModifiedUtc, out long start, out long upperBound)
    Parameters
    Type Name Description
    IHttpRequest this

    The IHttpRequest on which this method is called.

    Int64 contentLength

    The total length, in bytes, of the response entity, i.e. what would be sent in a 200 OK response.

    String entityTag

    An entity tag representing the response entity. This value is checked against the If-Range header, if it is present.

    DateTime lastModifiedUtc

    The date and time value, in Coordinated Universal Time, expressing the last modification time of the resource entity. This value is checked against the If-Range header, if it is present.

    Int64 start

    When this method returns true, the start of the requested byte range. This parameter is passed uninitialized.

    Int64 upperBound

    When this method returns true, the upper bound of the requested byte range. This parameter is passed uninitialized.

    Note that the upper bound of a range is NOT the sum of the range's start and length; for example, a range expressed as bytes=0-99 has a start of 0, an upper bound of 99, and a length of 100 bytes.

    Returns
    Type Description
    Boolean

    This method returns true if the following conditions are satisfied:

    • >the request's HTTP method is GET;
    • >a Range header is present in the request;
    • >either no If-Range header is present in the request, or it specifies an entity tag equal to entityTag, or a UTC date and time equal to lastModifiedUtc;
    • >the Range header specifies exactly one range;
    • >the specified range is entirely contained in the range from 0 to contentLength - 1.

    If the last condition is not satisfied, i.e. the specified range start and/or upper bound are out of the range from 0 to contentLength - 1, this method does not return; it throws a HttpRangeNotSatisfiableException instead.

    If any of the other conditions are not satisfied, this method returns false.

    Remarks

    According to RFC7233, Section 3.1, there are several conditions under which a server may ignore or reject a range request; therefore, clients are (or should be) prepared to receive a 200 OK response with the whole response entity instead of the requested range(s). For this reason, until the generation of multipart/byteranges responses is implemented in EmbedIO, this method will ignore range requests specifying more than one range, even if this behavior is not, strictly speaking, RFC7233-compliant.

    To make clients aware that range requests are accepted for a resource, every 200 OK (or 304 Not Modified) response for the same resource should include an Accept-Ranges header with the string bytes as value.

    SafeGetRemoteEndpointStr(IHttpRequest)

    Returns a string representing the remote IP address and port of an IHttpRequest interface.

    This method can be called even on a null interface, or one that has no remote end point, or no remote address; it will always return a non-null, non-empty string.

    Declaration
    public static string SafeGetRemoteEndpointStr(this IHttpRequest this)
    Parameters
    Type Name Description
    IHttpRequest this

    The IHttpRequest on which this method is called.

    Returns
    Type Description
    String

    If this is null, or its RemoteEndPoint is null, the string "<null>; otherwise, the remote end point's Address (or the string "<???>" if it is null) followed by a colon and the Port number.

    TryNegotiateContentEncoding(IHttpRequest, Boolean, out CompressionMethod, out Action<IHttpResponse>)

    Attempts to proactively negotiate a compression method for a response, based on a request's Accept-Encoding header (or lack of it).

    Declaration
    public static bool TryNegotiateContentEncoding(this IHttpRequest this, bool preferCompression, out CompressionMethod compressionMethod, out Action<IHttpResponse> prepareResponse)
    Parameters
    Type Name Description
    IHttpRequest this

    The IHttpRequest on which this method is called.

    Boolean preferCompression

    true if sending compressed data is preferred over sending non-compressed data; otherwise, false.

    CompressionMethod compressionMethod

    When this method returns, the compression method to use for the response, if content negotiation is successful. This parameter is passed uninitialized.

    Action<IHttpResponse> prepareResponse

    When this method returns, a callback that prepares data in an IHttpResponse according to the result of content negotiation. This parameter is passed uninitialized.

    Returns
    Type Description
    Boolean

    true if content negotiation is successful; otherwise, false.

    Remarks

    If this method returns true, the prepareResponse callback will set appropriate response headers to reflect the results of content negotiation.

    If this method returns false, the prepareResponse callback will throw a HttpNotAcceptableException to send a 406 Not Acceptable response with the Vary header set to Accept-Encoding, so that the client may know the reason why the request has been rejected.

    If this has noAccept-Encoding header, this method always returns true and sets compressionMethod to None.

    See Also
    HttpNotAcceptableException(String)

    Comments

    Back to top Copyright © 2017-2019 Unosquare