iglu.net
Class WebHandler

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--iglu.net.ClientHandler
              |
              +--iglu.net.WebHandler
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
ProxyServerExample.myURLHandler

public class WebHandler
extends ClientHandler

Implements the ClientHandler needed for a proxy server. This object lets PortServer act as a proxy server for web connections. It does all the proxy work for you. To use this object, create a descendent class and override the doneWithNetworkAccess method. In that method, put the code to deal with what should be done for a url once it has been accessed. When used with PortServer, once instance of this class will be created for every url a browser tries to access. So if an html file is accessed by a browser that has five pictures on it, at least six instances of this class will be created for that page. If using this class with the PortServer, each instance of this class will run on its own thread.

Notice that most of the members of this class get their values during processClient. So they should be set properly by doneWithNetworkAccess.

See iglu.examples.ProxyServerExample for information on how to use this class to create a proxy server.

Note: This version of WebHandler will not allow you to daisy chain onto other proxy servers. But it would not be too hard to do. Just add a new constructor which takes a host and port number of the proxy and stores them in global members. Then modify process client to not worry about any parsing. Just pass all information on to the proxy and send all the other information back.

Version:
0.5
Author:
Travis BauerTravis Bauer

Field Summary
 int ATTEMPTS
          The number of times to try to connect to the web server
 int BUFFSIZE
           
 java.lang.String clientCommands
          unprocessed list of chars from the client's request
 java.lang.String Command
          the command (GET, POST, etc) of the request
static int defaultSize
           
 java.lang.String doc
          the page that was requested (without the server name)
 java.lang.String host
          host name of the web server the request was for
static int HTML
          html request
 int port
          port for the web server the request went to
 byte[] rawServerContent
          unprocessed list of chars from server header
 java.lang.String requestedURL
          the url that was requested
 java.lang.String serverContent
           
 java.lang.String serverHeader
           
 java.lang.String serverReply
           
static int UNKNOWN
          Not known.
 int WAIT
          The amount of time to pause between retries
protected  java.net.Socket webServer
          a socket for connecting to the webserver
protected  java.io.InputStream wsin
          for reading from the webServer pipe
protected  java.io.OutputStream wsout
          for writing to the webServer pipe
 
Fields inherited from class iglu.net.ClientHandler
clientSocket, in, myServer, out
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
WebHandler(java.net.Socket client, PortServer ms)
          Initialize the variables for this class
 
Method Summary
protected  java.lang.String figureOutDestination()
           
static byte[] grow(byte[] array, int size)
           
 void internalDoneWithNetworkAccess()
          Unzips the input stream if it is gzipped.
static java.lang.String lookupValue(java.lang.String text, java.lang.String key)
           
protected  void performCom(java.lang.String firstLine)
           
 void processClient()
          Actually go out and get the document.
 int requestType()
          try to figure out what kind of request was made.
 
Methods inherited from class iglu.net.ClientHandler
doneWithNetworkAccess, init, run
 
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, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

webServer

protected java.net.Socket webServer
a socket for connecting to the webserver


wsin

protected java.io.InputStream wsin
for reading from the webServer pipe


wsout

protected java.io.OutputStream wsout
for writing to the webServer pipe


clientCommands

public java.lang.String clientCommands
unprocessed list of chars from the client's request


rawServerContent

public byte[] rawServerContent
unprocessed list of chars from server header


serverReply

public java.lang.String serverReply

serverHeader

public java.lang.String serverHeader

serverContent

public java.lang.String serverContent

defaultSize

public static final int defaultSize
See Also:
Constant Field Values

host

public java.lang.String host
host name of the web server the request was for


port

public int port
port for the web server the request went to


Command

public java.lang.String Command
the command (GET, POST, etc) of the request


doc

public java.lang.String doc
the page that was requested (without the server name)


requestedURL

public java.lang.String requestedURL
the url that was requested


ATTEMPTS

public int ATTEMPTS
The number of times to try to connect to the web server


WAIT

public int WAIT
The amount of time to pause between retries


BUFFSIZE

public int BUFFSIZE

UNKNOWN

public static final int UNKNOWN
Not known. This is the default type

See Also:
Constant Field Values

HTML

public static final int HTML
html request

See Also:
Constant Field Values
Constructor Detail

WebHandler

public WebHandler(java.net.Socket client,
                  PortServer ms)
           throws java.io.IOException
Initialize the variables for this class

Parameters:
client - What socket the client is listening/talking on.
ms - The portserver creating this client. Included to allow inter-client communication so it is easier to implement something like a chat server.
Throws:
java.io.IOException - If it cannot establish the correct communication.
Method Detail

processClient

public void processClient()
Actually go out and get the document. In the event that there is an error during the processing, it will print a message to the stdout, and simply return. After doneWithNetworkAccess is called, the object will close connections with the client. This will force the browser to give and "document contains no data" error, which is, of course, passed on to the user.

Overrides:
processClient in class ClientHandler

figureOutDestination

protected java.lang.String figureOutDestination()

performCom

protected void performCom(java.lang.String firstLine)

requestType

public int requestType()
try to figure out what kind of request was made.

Returns:
The requst type of this instance of the class.

internalDoneWithNetworkAccess

public void internalDoneWithNetworkAccess()
Unzips the input stream if it is gzipped. NOTE: Make sure you call super() when you override this method. Otherwise you may get garbage in the server content. In an earlier version, this code was done before the content was sent to the browser. However, the current implementation saves time since the browser does not have to wait until the zipping is done before moving on to new pages.

Overrides:
internalDoneWithNetworkAccess in class ClientHandler

lookupValue

public static java.lang.String lookupValue(java.lang.String text,
                                           java.lang.String key)

grow

public static byte[] grow(byte[] array,
                          int size)