Debian Codegraph @

crt/aws-c-event-stream/include/aws/event-stream/event_stream.h

raw symbols SCIP
1 #ifndef AWS_EVENT_STREAM_H_
2 #define AWS_EVENT_STREAM_H_
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/common/array_list.h>
10 #include <aws/common/byte_buf.h>
11 #include <aws/common/logging.h>
12 #include <aws/event-stream/event_stream_exports.h>
13
14 #include <stdio.h>
15
16 AWS_PUSH_SANE_WARNING_LEVEL
17
18 #define AWS_C_EVENT_STREAM_PACKAGE_ID 4
19 /* Current service-side max is 24 MB, likely to increase in future.
20 * This is encoded on the wire in 4 bytes, and could technically be larger (up to INT32_MAX). */
21 #define AWS_EVENT_STREAM_MAX_MESSAGE_SIZE (24 * 1024 * 1024)
22
23 /* Max total size for headers.
24 * This is encoded on the wire in 4 bytes, and could technically be larger (up to INT32_MAX). */
25 #define AWS_EVENT_STREAM_MAX_HEADERS_SIZE (AWS_EVENT_STREAM_MAX_MESSAGE_SIZE)
26
27 /* Max header name length is 255 bytes.
28 * This is encoded on the wire in 1 byte as unsigned int, it cannot change. */
29 #define AWS_EVENT_STREAM_HEADER_NAME_LEN_MAX (UINT8_MAX)
30
31 /* Max header value length is 32767 bytes.
32 * This is encoded on the wire in 2 bytes as unsigned int, but we followed Java implementation to limit to 32767.
33 * https://github.com/awslabs/aws-eventstream-java/blob/1e76ef478f0108b38e2d7b70b598b4e5f0def3d1/src/main/java/software/amazon/eventstream/Utils.java#L34-L40*/
34 #define AWS_EVENT_STREAM_HEADER_VALUE_LEN_MAX (INT16_MAX)
35
36 /*
37 * Not an actual part of the eventstream spec. Similar to HTTP where there is no spec-defined header maximum but
38 * implementations apply limits to bound memory usage.
39 */
40 #define AWS_EVENT_STREAM_MESSAGE_MAX_HEADERS 1024
41
42 enum aws_event_stream_errors {
43 AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH = AWS_ERROR_ENUM_BEGIN_RANGE(AWS_C_EVENT_STREAM_PACKAGE_ID),
44 AWS_ERROR_EVENT_STREAM_INSUFFICIENT_BUFFER_LEN,
45 AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED,
46 AWS_ERROR_EVENT_STREAM_PRELUDE_CHECKSUM_FAILURE,
47 AWS_ERROR_EVENT_STREAM_MESSAGE_CHECKSUM_FAILURE,
48 AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN,
49 AWS_ERROR_EVENT_STREAM_MESSAGE_UNKNOWN_HEADER_TYPE,
50 AWS_ERROR_EVENT_STREAM_MESSAGE_PARSER_ILLEGAL_STATE,
51 AWS_ERROR_EVENT_STREAM_RPC_CONNECTION_CLOSED,
52 AWS_ERROR_EVENT_STREAM_RPC_PROTOCOL_ERROR,
53 AWS_ERROR_EVENT_STREAM_RPC_STREAM_CLOSED,
54 AWS_ERROR_EVENT_STREAM_RPC_STREAM_NOT_ACTIVATED,
55 AWS_ERROR_EVENT_STREAM_MESSAGE_TOO_MANY_HEADERS,
56
57 AWS_ERROR_EVENT_STREAM_END_RANGE = AWS_ERROR_ENUM_END_RANGE(AWS_C_EVENT_STREAM_PACKAGE_ID),
58 };
59
60 enum aws_event_stream_log_subject {
61 AWS_LS_EVENT_STREAM_GENERAL = AWS_LOG_SUBJECT_BEGIN_RANGE(AWS_C_EVENT_STREAM_PACKAGE_ID),
62 AWS_LS_EVENT_STREAM_CHANNEL_HANDLER,
63 AWS_LS_EVENT_STREAM_RPC_SERVER,
64 AWS_LS_EVENT_STREAM_RPC_CLIENT,
65
66 AWS_LS_EVENT_STREAM_LAST = AWS_LOG_SUBJECT_END_RANGE(AWS_C_EVENT_STREAM_PACKAGE_ID),
67 };
68
69 struct aws_event_stream_message_prelude {
70 uint32_t total_len;
71 uint32_t headers_len;
72 uint32_t prelude_crc;
73 };
74
75 struct aws_event_stream_message {
76 struct aws_allocator *alloc;
77 struct aws_byte_buf message_buffer;
78 };
79
80 /* Prelude (12-bytes) = Total Byte Length (4-bytes) + Headers Byte Length (4-bytes) + Prelude CRC (4-bytes) */
81 #define AWS_EVENT_STREAM_PRELUDE_LENGTH (uint32_t)(12)
82
83 /* Trailer (4-bytes) = Message CRC (4-bytes) */
84 #define AWS_EVENT_STREAM_TRAILER_LENGTH (uint32_t)(4)
85
86 enum aws_event_stream_header_value_type {
87 AWS_EVENT_STREAM_HEADER_BOOL_TRUE = 0,
88 AWS_EVENT_STREAM_HEADER_BOOL_FALSE,
89 AWS_EVENT_STREAM_HEADER_BYTE,
90 AWS_EVENT_STREAM_HEADER_INT16,
91 AWS_EVENT_STREAM_HEADER_INT32,
92 AWS_EVENT_STREAM_HEADER_INT64,
93 AWS_EVENT_STREAM_HEADER_BYTE_BUF,
94 AWS_EVENT_STREAM_HEADER_STRING,
95 /* 64 bit integer (millis since epoch) */
96 AWS_EVENT_STREAM_HEADER_TIMESTAMP,
97 AWS_EVENT_STREAM_HEADER_UUID
98 };
99
100 struct aws_event_stream_header_value_pair {
101 uint8_t header_name_len;
102 char header_name[AWS_EVENT_STREAM_HEADER_NAME_LEN_MAX];
103 enum aws_event_stream_header_value_type header_value_type;
104 union {
105 uint8_t *variable_len_val; /* for variable-size types (STRING, BYTE_BUF)*/
106 uint8_t static_val[16]; /* largest fixed-size types are 16 bytes (INT64, UUID, TIMESTAMP) */
107 } header_value;
108
109 uint16_t header_value_len;
110 int8_t value_owned;
111 };
112
113 struct aws_event_stream_streaming_decoder;
114 typedef int(aws_event_stream_process_state_fn)(
115 struct aws_event_stream_streaming_decoder *decoder,
116 const uint8_t *data,
117 size_t len,
118 size_t *processed);
119
120 /**
121 * Called by aws_aws_event_stream_streaming_decoder when payload data has been received.
122 * 'data' doesn't belong to you, so copy the data if it is needed beyond the scope of your callback.
123 * final_segment == 1 indicates the current data is the last payload buffer for that message.
124 */
125 typedef void(aws_event_stream_process_on_payload_segment_fn)(
126 struct aws_event_stream_streaming_decoder *decoder,
127 struct aws_byte_buf *payload,
128 int8_t final_segment,
129 void *user_data);
130
131 /**
132 * Called by aws_aws_event_stream_streaming_decoder when a new message has arrived. The prelude will contain metadata
133 * about the message. At this point no headers or payload have been received. prelude is copyable.
134 */
135 typedef void(aws_event_stream_prelude_received_fn)(
136 struct aws_event_stream_streaming_decoder *decoder,
137 struct aws_event_stream_message_prelude *prelude,
138 void *user_data);
139
140 /**
141 * Called by aws_aws_event_stream_streaming_decoder when a header is encountered. 'header' is not yours. Copy the data
142 * you want from it if your scope extends beyond your callback.
143 */
144 typedef void(aws_event_stream_header_received_fn)(
145 struct aws_event_stream_streaming_decoder *decoder,
146 struct aws_event_stream_message_prelude *prelude,
147 struct aws_event_stream_header_value_pair *header,
148 void *user_data);
149
150 /**
151 * Called by aws_aws_event_stream_streaming_decoder when a message decoding is complete
152 * and crc is verified.
153 */
154 typedef void(aws_event_stream_on_complete_fn)(
155 struct aws_event_stream_streaming_decoder *decoder,
156 uint32_t message_crc,
157 void *user_data);
158
159 /**
160 * Called by aws_aws_event_stream_streaming_decoder when an error is encountered. The decoder is not in a good state for
161 * usage after this callback.
162 */
163 typedef void(aws_event_stream_on_error_fn)(
164 struct aws_event_stream_streaming_decoder *decoder,
165 struct aws_event_stream_message_prelude *prelude,
166 int error_code,
167 const char *message,
168 void *user_data);
169
170 struct aws_event_stream_streaming_decoder {
171 struct aws_allocator *alloc;
172 uint8_t working_buffer[AWS_EVENT_STREAM_PRELUDE_LENGTH];
173 size_t message_pos;
174 uint32_t running_crc;
175 size_t current_header_name_offset;
176 size_t current_header_value_offset;
177 struct aws_event_stream_header_value_pair current_header;
178 struct aws_event_stream_message_prelude prelude;
179 aws_event_stream_process_state_fn *state;
180 aws_event_stream_process_on_payload_segment_fn *on_payload;
181 aws_event_stream_prelude_received_fn *on_prelude;
182 aws_event_stream_header_received_fn *on_header;
183 aws_event_stream_on_complete_fn *on_complete;
184 aws_event_stream_on_error_fn *on_error;
185 void *user_context;
186 };
187
188 struct aws_event_stream_streaming_decoder_options {
189 /**
190 * (Required)
191 * Invoked repeatedly as payload segment are received.
192 * See `aws_event_stream_process_on_payload_segment_fn`.
193 */
194 aws_event_stream_process_on_payload_segment_fn *on_payload_segment;
195 /**
196 * (Required)
197 * Invoked when when a new message has arrived. The prelude will contain metadata about the message.
198 * See `aws_event_stream_prelude_received_fn`.
199 */
200 aws_event_stream_prelude_received_fn *on_prelude;
201 /**
202 * (Required)
203 * Invoked repeatedly as headers are received.
204 * See `aws_event_stream_header_received_fn`.
205 */
206 aws_event_stream_header_received_fn *on_header;
207 /**
208 * (Optional)
209 * Invoked if a message is decoded successfully.
210 * See `aws_event_stream_on_complete_fn`.
211 */
212 aws_event_stream_on_complete_fn *on_complete;
213 /**
214 * (Required)
215 * Invoked when an error is encountered. The decoder is not in a good state for usage after this callback.
216 * See `aws_event_stream_on_error_fn`.
217 */
218 aws_event_stream_on_error_fn *on_error;
219 /**
220 * (Optional)
221 * user_data passed to callbacks.
222 */
223 void *user_data;
224 };
225 AWS_EXTERN_C_BEGIN
226
227 /**
228 * Initializes with a list of headers, the payload, and a payload length. CRCs will be computed for you.
229 * If headers or payload is NULL, then the fields will be appropriately set to indicate no headers and/or no payload.
230 * Both payload and headers will result in an allocation.
231 */
232 AWS_EVENT_STREAM_API int aws_event_stream_message_init(
233 struct aws_event_stream_message *message,
234 struct aws_allocator *alloc,
235 const struct aws_array_list *headers,
236 const struct aws_byte_buf *payload);
237
238 /**
239 * Zero allocation, Zero copy. The message will simply wrap the buffer. The message functions are only useful as long as
240 * buffer is referencable memory.
241 */
242 AWS_EVENT_STREAM_API int aws_event_stream_message_from_buffer(
243 struct aws_event_stream_message *message,
244 struct aws_allocator *alloc,
245 struct aws_byte_buf *buffer);
246
247 /**
248 * Allocates memory and copies buffer. Otherwise the same as aws_aws_event_stream_message_from_buffer. This is slower,
249 * but possibly safer.
250 */
251 AWS_EVENT_STREAM_API int aws_event_stream_message_from_buffer_copy(
252 struct aws_event_stream_message *message,
253 struct aws_allocator *alloc,
254 const struct aws_byte_buf *buffer);
255
256 /**
257 * Cleans up any internally allocated memory. Always call this for API compatibility reasons, even if you only used the
258 * aws_aws_event_stream_message_from_buffer function.
259 */
260 AWS_EVENT_STREAM_API void aws_event_stream_message_clean_up(struct aws_event_stream_message *message);
261
262 /**
263 * Returns the total length of the message (including the length field).
264 */
265 AWS_EVENT_STREAM_API uint32_t aws_event_stream_message_total_length(const struct aws_event_stream_message *message);
266
267 /**
268 * Returns the length of the headers portion of the message.
269 */
270 AWS_EVENT_STREAM_API uint32_t aws_event_stream_message_headers_len(const struct aws_event_stream_message *message);
271
272 /**
273 * Returns the prelude crc (crc32)
274 */
275 AWS_EVENT_STREAM_API uint32_t aws_event_stream_message_prelude_crc(const struct aws_event_stream_message *message);
276
277 /**
278 * Writes the message to fd in json format. All strings and binary fields are base64 encoded.
279 */
280 AWS_EVENT_STREAM_API int aws_event_stream_message_to_debug_str(
281 FILE *fd,
282 const struct aws_event_stream_message *message);
283
284 /**
285 * Adds the headers for the message to list. The memory in each header is owned as part of the message, do not free it
286 * or pass its address around.
287 */
288 AWS_EVENT_STREAM_API int aws_event_stream_message_headers(
289 const struct aws_event_stream_message *message,
290 struct aws_array_list *headers);
291
292 /**
293 * Returns a pointer to the beginning of the message payload.
294 */
295 AWS_EVENT_STREAM_API const uint8_t *aws_event_stream_message_payload(const struct aws_event_stream_message *message);
296
297 /**
298 * Returns the length of the message payload.
299 */
300 AWS_EVENT_STREAM_API uint32_t aws_event_stream_message_payload_len(const struct aws_event_stream_message *message);
301
302 /**
303 * Returns the checksum of the entire message (crc32)
304 */
305 AWS_EVENT_STREAM_API uint32_t aws_event_stream_message_message_crc(const struct aws_event_stream_message *message);
306
307 /**
308 * Returns the message as a buffer ready for transport.
309 */
310 AWS_EVENT_STREAM_API const uint8_t *aws_event_stream_message_buffer(const struct aws_event_stream_message *message);
311
312 AWS_EVENT_STREAM_API uint32_t
313 aws_event_stream_compute_headers_required_buffer_len(const struct aws_array_list *headers);
314
315 /**
316 * Writes headers to buf assuming buf is large enough to hold the data. Prefer this function over the unsafe variant
317 * 'aws_event_stream_write_headers_to_buffer'.
318 *
319 * Returns AWS_OP_SUCCESS if the headers were successfully and completely written and AWS_OP_ERR otherwise.
320 */
321 AWS_EVENT_STREAM_API int aws_event_stream_write_headers_to_buffer_safe(
322 const struct aws_array_list *headers,
323 struct aws_byte_buf *buf);
324
325 /**
326 * Deprecated in favor of 'aws_event_stream_write_headers_to_buffer_safe' as this API is unsafe.
327 *
328 * Writes headers to buffer and returns the length of bytes written to buffer. Assumes buffer is large enough to
329 * store the headers.
330 */
331 AWS_EVENT_STREAM_API size_t
332 aws_event_stream_write_headers_to_buffer(const struct aws_array_list *headers, uint8_t *buffer);
333
334 /** Get the headers from the buffer, store them in the headers list.
335 * the user's responsibility to cleanup the list when they are finished with it.
336 * no buffer copies happen here, the lifetime of the buffer, must outlive the usage of the headers.
337 * returns error codes defined in the public interface.
338 */
339 AWS_EVENT_STREAM_API int aws_event_stream_read_headers_from_buffer(
340 struct aws_array_list *headers,
341 const uint8_t *buffer,
342 size_t headers_len);
343
344 /**
345 * Initialize a streaming decoder for messages with callbacks for usage
346 * and an optional user context pointer.
347 */
348 AWS_EVENT_STREAM_API
349 void aws_event_stream_streaming_decoder_init_from_options(
350 struct aws_event_stream_streaming_decoder *decoder,
351 struct aws_allocator *allocator,
352 const struct aws_event_stream_streaming_decoder_options *options);
353
354 /**
355 * Deprecated. Use aws_event_stream_streaming_decoder_init_from_options instead.
356 * Initialize a streaming decoder for messages with callbacks for usage and an optional user context pointer.
357 */
358 AWS_EVENT_STREAM_API void aws_event_stream_streaming_decoder_init(
359 struct aws_event_stream_streaming_decoder *decoder,
360 struct aws_allocator *alloc,
361 aws_event_stream_process_on_payload_segment_fn *on_payload_segment,
362 aws_event_stream_prelude_received_fn *on_prelude,
363 aws_event_stream_header_received_fn *on_header,
364 aws_event_stream_on_error_fn *on_error,
365 void *user_data);
366
367 /**
368 * Currently, no memory is allocated inside aws_aws_event_stream_streaming_decoder, but for future API compatibility,
369 * you should call this when finished with it.
370 */
371 AWS_EVENT_STREAM_API void aws_event_stream_streaming_decoder_clean_up(
372 struct aws_event_stream_streaming_decoder *decoder);
373
374 /**
375 * initializes a headers list for you. It will default to a capacity of 4 in dynamic mode.
376 */
377 AWS_EVENT_STREAM_API int aws_event_stream_headers_list_init(
378 struct aws_array_list *headers,
379 struct aws_allocator *allocator);
380
381 /**
382 * Cleans up the headers list. Also deallocates any headers that were the result of a copy flag for strings or buffer.
383 */
384 AWS_EVENT_STREAM_API void aws_event_stream_headers_list_cleanup(struct aws_array_list *headers);
385
386 /**
387 * Adds a string header to the list of headers. If copy is set to true, this will result in an allocation for the header
388 * value. Otherwise, the value will be set to the memory address of 'value'.
389 */
390 AWS_EVENT_STREAM_API int aws_event_stream_add_string_header(
391 struct aws_array_list *headers,
392 const char *name,
393 uint8_t name_len,
394 const char *value,
395 uint16_t value_len,
396 int8_t copy);
397
398 AWS_EVENT_STREAM_API struct aws_event_stream_header_value_pair aws_event_stream_create_string_header(
399 struct aws_byte_cursor name,
400 struct aws_byte_cursor value);
401
402 AWS_EVENT_STREAM_API struct aws_event_stream_header_value_pair aws_event_stream_create_int32_header(
403 struct aws_byte_cursor name,
404 int32_t value);
405
406 /**
407 * Adds a byte header to the list of headers.
408 */
409 AWS_EVENT_STREAM_API int aws_event_stream_add_byte_header(
410 struct aws_array_list *headers,
411 const char *name,
412 uint8_t name_len,
413 int8_t value);
414
415 /**
416 * Adds a bool header to the list of headers.
417 */
418 AWS_EVENT_STREAM_API int aws_event_stream_add_bool_header(
419 struct aws_array_list *headers,
420 const char *name,
421 uint8_t name_len,
422 int8_t value);
423
424 /**
425 * adds a 16 bit integer to the list of headers.
426 */
427 AWS_EVENT_STREAM_API int aws_event_stream_add_int16_header(
428 struct aws_array_list *headers,
429 const char *name,
430 uint8_t name_len,
431 int16_t value);
432
433 /**
434 * adds a 32 bit integer to the list of headers.
435 */
436 AWS_EVENT_STREAM_API int aws_event_stream_add_int32_header(
437 struct aws_array_list *headers,
438 const char *name,
439 uint8_t name_len,
440 int32_t value);
441
442 /**
443 * adds a 64 bit integer to the list of headers.
444 */
445 AWS_EVENT_STREAM_API int aws_event_stream_add_int64_header(
446 struct aws_array_list *headers,
447 const char *name,
448 uint8_t name_len,
449 int64_t value);
450
451 /**
452 * Adds a byte-buffer header to the list of headers. If copy is set to true, this will result in an allocation for the
453 * header value. Otherwise, the value will be set to the memory address of 'value'.
454 */
455 AWS_EVENT_STREAM_API int aws_event_stream_add_bytebuf_header(
456 struct aws_array_list *headers,
457 const char *name,
458 uint8_t name_len,
459 uint8_t *value,
460 uint16_t value_len,
461 int8_t copy);
462
463 /**
464 * adds a 64 bit integer representing milliseconds since unix epoch to the list of headers.
465 */
466 AWS_EVENT_STREAM_API int aws_event_stream_add_timestamp_header(
467 struct aws_array_list *headers,
468 const char *name,
469 uint8_t name_len,
470 int64_t value);
471
472 /**
473 * adds a uuid buffer to the list of headers. Value must always be 16 bytes long.
474 */
475 AWS_EVENT_STREAM_API int aws_event_stream_add_uuid_header(
476 struct aws_array_list *headers,
477 const char *name,
478 uint8_t name_len,
479 const uint8_t *value);
480
481 /**
482 * Adds a generic header to the list of headers.
483 * Makes a copy of the underlaying data.
484 */
485 AWS_EVENT_STREAM_API int aws_event_stream_add_header(
486 struct aws_array_list *headers,
487 const struct aws_event_stream_header_value_pair *header);
488
489 /* Cursor-based header APIs */
490
491 /**
492 * Adds a boolean-valued header to a header list
493 *
494 * @param headers header list to add to
495 * @param name name of the header to add
496 * @param value value of the header to add
497 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
498 */
499 AWS_EVENT_STREAM_API int aws_event_stream_add_bool_header_by_cursor(
500 struct aws_array_list *headers,
501 struct aws_byte_cursor name,
502 bool value);
503
504 /**
505 * Adds a byte-valued header to a header list
506 *
507 * @param headers header list to add to
508 * @param name name of the header to add
509 * @param value value of the header to add
510 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
511 */
512 AWS_EVENT_STREAM_API int aws_event_stream_add_byte_header_by_cursor(
513 struct aws_array_list *headers,
514 struct aws_byte_cursor name,
515 int8_t value);
516
517 /**
518 * Adds a int16-valued header to a header list
519 *
520 * @param headers header list to add to
521 * @param name name of the header to add
522 * @param value value of the header to add
523 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
524 */
525 AWS_EVENT_STREAM_API int aws_event_stream_add_int16_header_by_cursor(
526 struct aws_array_list *headers,
527 struct aws_byte_cursor name,
528 int16_t value);
529
530 /**
531 * Adds a int32-valued header to a header list
532 *
533 * @param headers header list to add to
534 * @param name name of the header to add
535 * @param value value of the header to add
536 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
537 */
538 AWS_EVENT_STREAM_API int aws_event_stream_add_int32_header_by_cursor(
539 struct aws_array_list *headers,
540 struct aws_byte_cursor name,
541 int32_t value);
542
543 /**
544 * Adds a int64-valued header to a header list
545 *
546 * @param headers header list to add to
547 * @param name name of the header to add
548 * @param value value of the header to add
549 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
550 */
551 AWS_EVENT_STREAM_API int aws_event_stream_add_int64_header_by_cursor(
552 struct aws_array_list *headers,
553 struct aws_byte_cursor name,
554 int64_t value);
555
556 /**
557 * Adds a string-valued header to a header list
558 *
559 * @param headers header list to add to
560 * @param name name of the header to add
561 * @param value value of the header to add
562 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
563 */
564 AWS_EVENT_STREAM_API int aws_event_stream_add_string_header_by_cursor(
565 struct aws_array_list *headers,
566 struct aws_byte_cursor name,
567 struct aws_byte_cursor value);
568
569 /**
570 * Adds a byte_buf-valued header to a header list
571 *
572 * @param headers header list to add to
573 * @param name name of the header to add
574 * @param value value of the header to add
575 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
576 */
577 AWS_EVENT_STREAM_API int aws_event_stream_add_byte_buf_header_by_cursor(
578 struct aws_array_list *headers,
579 struct aws_byte_cursor name,
580 struct aws_byte_cursor value);
581
582 /**
583 * Adds a timestamp-valued header to a header list
584 *
585 * @param headers header list to add to
586 * @param name name of the header to add
587 * @param value value of the header to add
588 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
589 */
590 AWS_EVENT_STREAM_API int aws_event_stream_add_timestamp_header_by_cursor(
591 struct aws_array_list *headers,
592 struct aws_byte_cursor name,
593 int64_t value);
594
595 /**
596 * Adds a uuid-valued header to a header list
597 *
598 * @param headers header list to add to
599 * @param name name of the header to add
600 * @param value value of the header to add
601 * @return AWS_OP_SUCCESS on success, AWS_OP_ERR on failure
602 */
603 AWS_EVENT_STREAM_API int aws_event_stream_add_uuid_header_by_cursor(
604 struct aws_array_list *headers,
605 struct aws_byte_cursor name,
606 struct aws_byte_cursor value);
607
608 /**
609 * Returns the header name. Note: this value is not null terminated
610 */
611 AWS_EVENT_STREAM_API struct aws_byte_buf aws_event_stream_header_name(
612 struct aws_event_stream_header_value_pair *header);
613
614 /**
615 * Returns the header value as a string. Note: this value is not null terminated.
616 */
617 AWS_EVENT_STREAM_API struct aws_byte_buf aws_event_stream_header_value_as_string(
618 struct aws_event_stream_header_value_pair *header);
619
620 /**
621 * Returns the header value as a byte
622 */
623 AWS_EVENT_STREAM_API int8_t aws_event_stream_header_value_as_byte(struct aws_event_stream_header_value_pair *header);
624
625 /**
626 * Returns the header value as a boolean value.
627 */
628 AWS_EVENT_STREAM_API int8_t aws_event_stream_header_value_as_bool(struct aws_event_stream_header_value_pair *header);
629
630 /**
631 * Returns the header value as a 16 bit integer.
632 */
633 AWS_EVENT_STREAM_API int16_t aws_event_stream_header_value_as_int16(struct aws_event_stream_header_value_pair *header);
634
635 /**
636 * Returns the header value as a 32 bit integer.
637 */
638 AWS_EVENT_STREAM_API int32_t aws_event_stream_header_value_as_int32(struct aws_event_stream_header_value_pair *header);
639
640 /**
641 * Returns the header value as a 64 bit integer.
642 */
643 AWS_EVENT_STREAM_API int64_t aws_event_stream_header_value_as_int64(struct aws_event_stream_header_value_pair *header);
644
645 /**
646 * Returns the header value as a pointer to a byte buffer, call aws_event_stream_header_value_length to determine
647 * the length of the buffer.
648 */
649 AWS_EVENT_STREAM_API struct aws_byte_buf aws_event_stream_header_value_as_bytebuf(
650 struct aws_event_stream_header_value_pair *header);
651
652 /**
653 * Returns the header value as a 64 bit integer representing milliseconds since unix epoch.
654 */
655 AWS_EVENT_STREAM_API int64_t
656 aws_event_stream_header_value_as_timestamp(struct aws_event_stream_header_value_pair *header);
657
658 /**
659 * Returns the header value a byte buffer which is 16 bytes long. Represents a UUID.
660 */
661 AWS_EVENT_STREAM_API struct aws_byte_buf aws_event_stream_header_value_as_uuid(
662 struct aws_event_stream_header_value_pair *header);
663
664 /**
665 * Returns the length of the header value buffer. This is mostly intended for string and byte buffer types.
666 */
667 AWS_EVENT_STREAM_API uint16_t aws_event_stream_header_value_length(struct aws_event_stream_header_value_pair *header);
668
669 /**
670 * The main driver of the decoder. Pass data that should be decoded with its length. A likely use-case here is in
671 * response to a read event from an io-device
672 */
673 AWS_EVENT_STREAM_API int aws_event_stream_streaming_decoder_pump(
674 struct aws_event_stream_streaming_decoder *decoder,
675 const struct aws_byte_buf *data);
676
677 /**
678 * Initializes internal datastructures used by aws-c-event-stream.
679 * Must be called before using any functionality in aws-c-event-stream.
680 */
681 AWS_EVENT_STREAM_API void aws_event_stream_library_init(struct aws_allocator *allocator);
682
683 /**
684 * Clean up internal datastructures used by aws-c-event-stream.
685 * Must not be called until application is done using functionality in aws-c-event-stream.
686 */
687 AWS_EVENT_STREAM_API void aws_event_stream_library_clean_up(void);
688
689 AWS_EXTERN_C_END
690 AWS_POP_SANE_WARNING_LEVEL
691
692 #endif /* AWS_EVENT_STREAM_H_ */