juglr.net
Class HTTPRequestReader

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

public class HTTPRequestReader
extends HTTPReader

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_URI_LENGTH
           
 
Fields inherited from class juglr.net.HTTPReader
buf, channel, MAX_HEADER_LENGTH
 
Constructor Summary
HTTPRequestReader(java.nio.channels.SocketChannel channel)
           
HTTPRequestReader(java.nio.channels.SocketChannel channel, java.nio.ByteBuffer buf)
           
 
Method Summary
 HTTP.Method readMethod()
           
 int readURI(byte[] target)
           
 HTTP.Version readVersion()
          Parse a HTTP protocol version declaration as used in the HTTP protocol.
 HTTPRequestReader reset(java.nio.channels.SocketChannel channel)
          Reset all state in the reader, preparing it for reading channel.
 
Methods inherited from class juglr.net.HTTPReader
close, readBody, readBody, readHeaderField, readLF, readSpace, readStatus, streamBody
 
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
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
Description copied from class: HTTPReader
Reset all state in the reader, preparing it for reading channel.

Overrides:
reset in class HTTPReader
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

readMethod

public HTTP.Method readMethod()

readURI

public int readURI(byte[] target)
            throws java.io.IOException
Throws:
java.io.IOException

readVersion

public HTTP.Version readVersion()
                         throws java.io.IOException
Description copied from class: HTTPReader
Parse a HTTP protocol version declaration as used in the HTTP protocol. The buffer will be positioned just after the last parsed character upon method return

Overrides:
readVersion in class HTTPReader
Returns:
the HTTP version or HTTP.Version.ERROR on errors
Throws:
java.io.IOException - upon errors reading from the socket