-
Notifications
You must be signed in to change notification settings - Fork 511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade HTTP header field-line parser #1908
Draft
yadij
wants to merge
2
commits into
squid-cache:master
Choose a base branch
from
yadij:arc-parserng-sf-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+191
−102
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright (C) 1996-2023 The Squid Software Foundation and contributors | ||
* | ||
* Squid software is distributed under GPLv2+ license and includes | ||
* contributions from numerous individuals and organizations. | ||
* Please see the COPYING and CONTRIBUTORS files for details. | ||
*/ | ||
|
||
#include "squid.h" | ||
#include "base/Raw.h" | ||
#include "http/one/FieldParser.h" | ||
#include "sbuf/Stream.h" | ||
#include "SquidConfig.h" | ||
#include "debug/Stream.h" | ||
|
||
Http::One::FieldParser::FieldParser(const SBuf &aBuf, const http_hdr_owner_type &aType) : | ||
Http::One::Parser(), | ||
msgType(aType), | ||
tok(aBuf) | ||
{} | ||
|
||
/** | ||
* RFC 9112 section 5: | ||
* | ||
* field-line = field-name ":" OWS field-value OWS | ||
*/ | ||
void | ||
Http::One::FieldParser::parseFieldLine(SBuf &name, SBuf &value) | ||
{ | ||
name = parseFieldName(); // consumes ':' delimiter | ||
value = parseFieldValue(); | ||
|
||
if (!tok.atEnd()) | ||
skipLineTerminator(tok); | ||
} | ||
|
||
/** | ||
* RFC 9110 section 5.1: | ||
* | ||
* field-name = token | ||
* token = 1*TCHAR | ||
*/ | ||
SBuf | ||
Http::One::FieldParser::parseFieldName() | ||
{ | ||
auto name = tok.prefix("field-name", CharacterSet::TCHAR); | ||
|
||
/* | ||
* RFC 9112 section 5.1: | ||
* "No whitespace is allowed between the field name and colon. | ||
* ... | ||
* A server MUST reject, with a response status code of 400 (Bad Request), | ||
* any received request message that contains whitespace between a header | ||
* field name and colon. A proxy MUST remove any such whitespace from a | ||
* response message before forwarding the message downstream." | ||
*/ | ||
const bool stripWhitespace = (msgType == hoReply) || Config.onoff.relaxed_header_parser; | ||
if (stripWhitespace && tok.skipAll(Http1::Parser::WhitespaceCharacters())) { | ||
// TODO: reduce log spam from 'tok.buf()' below | ||
debugs(11, Config.onoff.relaxed_header_parser <= 0 ? 2 : 3, | ||
"WARNING: Whitespace after field-name '" << name << tok.buf() << "'"); | ||
} | ||
|
||
if(!tok.skip(':')) | ||
throw TextException("invalid field-name", Here()); | ||
|
||
if (name.length() == 0) | ||
throw TextException("missing field-name", Here()); | ||
|
||
if (name.length() > 65534) { | ||
/* String must be LESS THAN 64K and it adds a terminating NULL */ | ||
// TODO: update this to show proper name.length() in Raw markup, but not print all that | ||
throw TextException(ToSBuf("huge field-line (", Raw("field-name", name.c_str(), 100), "...)"), Here()); | ||
} | ||
|
||
return name; | ||
} | ||
|
||
/** | ||
* RFC 9110 section 5.1: | ||
* | ||
* field-value = *field-content | ||
* field-content = field-vchar | ||
* [ 1*( SP / HTAB / field-vchar ) field-vchar ] | ||
* field-vchar = VCHAR / obs-text | ||
* obs-text = %x80-FF | ||
*/ | ||
SBuf | ||
Http::One::FieldParser::parseFieldValue() | ||
{ | ||
static const CharacterSet fvChars = (CharacterSet::VCHAR + CharacterSet::OBSTEXT).rename("field-value"); | ||
auto value = tok.prefix("field-value", fvChars); | ||
|
||
/** | ||
* RFC 9110 section 5.5: | ||
* field value does not include leading or trailing whitespace. | ||
* ... parsing implementation MUST exclude such whitespace prior | ||
* to evaluating the field value | ||
*/ | ||
const auto start = value.findFirstNotOf(Http1::Parser::WhitespaceCharacters()); | ||
const auto end = value.findLastNotOf(Http1::Parser::WhitespaceCharacters()); | ||
value.chop(start, (end-start)); | ||
|
||
if (value.length() > 65534) { | ||
/* String must be LESS THAN 64K and it adds a terminating NULL */ | ||
// TODO: update this to show proper length() in Raw markup, but not print all that | ||
throw TextException(ToSBuf("huge field-line (", Raw("field-value", value.c_str(), 100), "...)"), Here()); | ||
} | ||
|
||
return value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (C) 1996-2023 The Squid Software Foundation and contributors | ||
* | ||
* Squid software is distributed under GPLv2+ license and includes | ||
* contributions from numerous individuals and organizations. | ||
* Please see the COPYING and CONTRIBUTORS files for details. | ||
*/ | ||
|
||
#ifndef SQUID_SRC_HTTP_ONE_FIELDPARSER_H | ||
#define SQUID_SRC_HTTP_ONE_FIELDPARSER_H | ||
|
||
#include "http/one/Parser.h" | ||
#include "HttpHeader.h" | ||
#include "parser/Tokenizer.h" | ||
|
||
namespace Http { | ||
namespace One { | ||
|
||
/** HTTP/1.x header field parser | ||
* | ||
* Works on a raw character I/O buffer and tokenizes the content into | ||
* the field-lines of an HTTP/1 message: | ||
* | ||
* RFC 9112 section 5: | ||
* field-line = field-name ":" OWS field-value OWS | ||
*/ | ||
class FieldParser : public Http1::Parser | ||
{ | ||
public: | ||
FieldParser(const SBuf &, const http_hdr_owner_type &); | ||
|
||
/// extract a field name and value from the buffer being parsed. | ||
void parseFieldLine(SBuf &name, SBuf &value); | ||
|
||
/* Http1::Parser API */ | ||
void clear() { tok.reset(SBuf()); } | ||
bool parse(const SBuf &newBuf) override { const bool e = tok.atEnd(); tok.reset(newBuf); return e; } | ||
|
||
private: | ||
SBuf parseFieldName(); | ||
SBuf parseFieldValue(); | ||
|
||
/* Http1::Parser API */ | ||
size_type firstLineSize() const override { return 0; } | ||
|
||
// Whether a request or response message is being parsed. | ||
// Some parser validation and tolerance depends on type. | ||
const http_hdr_owner_type msgType; | ||
|
||
// low-level tokenizer to use for parsing. | ||
// owns and manages the buffer being parsed. | ||
Http1::Parser::Tokenizer tok; | ||
}; | ||
|
||
} // namespace One | ||
} // namespace Http | ||
|
||
#endif /* SQUID_SRC_HTTP_ONE_FIELDPARSER_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not derive a field parser from a message prefix parser. There is no "X is a Y" relationship between these two concepts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sigh. You vetoed extracting the useful parts of
Http1::Parser
out into a separate class this one should have been importing them from. I see this is going to have to be even simpler for you.