Basic parser for lines in the head section of an HTTP message. More...
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.
The following notice applies to the original API on which this API is based, and to its documentation. The documentation of this API has been revised from the original.
/* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
Public Member Functions | |
BasicLineParser (ProtocolVersion proto) | |
Creates a new line parser for the given HTTP-like protocol. More... | |
BasicLineParser () | |
Creates a new line parser for HTTP. | |
ProtocolVersion | parseProtocolVersion (final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException |
non-javadoc, see interface LineParser | |
boolean | hasProtocolVersion (final CharArrayBuffer buffer, final ParserCursor cursor) |
Checks whether there likely is a protocol version in a line. More... | |
RequestLine | parseRequestLine (final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException |
Parses a request line. More... | |
StatusLine | parseStatusLine (final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException |
non-javadoc, see interface LineParser | |
Header | parseHeader (CharArrayBuffer buffer) throws ParseException |
Creates a header from a line. More... | |
Static Public Member Functions | |
static final ProtocolVersion | parseProtocolVersion (String value, LineParser parser) throws ParseException |
non-javadoc, see interface LineParser | |
static final RequestLine | parseRequestLine (final String value, LineParser parser) throws ParseException |
non-javadoc, see interface LineParser | |
static final StatusLine | parseStatusLine (final String value, LineParser parser) throws ParseException |
non-javadoc, see interface LineParser | |
Static Public Attributes | |
static final BasicLineParser | DEFAULT = new BasicLineParser() |
A default instance of this class, for use as default or fallback. More... | |
Protected Member Functions | |
ProtocolVersion | createProtocolVersion (int major, int minor) |
Creates a protocol version. More... | |
RequestLine | createRequestLine (final String method, final String uri, final ProtocolVersion ver) |
Instantiates a new request line. More... | |
StatusLine | createStatusLine (final ProtocolVersion ver, final int status, final String reason) |
Instantiates a new status line. More... | |
void | skipWhitespace (final CharArrayBuffer buffer, final ParserCursor cursor) |
Helper to skip whitespace. | |
Protected Attributes | |
final ProtocolVersion | protocol |
A version of the protocol to parse. More... | |
BasicLineParser | ( | ProtocolVersion | proto | ) |
proto | a version of the protocol to parse, or null for HTTP. The actual version is not relevant, only the protocol name. |
|
protected |
Called from parseProtocolVersion.
major | the major version number, for example 1 in HTTP/1.0 |
minor | the minor version number, for example 0 in HTTP/1.0 |
boolean hasProtocolVersion | ( | final CharArrayBuffer | buffer, |
final ParserCursor | cursor | ||
) |
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.
buffer | a buffer holding the line to inspect |
cursor | 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. |
true
if there is a protocol version at the argument index (possibly ignoring whitespace), false
otherwise Implements LineParser.
RequestLine parseRequestLine | ( | final CharArrayBuffer | buffer, |
final ParserCursor | cursor | ||
) | throws ParseException |
buffer | a buffer holding the line to parse |
cursor |
ParseException | in case of a parse error |
Implements LineParser.
|
protected |
Called from parseRequestLine.
method | the request method |
uri | the requested URI |
ver | the protocol version |
|
protected |
Called from parseStatusLine.
ver | the protocol version |
status | the status code |
reason | the reason phrase |
Header parseHeader | ( | CharArrayBuffer | buffer | ) | throws ParseException |
The full header line is expected here. Header continuation lines must be joined by the caller before invoking this method.
buffer | a buffer holding the full header line. This buffer MUST NOT be re-used afterwards, since the returned object may reference the contents later. |
ParseException | in case of a parse error |
Implements LineParser.
|
static |
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.
|
protected |
The version is typically not relevant, but the protocol name.