GeezerVison is showing netty.io — HttpUtil (Netty API Reference (4.1.141.Final))
Class HttpUtil
public final class HttpUtil
extends Object
Utility methods useful in the HTTP context.
-
Method Summary
Modifier and Type
Method
Description
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
Fetch charset from message's Content-Type header.
Fetch charset from message's Content-Type header.
Fetch charset from Content-Type header value.
Fetch charset from Content-Type header value.
Fetch charset from message's Content-Type header as a char sequence.
Fetch charset from Content-Type header value as a char sequence.
static long
Returns the length of the content.
static int
static long
Returns the length of the content or the specified default value if the message does not have the
"Content-Length" header.
Fetch MIME type part from message's Content-Type header as a char sequence.
Fetch MIME type part from Content-Type header value as a char sequence.
static boolean
Returns true if and only if the specified message contains an expect header and the only expectation
present is the 100-continue expectation.
static boolean
Determine if a string uri is in asterisk-form according to
RFC 9112, 3.2.4.
static boolean
static boolean
static boolean
Validate that the given request line token is safe for verbatim encoding to the network.
static boolean
Returns true if and only if the connection can remain open and
thus 'kept alive'.
static boolean
static boolean
static boolean
Checks to see if the transfer encoding in a specified HttpMessage is chunked
static long
Validates, and optionally extracts the content length from headers.
static void
Sets or removes the "Expect: 100-continue" header to / from the
specified message.
static void
Sets the "Content-Length" header.
static void
Sets the value of the "Connection" header depending on the
protocol version of the specified message.
static void
Sets the value of the "Connection" header depending on the
protocol version of the specified message.
static void
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Method Details
-
isOriginForm
public static boolean isOriginForm(URI uri)
-
isOriginForm
public static boolean isOriginForm(String uri)
-
isAsteriskForm
public static boolean isAsteriskForm(URI uri)
-
isAsteriskForm
public static boolean isAsteriskForm(String uri)
Determine if a string uri is in asterisk-form according to
RFC 9112, 3.2.4.
-
isEncodingSafeStartLineToken
public static boolean isEncodingSafeStartLineToken(CharSequence token)
Validate that the given request line token is safe for verbatim encoding to the network.
This does not fully check that the token – HTTP method, version, or URI – is valid and formatted correctly.
Only that the token does not contain characters that would break or
desynchronize HTTP message parsing of the start line wherein the token would be included.
See RFC 9112, 3.
- Parameters:
token - The token to check.
- Returns:
true if the token is safe to encode verbatim into the HTTP message output stream,
otherwise false.
-
isKeepAlive
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().
-
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 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
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.
-
getContentLength
public static long getContentLength(HttpMessage message)
Returns the length of the content. Please note that this value is
not retrieved from ByteBufHolder.content() but from the
"Content-Length" header, and thus they are independent from each
other.
- Returns:
- the content length
- Throws:
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 from ByteBufHolder.content() but
from the "Content-Length" header, and thus they are independent from each other.
- Parameters:
message - the message
defaultValue - the default value
- Returns:
- the content length or the specified default value
- Throws:
NumberFormatException - if the "Content-Length" header does not parse as a long
-
getContentLength
public static int getContentLength(HttpMessage message,
int defaultValue)
- Returns:
- the content length or
defaultValue if this message does
not have the "Content-Length" header.
- Throws:
NumberFormatException - if the "Content-Length" header does not parse as an int
-
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)
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 - the message
- Returns:
true if and only if the expectation 100-continue is present and it is the only expectation
present
-
set100ContinueExpected
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.
-
isTransferEncodingChunked
public static boolean isTransferEncodingChunked(HttpMessage message)
Checks to see if the transfer encoding in a specified HttpMessage is chunked
- Parameters:
message - The message to check
- Returns:
- True if transfer encoding is chunked, otherwise false
-
setTransferEncodingChunked
-
getCharset
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
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
Fetch charset from message's Content-Type header.
- Parameters:
message - entity to fetch Content-Type header from
defaultCharset - 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
Fetch charset from Content-Type header value.
- Parameters:
contentTypeValue - Content-Type header value to parse
defaultCharset - 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
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
or null if charset is not presented
-
getCharsetAsSequence
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
or null if charset is not presented
-
getCharsetAsSequence
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
or null if charset is not presented
- Throws:
NullPointerException - in case if contentTypeValue == null
-
getMimeType
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
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
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
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
- Throws:
NullPointerException - in case if contentTypeValue == null
-
formatHostnameForHttp
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(List<? extends 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 - unused
allowDuplicateContentLengths - 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:
IllegalArgumentException - if the content-length fields are not valid
|