event_stream.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/event-stream/event_stream.h>
  6. #include <aws/checksums/crc.h>
  7. #include <aws/common/encoding.h>
  8. #include <aws/io/io.h>
  9. #include <inttypes.h>
  10. #define LIB_NAME "libaws-c-event-stream"
  11. #if _MSC_VER
  12. # pragma warning(push)
  13. # pragma warning(disable : 4221) /* aggregate initializer using local variable addresses */
  14. # pragma warning(disable : 4204) /* non-constant aggregate initializer */
  15. # pragma warning(disable : 4306) /* msft doesn't trust us to do pointer arithmetic. */
  16. #endif
  17. static struct aws_error_info s_errors[] = {
  18. AWS_DEFINE_ERROR_INFO(AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH, "Buffer length mismatch", LIB_NAME),
  19. AWS_DEFINE_ERROR_INFO(AWS_ERROR_EVENT_STREAM_INSUFFICIENT_BUFFER_LEN, "insufficient buffer length", LIB_NAME),
  20. AWS_DEFINE_ERROR_INFO(
  21. AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED,
  22. "a field for the message was too large",
  23. LIB_NAME),
  24. AWS_DEFINE_ERROR_INFO(AWS_ERROR_EVENT_STREAM_PRELUDE_CHECKSUM_FAILURE, "prelude checksum was incorrect", LIB_NAME),
  25. AWS_DEFINE_ERROR_INFO(AWS_ERROR_EVENT_STREAM_MESSAGE_CHECKSUM_FAILURE, "message checksum was incorrect", LIB_NAME),
  26. AWS_DEFINE_ERROR_INFO(
  27. AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN,
  28. "message headers length was incorrect",
  29. LIB_NAME),
  30. AWS_DEFINE_ERROR_INFO(
  31. AWS_ERROR_EVENT_STREAM_MESSAGE_UNKNOWN_HEADER_TYPE,
  32. "An unknown header type was encountered",
  33. LIB_NAME),
  34. AWS_DEFINE_ERROR_INFO(
  35. AWS_ERROR_EVENT_STREAM_MESSAGE_PARSER_ILLEGAL_STATE,
  36. "message parser encountered an illegal state",
  37. LIB_NAME),
  38. AWS_DEFINE_ERROR_INFO(
  39. AWS_ERROR_EVENT_STREAM_RPC_CONNECTION_CLOSED,
  40. "event stream rpc connection has been closed",
  41. LIB_NAME),
  42. AWS_DEFINE_ERROR_INFO(
  43. AWS_ERROR_EVENT_STREAM_RPC_PROTOCOL_ERROR,
  44. "event stream rpc connection has encountered a protocol error",
  45. LIB_NAME),
  46. AWS_DEFINE_ERROR_INFO(
  47. AWS_ERROR_EVENT_STREAM_RPC_STREAM_CLOSED,
  48. "event stream rpc connection stream is closed.",
  49. LIB_NAME),
  50. AWS_DEFINE_ERROR_INFO(
  51. AWS_ERROR_EVENT_STREAM_RPC_STREAM_NOT_ACTIVATED,
  52. "event stream rpc stream continuation was not successfully activated before use. Call "
  53. "aws_event_stream_rpc_client_continuation_activate()"
  54. " before using a stream continuation token.",
  55. LIB_NAME),
  56. };
  57. static struct aws_error_info_list s_list = {
  58. .error_list = s_errors,
  59. .count = sizeof(s_errors) / sizeof(struct aws_error_info),
  60. };
  61. static bool s_event_stream_library_initialized = false;
  62. static struct aws_log_subject_info s_event_stream_log_subject_infos[] = {
  63. DEFINE_LOG_SUBJECT_INFO(
  64. AWS_LS_EVENT_STREAM_GENERAL,
  65. "event-stream-general",
  66. "Subject for aws-c-event-stream logging that defies categorization."),
  67. DEFINE_LOG_SUBJECT_INFO(
  68. AWS_LS_EVENT_STREAM_CHANNEL_HANDLER,
  69. "event-stream-channel-handler",
  70. "Subject for event-stream channel handler related logging."),
  71. DEFINE_LOG_SUBJECT_INFO(
  72. AWS_LS_EVENT_STREAM_RPC_SERVER,
  73. "event-stream-rpc-server",
  74. "Subject for event-stream rpc server."),
  75. DEFINE_LOG_SUBJECT_INFO(
  76. AWS_LS_EVENT_STREAM_RPC_CLIENT,
  77. "event-stream-rpc-client",
  78. "Subject for event-stream rpc client."),
  79. };
  80. static struct aws_log_subject_info_list s_event_stream_log_subject_list = {
  81. .subject_list = s_event_stream_log_subject_infos,
  82. .count = AWS_ARRAY_SIZE(s_event_stream_log_subject_infos),
  83. };
  84. void aws_event_stream_library_init(struct aws_allocator *allocator) {
  85. if (!s_event_stream_library_initialized) {
  86. s_event_stream_library_initialized = true;
  87. aws_io_library_init(allocator);
  88. aws_register_error_info(&s_list);
  89. aws_register_log_subject_info_list(&s_event_stream_log_subject_list);
  90. }
  91. }
  92. void aws_event_stream_library_clean_up(void) {
  93. if (s_event_stream_library_initialized) {
  94. s_event_stream_library_initialized = false;
  95. aws_unregister_error_info(&s_list);
  96. aws_io_library_clean_up();
  97. }
  98. }
  99. #define TOTAL_LEN_OFFSET 0
  100. #define PRELUDE_CRC_OFFSET (sizeof(uint32_t) + sizeof(uint32_t))
  101. #define HEADER_LEN_OFFSET sizeof(uint32_t)
  102. /* Computes the byte length necessary to store the headers represented in the headers list.
  103. * returns that length. */
  104. uint32_t aws_event_stream_compute_headers_required_buffer_len(const struct aws_array_list *headers) {
  105. if (!headers || !aws_array_list_length(headers)) {
  106. return 0;
  107. }
  108. size_t headers_count = aws_array_list_length(headers);
  109. size_t headers_len = 0;
  110. for (size_t i = 0; i < headers_count; ++i) {
  111. struct aws_event_stream_header_value_pair *header = NULL;
  112. aws_array_list_get_at_ptr(headers, (void **)&header, i);
  113. headers_len += sizeof(header->header_name_len) + header->header_name_len + 1;
  114. if (header->header_value_type == AWS_EVENT_STREAM_HEADER_STRING ||
  115. header->header_value_type == AWS_EVENT_STREAM_HEADER_BYTE_BUF) {
  116. headers_len += sizeof(header->header_value_len);
  117. }
  118. if (header->header_value_type != AWS_EVENT_STREAM_HEADER_BOOL_FALSE &&
  119. header->header_value_type != AWS_EVENT_STREAM_HEADER_BOOL_TRUE) {
  120. headers_len += header->header_value_len;
  121. }
  122. }
  123. return (uint32_t)headers_len;
  124. }
  125. /* adds the headers represented in the headers list to the buffer.
  126. returns the new buffer offset for use elsewhere. Assumes buffer length is at least the length of the return value
  127. from compute_headers_length() */
  128. size_t aws_event_stream_write_headers_to_buffer(const struct aws_array_list *headers, uint8_t *buffer) {
  129. if (!headers || !aws_array_list_length(headers)) {
  130. return 0;
  131. }
  132. size_t headers_count = aws_array_list_length(headers);
  133. uint8_t *buffer_alias = buffer;
  134. for (size_t i = 0; i < headers_count; ++i) {
  135. struct aws_event_stream_header_value_pair *header = NULL;
  136. aws_array_list_get_at_ptr(headers, (void **)&header, i);
  137. *buffer_alias = (uint8_t)header->header_name_len;
  138. buffer_alias++;
  139. memcpy(buffer_alias, header->header_name, (size_t)header->header_name_len);
  140. buffer_alias += header->header_name_len;
  141. *buffer_alias = (uint8_t)header->header_value_type;
  142. buffer_alias++;
  143. switch (header->header_value_type) {
  144. case AWS_EVENT_STREAM_HEADER_BOOL_FALSE:
  145. case AWS_EVENT_STREAM_HEADER_BOOL_TRUE:
  146. break;
  147. case AWS_EVENT_STREAM_HEADER_BYTE:
  148. *buffer_alias = header->header_value.static_val[0];
  149. buffer_alias++;
  150. break;
  151. /* additions of integers here assume the endianness conversion has already happened */
  152. case AWS_EVENT_STREAM_HEADER_INT16:
  153. memcpy(buffer_alias, header->header_value.static_val, sizeof(uint16_t));
  154. buffer_alias += sizeof(uint16_t);
  155. break;
  156. case AWS_EVENT_STREAM_HEADER_INT32:
  157. memcpy(buffer_alias, header->header_value.static_val, sizeof(uint32_t));
  158. buffer_alias += sizeof(uint32_t);
  159. break;
  160. case AWS_EVENT_STREAM_HEADER_INT64:
  161. case AWS_EVENT_STREAM_HEADER_TIMESTAMP:
  162. memcpy(buffer_alias, header->header_value.static_val, sizeof(uint64_t));
  163. buffer_alias += sizeof(uint64_t);
  164. break;
  165. case AWS_EVENT_STREAM_HEADER_BYTE_BUF:
  166. case AWS_EVENT_STREAM_HEADER_STRING:
  167. aws_write_u16(header->header_value_len, buffer_alias);
  168. buffer_alias += sizeof(uint16_t);
  169. memcpy(buffer_alias, header->header_value.variable_len_val, header->header_value_len);
  170. buffer_alias += header->header_value_len;
  171. break;
  172. case AWS_EVENT_STREAM_HEADER_UUID:
  173. memcpy(buffer_alias, header->header_value.static_val, 16);
  174. buffer_alias += header->header_value_len;
  175. break;
  176. }
  177. }
  178. return buffer_alias - buffer;
  179. }
  180. int aws_event_stream_read_headers_from_buffer(
  181. struct aws_array_list *headers,
  182. const uint8_t *buffer,
  183. size_t headers_len) {
  184. if (AWS_UNLIKELY(headers_len > AWS_EVENT_STREAM_MAX_HEADERS_SIZE)) {
  185. return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED);
  186. }
  187. /* iterate the buffer per header. */
  188. const uint8_t *buffer_start = buffer;
  189. while ((size_t)(buffer - buffer_start) < headers_len) {
  190. struct aws_event_stream_header_value_pair header;
  191. AWS_ZERO_STRUCT(header);
  192. /* get the header info from the buffer, make sure to increment buffer offset. */
  193. header.header_name_len = *buffer;
  194. buffer += sizeof(header.header_name_len);
  195. memcpy((void *)header.header_name, buffer, (size_t)header.header_name_len);
  196. buffer += header.header_name_len;
  197. header.header_value_type = (enum aws_event_stream_header_value_type) * buffer;
  198. buffer++;
  199. switch (header.header_value_type) {
  200. case AWS_EVENT_STREAM_HEADER_BOOL_FALSE:
  201. header.header_value_len = 0;
  202. header.header_value.static_val[0] = 0;
  203. break;
  204. case AWS_EVENT_STREAM_HEADER_BOOL_TRUE:
  205. header.header_value_len = 0;
  206. header.header_value.static_val[0] = 1;
  207. break;
  208. case AWS_EVENT_STREAM_HEADER_BYTE:
  209. header.header_value_len = sizeof(uint8_t);
  210. header.header_value.static_val[0] = *buffer;
  211. buffer++;
  212. break;
  213. case AWS_EVENT_STREAM_HEADER_INT16:
  214. header.header_value_len = sizeof(uint16_t);
  215. memcpy(header.header_value.static_val, buffer, sizeof(uint16_t));
  216. buffer += sizeof(uint16_t);
  217. break;
  218. case AWS_EVENT_STREAM_HEADER_INT32:
  219. header.header_value_len = sizeof(uint32_t);
  220. memcpy(header.header_value.static_val, buffer, sizeof(uint32_t));
  221. buffer += sizeof(uint32_t);
  222. break;
  223. case AWS_EVENT_STREAM_HEADER_INT64:
  224. case AWS_EVENT_STREAM_HEADER_TIMESTAMP:
  225. header.header_value_len = sizeof(uint64_t);
  226. memcpy(header.header_value.static_val, buffer, sizeof(uint64_t));
  227. buffer += sizeof(uint64_t);
  228. break;
  229. case AWS_EVENT_STREAM_HEADER_BYTE_BUF:
  230. case AWS_EVENT_STREAM_HEADER_STRING:
  231. header.header_value_len = aws_read_u16(buffer);
  232. buffer += sizeof(header.header_value_len);
  233. header.header_value.variable_len_val = (uint8_t *)buffer;
  234. buffer += header.header_value_len;
  235. break;
  236. case AWS_EVENT_STREAM_HEADER_UUID:
  237. header.header_value_len = 16;
  238. memcpy(header.header_value.static_val, buffer, 16);
  239. buffer += header.header_value_len;
  240. break;
  241. }
  242. if (aws_array_list_push_back(headers, (const void *)&header)) {
  243. return AWS_OP_ERR;
  244. }
  245. }
  246. return AWS_OP_SUCCESS;
  247. }
  248. /* initialize message with the arguments
  249. * the underlying buffer will be allocated and payload will be copied.
  250. * see specification, this code should simply add these fields according to that.*/
  251. int aws_event_stream_message_init(
  252. struct aws_event_stream_message *message,
  253. struct aws_allocator *alloc,
  254. struct aws_array_list *headers,
  255. struct aws_byte_buf *payload) {
  256. size_t payload_len = payload ? payload->len : 0;
  257. uint32_t headers_length = aws_event_stream_compute_headers_required_buffer_len(headers);
  258. if (AWS_UNLIKELY(headers_length > AWS_EVENT_STREAM_MAX_HEADERS_SIZE)) {
  259. return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED);
  260. }
  261. uint32_t total_length =
  262. (uint32_t)(AWS_EVENT_STREAM_PRELUDE_LENGTH + headers_length + payload_len + AWS_EVENT_STREAM_TRAILER_LENGTH);
  263. if (AWS_UNLIKELY(total_length < headers_length || total_length < payload_len)) {
  264. return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
  265. }
  266. if (AWS_UNLIKELY(total_length > AWS_EVENT_STREAM_MAX_MESSAGE_SIZE)) {
  267. return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED);
  268. }
  269. message->alloc = alloc;
  270. message->message_buffer = aws_mem_acquire(message->alloc, total_length);
  271. if (message->message_buffer) {
  272. message->owns_buffer = 1;
  273. aws_write_u32(total_length, message->message_buffer);
  274. uint8_t *buffer_offset = message->message_buffer + sizeof(total_length);
  275. aws_write_u32(headers_length, buffer_offset);
  276. buffer_offset += sizeof(headers_length);
  277. uint32_t running_crc =
  278. aws_checksums_crc32(message->message_buffer, (int)(buffer_offset - message->message_buffer), 0);
  279. const uint8_t *message_crc_boundary_start = buffer_offset;
  280. aws_write_u32(running_crc, buffer_offset);
  281. buffer_offset += sizeof(running_crc);
  282. if (headers_length) {
  283. buffer_offset += aws_event_stream_write_headers_to_buffer(headers, buffer_offset);
  284. }
  285. if (payload) {
  286. memcpy(buffer_offset, payload->buffer, payload->len);
  287. buffer_offset += payload->len;
  288. }
  289. running_crc = aws_checksums_crc32(
  290. message_crc_boundary_start, (int)(buffer_offset - message_crc_boundary_start), running_crc);
  291. aws_write_u32(running_crc, buffer_offset);
  292. return AWS_OP_SUCCESS;
  293. }
  294. return aws_raise_error(AWS_ERROR_OOM);
  295. }
  296. /* add buffer to the message (non-owning). Verify buffer crcs and that length fields are reasonable. */
  297. int aws_event_stream_message_from_buffer(
  298. struct aws_event_stream_message *message,
  299. struct aws_allocator *alloc,
  300. struct aws_byte_buf *buffer) {
  301. AWS_ASSERT(buffer);
  302. message->alloc = alloc;
  303. message->owns_buffer = 0;
  304. if (AWS_UNLIKELY(buffer->len < AWS_EVENT_STREAM_PRELUDE_LENGTH + AWS_EVENT_STREAM_TRAILER_LENGTH)) {
  305. return aws_raise_error(AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH);
  306. }
  307. uint32_t message_length = aws_read_u32(buffer->buffer + TOTAL_LEN_OFFSET);
  308. if (AWS_UNLIKELY(message_length != buffer->len)) {
  309. return aws_raise_error(AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH);
  310. }
  311. if (AWS_UNLIKELY(message_length > AWS_EVENT_STREAM_MAX_MESSAGE_SIZE)) {
  312. return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED);
  313. }
  314. uint32_t running_crc = aws_checksums_crc32(buffer->buffer, (int)PRELUDE_CRC_OFFSET, 0);
  315. uint32_t prelude_crc = aws_read_u32(buffer->buffer + PRELUDE_CRC_OFFSET);
  316. if (running_crc != prelude_crc) {
  317. return aws_raise_error(AWS_ERROR_EVENT_STREAM_PRELUDE_CHECKSUM_FAILURE);
  318. }
  319. running_crc = aws_checksums_crc32(
  320. buffer->buffer + PRELUDE_CRC_OFFSET,
  321. (int)(message_length - PRELUDE_CRC_OFFSET - AWS_EVENT_STREAM_TRAILER_LENGTH),
  322. running_crc);
  323. uint32_t message_crc = aws_read_u32(buffer->buffer + message_length - AWS_EVENT_STREAM_TRAILER_LENGTH);
  324. if (running_crc != message_crc) {
  325. return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_CHECKSUM_FAILURE);
  326. }
  327. message->message_buffer = buffer->buffer;
  328. if (aws_event_stream_message_headers_len(message) >
  329. message_length - AWS_EVENT_STREAM_PRELUDE_LENGTH - AWS_EVENT_STREAM_TRAILER_LENGTH) {
  330. message->message_buffer = 0;
  331. return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN);
  332. }
  333. return AWS_OP_SUCCESS;
  334. }
  335. /* Verify buffer crcs and that length fields are reasonable. Once that is done, the buffer is copied to the message. */
  336. int aws_event_stream_message_from_buffer_copy(
  337. struct aws_event_stream_message *message,
  338. struct aws_allocator *alloc,
  339. const struct aws_byte_buf *buffer) {
  340. int parse_value = aws_event_stream_message_from_buffer(message, alloc, (struct aws_byte_buf *)buffer);
  341. if (!parse_value) {
  342. message->message_buffer = aws_mem_acquire(alloc, buffer->len);
  343. if (message->message_buffer) {
  344. memcpy(message->message_buffer, buffer->buffer, buffer->len);
  345. message->alloc = alloc;
  346. message->owns_buffer = 1;
  347. return AWS_OP_SUCCESS;
  348. }
  349. return aws_raise_error(AWS_ERROR_OOM);
  350. }
  351. return parse_value;
  352. }
  353. /* if buffer is owned, release the memory. */
  354. void aws_event_stream_message_clean_up(struct aws_event_stream_message *message) {
  355. if (message->message_buffer && message->owns_buffer) {
  356. aws_mem_release(message->alloc, message->message_buffer);
  357. }
  358. }
  359. uint32_t aws_event_stream_message_total_length(const struct aws_event_stream_message *message) {
  360. return aws_read_u32(message->message_buffer + TOTAL_LEN_OFFSET);
  361. }
  362. uint32_t aws_event_stream_message_headers_len(const struct aws_event_stream_message *message) {
  363. return aws_read_u32(message->message_buffer + HEADER_LEN_OFFSET);
  364. }
  365. uint32_t aws_event_stream_message_prelude_crc(const struct aws_event_stream_message *message) {
  366. return aws_read_u32(message->message_buffer + PRELUDE_CRC_OFFSET);
  367. }
  368. int aws_event_stream_message_headers(const struct aws_event_stream_message *message, struct aws_array_list *headers) {
  369. return aws_event_stream_read_headers_from_buffer(
  370. headers,
  371. message->message_buffer + AWS_EVENT_STREAM_PRELUDE_LENGTH,
  372. aws_event_stream_message_headers_len(message));
  373. }
  374. const uint8_t *aws_event_stream_message_payload(const struct aws_event_stream_message *message) {
  375. return message->message_buffer + AWS_EVENT_STREAM_PRELUDE_LENGTH + aws_event_stream_message_headers_len(message);
  376. }
  377. uint32_t aws_event_stream_message_payload_len(const struct aws_event_stream_message *message) {
  378. return aws_event_stream_message_total_length(message) -
  379. (AWS_EVENT_STREAM_PRELUDE_LENGTH + aws_event_stream_message_headers_len(message) +
  380. AWS_EVENT_STREAM_TRAILER_LENGTH);
  381. }
  382. uint32_t aws_event_stream_message_message_crc(const struct aws_event_stream_message *message) {
  383. return aws_read_u32(
  384. message->message_buffer + (aws_event_stream_message_total_length(message) - AWS_EVENT_STREAM_TRAILER_LENGTH));
  385. }
  386. const uint8_t *aws_event_stream_message_buffer(const struct aws_event_stream_message *message) {
  387. return message->message_buffer;
  388. }
  389. #define DEBUG_STR_PRELUDE_TOTAL_LEN "\"total_length\": "
  390. #define DEBUG_STR_PRELUDE_HDRS_LEN "\"headers_length\": "
  391. #define DEBUG_STR_PRELUDE_CRC "\"prelude_crc\": "
  392. #define DEBUG_STR_MESSAGE_CRC "\"message_crc\": "
  393. #define DEBUG_STR_HEADER_NAME "\"name\": "
  394. #define DEBUG_STR_HEADER_VALUE "\"value\": "
  395. #define DEBUG_STR_HEADER_TYPE "\"type\": "
  396. int aws_event_stream_message_to_debug_str(FILE *fd, const struct aws_event_stream_message *message) {
  397. struct aws_array_list headers;
  398. aws_event_stream_headers_list_init(&headers, message->alloc);
  399. aws_event_stream_message_headers(message, &headers);
  400. fprintf(
  401. fd,
  402. "{\n " DEBUG_STR_PRELUDE_TOTAL_LEN "%d,\n " DEBUG_STR_PRELUDE_HDRS_LEN "%d,\n " DEBUG_STR_PRELUDE_CRC
  403. "%d,\n",
  404. aws_event_stream_message_total_length(message),
  405. aws_event_stream_message_headers_len(message),
  406. aws_event_stream_message_prelude_crc(message));
  407. int count = 0;
  408. uint16_t headers_count = (uint16_t)aws_array_list_length(&headers);
  409. fprintf(fd, " \"headers\": [");
  410. for (uint16_t i = 0; i < headers_count; ++i) {
  411. struct aws_event_stream_header_value_pair *header = NULL;
  412. aws_array_list_get_at_ptr(&headers, (void **)&header, i);
  413. fprintf(fd, " {\n");
  414. fprintf(fd, " " DEBUG_STR_HEADER_NAME "\"");
  415. fwrite(header->header_name, sizeof(char), (size_t)header->header_name_len, fd);
  416. fprintf(fd, "\",\n");
  417. fprintf(fd, " " DEBUG_STR_HEADER_TYPE "%d,\n", header->header_value_type);
  418. if (header->header_value_type == AWS_EVENT_STREAM_HEADER_BOOL_FALSE) {
  419. fprintf(fd, " " DEBUG_STR_HEADER_VALUE "false\n");
  420. } else if (header->header_value_type == AWS_EVENT_STREAM_HEADER_BOOL_TRUE) {
  421. fprintf(fd, " " DEBUG_STR_HEADER_VALUE "true\n");
  422. } else if (header->header_value_type == AWS_EVENT_STREAM_HEADER_BYTE) {
  423. int8_t int_value = (int8_t)header->header_value.static_val[0];
  424. fprintf(fd, " " DEBUG_STR_HEADER_VALUE "%d\n", (int)int_value);
  425. } else if (header->header_value_type == AWS_EVENT_STREAM_HEADER_INT16) {
  426. int16_t int_value = aws_read_u16(header->header_value.static_val);
  427. fprintf(fd, " " DEBUG_STR_HEADER_VALUE "%d\n", (int)int_value);
  428. } else if (header->header_value_type == AWS_EVENT_STREAM_HEADER_INT32) {
  429. int32_t int_value = (int32_t)aws_read_u32(header->header_value.static_val);
  430. fprintf(fd, " " DEBUG_STR_HEADER_VALUE "%d\n", (int)int_value);
  431. } else if (
  432. header->header_value_type == AWS_EVENT_STREAM_HEADER_INT64 ||
  433. header->header_value_type == AWS_EVENT_STREAM_HEADER_TIMESTAMP) {
  434. int64_t int_value = (int64_t)aws_read_u64(header->header_value.static_val);
  435. fprintf(fd, " " DEBUG_STR_HEADER_VALUE "%lld\n", (long long)int_value);
  436. } else {
  437. size_t buffer_len = 0;
  438. aws_base64_compute_encoded_len(header->header_value_len, &buffer_len);
  439. char *encoded_buffer = (char *)aws_mem_acquire(message->alloc, buffer_len);
  440. if (!encoded_buffer) {
  441. return aws_raise_error(AWS_ERROR_OOM);
  442. }
  443. struct aws_byte_buf encode_output = aws_byte_buf_from_array((uint8_t *)encoded_buffer, buffer_len);
  444. if (header->header_value_type == AWS_EVENT_STREAM_HEADER_UUID) {
  445. struct aws_byte_cursor to_encode =
  446. aws_byte_cursor_from_array(header->header_value.static_val, header->header_value_len);
  447. aws_base64_encode(&to_encode, &encode_output);
  448. } else {
  449. struct aws_byte_cursor to_encode =
  450. aws_byte_cursor_from_array(header->header_value.variable_len_val, header->header_value_len);
  451. aws_base64_encode(&to_encode, &encode_output);
  452. }
  453. fprintf(fd, " " DEBUG_STR_HEADER_VALUE "\"%s\"\n", encoded_buffer);
  454. aws_mem_release(message->alloc, encoded_buffer);
  455. }
  456. fprintf(fd, " }");
  457. if (count < headers_count - 1) {
  458. fprintf(fd, ",");
  459. }
  460. fprintf(fd, "\n");
  461. count++;
  462. }
  463. aws_event_stream_headers_list_cleanup(&headers);
  464. fprintf(fd, " ],\n");
  465. size_t payload_len = aws_event_stream_message_payload_len(message);
  466. const uint8_t *payload = aws_event_stream_message_payload(message);
  467. size_t encoded_len = 0;
  468. aws_base64_compute_encoded_len(payload_len, &encoded_len);
  469. char *encoded_payload = (char *)aws_mem_acquire(message->alloc, encoded_len);
  470. if (!encoded_payload) {
  471. return aws_raise_error(AWS_ERROR_OOM);
  472. }
  473. struct aws_byte_cursor payload_buffer = aws_byte_cursor_from_array(payload, payload_len);
  474. struct aws_byte_buf encoded_payload_buffer = aws_byte_buf_from_array((uint8_t *)encoded_payload, encoded_len);
  475. aws_base64_encode(&payload_buffer, &encoded_payload_buffer);
  476. fprintf(fd, " \"payload\": \"%s\",\n", encoded_payload);
  477. fprintf(fd, " " DEBUG_STR_MESSAGE_CRC "%d\n}\n", aws_event_stream_message_message_crc(message));
  478. return AWS_OP_SUCCESS;
  479. }
  480. int aws_event_stream_headers_list_init(struct aws_array_list *headers, struct aws_allocator *allocator) {
  481. AWS_ASSERT(headers);
  482. AWS_ASSERT(allocator);
  483. return aws_array_list_init_dynamic(headers, allocator, 4, sizeof(struct aws_event_stream_header_value_pair));
  484. }
  485. void aws_event_stream_headers_list_cleanup(struct aws_array_list *headers) {
  486. if (AWS_UNLIKELY(!headers || !aws_array_list_is_valid(headers))) {
  487. return;
  488. }
  489. for (size_t i = 0; i < aws_array_list_length(headers); ++i) {
  490. struct aws_event_stream_header_value_pair *header = NULL;
  491. aws_array_list_get_at_ptr(headers, (void **)&header, i);
  492. if (header->value_owned) {
  493. aws_mem_release(headers->alloc, (void *)header->header_value.variable_len_val);
  494. }
  495. }
  496. aws_array_list_clean_up(headers);
  497. }
  498. static int s_add_variable_len_header(
  499. struct aws_array_list *headers,
  500. struct aws_event_stream_header_value_pair *header,
  501. const char *name,
  502. uint8_t name_len,
  503. uint8_t *value,
  504. uint16_t value_len,
  505. int8_t copy) {
  506. memcpy((void *)header->header_name, (void *)name, (size_t)name_len);
  507. if (copy) {
  508. header->header_value.variable_len_val = aws_mem_acquire(headers->alloc, value_len);
  509. if (!header->header_value.variable_len_val) {
  510. return aws_raise_error(AWS_ERROR_OOM);
  511. }
  512. header->value_owned = 1;
  513. memcpy((void *)header->header_value.variable_len_val, (void *)value, value_len);
  514. } else {
  515. header->value_owned = 0;
  516. header->header_value.variable_len_val = value;
  517. }
  518. if (aws_array_list_push_back(headers, (void *)header)) {
  519. if (header->value_owned) {
  520. aws_mem_release(headers->alloc, (void *)header->header_value.variable_len_val);
  521. }
  522. return AWS_OP_ERR;
  523. }
  524. return AWS_OP_SUCCESS;
  525. }
  526. int aws_event_stream_add_string_header(
  527. struct aws_array_list *headers,
  528. const char *name,
  529. uint8_t name_len,
  530. const char *value,
  531. uint16_t value_len,
  532. int8_t copy) {
  533. struct aws_event_stream_header_value_pair header = {.header_name_len = name_len,
  534. .header_value_len = value_len,
  535. .value_owned = copy,
  536. .header_value_type = AWS_EVENT_STREAM_HEADER_STRING};
  537. return s_add_variable_len_header(headers, &header, name, name_len, (uint8_t *)value, value_len, copy);
  538. }
  539. struct aws_event_stream_header_value_pair aws_event_stream_create_string_header(
  540. struct aws_byte_cursor name,
  541. struct aws_byte_cursor value) {
  542. AWS_PRECONDITION(name.len < INT8_MAX);
  543. AWS_PRECONDITION(value.len < INT16_MAX);
  544. struct aws_event_stream_header_value_pair header = {
  545. .header_value_type = AWS_EVENT_STREAM_HEADER_STRING,
  546. .header_value.variable_len_val = value.ptr,
  547. .header_value_len = (uint16_t)value.len,
  548. .header_name_len = (uint8_t)name.len,
  549. .value_owned = 0,
  550. };
  551. memcpy(header.header_name, name.ptr, name.len);
  552. return header;
  553. }
  554. struct aws_event_stream_header_value_pair aws_event_stream_create_int32_header(
  555. struct aws_byte_cursor name,
  556. int32_t value) {
  557. AWS_PRECONDITION(name.len < INT8_MAX);
  558. struct aws_event_stream_header_value_pair header = {
  559. .header_value_type = AWS_EVENT_STREAM_HEADER_INT32,
  560. .header_value_len = (uint16_t)sizeof(int32_t),
  561. .header_name_len = (uint8_t)name.len,
  562. .value_owned = 0,
  563. };
  564. memcpy(header.header_name, name.ptr, name.len);
  565. aws_write_u32((uint32_t)value, header.header_value.static_val);
  566. return header;
  567. }
  568. int aws_event_stream_add_byte_header(struct aws_array_list *headers, const char *name, uint8_t name_len, int8_t value) {
  569. struct aws_event_stream_header_value_pair header = {.header_name_len = name_len,
  570. .header_value_len = 1,
  571. .value_owned = 0,
  572. .header_value_type = AWS_EVENT_STREAM_HEADER_BYTE,
  573. .header_value.static_val[0] = (uint8_t)value};
  574. memcpy((void *)header.header_name, (void *)name, (size_t)name_len);
  575. return aws_array_list_push_back(headers, (void *)&header);
  576. }
  577. int aws_event_stream_add_bool_header(struct aws_array_list *headers, const char *name, uint8_t name_len, int8_t value) {
  578. struct aws_event_stream_header_value_pair header = {
  579. .header_name_len = name_len,
  580. .header_value_len = 0,
  581. .value_owned = 0,
  582. .header_value_type = value ? AWS_EVENT_STREAM_HEADER_BOOL_TRUE : AWS_EVENT_STREAM_HEADER_BOOL_FALSE,
  583. };
  584. memcpy((void *)header.header_name, (void *)name, (size_t)name_len);
  585. return aws_array_list_push_back(headers, (void *)&header);
  586. }
  587. int aws_event_stream_add_int16_header(
  588. struct aws_array_list *headers,
  589. const char *name,
  590. uint8_t name_len,
  591. int16_t value) {
  592. struct aws_event_stream_header_value_pair header = {
  593. .header_name_len = name_len,
  594. .header_value_len = sizeof(value),
  595. .value_owned = 0,
  596. .header_value_type = AWS_EVENT_STREAM_HEADER_INT16,
  597. };
  598. memcpy((void *)header.header_name, (void *)name, (size_t)name_len);
  599. aws_write_u16((uint16_t)value, header.header_value.static_val);
  600. return aws_array_list_push_back(headers, (void *)&header);
  601. }
  602. int aws_event_stream_add_int32_header(
  603. struct aws_array_list *headers,
  604. const char *name,
  605. uint8_t name_len,
  606. int32_t value) {
  607. struct aws_event_stream_header_value_pair header = {
  608. .header_name_len = name_len,
  609. .header_value_len = sizeof(value),
  610. .value_owned = 0,
  611. .header_value_type = AWS_EVENT_STREAM_HEADER_INT32,
  612. };
  613. memcpy((void *)header.header_name, (void *)name, (size_t)name_len);
  614. aws_write_u32((uint32_t)value, header.header_value.static_val);
  615. return aws_array_list_push_back(headers, (void *)&header);
  616. }
  617. int aws_event_stream_add_int64_header(
  618. struct aws_array_list *headers,
  619. const char *name,
  620. uint8_t name_len,
  621. int64_t value) {
  622. struct aws_event_stream_header_value_pair header = {
  623. .header_name_len = name_len,
  624. .header_value_len = sizeof(value),
  625. .value_owned = 0,
  626. .header_value_type = AWS_EVENT_STREAM_HEADER_INT64,
  627. };
  628. memcpy((void *)header.header_name, (void *)name, (size_t)name_len);
  629. aws_write_u64((uint64_t)value, header.header_value.static_val);
  630. return aws_array_list_push_back(headers, (void *)&header);
  631. }
  632. int aws_event_stream_add_bytebuf_header(
  633. struct aws_array_list *headers,
  634. const char *name,
  635. uint8_t name_len,
  636. uint8_t *value,
  637. uint16_t value_len,
  638. int8_t copy) {
  639. struct aws_event_stream_header_value_pair header = {.header_name_len = name_len,
  640. .header_value_len = value_len,
  641. .value_owned = copy,
  642. .header_value_type = AWS_EVENT_STREAM_HEADER_BYTE_BUF};
  643. return s_add_variable_len_header(headers, &header, name, name_len, value, value_len, copy);
  644. }
  645. int aws_event_stream_add_timestamp_header(
  646. struct aws_array_list *headers,
  647. const char *name,
  648. uint8_t name_len,
  649. int64_t value) {
  650. struct aws_event_stream_header_value_pair header = {
  651. .header_name_len = name_len,
  652. .header_value_len = sizeof(uint64_t),
  653. .value_owned = 0,
  654. .header_value_type = AWS_EVENT_STREAM_HEADER_TIMESTAMP,
  655. };
  656. memcpy((void *)header.header_name, (void *)name, (size_t)name_len);
  657. aws_write_u64((uint64_t)value, header.header_value.static_val);
  658. return aws_array_list_push_back(headers, (void *)&header);
  659. }
  660. int aws_event_stream_add_uuid_header(
  661. struct aws_array_list *headers,
  662. const char *name,
  663. uint8_t name_len,
  664. const uint8_t *value) {
  665. struct aws_event_stream_header_value_pair header = {
  666. .header_name_len = name_len,
  667. .header_value_len = 16,
  668. .value_owned = 0,
  669. .header_value_type = AWS_EVENT_STREAM_HEADER_UUID,
  670. };
  671. memcpy((void *)header.header_name, (void *)name, (size_t)name_len);
  672. memcpy((void *)header.header_value.static_val, value, 16);
  673. return aws_array_list_push_back(headers, (void *)&header);
  674. }
  675. struct aws_byte_buf aws_event_stream_header_name(struct aws_event_stream_header_value_pair *header) {
  676. return aws_byte_buf_from_array((uint8_t *)header->header_name, header->header_name_len);
  677. }
  678. int8_t aws_event_stream_header_value_as_byte(struct aws_event_stream_header_value_pair *header) {
  679. return (int8_t)header->header_value.static_val[0];
  680. }
  681. struct aws_byte_buf aws_event_stream_header_value_as_string(struct aws_event_stream_header_value_pair *header) {
  682. return aws_event_stream_header_value_as_bytebuf(header);
  683. }
  684. int8_t aws_event_stream_header_value_as_bool(struct aws_event_stream_header_value_pair *header) {
  685. return header->header_value_type == AWS_EVENT_STREAM_HEADER_BOOL_TRUE ? (int8_t)1 : (int8_t)0;
  686. }
  687. int16_t aws_event_stream_header_value_as_int16(struct aws_event_stream_header_value_pair *header) {
  688. return (int16_t)aws_read_u16(header->header_value.static_val);
  689. }
  690. int32_t aws_event_stream_header_value_as_int32(struct aws_event_stream_header_value_pair *header) {
  691. return (int32_t)aws_read_u32(header->header_value.static_val);
  692. }
  693. int64_t aws_event_stream_header_value_as_int64(struct aws_event_stream_header_value_pair *header) {
  694. return (int64_t)aws_read_u64(header->header_value.static_val);
  695. }
  696. struct aws_byte_buf aws_event_stream_header_value_as_bytebuf(struct aws_event_stream_header_value_pair *header) {
  697. return aws_byte_buf_from_array(header->header_value.variable_len_val, header->header_value_len);
  698. }
  699. int64_t aws_event_stream_header_value_as_timestamp(struct aws_event_stream_header_value_pair *header) {
  700. return aws_event_stream_header_value_as_int64(header);
  701. }
  702. struct aws_byte_buf aws_event_stream_header_value_as_uuid(struct aws_event_stream_header_value_pair *header) {
  703. return aws_byte_buf_from_array(header->header_value.static_val, 16);
  704. }
  705. uint16_t aws_event_stream_header_value_length(struct aws_event_stream_header_value_pair *header) {
  706. return header->header_value_len;
  707. }
  708. static struct aws_event_stream_message_prelude s_empty_prelude = {.total_len = 0, .headers_len = 0, .prelude_crc = 0};
  709. static void s_reset_header_state(struct aws_event_stream_streaming_decoder *decoder, uint8_t free_header_data) {
  710. if (free_header_data && decoder->current_header.value_owned) {
  711. aws_mem_release(decoder->alloc, (void *)decoder->current_header.header_value.variable_len_val);
  712. }
  713. memset((void *)&decoder->current_header, 0, sizeof(struct aws_event_stream_header_value_pair));
  714. }
  715. static void s_reset_state(struct aws_event_stream_streaming_decoder *decoder);
  716. static int s_headers_state(
  717. struct aws_event_stream_streaming_decoder *decoder,
  718. const uint8_t *data,
  719. size_t len,
  720. size_t *processed);
  721. static int s_read_header_value(
  722. struct aws_event_stream_streaming_decoder *decoder,
  723. const uint8_t *data,
  724. size_t len,
  725. size_t *processed) {
  726. size_t current_pos = decoder->message_pos;
  727. size_t length_read = current_pos - decoder->current_header_value_offset;
  728. struct aws_event_stream_header_value_pair *current_header = &decoder->current_header;
  729. if (!length_read) {
  730. /* save an allocation, this can only happen if the data we were handed is larger than the length of the header
  731. * value. we don't really need to handle offsets in this case. This expects the user is living by the contract
  732. * that they cannot act like they own this memory beyond the lifetime of their callback, and they should not
  733. * mutate it */
  734. if (len >= current_header->header_value_len) {
  735. /* this part works regardless of type since the layout of the union will line up. */
  736. current_header->header_value.variable_len_val = (uint8_t *)data;
  737. current_header->value_owned = 0;
  738. decoder->on_header(decoder, &decoder->prelude, &decoder->current_header, decoder->user_context);
  739. *processed += current_header->header_value_len;
  740. decoder->message_pos += current_header->header_value_len;
  741. decoder->running_crc =
  742. aws_checksums_crc32(data, (int)current_header->header_value_len, decoder->running_crc);
  743. s_reset_header_state(decoder, 1);
  744. decoder->state = s_headers_state;
  745. return AWS_OP_SUCCESS;
  746. }
  747. /* a possible optimization later would be to only allocate this once, and then keep reusing the same buffer. for
  748. * subsequent messages.*/
  749. if (current_header->header_value_type == AWS_EVENT_STREAM_HEADER_BYTE_BUF ||
  750. current_header->header_value_type == AWS_EVENT_STREAM_HEADER_STRING) {
  751. current_header->header_value.variable_len_val =
  752. aws_mem_acquire(decoder->alloc, decoder->current_header.header_value_len);
  753. if (!current_header->header_value.variable_len_val) {
  754. return aws_raise_error(AWS_ERROR_OOM);
  755. }
  756. current_header->value_owned = 1;
  757. }
  758. }
  759. size_t max_read =
  760. len >= current_header->header_value_len - length_read ? current_header->header_value_len - length_read : len;
  761. const uint8_t *header_value_alias = current_header->header_value_type == AWS_EVENT_STREAM_HEADER_BYTE_BUF ||
  762. current_header->header_value_type == AWS_EVENT_STREAM_HEADER_STRING
  763. ? current_header->header_value.variable_len_val
  764. : current_header->header_value.static_val;
  765. memcpy((void *)(header_value_alias + length_read), data, max_read);
  766. decoder->running_crc = aws_checksums_crc32(data, (int)max_read, decoder->running_crc);
  767. *processed += max_read;
  768. decoder->message_pos += max_read;
  769. length_read += max_read;
  770. if (length_read == current_header->header_value_len) {
  771. decoder->on_header(decoder, &decoder->prelude, current_header, decoder->user_context);
  772. s_reset_header_state(decoder, 1);
  773. decoder->state = s_headers_state;
  774. }
  775. return AWS_OP_SUCCESS;
  776. }
  777. static int s_read_header_value_len(
  778. struct aws_event_stream_streaming_decoder *decoder,
  779. const uint8_t *data,
  780. size_t len,
  781. size_t *processed) {
  782. size_t current_pos = decoder->message_pos;
  783. size_t length_portion_read = current_pos - decoder->current_header_value_offset;
  784. if (length_portion_read < sizeof(uint16_t)) {
  785. size_t max_to_read =
  786. len > sizeof(uint16_t) - length_portion_read ? sizeof(uint16_t) - length_portion_read : len;
  787. memcpy(decoder->working_buffer + length_portion_read, data, max_to_read);
  788. decoder->running_crc = aws_checksums_crc32(data, (int)max_to_read, decoder->running_crc);
  789. *processed += max_to_read;
  790. decoder->message_pos += max_to_read;
  791. length_portion_read = decoder->message_pos - decoder->current_header_value_offset;
  792. }
  793. if (length_portion_read == sizeof(uint16_t)) {
  794. decoder->current_header.header_value_len = aws_read_u16(decoder->working_buffer);
  795. decoder->current_header_value_offset = decoder->message_pos;
  796. decoder->state = s_read_header_value;
  797. }
  798. return AWS_OP_SUCCESS;
  799. }
  800. static int s_read_header_type(
  801. struct aws_event_stream_streaming_decoder *decoder,
  802. const uint8_t *data,
  803. size_t len,
  804. size_t *processed) {
  805. (void)len;
  806. uint8_t type = *data;
  807. decoder->running_crc = aws_checksums_crc32(data, 1, decoder->running_crc);
  808. *processed += 1;
  809. decoder->message_pos++;
  810. decoder->current_header_value_offset++;
  811. struct aws_event_stream_header_value_pair *current_header = &decoder->current_header;
  812. if (type >= AWS_EVENT_STREAM_HEADER_BOOL_FALSE && type <= AWS_EVENT_STREAM_HEADER_UUID) {
  813. current_header->header_value_type = (enum aws_event_stream_header_value_type)type;
  814. switch (type) {
  815. case AWS_EVENT_STREAM_HEADER_STRING:
  816. case AWS_EVENT_STREAM_HEADER_BYTE_BUF:
  817. decoder->state = s_read_header_value_len;
  818. break;
  819. case AWS_EVENT_STREAM_HEADER_BOOL_FALSE:
  820. current_header->header_value_len = 0;
  821. current_header->header_value.static_val[0] = 0;
  822. decoder->on_header(decoder, &decoder->prelude, current_header, decoder->user_context);
  823. s_reset_header_state(decoder, 1);
  824. break;
  825. case AWS_EVENT_STREAM_HEADER_BOOL_TRUE:
  826. current_header->header_value_len = 0;
  827. current_header->header_value.static_val[0] = 1;
  828. decoder->on_header(decoder, &decoder->prelude, current_header, decoder->user_context);
  829. s_reset_header_state(decoder, 1);
  830. break;
  831. case AWS_EVENT_STREAM_HEADER_BYTE:
  832. current_header->header_value_len = 1;
  833. decoder->state = s_read_header_value;
  834. break;
  835. case AWS_EVENT_STREAM_HEADER_INT16:
  836. current_header->header_value_len = sizeof(uint16_t);
  837. decoder->state = s_read_header_value;
  838. break;
  839. case AWS_EVENT_STREAM_HEADER_INT32:
  840. current_header->header_value_len = sizeof(uint32_t);
  841. decoder->state = s_read_header_value;
  842. break;
  843. case AWS_EVENT_STREAM_HEADER_INT64:
  844. case AWS_EVENT_STREAM_HEADER_TIMESTAMP:
  845. current_header->header_value_len = sizeof(uint64_t);
  846. decoder->state = s_read_header_value;
  847. break;
  848. case AWS_EVENT_STREAM_HEADER_UUID:
  849. current_header->header_value_len = 16;
  850. decoder->state = s_read_header_value;
  851. break;
  852. default:
  853. return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_UNKNOWN_HEADER_TYPE);
  854. }
  855. return AWS_OP_SUCCESS;
  856. }
  857. return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_UNKNOWN_HEADER_TYPE);
  858. }
  859. static int s_read_header_name(
  860. struct aws_event_stream_streaming_decoder *decoder,
  861. const uint8_t *data,
  862. size_t len,
  863. size_t *processed) {
  864. size_t current_pos = decoder->message_pos;
  865. size_t length_read = current_pos - decoder->current_header_name_offset;
  866. size_t max_read = len >= decoder->current_header.header_name_len - length_read
  867. ? decoder->current_header.header_name_len - length_read
  868. : len;
  869. memcpy((void *)(decoder->current_header.header_name + length_read), data, max_read);
  870. decoder->running_crc = aws_checksums_crc32(data, (int)max_read, decoder->running_crc);
  871. *processed += max_read;
  872. decoder->message_pos += max_read;
  873. length_read += max_read;
  874. if (length_read == decoder->current_header.header_name_len) {
  875. decoder->state = s_read_header_type;
  876. decoder->current_header_value_offset = decoder->message_pos;
  877. }
  878. return AWS_OP_SUCCESS;
  879. }
  880. static int s_read_header_name_len(
  881. struct aws_event_stream_streaming_decoder *decoder,
  882. const uint8_t *data,
  883. size_t len,
  884. size_t *processed) {
  885. (void)len;
  886. decoder->current_header.header_name_len = *data;
  887. decoder->message_pos++;
  888. decoder->current_header_name_offset++;
  889. *processed += 1;
  890. decoder->state = s_read_header_name;
  891. decoder->running_crc = aws_checksums_crc32(data, 1, decoder->running_crc);
  892. return AWS_OP_SUCCESS;
  893. }
  894. static int s_start_header(
  895. struct aws_event_stream_streaming_decoder *decoder,
  896. const uint8_t *data,
  897. size_t len,
  898. size_t *processed) /* NOLINT */ {
  899. (void)data;
  900. (void)len;
  901. (void)processed;
  902. decoder->state = s_read_header_name_len;
  903. decoder->current_header_name_offset = decoder->message_pos;
  904. return AWS_OP_SUCCESS;
  905. }
  906. static int s_payload_state(
  907. struct aws_event_stream_streaming_decoder *decoder,
  908. const uint8_t *data,
  909. size_t len,
  910. size_t *processed);
  911. /*Handles the initial state for header parsing.
  912. will oscillate between multiple other states as well.
  913. after all headers have been handled, payload will be set as the next state. */
  914. static int s_headers_state(
  915. struct aws_event_stream_streaming_decoder *decoder,
  916. const uint8_t *data,
  917. size_t len,
  918. size_t *processed) /* NOLINT */ {
  919. (void)data;
  920. (void)len;
  921. (void)processed;
  922. size_t current_pos = decoder->message_pos;
  923. size_t headers_boundary = decoder->prelude.headers_len + AWS_EVENT_STREAM_PRELUDE_LENGTH;
  924. if (current_pos < headers_boundary) {
  925. decoder->state = s_start_header;
  926. return AWS_OP_SUCCESS;
  927. }
  928. if (current_pos == headers_boundary) {
  929. decoder->state = s_payload_state;
  930. return AWS_OP_SUCCESS;
  931. }
  932. return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_PARSER_ILLEGAL_STATE);
  933. }
  934. /* handles reading the trailer. Once it has been read, it will be compared to the running checksum. If successful,
  935. * the state will be reset. */
  936. static int s_read_trailer_state(
  937. struct aws_event_stream_streaming_decoder *decoder,
  938. const uint8_t *data,
  939. size_t len,
  940. size_t *processed) {
  941. size_t remaining_amount = decoder->prelude.total_len - decoder->message_pos;
  942. size_t segment_length = len > remaining_amount ? remaining_amount : len;
  943. size_t offset = sizeof(uint32_t) - remaining_amount;
  944. memcpy(decoder->working_buffer + offset, data, segment_length);
  945. decoder->message_pos += segment_length;
  946. *processed += segment_length;
  947. if (decoder->message_pos == decoder->prelude.total_len) {
  948. uint32_t message_crc = aws_read_u32(decoder->working_buffer);
  949. if (message_crc == decoder->running_crc) {
  950. s_reset_state(decoder);
  951. } else {
  952. char error_message[70];
  953. snprintf(
  954. error_message,
  955. sizeof(error_message),
  956. "CRC Mismatch. message_crc was 0x08%" PRIX32 ", but computed 0x08%" PRIX32,
  957. message_crc,
  958. decoder->running_crc);
  959. aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_CHECKSUM_FAILURE);
  960. decoder->on_error(
  961. decoder,
  962. &decoder->prelude,
  963. AWS_ERROR_EVENT_STREAM_MESSAGE_CHECKSUM_FAILURE,
  964. error_message,
  965. decoder->user_context);
  966. return AWS_OP_ERR;
  967. }
  968. }
  969. return AWS_OP_SUCCESS;
  970. }
  971. /* handles the reading of the payload up to the final checksum. Sets read_trailer_state as the new state once
  972. * the payload has been processed. */
  973. static int s_payload_state(
  974. struct aws_event_stream_streaming_decoder *decoder,
  975. const uint8_t *data,
  976. size_t len,
  977. size_t *processed) {
  978. if (decoder->message_pos < decoder->prelude.total_len - AWS_EVENT_STREAM_TRAILER_LENGTH) {
  979. size_t remaining_amount = decoder->prelude.total_len - decoder->message_pos - AWS_EVENT_STREAM_TRAILER_LENGTH;
  980. size_t segment_length = len > remaining_amount ? remaining_amount : len;
  981. int8_t final_segment =
  982. (segment_length + decoder->message_pos) == (decoder->prelude.total_len - AWS_EVENT_STREAM_TRAILER_LENGTH);
  983. struct aws_byte_buf payload_buf = aws_byte_buf_from_array(data, segment_length);
  984. decoder->on_payload(decoder, &payload_buf, final_segment, decoder->user_context);
  985. decoder->message_pos += segment_length;
  986. decoder->running_crc = aws_checksums_crc32(data, (int)segment_length, decoder->running_crc);
  987. *processed += segment_length;
  988. }
  989. if (decoder->message_pos == decoder->prelude.total_len - AWS_EVENT_STREAM_TRAILER_LENGTH) {
  990. decoder->state = s_read_trailer_state;
  991. }
  992. return AWS_OP_SUCCESS;
  993. }
  994. /* Parses the payload and verifies checksums. Sets the next state if successful. */
  995. static int s_verify_prelude_state(
  996. struct aws_event_stream_streaming_decoder *decoder,
  997. const uint8_t *data,
  998. size_t len,
  999. size_t *processed) /* NOLINT */ {
  1000. (void)data;
  1001. (void)len;
  1002. (void)processed;
  1003. decoder->prelude.headers_len = aws_read_u32(decoder->working_buffer + HEADER_LEN_OFFSET);
  1004. decoder->prelude.prelude_crc = aws_read_u32(decoder->working_buffer + PRELUDE_CRC_OFFSET);
  1005. decoder->prelude.total_len = aws_read_u32(decoder->working_buffer + TOTAL_LEN_OFFSET);
  1006. decoder->running_crc = aws_checksums_crc32(decoder->working_buffer, PRELUDE_CRC_OFFSET, 0);
  1007. if (AWS_LIKELY(decoder->running_crc == decoder->prelude.prelude_crc)) {
  1008. if (AWS_UNLIKELY(
  1009. decoder->prelude.headers_len > AWS_EVENT_STREAM_MAX_HEADERS_SIZE ||
  1010. decoder->prelude.total_len > AWS_EVENT_STREAM_MAX_MESSAGE_SIZE)) {
  1011. aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED);
  1012. char error_message[] = "Maximum message field size exceeded";
  1013. decoder->on_error(
  1014. decoder,
  1015. &decoder->prelude,
  1016. AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED,
  1017. error_message,
  1018. decoder->user_context);
  1019. return AWS_OP_ERR;
  1020. }
  1021. /* Should only call on_prelude() after passing crc check and limitation check, otherwise call on_prelude() with
  1022. * incorrect prelude is error prune. */
  1023. decoder->on_prelude(decoder, &decoder->prelude, decoder->user_context);
  1024. decoder->running_crc = aws_checksums_crc32(
  1025. decoder->working_buffer + PRELUDE_CRC_OFFSET,
  1026. (int)sizeof(decoder->prelude.prelude_crc),
  1027. decoder->running_crc);
  1028. memset(decoder->working_buffer, 0, sizeof(decoder->working_buffer));
  1029. decoder->state = decoder->prelude.headers_len > 0 ? s_headers_state : s_payload_state;
  1030. } else {
  1031. char error_message[70];
  1032. snprintf(
  1033. error_message,
  1034. sizeof(error_message),
  1035. "CRC Mismatch. prelude_crc was 0x08%" PRIX32 ", but computed 0x08%" PRIX32,
  1036. decoder->prelude.prelude_crc,
  1037. decoder->running_crc);
  1038. aws_raise_error(AWS_ERROR_EVENT_STREAM_PRELUDE_CHECKSUM_FAILURE);
  1039. decoder->on_error(
  1040. decoder,
  1041. &decoder->prelude,
  1042. AWS_ERROR_EVENT_STREAM_PRELUDE_CHECKSUM_FAILURE,
  1043. error_message,
  1044. decoder->user_context);
  1045. return AWS_OP_ERR;
  1046. }
  1047. return AWS_OP_SUCCESS;
  1048. }
  1049. /* initial state handles up to the reading of the prelude */
  1050. static int s_start_state(
  1051. struct aws_event_stream_streaming_decoder *decoder,
  1052. const uint8_t *data,
  1053. size_t len,
  1054. size_t *processed) {
  1055. size_t previous_position = decoder->message_pos;
  1056. if (decoder->message_pos < AWS_EVENT_STREAM_PRELUDE_LENGTH) {
  1057. if (len >= AWS_EVENT_STREAM_PRELUDE_LENGTH - decoder->message_pos) {
  1058. memcpy(
  1059. decoder->working_buffer + decoder->message_pos,
  1060. data,
  1061. AWS_EVENT_STREAM_PRELUDE_LENGTH - decoder->message_pos);
  1062. decoder->message_pos += AWS_EVENT_STREAM_PRELUDE_LENGTH - decoder->message_pos;
  1063. } else {
  1064. memcpy(decoder->working_buffer + decoder->message_pos, data, len);
  1065. decoder->message_pos += len;
  1066. }
  1067. *processed += decoder->message_pos - previous_position;
  1068. }
  1069. if (decoder->message_pos == AWS_EVENT_STREAM_PRELUDE_LENGTH) {
  1070. decoder->state = s_verify_prelude_state;
  1071. }
  1072. return AWS_OP_SUCCESS;
  1073. }
  1074. static void s_reset_state(struct aws_event_stream_streaming_decoder *decoder) {
  1075. decoder->message_pos = 0;
  1076. decoder->prelude = s_empty_prelude;
  1077. decoder->running_crc = 0;
  1078. memset(decoder->working_buffer, 0, sizeof(decoder->working_buffer));
  1079. decoder->state = s_start_state;
  1080. }
  1081. void aws_event_stream_streaming_decoder_init(
  1082. struct aws_event_stream_streaming_decoder *decoder,
  1083. struct aws_allocator *alloc,
  1084. aws_event_stream_process_on_payload_segment_fn *on_payload_segment,
  1085. aws_event_stream_prelude_received_fn *on_prelude,
  1086. aws_event_stream_header_received_fn *on_header,
  1087. aws_event_stream_on_error_fn *on_error,
  1088. void *user_data) {
  1089. s_reset_state(decoder);
  1090. decoder->alloc = alloc;
  1091. decoder->on_error = on_error;
  1092. decoder->on_header = on_header;
  1093. decoder->on_payload = on_payload_segment;
  1094. decoder->on_prelude = on_prelude;
  1095. decoder->user_context = user_data;
  1096. }
  1097. void aws_event_stream_streaming_decoder_clean_up(struct aws_event_stream_streaming_decoder *decoder) {
  1098. s_reset_state(decoder);
  1099. decoder->on_error = 0;
  1100. decoder->on_header = 0;
  1101. decoder->on_payload = 0;
  1102. decoder->on_prelude = 0;
  1103. decoder->user_context = 0;
  1104. }
  1105. /* Simply sends the data to the state machine until all has been processed or an error is returned. */
  1106. int aws_event_stream_streaming_decoder_pump(
  1107. struct aws_event_stream_streaming_decoder *decoder,
  1108. const struct aws_byte_buf *data) {
  1109. size_t processed = 0;
  1110. int err_val = 0;
  1111. while (!err_val && data->buffer && data->len && processed < data->len) {
  1112. err_val = decoder->state(decoder, data->buffer + processed, data->len - processed, &processed);
  1113. }
  1114. return err_val;
  1115. }
  1116. #if _MSC_VER
  1117. # pragma warning(pop)
  1118. #endif