Top Description Fields Constructors Methods
io.netty5.handler.codec.http

public final Class HttpUtil

extends Object
Class Inheritance
Imports
io.netty5.handler.codec.http.headers.HttpHeaders, io.netty5.util.AsciiString, .NetUtil, io.netty5.util.internal.UnstableApi, java.net.InetSocketAddress, .URI, java.nio.charset.Charset, .IllegalCharsetNameException, .StandardCharsets, .UnsupportedCharsetException, java.util.ArrayList, .Iterator, .List

Utility methods useful in the HTTP context.

Field Summary

Modifier and TypeField and Description
private static final AsciiString
private static final String
private static final AsciiString

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

the formatted String
formatHostnameForHttp
(InetSocketAddress
the address
addr
)

Formats the host string of an address so it can be used for computing an HTTP component such as a URL or a Host header

public static Charset

Returns:

the charset from message's Content-Type header or StandardCharsets#ISO_8859_1 if charset is not presented or unparsable
getCharset
(HttpMessage
entity to fetch Content-Type header from
message
)

Fetch charset from message's Content-Type header.

public static Charset

Returns:

the charset from message's Content-Type header or StandardCharsets#ISO_8859_1 if charset is not presented or unparsable
getCharset
(CharSequence
Content-Type header value to parse
contentTypeValue
)

Fetch charset from Content-Type header value.

public static Charset

Returns:

the charset from message's Content-Type header or defaultCharset if charset is not presented or unparsable
getCharset
(HttpMessage
entity to fetch Content-Type header from
message
,
Charset
result to use in case of empty, incorrect or doesn't contain required part header value
defaultCharset
)

Fetch charset from message's Content-Type header.

public static Charset

Returns:

the charset from message's Content-Type header or defaultCharset if charset is not presented or unparsable
getCharset
(CharSequence
Content-Type header value to parse
contentTypeValue
,
Charset
result to use in case of empty, incorrect or doesn't contain required part header value
defaultCharset
)

Fetch charset from Content-Type header value.

public static CharSequence

Returns:

the CharSequence with charset from message's Content-Type header or null if charset is not presented
getCharsetAsSequence
(HttpMessage message)

Fetch charset from message's Content-Type header as a char sequence.

public static CharSequence

Returns:

the CharSequence with charset from message's Content-Type header or null if charset is not presented
getCharsetAsSequence
(CharSequence
Content-Type header value to parse
contentTypeValue
)

Fetch charset from Content-Type header value as a char sequence.

public static CharSequence

Returns:

the CharSequence with charset from message's Content-Type header or null if charset is not presented
getCharsetAsString
(HttpMessage
entity to fetch Content-Type header from
message
)

Fetch charset from message's Content-Type header as a char sequence.
public static long

Returns:

the content length
getContentLength
(HttpMessage message)

Returns the length of the content.

public static long

Returns:

the content length or the specified default value
getContentLength
(HttpMessage
the message
message
,
long
the default value
defaultValue
)

Returns the length of the content or the specified default value if the message does not have the "Content-Length" header.

public static int

Returns:

the content length or defaultValue if this message does not have the "Content-Length" header.
getContentLength
(HttpMessage message, int defaultValue)

Get an int representation of getContentLength(HttpMessage, long).

public static CharSequence

Returns:

the MIME type as a CharSequence from message's Content-Type header or null if content-type header or MIME type part of this header are not presented

"content-type: text/html; charset=utf-8" - "text/html" will be returned
"content-type: text/html" - "text/html" will be returned
"content-type: " or no header - null we be returned

getMimeType
(HttpMessage
entity to fetch Content-Type header from
message
)

Fetch MIME type part from message's Content-Type header as a char sequence.

public static CharSequence

Returns:

the MIME type as a CharSequence from message's Content-Type header or null if content-type header or MIME type part of this header are not presented

"content-type: text/html; charset=utf-8" - "text/html" will be returned
"content-type: text/html" - "text/html" will be returned
"content-type: empty header - null we be returned

getMimeType
(CharSequence
Content-Type header value to parse
contentTypeValue
)

Fetch MIME type part from Content-Type header value as a char sequence.

public static boolean

Returns:

true if and only if the expectation 100-continue is present and it is the only expectation present
is100ContinueExpected
(HttpMessage
the message
message
)

Returns true if and only if the specified message contains an expect header and the only expectation present is the 100-continue expectation.

public static boolean
isAsteriskForm(URI uri)

Determine if a uri is in asterisk-form according to rfc7230, 5.3.

public static boolean
isAsteriskForm(String uri)

Determine if a string uri is in asterisk-form according to rfc7230, 5.3.

public static boolean
private static boolean
public static boolean
isKeepAlive(HttpMessage message)

Returns true if and only if the connection can remain open and thus 'kept alive'.

public static boolean
isOriginForm(URI uri)

Determine if a uri is in origin-form according to rfc7230, 5.3.

public static boolean
isOriginForm(String uri)

Determine if a string uri is in origin-form according to rfc7230, 5.3.

public static boolean

Returns:

True if transfer encoding is chunked, otherwise false
isTransferEncodingChunked
(HttpMessage
The message to check
message
)

Checks to see if the transfer encoding in a specified HttpMessage is chunked

pack-priv static boolean

Returns:

true if and only if an expectation is present that is not supported
isUnsupportedExpectation
(HttpMessage
the message
message
)

Returns true if the specified message contains an expect header specifying an expectation that is not supported.

public static long

Returns:

the normalized content length from the headers or -1 if the fields were empty.
normalizeAndGetContentLength
(Iterator<CharSequence>
the content-length header fields.
contentLengthFields
,
boolean
true if we are handling HTTP/1.0 or earlier
isHttp10OrEarlier
,
boolean
true if multiple, identical-value content lengths should be allowed.
allowDuplicateContentLengths
)

Validates, and optionally extracts the content length from headers.

public static void
set100ContinueExpected(HttpMessage message, boolean expected)

Sets or removes the "Expect: 100-continue" header to / from the specified message.

public static void
setContentLength(HttpMessage message, long length)

Sets the "Content-Length" header.

public static void
setKeepAlive(HttpMessage message, boolean keepAlive)

Sets the value of the "Connection" header depending on the protocol version of the specified message.

public static void
setKeepAlive(HttpHeaders h, HttpVersion httpVersion, boolean keepAlive)

Sets the value of the "Connection" header depending on the protocol version of the specified message.

public static void
setTransferEncodingChunked(HttpMessage
The message which contains the headers to modify.
m
,
boolean
if true then include HttpHeaderValues#CHUNKED in the headers. otherwise remove HttpHeaderValues#CHUNKED from the headers.
chunked
)

Set the HttpHeaderNames#TRANSFER_ENCODING to either include HttpHeaderValues#CHUNKED if chunked is true, or remove HttpHeaderValues#CHUNKED if chunked is false.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CHARSET_EQUALSback to summary
private static final AsciiString CHARSET_EQUALS
COMMA_STRINGback to summary
private static final String COMMA_STRING
SEMICOLONback to summary
private static final AsciiString SEMICOLON

Constructor Detail

HttpUtilback to summary
private HttpUtil()

Method Detail

formatHostnameForHttpback to summary
public static String formatHostnameForHttp(InetSocketAddress addr)

Formats the host string of an address so it can be used for computing an HTTP component such as a URL or a Host header

Parameters
addr:InetSocketAddress

the address

Returns:String

the formatted String

getCharsetback to summary
public static Charset getCharset(HttpMessage message)

Fetch charset from message's Content-Type header.

Parameters
message:HttpMessage

entity to fetch Content-Type header from

Returns:Charset

the charset from message's Content-Type header or StandardCharsets#ISO_8859_1 if charset is not presented or unparsable

getCharsetback to summary
public static Charset getCharset(CharSequence contentTypeValue)

Fetch charset from Content-Type header value.

Parameters
contentTypeValue:CharSequence

Content-Type header value to parse

Returns:Charset

the charset from message's Content-Type header or StandardCharsets#ISO_8859_1 if charset is not presented or unparsable

getCharsetback to summary
public static Charset getCharset(HttpMessage message, Charset defaultCharset)

Fetch charset from message's Content-Type header.

Parameters
message:HttpMessage

entity to fetch Content-Type header from

defaultCharset:Charset

result to use in case of empty, incorrect or doesn't contain required part header value

Returns:Charset

the charset from message's Content-Type header or defaultCharset if charset is not presented or unparsable

getCharsetback to summary
public static Charset getCharset(CharSequence contentTypeValue, Charset defaultCharset)

Fetch charset from Content-Type header value.

Parameters
contentTypeValue:CharSequence

Content-Type header value to parse

defaultCharset:Charset

result to use in case of empty, incorrect or doesn't contain required part header value

Returns:Charset

the charset from message's Content-Type header or defaultCharset if charset is not presented or unparsable

getCharsetAsSequenceback to summary
public static CharSequence getCharsetAsSequence(HttpMessage message)

Fetch charset from message's Content-Type header as a char sequence.

A lot of sites/possibly clients have charset="CHARSET", for example charset="utf-8". Or "utf8" instead of "utf-8" This is not according to standard, but this method provide an ability to catch desired mistakes manually in code

Returns:CharSequence

the CharSequence with charset from message's Content-Type header or null if charset is not presented

getCharsetAsSequenceback to summary
public static CharSequence getCharsetAsSequence(CharSequence contentTypeValue)

Fetch charset from Content-Type header value as a char sequence.

A lot of sites/possibly clients have charset="CHARSET", for example charset="utf-8". Or "utf8" instead of "utf-8" This is not according to standard, but this method provide an ability to catch desired mistakes manually in code

Parameters
contentTypeValue:CharSequence

Content-Type header value to parse

Returns:CharSequence

the CharSequence with charset from message's Content-Type header or null if charset is not presented

Exceptions
NullPointerException:
in case if contentTypeValue == null
getCharsetAsStringback to summary
public static CharSequence getCharsetAsString(HttpMessage message)

Fetch charset from message's Content-Type header as a char sequence.

A lot of sites/possibly clients have charset="CHARSET", for example charset="utf-8". Or "utf8" instead of "utf-8" This is not according to standard, but this method provide an ability to catch desired mistakes manually in code

Parameters
message:HttpMessage

entity to fetch Content-Type header from

Returns:CharSequence

the CharSequence with charset from message's Content-Type header or null if charset is not presented

Annotations
@Deprecated
getContentLengthback to summary
public static long getContentLength(HttpMessage message)

Returns the length of the content. Please note that this value is not retrieved from HttpContent#payload() but from the "Content-Length" header, and thus they are independent from each other.

Returns:long

the content length

Exceptions
NumberFormatException:
if the message does not have the "Content-Length" header or its value is not a number
getContentLengthback to summary
public static long getContentLength(HttpMessage message, long defaultValue)

Returns the length of the content or the specified default value if the message does not have the "Content-Length" header. Please note that this value is not retrieved from HttpContent#payload() but from the "Content-Length" header, and thus they are independent from each other.

Parameters
message:HttpMessage

the message

defaultValue:long

the default value

Returns:long

the content length or the specified default value

Exceptions
NumberFormatException:
if the "Content-Length" header does not parse as a long
getContentLengthback to summary
public static int getContentLength(HttpMessage message, int defaultValue)

Get an int representation of getContentLength(HttpMessage, long).

Returns:int

the content length or defaultValue if this message does not have the "Content-Length" header.

Exceptions
NumberFormatException:
if the "Content-Length" header does not parse as an int
getMimeTypeback to summary
public static CharSequence getMimeType(HttpMessage message)

Fetch MIME type part from message's Content-Type header as a char sequence.

Parameters
message:HttpMessage

entity to fetch Content-Type header from

Returns:CharSequence

the MIME type as a CharSequence from message's Content-Type header or null if content-type header or MIME type part of this header are not presented

"content-type: text/html; charset=utf-8" - "text/html" will be returned
"content-type: text/html" - "text/html" will be returned
"content-type: " or no header - null we be returned

getMimeTypeback to summary
public static CharSequence getMimeType(CharSequence contentTypeValue)

Fetch MIME type part from Content-Type header value as a char sequence.

Parameters
contentTypeValue:CharSequence

Content-Type header value to parse

Returns:CharSequence

the MIME type as a CharSequence from message's Content-Type header or null if content-type header or MIME type part of this header are not presented

"content-type: text/html; charset=utf-8" - "text/html" will be returned
"content-type: text/html" - "text/html" will be returned
"content-type: empty header - null we be returned

Exceptions
NullPointerException:
in case if contentTypeValue == null
is100ContinueExpectedback to summary
public static boolean is100ContinueExpected(HttpMessage message)

Returns true if and only if the specified message contains an expect header and the only expectation present is the 100-continue expectation. Note that this method returns false if the expect header is not valid for the message (e.g., the message is a response, or the version on the message is HTTP/1.0).

Parameters
message:HttpMessage

the message

Returns:boolean

true if and only if the expectation 100-continue is present and it is the only expectation present

isAsteriskFormback to summary
public static boolean isAsteriskForm(URI uri)

Determine if a uri is in asterisk-form according to rfc7230, 5.3.

isAsteriskFormback to summary
public static boolean isAsteriskForm(String uri)

Determine if a string uri is in asterisk-form according to rfc7230, 5.3.

isContentLengthSetback to summary
public static boolean isContentLengthSet(HttpMessage m)
isExpectHeaderValidback to summary
private static boolean isExpectHeaderValid(final HttpMessage message)
isKeepAliveback to summary
public static boolean isKeepAlive(HttpMessage message)

Returns true if and only if the connection can remain open and thus 'kept alive'. This method respects the value of the "Connection" header first and then the return value of HttpVersion#isKeepAliveDefault().

isOriginFormback to summary
public static boolean isOriginForm(URI uri)

Determine if a uri is in origin-form according to rfc7230, 5.3.

isOriginFormback to summary
public static boolean isOriginForm(String uri)

Determine if a string uri is in origin-form according to rfc7230, 5.3.

isTransferEncodingChunkedback to summary
public static boolean isTransferEncodingChunked(HttpMessage message)

Checks to see if the transfer encoding in a specified HttpMessage is chunked

Parameters
message:HttpMessage

The message to check

Returns:boolean

True if transfer encoding is chunked, otherwise false

isUnsupportedExpectationback to summary
pack-priv static boolean isUnsupportedExpectation(HttpMessage message)

Returns true if the specified message contains an expect header specifying an expectation that is not supported. Note that this method returns false if the expect header is not valid for the message (e.g., the message is a response, or the version on the message is HTTP/1.0).

Parameters
message:HttpMessage

the message

Returns:boolean

true if and only if an expectation is present that is not supported

normalizeAndGetContentLengthback to summary
public static long normalizeAndGetContentLength(Iterator<CharSequence> contentLengthFields, boolean isHttp10OrEarlier, boolean allowDuplicateContentLengths)

Validates, and optionally extracts the content length from headers. This method is not intended for general use, but is here to be shared between HTTP/1 and HTTP/2 parsing.

Parameters
contentLengthFields:Iterator<CharSequence>

the content-length header fields.

isHttp10OrEarlier:boolean

true if we are handling HTTP/1.0 or earlier

allowDuplicateContentLengths:boolean

true if multiple, identical-value content lengths should be allowed.

Returns:long

the normalized content length from the headers or -1 if the fields were empty.

Annotations
@UnstableApi
Exceptions
IllegalArgumentException:
if the content-length fields are not valid
set100ContinueExpectedback to summary
public static void set100ContinueExpected(HttpMessage message, boolean expected)

Sets or removes the "Expect: 100-continue" header to / from the specified message. If expected is true, the "Expect: 100-continue" header is set and all other previous "Expect" headers are removed. Otherwise, all "Expect" headers are removed completely.

setContentLengthback to summary
public static void setContentLength(HttpMessage message, long length)

Sets the "Content-Length" header.

setKeepAliveback to summary
public static void setKeepAlive(HttpMessage message, boolean keepAlive)

Sets the value of the "Connection" header depending on the protocol version of the specified message. This getMethod sets or removes the "Connection" header depending on what the default keep alive mode of the message's protocol version is, as specified by HttpVersion#isKeepAliveDefault().

  • If the connection is kept alive by default:
    • set to "close" if keepAlive is false.
    • remove otherwise.
  • If the connection is closed by default:
    • set to "keep-alive" if keepAlive is true.
    • remove otherwise.
See Also
setKeepAlive(HttpHeaders, HttpVersion, boolean)
setKeepAliveback to summary
public static void setKeepAlive(HttpHeaders h, HttpVersion httpVersion, boolean keepAlive)

Sets the value of the "Connection" header depending on the protocol version of the specified message. This getMethod sets or removes the "Connection" header depending on what the default keep alive mode of the message's protocol version is, as specified by HttpVersion#isKeepAliveDefault().

  • If the connection is kept alive by default:
    • set to "close" if keepAlive is false.
    • remove otherwise.
  • If the connection is closed by default:
    • set to "keep-alive" if keepAlive is true.
    • remove otherwise.
setTransferEncodingChunkedback to summary
public static void setTransferEncodingChunked(HttpMessage m, boolean chunked)

Set the HttpHeaderNames#TRANSFER_ENCODING to either include HttpHeaderValues#CHUNKED if chunked is true, or remove HttpHeaderValues#CHUNKED if chunked is false.

Parameters
m:HttpMessage

The message which contains the headers to modify.

chunked:boolean

if true then include HttpHeaderValues#CHUNKED in the headers. otherwise remove HttpHeaderValues#CHUNKED from the headers.