rabbit.proxy
Class Connection

java.lang.Object
  extended byjava.lang.Thread
      extended byrabbit.util.RestartableThread
          extended byrabbit.proxy.Connection
All Implemented Interfaces:
java.lang.Runnable

public class Connection
extends RestartableThread

This is the class that handles one connection And make sure the content is delivered to the client. It reads the request and get the data (from the cache or the web), and filters it and sends the data to the client. If Keepalive is suitable it reads the next request.


Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Connection()
          Create a new Connection.
Connection(java.net.Socket socket)
          Create a new Connection for a given socket.
 
Method Summary
 HTTPHeader checkIfMatch(HTTPHeader header, NCacheEntry entry)
           
 boolean checkIPAccess(java.net.Socket socket)
          Check if this socket is in the possible range of ip:s being served.
 void doError(int statuscode, java.lang.Exception e)
          Send an error (400 Bad Request) to the client.
 void doError(int statuscode, java.lang.String message)
          Send an error (400 Bad Request) to the client.
 void doWork()
          Handle the incomming requests for as long as we can keep the connection alive.
 HTTPHeader filterHTTP(java.util.Vector filters, HTTPHeader in)
          Filter the headers using the methods in the vector.
 HTTPHeader get200()
          Get a 200 Ok header
 HTTPHeader get304(HTTPHeader oldresp)
          Get a 304 Not Modified header for the given old header
 HTTPHeader get400(java.lang.Exception exception)
          Get a 400 Bad Request header for the given exception.
 HTTPHeader get403()
          Get a 403 Forbidden header.
 HTTPHeader get407(java.lang.String realm, java.net.URL url)
          Get a 407 Proxy Authentication Required for the given realm and url.
 HTTPHeader get412()
          Get a 412 Precondition Failed header.
 HTTPHeader get417(java.lang.String expectation)
          Get a 417 Expectation Failed header.
 HTTPHeader get500(java.lang.Throwable exception)
          Get a 500 Internal Server Error header for the given exception.
 HTTPHeader get504(java.lang.Throwable exception)
          Get a 500 Internal Server Error header for the given exception.
 boolean getChunking()
          Get the chunking option.
 java.lang.String getContentLength()
          Get the content length of the response.
 java.lang.String getExtraInfo()
          Get the extra information for this request.
static HTTPHeader getHeader()
          Get a new HTTPHeader.
static HTTPHeader getHeader(java.lang.String statusLine)
          Get a new HTTPHeader initialized with some data.
 HTTPHeader getHttpHeader()
           
 boolean getKeepalive()
          Get the keepalive value.
 boolean getMayCache()
          Get the state of this request.
 boolean getMayFilter()
          Get the state of the request.
 boolean getMayUseCache()
          Get the state of this request.
 boolean getMeta()
          Get the state of this request.
 java.lang.String getPassWord()
          Get the password of the client.
 java.lang.String getRequestLine()
          Get the current request line
 java.net.Socket getSocket()
          Get the Socket that is being served.
 java.util.Date getStarted()
          Get the time this Connection was started.
 java.lang.String getStatus()
          Get the status of this Connection
 java.lang.String getStatusCode()
          Get the status code of the request.
 java.lang.String getUserName()
          Get the username of the client.
 void handleMeta(HTTPHeader header)
          Handle a meta page.
 void handleRequest(HTTPHeader header)
          Handle a request by getting the datastream (from the cache or the web).
 HTTPHeader is304(HTTPHeader in, NCacheEntry entry)
          Check if the request allows us to use a "304 Not modified" response.
 boolean isSSLRequest(HTTPHeader header)
          Check to see if this header is an SSL header.
 void setChunking(boolean b)
          Set the chunking option.
 void setContentLength(java.lang.String cl)
          Set the content length of the response.
 void setExtraInfo(java.lang.String exinfo)
          Set the extra information for this request.
 void setKeepalive(boolean keepalive)
          Set keepalive to a new value.
 void setMayCache(boolean cacheAllowed)
          Set the state of this request.
 void setMayFilter(boolean filterAllowed)
          Get the state of this request.
 void setMayUseCache(boolean usecache)
          Set the state of this request.
 void setMeta(boolean meta)
          Set the state of this request.
 void setPassWord(java.lang.String password)
          Set the password of the client.
 void setSocket(java.net.Socket socket)
          Give this connection a socket to handle
 void setUserName(java.lang.String username)
          Set the user name of the client.
 boolean sslIsAllowed(HTTPHeader header)
          Are we allowed to proxy ssl-type connections ?
 
Methods inherited from class rabbit.util.RestartableThread
run, start
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Connection

public Connection()
Create a new Connection. You need to call setsocket on this connection to make it do stuff.


Connection

public Connection(java.net.Socket socket)
Create a new Connection for a given socket.

Parameters:
socket - the Socket that is requesting service.
Method Detail

setSocket

public void setSocket(java.net.Socket socket)
Give this connection a socket to handle

Parameters:
socket - the Socket that is requesting service.

checkIPAccess

public boolean checkIPAccess(java.net.Socket socket)
Check if this socket is in the possible range of ip:s being served.

Parameters:
socket - the Socket to check.
Returns:
true if the socket is allowed, false if it should be rejected.

doWork

public void doWork()
Handle the incomming requests for as long as we can keep the connection alive.

Specified by:
doWork in class RestartableThread

filterHTTP

public HTTPHeader filterHTTP(java.util.Vector filters,
                             HTTPHeader in)
Filter the headers using the methods in the vector.

Parameters:
filters - a Vector with Methods.
in - the request or response header.
Returns:
null if all is ok, a HTTPHeader if this request is blocked.

checkIfMatch

public HTTPHeader checkIfMatch(HTTPHeader header,
                               NCacheEntry entry)

is304

public HTTPHeader is304(HTTPHeader in,
                        NCacheEntry entry)
Check if the request allows us to use a "304 Not modified" response.

Parameters:
in - the request being made.
entry - the cache entry.

handleRequest

public void handleRequest(HTTPHeader header)
Handle a request by getting the datastream (from the cache or the web). After getting the handler for the mimetype, send it.

Parameters:
header - the request made.

handleMeta

public void handleMeta(HTTPHeader header)
Handle a meta page.

Parameters:
header - the request being made.

isSSLRequest

public boolean isSSLRequest(HTTPHeader header)
Check to see if this header is an SSL header.

Parameters:
header - the header to check.
Returns:
true if this header is an SSL request.

sslIsAllowed

public boolean sslIsAllowed(HTTPHeader header)
Are we allowed to proxy ssl-type connections ?

Returns:
true if we allow the CONNECT <port> command.

doError

public void doError(int statuscode,
                    java.lang.String message)
Send an error (400 Bad Request) to the client.

Parameters:
statuscode - the status code of the error.
message - the error message to tell the client.

doError

public void doError(int statuscode,
                    java.lang.Exception e)
Send an error (400 Bad Request) to the client.

Parameters:
statuscode - the status code of the error.

getStatus

public java.lang.String getStatus()
Get the status of this Connection

Returns:
the current status.

getStarted

public java.util.Date getStarted()
Get the time this Connection was started.

Returns:
the time this Connection started serving requests.

setUserName

public void setUserName(java.lang.String username)
Set the user name of the client.

Parameters:
username - the username of the client.

getUserName

public java.lang.String getUserName()
Get the username of the client.

Returns:
the current username of the client.

setPassWord

public void setPassWord(java.lang.String password)
Set the password of the client.

Parameters:
password - the password of the client.

getPassWord

public java.lang.String getPassWord()
Get the password of the client.

Returns:
the current password of the client.

setKeepalive

public void setKeepalive(boolean keepalive)
Set keepalive to a new value. Note that keepalive can only be promoted down.

Parameters:
keepalive - the new keepalive value.

getKeepalive

public boolean getKeepalive()
Get the keepalive value.

Returns:
true if keepalive should be done, false otherwise.

setChunking

public void setChunking(boolean b)
Set the chunking option.

Parameters:
b - if true this connection should use chunking.

getChunking

public boolean getChunking()
Get the chunking option.

Returns:
if this connection is using chunking.

setMeta

public void setMeta(boolean meta)
Set the state of this request.

Parameters:
meta - true if this request is a metapage request, false otherwise.

getMeta

public boolean getMeta()
Get the state of this request.

Returns:
true if this is a metapage request, false otherwise.

setMayUseCache

public void setMayUseCache(boolean usecache)
Set the state of this request. This can only be promoted down..

Parameters:
usecache - true if we may use the cache for this request, false otherwise.

getMayUseCache

public boolean getMayUseCache()
Get the state of this request.

Returns:
true if we may use the cache for this request, false otherwise.

setMayCache

public void setMayCache(boolean cacheAllowed)
Set the state of this request. This can only be promoted down.

Parameters:
cacheAllowed - true if we may cache the response, false otherwise.

getMayCache

public boolean getMayCache()
Get the state of this request.

Returns:
true if we may cache the response, false otherwise.

setMayFilter

public void setMayFilter(boolean filterAllowed)
Get the state of this request. This can only be promoted down.

Parameters:
filterAllowed - true if we may filter the response, false otherwise.

getMayFilter

public boolean getMayFilter()
Get the state of the request.

Returns:
true if we may filter the response, false otherwise.

getSocket

public java.net.Socket getSocket()
Get the Socket that is being served.

Returns:
the requesting Socket.

getRequestLine

public java.lang.String getRequestLine()
Get the current request line

Returns:
the request being handled.

getStatusCode

public java.lang.String getStatusCode()
Get the status code of the request.

Returns:
the current status code.

getExtraInfo

public java.lang.String getExtraInfo()
Get the extra information for this request.

Returns:
the extra information if set.

setExtraInfo

public void setExtraInfo(java.lang.String exinfo)
Set the extra information for this request.

Parameters:
exinfo - the new extra information to set.

setContentLength

public void setContentLength(java.lang.String cl)
Set the content length of the response.

Parameters:
cl - the new content length.

getContentLength

public java.lang.String getContentLength()
Get the content length of the response.

Returns:
the content length of the response if set.

getHeader

public static HTTPHeader getHeader()
Get a new HTTPHeader. This is the same as getHeader ("HTTP/1.0 200 OK");

Returns:
a new HTTPHeader.

getHttpHeader

public HTTPHeader getHttpHeader()

getHeader

public static HTTPHeader getHeader(java.lang.String statusLine)
Get a new HTTPHeader initialized with some data.

Returns:
a new HTTPHeader.

get200

public HTTPHeader get200()
Get a 200 Ok header

Returns:
a 200 HTTPHeader .

get304

public HTTPHeader get304(HTTPHeader oldresp)
Get a 304 Not Modified header for the given old header

Parameters:
oldresp - the cached header.
Returns:
a 304 HTTPHeader .

get400

public HTTPHeader get400(java.lang.Exception exception)
Get a 400 Bad Request header for the given exception.

Parameters:
exception - the Exception handled.
Returns:
a HTTPHeader for the exception.

get403

public HTTPHeader get403()
Get a 403 Forbidden header.

Returns:
a HTTPHeader.

get407

public HTTPHeader get407(java.lang.String realm,
                         java.net.URL url)
Get a 407 Proxy Authentication Required for the given realm and url.

Parameters:
realm - the realm that requires auth.
url - the URL of the request made.
Returns:
a suitable HTTPHeader.

get412

public HTTPHeader get412()
Get a 412 Precondition Failed header.

Returns:
a suitable HTTPHeader.

get417

public HTTPHeader get417(java.lang.String expectation)
Get a 417 Expectation Failed header.

Returns:
a suitable HTTPHeader.

get500

public HTTPHeader get500(java.lang.Throwable exception)
Get a 500 Internal Server Error header for the given exception.

Parameters:
exception - the Exception made.
Returns:
a suitable HTTPHeader.

get504

public HTTPHeader get504(java.lang.Throwable exception)
Get a 500 Internal Server Error header for the given exception.

Parameters:
exception - the Exception made.
Returns:
a suitable HTTPHeader.