2020#include < sstream>
2121#include < unordered_map>
2222#include < string>
23+ #include < algorithm>
2324
2425#ifdef WITH_YAJL
2526#include < yajl/yajl_gen.h>
@@ -225,6 +226,49 @@ RegressionTest *RegressionTest::from_yajl_node(const yajl_val &node) {
225226 return u;
226227}
227228
229+ constexpr char ascii_tolower (char c) {
230+ return ' A' <= c && c <= ' Z' ? (c + (' a' - ' A' )) : c;
231+ }
232+
233+ bool iequals_ascii (std::string_view a, std::string_view b) {
234+ return a.size () == b.size () &&
235+ std::equal (a.begin (), a.end (), b.begin (), b.end (),
236+ [](char x, char y) {
237+ return ascii_tolower (x) == ascii_tolower (y);
238+ });
239+ }
240+
241+ static bool has_chunked_header (const std::vector<std::pair<std::string, std::string>> &headers) {
242+ for (const auto &header : headers) {
243+ if (iequals_ascii (header.first , " Transfer-Encoding" ) && iequals_ascii (header.second , " chunked" )) {
244+ return true ;
245+ }
246+ }
247+ return false ;
248+ }
249+
250+ static void update_content_length (std::vector<std::pair<std::string, std::string>> &headers, size_t length) {
251+ if (has_chunked_header (headers)) {
252+ return ;
253+ }
254+
255+ bool has_content_length = false ;
256+ for (auto &header : headers) {
257+ if (iequals_ascii (header.first , " Content-Length" )) {
258+ header.second = std::to_string (length);
259+ has_content_length = true ;
260+ }
261+ }
262+ if (!has_content_length) {
263+ headers.push_back (std::pair{" Content-Length" , std::to_string (length)});
264+ }
265+ }
266+
267+ void RegressionTest::update_content_lengths () {
268+ update_content_length (request_headers, request_body.size ());
269+ update_content_length (response_headers, response_body.size ());
270+ }
271+
228272RegressionTests *RegressionTests::from_yajl_node (const yajl_val &node) {
229273 RegressionTests *u = new RegressionTests ();
230274 size_t num_tests = node->u .array .len ;
@@ -235,6 +279,12 @@ RegressionTests *RegressionTests::from_yajl_node(const yajl_val &node) {
235279 return u;
236280}
237281
282+ void RegressionTests::update_content_lengths () {
283+ for (auto & test : tests) {
284+ test.update_content_lengths ();
285+ }
286+ }
287+
238288#ifdef WITH_YAJL
239289
240290static yajl_gen_status gen_string_view (yajl_gen g, std::string_view s) {
0 commit comments