h2_frames.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/http/private/h2_frames.h>
  6. #include <aws/compression/huffman.h>
  7. #include <aws/common/logging.h>
  8. #include <aws/io/stream.h>
  9. #include <inttypes.h>
  10. #ifdef _MSC_VER
  11. # pragma warning(disable : 4204) /* non-constant aggregate initializer */
  12. #endif
  13. #define ENCODER_LOGF(level, encoder, text, ...) \
  14. AWS_LOGF_##level(AWS_LS_HTTP_ENCODER, "id=%p " text, (encoder)->logging_id, __VA_ARGS__)
  15. #define ENCODER_LOG(level, encoder, text) ENCODER_LOGF(level, encoder, "%s", text)
  16. const struct aws_byte_cursor aws_h2_connection_preface_client_string =
  17. AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n");
  18. /* Initial values and bounds are from RFC-7540 6.5.2 */
  19. const uint32_t aws_h2_settings_initial[AWS_HTTP2_SETTINGS_END_RANGE] = {
  20. [AWS_HTTP2_SETTINGS_HEADER_TABLE_SIZE] = 4096,
  21. [AWS_HTTP2_SETTINGS_ENABLE_PUSH] = 1,
  22. [AWS_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS] = UINT32_MAX, /* "Initially there is no limit to this value" */
  23. [AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE] = AWS_H2_INIT_WINDOW_SIZE,
  24. [AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE] = 16384,
  25. [AWS_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE] = UINT32_MAX, /* "The initial value of this setting is unlimited" */
  26. };
  27. const uint32_t aws_h2_settings_bounds[AWS_HTTP2_SETTINGS_END_RANGE][2] = {
  28. [AWS_HTTP2_SETTINGS_HEADER_TABLE_SIZE][0] = 0,
  29. [AWS_HTTP2_SETTINGS_HEADER_TABLE_SIZE][1] = UINT32_MAX,
  30. [AWS_HTTP2_SETTINGS_ENABLE_PUSH][0] = 0,
  31. [AWS_HTTP2_SETTINGS_ENABLE_PUSH][1] = 1,
  32. [AWS_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS][0] = 0,
  33. [AWS_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS][1] = UINT32_MAX,
  34. [AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE][0] = 0,
  35. [AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE][1] = AWS_H2_WINDOW_UPDATE_MAX,
  36. [AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE][0] = 16384,
  37. [AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE][1] = AWS_H2_PAYLOAD_MAX,
  38. [AWS_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE][0] = 0,
  39. [AWS_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE][1] = UINT32_MAX,
  40. };
  41. /* Stream ids & dependencies should only write the bottom 31 bits */
  42. static const uint32_t s_u32_top_bit_mask = UINT32_MAX << 31;
  43. /* Bytes to initially reserve for encoding of an entire header block. Buffer will grow if necessary. */
  44. static const size_t s_encoded_header_block_reserve = 128; /* Value pulled from thin air */
  45. #define DEFINE_FRAME_VTABLE(NAME) \
  46. static aws_h2_frame_destroy_fn s_frame_##NAME##_destroy; \
  47. static aws_h2_frame_encode_fn s_frame_##NAME##_encode; \
  48. static const struct aws_h2_frame_vtable s_frame_##NAME##_vtable = { \
  49. .destroy = s_frame_##NAME##_destroy, \
  50. .encode = s_frame_##NAME##_encode, \
  51. }
  52. const char *aws_h2_frame_type_to_str(enum aws_h2_frame_type type) {
  53. switch (type) {
  54. case AWS_H2_FRAME_T_DATA:
  55. return "DATA";
  56. case AWS_H2_FRAME_T_HEADERS:
  57. return "HEADERS";
  58. case AWS_H2_FRAME_T_PRIORITY:
  59. return "PRIORITY";
  60. case AWS_H2_FRAME_T_RST_STREAM:
  61. return "RST_STREAM";
  62. case AWS_H2_FRAME_T_SETTINGS:
  63. return "SETTINGS";
  64. case AWS_H2_FRAME_T_PUSH_PROMISE:
  65. return "PUSH_PROMISE";
  66. case AWS_H2_FRAME_T_PING:
  67. return "PING";
  68. case AWS_H2_FRAME_T_GOAWAY:
  69. return "GOAWAY";
  70. case AWS_H2_FRAME_T_WINDOW_UPDATE:
  71. return "WINDOW_UPDATE";
  72. case AWS_H2_FRAME_T_CONTINUATION:
  73. return "CONTINUATION";
  74. default:
  75. return "**UNKNOWN**";
  76. }
  77. }
  78. const char *aws_http2_error_code_to_str(enum aws_http2_error_code h2_error_code) {
  79. switch (h2_error_code) {
  80. case AWS_HTTP2_ERR_NO_ERROR:
  81. return "NO_ERROR";
  82. case AWS_HTTP2_ERR_PROTOCOL_ERROR:
  83. return "PROTOCOL_ERROR";
  84. case AWS_HTTP2_ERR_INTERNAL_ERROR:
  85. return "INTERNAL_ERROR";
  86. case AWS_HTTP2_ERR_FLOW_CONTROL_ERROR:
  87. return "FLOW_CONTROL_ERROR";
  88. case AWS_HTTP2_ERR_SETTINGS_TIMEOUT:
  89. return "SETTINGS_TIMEOUT";
  90. case AWS_HTTP2_ERR_STREAM_CLOSED:
  91. return "STREAM_CLOSED";
  92. case AWS_HTTP2_ERR_FRAME_SIZE_ERROR:
  93. return "FRAME_SIZE_ERROR";
  94. case AWS_HTTP2_ERR_REFUSED_STREAM:
  95. return "REFUSED_STREAM";
  96. case AWS_HTTP2_ERR_CANCEL:
  97. return "CANCEL";
  98. case AWS_HTTP2_ERR_COMPRESSION_ERROR:
  99. return "COMPRESSION_ERROR";
  100. case AWS_HTTP2_ERR_CONNECT_ERROR:
  101. return "CONNECT_ERROR";
  102. case AWS_HTTP2_ERR_ENHANCE_YOUR_CALM:
  103. return "ENHANCE_YOUR_CALM";
  104. case AWS_HTTP2_ERR_INADEQUATE_SECURITY:
  105. return "INADEQUATE_SECURITY";
  106. case AWS_HTTP2_ERR_HTTP_1_1_REQUIRED:
  107. return "HTTP_1_1_REQUIRED";
  108. default:
  109. return "UNKNOWN_ERROR";
  110. }
  111. }
  112. struct aws_h2err aws_h2err_from_h2_code(enum aws_http2_error_code h2_error_code) {
  113. AWS_PRECONDITION(h2_error_code > AWS_HTTP2_ERR_NO_ERROR && h2_error_code < AWS_HTTP2_ERR_COUNT);
  114. return (struct aws_h2err){
  115. .h2_code = h2_error_code,
  116. .aws_code = AWS_ERROR_HTTP_PROTOCOL_ERROR,
  117. };
  118. }
  119. struct aws_h2err aws_h2err_from_aws_code(int aws_error_code) {
  120. AWS_PRECONDITION(aws_error_code != 0);
  121. return (struct aws_h2err){
  122. .h2_code = AWS_HTTP2_ERR_INTERNAL_ERROR,
  123. .aws_code = aws_error_code,
  124. };
  125. }
  126. struct aws_h2err aws_h2err_from_last_error(void) {
  127. return aws_h2err_from_aws_code(aws_last_error());
  128. }
  129. bool aws_h2err_success(struct aws_h2err err) {
  130. return err.h2_code == 0 && err.aws_code == 0;
  131. }
  132. bool aws_h2err_failed(struct aws_h2err err) {
  133. return err.h2_code != 0 || err.aws_code != 0;
  134. }
  135. int aws_h2_validate_stream_id(uint32_t stream_id) {
  136. if (stream_id == 0 || stream_id > AWS_H2_STREAM_ID_MAX) {
  137. return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  138. }
  139. return AWS_OP_SUCCESS;
  140. }
  141. /**
  142. * Determine max frame payload length that will:
  143. * 1) fit in output's available space
  144. * 2) obey encoders current MAX_FRAME_SIZE
  145. *
  146. * Assumes no part of the frame has been written yet to output.
  147. * The total length of the frame would be: returned-payload-len + AWS_H2_FRAME_PREFIX_SIZE
  148. *
  149. * Raises error if there is not enough space available for even a frame prefix.
  150. */
  151. static int s_get_max_contiguous_payload_length(
  152. const struct aws_h2_frame_encoder *encoder,
  153. const struct aws_byte_buf *output,
  154. size_t *max_payload_length) {
  155. const size_t space_available = output->capacity - output->len;
  156. size_t max_payload_given_space_available;
  157. if (aws_sub_size_checked(space_available, AWS_H2_FRAME_PREFIX_SIZE, &max_payload_given_space_available)) {
  158. return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
  159. }
  160. size_t max_payload_given_settings = encoder->settings.max_frame_size;
  161. *max_payload_length = aws_min_size(max_payload_given_space_available, max_payload_given_settings);
  162. return AWS_OP_SUCCESS;
  163. }
  164. /***********************************************************************************************************************
  165. * Priority
  166. **********************************************************************************************************************/
  167. static size_t s_frame_priority_settings_size = 5;
  168. static void s_frame_priority_settings_encode(
  169. const struct aws_h2_frame_priority_settings *priority,
  170. struct aws_byte_buf *output) {
  171. AWS_PRECONDITION(priority);
  172. AWS_PRECONDITION(output);
  173. AWS_PRECONDITION((priority->stream_dependency & s_u32_top_bit_mask) == 0);
  174. (void)s_u32_top_bit_mask;
  175. /* PRIORITY is encoded as (RFC-7540 6.3):
  176. * +-+-------------------------------------------------------------+
  177. * |E| Stream Dependency (31) |
  178. * +-+-------------+-----------------------------------------------+
  179. * | Weight (8) |
  180. * +-+-------------+
  181. */
  182. bool writes_ok = true;
  183. /* Write the top 4 bytes */
  184. uint32_t top_bytes = priority->stream_dependency | ((uint32_t)priority->stream_dependency_exclusive << 31);
  185. writes_ok &= aws_byte_buf_write_be32(output, top_bytes);
  186. /* Write the priority weight */
  187. writes_ok &= aws_byte_buf_write_u8(output, priority->weight);
  188. AWS_ASSERT(writes_ok);
  189. (void)writes_ok;
  190. }
  191. /***********************************************************************************************************************
  192. * Common Frame Prefix
  193. **********************************************************************************************************************/
  194. static void s_init_frame_base(
  195. struct aws_h2_frame *frame_base,
  196. struct aws_allocator *alloc,
  197. enum aws_h2_frame_type type,
  198. const struct aws_h2_frame_vtable *vtable,
  199. uint32_t stream_id) {
  200. frame_base->vtable = vtable;
  201. frame_base->alloc = alloc;
  202. frame_base->type = type;
  203. frame_base->stream_id = stream_id;
  204. }
  205. static void s_frame_prefix_encode(
  206. enum aws_h2_frame_type type,
  207. uint32_t stream_id,
  208. size_t length,
  209. uint8_t flags,
  210. struct aws_byte_buf *output) {
  211. AWS_PRECONDITION(output);
  212. AWS_PRECONDITION(!(stream_id & s_u32_top_bit_mask), "Invalid stream ID");
  213. AWS_PRECONDITION(length <= AWS_H2_PAYLOAD_MAX);
  214. /* Frame prefix is encoded like this (RFC-7540 4.1):
  215. * +-----------------------------------------------+
  216. * | Length (24) |
  217. * +---------------+---------------+---------------+
  218. * | Type (8) | Flags (8) |
  219. * +-+-------------+---------------+-------------------------------+
  220. * |R| Stream Identifier (31) |
  221. * +=+=============================================================+
  222. */
  223. bool writes_ok = true;
  224. /* Write length */
  225. writes_ok &= aws_byte_buf_write_be24(output, (uint32_t)length);
  226. /* Write type */
  227. writes_ok &= aws_byte_buf_write_u8(output, type);
  228. /* Write flags */
  229. writes_ok &= aws_byte_buf_write_u8(output, flags);
  230. /* Write stream id (with reserved first bit) */
  231. writes_ok &= aws_byte_buf_write_be32(output, stream_id);
  232. AWS_ASSERT(writes_ok);
  233. (void)writes_ok;
  234. }
  235. /***********************************************************************************************************************
  236. * Encoder
  237. **********************************************************************************************************************/
  238. int aws_h2_frame_encoder_init(
  239. struct aws_h2_frame_encoder *encoder,
  240. struct aws_allocator *allocator,
  241. const void *logging_id) {
  242. AWS_PRECONDITION(encoder);
  243. AWS_PRECONDITION(allocator);
  244. AWS_ZERO_STRUCT(*encoder);
  245. encoder->allocator = allocator;
  246. encoder->logging_id = logging_id;
  247. aws_hpack_encoder_init(&encoder->hpack, allocator, logging_id);
  248. encoder->settings.max_frame_size = aws_h2_settings_initial[AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE];
  249. return AWS_OP_SUCCESS;
  250. }
  251. void aws_h2_frame_encoder_clean_up(struct aws_h2_frame_encoder *encoder) {
  252. AWS_PRECONDITION(encoder);
  253. aws_hpack_encoder_clean_up(&encoder->hpack);
  254. }
  255. /***********************************************************************************************************************
  256. * DATA
  257. **********************************************************************************************************************/
  258. int aws_h2_encode_data_frame(
  259. struct aws_h2_frame_encoder *encoder,
  260. uint32_t stream_id,
  261. struct aws_input_stream *body_stream,
  262. bool body_ends_stream,
  263. uint8_t pad_length,
  264. int32_t *stream_window_size_peer,
  265. size_t *connection_window_size_peer,
  266. struct aws_byte_buf *output,
  267. bool *body_complete,
  268. bool *body_stalled) {
  269. AWS_PRECONDITION(encoder);
  270. AWS_PRECONDITION(body_stream);
  271. AWS_PRECONDITION(output);
  272. AWS_PRECONDITION(body_complete);
  273. AWS_PRECONDITION(body_stalled);
  274. AWS_PRECONDITION(*stream_window_size_peer > 0);
  275. if (aws_h2_validate_stream_id(stream_id)) {
  276. return AWS_OP_ERR;
  277. }
  278. *body_complete = false;
  279. *body_stalled = false;
  280. uint8_t flags = 0;
  281. /*
  282. * Payload-length is the first thing encoded in a frame, but we don't know how
  283. * much data we'll get from the body-stream until we actually read it.
  284. * Therefore, we determine the exact location that the body data should go,
  285. * then stream the body directly into that part of the output buffer.
  286. * Then we will go and write the other parts of the frame in around it.
  287. */
  288. size_t bytes_preceding_body = AWS_H2_FRAME_PREFIX_SIZE;
  289. size_t payload_overhead = 0; /* Amount of "payload" that will not contain body (padding) */
  290. if (pad_length > 0) {
  291. flags |= AWS_H2_FRAME_F_PADDED;
  292. /* Padding len is 1st byte of payload (padding itself goes at end of payload) */
  293. bytes_preceding_body += 1;
  294. payload_overhead = 1 + pad_length;
  295. }
  296. /* Max amount allowed by stream and connection flow-control window */
  297. size_t min_window_size = aws_min_size(*stream_window_size_peer, *connection_window_size_peer);
  298. /* Max amount of payload we can do right now */
  299. size_t max_payload;
  300. if (s_get_max_contiguous_payload_length(encoder, output, &max_payload)) {
  301. goto handle_waiting_for_more_space;
  302. }
  303. /* The flow-control window will limit the size for max_payload of a flow-controlled frame */
  304. max_payload = aws_min_size(max_payload, min_window_size);
  305. /* Max amount of body we can fit in the payload*/
  306. size_t max_body;
  307. if (aws_sub_size_checked(max_payload, payload_overhead, &max_body) || max_body == 0) {
  308. goto handle_waiting_for_more_space;
  309. }
  310. /* Use a sub-buffer to limit where body can go */
  311. struct aws_byte_buf body_sub_buf =
  312. aws_byte_buf_from_empty_array(output->buffer + output->len + bytes_preceding_body, max_body);
  313. /* Read body into sub-buffer */
  314. if (aws_input_stream_read(body_stream, &body_sub_buf)) {
  315. goto error;
  316. }
  317. /* Check if we've reached the end of the body */
  318. struct aws_stream_status body_status;
  319. if (aws_input_stream_get_status(body_stream, &body_status)) {
  320. goto error;
  321. }
  322. if (body_status.is_end_of_stream) {
  323. *body_complete = true;
  324. if (body_ends_stream) {
  325. flags |= AWS_H2_FRAME_F_END_STREAM;
  326. }
  327. } else {
  328. if (body_sub_buf.len < body_sub_buf.capacity) {
  329. /* Body stream was unable to provide as much data as it could have */
  330. *body_stalled = true;
  331. if (body_sub_buf.len == 0) {
  332. /* This frame would have no useful information, don't even bother sending it */
  333. goto handle_nothing_to_send_right_now;
  334. }
  335. }
  336. }
  337. ENCODER_LOGF(
  338. TRACE,
  339. encoder,
  340. "Encoding frame type=DATA stream_id=%" PRIu32 " data_len=%zu stalled=%d%s",
  341. stream_id,
  342. body_sub_buf.len,
  343. *body_stalled,
  344. (flags & AWS_H2_FRAME_F_END_STREAM) ? " END_STREAM" : "");
  345. /*
  346. * Write in the other parts of the frame.
  347. */
  348. bool writes_ok = true;
  349. /* Write the frame prefix */
  350. const size_t payload_len = body_sub_buf.len + payload_overhead;
  351. s_frame_prefix_encode(AWS_H2_FRAME_T_DATA, stream_id, payload_len, flags, output);
  352. /* Write pad length */
  353. if (flags & AWS_H2_FRAME_F_PADDED) {
  354. writes_ok &= aws_byte_buf_write_u8(output, pad_length);
  355. }
  356. /* Increment output->len to jump over the body that we already wrote in */
  357. AWS_ASSERT(output->buffer + output->len == body_sub_buf.buffer && "Streamed DATA to wrong position");
  358. output->len += body_sub_buf.len;
  359. /* Write padding */
  360. if (flags & AWS_H2_FRAME_F_PADDED) {
  361. writes_ok &= aws_byte_buf_write_u8_n(output, 0, pad_length);
  362. }
  363. /* update the connection window size now, we will update stream window size when this function returns */
  364. AWS_ASSERT(payload_len <= min_window_size);
  365. *connection_window_size_peer -= payload_len;
  366. *stream_window_size_peer -= (int32_t)payload_len;
  367. AWS_ASSERT(writes_ok);
  368. (void)writes_ok;
  369. return AWS_OP_SUCCESS;
  370. handle_waiting_for_more_space:
  371. ENCODER_LOGF(TRACE, encoder, "Insufficient space to encode DATA for stream %" PRIu32 " right now", stream_id);
  372. return AWS_OP_SUCCESS;
  373. handle_nothing_to_send_right_now:
  374. ENCODER_LOGF(INFO, encoder, "Stream %" PRIu32 " produced 0 bytes of body data", stream_id);
  375. return AWS_OP_SUCCESS;
  376. error:
  377. return AWS_OP_ERR;
  378. }
  379. /***********************************************************************************************************************
  380. * HEADERS / PUSH_PROMISE
  381. **********************************************************************************************************************/
  382. DEFINE_FRAME_VTABLE(headers);
  383. /* Represents a HEADERS or PUSH_PROMISE frame (followed by zero or more CONTINUATION frames) */
  384. struct aws_h2_frame_headers {
  385. struct aws_h2_frame base;
  386. /* Common data */
  387. const struct aws_http_headers *headers;
  388. uint8_t pad_length; /* Set to 0 to disable AWS_H2_FRAME_F_PADDED */
  389. /* HEADERS-only data */
  390. bool end_stream; /* AWS_H2_FRAME_F_END_STREAM */
  391. bool has_priority; /* AWS_H2_FRAME_F_PRIORITY */
  392. struct aws_h2_frame_priority_settings priority;
  393. /* PUSH_PROMISE-only data */
  394. uint32_t promised_stream_id;
  395. /* State */
  396. enum {
  397. AWS_H2_HEADERS_STATE_INIT,
  398. AWS_H2_HEADERS_STATE_FIRST_FRAME, /* header-block pre-encoded, no frames written yet */
  399. AWS_H2_HEADERS_STATE_CONTINUATION, /* first frame written, need to write CONTINUATION frames now */
  400. AWS_H2_HEADERS_STATE_COMPLETE,
  401. } state;
  402. struct aws_byte_buf whole_encoded_header_block;
  403. struct aws_byte_cursor header_block_cursor; /* tracks progress sending encoded header-block in fragments */
  404. };
  405. static struct aws_h2_frame *s_frame_new_headers_or_push_promise(
  406. struct aws_allocator *allocator,
  407. enum aws_h2_frame_type frame_type,
  408. uint32_t stream_id,
  409. const struct aws_http_headers *headers,
  410. uint8_t pad_length,
  411. bool end_stream,
  412. const struct aws_h2_frame_priority_settings *optional_priority,
  413. uint32_t promised_stream_id) {
  414. /* TODO: Host and ":authority" are no longer permitted to disagree. Should we enforce it here or sent it as
  415. * requested, let the server side reject the request? */
  416. AWS_PRECONDITION(allocator);
  417. AWS_PRECONDITION(frame_type == AWS_H2_FRAME_T_HEADERS || frame_type == AWS_H2_FRAME_T_PUSH_PROMISE);
  418. AWS_PRECONDITION(headers);
  419. /* Validate args */
  420. if (aws_h2_validate_stream_id(stream_id)) {
  421. return NULL;
  422. }
  423. if (frame_type == AWS_H2_FRAME_T_PUSH_PROMISE) {
  424. if (aws_h2_validate_stream_id(promised_stream_id)) {
  425. return NULL;
  426. }
  427. }
  428. if (optional_priority && aws_h2_validate_stream_id(optional_priority->stream_dependency)) {
  429. return NULL;
  430. }
  431. /* Create */
  432. struct aws_h2_frame_headers *frame = aws_mem_calloc(allocator, 1, sizeof(struct aws_h2_frame_headers));
  433. if (!frame) {
  434. return NULL;
  435. }
  436. if (aws_byte_buf_init(&frame->whole_encoded_header_block, allocator, s_encoded_header_block_reserve)) {
  437. goto error;
  438. }
  439. if (frame_type == AWS_H2_FRAME_T_HEADERS) {
  440. frame->end_stream = end_stream;
  441. if (optional_priority) {
  442. frame->has_priority = true;
  443. frame->priority = *optional_priority;
  444. }
  445. } else {
  446. frame->promised_stream_id = promised_stream_id;
  447. }
  448. s_init_frame_base(&frame->base, allocator, frame_type, &s_frame_headers_vtable, stream_id);
  449. aws_http_headers_acquire((struct aws_http_headers *)headers);
  450. frame->headers = headers;
  451. frame->pad_length = pad_length;
  452. return &frame->base;
  453. error:
  454. s_frame_headers_destroy(&frame->base);
  455. return NULL;
  456. }
  457. struct aws_h2_frame *aws_h2_frame_new_headers(
  458. struct aws_allocator *allocator,
  459. uint32_t stream_id,
  460. const struct aws_http_headers *headers,
  461. bool end_stream,
  462. uint8_t pad_length,
  463. const struct aws_h2_frame_priority_settings *optional_priority) {
  464. return s_frame_new_headers_or_push_promise(
  465. allocator,
  466. AWS_H2_FRAME_T_HEADERS,
  467. stream_id,
  468. headers,
  469. pad_length,
  470. end_stream,
  471. optional_priority,
  472. 0 /* HEADERS doesn't have promised_stream_id */);
  473. }
  474. struct aws_h2_frame *aws_h2_frame_new_push_promise(
  475. struct aws_allocator *allocator,
  476. uint32_t stream_id,
  477. uint32_t promised_stream_id,
  478. const struct aws_http_headers *headers,
  479. uint8_t pad_length) {
  480. return s_frame_new_headers_or_push_promise(
  481. allocator,
  482. AWS_H2_FRAME_T_PUSH_PROMISE,
  483. stream_id,
  484. headers,
  485. pad_length,
  486. false /* PUSH_PROMISE doesn't have end_stream flag */,
  487. NULL /* PUSH_PROMISE doesn't have priority_settings */,
  488. promised_stream_id);
  489. }
  490. static void s_frame_headers_destroy(struct aws_h2_frame *frame_base) {
  491. struct aws_h2_frame_headers *frame = AWS_CONTAINER_OF(frame_base, struct aws_h2_frame_headers, base);
  492. aws_http_headers_release((struct aws_http_headers *)frame->headers);
  493. aws_byte_buf_clean_up(&frame->whole_encoded_header_block);
  494. aws_mem_release(frame->base.alloc, frame);
  495. }
  496. /* Encode the next frame for this header-block (or encode nothing if output buffer is too small). */
  497. static void s_encode_single_header_block_frame(
  498. struct aws_h2_frame_headers *frame,
  499. struct aws_h2_frame_encoder *encoder,
  500. struct aws_byte_buf *output,
  501. bool *waiting_for_more_space) {
  502. /*
  503. * Figure out the details of the next frame to encode.
  504. * The first frame will be either HEADERS or PUSH_PROMISE.
  505. * All subsequent frames will be CONTINUATION
  506. */
  507. enum aws_h2_frame_type frame_type;
  508. uint8_t flags = 0;
  509. uint8_t pad_length = 0;
  510. const struct aws_h2_frame_priority_settings *priority_settings = NULL;
  511. const uint32_t *promised_stream_id = NULL;
  512. size_t payload_overhead = 0; /* Amount of payload holding things other than header-block (padding, etc) */
  513. if (frame->state == AWS_H2_HEADERS_STATE_FIRST_FRAME) {
  514. frame_type = frame->base.type;
  515. if (frame->pad_length > 0) {
  516. flags |= AWS_H2_FRAME_F_PADDED;
  517. pad_length = frame->pad_length;
  518. payload_overhead += 1 + pad_length;
  519. }
  520. if (frame->has_priority) {
  521. priority_settings = &frame->priority;
  522. flags |= AWS_H2_FRAME_F_PRIORITY;
  523. payload_overhead += s_frame_priority_settings_size;
  524. }
  525. if (frame->end_stream) {
  526. flags |= AWS_H2_FRAME_F_END_STREAM;
  527. }
  528. if (frame_type == AWS_H2_FRAME_T_PUSH_PROMISE) {
  529. promised_stream_id = &frame->promised_stream_id;
  530. payload_overhead += 4;
  531. }
  532. } else /* CONTINUATION */ {
  533. frame_type = AWS_H2_FRAME_T_CONTINUATION;
  534. }
  535. /*
  536. * Figure out what size header-block fragment should go in this frame.
  537. */
  538. size_t max_payload;
  539. if (s_get_max_contiguous_payload_length(encoder, output, &max_payload)) {
  540. goto handle_waiting_for_more_space;
  541. }
  542. size_t max_fragment;
  543. if (aws_sub_size_checked(max_payload, payload_overhead, &max_fragment)) {
  544. goto handle_waiting_for_more_space;
  545. }
  546. const size_t fragment_len = aws_min_size(max_fragment, frame->header_block_cursor.len);
  547. if (fragment_len == frame->header_block_cursor.len) {
  548. /* This will finish the header-block */
  549. flags |= AWS_H2_FRAME_F_END_HEADERS;
  550. } else {
  551. /* If we're not finishing the header-block, is it even worth trying to send this frame now? */
  552. const size_t even_worth_sending_threshold = AWS_H2_FRAME_PREFIX_SIZE + payload_overhead;
  553. if (fragment_len < even_worth_sending_threshold) {
  554. goto handle_waiting_for_more_space;
  555. }
  556. }
  557. /*
  558. * Ok, it fits! Write the frame
  559. */
  560. ENCODER_LOGF(
  561. TRACE,
  562. encoder,
  563. "Encoding frame type=%s stream_id=%" PRIu32 "%s%s",
  564. aws_h2_frame_type_to_str(frame_type),
  565. frame->base.stream_id,
  566. (flags & AWS_H2_FRAME_F_END_HEADERS) ? " END_HEADERS" : "",
  567. (flags & AWS_H2_FRAME_F_END_STREAM) ? " END_STREAM" : "");
  568. bool writes_ok = true;
  569. /* Write the frame prefix */
  570. const size_t payload_len = fragment_len + payload_overhead;
  571. s_frame_prefix_encode(frame_type, frame->base.stream_id, payload_len, flags, output);
  572. /* Write pad length */
  573. if (flags & AWS_H2_FRAME_F_PADDED) {
  574. AWS_ASSERT(frame_type != AWS_H2_FRAME_T_CONTINUATION);
  575. writes_ok &= aws_byte_buf_write_u8(output, pad_length);
  576. }
  577. /* Write priority */
  578. if (flags & AWS_H2_FRAME_F_PRIORITY) {
  579. AWS_ASSERT(frame_type == AWS_H2_FRAME_T_HEADERS);
  580. s_frame_priority_settings_encode(priority_settings, output);
  581. }
  582. /* Write promised stream ID */
  583. if (promised_stream_id) {
  584. AWS_ASSERT(frame_type == AWS_H2_FRAME_T_PUSH_PROMISE);
  585. writes_ok &= aws_byte_buf_write_be32(output, *promised_stream_id);
  586. }
  587. /* Write header-block fragment */
  588. if (fragment_len > 0) {
  589. struct aws_byte_cursor fragment = aws_byte_cursor_advance(&frame->header_block_cursor, fragment_len);
  590. writes_ok &= aws_byte_buf_write_from_whole_cursor(output, fragment);
  591. }
  592. /* Write padding */
  593. if (flags & AWS_H2_FRAME_F_PADDED) {
  594. writes_ok &= aws_byte_buf_write_u8_n(output, 0, pad_length);
  595. }
  596. AWS_ASSERT(writes_ok);
  597. (void)writes_ok;
  598. /* Success! Wrote entire frame. It's safe to change state now */
  599. frame->state =
  600. flags & AWS_H2_FRAME_F_END_HEADERS ? AWS_H2_HEADERS_STATE_COMPLETE : AWS_H2_HEADERS_STATE_CONTINUATION;
  601. *waiting_for_more_space = false;
  602. return;
  603. handle_waiting_for_more_space:
  604. ENCODER_LOGF(
  605. TRACE,
  606. encoder,
  607. "Insufficient space to encode %s for stream %" PRIu32 " right now",
  608. aws_h2_frame_type_to_str(frame->base.type),
  609. frame->base.stream_id);
  610. *waiting_for_more_space = true;
  611. }
  612. static int s_frame_headers_encode(
  613. struct aws_h2_frame *frame_base,
  614. struct aws_h2_frame_encoder *encoder,
  615. struct aws_byte_buf *output,
  616. bool *complete) {
  617. struct aws_h2_frame_headers *frame = AWS_CONTAINER_OF(frame_base, struct aws_h2_frame_headers, base);
  618. /* Pre-encode the entire header-block into another buffer
  619. * the first time we're called. */
  620. if (frame->state == AWS_H2_HEADERS_STATE_INIT) {
  621. if (aws_hpack_encode_header_block(&encoder->hpack, frame->headers, &frame->whole_encoded_header_block)) {
  622. ENCODER_LOGF(
  623. ERROR,
  624. encoder,
  625. "Error doing HPACK encoding on %s of stream %" PRIu32 ": %s",
  626. aws_h2_frame_type_to_str(frame->base.type),
  627. frame->base.stream_id,
  628. aws_error_name(aws_last_error()));
  629. goto error;
  630. }
  631. frame->header_block_cursor = aws_byte_cursor_from_buf(&frame->whole_encoded_header_block);
  632. frame->state = AWS_H2_HEADERS_STATE_FIRST_FRAME;
  633. }
  634. /* Write frames (HEADER or PUSH_PROMISE, followed by N CONTINUATION frames)
  635. * until we're done writing header-block or the buffer is too full to continue */
  636. bool waiting_for_more_space = false;
  637. while (frame->state < AWS_H2_HEADERS_STATE_COMPLETE && !waiting_for_more_space) {
  638. s_encode_single_header_block_frame(frame, encoder, output, &waiting_for_more_space);
  639. }
  640. *complete = frame->state == AWS_H2_HEADERS_STATE_COMPLETE;
  641. return AWS_OP_SUCCESS;
  642. error:
  643. return AWS_OP_ERR;
  644. }
  645. /***********************************************************************************************************************
  646. * aws_h2_frame_prebuilt - Used by small simple frame types that we can pre-encode at the time of creation.
  647. * The pre-encoded buffer is then just copied bit-by-bit during the actual "encode()" function.
  648. *
  649. * It's safe to pre-encode a frame if it doesn't query/mutate any external state. So PING is totally great
  650. * to pre-encode, but HEADERS (which queries MAX_FRAME_SIZE and mutates the HPACK table) would be a bad candidate.
  651. **********************************************************************************************************************/
  652. struct aws_h2_frame_prebuilt {
  653. struct aws_h2_frame base;
  654. /* The whole entire frame is pre-encoded to this buffer during construction.
  655. * The buffer has the exact capacity necessary to hold the frame */
  656. struct aws_byte_buf encoded_buf;
  657. /* After construction, this cursor points to the full contents of encoded_buf.
  658. * As encode() is called, we copy the contents to output and advance the cursor.*/
  659. struct aws_byte_cursor cursor;
  660. };
  661. DEFINE_FRAME_VTABLE(prebuilt);
  662. /* Can't pre-encode a frame unless it's guaranteed to fit, regardless of current settings. */
  663. static size_t s_prebuilt_payload_max(void) {
  664. return aws_h2_settings_bounds[AWS_HTTP2_SETTINGS_MAX_FRAME_SIZE][0];
  665. }
  666. /* Create aws_h2_frame_prebuilt and encode frame prefix into frame->encoded_buf.
  667. * Caller must encode the payload to fill the rest of the encoded_buf. */
  668. static struct aws_h2_frame_prebuilt *s_h2_frame_new_prebuilt(
  669. struct aws_allocator *allocator,
  670. enum aws_h2_frame_type type,
  671. uint32_t stream_id,
  672. size_t payload_len,
  673. uint8_t flags) {
  674. AWS_PRECONDITION(payload_len <= s_prebuilt_payload_max());
  675. const size_t encoded_frame_len = AWS_H2_FRAME_PREFIX_SIZE + payload_len;
  676. /* Use single allocation for frame and buffer storage */
  677. struct aws_h2_frame_prebuilt *frame;
  678. void *storage;
  679. if (!aws_mem_acquire_many(
  680. allocator, 2, &frame, sizeof(struct aws_h2_frame_prebuilt), &storage, encoded_frame_len)) {
  681. return NULL;
  682. }
  683. AWS_ZERO_STRUCT(*frame);
  684. s_init_frame_base(&frame->base, allocator, type, &s_frame_prebuilt_vtable, stream_id);
  685. /* encoded_buf has the exact amount of space necessary for the full encoded frame.
  686. * The constructor of our subclass must finish filling up encoded_buf with the payload. */
  687. frame->encoded_buf = aws_byte_buf_from_empty_array(storage, encoded_frame_len);
  688. /* cursor points to full capacity of encoded_buf.
  689. * Our subclass's constructor will finish writing the payload and fill encoded_buf to capacity.
  690. * When encode() is called, we'll copy cursor's contents into available output space and advance the cursor. */
  691. frame->cursor = aws_byte_cursor_from_array(storage, encoded_frame_len);
  692. /* Write frame prefix */
  693. s_frame_prefix_encode(type, stream_id, payload_len, flags, &frame->encoded_buf);
  694. return frame;
  695. }
  696. static void s_frame_prebuilt_destroy(struct aws_h2_frame *frame_base) {
  697. aws_mem_release(frame_base->alloc, frame_base);
  698. }
  699. static int s_frame_prebuilt_encode(
  700. struct aws_h2_frame *frame_base,
  701. struct aws_h2_frame_encoder *encoder,
  702. struct aws_byte_buf *output,
  703. bool *complete) {
  704. (void)encoder;
  705. struct aws_h2_frame_prebuilt *frame = AWS_CONTAINER_OF(frame_base, struct aws_h2_frame_prebuilt, base);
  706. /* encoded_buf should have been filled to capacity during construction */
  707. AWS_ASSERT(frame->encoded_buf.len == frame->encoded_buf.capacity);
  708. /* After construction, cursor points to the full contents of encoded_buf.
  709. * As encode() is called, we copy the contents to output and advance the cursor. */
  710. if (frame->cursor.len == frame->encoded_buf.len) {
  711. /* We haven't sent anything yet, announce start of frame */
  712. ENCODER_LOGF(
  713. TRACE,
  714. encoder,
  715. "Encoding frame type=%s stream_id=%" PRIu32,
  716. aws_h2_frame_type_to_str(frame->base.type),
  717. frame->base.stream_id);
  718. } else {
  719. /* We've already sent a bit, announce that we're resuming */
  720. ENCODER_LOGF(
  721. TRACE,
  722. encoder,
  723. "Resume encoding frame type=%s stream_id=%" PRIu32,
  724. aws_h2_frame_type_to_str(frame->base.type),
  725. frame->base.stream_id);
  726. }
  727. bool writes_ok = true;
  728. /* Copy as much as we can from cursor (pre-encoded frame contents) to output.
  729. * Advance the cursor to mark our progress. */
  730. size_t chunk_len = aws_min_size(frame->cursor.len, output->capacity - output->len);
  731. struct aws_byte_cursor chunk = aws_byte_cursor_advance(&frame->cursor, chunk_len);
  732. writes_ok &= aws_byte_buf_write_from_whole_cursor(output, chunk);
  733. AWS_ASSERT(writes_ok);
  734. (void)writes_ok;
  735. if (frame->cursor.len == 0) {
  736. *complete = true;
  737. } else {
  738. ENCODER_LOGF(
  739. TRACE,
  740. encoder,
  741. "Incomplete encoding of frame type=%s stream_id=%" PRIu32 ", will resume later...",
  742. aws_h2_frame_type_to_str(frame->base.type),
  743. frame->base.stream_id);
  744. *complete = false;
  745. }
  746. return AWS_OP_SUCCESS;
  747. }
  748. /***********************************************************************************************************************
  749. * PRIORITY
  750. **********************************************************************************************************************/
  751. struct aws_h2_frame *aws_h2_frame_new_priority(
  752. struct aws_allocator *allocator,
  753. uint32_t stream_id,
  754. const struct aws_h2_frame_priority_settings *priority) {
  755. AWS_PRECONDITION(allocator);
  756. AWS_PRECONDITION(priority);
  757. if (aws_h2_validate_stream_id(stream_id) || aws_h2_validate_stream_id(priority->stream_dependency)) {
  758. return NULL;
  759. }
  760. /* PRIORITY can be pre-encoded */
  761. const uint8_t flags = 0;
  762. const size_t payload_len = s_frame_priority_settings_size;
  763. struct aws_h2_frame_prebuilt *frame =
  764. s_h2_frame_new_prebuilt(allocator, AWS_H2_FRAME_T_PRIORITY, stream_id, payload_len, flags);
  765. if (!frame) {
  766. return NULL;
  767. }
  768. /* Write the priority settings */
  769. s_frame_priority_settings_encode(priority, &frame->encoded_buf);
  770. return &frame->base;
  771. }
  772. /***********************************************************************************************************************
  773. * RST_STREAM
  774. **********************************************************************************************************************/
  775. static const size_t s_frame_rst_stream_length = 4;
  776. struct aws_h2_frame *aws_h2_frame_new_rst_stream(
  777. struct aws_allocator *allocator,
  778. uint32_t stream_id,
  779. uint32_t error_code) {
  780. if (aws_h2_validate_stream_id(stream_id)) {
  781. return NULL;
  782. }
  783. /* RST_STREAM can be pre-encoded */
  784. const uint8_t flags = 0;
  785. const size_t payload_len = s_frame_rst_stream_length;
  786. struct aws_h2_frame_prebuilt *frame =
  787. s_h2_frame_new_prebuilt(allocator, AWS_H2_FRAME_T_RST_STREAM, stream_id, payload_len, flags);
  788. if (!frame) {
  789. return NULL;
  790. }
  791. /* Write RST_STREAM payload (RFC-7540 6.4):
  792. * +---------------------------------------------------------------+
  793. * | Error Code (32) |
  794. * +---------------------------------------------------------------+
  795. */
  796. bool writes_ok = true;
  797. writes_ok &= aws_byte_buf_write_be32(&frame->encoded_buf, error_code);
  798. AWS_ASSERT(writes_ok);
  799. (void)writes_ok;
  800. return &frame->base;
  801. }
  802. /***********************************************************************************************************************
  803. * SETTINGS
  804. **********************************************************************************************************************/
  805. static const size_t s_frame_setting_length = 6;
  806. struct aws_h2_frame *aws_h2_frame_new_settings(
  807. struct aws_allocator *allocator,
  808. const struct aws_http2_setting *settings_array,
  809. size_t num_settings,
  810. bool ack) {
  811. AWS_PRECONDITION(settings_array || num_settings == 0);
  812. /* Cannot send settings in an ACK frame */
  813. if (ack && num_settings > 0) {
  814. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  815. return NULL;
  816. }
  817. /* Check against insane edge case of too many settings to fit in a frame. */
  818. const size_t max_settings = s_prebuilt_payload_max() / s_frame_setting_length;
  819. if (num_settings > max_settings) {
  820. AWS_LOGF_ERROR(
  821. AWS_LS_HTTP_ENCODER,
  822. "Cannot create SETTINGS frame with %zu settings, the limit is %zu.",
  823. num_settings,
  824. max_settings);
  825. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  826. return NULL;
  827. }
  828. /* SETTINGS can be pre-encoded */
  829. const uint8_t flags = ack ? AWS_H2_FRAME_F_ACK : 0;
  830. const size_t payload_len = num_settings * s_frame_setting_length;
  831. const uint32_t stream_id = 0;
  832. struct aws_h2_frame_prebuilt *frame =
  833. s_h2_frame_new_prebuilt(allocator, AWS_H2_FRAME_T_SETTINGS, stream_id, payload_len, flags);
  834. if (!frame) {
  835. return NULL;
  836. }
  837. /* Write the settings, each one is encoded like (RFC-7540 6.5.1):
  838. * +-------------------------------+
  839. * | Identifier (16) |
  840. * +-------------------------------+-------------------------------+
  841. * | Value (32) |
  842. * +---------------------------------------------------------------+
  843. */
  844. bool writes_ok = true;
  845. for (size_t i = 0; i < num_settings; ++i) {
  846. writes_ok &= aws_byte_buf_write_be16(&frame->encoded_buf, settings_array[i].id);
  847. writes_ok &= aws_byte_buf_write_be32(&frame->encoded_buf, settings_array[i].value);
  848. }
  849. AWS_ASSERT(writes_ok);
  850. (void)writes_ok;
  851. return &frame->base;
  852. }
  853. /***********************************************************************************************************************
  854. * PING
  855. **********************************************************************************************************************/
  856. struct aws_h2_frame *aws_h2_frame_new_ping(
  857. struct aws_allocator *allocator,
  858. bool ack,
  859. const uint8_t opaque_data[AWS_HTTP2_PING_DATA_SIZE]) {
  860. /* PING can be pre-encoded */
  861. const uint8_t flags = ack ? AWS_H2_FRAME_F_ACK : 0;
  862. const size_t payload_len = AWS_HTTP2_PING_DATA_SIZE;
  863. const uint32_t stream_id = 0;
  864. struct aws_h2_frame_prebuilt *frame =
  865. s_h2_frame_new_prebuilt(allocator, AWS_H2_FRAME_T_PING, stream_id, payload_len, flags);
  866. if (!frame) {
  867. return NULL;
  868. }
  869. /* Write the PING payload (RFC-7540 6.7):
  870. * +---------------------------------------------------------------+
  871. * | |
  872. * | Opaque Data (64) |
  873. * | |
  874. * +---------------------------------------------------------------+
  875. */
  876. bool writes_ok = true;
  877. writes_ok &= aws_byte_buf_write(&frame->encoded_buf, opaque_data, AWS_HTTP2_PING_DATA_SIZE);
  878. AWS_ASSERT(writes_ok);
  879. (void)writes_ok;
  880. /* PING responses SHOULD be given higher priority than any other frame */
  881. frame->base.high_priority = ack;
  882. return &frame->base;
  883. }
  884. /***********************************************************************************************************************
  885. * GOAWAY
  886. **********************************************************************************************************************/
  887. static const size_t s_frame_goaway_length_min = 8;
  888. struct aws_h2_frame *aws_h2_frame_new_goaway(
  889. struct aws_allocator *allocator,
  890. uint32_t last_stream_id,
  891. uint32_t error_code,
  892. struct aws_byte_cursor debug_data) {
  893. /* If debug_data is too long, don't sent it.
  894. * It's more important that the GOAWAY frame gets sent. */
  895. const size_t debug_data_max = s_prebuilt_payload_max() - s_frame_goaway_length_min;
  896. if (debug_data.len > debug_data_max) {
  897. AWS_LOGF_WARN(
  898. AWS_LS_HTTP_ENCODER,
  899. "Sending GOAWAY without debug-data. Debug-data size %zu exceeds internal limit of %zu",
  900. debug_data.len,
  901. debug_data_max);
  902. debug_data.len = 0;
  903. }
  904. /* It would be illegal to send a lower value, this is unrecoverable */
  905. AWS_FATAL_ASSERT(last_stream_id <= AWS_H2_STREAM_ID_MAX);
  906. /* GOAWAY can be pre-encoded */
  907. const uint8_t flags = 0;
  908. const size_t payload_len = debug_data.len + s_frame_goaway_length_min;
  909. const uint32_t stream_id = 0;
  910. struct aws_h2_frame_prebuilt *frame =
  911. s_h2_frame_new_prebuilt(allocator, AWS_H2_FRAME_T_GOAWAY, stream_id, payload_len, flags);
  912. if (!frame) {
  913. return NULL;
  914. }
  915. /* Write the GOAWAY payload (RFC-7540 6.8):
  916. * +-+-------------------------------------------------------------+
  917. * |R| Last-Stream-ID (31) |
  918. * +-+-------------------------------------------------------------+
  919. * | Error Code (32) |
  920. * +---------------------------------------------------------------+
  921. * | Additional Debug Data (*) |
  922. * +---------------------------------------------------------------+
  923. */
  924. bool writes_ok = true;
  925. writes_ok &= aws_byte_buf_write_be32(&frame->encoded_buf, last_stream_id);
  926. writes_ok &= aws_byte_buf_write_be32(&frame->encoded_buf, error_code);
  927. writes_ok &= aws_byte_buf_write_from_whole_cursor(&frame->encoded_buf, debug_data);
  928. AWS_ASSERT(writes_ok);
  929. (void)writes_ok;
  930. return &frame->base;
  931. }
  932. /***********************************************************************************************************************
  933. * WINDOW_UPDATE
  934. **********************************************************************************************************************/
  935. static const size_t s_frame_window_update_length = 4;
  936. struct aws_h2_frame *aws_h2_frame_new_window_update(
  937. struct aws_allocator *allocator,
  938. uint32_t stream_id,
  939. uint32_t window_size_increment) {
  940. /* Note: stream_id may be zero or non-zero */
  941. if (stream_id > AWS_H2_STREAM_ID_MAX) {
  942. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  943. return NULL;
  944. }
  945. if (window_size_increment > AWS_H2_WINDOW_UPDATE_MAX) {
  946. AWS_LOGF_ERROR(
  947. AWS_LS_HTTP_ENCODER,
  948. "Window increment size %" PRIu32 " exceeds HTTP/2 max %" PRIu32,
  949. window_size_increment,
  950. AWS_H2_WINDOW_UPDATE_MAX);
  951. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  952. return NULL;
  953. }
  954. /* WINDOW_UPDATE can be pre-encoded */
  955. const uint8_t flags = 0;
  956. const size_t payload_len = s_frame_window_update_length;
  957. struct aws_h2_frame_prebuilt *frame =
  958. s_h2_frame_new_prebuilt(allocator, AWS_H2_FRAME_T_WINDOW_UPDATE, stream_id, payload_len, flags);
  959. if (!frame) {
  960. return NULL;
  961. }
  962. /* Write the WINDOW_UPDATE payload (RFC-7540 6.9):
  963. * +-+-------------------------------------------------------------+
  964. * |R| Window Size Increment (31) |
  965. * +-+-------------------------------------------------------------+
  966. */
  967. bool writes_ok = true;
  968. writes_ok &= aws_byte_buf_write_be32(&frame->encoded_buf, window_size_increment);
  969. AWS_ASSERT(writes_ok);
  970. (void)writes_ok;
  971. return &frame->base;
  972. }
  973. void aws_h2_frame_destroy(struct aws_h2_frame *frame) {
  974. if (frame) {
  975. frame->vtable->destroy(frame);
  976. }
  977. }
  978. int aws_h2_encode_frame(
  979. struct aws_h2_frame_encoder *encoder,
  980. struct aws_h2_frame *frame,
  981. struct aws_byte_buf *output,
  982. bool *frame_complete) {
  983. AWS_PRECONDITION(encoder);
  984. AWS_PRECONDITION(frame);
  985. AWS_PRECONDITION(output);
  986. AWS_PRECONDITION(frame_complete);
  987. if (encoder->has_errored) {
  988. ENCODER_LOG(ERROR, encoder, "Encoder cannot be used again after an error");
  989. return aws_raise_error(AWS_ERROR_INVALID_STATE);
  990. }
  991. if (encoder->current_frame && (encoder->current_frame != frame)) {
  992. ENCODER_LOG(ERROR, encoder, "Cannot encode new frame until previous frame completes");
  993. return aws_raise_error(AWS_ERROR_INVALID_STATE);
  994. }
  995. *frame_complete = false;
  996. if (frame->vtable->encode(frame, encoder, output, frame_complete)) {
  997. ENCODER_LOGF(
  998. ERROR,
  999. encoder,
  1000. "Failed to encode frame type=%s stream_id=%" PRIu32 ", %s",
  1001. aws_h2_frame_type_to_str(frame->type),
  1002. frame->stream_id,
  1003. aws_error_name(aws_last_error()));
  1004. encoder->has_errored = true;
  1005. return AWS_OP_ERR;
  1006. }
  1007. encoder->current_frame = *frame_complete ? NULL : frame;
  1008. return AWS_OP_SUCCESS;
  1009. }
  1010. void aws_h2_frame_encoder_set_setting_header_table_size(struct aws_h2_frame_encoder *encoder, uint32_t data) {
  1011. /* Setting for dynamic table size changed from peer, we will update the dynamic table size when we encoder the next
  1012. * header block */
  1013. aws_hpack_encoder_update_max_table_size(&encoder->hpack, data);
  1014. }
  1015. void aws_h2_frame_encoder_set_setting_max_frame_size(struct aws_h2_frame_encoder *encoder, uint32_t data) {
  1016. encoder->settings.max_frame_size = data;
  1017. }