public class ProxyUtils extends Object
Modifier and Type | Field and Description |
---|---|
private static com.google.common.base.Splitter |
COMMA_SEPARATED_HEADER_VALUE_SPLITTER
Splits comma-separated header values (such as Connection) into their individual tokens.
|
private static TimeZone |
GMT |
private static Pattern |
HTTP_PREFIX |
private static org.slf4j.Logger |
LOG |
private static String |
PATTERN_RFC1123
Date format pattern used to parse HTTP date headers in RFC 1123 format.
|
private static Set<String> |
SHOULD_NOT_PROXY_HOP_BY_HOP_HEADERS
Hop-by-hop headers that should be removed when proxying, as defined by the HTTP 1.1 spec, section 13.5.1
(http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1).
|
Constructor and Description |
---|
ProxyUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
addVia(HttpMessage httpMessage,
String alias)
Adds the Via header to specify that the message has passed through the proxy.
|
private static boolean |
checkTrueOrFalse(String val,
String str1,
String str2) |
static HttpResponse |
copyMutableResponseFields(HttpResponse original)
Make a copy of the response including all mutable fields.
|
static FullHttpResponse |
createFullHttpResponse(HttpVersion httpVersion,
HttpResponseStatus status)
Creates a new
FullHttpResponse with no body content |
static FullHttpResponse |
createFullHttpResponse(HttpVersion httpVersion,
HttpResponseStatus status,
String body)
Creates a new
FullHttpResponse with the specified String as the body contents (encoded using UTF-8). |
static FullHttpResponse |
createFullHttpResponse(HttpVersion httpVersion,
HttpResponseStatus status,
String contentType,
ByteBuf body,
int contentLength)
Creates a new
FullHttpResponse with the specified body. |
static HttpResponse |
duplicateHttpResponse(HttpResponse originalResponse)
Duplicates the status line and headers of an HttpResponse object.
|
static boolean |
extractBooleanDefaultFalse(Properties props,
String key) |
static boolean |
extractBooleanDefaultTrue(Properties props,
String key) |
static int |
extractInt(Properties props,
String key) |
static int |
extractInt(Properties props,
String key,
int defaultValue) |
static String |
formatDate(Date date)
Formats the given date according to the RFC 1123 pattern.
|
static String |
formatDate(Date date,
String pattern)
Formats the given date according to the specified pattern.
|
static List<String> |
getAllCommaSeparatedHeaderValues(String headerName,
HttpMessage httpMessage)
Retrieves all comma-separated values for headers with the specified name on the HttpMessage.
|
static String |
getHostName()
Attempts to resolve the local machine's hostname.
|
static boolean |
isChunked(HttpObject httpObject)
If an HttpObject is not the last chunk, then that means there are other
chunks that will follow.
|
static boolean |
isCONNECT(HttpObject httpObject) |
static boolean |
isContentAlwaysEmpty(HttpMessage msg)
Returns true if the HTTP message cannot contain an entity body, according to the HTTP spec.
|
static boolean |
isFalse(String val)
Returns
true if the specified string is either "false" or
"off" ignoring case. |
static boolean |
isHEAD(HttpRequest httpRequest)
Returns true if the specified HttpRequest is a HEAD request.
|
static boolean |
isLastChunk(HttpObject httpObject)
If an HttpObject implements the market interface LastHttpContent, it
represents the last chunk of a transfer.
|
static boolean |
isResponseSelfTerminating(HttpResponse response)
Returns true if the HTTP response from the server is expected to indicate its own message length/end-of-message.
|
static boolean |
isTrue(String val)
Returns
true if the specified string is either "true" or
"on" ignoring case. |
static boolean |
isUdtAvailable()
Determines if UDT is available on the classpath.
|
static String |
parseHostAndPort(HttpRequest httpRequest)
Parses the host and port an HTTP request is being sent to.
|
static String |
parseHostAndPort(String uri)
Parses the host and port an HTTP request is being sent to.
|
static void |
removeSdchEncoding(HttpHeaders headers)
Given an HttpHeaders instance, removes 'sdch' from the 'Accept-Encoding'
header list (if it exists) and returns the modified instance.
|
static boolean |
shouldRemoveHopByHopHeader(String headerName)
Determines if the specified header should be removed from the proxied response because it is a hop-by-hop header, as defined by the
HTTP 1.1 spec in section 13.5.1.
|
static List<String> |
splitCommaSeparatedHeaderValues(String headerValue)
Splits comma-separated header values into tokens.
|
static String |
stripHost(String uri)
Strips the host from a URI string.
|
private static final Set<String> SHOULD_NOT_PROXY_HOP_BY_HOP_HEADERS
shouldRemoveHopByHopHeader(String)
.
Header names are stored as lowercase to make case-insensitive comparisons easier.private static final org.slf4j.Logger LOG
private static final TimeZone GMT
private static final com.google.common.base.Splitter COMMA_SEPARATED_HEADER_VALUE_SPLITTER
private static final String PATTERN_RFC1123
private static Pattern HTTP_PREFIX
public static String stripHost(String uri)
uri
- The URI to transform.public static String formatDate(Date date)
date
- The date to format.PATTERN_RFC1123
public static String formatDate(Date date, String pattern)
simple date
format
class.date
- The date to format.pattern
- The pattern to use for formatting the date.IllegalArgumentException
- If the given date pattern is invalid.SimpleDateFormat
public static boolean isLastChunk(HttpObject httpObject)
httpObject
- LastHttpContent
public static boolean isChunked(HttpObject httpObject)
httpObject
- FullHttpMessage
public static String parseHostAndPort(HttpRequest httpRequest)
httpRequest
- The request.public static String parseHostAndPort(String uri)
uri
- The URI.public static HttpResponse copyMutableResponseFields(HttpResponse original)
original
- The original response to copy from.public static void addVia(HttpMessage httpMessage, String alias)
The received-by portion of the field value is normally the host and optional port number of a recipient server or client that subsequently forwarded the message. However, if the real host is considered to be sensitive information, a sender MAY replace it with a pseudonym.
httpMessage
- HTTP message to add the Via header toalias
- the alias to provide in the Via header for this proxypublic static boolean isTrue(String val)
true
if the specified string is either "true" or
"on" ignoring case.val
- The string in question.true
if the specified string is either "true" or
"on" ignoring case, otherwise false
.public static boolean isFalse(String val)
true
if the specified string is either "false" or
"off" ignoring case.val
- The string in question.true
if the specified string is either "false" or
"off" ignoring case, otherwise false
.public static boolean extractBooleanDefaultFalse(Properties props, String key)
public static boolean extractBooleanDefaultTrue(Properties props, String key)
public static int extractInt(Properties props, String key)
public static int extractInt(Properties props, String key, int defaultValue)
public static boolean isCONNECT(HttpObject httpObject)
public static boolean isHEAD(HttpRequest httpRequest)
httpRequest
- http requestpublic static boolean isContentAlwaysEmpty(HttpMessage msg)
HttpObjectDecoder.isContentAlwaysEmpty(HttpMessage)
.msg
- HTTP messagepublic static boolean isResponseSelfTerminating(HttpResponse response)
This method is based on the allowed message length indicators in the HTTP specification, section 4.4:
4.4 Message Length The transfer-length of a message is the length of the message-body as it appears in the message; that is, after any transfer-codings have been applied. When a message-body is included with a message, the transfer-length of that body is determined by one of the following (in order of precedence): 1.Any response message which "MUST NOT" include a message-body (such as the 1xx, 204, and 304 responses and any response to a HEAD request) is always terminated by the first empty line after the header fields, regardless of the entity-header fields present in the message. 2.If a Transfer-Encoding header field (section 14.41) is present and has any value other than "identity", then the transfer-length is defined by use of the "chunked" transfer-coding (section 3.6), unless the message is terminated by closing the connection. 3.If a Content-Length header field (section 14.13) is present, its decimal value in OCTETs represents both the entity-length and the transfer-length. The Content-Length header field MUST NOT be sent if these two lengths are different (i.e., if a Transfer-Encoding header field is present). If a message is received with both a Transfer-Encoding header field and a Content-Length header field, the latter MUST be ignored. [LP note: multipart/byteranges support has been removed from the HTTP 1.1 spec by RFC 7230, section A.2. Since it is seldom used, LittleProxy does not check for it.] 5.By the server closing the connection. (Closing the connection cannot be used to indicate the end of a request body, since that would leave no possibility for the server to send back a response.)The rules for Transfer-Encoding are clarified in RFC 7230, section 3.3.1 and 3.3.3 (3):
If any transfer coding other than chunked is applied to a response payload body, the sender MUST either apply chunked as the final transfer coding or terminate the message by closing the connection.
response
- the HTTP response objectpublic static List<String> getAllCommaSeparatedHeaderValues(String headerName, HttpMessage httpMessage)
Transfer-Encoding: gzip,deflate Transfer-Encoding: chunkedThis method will return a list of three values: "gzip", "deflate", "chunked".
Placing values on multiple header lines is allowed under certain circumstances in RFC 2616 section 4.2, and in RFC 7230 section 3.2.2 quoted here:
A sender MUST NOT generate multiple header fields with the same field name in a message unless either the entire field value for that header field is defined as a comma-separated list [i.e., #(values)] or the header field is a well-known exception (as noted below). A recipient MAY combine multiple header fields with the same field name into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field value to the combined field value in order, separated by a comma. The order in which header fields with the same field name are received is therefore significant to the interpretation of the combined field value; a proxy MUST NOT change the order of these field values when forwarding a message.
headerName
- the name of the header for which values will be retrievedhttpMessage
- the HTTP message whose header values will be retrievedpublic static HttpResponse duplicateHttpResponse(HttpResponse originalResponse)
originalResponse
- HttpResponse to be duplicatedpublic static String getHostName()
public static boolean shouldRemoveHopByHopHeader(String headerName)
The following HTTP/1.1 headers are hop-by-hop headers: - Connection - Keep-Alive - Proxy-Authenticate - Proxy-Authorization - TE - Trailers [LittleProxy note: actual header name is Trailer] - Transfer-Encoding [LittleProxy note: this header is not normally removed when proxying, since the proxy does not re-chunk responses. The exception is when an HttpObjectAggregator is enabled, which aggregates chunked content and removes the 'Transfer-Encoding: chunked' header itself.] - Upgrade All other headers defined by HTTP/1.1 are end-to-end headers.
headerName
- the header namepublic static List<String> splitCommaSeparatedHeaderValues(String headerValue)
getAllCommaSeparatedHeaderValues(String, HttpMessage)
, this method only operates on
a single header value, rather than all instances of the header in a message.headerValue
- the un-tokenized header value (must not be null)public static boolean isUdtAvailable()
public static FullHttpResponse createFullHttpResponse(HttpVersion httpVersion, HttpResponseStatus status, String body)
FullHttpResponse
with the specified String as the body contents (encoded using UTF-8).httpVersion
- HTTP version of the responsestatus
- HTTP status codebody
- body to include in the FullHttpResponse; will be UTF-8 encodedpublic static FullHttpResponse createFullHttpResponse(HttpVersion httpVersion, HttpResponseStatus status)
FullHttpResponse
with no body contenthttpVersion
- HTTP version of the responsestatus
- HTTP status codepublic static FullHttpResponse createFullHttpResponse(HttpVersion httpVersion, HttpResponseStatus status, String contentType, ByteBuf body, int contentLength)
FullHttpResponse
with the specified body.httpVersion
- HTTP version of the responsestatus
- HTTP status codecontentType
- the Content-Type of the bodybody
- body to include in the FullHttpResponse; if nullcontentLength
- number of bytes to send in the Content-Length header; should equal the number of bytes in the ByteBufpublic static void removeSdchEncoding(HttpHeaders headers)
headers
- The headers to modify.Copyright © 2009–2017 LittleShoot. All rights reserved.