|
27 | 27 | #include <cassert> |
28 | 28 | #include <cstdio> |
29 | 29 | #include <cstring> |
| 30 | +#include <string_view> |
30 | 31 | #include "HTTP.h" |
31 | 32 | #include "HdrToken.h" |
32 | 33 | #include "tscore/Diags.h" |
@@ -1261,26 +1262,27 @@ validate_hdr_content_length(HdrHeap *heap, HTTPHdrImpl *hh) |
1261 | 1262 | // recipient MUST treat it as an unrecoverable error. If this is a |
1262 | 1263 | // request message, the server MUST respond with a 400 (Bad Request) |
1263 | 1264 | // status code and then close the connection |
1264 | | - int content_length_len = 0; |
1265 | | - const char *content_length_val = content_length_field->value_get(&content_length_len); |
| 1265 | + std::string_view value = content_length_field->value_get(); |
1266 | 1266 |
|
1267 | | - // RFC 7230 section 3.3.2 |
| 1267 | + // RFC 9110 section 8.6. |
1268 | 1268 | // Content-Length = 1*DIGIT |
1269 | 1269 | // |
| 1270 | + if (value.empty()) { |
| 1271 | + Debug("http", "Content-Length headers don't match the ABNF, returning parse error"); |
| 1272 | + return PARSE_RESULT_ERROR; |
| 1273 | + } |
| 1274 | + |
1270 | 1275 | // If the content-length value contains a non-numeric value, the header is invalid |
1271 | | - for (int i = 0; i < content_length_len; i++) { |
1272 | | - if (!isdigit(content_length_val[i])) { |
1273 | | - Debug("http", "Content-Length value contains non-digit, returning parse error"); |
1274 | | - return PARSE_RESULT_ERROR; |
1275 | | - } |
| 1276 | + if (std::find_if(value.cbegin(), value.cend(), [](std::string_view::value_type c) { return !std::isdigit(c); }) != |
| 1277 | + value.cend()) { |
| 1278 | + Debug("http", "Content-Length value contains non-digit, returning parse error"); |
| 1279 | + return PARSE_RESULT_ERROR; |
1276 | 1280 | } |
1277 | 1281 |
|
1278 | 1282 | while (content_length_field->has_dups()) { |
1279 | | - int content_length_len_2 = 0; |
1280 | | - const char *content_length_val_2 = content_length_field->m_next_dup->value_get(&content_length_len_2); |
| 1283 | + std::string_view value_dup = content_length_field->m_next_dup->value_get(); |
1281 | 1284 |
|
1282 | | - if ((content_length_len != content_length_len_2) || |
1283 | | - (memcmp(content_length_val, content_length_val_2, content_length_len) != 0)) { |
| 1285 | + if ((value.length() != value_dup.length()) || value.compare(value_dup) != 0) { |
1284 | 1286 | // Values are different, parse error |
1285 | 1287 | Debug("http", "Content-Length headers don't match, returning parse error"); |
1286 | 1288 | return PARSE_RESULT_ERROR; |
|
0 commit comments