Skip to content

Commit be9d3ff

Browse files
maskitzwoop
authored andcommitted
TS-4097: Encode empty string properly with HPACK
This closes #395. (cherry picked from commit 413dd51)
1 parent 74f92f4 commit be9d3ff

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

proxy/http2/HPACK.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ encode_string(uint8_t *buf_start, const uint8_t *buf_end, const char *value, siz
338338

339339
// TODO Choose whether to use Huffman encoding wisely
340340

341-
if (use_huffman) {
341+
if (use_huffman && value_len) {
342342
data = static_cast<char *>(ats_malloc(value_len * 4));
343343
if (data == NULL)
344344
return -1;
@@ -369,8 +369,10 @@ encode_string(uint8_t *buf_start, const uint8_t *buf_end, const char *value, siz
369369
}
370370

371371
// Value
372-
memcpy(p, data, data_len);
373-
p += data_len;
372+
if (data_len) {
373+
memcpy(p, data, data_len);
374+
p += data_len;
375+
}
374376

375377
if (use_huffman) {
376378
ats_free(data);

proxy/http2/RegressionHPACK.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,15 @@ const static struct {
5353
uint32_t raw_string_len;
5454
uint8_t *encoded_field;
5555
int encoded_field_len;
56-
} string_test_case[] = {{(char *)"custom-key", 10, (uint8_t *) "\xA"
56+
} string_test_case[] = {{(char *)"", 0, (uint8_t *) "\x0"
57+
"",
58+
1},
59+
{(char *)"custom-key", 10, (uint8_t *) "\xA"
5760
"custom-key",
5861
11},
62+
{(char *)"", 0, (uint8_t *) "\x80"
63+
"",
64+
1},
5965
{(char *)"custom-key", 10, (uint8_t *) "\x88"
6066
"\x25\xa8\x49\xe9\x5b\xa9\x7d\x7f",
6167
9}};
@@ -232,13 +238,13 @@ REGRESSION_TEST(HPACK_EncodeString)(RegressionTest *t, int, int *pstatus)
232238
int len;
233239

234240
// FIXME Current encoder support only huffman conding.
235-
for (unsigned int i = 1; i < 2; i++) {
241+
for (unsigned int i = 2; i < sizeof(string_test_case) / sizeof(string_test_case[0]); i++) {
236242
memset(buf, 0, BUFSIZE_FOR_REGRESSION_TEST);
237243

238244
len = encode_string(buf, buf + BUFSIZE_FOR_REGRESSION_TEST, string_test_case[i].raw_string, string_test_case[i].raw_string_len);
239245

240246
box.check(len == string_test_case[i].encoded_field_len, "encoded length was %d, expecting %d", len,
241-
integer_test_case[i].encoded_field_len);
247+
string_test_case[i].encoded_field_len);
242248
box.check(len > 0 && memcmp(buf, string_test_case[i].encoded_field, len) == 0, "encoded string was invalid");
243249
}
244250
}

0 commit comments

Comments
 (0)