public final class HttpUtil
extends java.lang.Object
限定符和类型 | 方法和说明 |
---|---|
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
|
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.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.lang.CharSequence |
getCharsetAsSequence(java.lang.CharSequence contentTypeValue)
Fetch charset from Content-Type header value as a char sequence.
|
static java.lang.CharSequence |
getCharsetAsSequence(HttpMessage message)
Fetch charset from message's Content-Type header as a char sequence.
|
static java.lang.CharSequence |
getCharsetAsString(HttpMessage message)
已过时。
|
static long |
getContentLength(HttpMessage message)
Returns the length of the content.
|
static int |
getContentLength(HttpMessage message,
int defaultValue)
Get an
int representation of getContentLength(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(java.lang.CharSequence contentTypeValue)
Fetch MIME type part from Content-Type header value as a char sequence.
|
static java.lang.CharSequence |
getMimeType(HttpMessage message)
Fetch MIME type part from message's Content-Type header as a char sequence.
|
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. |
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) |
static boolean |
isKeepAlive(HttpMessage message)
Returns
true 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 specified
HttpMessage is chunked |
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 the
HttpHeaderNames.TRANSFER_ENCODING to either include HttpHeaderValues.CHUNKED if
chunked is true , or remove HttpHeaderValues.CHUNKED if chunked is false . |
public static boolean isOriginForm(java.net.URI uri)
public static boolean isOriginForm(java.lang.String uri)
public static boolean isAsteriskForm(java.net.URI uri)
public static boolean isAsteriskForm(java.lang.String uri)
public static boolean isKeepAlive(HttpMessage message)
true
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 of
HttpVersion.isKeepAliveDefault()
.public static void setKeepAlive(HttpMessage message, boolean keepAlive)
"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()
.
"close"
if keepAlive
is false
."keep-alive"
if keepAlive
is true
.public static void setKeepAlive(HttpHeaders h, HttpVersion httpVersion, boolean keepAlive)
"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()
.
"close"
if keepAlive
is false
."keep-alive"
if keepAlive
is true
.public static long getContentLength(HttpMessage message)
ByteBufHolder.content()
but from the
"Content-Length"
header, and thus they are independent from each
other.java.lang.NumberFormatException
- if the message does not have the "Content-Length"
header
or its value is not a numberpublic static long getContentLength(HttpMessage message, long defaultValue)
"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.message
- the messagedefaultValue
- the default valuejava.lang.NumberFormatException
- if the "Content-Length"
header does not parse as a longpublic static int getContentLength(HttpMessage message, int defaultValue)
int
representation of getContentLength(HttpMessage, long)
.defaultValue
if this message does
not have the "Content-Length"
header.java.lang.NumberFormatException
- if the "Content-Length"
header does not parse as an intpublic static void setContentLength(HttpMessage message, long length)
"Content-Length"
header.public static boolean isContentLengthSet(HttpMessage m)
public static boolean is100ContinueExpected(HttpMessage message)
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).message
- the messagetrue
if and only if the expectation 100-continue is present and it is the only expectation
presentpublic static void set100ContinueExpected(HttpMessage message, boolean expected)
"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.public static boolean isTransferEncodingChunked(HttpMessage message)
HttpMessage
is chunkedmessage
- The message to checkpublic static void setTransferEncodingChunked(HttpMessage m, boolean chunked)
HttpHeaderNames.TRANSFER_ENCODING
to either include HttpHeaderValues.CHUNKED
if
chunked
is true
, or remove HttpHeaderValues.CHUNKED
if chunked
is false
.m
- The message which contains the headers to modify.chunked
- if true
then include HttpHeaderValues.CHUNKED
in the headers. otherwise remove
HttpHeaderValues.CHUNKED
from the headers.public static java.nio.charset.Charset getCharset(HttpMessage message)
message
- entity to fetch Content-Type header fromCharsetUtil.ISO_8859_1
if charset is not presented or unparsablepublic static java.nio.charset.Charset getCharset(java.lang.CharSequence contentTypeValue)
contentTypeValue
- Content-Type header value to parseCharsetUtil.ISO_8859_1
if charset is not presented or unparsablepublic static java.nio.charset.Charset getCharset(HttpMessage message, java.nio.charset.Charset defaultCharset)
message
- entity to fetch Content-Type header fromdefaultCharset
- result to use in case of empty, incorrect or doesn't contain required part header valuedefaultCharset
if charset is not presented or unparsablepublic static java.nio.charset.Charset getCharset(java.lang.CharSequence contentTypeValue, java.nio.charset.Charset defaultCharset)
contentTypeValue
- Content-Type header value to parsedefaultCharset
- result to use in case of empty, incorrect or doesn't contain required part header valuedefaultCharset
if charset is not presented or unparsable@Deprecated public static java.lang.CharSequence getCharsetAsString(HttpMessage message)
getCharsetAsSequence(HttpMessage)
message
- entity to fetch Content-Type header fromCharSequence
with charset from message's Content-Type header
or null
if charset is not presentedpublic static java.lang.CharSequence getCharsetAsSequence(HttpMessage message)
CharSequence
with charset from message's Content-Type header
or null
if charset is not presentedpublic static java.lang.CharSequence getCharsetAsSequence(java.lang.CharSequence contentTypeValue)
contentTypeValue
- Content-Type header value to parseCharSequence
with charset from message's Content-Type header
or null
if charset is not presentedjava.lang.NullPointerException
- in case if contentTypeValue == null
public static java.lang.CharSequence getMimeType(HttpMessage message)
message
- entity to fetch Content-Type header fromCharSequence
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 null
we be returnedpublic static java.lang.CharSequence getMimeType(java.lang.CharSequence contentTypeValue)
contentTypeValue
- Content-Type header value to parseCharSequence
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 null
we be returnedjava.lang.NullPointerException
- in case if contentTypeValue == null
public static java.lang.String formatHostnameForHttp(java.net.InetSocketAddress addr)
addr
- the address@UnstableApi public static long normalizeAndGetContentLength(java.util.List<? extends java.lang.CharSequence> contentLengthFields, boolean isHttp10OrEarlier, boolean allowDuplicateContentLengths)
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.-1
if the fields were empty.java.lang.IllegalArgumentException
- if the content-length fields are not valid