anon.proxy
Class HTTPProxyCallback

java.lang.Object
  extended by anon.proxy.HTTPProxyCallback
All Implemented Interfaces:
ProxyCallback

public class HTTPProxyCallback
extends java.lang.Object
implements ProxyCallback

Provides functionality for parsing and storing the headers of a HTTP-Connection. Invoked by the ProxyCallbackHandler framework. HTTPConnectionListeners can be registered to examine and modify the corresponding HTTP messages before they are transmitted further. Also Listeners can be notified about how much payload was transmitted via the corresponding HTTPConnection.

Author:
Simon Pecher

Nested Class Summary
 class HTTPProxyCallback.HTTPConnectionHeader
           
private static interface HTTPProxyCallback.IHTTPHelper
           
 
Field Summary
(package private) static java.lang.String CRLF
           
private static HTTPProxyCallback.IHTTPHelper DOWNSTREAM_HELPER
           
private static boolean FIRE_EVENT
           
static java.lang.String HTTP_ACCEPT
           
static java.lang.String HTTP_ACCEPT_CHARSET
           
static java.lang.String HTTP_ACCEPT_ENCODING
           
static java.lang.String HTTP_ACCEPT_LANGUAGE
           
static java.lang.String HTTP_CACHE_CONTROL
           
static java.lang.String HTTP_CONNECTION
           
static java.lang.String HTTP_CONTENT_ENCODING
           
static java.lang.String HTTP_CONTENT_LENGTH
           
(package private) static java.lang.String HTTP_HEADER_DELIM
           
(package private) static java.lang.String HTTP_HEADER_END
           
(package private) static byte[] HTTP_HEADER_END_BYTES
           
static java.lang.String HTTP_HOST
           
static java.lang.String HTTP_IE_UA_CPU
           
static java.lang.String HTTP_KEEP_ALIVE
           
static java.lang.String HTTP_PRAGMA
           
static java.lang.String HTTP_PROXY_CONNECTION
           
static java.lang.String HTTP_RANGE
           
static java.lang.String HTTP_REFERER
           
(package private) static java.lang.String[] HTTP_REQUEST_METHODS
           
(package private) static byte[][] HTTP_REQUEST_METHODS_BYTES
           
(package private) static java.lang.String HTTP_START_LINE_KEY
           
static java.lang.String HTTP_USER_AGENT
           
(package private) static java.lang.String HTTP_VERSION_PREFIX
           
(package private) static byte[] HTTP_VERSION_PREFIX_BYTES
           
private  java.util.Hashtable m_connectionHTTPHeaders
          Container for the Headers of a whole HTTP Connection including Request and Response.
private  java.util.Hashtable m_downstreamBytes
           
private  java.util.Vector m_httpConnectionListeners
           
private  java.util.Hashtable m_unfinishedRequests
          request messages whose parsing hasn't finished yet
private  java.util.Hashtable m_unfinishedResponses
          response messages whose parsing hasn't finished yet
private  java.util.Hashtable m_upstreamBytes
           
(package private) static int MESSAGE_TYPE_REQUEST
           
(package private) static int MESSAGE_TYPE_RESPONSE
           
(package private) static java.lang.String MSG_INVALID_LINETERM_REQUEST
           
(package private) static java.lang.String MSG_INVALID_LINETERM_RESPONSE
           
private static HTTPProxyCallback.IHTTPHelper UPSTREAM_HELPER
           
 
Fields inherited from interface anon.proxy.ProxyCallback
STATUS_DELAY, STATUS_FINISHED, STATUS_PROCESSABLE
 
Constructor Summary
HTTPProxyCallback()
           
 
Method Summary
 void addHTTPConnectionListener(AbstractHTTPConnectionListener listener)
           
static boolean checkValidity(java.lang.String headerData)
          checks whether the HTTP message headers fragment specified by headerData contains invalid line terminations, i.e.
 void closeRequest(AnonProxyRequest anonRequest)
           
private  long countContentBytes(AnonProxyRequest anonRequest, int contentBytes, java.util.Hashtable contentBytesCount, boolean fire)
           
private  boolean extractHeaderParts(AnonProxyRequest anonRequest, java.lang.String chunkData, int messageType)
           
 void fireDownstreamContentBytesReceived(HTTPConnectionEvent event)
           
 void fireRequestHeadersReceived(HTTPConnectionEvent event)
           
 void fireResponseHeadersReceived(HTTPConnectionEvent event)
           
 void fireUpstreamContentBytesReceived(HTTPConnectionEvent event)
           
private  long getContentBytes(AnonProxyRequest anonRequest, java.util.Hashtable contentByteCount)
           
 long getDownStreamContentBytes(AnonProxyRequest anonRequest)
           
private  HTTPConnectionEvent getEvent(AnonProxyRequest anonRequest)
           
 long getUpStreamContentBytes(AnonProxyRequest anonRequest)
           
 int handleDownstreamChunk(AnonProxyRequest anonRequest, ProxyCallbackBuffer buffer)
           
private  int handleStreamChunk(AnonProxyRequest anonRequest, ProxyCallbackBuffer buffer, int a_messageType, HTTPProxyCallback.IHTTPHelper a_helper)
           
 int handleUpstreamChunk(AnonProxyRequest anonRequest, ProxyCallbackBuffer buffer)
           
private  boolean hasAlignedHTTPStartLine(byte[] chunk, int off, int len, int messageType)
           
private  boolean hasAlignedHTTPStartLine(java.lang.String chunkData, int messageType)
           
static int indexOfHTTPHeaderEnd(byte[] prefix, byte[] chunk, int chunkStartIndex, int chunkEndIndex)
          Returns the index of the first char after the first occurence of the CRLFCRLF termination in the array 'chunk' or -1 if such a termination sequence was not found.
static int indexOfHTTPHeaderEnd(byte[] chunk, int startIndex, int endIndex)
          Returns the index of the first char after the first occurence of the CRLFCRLF termination in the array 'chunk' or -1 if such a termination sequence was not found.
private  boolean isRequest(byte[] chunk, int off, int len)
          this method makes a bytewise comparison so we don't have to turn the whole chunk into a string for header detection.
private  boolean isRequest(java.lang.String chunkData)
           
private  boolean isResponse(byte[] chunk, int off, int len)
           
private  boolean isResponse(java.lang.String chunkData)
           
private  void parseHTTPHeader(java.lang.String headerData, HTTPProxyCallback.HTTPConnectionHeader connHeader, int headerType)
           
 void removeAllHTTPConnectionListeners()
           
 void removeHTTPConnectionListener(AbstractHTTPConnectionListener listener)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FIRE_EVENT

private static final boolean FIRE_EVENT
See Also:
Constant Field Values

MESSAGE_TYPE_REQUEST

static final int MESSAGE_TYPE_REQUEST
See Also:
Constant Field Values

MESSAGE_TYPE_RESPONSE

static final int MESSAGE_TYPE_RESPONSE
See Also:
Constant Field Values

CRLF

static final java.lang.String CRLF
See Also:
Constant Field Values

HTTP_HEADER_END

static final java.lang.String HTTP_HEADER_END
See Also:
Constant Field Values

HTTP_HEADER_END_BYTES

static final byte[] HTTP_HEADER_END_BYTES

HTTP_HEADER_DELIM

static final java.lang.String HTTP_HEADER_DELIM
See Also:
Constant Field Values

HTTP_START_LINE_KEY

static final java.lang.String HTTP_START_LINE_KEY
See Also:
Constant Field Values

HTTP_VERSION_PREFIX

static final java.lang.String HTTP_VERSION_PREFIX
See Also:
Constant Field Values

HTTP_VERSION_PREFIX_BYTES

static final byte[] HTTP_VERSION_PREFIX_BYTES

HTTP_REQUEST_METHODS

static final java.lang.String[] HTTP_REQUEST_METHODS

HTTP_REQUEST_METHODS_BYTES

static final byte[][] HTTP_REQUEST_METHODS_BYTES

MSG_INVALID_LINETERM_REQUEST

static final java.lang.String MSG_INVALID_LINETERM_REQUEST
See Also:
Constant Field Values

MSG_INVALID_LINETERM_RESPONSE

static final java.lang.String MSG_INVALID_LINETERM_RESPONSE
See Also:
Constant Field Values

HTTP_CONTENT_LENGTH

public static final java.lang.String HTTP_CONTENT_LENGTH
See Also:
Constant Field Values

HTTP_CONTENT_ENCODING

public static final java.lang.String HTTP_CONTENT_ENCODING
See Also:
Constant Field Values

HTTP_HOST

public static final java.lang.String HTTP_HOST
See Also:
Constant Field Values

HTTP_USER_AGENT

public static final java.lang.String HTTP_USER_AGENT
See Also:
Constant Field Values

HTTP_ACCEPT

public static final java.lang.String HTTP_ACCEPT
See Also:
Constant Field Values

HTTP_ACCEPT_LANGUAGE

public static final java.lang.String HTTP_ACCEPT_LANGUAGE
See Also:
Constant Field Values

HTTP_ACCEPT_ENCODING

public static final java.lang.String HTTP_ACCEPT_ENCODING
See Also:
Constant Field Values

HTTP_ACCEPT_CHARSET

public static final java.lang.String HTTP_ACCEPT_CHARSET
See Also:
Constant Field Values

HTTP_KEEP_ALIVE

public static final java.lang.String HTTP_KEEP_ALIVE
See Also:
Constant Field Values

HTTP_PROXY_CONNECTION

public static final java.lang.String HTTP_PROXY_CONNECTION
See Also:
Constant Field Values

HTTP_CONNECTION

public static final java.lang.String HTTP_CONNECTION
See Also:
Constant Field Values

HTTP_REFERER

public static final java.lang.String HTTP_REFERER
See Also:
Constant Field Values

HTTP_CACHE_CONTROL

public static final java.lang.String HTTP_CACHE_CONTROL
See Also:
Constant Field Values

HTTP_PRAGMA

public static final java.lang.String HTTP_PRAGMA
See Also:
Constant Field Values

HTTP_RANGE

public static final java.lang.String HTTP_RANGE
See Also:
Constant Field Values

HTTP_IE_UA_CPU

public static final java.lang.String HTTP_IE_UA_CPU
See Also:
Constant Field Values

m_connectionHTTPHeaders

private java.util.Hashtable m_connectionHTTPHeaders
Container for the Headers of a whole HTTP Connection including Request and Response.


m_unfinishedRequests

private java.util.Hashtable m_unfinishedRequests
request messages whose parsing hasn't finished yet


m_unfinishedResponses

private java.util.Hashtable m_unfinishedResponses
response messages whose parsing hasn't finished yet


m_downstreamBytes

private java.util.Hashtable m_downstreamBytes

m_upstreamBytes

private java.util.Hashtable m_upstreamBytes

m_httpConnectionListeners

private java.util.Vector m_httpConnectionListeners

UPSTREAM_HELPER

private static final HTTPProxyCallback.IHTTPHelper UPSTREAM_HELPER

DOWNSTREAM_HELPER

private static final HTTPProxyCallback.IHTTPHelper DOWNSTREAM_HELPER
Constructor Detail

HTTPProxyCallback

public HTTPProxyCallback()
Method Detail

handleUpstreamChunk

public int handleUpstreamChunk(AnonProxyRequest anonRequest,
                               ProxyCallbackBuffer buffer)
                        throws ProxyCallbackNotProcessableException
Specified by:
handleUpstreamChunk in interface ProxyCallback
Throws:
ProxyCallbackNotProcessableException

handleDownstreamChunk

public int handleDownstreamChunk(AnonProxyRequest anonRequest,
                                 ProxyCallbackBuffer buffer)
                          throws ProxyCallbackNotProcessableException
Specified by:
handleDownstreamChunk in interface ProxyCallback
Throws:
ProxyCallbackNotProcessableException

handleStreamChunk

private int handleStreamChunk(AnonProxyRequest anonRequest,
                              ProxyCallbackBuffer buffer,
                              int a_messageType,
                              HTTPProxyCallback.IHTTPHelper a_helper)
                       throws ProxyCallbackNotProcessableException
Throws:
ProxyCallbackNotProcessableException

countContentBytes

private long countContentBytes(AnonProxyRequest anonRequest,
                               int contentBytes,
                               java.util.Hashtable contentBytesCount,
                               boolean fire)

getUpStreamContentBytes

public long getUpStreamContentBytes(AnonProxyRequest anonRequest)

getDownStreamContentBytes

public long getDownStreamContentBytes(AnonProxyRequest anonRequest)

getContentBytes

private long getContentBytes(AnonProxyRequest anonRequest,
                             java.util.Hashtable contentByteCount)

getEvent

private HTTPConnectionEvent getEvent(AnonProxyRequest anonRequest)

extractHeaderParts

private boolean extractHeaderParts(AnonProxyRequest anonRequest,
                                   java.lang.String chunkData,
                                   int messageType)
                            throws ProxyCallbackNotProcessableException
Throws:
ProxyCallbackNotProcessableException

checkValidity

public static boolean checkValidity(java.lang.String headerData)
checks whether the HTTP message headers fragment specified by headerData contains invalid line terminations, i.e. all terminations other than CRLF

Parameters:
headerData - the fragment of the HTTP headers of a HTTP message
Returns:
false if the fragment contains invalid line terminations, true otherwise.

hasAlignedHTTPStartLine

private boolean hasAlignedHTTPStartLine(java.lang.String chunkData,
                                        int messageType)

hasAlignedHTTPStartLine

private boolean hasAlignedHTTPStartLine(byte[] chunk,
                                        int off,
                                        int len,
                                        int messageType)

isRequest

private boolean isRequest(java.lang.String chunkData)

isRequest

private boolean isRequest(byte[] chunk,
                          int off,
                          int len)
this method makes a bytewise comparison so we don't have to turn the whole chunk into a string for header detection.

Parameters:
chunk -
len -
Returns:

isResponse

private boolean isResponse(java.lang.String chunkData)

isResponse

private boolean isResponse(byte[] chunk,
                           int off,
                           int len)

indexOfHTTPHeaderEnd

public static int indexOfHTTPHeaderEnd(byte[] chunk,
                                       int startIndex,
                                       int endIndex)
Returns the index of the first char after the first occurence of the CRLFCRLF termination in the array 'chunk' or -1 if such a termination sequence was not found.


indexOfHTTPHeaderEnd

public static int indexOfHTTPHeaderEnd(byte[] prefix,
                                       byte[] chunk,
                                       int chunkStartIndex,
                                       int chunkEndIndex)
Returns the index of the first char after the first occurence of the CRLFCRLF termination in the array 'chunk' or -1 if such a termination sequence was not found. Also considers that some of the line termination characters are stored at the end of the array 'prefix' which are followed by the corresponding characters in 'chunk' to complete the CRLFCRLF sequence. The index returned refers to the array 'chunk'.


parseHTTPHeader

private void parseHTTPHeader(java.lang.String headerData,
                             HTTPProxyCallback.HTTPConnectionHeader connHeader,
                             int headerType)

addHTTPConnectionListener

public void addHTTPConnectionListener(AbstractHTTPConnectionListener listener)

removeHTTPConnectionListener

public void removeHTTPConnectionListener(AbstractHTTPConnectionListener listener)

removeAllHTTPConnectionListeners

public void removeAllHTTPConnectionListeners()

fireRequestHeadersReceived

public void fireRequestHeadersReceived(HTTPConnectionEvent event)

fireResponseHeadersReceived

public void fireResponseHeadersReceived(HTTPConnectionEvent event)

fireDownstreamContentBytesReceived

public void fireDownstreamContentBytesReceived(HTTPConnectionEvent event)

fireUpstreamContentBytesReceived

public void fireUpstreamContentBytesReceived(HTTPConnectionEvent event)

closeRequest

public void closeRequest(AnonProxyRequest anonRequest)
Specified by:
closeRequest in interface ProxyCallback