2019-07-10 14:19:33 -07:00
|
|
|
#include "IMFHeader.h"
|
|
|
|
|
#include "Exception.h"
|
|
|
|
|
|
|
|
|
|
namespace coreutils {
|
|
|
|
|
|
2026-08-23 14:25:58 -07:00
|
|
|
IMFHeader::IMFHeader(StreamReader &reader) : Parser(reader) {}
|
|
|
|
|
|
|
|
|
|
Parser::Status IMFHeader::parse() {
|
|
|
|
|
MString &buffer = reader.getBuffer();
|
|
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
|
|
|
|
|
|
case NAME: {
|
|
|
|
|
if (buffer.eod())
|
|
|
|
|
return Status::INCOMPLETE;
|
|
|
|
|
if (buffer.getChar() == '\r') {
|
|
|
|
|
// ZString::getLength() is already cursor-relative.
|
|
|
|
|
if (buffer.getLength() < 2)
|
|
|
|
|
return Status::INCOMPLETE;
|
|
|
|
|
if (!buffer.ifNext(ZString("\r\n")))
|
|
|
|
|
throw coreutils::Exception("IMFHeader: expected CRLF at end of header block.");
|
|
|
|
|
return Status::COMPLETE;
|
|
|
|
|
}
|
|
|
|
|
name << buffer.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-0123456789");
|
|
|
|
|
if (buffer.eod())
|
|
|
|
|
return Status::INCOMPLETE;
|
|
|
|
|
if (!buffer.ifNext(ZString(":")))
|
|
|
|
|
throw coreutils::Exception("IMFHeader: expected ':' in header name.");
|
|
|
|
|
state = SKIP_SPACE;
|
|
|
|
|
return Status::DATA_AVAIL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SKIP_SPACE:
|
|
|
|
|
while (!buffer.eod() && buffer.getChar() == ' ')
|
|
|
|
|
buffer.nextChar();
|
|
|
|
|
if (buffer.eod())
|
|
|
|
|
return Status::INCOMPLETE;
|
|
|
|
|
state = FIELD;
|
|
|
|
|
return Status::DATA_AVAIL;
|
2025-12-17 17:07:14 -08:00
|
|
|
|
2025-12-18 15:40:37 -08:00
|
|
|
case FIELD:
|
2026-08-23 14:25:58 -07:00
|
|
|
field << buffer.getTokenExclude("\r\n");
|
2026-08-23 15:07:09 -07:00
|
|
|
// Need 2 bytes buffered to reliably test for "\r\n" - eod() (a
|
|
|
|
|
// 1-byte check) would wrongly treat a lone trailing \r as
|
|
|
|
|
// malformed instead of "wait for the \n". Same bug class already
|
|
|
|
|
// fixed once in IMFBoundary's TAIL state.
|
|
|
|
|
if (buffer.getLength() < 2)
|
2026-08-23 14:25:58 -07:00
|
|
|
return Status::INCOMPLETE;
|
|
|
|
|
if (!buffer.ifNext(ZString("\r\n")))
|
|
|
|
|
throw coreutils::Exception("IMFHeader: expected CRLF at end of header field.");
|
|
|
|
|
state = FOLD_CHECK;
|
|
|
|
|
return Status::DATA_AVAIL;
|
|
|
|
|
|
|
|
|
|
case FOLD_CHECK:
|
|
|
|
|
if (buffer.eod())
|
|
|
|
|
return Status::INCOMPLETE;
|
|
|
|
|
if (buffer.getChar() == ' ' || buffer.getChar() == '\t') {
|
|
|
|
|
buffer.nextChar();
|
|
|
|
|
field << " ";
|
|
|
|
|
state = SKIP_SPACE;
|
|
|
|
|
return Status::DATA_AVAIL;
|
|
|
|
|
}
|
|
|
|
|
commitField();
|
|
|
|
|
state = NAME;
|
|
|
|
|
return Status::DATA_AVAIL;
|
|
|
|
|
|
2020-03-03 15:46:24 -08:00
|
|
|
}
|
2026-08-23 14:25:58 -07:00
|
|
|
throw coreutils::Exception("IMFHeader: unreachable state.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IMFHeader::commitField() {
|
|
|
|
|
headers[name] = field;
|
|
|
|
|
name = MString();
|
|
|
|
|
field = MString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::map<MString, MString> &IMFHeader::getHeaders() {
|
|
|
|
|
return headers;
|
2019-07-10 14:19:33 -07:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 14:25:58 -07:00
|
|
|
}
|