sip: a header is read from the header section only

Header and Headers iterated every line of a message, body included, so a
body line that spells a header name — a text someone typed, or the UCS-2
bytes of one — would be read as that header: a second Via for a final to
echo, a P-Asserted-Identity that CallerId takes for the sender. ForEachLine now ends
at the first empty line after a line of text (RFC 3261 7: the headers
end at the empty line) and skips empty lines before it (a keepalive's
CRLFs ahead of a message), so every reader of a header reads the header
section. Pinned in tests/Sip:
a body Via is no Via, a name that occurs only in the body is absent,
leading CRLFs are skipped. With these the suite has more than 77 checks,
so it returns 1 for any failure and no longer their count: `crafter-build
test` takes an exit status of 77 for a skipped suite.
This commit is contained in:
Jorijn van der Graaf 2026-10-18 05:44:20 +02:00
commit 89048c3602
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
3 changed files with 32 additions and 8 deletions

View file

@ -40,15 +40,26 @@ namespace imsd::sip {
return s;
}
// Iterate \n-separated lines (any trailing CR stays on the line and is
// stripped by Trim when a value is extracted).
// Iterate the \n-separated lines of the header section (any trailing CR
// stays on the line and is stripped by Trim when a value is extracted).
// The first empty line after a line of text ends the headers (RFC 3261
// 7): a body line that spells a header name — a text someone typed, or
// UCS-2 bytes that happen to — is never read as one. Empty lines before
// the first line of text (a keepalive's CRLFs ahead of a message) are
// skipped.
template <typename F>
void ForEachLine(std::string_view msg, F&& f) {
std::size_t pos = 0;
bool started = false;
while (pos <= msg.size()) {
std::size_t nl = msg.find('\n', pos);
std::string_view line = msg.substr(pos, nl == std::string_view::npos ? std::string_view::npos : nl - pos);
if (!f(line)) return;
const bool blank = line.empty() || line == "\r";
if (blank && started) return;
if (!blank) {
started = true;
if (!f(line)) return;
}
if (nl == std::string_view::npos) return;
pos = nl + 1;
}