@@ -2790,6 +2790,10 @@ inline bool stream_line_reader::getline() {
2790
2790
fixed_buffer_used_size_ = 0 ;
2791
2791
glowable_buffer_.clear ();
2792
2792
2793
+ #ifndef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR
2794
+ char prev_byte = 0 ;
2795
+ #endif
2796
+
2793
2797
for (size_t i = 0 ;; i++) {
2794
2798
char byte;
2795
2799
auto n = strm_.read (&byte, 1 );
@@ -2806,7 +2810,12 @@ inline bool stream_line_reader::getline() {
2806
2810
2807
2811
append (byte);
2808
2812
2813
+ #ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR
2809
2814
if (byte == ' \n ' ) { break ; }
2815
+ #else
2816
+ if (prev_byte == ' \r ' && byte == ' \n ' ) { break ; }
2817
+ prev_byte = byte;
2818
+ #endif
2810
2819
}
2811
2820
2812
2821
return true ;
@@ -2862,7 +2871,8 @@ inline bool mmap::open(const char *path) {
2862
2871
// If the following line doesn't compile due to QuadPart, update Windows SDK.
2863
2872
// See:
2864
2873
// 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 ()) {
2866
2876
// `size_t` might be 32-bits, on 32-bits Windows.
2867
2877
return false ;
2868
2878
}
@@ -4049,7 +4059,22 @@ inline bool read_headers(Stream &strm, Headers &headers) {
4049
4059
auto end = line_reader.ptr () + line_reader.size () - line_terminator_len;
4050
4060
4051
4061
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
+ }
4053
4078
headers.emplace (key, val);
4054
4079
});
4055
4080
}
0 commit comments