src/packages/EmbedIO/Utilities/Validate-HttpHeaders.cs
/// <summary>
/// <para>Ensures that a <see langword="string"/> argument is valid as an HTTP header value as defined by
/// <see href="https://tools.ietf.org/html/rfc7230#section-3.2">RFC7230, Section 3.2</see>.</para>
/// <para>Only visible ASCII visible characters, plus space and horizontal tab, are allowed.</para>
/// <para><see langword="null"/> is allowed, but it is turned into the empty string.</para>
/// <para>Obsolete line folding (see <see href="https://tools.ietf.org/html/rfc7230#section-3.2.4">Section 3.2.4</see>)
/// is NOT allowed, even if it is still allowed by Microsoft's <see cref="WebHeaderCollection"/>.</para>
/// </summary>
/// <param name="argumentName">The name of the argument to validate.</param>
/// <param name="value">The value to validate.</param>
/// <returns><paramref name="value"/>, if it is a valid HTTP header value, with spaces
/// and horizontal tabs trimmed on both ends..</returns>
/// <exception cref="ArgumentException"><paramref name="value"/> contains one or more characters
/// that are not allowed in a HTTP header value.</exception>
Ensures that a  argument is valid as an HTTP header value as defined by
RFC7230, Section 3.2Only visible ASCII visible characters, plus space and horizontal tab, are allowed. is allowed, but it is turned into the empty string.Obsolete line folding (see Section 3.2.4.
Ensures that a argument is valid as an HTTP header value as defined by RFC7230 Section 3 . 2Only visible ASCII visible characters plus space and horizontal tab are allowed . is allowed but it is turned into the empty string . Obsolete line folding ( see Section 3 . 2 . 4 .
public static string HttpHeaderValue(string argumentName, string value)
        {
            if (string.IsNullOrEmpty(value))
                return string.Empty;
            if (!value.All(c => c == '\t' || (c >= ' ' && c <= '~')))
                throw new ArgumentException("Header value contains one or more invalid characters.", argumentName);
            return value.Trim(HttpTrimCharacters);
        }
public static string HttpHeaderValue ( string argumentName , string value ) { if ( string . IsNullOrEmpty ( value ) ) return string . Empty ; if ( ! value . All ( c => c == ' \t ' || ( c >= ' ' && c <= ' ' ) ) ) throw new ArgumentException ( " " , argumentName ) ; return value . Trim ( HttpTrimCharacters ) ; }
Ensures that a  argument is valid as an HTTP header value as defined by
RFC7230, Section 3.2Only visible ASCII visible characters, plus space and horizontal tab, are allowed.
Ensures that a argument is valid as an HTTP header value as defined by RFC7230 Section 3 . 2Only visible ASCII visible characters plus space and horizontal tab are allowed .
// Allow null but turn it into an empty string // RFC7230, Section 3.2 // Only USASCII visible characters +  space + horizontal tab are allowed in field content. // As per Section 3.2.4 no line folding is allowed (even if Microsoft's WebHeaderCollection still allows it)
, if it is a valid HTTP header value, with spaces
and horizontal tabs trimmed on both ends
if it is a valid HTTP header value with spaces and horizontal tabs trimmed on both ends
contains one or more characters
that are not allowed in a HTTP header value.
contains one or more characters that are not allowed in a HTTP header value .
argumentName
string
The name of the argument to validate.
The name of the argument to validate .
value
string
The value to validate.
The value to validate .