@@ -2790,6 +2790,10 @@ inline bool stream_line_reader::getline() {
27902790 fixed_buffer_used_size_ = 0 ;
27912791 glowable_buffer_.clear ();
27922792
2793+ #ifndef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR
2794+ char prev_byte = 0 ;
2795+ #endif
2796+
27932797 for (size_t i = 0 ;; i++) {
27942798 char byte;
27952799 auto n = strm_.read (&byte, 1 );
@@ -2806,7 +2810,12 @@ inline bool stream_line_reader::getline() {
28062810
28072811 append (byte);
28082812
2813+ #ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR
28092814 if (byte == ' \n ' ) { break ; }
2815+ #else
2816+ if (prev_byte == ' \r ' && byte == ' \n ' ) { break ; }
2817+ prev_byte = byte;
2818+ #endif
28102819 }
28112820
28122821 return true ;
@@ -2862,7 +2871,8 @@ inline bool mmap::open(const char *path) {
28622871 // If the following line doesn't compile due to QuadPart, update Windows SDK.
28632872 // See:
28642873 // https://github.com/yhirose/cpp-httplib/issues/1903#issuecomment-2316520721
2865- if (static_cast <ULONGLONG>(size.QuadPart ) > std::numeric_limits<decltype (size_)>::max ()) {
2874+ if (static_cast <ULONGLONG>(size.QuadPart ) >
2875+ std::numeric_limits<decltype (size_)>::max ()) {
28662876 // `size_t` might be 32-bits, on 32-bits Windows.
28672877 return false ;
28682878 }
@@ -4049,7 +4059,22 @@ inline bool read_headers(Stream &strm, Headers &headers) {
40494059 auto end = line_reader.ptr () + line_reader.size () - line_terminator_len;
40504060
40514061 parse_header (line_reader.ptr (), end,
4052- [&](const std::string &key, const std::string &val) {
4062+ [&](const std::string &key, std::string &val) {
4063+ // NOTE: From RFC 9110:
4064+ // Field values containing CR, LF, or NUL characters are
4065+ // invalid and dangerous, due to the varying ways that
4066+ // implementations might parse and interpret those
4067+ // characters; a recipient of CR, LF, or NUL within a field
4068+ // value MUST either reject the message or replace each of
4069+ // those characters with SP before further processing or
4070+ // forwarding of that message.
4071+ for (auto &c : val) {
4072+ switch (c) {
4073+ case ' \0 ' :
4074+ case ' \n ' :
4075+ case ' \r ' : c = ' ' ; break ;
4076+ }
4077+ }
40534078 headers.emplace (key, val);
40544079 });
40554080 }
0 commit comments