|
23 | 23 | #include <limits.h> |
24 | 24 | #include <stdio.h> |
25 | 25 | #include <stdlib.h> |
| 26 | +#include <string.h> |
| 27 | +#include <strings.h> |
| 28 | + |
| 29 | +#ifdef WITH_ZLIB |
| 30 | +#include <zlib.h> |
| 31 | +#endif |
26 | 32 |
|
27 | 33 | #define HTTP3_HEADER_FRAME 1 |
28 | 34 | #define HTTP3_DATA_FRAME 0 |
| 35 | +#define HTTP3_MAX_DECOMPRESSED_SIZE (1024 * 1024) |
29 | 36 |
|
30 | 37 | static int _http3_get_expected_data_len(struct http_head *http_head, int *expect_data_len) |
31 | 38 | { |
@@ -56,6 +63,148 @@ static int _http3_get_expected_data_len(struct http_head *http_head, int *expect |
56 | 63 | return 0; |
57 | 64 | } |
58 | 65 |
|
| 66 | +static int _http3_decompress_data(const uint8_t *compressed, int compressed_len, uint8_t **decompressed, |
| 67 | + int *decompressed_len, int is_gzip) |
| 68 | +{ |
| 69 | +#ifdef WITH_ZLIB |
| 70 | + z_stream strm; |
| 71 | + int ret = Z_OK; |
| 72 | + int window_bits = is_gzip ? (15 + 16) : 15; |
| 73 | + int out_size = compressed_len * 4; |
| 74 | + uint8_t *out_buf = NULL; |
| 75 | + |
| 76 | + if (compressed == NULL || compressed_len <= 0 || decompressed == NULL || decompressed_len == NULL) { |
| 77 | + return -2; |
| 78 | + } |
| 79 | + |
| 80 | + if (out_size < 8192) { |
| 81 | + out_size = 8192; |
| 82 | + } |
| 83 | + if (out_size > HTTP3_MAX_DECOMPRESSED_SIZE) { |
| 84 | + out_size = HTTP3_MAX_DECOMPRESSED_SIZE; |
| 85 | + } |
| 86 | + |
| 87 | + out_buf = malloc(out_size); |
| 88 | + if (out_buf == NULL) { |
| 89 | + return -3; |
| 90 | + } |
| 91 | + |
| 92 | + memset(&strm, 0, sizeof(strm)); |
| 93 | + strm.avail_in = compressed_len; |
| 94 | + strm.next_in = (Bytef *)compressed; |
| 95 | + |
| 96 | + ret = inflateInit2(&strm, window_bits); |
| 97 | + if (ret != Z_OK) { |
| 98 | + free(out_buf); |
| 99 | + return -2; |
| 100 | + } |
| 101 | + |
| 102 | + while (ret != Z_STREAM_END) { |
| 103 | + if ((int)strm.total_out == out_size) { |
| 104 | + int new_size = out_size * 2; |
| 105 | + uint8_t *new_buf = NULL; |
| 106 | + |
| 107 | + if (new_size <= out_size || new_size > HTTP3_MAX_DECOMPRESSED_SIZE) { |
| 108 | + inflateEnd(&strm); |
| 109 | + free(out_buf); |
| 110 | + return -3; |
| 111 | + } |
| 112 | + |
| 113 | + new_buf = realloc(out_buf, new_size); |
| 114 | + if (new_buf == NULL) { |
| 115 | + inflateEnd(&strm); |
| 116 | + free(out_buf); |
| 117 | + return -3; |
| 118 | + } |
| 119 | + |
| 120 | + out_buf = new_buf; |
| 121 | + out_size = new_size; |
| 122 | + } |
| 123 | + |
| 124 | + strm.avail_out = out_size - strm.total_out; |
| 125 | + strm.next_out = out_buf + strm.total_out; |
| 126 | + |
| 127 | + ret = inflate(&strm, Z_NO_FLUSH); |
| 128 | + if (ret == Z_STREAM_END) { |
| 129 | + break; |
| 130 | + } |
| 131 | + |
| 132 | + if (ret == Z_BUF_ERROR || (ret == Z_OK && strm.avail_in == 0 && strm.avail_out > 0)) { |
| 133 | + inflateEnd(&strm); |
| 134 | + free(out_buf); |
| 135 | + return -1; |
| 136 | + } |
| 137 | + |
| 138 | + if (ret != Z_OK) { |
| 139 | + inflateEnd(&strm); |
| 140 | + free(out_buf); |
| 141 | + return -2; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + *decompressed = out_buf; |
| 146 | + *decompressed_len = strm.total_out; |
| 147 | + inflateEnd(&strm); |
| 148 | + return 0; |
| 149 | +#else |
| 150 | + return -2; |
| 151 | +#endif |
| 152 | +} |
| 153 | + |
| 154 | +static int _http3_decode_content_encoding(struct http_head *http_head) |
| 155 | +{ |
| 156 | + const char *content_encoding = NULL; |
| 157 | + uint8_t *decompressed = NULL; |
| 158 | + int decompressed_len = 0; |
| 159 | + int is_gzip = 0; |
| 160 | + int ret = 0; |
| 161 | + int data_offset = 0; |
| 162 | + int data_end_offset = 0; |
| 163 | + |
| 164 | + if (http_head == NULL || http_head->data == NULL || http_head->data_len <= 0) { |
| 165 | + return 0; |
| 166 | + } |
| 167 | + |
| 168 | + content_encoding = http_head_get_fields_value(http_head, "content-encoding"); |
| 169 | + if (content_encoding == NULL || content_encoding[0] == '\0') { |
| 170 | + return 0; |
| 171 | + } |
| 172 | + |
| 173 | + is_gzip = strcasecmp(content_encoding, "gzip") == 0; |
| 174 | + if (!is_gzip && strcasecmp(content_encoding, "deflate") != 0) { |
| 175 | + return 0; |
| 176 | + } |
| 177 | + |
| 178 | + ret = _http3_decompress_data(http_head->data, http_head->data_len, &decompressed, &decompressed_len, is_gzip); |
| 179 | + if (ret != 0) { |
| 180 | + return ret; |
| 181 | + } |
| 182 | + |
| 183 | + data_offset = http_head->data - http_head->buff; |
| 184 | + data_end_offset = data_offset + http_head->data_len; |
| 185 | + if (data_offset < 0 || data_offset > http_head->buff_size || decompressed_len > http_head->buff_size - data_offset) { |
| 186 | + free(decompressed); |
| 187 | + return -3; |
| 188 | + } |
| 189 | + |
| 190 | + if (data_end_offset == http_head->buff_len) { |
| 191 | + memcpy(http_head->buff + data_offset, decompressed, decompressed_len); |
| 192 | + http_head->buff_len = data_offset + decompressed_len; |
| 193 | + http_head->data = http_head->buff + data_offset; |
| 194 | + } else { |
| 195 | + if (_http_head_buffer_left_len(http_head) < decompressed_len) { |
| 196 | + free(decompressed); |
| 197 | + return -3; |
| 198 | + } |
| 199 | + http_head->data = _http_head_buffer_get_end(http_head); |
| 200 | + _http_head_buffer_append(http_head, decompressed, decompressed_len); |
| 201 | + } |
| 202 | + http_head->data_len = decompressed_len; |
| 203 | + |
| 204 | + free(decompressed); |
| 205 | + return 0; |
| 206 | +} |
| 207 | + |
59 | 208 | static int _quicvarint_encode(uint64_t value, uint8_t *buffer, int buffer_size) |
60 | 209 | { |
61 | 210 | if (value <= 63) { |
@@ -660,6 +809,11 @@ int http_head_parse_http3_0(struct http_head *http_head, const uint8_t *data, in |
660 | 809 | } |
661 | 810 | } |
662 | 811 |
|
| 812 | + offset_ret = _http3_decode_content_encoding(http_head); |
| 813 | + if (offset_ret < 0) { |
| 814 | + return offset_ret; |
| 815 | + } |
| 816 | + |
663 | 817 | if (offset >= http_head->buff_size) { |
664 | 818 | return -3; |
665 | 819 | } |
|
0 commit comments