Package io.netty.handler.codec.http
Class HttpUtil
- java.lang.Object
-
- io.netty.handler.codec.http.HttpUtil
-
public final class HttpUtil extends java.lang.Object
Utility methods useful in the HTTP context.
-
-
Field Summary
Fields Modifier and Type Field Description private static AsciiString
CHARSET_EQUALS
private static java.lang.String
COMMA_STRING
private static AsciiString
SEMICOLON
private static long
TOKEN_CHARS_HIGH
private static long
TOKEN_CHARS_LOW
-
Constructor Summary
Constructors Modifier Constructor Description private
HttpUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.lang.String
formatHostnameForHttp(java.net.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 headerstatic java.nio.charset.Charset
getCharset(HttpMessage message)
Fetch charset from message's Content-Type header.static java.nio.charset.Charset
getCharset(HttpMessage message, java.nio.charset.Charset defaultCharset)
Fetch charset from message's Content-Type header.static java.nio.charset.Charset
getCharset(java.lang.CharSequence contentTypeValue)
Fetch charset from Content-Type header value.static java.nio.charset.Charset
getCharset(java.lang.CharSequence contentTypeValue, java.nio.charset.Charset defaultCharset)
Fetch charset from Content-Type header value.static java.lang.CharSequence
getCharsetAsSequence(HttpMessage message)
Fetch charset from message's Content-Type header as a char sequence.static java.lang.CharSequence
getCharsetAsSequence(java.lang.CharSequence contentTypeValue)
Fetch charset from Content-Type header value as a char sequence.static java.lang.CharSequence
getCharsetAsString(HttpMessage message)
Deprecated.static long
getContentLength(HttpMessage message)
Returns the length of the content.static int
getContentLength(HttpMessage message, int defaultValue)
Get anint
representation ofgetContentLength(HttpMessage, long)
.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
.static java.lang.CharSequence
getMimeType(HttpMessage message)
Fetch MIME type part from message's Content-Type header as a char sequence.static java.lang.CharSequence
getMimeType(java.lang.CharSequence contentTypeValue)
Fetch MIME type part from Content-Type header value as a char sequence.(package private) static int
getWebSocketContentLength(HttpMessage message)
Returns the content length of the specified web socket message.static boolean
is100ContinueExpected(HttpMessage message)
Returnstrue
if and only if the specified message contains an expect header and the only expectation present is the 100-continue expectation.static boolean
isAsteriskForm(java.lang.String uri)
Determine if a string uri is in asterisk-form according to rfc7230, 5.3.static boolean
isAsteriskForm(java.net.URI uri)
Determine if a uri is in asterisk-form according to rfc7230, 5.3.static boolean
isContentLengthSet(HttpMessage m)
private static boolean
isExpectHeaderValid(HttpMessage message)
static boolean
isKeepAlive(HttpMessage message)
Returnstrue
if and only if the connection can remain open and thus 'kept alive'.static boolean
isOriginForm(java.lang.String uri)
Determine if a string uri is in origin-form according to rfc7230, 5.3.static boolean
isOriginForm(java.net.URI uri)
Determine if a uri is in origin-form according to rfc7230, 5.3.static boolean
isTransferEncodingChunked(HttpMessage message)
Checks to see if the transfer encoding in a specifiedHttpMessage
is chunked(package private) static boolean
isUnsupportedExpectation(HttpMessage message)
Returnstrue
if the specified message contains an expect header specifying an expectation that is not supported.private static boolean
isValidTokenChar(byte bit)
static long
normalizeAndGetContentLength(java.util.List<? extends java.lang.CharSequence> contentLengthFields, boolean isHttp10OrEarlier, boolean allowDuplicateContentLengths)
Validates, and optionally extracts the content length from headers.static void
set100ContinueExpected(HttpMessage message, boolean expected)
Sets or removes the"Expect: 100-continue"
header to / from the specified message.static void
setContentLength(HttpMessage message, long length)
Sets the"Content-Length"
header.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.static void
setKeepAlive(HttpMessage message, boolean keepAlive)
Sets the value of the"Connection"
header depending on the protocol version of the specified message.static void
setTransferEncodingChunked(HttpMessage m, boolean chunked)
Set theHttpHeaderNames.TRANSFER_ENCODING
to either includeHttpHeaderValues.CHUNKED
ifchunked
istrue
, or removeHttpHeaderValues.CHUNKED
ifchunked
isfalse
.private static int
validateAsciiStringToken(AsciiString token)
Validate that anAsciiString
contain onlu valid token characters.private static int
validateCharSequenceToken(java.lang.CharSequence token)
Validate that aCharSequence
contain onlu valid token characters.(package private) static int
validateToken(java.lang.CharSequence token)
Validate a token contains only allowed characters.
-
-
-
Field Detail
-
CHARSET_EQUALS
private static final AsciiString CHARSET_EQUALS
-
SEMICOLON
private static final AsciiString SEMICOLON
-
COMMA_STRING
private static final java.lang.String COMMA_STRING
-
TOKEN_CHARS_HIGH
private static final long TOKEN_CHARS_HIGH
- See Also:
- Constant Field Values
-
TOKEN_CHARS_LOW
private static final long TOKEN_CHARS_LOW
- See Also:
- Constant Field Values
-
-
Method Detail
-
isOriginForm
public static boolean isOriginForm(java.net.URI uri)
Determine if a uri is in origin-form according to rfc7230, 5.3.
-
isOriginForm
public static boolean isOriginForm(java.lang.String uri)
Determine if a string uri is in origin-form according to rfc7230, 5.3.
-
isAsteriskForm
public static boolean isAsteriskForm(java.net.URI uri)
Determine if a uri is in asterisk-form according to rfc7230, 5.3.
-
isAsteriskForm
public static boolean isAsteriskForm(java.lang.String uri)
Determine if a string uri is in asterisk-form according to rfc7230, 5.3.
-
isKeepAlive
public static boolean isKeepAlive(HttpMessage message)
Returnstrue
if and only if the connection can remain open and thus 'kept alive'. This methods respects the value of the."Connection"
header first and then the return value ofHttpVersion.isKeepAliveDefault()
.
-
setKeepAlive
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 byHttpVersion.isKeepAliveDefault()
.- If the connection is kept alive by default:
- set to
"close"
ifkeepAlive
isfalse
. - remove otherwise.
- set to
- If the connection is closed by default:
- set to
"keep-alive"
ifkeepAlive
istrue
. - remove otherwise.
- set to
- If the connection is kept alive by default:
-
setKeepAlive
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 byHttpVersion.isKeepAliveDefault()
.- If the connection is kept alive by default:
- set to
"close"
ifkeepAlive
isfalse
. - remove otherwise.
- set to
- If the connection is closed by default:
- set to
"keep-alive"
ifkeepAlive
istrue
. - remove otherwise.
- set to
- If the connection is kept alive by default:
-
getContentLength
public static long getContentLength(HttpMessage message)
Returns the length of the content. Please note that this value is not retrieved fromByteBufHolder.content()
but from the"Content-Length"
header, and thus they are independent from each other.- Returns:
- the content length
- Throws:
java.lang.NumberFormatException
- if the message does not have the"Content-Length"
header or its value is not a number
-
getContentLength
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 fromByteBufHolder.content()
but from the"Content-Length"
header, and thus they are independent from each other.- Parameters:
message
- the messagedefaultValue
- the default value- Returns:
- the content length or the specified default value
- Throws:
java.lang.NumberFormatException
- if the"Content-Length"
header does not parse as a long
-
getContentLength
public static int getContentLength(HttpMessage message, int defaultValue)
Get anint
representation ofgetContentLength(HttpMessage, long)
.- Returns:
- the content length or
defaultValue
if this message does not have the"Content-Length"
header. - Throws:
java.lang.NumberFormatException
- if the"Content-Length"
header does not parse as an int
-
getWebSocketContentLength
static int getWebSocketContentLength(HttpMessage message)
Returns the content length of the specified web socket message. If the specified message is not a web socket message,-1
is returned.
-
setContentLength
public static void setContentLength(HttpMessage message, long length)
Sets the"Content-Length"
header.
-
isContentLengthSet
public static boolean isContentLengthSet(HttpMessage m)
-
is100ContinueExpected
public static boolean is100ContinueExpected(HttpMessage message)
Returnstrue
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 returnsfalse
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
- the message- Returns:
true
if and only if the expectation 100-continue is present and it is the only expectation present
-
isUnsupportedExpectation
static boolean isUnsupportedExpectation(HttpMessage message)
Returnstrue
if the specified message contains an expect header specifying an expectation that is not supported. Note that this method returnsfalse
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
- the message- Returns:
true
if and only if an expectation is present that is not supported
-
isExpectHeaderValid
private static boolean isExpectHeaderValid(HttpMessage message)
-
set100ContinueExpected
public static void set100ContinueExpected(HttpMessage message, boolean expected)
Sets or removes the"Expect: 100-continue"
header to / from the specified message. Ifexpected
istrue
, the"Expect: 100-continue"
header is set and all other previous"Expect"
headers are removed. Otherwise, all"Expect"
headers are removed completely.
-
isTransferEncodingChunked
public static boolean isTransferEncodingChunked(HttpMessage message)
Checks to see if the transfer encoding in a specifiedHttpMessage
is chunked- Parameters:
message
- The message to check- Returns:
- True if transfer encoding is chunked, otherwise false
-
setTransferEncodingChunked
public static void setTransferEncodingChunked(HttpMessage m, boolean chunked)
Set theHttpHeaderNames.TRANSFER_ENCODING
to either includeHttpHeaderValues.CHUNKED
ifchunked
istrue
, or removeHttpHeaderValues.CHUNKED
ifchunked
isfalse
.- Parameters:
m
- The message which contains the headers to modify.chunked
- iftrue
then includeHttpHeaderValues.CHUNKED
in the headers. otherwise removeHttpHeaderValues.CHUNKED
from the headers.
-
getCharset
public static java.nio.charset.Charset getCharset(HttpMessage message)
Fetch charset from message's Content-Type header.- Parameters:
message
- entity to fetch Content-Type header from- Returns:
- the charset from message's Content-Type header or
CharsetUtil.ISO_8859_1
if charset is not presented or unparsable
-
getCharset
public static java.nio.charset.Charset getCharset(java.lang.CharSequence contentTypeValue)
Fetch charset from Content-Type header value.- Parameters:
contentTypeValue
- Content-Type header value to parse- Returns:
- the charset from message's Content-Type header or
CharsetUtil.ISO_8859_1
if charset is not presented or unparsable
-
getCharset
public static java.nio.charset.Charset getCharset(HttpMessage message, java.nio.charset.Charset defaultCharset)
Fetch charset from message's Content-Type header.- Parameters:
message
- entity to fetch Content-Type header fromdefaultCharset
- result to use in case of empty, incorrect or doesn't contain required part header value- Returns:
- the charset from message's Content-Type header or
defaultCharset
if charset is not presented or unparsable
-
getCharset
public static java.nio.charset.Charset getCharset(java.lang.CharSequence contentTypeValue, java.nio.charset.Charset defaultCharset)
Fetch charset from Content-Type header value.- Parameters:
contentTypeValue
- Content-Type header value to parsedefaultCharset
- result to use in case of empty, incorrect or doesn't contain required part header value- Returns:
- the charset from message's Content-Type header or
defaultCharset
if charset is not presented or unparsable
-
getCharsetAsString
@Deprecated public static java.lang.CharSequence getCharsetAsString(HttpMessage message)
Deprecated.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
- entity to fetch Content-Type header from- Returns:
- the
CharSequence
with charset from message's Content-Type header ornull
if charset is not presented
-
getCharsetAsSequence
public static java.lang.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:
- the
CharSequence
with charset from message's Content-Type header ornull
if charset is not presented
-
getCharsetAsSequence
public static java.lang.CharSequence getCharsetAsSequence(java.lang.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
- Content-Type header value to parse- Returns:
- the
CharSequence
with charset from message's Content-Type header ornull
if charset is not presented - Throws:
java.lang.NullPointerException
- in case ifcontentTypeValue == null
-
getMimeType
public static java.lang.CharSequence getMimeType(HttpMessage message)
Fetch MIME type part from message's Content-Type header as a char sequence.- Parameters:
message
- entity to fetch Content-Type header from- Returns:
- the MIME type as a
CharSequence
from message's Content-Type header ornull
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
public static java.lang.CharSequence getMimeType(java.lang.CharSequence contentTypeValue)
Fetch MIME type part from Content-Type header value as a char sequence.- Parameters:
contentTypeValue
- Content-Type header value to parse- Returns:
- the MIME type as a
CharSequence
from message's Content-Type header ornull
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 - Throws:
java.lang.NullPointerException
- in case ifcontentTypeValue == null
-
formatHostnameForHttp
public static java.lang.String formatHostnameForHttp(java.net.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
- the address- Returns:
- the formatted String
-
normalizeAndGetContentLength
public static long normalizeAndGetContentLength(java.util.List<? extends java.lang.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
- the content-length header fields.isHttp10OrEarlier
-true
if we are handling HTTP/1.0 or earlierallowDuplicateContentLengths
-true
if multiple, identical-value content lengths should be allowed.- Returns:
- the normalized content length from the headers or
-1
if the fields were empty. - Throws:
java.lang.IllegalArgumentException
- if the content-length fields are not valid
-
validateToken
static int validateToken(java.lang.CharSequence token)
Validate a token contains only allowed characters.The token format is used for variety of HTTP components, like cookie-name, field-name of a header-field, or request method.
- Parameters:
token
- the token to validate.- Returns:
- the index of the first invalid token character found, or
-1
if there are none.
-
validateAsciiStringToken
private static int validateAsciiStringToken(AsciiString token)
Validate that anAsciiString
contain onlu valid token characters.- Parameters:
token
- the ascii string to validate.
-
validateCharSequenceToken
private static int validateCharSequenceToken(java.lang.CharSequence token)
Validate that aCharSequence
contain onlu valid token characters.- Parameters:
token
- the character sequence to validate.
-
isValidTokenChar
private static boolean isValidTokenChar(byte bit)
-
-