org.littleshoot.proxy
Class ProxyUtils

java.lang.Object
  extended by org.littleshoot.proxy.ProxyUtils

public class ProxyUtils
extends Object

Utilities for the proxy.


Field Summary
static String CONNECT_OK_HEADERS
          Constant for the headers for an OK response to an HTTP connect request.
static org.jboss.netty.channel.ChannelFutureListener NO_OP_LISTENER
          Utility class for a no-op ChannelFutureListener.
static HttpRequestFilter PASS_THROUGH_REQUEST_FILTER
           
static String PATTERN_RFC1036
          Date format pattern used to parse HTTP date headers in RFC 1036 format.
static String PATTERN_RFC1123
          Date format pattern used to parse HTTP date headers in RFC 1123 format.
static String PROXY_ERROR_HEADERS
          Constant for the headers for a proxy error response.
 
Method Summary
static void addVia(org.jboss.netty.handler.codec.http.HttpMessage msg)
          Adds the Via header to specify that the message has passed through the proxy.
static String cacheUri(org.jboss.netty.handler.codec.http.HttpRequest httpRequest)
          Builds the cache URI from the request, including the host and the path.
static void closeOnFlush(org.jboss.netty.channel.Channel ch)
          Closes the specified channel after all queued write requests are flushed.
static org.jboss.netty.handler.codec.http.HttpRequest copyHttpRequest(org.jboss.netty.handler.codec.http.HttpRequest original)
          Creates a copy of an original HTTP request to void modifying it.
static org.jboss.netty.handler.codec.http.HttpRequest copyHttpRequest(org.jboss.netty.handler.codec.http.HttpRequest original, boolean keepProxyFormat)
          Creates a copy of an original HTTP request to void modifying it.
static org.jboss.netty.handler.codec.http.HttpResponse copyMutableResponseFields(org.jboss.netty.handler.codec.http.HttpResponse original, org.jboss.netty.handler.codec.http.HttpResponse copy)
          Copies the mutable fields from the response original to the copy.
static Charset detectCharset(org.jboss.netty.handler.codec.http.HttpResponse http)
          Detect Charset Encoding of a HttpResponse based on Headers and Meta Tags
static boolean extractBooleanDefaultFalse(Properties props, String key)
           
static long extractLong(Properties props, String key)
           
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 String httpDate()
          Creates a Date formatted for HTTP headers for the current time.
static boolean isFalse(String val)
          Returns true if the specified string is either "false" or "off" ignoring case.
static boolean isTrue(String val)
          Returns true if the specified string is either "true" or "on" ignoring case.
static ProxyCacheManager loadCacheManager()
           
static String parseHost(org.jboss.netty.handler.codec.http.HttpRequest request)
           
static String parseHost(String request)
           
static String parseHostAndPort(org.jboss.netty.handler.codec.http.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 int parsePort(org.jboss.netty.handler.codec.http.HttpRequest httpRequest)
          Parses the port from an address.
static void printHeader(org.jboss.netty.handler.codec.http.HttpMessage msg, String name)
          Prints the specified header from the specified method.
static void printHeaders(org.jboss.netty.handler.codec.http.HttpMessage msg)
          Prints the headers of the message (for debugging).
static void stripHopByHopHeaders(org.jboss.netty.handler.codec.http.HttpMessage msg)
          Removes all headers that should not be forwarded.
static String stripHost(String uri)
          Strips the host from a URI string.
static void writeResponse(org.jboss.netty.channel.Channel channel, String statusLine, String headers)
          Writes a raw HTTP response to the channel.
static void writeResponse(org.jboss.netty.channel.Channel channel, String statusLine, String headers, String responseBody)
          Writes a raw HTTP response to the channel.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PATTERN_RFC1123

public static final String PATTERN_RFC1123
Date format pattern used to parse HTTP date headers in RFC 1123 format.

See Also:
Constant Field Values

PATTERN_RFC1036

public static final String PATTERN_RFC1036
Date format pattern used to parse HTTP date headers in RFC 1036 format.

See Also:
Constant Field Values

NO_OP_LISTENER

public static final org.jboss.netty.channel.ChannelFutureListener NO_OP_LISTENER
Utility class for a no-op ChannelFutureListener.


CONNECT_OK_HEADERS

public static final String CONNECT_OK_HEADERS
Constant for the headers for an OK response to an HTTP connect request.


PROXY_ERROR_HEADERS

public static final String PROXY_ERROR_HEADERS
Constant for the headers for a proxy error response.


PASS_THROUGH_REQUEST_FILTER

public static final HttpRequestFilter PASS_THROUGH_REQUEST_FILTER
Method Detail

stripHost

public static String stripHost(String uri)
Strips the host from a URI string. This will turn "http://host.com/path" into "/path".

Parameters:
uri - The URI to transform.
Returns:
A string with the URI stripped.

cacheUri

public static String cacheUri(org.jboss.netty.handler.codec.http.HttpRequest httpRequest)
Builds the cache URI from the request, including the host and the path.

Parameters:
httpRequest - The request.
Returns:
The cache URI.

formatDate

public static String formatDate(Date date)
Formats the given date according to the RFC 1123 pattern.

Parameters:
date - The date to format.
Returns:
An RFC 1123 formatted date string.
See Also:
PATTERN_RFC1123

formatDate

public static String formatDate(Date date,
                                String pattern)
Formats the given date according to the specified pattern. The pattern must conform to that used by the simple date format class.

Parameters:
date - The date to format.
pattern - The pattern to use for formatting the date.
Returns:
A formatted date string.
Throws:
IllegalArgumentException - If the given date pattern is invalid.
See Also:
SimpleDateFormat

httpDate

public static String httpDate()
Creates a Date formatted for HTTP headers for the current time.

Returns:
The formatted HTTP date.

copyMutableResponseFields

public static org.jboss.netty.handler.codec.http.HttpResponse copyMutableResponseFields(org.jboss.netty.handler.codec.http.HttpResponse original,
                                                                                        org.jboss.netty.handler.codec.http.HttpResponse copy)
Copies the mutable fields from the response original to the copy.

Parameters:
original - The original response to copy from.
copy - The copy.
Returns:
The copy with all mutable fields from the original.

writeResponse

public static void writeResponse(org.jboss.netty.channel.Channel channel,
                                 String statusLine,
                                 String headers)
Writes a raw HTTP response to the channel.

Parameters:
channel - The channel.
statusLine - The status line of the response.
headers - The raw headers string.

writeResponse

public static void writeResponse(org.jboss.netty.channel.Channel channel,
                                 String statusLine,
                                 String headers,
                                 String responseBody)
Writes a raw HTTP response to the channel.

Parameters:
channel - The channel.
statusLine - The status line of the response.
headers - The raw headers string.
responseBody - The response body.

printHeaders

public static void printHeaders(org.jboss.netty.handler.codec.http.HttpMessage msg)
Prints the headers of the message (for debugging).

Parameters:
msg - The HttpMessage.

printHeader

public static void printHeader(org.jboss.netty.handler.codec.http.HttpMessage msg,
                               String name)
Prints the specified header from the specified method.

Parameters:
msg - The HTTP message.
name - The name of the header to print.

closeOnFlush

public static void closeOnFlush(org.jboss.netty.channel.Channel ch)
Closes the specified channel after all queued write requests are flushed.

Parameters:
ch - The Channel to close.

parseHostAndPort

public static String parseHostAndPort(org.jboss.netty.handler.codec.http.HttpRequest httpRequest)
Parses the host and port an HTTP request is being sent to.

Parameters:
httpRequest - The request.
Returns:
The host and port string.

parseHostAndPort

public static String parseHostAndPort(String uri)
Parses the host and port an HTTP request is being sent to.

Parameters:
uri - The URI.
Returns:
The host and port string.

parseHost

public static String parseHost(org.jboss.netty.handler.codec.http.HttpRequest request)

parseHost

public static String parseHost(String request)

parsePort

public static int parsePort(org.jboss.netty.handler.codec.http.HttpRequest httpRequest)
Parses the port from an address.

Parameters:
httpRequest - The request containing the URI.
Returns:
The port. If not port is explicitly specified, returns the the default port 80 if the protocol is HTTP and 443 if the protocol is HTTPS.

copyHttpRequest

public static org.jboss.netty.handler.codec.http.HttpRequest copyHttpRequest(org.jboss.netty.handler.codec.http.HttpRequest original,
                                                                             boolean keepProxyFormat)
Creates a copy of an original HTTP request to void modifying it.

Parameters:
original - The original request.
keepProxyFormat - keep proxy-formatted URI (used in chaining)
Returns:
The request copy.

stripHopByHopHeaders

public static void stripHopByHopHeaders(org.jboss.netty.handler.codec.http.HttpMessage msg)
Removes all headers that should not be forwarded. See RFC 2616 13.5.1 End-to-end and Hop-by-hop Headers.

Parameters:
msg - The message to strip headers from.

copyHttpRequest

public static org.jboss.netty.handler.codec.http.HttpRequest copyHttpRequest(org.jboss.netty.handler.codec.http.HttpRequest original)
Creates a copy of an original HTTP request to void modifying it. This variant will unconditionally strip the proxy-formatted request.

Parameters:
original - The original request.
Returns:
The request copy.

addVia

public static void addVia(org.jboss.netty.handler.codec.http.HttpMessage msg)
Adds the Via header to specify that the message has passed through the proxy.

Parameters:
msg - The HTTP message.

detectCharset

public static Charset detectCharset(org.jboss.netty.handler.codec.http.HttpResponse http)
Detect Charset Encoding of a HttpResponse based on Headers and Meta Tags

Parameters:
http - The HTTP Response.
Returns:
Returns the detected charset.

isTrue

public static boolean isTrue(String val)
Returns true if the specified string is either "true" or "on" ignoring case.

Parameters:
val - The string in question.
Returns:
true if the specified string is either "true" or "on" ignoring case, otherwise false.

isFalse

public static boolean isFalse(String val)
Returns true if the specified string is either "false" or "off" ignoring case.

Parameters:
val - The string in question.
Returns:
true if the specified string is either "false" or "off" ignoring case, otherwise false.

extractBooleanDefaultFalse

public static boolean extractBooleanDefaultFalse(Properties props,
                                                 String key)

extractLong

public static long extractLong(Properties props,
                               String key)

loadCacheManager

public static ProxyCacheManager loadCacheManager()


Copyright © 2009-2013 LittleShoot. All Rights Reserved.