@@ -19,6 +19,12 @@ HTTP_Request_Parser::s_settings[1] =
1919 // on_url
2020 +[](::http_parser* ps, const char * str, size_t len)
2121 {
22+ // Append to the the URI.
23+ if (this ->m_headers .estimate_size () + len > this ->m_max_header_length )
24+ POSEIDON_THROW ((
25+ " HTTP header length limit exceeded: `$1` > `$2`" ),
26+ this ->m_headers .estimate_size () + len, this ->m_max_content_length );
27+
2228 this ->m_headers .raw_host .append (str, len);
2329 return 0 ;
2430 },
@@ -36,6 +42,11 @@ HTTP_Request_Parser::s_settings[1] =
3642
3743 // Append the header name to the last key, as this callback might be
3844 // invoked repeatedly.
45+ if (this ->m_headers .estimate_size () + len > this ->m_max_header_length )
46+ POSEIDON_THROW ((
47+ " HTTP header length limit exceeded: `$1` > `$2`" ),
48+ this ->m_headers .estimate_size () + len, this ->m_max_content_length );
49+
3950 this ->m_headers .headers .mut_back ().first .mut_str ().append (str, len);
4051 return 0 ;
4152 },
@@ -44,6 +55,11 @@ HTTP_Request_Parser::s_settings[1] =
4455 +[](::http_parser* ps, const char * str, size_t len)
4556 {
4657 // Append the header value, as this callback might be invoked repeatedly.
58+ if (this ->m_headers .estimate_size () + len > this ->m_max_header_length )
59+ POSEIDON_THROW ((
60+ " HTTP header length limit exceeded: `$1` > `$2`" ),
61+ this ->m_headers .estimate_size () + len, this ->m_max_content_length );
62+
4763 cow_string value = this ->m_headers .headers .back ().second .as_string ();
4864 value.append (str, len);
4965 this ->m_headers .headers .mut_back ().second = move (value);
@@ -195,8 +211,10 @@ HTTP_Request_Parser()
195211 auto conf_file = main_config.copy ();
196212 this ->m_default_compression_level = static_cast <int >(conf_file.get_integer_opt (
197213 &" network.http.default_compression_level" , 0 , 9 ).value_or (6 ));
214+ this ->m_max_header_length = static_cast <uint32_t >(conf_file.get_integer_opt (
215+ &" network.http.max_header_length" , 256 , 16777216 ).value_or (262144 ));
198216 this ->m_max_content_length = static_cast <uint32_t >(conf_file.get_integer_opt (
199- &" network.http.max_request_content_length" , 256 , 16777216 ).value_or (1048576 ));
217+ &" network.http.max_request_content_length" , 256 , 16777216 ).value_or (1048576 ));
200218 }
201219
202220HTTP_Request_Parser::
0 commit comments