Skip to content

Commit

Permalink
Added new argument to Http::One::ParseBws()
Browse files Browse the repository at this point in the history
Depending on new wsp_only argument in ParseBws() it will be decided
which set of whitespaces characters will be parsed. If wsp_only is set
to true, only SP and HTAB chars will be parsed.

Also optimized number of ParseBws calls.
  • Loading branch information
uhliarik committed Oct 11, 2024
1 parent 8d0ee42 commit 0174f3b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/http/one/Parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ Http::One::ErrorLevel()

// BWS = *( SP / HTAB ) ; WhitespaceCharacters() may relax this RFC 7230 rule
void
Http::One::ParseBws(Parser::Tokenizer &tok)
Http::One::ParseBws(Parser::Tokenizer &tok, const bool wsp_only)
{
const auto count = tok.skipAll(Parser::WhitespaceCharacters());
const auto count = tok.skipAll(wsp_only ? CharacterSet::WSP : Parser::WhitespaceCharacters());

if (tok.atEnd())
throw InsufficientInput(); // even if count is positive
Expand Down
3 changes: 2 additions & 1 deletion src/http/one/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ class Parser : public RefCountable
};

/// skips and, if needed, warns about RFC 7230 BWS ("bad" whitespace)
/// \param wsp_only force skipping of whatespaces only, don't consider skipping relaxed delimeter chars
/// \throws InsufficientInput when the end of BWS cannot be confirmed
void ParseBws(Parser::Tokenizer &);
void ParseBws(Parser::Tokenizer &, const bool wsp_only = false);

/// the right debugs() level for logging HTTP violation messages
int ErrorLevel();
Expand Down
13 changes: 9 additions & 4 deletions src/http/one/TeChunkedParser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,11 @@ Http::One::TeChunkedParser::parseChunkMetadataSuffix(Tokenizer &tok)
// Code becomes much simpler when incremental parsing functions throw on
// bad or insufficient input, like in the code below. TODO: Expand up.
try {
tok.skipAll(CharacterSet::WSP); // Some servers send SP/TAB after chunk-size
parseChunkExtensions(tok); // a possibly empty chunk-ext list
// A possibly empty chunk-ext list. If no chunk-ext has been found,
// try to skip trailing BWS, because some servers send "chunk-size BWS CRLF".
if (!parseChunkExtensions(tok))
ParseBws(tok, true);

tok.skipRequired("CRLF after [chunk-ext]", Http1::CrLf());
buf_ = tok.remaining();
parsingStage_ = theChunkSize ? Http1::HTTP_PARSE_CHUNK : Http1::HTTP_PARSE_MIME;
Expand All @@ -140,20 +143,22 @@ Http::One::TeChunkedParser::parseChunkMetadataSuffix(Tokenizer &tok)

/// Parses the chunk-ext list (RFC 9112 section 7.1.1:
/// chunk-ext = *( BWS ";" BWS chunk-ext-name [ BWS "=" BWS chunk-ext-val ] )
void
bool
Http::One::TeChunkedParser::parseChunkExtensions(Tokenizer &callerTok)
{
bool foundChunkExt = false;
do {
auto tok = callerTok;

ParseBws(tok); // Bug 4492: IBM_HTTP_Server sends SP after chunk-size

if (!tok.skip(';'))
return; // reached the end of extensions (if any)
return foundChunkExt; // reached the end of extensions (if any)

parseOneChunkExtension(tok);
buf_ = tok.remaining(); // got one extension
callerTok = tok;
foundChunkExt = true;
} while (true);
}

Expand Down
2 changes: 1 addition & 1 deletion src/http/one/TeChunkedParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class TeChunkedParser : public Http1::Parser
private:
bool parseChunkSize(Tokenizer &tok);
bool parseChunkMetadataSuffix(Tokenizer &);
void parseChunkExtensions(Tokenizer &);
bool parseChunkExtensions(Tokenizer &);
void parseOneChunkExtension(Tokenizer &);
bool parseChunkBody(Tokenizer &tok);
bool parseChunkEnd(Tokenizer &tok);
Expand Down

0 comments on commit 0174f3b

Please sign in to comment.