juglr.net
Class HTTPRequestReader

java.lang.Object
  extended by juglr.net.HTTPRequestReader

public class HTTPRequestReader
extends java.lang.Object

A Reader-like API for parsing HTTP requests from a SocketChannel. This class does not automatically parse the HTTP header and body. You must manually do this, by calling the appropriate read*-methods in the order the corresponding elements occur in the HTTP request. The reason for this is to allow for absolutely optimized parsing of the HTTP request.

To parse a HTTP request you would do something like:

 byte[] buf = new byte[1024];
 HTTPRequestReader reader = new HTTPRequestReader(chan);
 HTTP.Method method = reader.readMethod();
 int uriLen = reader.readURI(buf);
 HTTP.Version version = reader.readVersion();

 System.out.println("Request URI: " + new String(buf, 0, uriLen));

 while ((int headerLen = reader.readHeaderField(buf)) > 0) {
     System.out.println("Header field: " + new String(buf, 0, headerLen));
 }

 System.out.println("Body:");
 while ((int len = reader.readBody(buf)) > 0) {
     System.out.print(new String(buf, 0, len));
 }
 System.out.println();
 


Field Summary
static int MAX_HEADER_LENGTH
           
static int MAX_URI_LENGTH
           
 
Constructor Summary
HTTPRequestReader(java.nio.channels.SocketChannel channel)
           
HTTPRequestReader(java.nio.channels.SocketChannel channel, java.nio.ByteBuffer buf)
           
 
Method Summary
 void close()
          Close the underlying channel if it's open
 int readBody(byte[] target)
           
 int readHeaderField(byte[] target)
           
 HTTP.Method readMethod()
           
 int readURI(byte[] target)
           
 HTTP.Version readVersion()
           
 HTTPRequestReader reset(java.nio.channels.SocketChannel channel)
          Reset all state in the reader, preparing it for reading channel.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_URI_LENGTH

public static final int MAX_URI_LENGTH
See Also:
Constant Field Values

MAX_HEADER_LENGTH

public static final int MAX_HEADER_LENGTH
See Also:
Constant Field Values
Constructor Detail

HTTPRequestReader

public HTTPRequestReader(java.nio.channels.SocketChannel channel,
                         java.nio.ByteBuffer buf)

HTTPRequestReader

public HTTPRequestReader(java.nio.channels.SocketChannel channel)
Method Detail

reset

public HTTPRequestReader reset(java.nio.channels.SocketChannel channel)
                        throws java.io.IOException
Reset all state in the reader, preparing it for reading channel.

Parameters:
channel - the channel to start reading from. If the reader refers a channel then this channel will be closed.
Returns:
always returns this
Throws:
java.io.IOException - if the reader already refers an open channel and there is an error closing it

close

public void close()
           throws java.io.IOException
Close the underlying channel if it's open

Throws:
java.io.IOException - if the underlying channel is open and there is an error closing the channel

readMethod

public HTTP.Method readMethod()

readURI

public int readURI(byte[] target)

readVersion

public HTTP.Version readVersion()

readHeaderField

public int readHeaderField(byte[] target)

readBody

public int readBody(byte[] target)