Top Description Fields Constructors Methods
org.apache.http.message

public Class BasicLineParser

extends Object
implements LineParser
Class Inheritance
All Implemented Interfaces
org.apache.http.message.LineParser
Annotations
@Contract
threading:IMMUTABLE
Imports
org.apache.http.Header, .HttpVersion, .ParseException, .ProtocolVersion, .RequestLine, .StatusLine, org.apache.http.annotation.ThreadingBehavior, .Contract, org.apache.http.protocol.HTTP, org.apache.http.util.Args, .CharArrayBuffer

Basic parser for lines in the head section of an HTTP message. There are individual methods for parsing a request line, a status line, or a header line. The lines to parse are passed in memory, the parser does not depend on any specific IO mechanism. Instances of this class are stateless and thread-safe. Derived classes MUST maintain these properties.

Note

This class was created by refactoring parsing code located in various other classes. The author tags from those other classes have been replicated here, although the association with the parsing code taken from there has not been traced.

Since
4.0

Field Summary

Modifier and TypeField and Description
public static final BasicLineParser
DEFAULT

A default instance of this class, for use as default or fallback.

public static final BasicLineParser
protected final ProtocolVersion
protocol

A version of the protocol to parse.

Constructor Summary

AccessConstructor and Description
public
BasicLineParser(final ProtocolVersion
a version of the protocol to parse, or null for HTTP. The actual version is not relevant, only the protocol name.
proto
)

Creates a new line parser for the given HTTP-like protocol.

public
BasicLineParser()

Creates a new line parser for HTTP.

Method Summary

Modifier and TypeMethod and Description
protected ProtocolVersion

Returns:

the protocol version
createProtocolVersion
(final int
the major version number, for example 1 in HTTP/1.0
major
,
final int
the minor version number, for example 0 in HTTP/1.0
minor
)

Creates a protocol version.

protected RequestLine

Returns:

a new status line with the given data
createRequestLine
(final String
the request method
method
,
final String
the requested URI
uri
,
final ProtocolVersion
the protocol version
ver
)

Instantiates a new request line.

protected StatusLine

Returns:

a new status line with the given data
createStatusLine
(final ProtocolVersion
the protocol version
ver
,
final int
the status code
status
,
final String
the reason phrase
reason
)

Instantiates a new status line.

public boolean
hasProtocolVersion(final CharArrayBuffer
a buffer holding the line to inspect
buffer
,
final ParserCursor
the cursor at which to check for a protocol version, or negative for "end of line". Whether the check tolerates whitespace before or after the protocol version is implementation dependent.
cursor
)

Implements org.apache.http.message.LineParser.hasProtocolVersion.

Checks whether there likely is a protocol version in a line.
public static Header
parseHeader(final String value, final LineParser parser)

public Header
parseHeader(final CharArrayBuffer
a buffer holding the full header line. This buffer MUST NOT be re-used afterwards, since the returned object may reference the contents later.
buffer
)

Implements org.apache.http.message.LineParser.parseHeader.

Creates a header from a line.
public static ProtocolVersion
parseProtocolVersion(final String value, final LineParser parser)

public ProtocolVersion
parseProtocolVersion(final CharArrayBuffer
a buffer holding the protocol version to parse
buffer
,
final ParserCursor
the parser cursor containing the current position and the bounds within the buffer for the parsing operation
cursor
)

Implements org.apache.http.message.LineParser.parseProtocolVersion.

Parses the textual representation of a protocol version.
public static RequestLine
parseRequestLine(final String value, final LineParser parser)

public RequestLine

Returns:

the parsed request line
parseRequestLine
(final CharArrayBuffer
a buffer holding the line to parse
buffer
,
final ParserCursor
the parser cursor containing the current position and the bounds within the buffer for the parsing operation
cursor
)

Implements org.apache.http.message.LineParser.parseRequestLine.

Parses a request line.
public static StatusLine
parseStatusLine(final String value, final LineParser parser)

public StatusLine
parseStatusLine(final CharArrayBuffer
a buffer holding the line to parse
buffer
,
final ParserCursor
the parser cursor containing the current position and the bounds within the buffer for the parsing operation
cursor
)

Implements org.apache.http.message.LineParser.parseStatusLine.

Parses a status line.
protected void
skipWhitespace(final CharArrayBuffer buffer, final ParserCursor cursor)

Helper to skip whitespace.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULTback to summary
public static final BasicLineParser DEFAULT

Deprecated

(4.3) use INSTANCE

A default instance of this class, for use as default or fallback. Note that BasicLineParser is not a singleton, there can be many instances of the class itself and of derived classes. The instance here provides non-customized, default behavior.

Annotations
@Deprecated
INSTANCEback to summary
public static final BasicLineParser INSTANCE
protocolback to summary
protected final ProtocolVersion protocol

A version of the protocol to parse. The version is typically not relevant, but the protocol name.

Constructor Detail

BasicLineParserback to summary
public BasicLineParser(final ProtocolVersion proto)

Creates a new line parser for the given HTTP-like protocol.

Parameters
proto:ProtocolVersion

a version of the protocol to parse, or null for HTTP. The actual version is not relevant, only the protocol name.

BasicLineParserback to summary
public BasicLineParser()

Creates a new line parser for HTTP.

Method Detail

createProtocolVersionback to summary
protected ProtocolVersion createProtocolVersion(final int major, final int minor)

Creates a protocol version. Called from parseProtocolVersion.

Parameters
major:int

the major version number, for example 1 in HTTP/1.0

minor:int

the minor version number, for example 0 in HTTP/1.0

Returns:ProtocolVersion

the protocol version

createRequestLineback to summary
protected RequestLine createRequestLine(final String method, final String uri, final ProtocolVersion ver)

Instantiates a new request line. Called from parseRequestLine.

Parameters
method:String

the request method

uri:String

the requested URI

ver:ProtocolVersion

the protocol version

Returns:RequestLine

a new status line with the given data

createStatusLineback to summary
protected StatusLine createStatusLine(final ProtocolVersion ver, final int status, final String reason)

Instantiates a new status line. Called from parseStatusLine.

Parameters
ver:ProtocolVersion

the protocol version

status:int

the status code

reason:String

the reason phrase

Returns:StatusLine

a new status line with the given data

hasProtocolVersionback to summary
public boolean hasProtocolVersion(final CharArrayBuffer buffer, final ParserCursor cursor)

Implements org.apache.http.message.LineParser.hasProtocolVersion.

Doc from org.apache.http.message.LineParser.hasProtocolVersion.

Checks whether there likely is a protocol version in a line. This method implements a heuristic to check for a likely protocol version specification. It does not guarantee that parseProtocolVersion would not detect a parse error. This can be used to detect garbage lines before a request or status line.

Parameters
buffer:CharArrayBuffer

a buffer holding the line to inspect

cursor:ParserCursor

the cursor at which to check for a protocol version, or negative for "end of line". Whether the check tolerates whitespace before or after the protocol version is implementation dependent.

Returns:boolean

true if there is a protocol version at the argument index (possibly ignoring whitespace), false otherwise

Annotations
@Override
parseHeaderback to summary
public static Header parseHeader(final String value, final LineParser parser) throws ParseException
parseHeaderback to summary
public Header parseHeader(final CharArrayBuffer buffer) throws ParseException

Implements org.apache.http.message.LineParser.parseHeader.

Doc from org.apache.http.message.LineParser.parseHeader.

Creates a header from a line. The full header line is expected here. Header continuation lines must be joined by the caller before invoking this method.

Parameters
buffer:CharArrayBuffer

a buffer holding the full header line. This buffer MUST NOT be re-used afterwards, since the returned object may reference the contents later.

Returns:Header

the header in the argument buffer. The returned object MAY be a wrapper for the argument buffer. The argument buffer MUST NOT be re-used or changed afterwards.

Annotations
@Override
Exceptions
ParseException:
in case of a parse error
parseProtocolVersionback to summary
public static ProtocolVersion parseProtocolVersion(final String value, final LineParser parser) throws ParseException
parseProtocolVersionback to summary
public ProtocolVersion parseProtocolVersion(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException

Implements org.apache.http.message.LineParser.parseProtocolVersion.

Doc from org.apache.http.message.LineParser.parseProtocolVersion.

Parses the textual representation of a protocol version. This is needed for parsing request lines (last element) as well as status lines (first element).

Parameters
buffer:CharArrayBuffer

a buffer holding the protocol version to parse

cursor:ParserCursor

the parser cursor containing the current position and the bounds within the buffer for the parsing operation

Returns:ProtocolVersion

the parsed protocol version

Annotations
@Override
Exceptions
ParseException:
in case of a parse error
parseRequestLineback to summary
public static RequestLine parseRequestLine(final String value, final LineParser parser) throws ParseException
parseRequestLineback to summary
public RequestLine parseRequestLine(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException

Implements org.apache.http.message.LineParser.parseRequestLine.

Parses a request line.

Parameters
buffer:CharArrayBuffer

a buffer holding the line to parse

cursor:ParserCursor

Doc from org.apache.http.message.LineParser.parseRequestLine.

the parser cursor containing the current position and the bounds within the buffer for the parsing operation

Returns:RequestLine

the parsed request line

Annotations
@Override
Exceptions
ParseException:
in case of a parse error
parseStatusLineback to summary
public static StatusLine parseStatusLine(final String value, final LineParser parser) throws ParseException
parseStatusLineback to summary
public StatusLine parseStatusLine(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException

Implements org.apache.http.message.LineParser.parseStatusLine.

Doc from org.apache.http.message.LineParser.parseStatusLine.

Parses a status line.

Parameters
buffer:CharArrayBuffer

a buffer holding the line to parse

cursor:ParserCursor

the parser cursor containing the current position and the bounds within the buffer for the parsing operation

Returns:StatusLine

the parsed status line

Annotations
@Override
Exceptions
ParseException:
in case of a parse error
skipWhitespaceback to summary
protected void skipWhitespace(final CharArrayBuffer buffer, final ParserCursor cursor)

Helper to skip whitespace.