#include <ResponseParser.h>

Inheritance diagram for Http::One::ResponseParser:
Collaboration diagram for Http::One::ResponseParser:

Public Types

typedef SBuf::size_type size_type
typedef ::Parser::Tokenizer Tokenizer

Public Member Functions

 ResponseParser ()=default
 ResponseParser (const ResponseParser &)=default
ResponseParseroperator= (const ResponseParser &)=default
 ResponseParser (ResponseParser &&)=default
ResponseParseroperator= (ResponseParser &&)=default
virtual ~ResponseParser ()
virtual void clear ()
virtual Http1::Parser::size_type firstLineSize () const
 size in bytes of the first line including CRLF terminator More...
virtual bool parse (const SBuf &aBuf)
Http::StatusCode messageStatus () const
SBuf reasonPhrase () const
bool needsMoreData () const
size_type headerBlockSize () const
size_type messageHeaderSize () const
SBuf mimeHeader () const
 buffer containing HTTP mime headers, excluding message first-line. More...
const AnyP::ProtocolVersionmessageProtocol () const
 the protocol label for this message More...
char * getHostHeaderField ()
const SBufremaining () const
 the remaining unprocessed section of buffer More...

Static Public Member Functions

static void ParseResponseStatus (Tokenizer &, StatusCode &code)
static const CharacterSetWhitespaceCharacters ()
static const CharacterSetDelimiterCharacters ()

Public Attributes

Http::StatusCode parseStatusCode = Http::scNone

Protected Member Functions

void skipLineTerminator (Tokenizer &) const
bool grabMimeBlock (const char *which, const size_t limit)

Protected Attributes

SBuf buf_
 bytes remaining to be parsed More...
ParseState parsingStage_ = HTTP_PARSE_NONE
 what stage the parser is currently up to More...
AnyP::ProtocolVersion msgProtocol_
 what protocol label has been found in the first line (if any) More...
SBuf mimeHeaderBlock_
 buffer holding the mime headers (if any) More...
bool hackExpectsMime_ = false
 Whether the invalid HTTP as HTTP/0.9 hack expects a mime header block. More...

Static Protected Attributes

static const SBuf Http1magic
 RFC 7230 section 2.6 - 7 magic octets. More...

Private Member Functions

int parseResponseFirstLine ()
int parseResponseStatusAndReason (Tokenizer &)
void cleanMimePrefix ()
void unfoldMime ()

Private Attributes

bool completedStatus_ = false
Http::StatusCode statusCode_ = Http::scNone
 HTTP/1 status-line status code. More...
SBuf reasonPhrase_
 HTTP/1 status-line reason phrase. More...

Static Private Attributes

static const SBuf IcyMagic
 magic prefix for identifying ICY response messages More...

Detailed Description

HTTP/1.x protocol response parser

Also capable of parsing unexpected ICY responses and upgrading HTTP/0.9 syntax responses to HTTP/1.1

Works on a raw character I/O buffer and tokenizes the content into the major CRLF delimited segments of an HTTP/1 respone message:

  • status-line (version SP status SP reash-phrase)
  • mime-header (set of RFC2616 syntax header fields)

Definition at line 29 of file ResponseParser.h.

Member Typedef Documentation

◆ size_type

Definition at line 43 of file Parser.h.

◆ Tokenizer

Definition at line 44 of file Parser.h.

Constructor & Destructor Documentation

◆ ResponseParser() [1/3]

Http::One::ResponseParser::ResponseParser ( )

Referenced by clear().

◆ ResponseParser() [2/3]

Http::One::ResponseParser::ResponseParser ( const ResponseParser )

◆ ResponseParser() [3/3]

Http::One::ResponseParser::ResponseParser ( ResponseParser &&  )

◆ ~ResponseParser()

virtual Http::One::ResponseParser::~ResponseParser ( )

Definition at line 37 of file ResponseParser.h.

Member Function Documentation

◆ cleanMimePrefix()

void Http::One::Parser::cleanMimePrefix ( )

Remove invalid lines (if any) from the mime prefix

RFC 7230 section 3: "A recipient that receives whitespace between the start-line and the first header field MUST ... consume each whitespace-preceded line without further processing of it."

We need to always use the relaxed delimiters here to prevent line smuggling through strict parsers.

Note that 'whitespace' in RFC 7230 includes CR. So that means sequences of CRLF will be pruned, but not sequences of bare-LF.

Definition at line 103 of file Parser.cc.

References Http::One::CrLf(), CharacterSet::LF, LineCharacters(), and RelaxedDelimiterCharacters().

◆ clear()

virtual void Http::One::ResponseParser::clear ( )

Set this parser back to a default state. Will DROP any reference to a buffer (does not free).

Implements Http::One::Parser.

Definition at line 40 of file ResponseParser.h.

References ResponseParser().

◆ DelimiterCharacters()

const CharacterSet & Http::One::Parser::DelimiterCharacters ( )

Whitespace between protocol elements in restricted contexts like request line, status line, asctime-date, and credentials Seen in RFCs as SP but may be "relaxed" by us. See also: WhitespaceCharacters(). XXX: Misnamed and overused.

Definition at line 59 of file Parser.cc.

References Config, SquidConfig::onoff, SquidConfig::relaxed_header_parser, RelaxedDelimiterCharacters(), and CharacterSet::SP.

Referenced by Http::ContentLengthInterpreter::goodSuffix(), and ParseResponseStatus().

◆ firstLineSize()

◆ getHostHeaderField()

char * Http::One::Parser::getHostHeaderField ( )

Scan the mime header block (badly) for a Host header.

BUG: omits lines when searching for headers with obs-fold or multiple entries.

BUG: limits output to just 1KB when Squid accepts up to 64KB line length.

A pointer to a field-value of the first matching field-name, or NULL.

Definition at line 219 of file Parser.cc.

References CharacterSet::ALPHA, SBuf::caseCmp(), SBuf::chop(), SBuf::consume(), Http::One::CrLf(), debugs, CharacterSet::DIGIT, SBuf::findFirstNotOf(), GET_HDR_SZ, CharacterSet::LF, LineCharacters(), LOCAL_ARRAY, SBuf::npos, NULL, SBufToCstring(), SBuf::substr(), SBuf::trim(), and CharacterSet::WSP.

◆ grabMimeBlock()

bool Http::One::Parser::grabMimeBlock ( const char *  which,
const size_t  limit 

Scan to find the mime headers block for current message.

Return values
trueIf mime block (or a blocks non-existence) has been identified accurately within limit characters. mimeHeaderBlock_ has been updated and buf_ consumed.
falseAn error occurred, or no mime terminator found within limit.

Definition at line 163 of file Parser.cc.

References debugs, headersEnd(), Http::One::HTTP_PARSE_DONE, AnyP::PROTO_HTTP, AnyP::PROTO_ICY, and Http::scHeaderTooLarge.

◆ headerBlockSize()

size_type Http::One::Parser::headerBlockSize ( ) const

size in bytes of the message headers including CRLF terminator(s) but excluding first-line bytes

Definition at line 73 of file Parser.h.

References SBuf::length(), and Http::One::Parser::mimeHeaderBlock_.

Referenced by Http::One::Parser::messageHeaderSize(), and Http::Message::parseHeader().

◆ messageHeaderSize()

size_type Http::One::Parser::messageHeaderSize ( ) const

size in bytes of HTTP message block, includes first-line and mime headers excludes any body/entity/payload bytes excludes any garbage prefix before the first-line

Definition at line 78 of file Parser.h.

References Http::One::Parser::firstLineSize(), and Http::One::Parser::headerBlockSize().

Referenced by Http::Message::parseHeader().

◆ messageProtocol()

const AnyP::ProtocolVersion& Http::One::Parser::messageProtocol ( ) const

Definition at line 84 of file Parser.h.

References Http::One::Parser::msgProtocol_.

◆ messageStatus()

Http::StatusCode Http::One::ResponseParser::messageStatus ( ) const

Definition at line 45 of file ResponseParser.h.

References statusCode_.

◆ mimeHeader()

SBuf Http::One::Parser::mimeHeader ( ) const

Definition at line 81 of file Parser.h.

References Http::One::Parser::mimeHeaderBlock_.

Referenced by Http::Message::parseHeader().

◆ needsMoreData()

bool Http::One::Parser::needsMoreData ( ) const

Whether the parser is waiting on more data to complete parsing a message. Use to distinguish between incomplete data and error results when parse() returns false.

Definition at line 66 of file Parser.h.

References Http::One::HTTP_PARSE_DONE, and Http::One::Parser::parsingStage_.

Referenced by ConnStateData::handleChunkedRequestBody(), testHttp1Parser::testDripFeed(), testHttp1Parser::testParserConstruct(), and testResults().

◆ operator=() [1/2]

ResponseParser& Http::One::ResponseParser::operator= ( const ResponseParser )

◆ operator=() [2/2]

ResponseParser& Http::One::ResponseParser::operator= ( ResponseParser &&  )

◆ parse()

bool Http::One::ResponseParser::parse ( const SBuf aBuf)

attempt to parse a message from the buffer

Return values
trueif a full message was found and parsed
falseif incomplete, invalid or no message was found

Implements Http::One::Parser.

Definition at line 197 of file ResponseParser.cc.

References Config, DBG_DATA, debugs, Http::One::HTTP_PARSE_DONE, Http::One::HTTP_PARSE_FIRST, Http::One::HTTP_PARSE_MIME, Http::One::HTTP_PARSE_NONE, SBuf::length(), SquidConfig::maxReplyHeaderSize, and Http::scInvalidHeader.

Referenced by Http::Tunneler::handleResponse().

◆ parseResponseFirstLine()

int Http::One::ResponseParser::parseResponseFirstLine ( )

Attempt to parse the method field out of an HTTP message status-line.

Governed by: RFC 1945 section 6.1 RFC 7230 section 2.6, 3.1 and 3.5

Parsing state is stored between calls. The current implementation uses checkpoints after each successful status-line field. The return value tells you whether the parsing is completed or not.

Return values
-1an error occurred.
1successful parse. statusCode_ and maybe reasonPhrase_ are filled and buffer consumed including first delimiter.
0more data is needed to complete the parse

Definition at line 124 of file ResponseParser.cc.

References assert, DBG_DATA, debugs, Http::One::HTTP_PARSE_DONE, AnyP::PROTO_HTTP, AnyP::PROTO_ICY, AnyP::PROTO_NONE, Http::ProtocolVersion(), and Http::scOkay.

◆ ParseResponseStatus()

void Http::One::ResponseParser::ParseResponseStatus ( Tokenizer tok,
StatusCode code 

extracts response status-code and the following delimiter; validates status-code

[out]codesyntactically valid status-code (unchanged on syntax errors)
InsuffientInputand other exceptions on syntax and validation errors

Definition at line 85 of file ResponseParser.cc.

References code, debugs, Http::One::Parser::DelimiterCharacters(), Here, and ToSBuf().

Referenced by Http::StatusLine::parse().

◆ parseResponseStatusAndReason()

int Http::One::ResponseParser::parseResponseStatusAndReason ( Tokenizer tok)

◆ reasonPhrase()

SBuf Http::One::ResponseParser::reasonPhrase ( ) const

Definition at line 46 of file ResponseParser.h.

References reasonPhrase_.

◆ remaining()

const SBuf& Http::One::Parser::remaining ( ) const

◆ skipLineTerminator()

void Http::One::Parser::skipLineTerminator ( Tokenizer tok) const

detect and skip the CRLF or (if tolerant) LF line terminator consume from the tokenizer.

exceptionon bad or InsuffientInput.
Return values
trueonly if line terminator found.
falseincomplete or missing line terminator, need more data.

Definition at line 66 of file Parser.cc.

References Config, Http::One::CrLf(), CharacterSet::LF, SquidConfig::onoff, SquidConfig::relaxed_header_parser, and TexcHere.

◆ unfoldMime()

void Http::One::Parser::unfoldMime ( )

Replace obs-fold with a single SP,

RFC 7230 section 3.2.4 "A server that receives an obs-fold in a request message that is not within a message/http container MUST ... replace each received obs-fold with one or more SP octets prior to interpreting the field value or forwarding the message downstream."

"A proxy or gateway that receives an obs-fold in a response message that is not within a message/http container MUST ... replace each received obs-fold with one or more SP octets prior to interpreting the field value or forwarding the message downstream."

Definition at line 138 of file Parser.cc.

References CharacterSet::CR, CharacterSet::LF, CharacterSet::rename(), SBuf::substr(), and CharacterSet::WSP.

◆ WhitespaceCharacters()

const CharacterSet & Http::One::Parser::WhitespaceCharacters ( )

Whitespace between regular protocol elements. Seen in RFCs as OWS, RWS, BWS, SP/HTAB but may be "relaxed" by us. See also: DelimiterCharacters().

Definition at line 52 of file Parser.cc.

References Config, SquidConfig::onoff, SquidConfig::relaxed_header_parser, RelaxedDelimiterCharacters(), and CharacterSet::WSP.

Referenced by Http::ContentLengthInterpreter::findDigits(), and Http::One::ParseBws().

Member Data Documentation

◆ buf_

SBuf Http::One::Parser::buf_

◆ completedStatus_

bool Http::One::ResponseParser::completedStatus_ = false

Whether we found the status code yet. We cannot rely on status value because server may send "000".

Definition at line 62 of file ResponseParser.h.

◆ hackExpectsMime_

bool Http::One::Parser::hackExpectsMime_ = false

Definition at line 160 of file Parser.h.

◆ Http1magic

const SBuf Http::One::Parser::Http1magic

Definition at line 145 of file Parser.h.

Referenced by firstLineSize().

◆ IcyMagic

const SBuf Http::One::ResponseParser::IcyMagic

Definition at line 58 of file ResponseParser.h.

Referenced by firstLineSize().

◆ mimeHeaderBlock_

SBuf Http::One::Parser::mimeHeaderBlock_

◆ msgProtocol_

◆ parseStatusCode

Http::StatusCode Http::One::Parser::parseStatusCode = Http::scNone

HTTP status code resulting from the parse process. to be used on the invalid message handling.

Http::scNone indicates incomplete parse, Http::scOkay indicates no error, other codes represent a parse error.

Definition at line 108 of file Parser.h.

Referenced by testHttp1Parser::testParserConstruct(), and testResults().

◆ parsingStage_

ParseState Http::One::Parser::parsingStage_ = HTTP_PARSE_NONE

◆ reasonPhrase_

SBuf Http::One::ResponseParser::reasonPhrase_

Definition at line 68 of file ResponseParser.h.

Referenced by firstLineSize(), and reasonPhrase().

◆ statusCode_

Http::StatusCode Http::One::ResponseParser::statusCode_ = Http::scNone

Definition at line 65 of file ResponseParser.h.

Referenced by messageStatus().

The documentation for this class was generated from the following files:






Web Site Translations