Skip to content

Commit e5cacb4

Browse files
authored
Fix #1172 (#1173)
This change is based on RFC7230, § 3.5 'Message Parsing Robustness': "Although the line terminator for the start-line and header fields is the sequence CRLF, a recipient MAY recognize a single LF as a line terminator and ignore any preceding CR."
1 parent ee8371f commit e5cacb4

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

httplib.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3217,17 +3217,26 @@ inline bool read_headers(Stream &strm, Headers &headers) {
32173217
if (!line_reader.getline()) { return false; }
32183218

32193219
// Check if the line ends with CRLF.
3220+
auto line_terminator_len = 2;
32203221
if (line_reader.end_with_crlf()) {
32213222
// Blank line indicates end of headers.
32223223
if (line_reader.size() == 2) { break; }
3224+
#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR
3225+
} else {
3226+
// Blank line indicates end of headers.
3227+
if (line_reader.size() == 1) { break; }
3228+
line_terminator_len = 1;
3229+
}
3230+
#else
32233231
} else {
32243232
continue; // Skip invalid line.
32253233
}
3234+
#endif
32263235

32273236
if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; }
32283237

3229-
// Exclude CRLF
3230-
auto end = line_reader.ptr() + line_reader.size() - 2;
3238+
// Exclude line terminator
3239+
auto end = line_reader.ptr() + line_reader.size() - line_terminator_len;
32313240

32323241
parse_header(line_reader.ptr(), end,
32333242
[&](std::string &&key, std::string &&val) {
@@ -5837,7 +5846,11 @@ inline bool ClientImpl::read_response_line(Stream &strm, const Request &req,
58375846

58385847
if (!line_reader.getline()) { return false; }
58395848

5849+
#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR
58405850
const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n");
5851+
#else
5852+
const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r?\n");
5853+
#endif
58415854

58425855
std::cmatch m;
58435856
if (!std::regex_match(line_reader.ptr(), m, re)) {

0 commit comments

Comments
 (0)