org.norther.tammi.acorn.http
Class HttpParser

java.lang.Object
  extended by org.norther.tammi.acorn.http.HttpParser

public abstract class HttpParser
extends Object

A static HTTP header parser. As HTTP data should be 8-bit stuff, we don't use encodings to gain best performance.

Author:
Ilkka Priha

Nested Class Summary
static class HttpParser.ParsedState
          A state holder for the parser.
 
Field Summary
static String ACCEPT
          Accept request header.
static String ACCEPT_CHARSET
          Accept-Charset request header.
static String ACCEPT_LANGUAGE
          Accept-Language request header.
static String ACCEPT_RANGES
          Accept-Ranges response header.
static String AUTHENTICATE
          WWW-Authenticate response header.
static String AUTHORIZATION
          Authorization request header.
static String CACHE_CONTROL
          Cache-Control header.
static String CONNECTION
          Connection header.
static String CONTENT_DISPOSITION
          Content-Disposition content header.
static String CONTENT_ENCODING
          Content-Encoding content header.
static String CONTENT_LANGUAGE
          Content-Language content header.
static String CONTENT_LENGTH
          Content-Length content header.
static String CONTENT_TYPE
          Content-Type content header.
static String CONTEXT_URL
          Contect-URL pragma.
static String COOKIE
          Cookie request header.
static String DATE
          Date header.
static String DEFAULT_PROVIDER
          The default provider.
static String DELETE
          Delete method.
static String DOCTYPE_HTML_4_01
          HTML doctype.
static String DOCTYPE_WML_1_1
          WML doctype.
static String DOCTYPE_XHTML_1_0
          XHTML doctype.
static String DURATION
          Duration access log comment.
static String EXPECT
          Expect request header.
static String EXPECT_100_CONTINUE
          100-Continue expect header value.
static String EXPIRES
          Expires entity header.
static String FORM_DATA
          Form-Data- Content-Disposition parameter.
static String FORM_DATA_FILENAME
          Form-Data-Filename Content-Disposition parameter.
static String FORM_DATA_NAME
          Form-Data-Name Content-Disposition parameter.
static String FORM_DATA_TYPE
          Form-Data-Type Content-Disposition parameter.
static String FORM_LOGIN_ACTION
          Login action (Servlet API 2.3).
static String FORM_LOGIN_PASSWORD
          Login password (Servlet API 2.3).
static String FORM_LOGIN_PATH
          Login path.
static String FORM_LOGIN_REMEMBER
          Remember login.
static String FORM_LOGIN_USERNAME
          Login username (Servlet API 2.3).
static String GENERIC_USER_AGENT
          The generic user agent.
static String GET
          Get method.
static String HEAD
          Head method.
static String HOST
          Host request header.
static String HTTP
          HTTP protocol.
static int HTTP_PORT
          HTTP port.
static String HTTP_REDIRECTION
          The redirection URI path parameter.
static String HTTP_REQUEST
          Http-Request access log comment.
static String HTTP_SESSION_COOKIE
          The session id cookie (servlet API 2.3).
static String HTTP_SESSION_ID
          The session id URI path parameter (servlet API 2.3).
static String HTTPS
          HTTPS protocol.
static int HTTPS_PORT
          HTTPS port.
static String IF_MODIFIED_SINCE
          If-Modified-Since entity header.
static String IF_UNMODIFIED_SINCE
          If-Unmodified-Since entity header.
static String IS_SECURE
          Is-Secure pragma.
static String LAST_MODIFIED
          Last-Modified entity header.
static String LOCAL_ADDR
          Local address.
static String LOCAL_HOST
          Local host.
static String LOCATION
          Location response header.
static String NO_CACHE
          No caching (HTTP 1.0).
static String NO_CACHE_CONTROL
          No caching (HTTP 1.1).
static int OK
          The OK status.
static String OK_RESPONSE
          The OK message.
static String OPTIONS
          Options method.
static String POST
          Post method.
static String PRAGMA
          Pragma header.
static String PRIVATE_CACHE_CONTROL
          Private caching (HTTP 1.1).
static String PUT
          Put method.
static int RAW
          0 Raw.
static String REFERER
          Referer request header.
static String REMOTE_ADDR
          Remote-Addr pragma.
static String REMOTE_HOST
          Remote-Host access log comment.
static String REMOTE_USER
          Remote-User access log comment.
static String RFC931_USER
          RFC931-User access log comment.
static String SERVER
          Server response header.
static String SERVER_INFO
          Server info.
static String SERVER_PORT
          Server-Port pragma.
static String SET_COOKIE
          Set-Cookie response header.
static String STATUS
          Status response header.
static String TRACE
          Trace method.
static String TRAILER
          Trailer header.
static String TRANSFER_ENCODING
          Transfer-Encoding header.
static String UNAUTHENTICATED
          Unauthenticated.
static String UPGRADE
          Upgrade header.
static String USER_AGENT
          User-Agent request header.
static String VERSION_INFO
          Version info.
static String VIA
          Via header.
static String WARNING
          Warning header.
static String XHTML_XMLNS
          XMLNS attribute.
 
Constructor Summary
HttpParser()
           
 
Method Summary
static String getDate(long date)
          Gets a MIME date.
protected static char[] getLine(InputStream in, HttpParser.ParsedState ps, int max)
          Gets the next line buffer from an input stream.
static String getMessage(int code)
          Gets the standard message for a status code.
static String parseCharacterEncoding(String str)
          Parses the character encoding of a header string.
static String parseCharacterEncoding(String str, String enc)
          Parses the character encoding of a header string with a default.
static List parseCharSets(Enumeration headers)
          Parses charsets from headers.
static List parseContentTypes(Enumeration headers)
          Parses MIME type strings from headers.
static long parseDate(String str)
          Parses the date of a header string.
static Map parseHeaders(InputStream in)
          Parses the headers of an input stream.
static void parseHeaders(InputStream in, Map headers)
          Parses the headers of an input stream.
protected static List parseHeaderValues(Enumeration headers, boolean params)
          Parses sorted values from headers.
static List parseLocales(Enumeration headers)
          Parses locales from header values.
static List parseMimeTypes(Enumeration headers)
          Parses MIME types from headers.
static float parseProtocol(String str)
          Parses the protocol.
protected static void parseRequest(char[] buf, HttpParser.ParsedState ps, List request)
          Parses the request from a char array.
static List parseRequest(InputStream in)
          Parses the request from an input stream.
static List parseRequest(InputStream in, int maxLength)
          Parses the request from an input stream.
static void parseRequest(InputStream in, List request, int maxLength)
          Parses the request from an input stream.
static List parseRequest(String str)
          Parses the request from a string.
static void parseRequest(String str, List request)
          Parses the request from a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PROVIDER

public static final String DEFAULT_PROVIDER
The default provider.

See Also:
Constant Field Values

GENERIC_USER_AGENT

public static final String GENERIC_USER_AGENT
The generic user agent.

See Also:
Constant Field Values

HTTP

public static final String HTTP
HTTP protocol.

See Also:
Constant Field Values

HTTPS

public static final String HTTPS
HTTPS protocol.

See Also:
Constant Field Values

LOCAL_HOST

public static final String LOCAL_HOST
Local host.

See Also:
Constant Field Values

LOCAL_ADDR

public static final String LOCAL_ADDR
Local address.

See Also:
Constant Field Values

HTTP_PORT

public static final int HTTP_PORT
HTTP port.

See Also:
Constant Field Values

HTTPS_PORT

public static final int HTTPS_PORT
HTTPS port.

See Also:
Constant Field Values

GET

public static final String GET
Get method.

See Also:
Constant Field Values

PUT

public static final String PUT
Put method.

See Also:
Constant Field Values

POST

public static final String POST
Post method.

See Also:
Constant Field Values

HEAD

public static final String HEAD
Head method.

See Also:
Constant Field Values

TRACE

public static final String TRACE
Trace method.

See Also:
Constant Field Values

DELETE

public static final String DELETE
Delete method.

See Also:
Constant Field Values

OPTIONS

public static final String OPTIONS
Options method.

See Also:
Constant Field Values

CACHE_CONTROL

public static final String CACHE_CONTROL
Cache-Control header.

See Also:
Constant Field Values

CONNECTION

public static final String CONNECTION
Connection header.

See Also:
Constant Field Values

DATE

public static final String DATE
Date header.

See Also:
Constant Field Values

PRAGMA

public static final String PRAGMA
Pragma header.

See Also:
Constant Field Values

TRAILER

public static final String TRAILER
Trailer header.

See Also:
Constant Field Values

UPGRADE

public static final String UPGRADE
Upgrade header.

See Also:
Constant Field Values

VIA

public static final String VIA
Via header.

See Also:
Constant Field Values

WARNING

public static final String WARNING
Warning header.

See Also:
Constant Field Values

TRANSFER_ENCODING

public static final String TRANSFER_ENCODING
Transfer-Encoding header.

See Also:
Constant Field Values

ACCEPT

public static final String ACCEPT
Accept request header.

See Also:
Constant Field Values

ACCEPT_CHARSET

public static final String ACCEPT_CHARSET
Accept-Charset request header.

See Also:
Constant Field Values

ACCEPT_LANGUAGE

public static final String ACCEPT_LANGUAGE
Accept-Language request header.

See Also:
Constant Field Values

AUTHORIZATION

public static final String AUTHORIZATION
Authorization request header.

See Also:
Constant Field Values

COOKIE

public static final String COOKIE
Cookie request header.

See Also:
Constant Field Values

EXPECT

public static final String EXPECT
Expect request header.

See Also:
Constant Field Values

EXPECT_100_CONTINUE

public static final String EXPECT_100_CONTINUE
100-Continue expect header value.

See Also:
Constant Field Values

HOST

public static final String HOST
Host request header.

See Also:
Constant Field Values

REFERER

public static final String REFERER
Referer request header.

See Also:
Constant Field Values

USER_AGENT

public static final String USER_AGENT
User-Agent request header.

See Also:
Constant Field Values

ACCEPT_RANGES

public static final String ACCEPT_RANGES
Accept-Ranges response header.

See Also:
Constant Field Values

AUTHENTICATE

public static final String AUTHENTICATE
WWW-Authenticate response header.

See Also:
Constant Field Values

LOCATION

public static final String LOCATION
Location response header.

See Also:
Constant Field Values

SERVER

public static final String SERVER
Server response header.

See Also:
Constant Field Values

SET_COOKIE

public static final String SET_COOKIE
Set-Cookie response header.

See Also:
Constant Field Values

STATUS

public static final String STATUS
Status response header.

See Also:
Constant Field Values

EXPIRES

public static final String EXPIRES
Expires entity header.

See Also:
Constant Field Values

IF_MODIFIED_SINCE

public static final String IF_MODIFIED_SINCE
If-Modified-Since entity header.

See Also:
Constant Field Values

IF_UNMODIFIED_SINCE

public static final String IF_UNMODIFIED_SINCE
If-Unmodified-Since entity header.

See Also:
Constant Field Values

LAST_MODIFIED

public static final String LAST_MODIFIED
Last-Modified entity header.

See Also:
Constant Field Values

CONTENT_DISPOSITION

public static final String CONTENT_DISPOSITION
Content-Disposition content header.

See Also:
Constant Field Values

CONTENT_ENCODING

public static final String CONTENT_ENCODING
Content-Encoding content header.

See Also:
Constant Field Values

CONTENT_LANGUAGE

public static final String CONTENT_LANGUAGE
Content-Language content header.

See Also:
Constant Field Values

CONTENT_LENGTH

public static final String CONTENT_LENGTH
Content-Length content header.

See Also:
Constant Field Values

CONTENT_TYPE

public static final String CONTENT_TYPE
Content-Type content header.

See Also:
Constant Field Values

HTTP_REQUEST

public static final String HTTP_REQUEST
Http-Request access log comment.

See Also:
Constant Field Values

REMOTE_HOST

public static final String REMOTE_HOST
Remote-Host access log comment.

See Also:
Constant Field Values

RFC931_USER

public static final String RFC931_USER
RFC931-User access log comment.

See Also:
Constant Field Values

REMOTE_USER

public static final String REMOTE_USER
Remote-User access log comment.

See Also:
Constant Field Values

DURATION

public static final String DURATION
Duration access log comment.

See Also:
Constant Field Values

FORM_DATA

public static final String FORM_DATA
Form-Data- Content-Disposition parameter.

See Also:
Constant Field Values

FORM_DATA_TYPE

public static final String FORM_DATA_TYPE
Form-Data-Type Content-Disposition parameter.

See Also:
Constant Field Values

FORM_DATA_NAME

public static final String FORM_DATA_NAME
Form-Data-Name Content-Disposition parameter.

See Also:
Constant Field Values

FORM_DATA_FILENAME

public static final String FORM_DATA_FILENAME
Form-Data-Filename Content-Disposition parameter.

See Also:
Constant Field Values

SERVER_INFO

public static final String SERVER_INFO
Server info.

See Also:
Constant Field Values

VERSION_INFO

public static final String VERSION_INFO
Version info.

See Also:
Constant Field Values

HTTP_SESSION_COOKIE

public static final String HTTP_SESSION_COOKIE
The session id cookie (servlet API 2.3).

See Also:
Constant Field Values

HTTP_SESSION_ID

public static final String HTTP_SESSION_ID
The session id URI path parameter (servlet API 2.3).

See Also:
Constant Field Values

HTTP_REDIRECTION

public static final String HTTP_REDIRECTION
The redirection URI path parameter.

See Also:
Constant Field Values

DOCTYPE_HTML_4_01

public static final String DOCTYPE_HTML_4_01
HTML doctype.

See Also:
Constant Field Values

DOCTYPE_XHTML_1_0

public static final String DOCTYPE_XHTML_1_0
XHTML doctype.

See Also:
Constant Field Values

DOCTYPE_WML_1_1

public static final String DOCTYPE_WML_1_1
WML doctype.

See Also:
Constant Field Values

XHTML_XMLNS

public static final String XHTML_XMLNS
XMLNS attribute.

See Also:
Constant Field Values

NO_CACHE

public static final String NO_CACHE
No caching (HTTP 1.0).

See Also:
Constant Field Values

NO_CACHE_CONTROL

public static final String NO_CACHE_CONTROL
No caching (HTTP 1.1).

See Also:
Constant Field Values

PRIVATE_CACHE_CONTROL

public static final String PRIVATE_CACHE_CONTROL
Private caching (HTTP 1.1).

See Also:
Constant Field Values

CONTEXT_URL

public static final String CONTEXT_URL
Contect-URL pragma.

See Also:
Constant Field Values

SERVER_PORT

public static final String SERVER_PORT
Server-Port pragma.

See Also:
Constant Field Values

REMOTE_ADDR

public static final String REMOTE_ADDR
Remote-Addr pragma.

See Also:
Constant Field Values

IS_SECURE

public static final String IS_SECURE
Is-Secure pragma.

See Also:
Constant Field Values

FORM_LOGIN_PATH

public static final String FORM_LOGIN_PATH
Login path.

See Also:
Constant Field Values

FORM_LOGIN_REMEMBER

public static final String FORM_LOGIN_REMEMBER
Remember login.

See Also:
Constant Field Values

FORM_LOGIN_USERNAME

public static final String FORM_LOGIN_USERNAME
Login username (Servlet API 2.3).

See Also:
Constant Field Values

FORM_LOGIN_PASSWORD

public static final String FORM_LOGIN_PASSWORD
Login password (Servlet API 2.3).

See Also:
Constant Field Values

FORM_LOGIN_ACTION

public static final String FORM_LOGIN_ACTION
Login action (Servlet API 2.3).

See Also:
Constant Field Values

UNAUTHENTICATED

public static final String UNAUTHENTICATED
Unauthenticated.

See Also:
Constant Field Values

RAW

public static final int RAW
0 Raw.

See Also:
Constant Field Values

OK

public static final int OK
The OK status.

See Also:
Constant Field Values

OK_RESPONSE

public static final String OK_RESPONSE
The OK message.

Constructor Detail

HttpParser

public HttpParser()
Method Detail

parseRequest

public static final List parseRequest(InputStream in)
                               throws IOException
Parses the request from an input stream. The result is returned as a list containing the request, method, URI, protocol and optional query as separate items.

Parameters:
in - an input stream.
Returns:
a list of request items.
Throws:
IOException - for I/O errors.
IllegalArgumentException - for parsing errors.

parseRequest

public static final List parseRequest(InputStream in,
                                      int maxLength)
                               throws IOException
Parses the request from an input stream. The result is returned as a list containing the request, method, URI, protocol and optional query as separate items. The length of the parsed line may not exceed the specified length limit.

Parameters:
in - an input stream.
maxLength - the maximum length of the parsed line.
Returns:
a list of request items.
Throws:
IOException - for I/O errors.
IllegalArgumentException - for parsing errors.

parseRequest

public static final void parseRequest(InputStream in,
                                      List request,
                                      int maxLength)
                               throws IOException
Parses the request from an input stream. The result is returned as a list containing the request, method, URI, protocol and optional query as separate items. The length of the parsed line may not exceed the specified length limit.

Parameters:
in - an input stream.
request - a list for the result.
maxLength - the maximum length of the parsed line.
Throws:
IOException - for I/O errors.
IllegalArgumentException - for parsing errors.

parseRequest

public static final List parseRequest(String str)
Parses the request from a string. The result is returned as a list containing the request, method, URI, protocol and optional query as separate items.

Parameters:
str - the string to parse.
Returns:
a list of request items.
Throws:
IllegalArgumentException - for parsing errors.

parseRequest

public static final void parseRequest(String str,
                                      List request)
Parses the request from a string. The result is returned as a list containing the request, method, URI, protocol and optional query as separate items.

Parameters:
str - the string to parse.
request - a list for the result.
Throws:
IllegalArgumentException - for parsing errors.

parseHeaders

public static final Map parseHeaders(InputStream in)
                              throws IOException
Parses the headers of an input stream. Header values will be presented as vectors of strings (supporting Enumerations as required by the Servlet API).

Parameters:
in - an input stream.
Returns:
a map of headers.
Throws:
IOException - for I/O errors.
IllegalArgumentException - for parsing errors.

parseHeaders

public static final void parseHeaders(InputStream in,
                                      Map headers)
                               throws IOException
Parses the headers of an input stream. Header values will be presented as vectors of strings (supporting Enumerations as required by the Servlet API).

Parameters:
in - an input stream.
headers - a map for the result.
Throws:
IOException - for I/O errors.
IllegalArgumentException - for parsing errors.

parseProtocol

public static final float parseProtocol(String str)
Parses the protocol.

Parameters:
str - a protocol string to parse.
Returns:
the version of the protocol.
Throws:
NumberFormatException - for parsing errors.

parseLocales

public static final List parseLocales(Enumeration headers)
Parses locales from header values. The result is sorted according to q-values. Parameters are not included in parsed locales.

Parameters:
headers - an enumeration of header values.
Returns:
a list of locales.
Throws:
IllegalArgumentException - for parsing errors.

parseCharSets

public static final List parseCharSets(Enumeration headers)
Parses charsets from headers. The result is sorted according to q-values. Parameters are not included in parsed charsets.

Parameters:
headers - an enumeration of header values.
Returns:
a list of charsets.
Throws:
IllegalArgumentException - for parsing errors.

parseCharacterEncoding

public static final String parseCharacterEncoding(String str)
Parses the character encoding of a header string.

Parameters:
str - a header string.
Returns:
the character encoding or ISO-8859-1 if not specified.

parseCharacterEncoding

public static final String parseCharacterEncoding(String str,
                                                  String enc)
Parses the character encoding of a header string with a default.

Parameters:
str - a header string.
enc - an optional default encoding.
Returns:
the character encoding.

parseDate

public static final long parseDate(String str)
Parses the date of a header string.

Parameters:
str - a header string.
Returns:
the date in msecs.

parseContentTypes

public static final List parseContentTypes(Enumeration headers)
Parses MIME type strings from headers. The result is sorted according to q-values. Parameters are not included in parsed types.

Parameters:
headers - an enumeration of header values.
Returns:
a list of MIME type strings.
Throws:
IllegalArgumentException - for parsing errors.

parseMimeTypes

public static final List parseMimeTypes(Enumeration headers)
Parses MIME types from headers. The result is sorted according to q-values.

Parameters:
headers - an enumeration of header values.
Returns:
a list of MIME types.
Throws:
IllegalArgumentException - for parsing errors.

getDate

public static final String getDate(long date)
Gets a MIME date.

Parameters:
date - a date in msecs.
Returns:
the date in MIME format.

getMessage

public static final String getMessage(int code)
Gets the standard message for a status code.

Parameters:
code - a code to get.
Returns:
the standard message.

parseRequest

protected static final void parseRequest(char[] buf,
                                         HttpParser.ParsedState ps,
                                         List request)
Parses the request from a char array. The result is returned as a list containing the request, method, URI, protocol and optional query as separate items.

Parameters:
buf - the char array to parse.
ps - the parsed state to apply.
request - a list for the result.
Throws:
IllegalArgumentException - for parsing errors.

parseHeaderValues

protected static final List parseHeaderValues(Enumeration headers,
                                              boolean params)
Parses sorted values from headers. The result is sorted according to q-values.

Parameters:
headers - an enumeration of header values.
params - a flag to include parameters.
Returns:
a list of parsed values.
Throws:
IllegalArgumentException - for parsing errors.

getLine

protected static final char[] getLine(InputStream in,
                                      HttpParser.ParsedState ps,
                                      int max)
                               throws IOException
Gets the next line buffer from an input stream.

Parameters:
in - an input stream.
ps - a parsed state.
max - the maximum accepted length.
Returns:
the line as chars.
Throws:
IOException - on I/O errors.


Copyright © 2004 The Norther Organization. All rights reserved.