| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/http/private/h2_frames.h> |
| 7 |
|
| 8 |
#include <aws/compression/huffman.h> |
| 9 |
|
| 10 |
#include <aws/common/logging.h> |
| 11 |
|
| 12 |
#include <aws/io/stream.h> |
| 13 |
|
| 14 |
#include <inttypes.h> |
| 15 |
|
| 16 |
#ifdef _MSC_VER |
| 17 |
# pragma warning(disable : 4204) |
| 18 |
#endif |
| 19 |
|
| 20 |
#define ENCODER_LOGF(level, encoder, text, ...) \ |
| 21 |
AWS_LOGF_##level(AWS_LS_HTTP_ENCODER, "id=%p " text, (encoder)->logging_id, __VA_ARGS__) |
| 22 |
|
| 23 |
#define ENCODER_LOG(level, encoder, text) ENCODER_LOGF(level, encoder, "%s", text) |
| 24 |
|
| 25 |
const struct aws_byte_cursor aws_h2_connection_preface_client_string = |
| 26 |
AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"); |
| 27 |
|
| 28 |
|
| 29 |
const uint32_t aws_h2_settings_initial[AWS_HTTP2_SETTINGS_END_RANGE] = { |
| 30 |
[AWS_HTTP2_SETTINGS_HEADER_TABLE_SIZE] = 4096, |
| 31 |
[AWS_HTTP2_SETTINGS_ENABLE_PUSH] = 1, |
| 32 |
[AWS_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS] = UINT32_MAX, |
| 33 |
[AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE] = AWS_H2_INIT_WINDOW_SIZE, |
| 34 |
[AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE] = 16384, |
| 35 |
[AWS_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE] = UINT32_MAX, |
| 36 |
}; |
| 37 |
|
| 38 |
const uint32_t aws_h2_settings_bounds[AWS_HTTP2_SETTINGS_END_RANGE][2] = { |
| 39 |
[AWS_HTTP2_SETTINGS_HEADER_TABLE_SIZE][0] = 0, |
| 40 |
[AWS_HTTP2_SETTINGS_HEADER_TABLE_SIZE][1] = UINT32_MAX, |
| 41 |
|
| 42 |
[AWS_HTTP2_SETTINGS_ENABLE_PUSH][0] = 0, |
| 43 |
[AWS_HTTP2_SETTINGS_ENABLE_PUSH][1] = 1, |
| 44 |
|
| 45 |
[AWS_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS][0] = 0, |
| 46 |
[AWS_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS][1] = UINT32_MAX, |
| 47 |
|
| 48 |
[AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE][0] = 0, |
| 49 |
[AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE][1] = AWS_H2_WINDOW_UPDATE_MAX, |
| 50 |
|
| 51 |
[AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE][0] = 16384, |
| 52 |
[AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE][1] = AWS_H2_PAYLOAD_MAX, |
| 53 |
|
| 54 |
[AWS_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE][0] = 0, |
| 55 |
[AWS_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE][1] = UINT32_MAX, |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
static const uint32_t s_u32_top_bit_mask = UINT32_MAX << 31; |
| 60 |
|
| 61 |
|
| 62 |
static const size_t s_encoded_header_block_reserve = 128; |
| 63 |
|
| 64 |
#define DEFINE_FRAME_VTABLE(NAME) \ |
| 65 |
static aws_h2_frame_destroy_fn s_frame_##NAME##_destroy; \ |
| 66 |
static aws_h2_frame_encode_fn s_frame_##NAME##_encode; \ |
| 67 |
static const struct aws_h2_frame_vtable s_frame_##NAME##_vtable = { \ |
| 68 |
.destroy = s_frame_##NAME##_destroy, \ |
| 69 |
.encode = s_frame_##NAME##_encode, \ |
| 70 |
} |
| 71 |
|
| 72 |
const char *aws_h2_frame_type_to_str(enum aws_h2_frame_type type) { |
| 73 |
switch (type) { |
| 74 |
case AWS_H2_FRAME_T_DATA: |
| 75 |
return "DATA"; |
| 76 |
case AWS_H2_FRAME_T_HEADERS: |
| 77 |
return "HEADERS"; |
| 78 |
case AWS_H2_FRAME_T_PRIORITY: |
| 79 |
return "PRIORITY"; |
| 80 |
case AWS_H2_FRAME_T_RST_STREAM: |
| 81 |
return "RST_STREAM"; |
| 82 |
case AWS_H2_FRAME_T_SETTINGS: |
| 83 |
return "SETTINGS"; |
| 84 |
case AWS_H2_FRAME_T_PUSH_PROMISE: |
| 85 |
return "PUSH_PROMISE"; |
| 86 |
case AWS_H2_FRAME_T_PING: |
| 87 |
return "PING"; |
| 88 |
case AWS_H2_FRAME_T_GOAWAY: |
| 89 |
return "GOAWAY"; |
| 90 |
case AWS_H2_FRAME_T_WINDOW_UPDATE: |
| 91 |
return "WINDOW_UPDATE"; |
| 92 |
case AWS_H2_FRAME_T_CONTINUATION: |
| 93 |
return "CONTINUATION"; |
| 94 |
default: |
| 95 |
return "**UNKNOWN**"; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
const char *aws_http2_error_code_to_str(enum aws_http2_error_code h2_error_code) { |
| 100 |
switch (h2_error_code) { |
| 101 |
case AWS_HTTP2_ERR_NO_ERROR: |
| 102 |
return "NO_ERROR"; |
| 103 |
case AWS_HTTP2_ERR_PROTOCOL_ERROR: |
| 104 |
return "PROTOCOL_ERROR"; |
| 105 |
case AWS_HTTP2_ERR_INTERNAL_ERROR: |
| 106 |
return "INTERNAL_ERROR"; |
| 107 |
case AWS_HTTP2_ERR_FLOW_CONTROL_ERROR: |
| 108 |
return "FLOW_CONTROL_ERROR"; |
| 109 |
case AWS_HTTP2_ERR_SETTINGS_TIMEOUT: |
| 110 |
return "SETTINGS_TIMEOUT"; |
| 111 |
case AWS_HTTP2_ERR_STREAM_CLOSED: |
| 112 |
return "STREAM_CLOSED"; |
| 113 |
case AWS_HTTP2_ERR_FRAME_SIZE_ERROR: |
| 114 |
return "FRAME_SIZE_ERROR"; |
| 115 |
case AWS_HTTP2_ERR_REFUSED_STREAM: |
| 116 |
return "REFUSED_STREAM"; |
| 117 |
case AWS_HTTP2_ERR_CANCEL: |
| 118 |
return "CANCEL"; |
| 119 |
case AWS_HTTP2_ERR_COMPRESSION_ERROR: |
| 120 |
return "COMPRESSION_ERROR"; |
| 121 |
case AWS_HTTP2_ERR_CONNECT_ERROR: |
| 122 |
return "CONNECT_ERROR"; |
| 123 |
case AWS_HTTP2_ERR_ENHANCE_YOUR_CALM: |
| 124 |
return "ENHANCE_YOUR_CALM"; |
| 125 |
case AWS_HTTP2_ERR_INADEQUATE_SECURITY: |
| 126 |
return "INADEQUATE_SECURITY"; |
| 127 |
case AWS_HTTP2_ERR_HTTP_1_1_REQUIRED: |
| 128 |
return "HTTP_1_1_REQUIRED"; |
| 129 |
default: |
| 130 |
return "UNKNOWN_ERROR"; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
struct aws_h2err aws_h2err_from_h2_code(enum aws_http2_error_code h2_error_code) { |
| 135 |
AWS_PRECONDITION(h2_error_code > AWS_HTTP2_ERR_NO_ERROR && h2_error_code < AWS_HTTP2_ERR_COUNT); |
| 136 |
|
| 137 |
return (struct aws_h2err){ |
| 138 |
.h2_code = h2_error_code, |
| 139 |
.aws_code = AWS_ERROR_HTTP_PROTOCOL_ERROR, |
| 140 |
}; |
| 141 |
} |
| 142 |
|
| 143 |
struct aws_h2err aws_h2err_from_aws_code(int aws_error_code) { |
| 144 |
AWS_PRECONDITION(aws_error_code != 0); |
| 145 |
|
| 146 |
return (struct aws_h2err){ |
| 147 |
.h2_code = AWS_HTTP2_ERR_INTERNAL_ERROR, |
| 148 |
.aws_code = aws_error_code, |
| 149 |
}; |
| 150 |
} |
| 151 |
|
| 152 |
struct aws_h2err aws_h2err_from_last_error(void) { |
| 153 |
return aws_h2err_from_aws_code(aws_last_error()); |
| 154 |
} |
| 155 |
|
| 156 |
bool aws_h2err_success(struct aws_h2err err) { |
| 157 |
return err.h2_code == 0 && err.aws_code == 0; |
| 158 |
} |
| 159 |
|
| 160 |
bool aws_h2err_failed(struct aws_h2err err) { |
| 161 |
return err.h2_code != 0 || err.aws_code != 0; |
| 162 |
} |
| 163 |
|
| 164 |
int aws_h2_validate_stream_id(uint32_t stream_id) { |
| 165 |
if (stream_id == 0 || stream_id > AWS_H2_STREAM_ID_MAX) { |
| 166 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 167 |
} |
| 168 |
return AWS_OP_SUCCESS; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
static int s_get_max_contiguous_payload_length( |
| 182 |
const struct aws_h2_frame_encoder *encoder, |
| 183 |
const struct aws_byte_buf *output, |
| 184 |
size_t *max_payload_length) { |
| 185 |
|
| 186 |
const size_t space_available = output->capacity - output->len; |
| 187 |
|
| 188 |
size_t max_payload_given_space_available; |
| 189 |
if (aws_sub_size_checked(space_available, AWS_H2_FRAME_PREFIX_SIZE, &max_payload_given_space_available)) { |
| 190 |
return aws_raise_error(AWS_ERROR_SHORT_BUFFER); |
| 191 |
} |
| 192 |
|
| 193 |
size_t max_payload_given_settings = encoder->settings.max_frame_size; |
| 194 |
|
| 195 |
*max_payload_length = aws_min_size(max_payload_given_space_available, max_payload_given_settings); |
| 196 |
return AWS_OP_SUCCESS; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
static size_t s_frame_priority_settings_size = 5; |
| 203 |
|
| 204 |
static void s_frame_priority_settings_encode( |
| 205 |
const struct aws_h2_frame_priority_settings *priority, |
| 206 |
struct aws_byte_buf *output) { |
| 207 |
AWS_PRECONDITION(priority); |
| 208 |
AWS_PRECONDITION(output); |
| 209 |
AWS_PRECONDITION((priority->stream_dependency & s_u32_top_bit_mask) == 0); |
| 210 |
(void)s_u32_top_bit_mask; |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
bool writes_ok = true; |
| 220 |
|
| 221 |
|
| 222 |
uint32_t top_bytes = priority->stream_dependency | ((uint32_t)priority->stream_dependency_exclusive << 31); |
| 223 |
writes_ok &= aws_byte_buf_write_be32(output, top_bytes); |
| 224 |
|
| 225 |
|
| 226 |
writes_ok &= aws_byte_buf_write_u8(output, priority->weight); |
| 227 |
|
| 228 |
AWS_ASSERT(writes_ok); |
| 229 |
(void)writes_ok; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
static void s_init_frame_base( |
| 236 |
struct aws_h2_frame *frame_base, |
| 237 |
struct aws_allocator *alloc, |
| 238 |
enum aws_h2_frame_type type, |
| 239 |
const struct aws_h2_frame_vtable *vtable, |
| 240 |
uint32_t stream_id) { |
| 241 |
|
| 242 |
frame_base->vtable = vtable; |
| 243 |
frame_base->alloc = alloc; |
| 244 |
frame_base->type = type; |
| 245 |
frame_base->stream_id = stream_id; |
| 246 |
} |
| 247 |
|
| 248 |
static void s_frame_prefix_encode( |
| 249 |
enum aws_h2_frame_type type, |
| 250 |
uint32_t stream_id, |
| 251 |
size_t length, |
| 252 |
uint8_t flags, |
| 253 |
struct aws_byte_buf *output) { |
| 254 |
AWS_PRECONDITION(output); |
| 255 |
AWS_PRECONDITION(!(stream_id & s_u32_top_bit_mask), "Invalid stream ID"); |
| 256 |
AWS_PRECONDITION(length <= AWS_H2_PAYLOAD_MAX); |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
bool writes_ok = true; |
| 268 |
|
| 269 |
|
| 270 |
writes_ok &= aws_byte_buf_write_be24(output, (uint32_t)length); |
| 271 |
|
| 272 |
|
| 273 |
writes_ok &= aws_byte_buf_write_u8(output, type); |
| 274 |
|
| 275 |
|
| 276 |
writes_ok &= aws_byte_buf_write_u8(output, flags); |
| 277 |
|
| 278 |
|
| 279 |
writes_ok &= aws_byte_buf_write_be32(output, stream_id); |
| 280 |
|
| 281 |
AWS_ASSERT(writes_ok); |
| 282 |
(void)writes_ok; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
int aws_h2_frame_encoder_init( |
| 289 |
struct aws_h2_frame_encoder *encoder, |
| 290 |
struct aws_allocator *allocator, |
| 291 |
const void *logging_id) { |
| 292 |
|
| 293 |
AWS_PRECONDITION(encoder); |
| 294 |
AWS_PRECONDITION(allocator); |
| 295 |
|
| 296 |
AWS_ZERO_STRUCT(*encoder); |
| 297 |
encoder->allocator = allocator; |
| 298 |
encoder->logging_id = logging_id; |
| 299 |
|
| 300 |
aws_hpack_encoder_init(&encoder->hpack, allocator, logging_id); |
| 301 |
|
| 302 |
encoder->settings.max_frame_size = aws_h2_settings_initial[AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE]; |
| 303 |
return AWS_OP_SUCCESS; |
| 304 |
} |
| 305 |
void aws_h2_frame_encoder_clean_up(struct aws_h2_frame_encoder *encoder) { |
| 306 |
AWS_PRECONDITION(encoder); |
| 307 |
|
| 308 |
aws_hpack_encoder_clean_up(&encoder->hpack); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
int aws_h2_encode_data_frame( |
| 315 |
struct aws_h2_frame_encoder *encoder, |
| 316 |
uint32_t stream_id, |
| 317 |
struct aws_input_stream *body_stream, |
| 318 |
bool body_ends_stream, |
| 319 |
uint8_t pad_length, |
| 320 |
int32_t *stream_window_size_peer, |
| 321 |
uint32_t *connection_window_size_peer, |
| 322 |
struct aws_byte_buf *output, |
| 323 |
bool *body_complete, |
| 324 |
bool *body_stalled, |
| 325 |
bool *body_failed) { |
| 326 |
|
| 327 |
AWS_PRECONDITION(encoder); |
| 328 |
AWS_PRECONDITION(body_stream); |
| 329 |
AWS_PRECONDITION(output); |
| 330 |
AWS_PRECONDITION(body_complete); |
| 331 |
AWS_PRECONDITION(body_stalled); |
| 332 |
AWS_PRECONDITION(body_failed); |
| 333 |
AWS_PRECONDITION(*stream_window_size_peer > 0); |
| 334 |
|
| 335 |
if (aws_h2_validate_stream_id(stream_id)) { |
| 336 |
return AWS_OP_ERR; |
| 337 |
} |
| 338 |
|
| 339 |
*body_complete = false; |
| 340 |
*body_stalled = false; |
| 341 |
*body_failed = false; |
| 342 |
uint8_t flags = 0; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
size_t bytes_preceding_body = AWS_H2_FRAME_PREFIX_SIZE; |
| 353 |
size_t payload_overhead = 0; |
| 354 |
if (pad_length > 0) { |
| 355 |
flags |= AWS_H2_FRAME_F_PADDED; |
| 356 |
|
| 357 |
|
| 358 |
bytes_preceding_body += 1; |
| 359 |
payload_overhead = 1 + pad_length; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
size_t min_window_size = aws_min_size(*stream_window_size_peer, *connection_window_size_peer); |
| 364 |
|
| 365 |
|
| 366 |
size_t max_payload; |
| 367 |
if (s_get_max_contiguous_payload_length(encoder, output, &max_payload)) { |
| 368 |
goto handle_waiting_for_more_space; |
| 369 |
} |
| 370 |
|
| 371 |
max_payload = aws_min_size(max_payload, min_window_size); |
| 372 |
|
| 373 |
size_t max_body; |
| 374 |
if (aws_sub_size_checked(max_payload, payload_overhead, &max_body) || max_body == 0) { |
| 375 |
goto handle_waiting_for_more_space; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
struct aws_byte_buf body_sub_buf = |
| 380 |
aws_byte_buf_from_empty_array(output->buffer + output->len + bytes_preceding_body, max_body); |
| 381 |
|
| 382 |
if (aws_input_stream_read(body_stream, &body_sub_buf)) { |
| 383 |
*body_failed = true; |
| 384 |
goto error; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
struct aws_stream_status body_status; |
| 389 |
if (aws_input_stream_get_status(body_stream, &body_status)) { |
| 390 |
*body_failed = true; |
| 391 |
goto error; |
| 392 |
} |
| 393 |
|
| 394 |
if (body_status.is_end_of_stream) { |
| 395 |
*body_complete = true; |
| 396 |
if (body_ends_stream) { |
| 397 |
flags |= AWS_H2_FRAME_F_END_STREAM; |
| 398 |
} |
| 399 |
} else { |
| 400 |
if (body_sub_buf.len < body_sub_buf.capacity) { |
| 401 |
|
| 402 |
*body_stalled = true; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
if (body_sub_buf.len == 0 && !(flags & AWS_H2_FRAME_F_END_STREAM)) { |
| 407 |
|
| 408 |
goto handle_nothing_to_send_right_now; |
| 409 |
} |
| 410 |
|
| 411 |
ENCODER_LOGF( |
| 412 |
TRACE, |
| 413 |
encoder, |
| 414 |
"Encoding frame type=DATA stream_id=%" PRIu32 " data_len=%zu stalled=%d%s", |
| 415 |
stream_id, |
| 416 |
body_sub_buf.len, |
| 417 |
*body_stalled, |
| 418 |
(flags & AWS_H2_FRAME_F_END_STREAM) ? " END_STREAM" : ""); |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
bool writes_ok = true; |
| 424 |
|
| 425 |
|
| 426 |
const size_t payload_len = body_sub_buf.len + payload_overhead; |
| 427 |
s_frame_prefix_encode(AWS_H2_FRAME_T_DATA, stream_id, payload_len, flags, output); |
| 428 |
|
| 429 |
|
| 430 |
if (flags & AWS_H2_FRAME_F_PADDED) { |
| 431 |
writes_ok &= aws_byte_buf_write_u8(output, pad_length); |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
AWS_ASSERT(output->buffer + output->len == body_sub_buf.buffer && "Streamed DATA to wrong position"); |
| 436 |
output->len += body_sub_buf.len; |
| 437 |
|
| 438 |
|
| 439 |
if (flags & AWS_H2_FRAME_F_PADDED) { |
| 440 |
writes_ok &= aws_byte_buf_write_u8_n(output, 0, pad_length); |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
AWS_ASSERT(payload_len <= min_window_size); |
| 445 |
*connection_window_size_peer -= (uint32_t)payload_len; |
| 446 |
*stream_window_size_peer -= (int32_t)payload_len; |
| 447 |
|
| 448 |
AWS_ASSERT(writes_ok); |
| 449 |
(void)writes_ok; |
| 450 |
return AWS_OP_SUCCESS; |
| 451 |
|
| 452 |
handle_waiting_for_more_space: |
| 453 |
ENCODER_LOGF(TRACE, encoder, "Insufficient space to encode DATA for stream %" PRIu32 " right now", stream_id); |
| 454 |
return AWS_OP_SUCCESS; |
| 455 |
|
| 456 |
handle_nothing_to_send_right_now: |
| 457 |
ENCODER_LOGF(INFO, encoder, "Stream %" PRIu32 " produced 0 bytes of body data", stream_id); |
| 458 |
return AWS_OP_SUCCESS; |
| 459 |
|
| 460 |
error: |
| 461 |
return AWS_OP_ERR; |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
DEFINE_FRAME_VTABLE(headers); |
| 468 |
|
| 469 |
|
| 470 |
struct aws_h2_frame_headers { |
| 471 |
struct aws_h2_frame base; |
| 472 |
|
| 473 |
|
| 474 |
const struct aws_http_headers *headers; |
| 475 |
uint8_t pad_length; |
| 476 |
|
| 477 |
|
| 478 |
bool end_stream; |
| 479 |
bool has_priority; |
| 480 |
struct aws_h2_frame_priority_settings priority; |
| 481 |
|
| 482 |
|
| 483 |
uint32_t promised_stream_id; |
| 484 |
|
| 485 |
|
| 486 |
enum { |
| 487 |
AWS_H2_HEADERS_STATE_INIT, |
| 488 |
AWS_H2_HEADERS_STATE_FIRST_FRAME, |
| 489 |
AWS_H2_HEADERS_STATE_CONTINUATION, |
| 490 |
AWS_H2_HEADERS_STATE_COMPLETE, |
| 491 |
} state; |
| 492 |
|
| 493 |
struct aws_byte_buf whole_encoded_header_block; |
| 494 |
struct aws_byte_cursor header_block_cursor; |
| 495 |
}; |
| 496 |
|
| 497 |
static struct aws_h2_frame *s_frame_new_headers_or_push_promise( |
| 498 |
struct aws_allocator *allocator, |
| 499 |
enum aws_h2_frame_type frame_type, |
| 500 |
uint32_t stream_id, |
| 501 |
const struct aws_http_headers *headers, |
| 502 |
uint8_t pad_length, |
| 503 |
bool end_stream, |
| 504 |
const struct aws_h2_frame_priority_settings *optional_priority, |
| 505 |
uint32_t promised_stream_id) { |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
AWS_PRECONDITION(allocator); |
| 510 |
AWS_PRECONDITION(frame_type == AWS_H2_FRAME_T_HEADERS || frame_type == AWS_H2_FRAME_T_PUSH_PROMISE); |
| 511 |
AWS_PRECONDITION(headers); |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
if (aws_h2_validate_stream_id(stream_id)) { |
| 516 |
return NULL; |
| 517 |
} |
| 518 |
|
| 519 |
if (frame_type == AWS_H2_FRAME_T_PUSH_PROMISE) { |
| 520 |
if (aws_h2_validate_stream_id(promised_stream_id)) { |
| 521 |
return NULL; |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
if (optional_priority && aws_h2_validate_stream_id(optional_priority->stream_dependency)) { |
| 526 |
return NULL; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
struct aws_h2_frame_headers *frame = aws_mem_calloc(allocator, 1, sizeof(struct aws_h2_frame_headers)); |
| 532 |
if (!frame) { |
| 533 |
return NULL; |
| 534 |
} |
| 535 |
|
| 536 |
if (aws_byte_buf_init(&frame->whole_encoded_header_block, allocator, s_encoded_header_block_reserve)) { |
| 537 |
goto error; |
| 538 |
} |
| 539 |
|
| 540 |
if (frame_type == AWS_H2_FRAME_T_HEADERS) { |
| 541 |
frame->end_stream = end_stream; |
| 542 |
if (optional_priority) { |
| 543 |
frame->has_priority = true; |
| 544 |
frame->priority = *optional_priority; |
| 545 |
} |
| 546 |
} else { |
| 547 |
frame->promised_stream_id = promised_stream_id; |
| 548 |
} |
| 549 |
|
| 550 |
s_init_frame_base(&frame->base, allocator, frame_type, &s_frame_headers_vtable, stream_id); |
| 551 |
|
| 552 |
aws_http_headers_acquire((struct aws_http_headers *)headers); |
| 553 |
frame->headers = headers; |
| 554 |
frame->pad_length = pad_length; |
| 555 |
|
| 556 |
return &frame->base; |
| 557 |
|
| 558 |
error: |
| 559 |
s_frame_headers_destroy(&frame->base); |
| 560 |
return NULL; |
| 561 |
} |
| 562 |
|
| 563 |
struct aws_h2_frame *aws_h2_frame_new_headers( |
| 564 |
struct aws_allocator *allocator, |
| 565 |
uint32_t stream_id, |
| 566 |
const struct aws_http_headers *headers, |
| 567 |
bool end_stream, |
| 568 |
uint8_t pad_length, |
| 569 |
const struct aws_h2_frame_priority_settings *optional_priority) { |
| 570 |
|
| 571 |
return s_frame_new_headers_or_push_promise( |
| 572 |
allocator, |
| 573 |
AWS_H2_FRAME_T_HEADERS, |
| 574 |
stream_id, |
| 575 |
headers, |
| 576 |
pad_length, |
| 577 |
end_stream, |
| 578 |
optional_priority, |
| 579 |
0 ); |
| 580 |
} |
| 581 |
|
| 582 |
struct aws_h2_frame *aws_h2_frame_new_push_promise( |
| 583 |
struct aws_allocator *allocator, |
| 584 |
uint32_t stream_id, |
| 585 |
uint32_t promised_stream_id, |
| 586 |
const struct aws_http_headers *headers, |
| 587 |
uint8_t pad_length) { |
| 588 |
|
| 589 |
return s_frame_new_headers_or_push_promise( |
| 590 |
allocator, |
| 591 |
AWS_H2_FRAME_T_PUSH_PROMISE, |
| 592 |
stream_id, |
| 593 |
headers, |
| 594 |
pad_length, |
| 595 |
false , |
| 596 |
NULL , |
| 597 |
promised_stream_id); |
| 598 |
} |
| 599 |
|
| 600 |
static void s_frame_headers_destroy(struct aws_h2_frame *frame_base) { |
| 601 |
struct aws_h2_frame_headers *frame = AWS_CONTAINER_OF(frame_base, struct aws_h2_frame_headers, base); |
| 602 |
aws_http_headers_release((struct aws_http_headers *)frame->headers); |
| 603 |
aws_byte_buf_clean_up(&frame->whole_encoded_header_block); |
| 604 |
aws_mem_release(frame->base.alloc, frame); |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
static void s_encode_single_header_block_frame( |
| 609 |
struct aws_h2_frame_headers *frame, |
| 610 |
struct aws_h2_frame_encoder *encoder, |
| 611 |
struct aws_byte_buf *output, |
| 612 |
bool *waiting_for_more_space) { |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
enum aws_h2_frame_type frame_type; |
| 621 |
uint8_t flags = 0; |
| 622 |
uint8_t pad_length = 0; |
| 623 |
const struct aws_h2_frame_priority_settings *priority_settings = NULL; |
| 624 |
const uint32_t *promised_stream_id = NULL; |
| 625 |
size_t payload_overhead = 0; |
| 626 |
|
| 627 |
if (frame->state == AWS_H2_HEADERS_STATE_FIRST_FRAME) { |
| 628 |
frame_type = frame->base.type; |
| 629 |
|
| 630 |
if (frame->pad_length > 0) { |
| 631 |
flags |= AWS_H2_FRAME_F_PADDED; |
| 632 |
pad_length = frame->pad_length; |
| 633 |
payload_overhead += 1 + pad_length; |
| 634 |
} |
| 635 |
|
| 636 |
if (frame->has_priority) { |
| 637 |
priority_settings = &frame->priority; |
| 638 |
flags |= AWS_H2_FRAME_F_PRIORITY; |
| 639 |
payload_overhead += s_frame_priority_settings_size; |
| 640 |
} |
| 641 |
|
| 642 |
if (frame->end_stream) { |
| 643 |
flags |= AWS_H2_FRAME_F_END_STREAM; |
| 644 |
} |
| 645 |
|
| 646 |
if (frame_type == AWS_H2_FRAME_T_PUSH_PROMISE) { |
| 647 |
promised_stream_id = &frame->promised_stream_id; |
| 648 |
payload_overhead += 4; |
| 649 |
} |
| 650 |
|
| 651 |
} else { |
| 652 |
frame_type = AWS_H2_FRAME_T_CONTINUATION; |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
size_t max_payload; |
| 660 |
if (s_get_max_contiguous_payload_length(encoder, output, &max_payload)) { |
| 661 |
goto handle_waiting_for_more_space; |
| 662 |
} |
| 663 |
|
| 664 |
size_t max_fragment; |
| 665 |
if (aws_sub_size_checked(max_payload, payload_overhead, &max_fragment)) { |
| 666 |
goto handle_waiting_for_more_space; |
| 667 |
} |
| 668 |
|
| 669 |
const size_t fragment_len = aws_min_size(max_fragment, frame->header_block_cursor.len); |
| 670 |
if (fragment_len == frame->header_block_cursor.len) { |
| 671 |
|
| 672 |
flags |= AWS_H2_FRAME_F_END_HEADERS; |
| 673 |
} else { |
| 674 |
|
| 675 |
const size_t even_worth_sending_threshold = AWS_H2_FRAME_PREFIX_SIZE + payload_overhead; |
| 676 |
if (fragment_len < even_worth_sending_threshold) { |
| 677 |
goto handle_waiting_for_more_space; |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
ENCODER_LOGF( |
| 685 |
TRACE, |
| 686 |
encoder, |
| 687 |
"Encoding frame type=%s stream_id=%" PRIu32 "%s%s", |
| 688 |
aws_h2_frame_type_to_str(frame_type), |
| 689 |
frame->base.stream_id, |
| 690 |
(flags & AWS_H2_FRAME_F_END_HEADERS) ? " END_HEADERS" : "", |
| 691 |
(flags & AWS_H2_FRAME_F_END_STREAM) ? " END_STREAM" : ""); |
| 692 |
|
| 693 |
bool writes_ok = true; |
| 694 |
|
| 695 |
|
| 696 |
const size_t payload_len = fragment_len + payload_overhead; |
| 697 |
s_frame_prefix_encode(frame_type, frame->base.stream_id, payload_len, flags, output); |
| 698 |
|
| 699 |
|
| 700 |
if (flags & AWS_H2_FRAME_F_PADDED) { |
| 701 |
AWS_ASSERT(frame_type != AWS_H2_FRAME_T_CONTINUATION); |
| 702 |
writes_ok &= aws_byte_buf_write_u8(output, pad_length); |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
if (flags & AWS_H2_FRAME_F_PRIORITY) { |
| 707 |
AWS_ASSERT(frame_type == AWS_H2_FRAME_T_HEADERS); |
| 708 |
s_frame_priority_settings_encode(priority_settings, output); |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
if (promised_stream_id) { |
| 713 |
AWS_ASSERT(frame_type == AWS_H2_FRAME_T_PUSH_PROMISE); |
| 714 |
writes_ok &= aws_byte_buf_write_be32(output, *promised_stream_id); |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
if (fragment_len > 0) { |
| 719 |
struct aws_byte_cursor fragment = aws_byte_cursor_advance(&frame->header_block_cursor, fragment_len); |
| 720 |
writes_ok &= aws_byte_buf_write_from_whole_cursor(output, fragment); |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
if (flags & AWS_H2_FRAME_F_PADDED) { |
| 725 |
writes_ok &= aws_byte_buf_write_u8_n(output, 0, pad_length); |
| 726 |
} |
| 727 |
|
| 728 |
AWS_ASSERT(writes_ok); |
| 729 |
(void)writes_ok; |
| 730 |
|
| 731 |
|
| 732 |
frame->state = |
| 733 |
flags & AWS_H2_FRAME_F_END_HEADERS ? AWS_H2_HEADERS_STATE_COMPLETE : AWS_H2_HEADERS_STATE_CONTINUATION; |
| 734 |
*waiting_for_more_space = false; |
| 735 |
return; |
| 736 |
|
| 737 |
handle_waiting_for_more_space: |
| 738 |
ENCODER_LOGF( |
| 739 |
TRACE, |
| 740 |
encoder, |
| 741 |
"Insufficient space to encode %s for stream %" PRIu32 " right now", |
| 742 |
aws_h2_frame_type_to_str(frame->base.type), |
| 743 |
frame->base.stream_id); |
| 744 |
*waiting_for_more_space = true; |
| 745 |
} |
| 746 |
|
| 747 |
static int s_frame_headers_encode( |
| 748 |
struct aws_h2_frame *frame_base, |
| 749 |
struct aws_h2_frame_encoder *encoder, |
| 750 |
struct aws_byte_buf *output, |
| 751 |
bool *complete) { |
| 752 |
|
| 753 |
struct aws_h2_frame_headers *frame = AWS_CONTAINER_OF(frame_base, struct aws_h2_frame_headers, base); |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
if (frame->state == AWS_H2_HEADERS_STATE_INIT) { |
| 758 |
if (aws_hpack_encode_header_block(&encoder->hpack, frame->headers, &frame->whole_encoded_header_block)) { |
| 759 |
ENCODER_LOGF( |
| 760 |
ERROR, |
| 761 |
encoder, |
| 762 |
"Error doing HPACK encoding on %s of stream %" PRIu32 ": %s", |
| 763 |
aws_h2_frame_type_to_str(frame->base.type), |
| 764 |
frame->base.stream_id, |
| 765 |
aws_error_name(aws_last_error())); |
| 766 |
goto error; |
| 767 |
} |
| 768 |
|
| 769 |
frame->header_block_cursor = aws_byte_cursor_from_buf(&frame->whole_encoded_header_block); |
| 770 |
frame->state = AWS_H2_HEADERS_STATE_FIRST_FRAME; |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
bool waiting_for_more_space = false; |
| 776 |
while (frame->state < AWS_H2_HEADERS_STATE_COMPLETE && !waiting_for_more_space) { |
| 777 |
s_encode_single_header_block_frame(frame, encoder, output, &waiting_for_more_space); |
| 778 |
} |
| 779 |
|
| 780 |
*complete = frame->state == AWS_H2_HEADERS_STATE_COMPLETE; |
| 781 |
return AWS_OP_SUCCESS; |
| 782 |
|
| 783 |
error: |
| 784 |
return AWS_OP_ERR; |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
struct aws_h2_frame_prebuilt { |
| 795 |
struct aws_h2_frame base; |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
struct aws_byte_buf encoded_buf; |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
struct aws_byte_cursor cursor; |
| 804 |
}; |
| 805 |
|
| 806 |
DEFINE_FRAME_VTABLE(prebuilt); |
| 807 |
|
| 808 |
|
| 809 |
static size_t s_prebuilt_payload_max(void) { |
| 810 |
return aws_h2_settings_bounds[AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE][0]; |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
static struct aws_h2_frame_prebuilt *s_h2_frame_new_prebuilt( |
| 816 |
struct aws_allocator *allocator, |
| 817 |
enum aws_h2_frame_type type, |
| 818 |
uint32_t stream_id, |
| 819 |
size_t payload_len, |
| 820 |
uint8_t flags) { |
| 821 |
|
| 822 |
AWS_PRECONDITION(payload_len <= s_prebuilt_payload_max()); |
| 823 |
|
| 824 |
const size_t encoded_frame_len = AWS_H2_FRAME_PREFIX_SIZE + payload_len; |
| 825 |
|
| 826 |
|
| 827 |
struct aws_h2_frame_prebuilt *frame; |
| 828 |
void *storage; |
| 829 |
if (!aws_mem_acquire_many( |
| 830 |
allocator, 2, &frame, sizeof(struct aws_h2_frame_prebuilt), &storage, encoded_frame_len)) { |
| 831 |
return NULL; |
| 832 |
} |
| 833 |
|
| 834 |
AWS_ZERO_STRUCT(*frame); |
| 835 |
s_init_frame_base(&frame->base, allocator, type, &s_frame_prebuilt_vtable, stream_id); |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
frame->encoded_buf = aws_byte_buf_from_empty_array(storage, encoded_frame_len); |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
frame->cursor = aws_byte_cursor_from_array(storage, encoded_frame_len); |
| 845 |
|
| 846 |
|
| 847 |
s_frame_prefix_encode(type, stream_id, payload_len, flags, &frame->encoded_buf); |
| 848 |
|
| 849 |
return frame; |
| 850 |
} |
| 851 |
|
| 852 |
static void s_frame_prebuilt_destroy(struct aws_h2_frame *frame_base) { |
| 853 |
aws_mem_release(frame_base->alloc, frame_base); |
| 854 |
} |
| 855 |
|
| 856 |
static int s_frame_prebuilt_encode( |
| 857 |
struct aws_h2_frame *frame_base, |
| 858 |
struct aws_h2_frame_encoder *encoder, |
| 859 |
struct aws_byte_buf *output, |
| 860 |
bool *complete) { |
| 861 |
|
| 862 |
(void)encoder; |
| 863 |
struct aws_h2_frame_prebuilt *frame = AWS_CONTAINER_OF(frame_base, struct aws_h2_frame_prebuilt, base); |
| 864 |
|
| 865 |
|
| 866 |
AWS_ASSERT(frame->encoded_buf.len == frame->encoded_buf.capacity); |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
if (frame->cursor.len == frame->encoded_buf.len) { |
| 871 |
|
| 872 |
ENCODER_LOGF( |
| 873 |
TRACE, |
| 874 |
encoder, |
| 875 |
"Encoding frame type=%s stream_id=%" PRIu32, |
| 876 |
aws_h2_frame_type_to_str(frame->base.type), |
| 877 |
frame->base.stream_id); |
| 878 |
} else { |
| 879 |
|
| 880 |
ENCODER_LOGF( |
| 881 |
TRACE, |
| 882 |
encoder, |
| 883 |
"Resume encoding frame type=%s stream_id=%" PRIu32, |
| 884 |
aws_h2_frame_type_to_str(frame->base.type), |
| 885 |
frame->base.stream_id); |
| 886 |
} |
| 887 |
|
| 888 |
bool writes_ok = true; |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
size_t chunk_len = aws_min_size(frame->cursor.len, output->capacity - output->len); |
| 893 |
struct aws_byte_cursor chunk = aws_byte_cursor_advance(&frame->cursor, chunk_len); |
| 894 |
writes_ok &= aws_byte_buf_write_from_whole_cursor(output, chunk); |
| 895 |
AWS_ASSERT(writes_ok); |
| 896 |
(void)writes_ok; |
| 897 |
|
| 898 |
if (frame->cursor.len == 0) { |
| 899 |
*complete = true; |
| 900 |
} else { |
| 901 |
ENCODER_LOGF( |
| 902 |
TRACE, |
| 903 |
encoder, |
| 904 |
"Incomplete encoding of frame type=%s stream_id=%" PRIu32 ", will resume later...", |
| 905 |
aws_h2_frame_type_to_str(frame->base.type), |
| 906 |
frame->base.stream_id); |
| 907 |
|
| 908 |
*complete = false; |
| 909 |
} |
| 910 |
return AWS_OP_SUCCESS; |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
struct aws_h2_frame *aws_h2_frame_new_priority( |
| 917 |
struct aws_allocator *allocator, |
| 918 |
uint32_t stream_id, |
| 919 |
const struct aws_h2_frame_priority_settings *priority) { |
| 920 |
|
| 921 |
AWS_PRECONDITION(allocator); |
| 922 |
AWS_PRECONDITION(priority); |
| 923 |
|
| 924 |
if (aws_h2_validate_stream_id(stream_id) || aws_h2_validate_stream_id(priority->stream_dependency)) { |
| 925 |
return NULL; |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
const uint8_t flags = 0; |
| 930 |
const size_t payload_len = s_frame_priority_settings_size; |
| 931 |
|
| 932 |
struct aws_h2_frame_prebuilt *frame = |
| 933 |
s_h2_frame_new_prebuilt(allocator, AWS_H2_FRAME_T_PRIORITY, stream_id, payload_len, flags); |
| 934 |
if (!frame) { |
| 935 |
return NULL; |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
s_frame_priority_settings_encode(priority, &frame->encoded_buf); |
| 940 |
|
| 941 |
return &frame->base; |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
static const size_t s_frame_rst_stream_length = 4; |
| 948 |
|
| 949 |
struct aws_h2_frame *aws_h2_frame_new_rst_stream( |
| 950 |
struct aws_allocator *allocator, |
| 951 |
uint32_t stream_id, |
| 952 |
uint32_t error_code) { |
| 953 |
|
| 954 |
if (aws_h2_validate_stream_id(stream_id)) { |
| 955 |
return NULL; |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
const uint8_t flags = 0; |
| 960 |
const size_t payload_len = s_frame_rst_stream_length; |
| 961 |
|
| 962 |
struct aws_h2_frame_prebuilt *frame = |
| 963 |
s_h2_frame_new_prebuilt(allocator, AWS_H2_FRAME_T_RST_STREAM, stream_id, payload_len, flags); |
| 964 |
if (!frame) { |
| 965 |
return NULL; |
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
bool writes_ok = true; |
| 974 |
writes_ok &= aws_byte_buf_write_be32(&frame->encoded_buf, error_code); |
| 975 |
AWS_ASSERT(writes_ok); |
| 976 |
(void)writes_ok; |
| 977 |
|
| 978 |
return &frame->base; |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
static const size_t s_frame_setting_length = 6; |
| 985 |
|
| 986 |
struct aws_h2_frame *aws_h2_frame_new_settings( |
| 987 |
struct aws_allocator *allocator, |
| 988 |
const struct aws_http2_setting *settings_array, |
| 989 |
size_t num_settings, |
| 990 |
bool ack) { |
| 991 |
|
| 992 |
AWS_PRECONDITION(settings_array || num_settings == 0); |
| 993 |
|
| 994 |
|
| 995 |
if (ack && num_settings > 0) { |
| 996 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 997 |
return NULL; |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
const size_t max_settings = s_prebuilt_payload_max() / s_frame_setting_length; |
| 1002 |
if (num_settings > max_settings) { |
| 1003 |
AWS_LOGF_ERROR( |
| 1004 |
AWS_LS_HTTP_ENCODER, |
| 1005 |
"Cannot create SETTINGS frame with %zu settings, the limit is %zu.", |
| 1006 |
num_settings, |
| 1007 |
max_settings); |
| 1008 |
|
| 1009 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 1010 |
return NULL; |
| 1011 |
} |
| 1012 |
|
| 1013 |
|
| 1014 |
const uint8_t flags = ack ? AWS_H2_FRAME_F_ACK : 0; |
| 1015 |
const size_t payload_len = num_settings * s_frame_setting_length; |
| 1016 |
const uint32_t stream_id = 0; |
| 1017 |
|
| 1018 |
struct aws_h2_frame_prebuilt *frame = |
| 1019 |
s_h2_frame_new_prebuilt(allocator, AWS_H2_FRAME_T_SETTINGS, stream_id, payload_len, flags); |
| 1020 |
if (!frame) { |
| 1021 |
return NULL; |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
bool writes_ok = true; |
| 1032 |
for (size_t i = 0; i < num_settings; ++i) { |
| 1033 |
writes_ok &= aws_byte_buf_write_be16(&frame->encoded_buf, settings_array[i].id); |
| 1034 |
writes_ok &= aws_byte_buf_write_be32(&frame->encoded_buf, settings_array[i].value); |
| 1035 |
} |
| 1036 |
AWS_ASSERT(writes_ok); |
| 1037 |
(void)writes_ok; |
| 1038 |
|
| 1039 |
return &frame->base; |
| 1040 |
} |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
struct aws_h2_frame *aws_h2_frame_new_ping( |
| 1046 |
struct aws_allocator *allocator, |
| 1047 |
bool ack, |
| 1048 |
const uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE]) { |
| 1049 |
|
| 1050 |
|
| 1051 |
const uint8_t flags = ack ? AWS_H2_FRAME_F_ACK : 0; |
| 1052 |
const size_t payload_len = AWS_HTTP2_PING_DATA_SIZE; |
| 1053 |
const uint32_t stream_id = 0; |
| 1054 |
|
| 1055 |
struct aws_h2_frame_prebuilt *frame = |
| 1056 |
s_h2_frame_new_prebuilt(allocator, AWS_H2_FRAME_T_PING, stream_id, payload_len, flags); |
| 1057 |
if (!frame) { |
| 1058 |
return NULL; |
| 1059 |
} |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
bool writes_ok = true; |
| 1069 |
writes_ok &= aws_byte_buf_write(&frame->encoded_buf, opaque_data, AWS_HTTP2_PING_DATA_SIZE); |
| 1070 |
AWS_ASSERT(writes_ok); |
| 1071 |
(void)writes_ok; |
| 1072 |
|
| 1073 |
|
| 1074 |
frame->base.high_priority = ack; |
| 1075 |
return &frame->base; |
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
static const size_t s_frame_goaway_length_min = 8; |
| 1082 |
|
| 1083 |
struct aws_h2_frame *aws_h2_frame_new_goaway( |
| 1084 |
struct aws_allocator *allocator, |
| 1085 |
uint32_t last_stream_id, |
| 1086 |
uint32_t error_code, |
| 1087 |
struct aws_byte_cursor debug_data) { |
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
const size_t debug_data_max = s_prebuilt_payload_max() - s_frame_goaway_length_min; |
| 1092 |
if (debug_data.len > debug_data_max) { |
| 1093 |
AWS_LOGF_WARN( |
| 1094 |
AWS_LS_HTTP_ENCODER, |
| 1095 |
"Sending GOAWAY without debug-data. Debug-data size %zu exceeds internal limit of %zu", |
| 1096 |
debug_data.len, |
| 1097 |
debug_data_max); |
| 1098 |
|
| 1099 |
debug_data.len = 0; |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
AWS_FATAL_ASSERT(last_stream_id <= AWS_H2_STREAM_ID_MAX); |
| 1104 |
|
| 1105 |
|
| 1106 |
const uint8_t flags = 0; |
| 1107 |
const size_t payload_len = debug_data.len + s_frame_goaway_length_min; |
| 1108 |
const uint32_t stream_id = 0; |
| 1109 |
|
| 1110 |
struct aws_h2_frame_prebuilt *frame = |
| 1111 |
s_h2_frame_new_prebuilt(allocator, AWS_H2_FRAME_T_GOAWAY, stream_id, payload_len, flags); |
| 1112 |
if (!frame) { |
| 1113 |
return NULL; |
| 1114 |
} |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
bool writes_ok = true; |
| 1126 |
writes_ok &= aws_byte_buf_write_be32(&frame->encoded_buf, last_stream_id); |
| 1127 |
writes_ok &= aws_byte_buf_write_be32(&frame->encoded_buf, error_code); |
| 1128 |
writes_ok &= aws_byte_buf_write_from_whole_cursor(&frame->encoded_buf, debug_data); |
| 1129 |
AWS_ASSERT(writes_ok); |
| 1130 |
(void)writes_ok; |
| 1131 |
|
| 1132 |
return &frame->base; |
| 1133 |
} |
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
static const size_t s_frame_window_update_length = 4; |
| 1139 |
|
| 1140 |
struct aws_h2_frame *aws_h2_frame_new_window_update( |
| 1141 |
struct aws_allocator *allocator, |
| 1142 |
uint32_t stream_id, |
| 1143 |
uint32_t window_size_increment) { |
| 1144 |
|
| 1145 |
|
| 1146 |
if (stream_id > AWS_H2_STREAM_ID_MAX) { |
| 1147 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 1148 |
return NULL; |
| 1149 |
} |
| 1150 |
|
| 1151 |
if (window_size_increment > AWS_H2_WINDOW_UPDATE_MAX) { |
| 1152 |
AWS_LOGF_ERROR( |
| 1153 |
AWS_LS_HTTP_ENCODER, |
| 1154 |
"Window increment size %" PRIu32 " exceeds HTTP/2 max %" PRIu32, |
| 1155 |
window_size_increment, |
| 1156 |
AWS_H2_WINDOW_UPDATE_MAX); |
| 1157 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 1158 |
return NULL; |
| 1159 |
} |
| 1160 |
|
| 1161 |
|
| 1162 |
const uint8_t flags = 0; |
| 1163 |
const size_t payload_len = s_frame_window_update_length; |
| 1164 |
|
| 1165 |
struct aws_h2_frame_prebuilt *frame = |
| 1166 |
s_h2_frame_new_prebuilt(allocator, AWS_H2_FRAME_T_WINDOW_UPDATE, stream_id, payload_len, flags); |
| 1167 |
if (!frame) { |
| 1168 |
return NULL; |
| 1169 |
} |
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
bool writes_ok = true; |
| 1177 |
writes_ok &= aws_byte_buf_write_be32(&frame->encoded_buf, window_size_increment); |
| 1178 |
AWS_ASSERT(writes_ok); |
| 1179 |
(void)writes_ok; |
| 1180 |
|
| 1181 |
return &frame->base; |
| 1182 |
} |
| 1183 |
|
| 1184 |
void aws_h2_frame_destroy(struct aws_h2_frame *frame) { |
| 1185 |
if (frame) { |
| 1186 |
frame->vtable->destroy(frame); |
| 1187 |
} |
| 1188 |
} |
| 1189 |
|
| 1190 |
int aws_h2_encode_frame( |
| 1191 |
struct aws_h2_frame_encoder *encoder, |
| 1192 |
struct aws_h2_frame *frame, |
| 1193 |
struct aws_byte_buf *output, |
| 1194 |
bool *frame_complete) { |
| 1195 |
|
| 1196 |
AWS_PRECONDITION(encoder); |
| 1197 |
AWS_PRECONDITION(frame); |
| 1198 |
AWS_PRECONDITION(output); |
| 1199 |
AWS_PRECONDITION(frame_complete); |
| 1200 |
|
| 1201 |
if (encoder->has_errored) { |
| 1202 |
ENCODER_LOG(ERROR, encoder, "Encoder cannot be used again after an error"); |
| 1203 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 1204 |
} |
| 1205 |
|
| 1206 |
if (encoder->current_frame && (encoder->current_frame != frame)) { |
| 1207 |
ENCODER_LOG(ERROR, encoder, "Cannot encode new frame until previous frame completes"); |
| 1208 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 1209 |
} |
| 1210 |
|
| 1211 |
*frame_complete = false; |
| 1212 |
|
| 1213 |
if (frame->vtable->encode(frame, encoder, output, frame_complete)) { |
| 1214 |
ENCODER_LOGF( |
| 1215 |
ERROR, |
| 1216 |
encoder, |
| 1217 |
"Failed to encode frame type=%s stream_id=%" PRIu32 ", %s", |
| 1218 |
aws_h2_frame_type_to_str(frame->type), |
| 1219 |
frame->stream_id, |
| 1220 |
aws_error_name(aws_last_error())); |
| 1221 |
encoder->has_errored = true; |
| 1222 |
return AWS_OP_ERR; |
| 1223 |
} |
| 1224 |
|
| 1225 |
encoder->current_frame = *frame_complete ? NULL : frame; |
| 1226 |
return AWS_OP_SUCCESS; |
| 1227 |
} |
| 1228 |
|
| 1229 |
void aws_h2_frame_encoder_set_setting_header_table_size(struct aws_h2_frame_encoder *encoder, uint32_t data) { |
| 1230 |
|
| 1231 |
|
| 1232 |
aws_hpack_encoder_update_max_table_size(&encoder->hpack, data); |
| 1233 |
} |
| 1234 |
|
| 1235 |
void aws_h2_frame_encoder_set_setting_max_frame_size(struct aws_h2_frame_encoder *encoder, uint32_t data) { |
| 1236 |
encoder->settings.max_frame_size = data; |
| 1237 |
} |