| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/event-stream/event_stream.h> |
| 7 |
|
| 8 |
#include <aws/checksums/crc.h> |
| 9 |
|
| 10 |
#include <aws/common/byte_buf.h> |
| 11 |
#include <aws/common/encoding.h> |
| 12 |
#include <aws/io/io.h> |
| 13 |
|
| 14 |
#include <inttypes.h> |
| 15 |
|
| 16 |
#define LIB_NAME "libaws-c-event-stream" |
| 17 |
|
| 18 |
#ifdef _MSC_VER |
| 19 |
# pragma warning(push) |
| 20 |
# pragma warning(disable : 4221) |
| 21 |
# pragma warning(disable : 4204) |
| 22 |
# pragma warning(disable : 4306) |
| 23 |
#endif |
| 24 |
|
| 25 |
static struct aws_error_info s_errors[] = { |
| 26 |
AWS_DEFINE_ERROR_INFO(AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH, "Buffer length mismatch", LIB_NAME), |
| 27 |
AWS_DEFINE_ERROR_INFO(AWS_ERROR_EVENT_STREAM_INSUFFICIENT_BUFFER_LEN, "insufficient buffer length", LIB_NAME), |
| 28 |
AWS_DEFINE_ERROR_INFO( |
| 29 |
AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED, |
| 30 |
"a field for the message was too large", |
| 31 |
LIB_NAME), |
| 32 |
AWS_DEFINE_ERROR_INFO(AWS_ERROR_EVENT_STREAM_PRELUDE_CHECKSUM_FAILURE, "prelude checksum was incorrect", LIB_NAME), |
| 33 |
AWS_DEFINE_ERROR_INFO(AWS_ERROR_EVENT_STREAM_MESSAGE_CHECKSUM_FAILURE, "message checksum was incorrect", LIB_NAME), |
| 34 |
AWS_DEFINE_ERROR_INFO( |
| 35 |
AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN, |
| 36 |
"message headers length was incorrect", |
| 37 |
LIB_NAME), |
| 38 |
AWS_DEFINE_ERROR_INFO( |
| 39 |
AWS_ERROR_EVENT_STREAM_MESSAGE_UNKNOWN_HEADER_TYPE, |
| 40 |
"An unknown header type was encountered", |
| 41 |
LIB_NAME), |
| 42 |
AWS_DEFINE_ERROR_INFO( |
| 43 |
AWS_ERROR_EVENT_STREAM_MESSAGE_PARSER_ILLEGAL_STATE, |
| 44 |
"message parser encountered an illegal state", |
| 45 |
LIB_NAME), |
| 46 |
AWS_DEFINE_ERROR_INFO( |
| 47 |
AWS_ERROR_EVENT_STREAM_RPC_CONNECTION_CLOSED, |
| 48 |
"event stream rpc connection has been closed", |
| 49 |
LIB_NAME), |
| 50 |
AWS_DEFINE_ERROR_INFO( |
| 51 |
AWS_ERROR_EVENT_STREAM_RPC_PROTOCOL_ERROR, |
| 52 |
"event stream rpc connection has encountered a protocol error", |
| 53 |
LIB_NAME), |
| 54 |
AWS_DEFINE_ERROR_INFO( |
| 55 |
AWS_ERROR_EVENT_STREAM_RPC_STREAM_CLOSED, |
| 56 |
"event stream rpc connection stream is closed.", |
| 57 |
LIB_NAME), |
| 58 |
AWS_DEFINE_ERROR_INFO( |
| 59 |
AWS_ERROR_EVENT_STREAM_RPC_STREAM_NOT_ACTIVATED, |
| 60 |
"event stream rpc stream continuation was not successfully activated before use. Call " |
| 61 |
"aws_event_stream_rpc_client_continuation_activate()" |
| 62 |
" before using a stream continuation token.", |
| 63 |
LIB_NAME), |
| 64 |
AWS_DEFINE_ERROR_INFO( |
| 65 |
AWS_ERROR_EVENT_STREAM_MESSAGE_TOO_MANY_HEADERS, |
| 66 |
"message contained too many headers", |
| 67 |
LIB_NAME), |
| 68 |
}; |
| 69 |
|
| 70 |
static struct aws_error_info_list s_list = { |
| 71 |
.error_list = s_errors, |
| 72 |
.count = sizeof(s_errors) / sizeof(struct aws_error_info), |
| 73 |
}; |
| 74 |
|
| 75 |
static bool s_event_stream_library_initialized = false; |
| 76 |
|
| 77 |
static struct aws_log_subject_info s_event_stream_log_subject_infos[] = { |
| 78 |
DEFINE_LOG_SUBJECT_INFO( |
| 79 |
AWS_LS_EVENT_STREAM_GENERAL, |
| 80 |
"event-stream-general", |
| 81 |
"Subject for aws-c-event-stream logging that defies categorization."), |
| 82 |
DEFINE_LOG_SUBJECT_INFO( |
| 83 |
AWS_LS_EVENT_STREAM_CHANNEL_HANDLER, |
| 84 |
"event-stream-channel-handler", |
| 85 |
"Subject for event-stream channel handler related logging."), |
| 86 |
DEFINE_LOG_SUBJECT_INFO( |
| 87 |
AWS_LS_EVENT_STREAM_RPC_SERVER, |
| 88 |
"event-stream-rpc-server", |
| 89 |
"Subject for event-stream rpc server."), |
| 90 |
DEFINE_LOG_SUBJECT_INFO( |
| 91 |
AWS_LS_EVENT_STREAM_RPC_CLIENT, |
| 92 |
"event-stream-rpc-client", |
| 93 |
"Subject for event-stream rpc client."), |
| 94 |
}; |
| 95 |
|
| 96 |
static struct aws_log_subject_info_list s_event_stream_log_subject_list = { |
| 97 |
.subject_list = s_event_stream_log_subject_infos, |
| 98 |
.count = AWS_ARRAY_SIZE(s_event_stream_log_subject_infos), |
| 99 |
}; |
| 100 |
|
| 101 |
static const uint16_t UUID_LEN = 16U; |
| 102 |
|
| 103 |
void aws_event_stream_library_init(struct aws_allocator *allocator) { |
| 104 |
if (!s_event_stream_library_initialized) { |
| 105 |
s_event_stream_library_initialized = true; |
| 106 |
aws_io_library_init(allocator); |
| 107 |
aws_register_error_info(&s_list); |
| 108 |
aws_register_log_subject_info_list(&s_event_stream_log_subject_list); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
void aws_event_stream_library_clean_up(void) { |
| 113 |
if (s_event_stream_library_initialized) { |
| 114 |
s_event_stream_library_initialized = false; |
| 115 |
aws_unregister_error_info(&s_list); |
| 116 |
aws_io_library_clean_up(); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
#define TOTAL_LEN_OFFSET 0 |
| 121 |
#define PRELUDE_CRC_OFFSET (sizeof(uint32_t) + sizeof(uint32_t)) |
| 122 |
#define HEADER_LEN_OFFSET sizeof(uint32_t) |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
uint32_t aws_event_stream_compute_headers_required_buffer_len(const struct aws_array_list *headers) { |
| 127 |
if (!headers || !aws_array_list_length(headers)) { |
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
size_t headers_count = aws_array_list_length(headers); |
| 132 |
size_t headers_len = 0; |
| 133 |
|
| 134 |
for (size_t i = 0; i < headers_count; ++i) { |
| 135 |
struct aws_event_stream_header_value_pair *header = NULL; |
| 136 |
|
| 137 |
aws_array_list_get_at_ptr(headers, (void **)&header, i); |
| 138 |
AWS_FATAL_ASSERT( |
| 139 |
!aws_add_size_checked(headers_len, sizeof(header->header_name_len), &headers_len) && |
| 140 |
"integer overflow occurred computing total headers length."); |
| 141 |
AWS_FATAL_ASSERT( |
| 142 |
!aws_add_size_checked(headers_len, header->header_name_len + 1, &headers_len) && |
| 143 |
"integer overflow occurred computing total headers length."); |
| 144 |
|
| 145 |
if (header->header_value_type == AWS_EVENT_STREAM_HEADER_STRING || |
| 146 |
header->header_value_type == AWS_EVENT_STREAM_HEADER_BYTE_BUF) { |
| 147 |
AWS_FATAL_ASSERT( |
| 148 |
!aws_add_size_checked(headers_len, sizeof(header->header_value_len), &headers_len) && |
| 149 |
"integer overflow occurred computing total headers length."); |
| 150 |
} |
| 151 |
|
| 152 |
if (header->header_value_type != AWS_EVENT_STREAM_HEADER_BOOL_FALSE && |
| 153 |
header->header_value_type != AWS_EVENT_STREAM_HEADER_BOOL_TRUE) { |
| 154 |
AWS_FATAL_ASSERT( |
| 155 |
!aws_add_size_checked(headers_len, header->header_value_len, &headers_len) && |
| 156 |
"integer overflow occurred computing total headers length."); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return (uint32_t)headers_len; |
| 161 |
} |
| 162 |
|
| 163 |
int aws_event_stream_write_headers_to_buffer_safe(const struct aws_array_list *headers, struct aws_byte_buf *buf) { |
| 164 |
AWS_FATAL_PRECONDITION(buf); |
| 165 |
|
| 166 |
if (!headers || !aws_array_list_length(headers)) { |
| 167 |
return AWS_OP_SUCCESS; |
| 168 |
} |
| 169 |
|
| 170 |
size_t headers_count = aws_array_list_length(headers); |
| 171 |
|
| 172 |
for (size_t i = 0; i < headers_count; ++i) { |
| 173 |
struct aws_event_stream_header_value_pair *header = NULL; |
| 174 |
|
| 175 |
aws_array_list_get_at_ptr(headers, (void **)&header, i); |
| 176 |
AWS_RETURN_ERROR_IF( |
| 177 |
aws_byte_buf_write_u8(buf, header->header_name_len), AWS_ERROR_EVENT_STREAM_INSUFFICIENT_BUFFER_LEN); |
| 178 |
AWS_RETURN_ERROR_IF( |
| 179 |
aws_byte_buf_write(buf, (uint8_t *)header->header_name, (size_t)header->header_name_len), |
| 180 |
AWS_ERROR_EVENT_STREAM_INSUFFICIENT_BUFFER_LEN); |
| 181 |
AWS_RETURN_ERROR_IF( |
| 182 |
aws_byte_buf_write_u8(buf, (uint8_t)header->header_value_type), |
| 183 |
AWS_ERROR_EVENT_STREAM_INSUFFICIENT_BUFFER_LEN); |
| 184 |
|
| 185 |
switch (header->header_value_type) { |
| 186 |
case AWS_EVENT_STREAM_HEADER_BOOL_FALSE: |
| 187 |
case AWS_EVENT_STREAM_HEADER_BOOL_TRUE: |
| 188 |
break; |
| 189 |
|
| 190 |
case AWS_EVENT_STREAM_HEADER_BYTE: |
| 191 |
case AWS_EVENT_STREAM_HEADER_INT16: |
| 192 |
case AWS_EVENT_STREAM_HEADER_INT32: |
| 193 |
case AWS_EVENT_STREAM_HEADER_INT64: |
| 194 |
case AWS_EVENT_STREAM_HEADER_TIMESTAMP: |
| 195 |
case AWS_EVENT_STREAM_HEADER_UUID: |
| 196 |
AWS_RETURN_ERROR_IF( |
| 197 |
aws_byte_buf_write(buf, header->header_value.static_val, header->header_value_len), |
| 198 |
AWS_ERROR_EVENT_STREAM_INSUFFICIENT_BUFFER_LEN); |
| 199 |
break; |
| 200 |
case AWS_EVENT_STREAM_HEADER_BYTE_BUF: |
| 201 |
case AWS_EVENT_STREAM_HEADER_STRING: |
| 202 |
AWS_RETURN_ERROR_IF( |
| 203 |
aws_byte_buf_write_be16(buf, header->header_value_len), |
| 204 |
AWS_ERROR_EVENT_STREAM_INSUFFICIENT_BUFFER_LEN); |
| 205 |
AWS_RETURN_ERROR_IF( |
| 206 |
aws_byte_buf_write(buf, header->header_value.variable_len_val, header->header_value_len), |
| 207 |
AWS_ERROR_EVENT_STREAM_INSUFFICIENT_BUFFER_LEN); |
| 208 |
break; |
| 209 |
default: |
| 210 |
AWS_FATAL_ASSERT(false && !"Unknown header type!"); |
| 211 |
break; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
return AWS_OP_SUCCESS; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
size_t aws_event_stream_write_headers_to_buffer(const struct aws_array_list *headers, uint8_t *buffer) { |
| 222 |
AWS_FATAL_PRECONDITION(buffer); |
| 223 |
|
| 224 |
uint32_t min_buffer_len_assumption = aws_event_stream_compute_headers_required_buffer_len(headers); |
| 225 |
struct aws_byte_buf safer_buf = aws_byte_buf_from_empty_array(buffer, min_buffer_len_assumption); |
| 226 |
|
| 227 |
if (aws_event_stream_write_headers_to_buffer_safe(headers, &safer_buf)) { |
| 228 |
return 0; |
| 229 |
} |
| 230 |
|
| 231 |
return safer_buf.len; |
| 232 |
} |
| 233 |
|
| 234 |
int aws_event_stream_read_headers_from_buffer( |
| 235 |
struct aws_array_list *headers, |
| 236 |
const uint8_t *buffer, |
| 237 |
size_t headers_len) { |
| 238 |
|
| 239 |
AWS_FATAL_PRECONDITION(headers); |
| 240 |
AWS_FATAL_PRECONDITION(buffer); |
| 241 |
|
| 242 |
if (AWS_UNLIKELY(headers_len > (size_t)AWS_EVENT_STREAM_MAX_HEADERS_SIZE)) { |
| 243 |
return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED); |
| 244 |
} |
| 245 |
|
| 246 |
size_t header_count = 0; |
| 247 |
struct aws_byte_cursor buffer_cur = aws_byte_cursor_from_array(buffer, headers_len); |
| 248 |
|
| 249 |
while (buffer_cur.len) { |
| 250 |
struct aws_event_stream_header_value_pair header; |
| 251 |
AWS_ZERO_STRUCT(header); |
| 252 |
|
| 253 |
if (header_count >= AWS_EVENT_STREAM_MESSAGE_MAX_HEADERS) { |
| 254 |
return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_TOO_MANY_HEADERS); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
aws_byte_cursor_read_u8(&buffer_cur, &header.header_name_len); |
| 259 |
AWS_RETURN_ERROR_IF( |
| 260 |
aws_byte_cursor_read(&buffer_cur, header.header_name, (size_t)header.header_name_len), |
| 261 |
AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH); |
| 262 |
AWS_RETURN_ERROR_IF( |
| 263 |
aws_byte_cursor_read_u8(&buffer_cur, (uint8_t *)&header.header_value_type), |
| 264 |
AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH); |
| 265 |
|
| 266 |
switch (header.header_value_type) { |
| 267 |
case AWS_EVENT_STREAM_HEADER_BOOL_FALSE: |
| 268 |
header.header_value_len = 0; |
| 269 |
header.header_value.static_val[0] = 0; |
| 270 |
break; |
| 271 |
case AWS_EVENT_STREAM_HEADER_BOOL_TRUE: |
| 272 |
header.header_value_len = 0; |
| 273 |
header.header_value.static_val[0] = 1; |
| 274 |
break; |
| 275 |
case AWS_EVENT_STREAM_HEADER_BYTE: |
| 276 |
header.header_value_len = sizeof(uint8_t); |
| 277 |
AWS_RETURN_ERROR_IF( |
| 278 |
aws_byte_cursor_read(&buffer_cur, header.header_value.static_val, header.header_value_len), |
| 279 |
AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH); |
| 280 |
break; |
| 281 |
case AWS_EVENT_STREAM_HEADER_INT16: |
| 282 |
header.header_value_len = sizeof(uint16_t); |
| 283 |
AWS_RETURN_ERROR_IF( |
| 284 |
aws_byte_cursor_read(&buffer_cur, header.header_value.static_val, header.header_value_len), |
| 285 |
AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH); |
| 286 |
break; |
| 287 |
case AWS_EVENT_STREAM_HEADER_INT32: |
| 288 |
header.header_value_len = sizeof(uint32_t); |
| 289 |
AWS_RETURN_ERROR_IF( |
| 290 |
aws_byte_cursor_read(&buffer_cur, header.header_value.static_val, header.header_value_len), |
| 291 |
AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH); |
| 292 |
break; |
| 293 |
case AWS_EVENT_STREAM_HEADER_INT64: |
| 294 |
case AWS_EVENT_STREAM_HEADER_TIMESTAMP: |
| 295 |
header.header_value_len = sizeof(uint64_t); |
| 296 |
AWS_RETURN_ERROR_IF( |
| 297 |
aws_byte_cursor_read(&buffer_cur, header.header_value.static_val, header.header_value_len), |
| 298 |
AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH); |
| 299 |
break; |
| 300 |
case AWS_EVENT_STREAM_HEADER_BYTE_BUF: |
| 301 |
case AWS_EVENT_STREAM_HEADER_STRING: |
| 302 |
AWS_RETURN_ERROR_IF( |
| 303 |
aws_byte_cursor_read_be16(&buffer_cur, &header.header_value_len), |
| 304 |
AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH); |
| 305 |
AWS_RETURN_ERROR_IF( |
| 306 |
header.header_value_len <= INT16_MAX, AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN); |
| 307 |
AWS_RETURN_ERROR_IF( |
| 308 |
buffer_cur.len >= header.header_value_len, AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH); |
| 309 |
header.header_value.variable_len_val = (uint8_t *)buffer_cur.ptr; |
| 310 |
aws_byte_cursor_advance(&buffer_cur, header.header_value_len); |
| 311 |
break; |
| 312 |
case AWS_EVENT_STREAM_HEADER_UUID: |
| 313 |
header.header_value_len = UUID_LEN; |
| 314 |
AWS_RETURN_ERROR_IF( |
| 315 |
aws_byte_cursor_read(&buffer_cur, header.header_value.static_val, UUID_LEN), |
| 316 |
AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH); |
| 317 |
break; |
| 318 |
} |
| 319 |
|
| 320 |
if (aws_array_list_push_back(headers, (const void *)&header)) { |
| 321 |
return AWS_OP_ERR; |
| 322 |
} |
| 323 |
|
| 324 |
++header_count; |
| 325 |
} |
| 326 |
|
| 327 |
return AWS_OP_SUCCESS; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
int aws_event_stream_message_init( |
| 334 |
struct aws_event_stream_message *message, |
| 335 |
struct aws_allocator *alloc, |
| 336 |
const struct aws_array_list *headers, |
| 337 |
const struct aws_byte_buf *payload) { |
| 338 |
AWS_FATAL_PRECONDITION(message); |
| 339 |
AWS_FATAL_PRECONDITION(alloc); |
| 340 |
|
| 341 |
size_t payload_len = payload ? payload->len : 0; |
| 342 |
|
| 343 |
uint32_t headers_length = aws_event_stream_compute_headers_required_buffer_len(headers); |
| 344 |
|
| 345 |
if (AWS_UNLIKELY(headers_length > AWS_EVENT_STREAM_MAX_HEADERS_SIZE)) { |
| 346 |
return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED); |
| 347 |
} |
| 348 |
|
| 349 |
uint32_t total_length = |
| 350 |
(uint32_t)(AWS_EVENT_STREAM_PRELUDE_LENGTH + headers_length + payload_len + AWS_EVENT_STREAM_TRAILER_LENGTH); |
| 351 |
|
| 352 |
if (AWS_UNLIKELY(total_length < headers_length || total_length < payload_len)) { |
| 353 |
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); |
| 354 |
} |
| 355 |
|
| 356 |
if (AWS_UNLIKELY(total_length > AWS_EVENT_STREAM_MAX_MESSAGE_SIZE)) { |
| 357 |
return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED); |
| 358 |
} |
| 359 |
|
| 360 |
message->alloc = alloc; |
| 361 |
aws_byte_buf_init(&message->message_buffer, message->alloc, total_length); |
| 362 |
|
| 363 |
aws_byte_buf_write_be32(&message->message_buffer, total_length); |
| 364 |
aws_byte_buf_write_be32(&message->message_buffer, headers_length); |
| 365 |
|
| 366 |
uint32_t running_crc = aws_checksums_crc32(message->message_buffer.buffer, (int)message->message_buffer.len, 0); |
| 367 |
|
| 368 |
const uint8_t *pre_prelude_marker = message->message_buffer.buffer + message->message_buffer.len; |
| 369 |
size_t pre_prelude_position_marker = message->message_buffer.len; |
| 370 |
aws_byte_buf_write_be32(&message->message_buffer, running_crc); |
| 371 |
|
| 372 |
if (headers_length) { |
| 373 |
if (aws_event_stream_write_headers_to_buffer_safe(headers, &message->message_buffer)) { |
| 374 |
aws_event_stream_message_clean_up(message); |
| 375 |
return AWS_OP_ERR; |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
if (payload) { |
| 380 |
aws_byte_buf_write_from_whole_buffer(&message->message_buffer, *payload); |
| 381 |
} |
| 382 |
|
| 383 |
running_crc = aws_checksums_crc32( |
| 384 |
pre_prelude_marker, (int)(message->message_buffer.len - pre_prelude_position_marker), running_crc); |
| 385 |
aws_byte_buf_write_be32(&message->message_buffer, running_crc); |
| 386 |
|
| 387 |
return AWS_OP_SUCCESS; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
int aws_event_stream_message_from_buffer( |
| 392 |
struct aws_event_stream_message *message, |
| 393 |
struct aws_allocator *alloc, |
| 394 |
struct aws_byte_buf *buffer) { |
| 395 |
AWS_FATAL_PRECONDITION(message); |
| 396 |
AWS_FATAL_PRECONDITION(alloc); |
| 397 |
AWS_FATAL_PRECONDITION(buffer); |
| 398 |
|
| 399 |
message->alloc = alloc; |
| 400 |
|
| 401 |
if (AWS_UNLIKELY(buffer->len < AWS_EVENT_STREAM_PRELUDE_LENGTH + AWS_EVENT_STREAM_TRAILER_LENGTH)) { |
| 402 |
return aws_raise_error(AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH); |
| 403 |
} |
| 404 |
|
| 405 |
struct aws_byte_cursor parsing_cur = aws_byte_cursor_from_buf(buffer); |
| 406 |
|
| 407 |
uint32_t message_length = 0; |
| 408 |
aws_byte_cursor_read_be32(&parsing_cur, &message_length); |
| 409 |
|
| 410 |
if (AWS_UNLIKELY(message_length != buffer->len)) { |
| 411 |
return aws_raise_error(AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH); |
| 412 |
} |
| 413 |
|
| 414 |
if (AWS_UNLIKELY(message_length > AWS_EVENT_STREAM_MAX_MESSAGE_SIZE)) { |
| 415 |
return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED); |
| 416 |
} |
| 417 |
|
| 418 |
aws_byte_cursor_advance(&parsing_cur, sizeof(uint32_t)); |
| 419 |
uint32_t running_crc = aws_checksums_crc32(buffer->buffer, (int)PRELUDE_CRC_OFFSET, 0); |
| 420 |
uint32_t prelude_crc = 0; |
| 421 |
const uint8_t *start_of_payload_checksum = parsing_cur.ptr; |
| 422 |
size_t start_of_payload_checksum_pos = PRELUDE_CRC_OFFSET; |
| 423 |
aws_byte_cursor_read_be32(&parsing_cur, &prelude_crc); |
| 424 |
|
| 425 |
if (running_crc != prelude_crc) { |
| 426 |
return aws_raise_error(AWS_ERROR_EVENT_STREAM_PRELUDE_CHECKSUM_FAILURE); |
| 427 |
} |
| 428 |
|
| 429 |
running_crc = aws_checksums_crc32( |
| 430 |
start_of_payload_checksum, |
| 431 |
(int)(message_length - start_of_payload_checksum_pos - AWS_EVENT_STREAM_TRAILER_LENGTH), |
| 432 |
running_crc); |
| 433 |
uint32_t message_crc = aws_read_u32(buffer->buffer + message_length - AWS_EVENT_STREAM_TRAILER_LENGTH); |
| 434 |
|
| 435 |
if (running_crc != message_crc) { |
| 436 |
return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_CHECKSUM_FAILURE); |
| 437 |
} |
| 438 |
|
| 439 |
message->message_buffer = *buffer; |
| 440 |
|
| 441 |
|
| 442 |
message->message_buffer.allocator = NULL; |
| 443 |
|
| 444 |
if (aws_event_stream_message_headers_len(message) > |
| 445 |
message_length - AWS_EVENT_STREAM_PRELUDE_LENGTH - AWS_EVENT_STREAM_TRAILER_LENGTH) { |
| 446 |
AWS_ZERO_STRUCT(message->message_buffer); |
| 447 |
return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN); |
| 448 |
} |
| 449 |
|
| 450 |
return AWS_OP_SUCCESS; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
int aws_event_stream_message_from_buffer_copy( |
| 455 |
struct aws_event_stream_message *message, |
| 456 |
struct aws_allocator *alloc, |
| 457 |
const struct aws_byte_buf *buffer) { |
| 458 |
int parse_value = aws_event_stream_message_from_buffer(message, alloc, (struct aws_byte_buf *)buffer); |
| 459 |
|
| 460 |
if (!parse_value) { |
| 461 |
aws_byte_buf_init_copy(&message->message_buffer, alloc, buffer); |
| 462 |
message->alloc = alloc; |
| 463 |
return AWS_OP_SUCCESS; |
| 464 |
} |
| 465 |
|
| 466 |
return parse_value; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
void aws_event_stream_message_clean_up(struct aws_event_stream_message *message) { |
| 471 |
aws_byte_buf_clean_up(&message->message_buffer); |
| 472 |
} |
| 473 |
|
| 474 |
uint32_t aws_event_stream_message_total_length(const struct aws_event_stream_message *message) { |
| 475 |
struct aws_byte_cursor read_cur = aws_byte_cursor_from_buf(&message->message_buffer); |
| 476 |
aws_byte_cursor_advance(&read_cur, TOTAL_LEN_OFFSET); |
| 477 |
uint32_t total_len = 0; |
| 478 |
aws_byte_cursor_read_be32(&read_cur, &total_len); |
| 479 |
|
| 480 |
return total_len; |
| 481 |
} |
| 482 |
|
| 483 |
uint32_t aws_event_stream_message_headers_len(const struct aws_event_stream_message *message) { |
| 484 |
struct aws_byte_cursor read_cur = aws_byte_cursor_from_buf(&message->message_buffer); |
| 485 |
aws_byte_cursor_advance(&read_cur, HEADER_LEN_OFFSET); |
| 486 |
|
| 487 |
uint32_t headers_len = 0; |
| 488 |
aws_byte_cursor_read_be32(&read_cur, &headers_len); |
| 489 |
|
| 490 |
return headers_len; |
| 491 |
} |
| 492 |
|
| 493 |
uint32_t aws_event_stream_message_prelude_crc(const struct aws_event_stream_message *message) { |
| 494 |
struct aws_byte_cursor read_cur = aws_byte_cursor_from_buf(&message->message_buffer); |
| 495 |
aws_byte_cursor_advance(&read_cur, PRELUDE_CRC_OFFSET); |
| 496 |
|
| 497 |
uint32_t prelude_crc = 0; |
| 498 |
aws_byte_cursor_read_be32(&read_cur, &prelude_crc); |
| 499 |
|
| 500 |
return prelude_crc; |
| 501 |
} |
| 502 |
|
| 503 |
int aws_event_stream_message_headers(const struct aws_event_stream_message *message, struct aws_array_list *headers) { |
| 504 |
struct aws_byte_cursor read_cur = aws_byte_cursor_from_buf(&message->message_buffer); |
| 505 |
aws_byte_cursor_advance(&read_cur, AWS_EVENT_STREAM_PRELUDE_LENGTH); |
| 506 |
|
| 507 |
return aws_event_stream_read_headers_from_buffer( |
| 508 |
headers, read_cur.ptr, aws_event_stream_message_headers_len(message)); |
| 509 |
} |
| 510 |
|
| 511 |
const uint8_t *aws_event_stream_message_payload(const struct aws_event_stream_message *message) { |
| 512 |
AWS_FATAL_PRECONDITION(message); |
| 513 |
struct aws_byte_cursor read_cur = aws_byte_cursor_from_buf(&message->message_buffer); |
| 514 |
aws_byte_cursor_advance(&read_cur, AWS_EVENT_STREAM_PRELUDE_LENGTH + aws_event_stream_message_headers_len(message)); |
| 515 |
return read_cur.ptr; |
| 516 |
} |
| 517 |
|
| 518 |
uint32_t aws_event_stream_message_payload_len(const struct aws_event_stream_message *message) { |
| 519 |
AWS_FATAL_PRECONDITION(message); |
| 520 |
return aws_event_stream_message_total_length(message) - |
| 521 |
(AWS_EVENT_STREAM_PRELUDE_LENGTH + aws_event_stream_message_headers_len(message) + |
| 522 |
AWS_EVENT_STREAM_TRAILER_LENGTH); |
| 523 |
} |
| 524 |
|
| 525 |
uint32_t aws_event_stream_message_message_crc(const struct aws_event_stream_message *message) { |
| 526 |
AWS_FATAL_PRECONDITION(message); |
| 527 |
struct aws_byte_cursor read_cur = aws_byte_cursor_from_buf(&message->message_buffer); |
| 528 |
aws_byte_cursor_advance( |
| 529 |
&read_cur, aws_event_stream_message_total_length(message) - AWS_EVENT_STREAM_TRAILER_LENGTH); |
| 530 |
|
| 531 |
uint32_t message_crc = 0; |
| 532 |
aws_byte_cursor_read_be32(&read_cur, &message_crc); |
| 533 |
|
| 534 |
return message_crc; |
| 535 |
} |
| 536 |
|
| 537 |
const uint8_t *aws_event_stream_message_buffer(const struct aws_event_stream_message *message) { |
| 538 |
AWS_FATAL_PRECONDITION(message); |
| 539 |
return message->message_buffer.buffer; |
| 540 |
} |
| 541 |
|
| 542 |
#define DEBUG_STR_PRELUDE_TOTAL_LEN "\"total_length\": " |
| 543 |
#define DEBUG_STR_PRELUDE_HDRS_LEN "\"headers_length\": " |
| 544 |
#define DEBUG_STR_PRELUDE_CRC "\"prelude_crc\": " |
| 545 |
#define DEBUG_STR_MESSAGE_CRC "\"message_crc\": " |
| 546 |
#define DEBUG_STR_HEADER_NAME "\"name\": " |
| 547 |
#define DEBUG_STR_HEADER_VALUE "\"value\": " |
| 548 |
#define DEBUG_STR_HEADER_TYPE "\"type\": " |
| 549 |
|
| 550 |
int aws_event_stream_message_to_debug_str(FILE *fd, const struct aws_event_stream_message *message) { |
| 551 |
AWS_FATAL_PRECONDITION(fd); |
| 552 |
AWS_FATAL_PRECONDITION(message); |
| 553 |
|
| 554 |
struct aws_array_list headers; |
| 555 |
aws_event_stream_headers_list_init(&headers, message->alloc); |
| 556 |
aws_event_stream_message_headers(message, &headers); |
| 557 |
|
| 558 |
fprintf( |
| 559 |
fd, |
| 560 |
"{\n " DEBUG_STR_PRELUDE_TOTAL_LEN "%d,\n " DEBUG_STR_PRELUDE_HDRS_LEN "%d,\n " DEBUG_STR_PRELUDE_CRC |
| 561 |
"%d,\n", |
| 562 |
aws_event_stream_message_total_length(message), |
| 563 |
aws_event_stream_message_headers_len(message), |
| 564 |
aws_event_stream_message_prelude_crc(message)); |
| 565 |
|
| 566 |
int count = 0; |
| 567 |
|
| 568 |
uint16_t headers_count = (uint16_t)aws_array_list_length(&headers); |
| 569 |
|
| 570 |
fprintf(fd, " \"headers\": ["); |
| 571 |
|
| 572 |
for (uint16_t i = 0; i < headers_count; ++i) { |
| 573 |
struct aws_event_stream_header_value_pair *header = NULL; |
| 574 |
|
| 575 |
aws_array_list_get_at_ptr(&headers, (void **)&header, i); |
| 576 |
|
| 577 |
fprintf(fd, " {\n"); |
| 578 |
|
| 579 |
fprintf(fd, " " DEBUG_STR_HEADER_NAME "\""); |
| 580 |
fwrite(header->header_name, sizeof(char), (size_t)header->header_name_len, fd); |
| 581 |
fprintf(fd, "\",\n"); |
| 582 |
|
| 583 |
fprintf(fd, " " DEBUG_STR_HEADER_TYPE "%d,\n", header->header_value_type); |
| 584 |
|
| 585 |
if (header->header_value_type == AWS_EVENT_STREAM_HEADER_BOOL_FALSE) { |
| 586 |
fprintf(fd, " " DEBUG_STR_HEADER_VALUE "false\n"); |
| 587 |
} else if (header->header_value_type == AWS_EVENT_STREAM_HEADER_BOOL_TRUE) { |
| 588 |
fprintf(fd, " " DEBUG_STR_HEADER_VALUE "true\n"); |
| 589 |
} else if (header->header_value_type == AWS_EVENT_STREAM_HEADER_BYTE) { |
| 590 |
int8_t int_value = (int8_t)header->header_value.static_val[0]; |
| 591 |
fprintf(fd, " " DEBUG_STR_HEADER_VALUE "%d\n", (int)int_value); |
| 592 |
} else if (header->header_value_type == AWS_EVENT_STREAM_HEADER_INT16) { |
| 593 |
int16_t int_value = aws_read_u16(header->header_value.static_val); |
| 594 |
fprintf(fd, " " DEBUG_STR_HEADER_VALUE "%d\n", (int)int_value); |
| 595 |
} else if (header->header_value_type == AWS_EVENT_STREAM_HEADER_INT32) { |
| 596 |
int32_t int_value = (int32_t)aws_read_u32(header->header_value.static_val); |
| 597 |
fprintf(fd, " " DEBUG_STR_HEADER_VALUE "%d\n", (int)int_value); |
| 598 |
} else if ( |
| 599 |
header->header_value_type == AWS_EVENT_STREAM_HEADER_INT64 || |
| 600 |
header->header_value_type == AWS_EVENT_STREAM_HEADER_TIMESTAMP) { |
| 601 |
int64_t int_value = (int64_t)aws_read_u64(header->header_value.static_val); |
| 602 |
fprintf(fd, " " DEBUG_STR_HEADER_VALUE "%lld\n", (long long)int_value); |
| 603 |
} else { |
| 604 |
size_t buffer_len = 0; |
| 605 |
aws_base64_compute_encoded_len(header->header_value_len, &buffer_len); |
| 606 |
|
| 607 |
struct aws_byte_buf encode_output; |
| 608 |
aws_byte_buf_init(&encode_output, message->alloc, buffer_len); |
| 609 |
|
| 610 |
if (header->header_value_type == AWS_EVENT_STREAM_HEADER_UUID) { |
| 611 |
struct aws_byte_cursor to_encode = |
| 612 |
aws_byte_cursor_from_array(header->header_value.static_val, header->header_value_len); |
| 613 |
|
| 614 |
aws_base64_encode(&to_encode, &encode_output); |
| 615 |
} else { |
| 616 |
struct aws_byte_cursor to_encode = |
| 617 |
aws_byte_cursor_from_array(header->header_value.variable_len_val, header->header_value_len); |
| 618 |
aws_base64_encode(&to_encode, &encode_output); |
| 619 |
} |
| 620 |
fprintf(fd, " " DEBUG_STR_HEADER_VALUE "\"" PRInSTR "\"\n", AWS_BYTE_BUF_PRI(encode_output)); |
| 621 |
aws_byte_buf_clean_up(&encode_output); |
| 622 |
} |
| 623 |
|
| 624 |
fprintf(fd, " }"); |
| 625 |
|
| 626 |
if (count < headers_count - 1) { |
| 627 |
fprintf(fd, ","); |
| 628 |
} |
| 629 |
fprintf(fd, "\n"); |
| 630 |
|
| 631 |
count++; |
| 632 |
} |
| 633 |
aws_event_stream_headers_list_cleanup(&headers); |
| 634 |
fprintf(fd, " ],\n"); |
| 635 |
|
| 636 |
size_t payload_len = aws_event_stream_message_payload_len(message); |
| 637 |
const uint8_t *payload = aws_event_stream_message_payload(message); |
| 638 |
size_t encoded_len = 0; |
| 639 |
aws_base64_compute_encoded_len(payload_len, &encoded_len); |
| 640 |
|
| 641 |
struct aws_byte_cursor payload_buffer = aws_byte_cursor_from_array(payload, payload_len); |
| 642 |
struct aws_byte_buf encoded_payload_buffer; |
| 643 |
aws_byte_buf_init(&encoded_payload_buffer, message->alloc, encoded_len); |
| 644 |
|
| 645 |
aws_base64_encode(&payload_buffer, &encoded_payload_buffer); |
| 646 |
fprintf(fd, " \"payload\": \"" PRInSTR "\",\n", AWS_BYTE_BUF_PRI(encoded_payload_buffer)); |
| 647 |
aws_byte_buf_clean_up(&encoded_payload_buffer); |
| 648 |
|
| 649 |
fprintf(fd, " " DEBUG_STR_MESSAGE_CRC "%d\n}\n", aws_event_stream_message_message_crc(message)); |
| 650 |
|
| 651 |
return AWS_OP_SUCCESS; |
| 652 |
} |
| 653 |
|
| 654 |
int aws_event_stream_headers_list_init(struct aws_array_list *headers, struct aws_allocator *allocator) { |
| 655 |
AWS_FATAL_PRECONDITION(headers); |
| 656 |
AWS_FATAL_PRECONDITION(allocator); |
| 657 |
|
| 658 |
return aws_array_list_init_dynamic(headers, allocator, 4, sizeof(struct aws_event_stream_header_value_pair)); |
| 659 |
} |
| 660 |
|
| 661 |
void aws_event_stream_headers_list_cleanup(struct aws_array_list *headers) { |
| 662 |
AWS_FATAL_PRECONDITION(headers); |
| 663 |
|
| 664 |
if (AWS_UNLIKELY(!headers || !aws_array_list_is_valid(headers))) { |
| 665 |
return; |
| 666 |
} |
| 667 |
|
| 668 |
for (size_t i = 0; i < aws_array_list_length(headers); ++i) { |
| 669 |
struct aws_event_stream_header_value_pair *header = NULL; |
| 670 |
aws_array_list_get_at_ptr(headers, (void **)&header, i); |
| 671 |
|
| 672 |
if (header->value_owned) { |
| 673 |
aws_mem_release(headers->alloc, (void *)header->header_value.variable_len_val); |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
aws_array_list_clean_up(headers); |
| 678 |
} |
| 679 |
|
| 680 |
static int s_add_variable_len_header( |
| 681 |
struct aws_array_list *headers, |
| 682 |
struct aws_event_stream_header_value_pair *header, |
| 683 |
const char *name, |
| 684 |
uint8_t name_len, |
| 685 |
uint8_t *value, |
| 686 |
uint16_t value_len, |
| 687 |
int8_t copy) { |
| 688 |
|
| 689 |
memcpy((void *)header->header_name, (void *)name, (size_t)name_len); |
| 690 |
|
| 691 |
if (value_len != 0 && copy) { |
| 692 |
header->header_value.variable_len_val = aws_mem_acquire(headers->alloc, value_len); |
| 693 |
header->value_owned = 1; |
| 694 |
memcpy((void *)header->header_value.variable_len_val, (void *)value, value_len); |
| 695 |
} else { |
| 696 |
header->value_owned = 0; |
| 697 |
header->header_value.variable_len_val = value; |
| 698 |
} |
| 699 |
|
| 700 |
if (aws_array_list_push_back(headers, (void *)header)) { |
| 701 |
if (header->value_owned) { |
| 702 |
aws_mem_release(headers->alloc, (void *)header->header_value.variable_len_val); |
| 703 |
} |
| 704 |
return AWS_OP_ERR; |
| 705 |
} |
| 706 |
|
| 707 |
return AWS_OP_SUCCESS; |
| 708 |
} |
| 709 |
|
| 710 |
int aws_event_stream_add_string_header( |
| 711 |
struct aws_array_list *headers, |
| 712 |
const char *name, |
| 713 |
uint8_t name_len, |
| 714 |
const char *value, |
| 715 |
uint16_t value_len, |
| 716 |
int8_t copy) { |
| 717 |
AWS_FATAL_PRECONDITION(headers); |
| 718 |
AWS_RETURN_ERROR_IF( |
| 719 |
value_len <= AWS_EVENT_STREAM_HEADER_VALUE_LEN_MAX, AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN); |
| 720 |
struct aws_event_stream_header_value_pair header = { |
| 721 |
.header_name_len = name_len, |
| 722 |
.header_value_len = value_len, |
| 723 |
.value_owned = copy, |
| 724 |
.header_value_type = AWS_EVENT_STREAM_HEADER_STRING, |
| 725 |
}; |
| 726 |
|
| 727 |
return s_add_variable_len_header(headers, &header, name, name_len, (uint8_t *)value, value_len, copy); |
| 728 |
} |
| 729 |
|
| 730 |
struct aws_event_stream_header_value_pair aws_event_stream_create_string_header( |
| 731 |
struct aws_byte_cursor name, |
| 732 |
struct aws_byte_cursor value) { |
| 733 |
AWS_FATAL_PRECONDITION(name.len <= AWS_EVENT_STREAM_HEADER_NAME_LEN_MAX); |
| 734 |
AWS_FATAL_PRECONDITION(value.len <= AWS_EVENT_STREAM_HEADER_VALUE_LEN_MAX); |
| 735 |
|
| 736 |
struct aws_event_stream_header_value_pair header = { |
| 737 |
.header_value_type = AWS_EVENT_STREAM_HEADER_STRING, |
| 738 |
.header_value.variable_len_val = value.ptr, |
| 739 |
.header_value_len = (uint16_t)value.len, |
| 740 |
.header_name_len = (uint8_t)name.len, |
| 741 |
.value_owned = 0, |
| 742 |
}; |
| 743 |
|
| 744 |
memcpy(header.header_name, name.ptr, name.len); |
| 745 |
|
| 746 |
return header; |
| 747 |
} |
| 748 |
|
| 749 |
struct aws_event_stream_header_value_pair aws_event_stream_create_int32_header( |
| 750 |
struct aws_byte_cursor name, |
| 751 |
int32_t value) { |
| 752 |
AWS_FATAL_PRECONDITION(name.len <= AWS_EVENT_STREAM_HEADER_NAME_LEN_MAX); |
| 753 |
|
| 754 |
struct aws_event_stream_header_value_pair header = { |
| 755 |
.header_value_type = AWS_EVENT_STREAM_HEADER_INT32, |
| 756 |
.header_value_len = (uint16_t)sizeof(int32_t), |
| 757 |
.header_name_len = (uint8_t)name.len, |
| 758 |
.value_owned = 0, |
| 759 |
}; |
| 760 |
|
| 761 |
memcpy(header.header_name, name.ptr, name.len); |
| 762 |
aws_write_u32((uint32_t)value, header.header_value.static_val); |
| 763 |
|
| 764 |
return header; |
| 765 |
} |
| 766 |
|
| 767 |
int aws_event_stream_add_bool_header(struct aws_array_list *headers, const char *name, uint8_t name_len, int8_t value) { |
| 768 |
struct aws_byte_cursor name_cursor = aws_byte_cursor_from_array(name, (size_t)name_len); |
| 769 |
|
| 770 |
return aws_event_stream_add_bool_header_by_cursor(headers, name_cursor, value != 0); |
| 771 |
} |
| 772 |
|
| 773 |
#define AWS_EVENT_STREAM_VALIDATE_HEADER_NAME_CURSOR(name_cursor) \ |
| 774 |
AWS_FATAL_PRECONDITION(name_cursor.len > 0); \ |
| 775 |
AWS_FATAL_PRECONDITION(name_cursor.ptr != NULL); \ |
| 776 |
AWS_RETURN_ERROR_IF( \ |
| 777 |
name_cursor.len <= AWS_EVENT_STREAM_HEADER_NAME_LEN_MAX, AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN); |
| 778 |
|
| 779 |
int aws_event_stream_add_bool_header_by_cursor( |
| 780 |
struct aws_array_list *headers, |
| 781 |
struct aws_byte_cursor name, |
| 782 |
bool value) { |
| 783 |
|
| 784 |
AWS_FATAL_PRECONDITION(headers); |
| 785 |
AWS_EVENT_STREAM_VALIDATE_HEADER_NAME_CURSOR(name); |
| 786 |
|
| 787 |
struct aws_event_stream_header_value_pair header = { |
| 788 |
.header_name_len = (uint8_t)name.len, |
| 789 |
.header_value_len = 0, |
| 790 |
.value_owned = 0, |
| 791 |
.header_value_type = value ? AWS_EVENT_STREAM_HEADER_BOOL_TRUE : AWS_EVENT_STREAM_HEADER_BOOL_FALSE, |
| 792 |
}; |
| 793 |
|
| 794 |
memcpy((void *)header.header_name, (void *)name.ptr, (size_t)name.len); |
| 795 |
|
| 796 |
return aws_array_list_push_back(headers, (void *)&header); |
| 797 |
} |
| 798 |
|
| 799 |
int aws_event_stream_add_byte_header(struct aws_array_list *headers, const char *name, uint8_t name_len, int8_t value) { |
| 800 |
struct aws_byte_cursor name_cursor = aws_byte_cursor_from_array(name, (size_t)name_len); |
| 801 |
|
| 802 |
return aws_event_stream_add_byte_header_by_cursor(headers, name_cursor, value); |
| 803 |
} |
| 804 |
|
| 805 |
int aws_event_stream_add_byte_header_by_cursor( |
| 806 |
struct aws_array_list *headers, |
| 807 |
struct aws_byte_cursor name, |
| 808 |
int8_t value) { |
| 809 |
|
| 810 |
AWS_FATAL_PRECONDITION(headers); |
| 811 |
AWS_EVENT_STREAM_VALIDATE_HEADER_NAME_CURSOR(name); |
| 812 |
|
| 813 |
struct aws_event_stream_header_value_pair header = { |
| 814 |
.header_name_len = (uint8_t)name.len, |
| 815 |
.header_value_len = 1, |
| 816 |
.value_owned = 0, |
| 817 |
.header_value_type = AWS_EVENT_STREAM_HEADER_BYTE, |
| 818 |
}; |
| 819 |
header.header_value.static_val[0] = (uint8_t)value; |
| 820 |
|
| 821 |
memcpy((void *)header.header_name, (void *)name.ptr, (size_t)name.len); |
| 822 |
|
| 823 |
return aws_array_list_push_back(headers, (void *)&header); |
| 824 |
} |
| 825 |
|
| 826 |
int aws_event_stream_add_int16_header( |
| 827 |
struct aws_array_list *headers, |
| 828 |
const char *name, |
| 829 |
uint8_t name_len, |
| 830 |
int16_t value) { |
| 831 |
|
| 832 |
struct aws_byte_cursor name_cursor = aws_byte_cursor_from_array(name, (size_t)name_len); |
| 833 |
|
| 834 |
return aws_event_stream_add_int16_header_by_cursor(headers, name_cursor, value); |
| 835 |
} |
| 836 |
|
| 837 |
int aws_event_stream_add_int16_header_by_cursor( |
| 838 |
struct aws_array_list *headers, |
| 839 |
struct aws_byte_cursor name, |
| 840 |
int16_t value) { |
| 841 |
|
| 842 |
AWS_FATAL_PRECONDITION(headers); |
| 843 |
AWS_EVENT_STREAM_VALIDATE_HEADER_NAME_CURSOR(name); |
| 844 |
|
| 845 |
struct aws_event_stream_header_value_pair header = { |
| 846 |
.header_name_len = (uint8_t)name.len, |
| 847 |
.header_value_len = sizeof(value), |
| 848 |
.value_owned = 0, |
| 849 |
.header_value_type = AWS_EVENT_STREAM_HEADER_INT16, |
| 850 |
}; |
| 851 |
|
| 852 |
aws_write_u16((uint16_t)value, header.header_value.static_val); |
| 853 |
|
| 854 |
memcpy((void *)header.header_name, (void *)name.ptr, (size_t)name.len); |
| 855 |
|
| 856 |
return aws_array_list_push_back(headers, (void *)&header); |
| 857 |
} |
| 858 |
|
| 859 |
int aws_event_stream_add_int32_header( |
| 860 |
struct aws_array_list *headers, |
| 861 |
const char *name, |
| 862 |
uint8_t name_len, |
| 863 |
int32_t value) { |
| 864 |
|
| 865 |
struct aws_byte_cursor name_cursor = aws_byte_cursor_from_array(name, (size_t)name_len); |
| 866 |
|
| 867 |
return aws_event_stream_add_int32_header_by_cursor(headers, name_cursor, value); |
| 868 |
} |
| 869 |
|
| 870 |
int aws_event_stream_add_int32_header_by_cursor( |
| 871 |
struct aws_array_list *headers, |
| 872 |
struct aws_byte_cursor name, |
| 873 |
int32_t value) { |
| 874 |
AWS_FATAL_PRECONDITION(headers); |
| 875 |
AWS_EVENT_STREAM_VALIDATE_HEADER_NAME_CURSOR(name); |
| 876 |
|
| 877 |
struct aws_event_stream_header_value_pair header = { |
| 878 |
.header_name_len = (uint8_t)name.len, |
| 879 |
.header_value_len = sizeof(value), |
| 880 |
.value_owned = 0, |
| 881 |
.header_value_type = AWS_EVENT_STREAM_HEADER_INT32, |
| 882 |
}; |
| 883 |
|
| 884 |
aws_write_u32((uint32_t)value, header.header_value.static_val); |
| 885 |
|
| 886 |
memcpy((void *)header.header_name, (void *)name.ptr, (size_t)name.len); |
| 887 |
|
| 888 |
return aws_array_list_push_back(headers, (void *)&header); |
| 889 |
} |
| 890 |
|
| 891 |
int aws_event_stream_add_int64_header( |
| 892 |
struct aws_array_list *headers, |
| 893 |
const char *name, |
| 894 |
uint8_t name_len, |
| 895 |
int64_t value) { |
| 896 |
|
| 897 |
struct aws_byte_cursor name_cursor = aws_byte_cursor_from_array(name, (size_t)name_len); |
| 898 |
|
| 899 |
return aws_event_stream_add_int64_header_by_cursor(headers, name_cursor, value); |
| 900 |
} |
| 901 |
|
| 902 |
int aws_event_stream_add_int64_header_by_cursor( |
| 903 |
struct aws_array_list *headers, |
| 904 |
struct aws_byte_cursor name, |
| 905 |
int64_t value) { |
| 906 |
|
| 907 |
AWS_FATAL_PRECONDITION(headers); |
| 908 |
AWS_EVENT_STREAM_VALIDATE_HEADER_NAME_CURSOR(name); |
| 909 |
|
| 910 |
struct aws_event_stream_header_value_pair header = { |
| 911 |
.header_name_len = (uint8_t)name.len, |
| 912 |
.header_value_len = sizeof(value), |
| 913 |
.value_owned = 0, |
| 914 |
.header_value_type = AWS_EVENT_STREAM_HEADER_INT64, |
| 915 |
}; |
| 916 |
|
| 917 |
aws_write_u64((uint64_t)value, header.header_value.static_val); |
| 918 |
|
| 919 |
memcpy((void *)header.header_name, (void *)name.ptr, (size_t)name.len); |
| 920 |
|
| 921 |
return aws_array_list_push_back(headers, (void *)&header); |
| 922 |
} |
| 923 |
|
| 924 |
int aws_event_stream_add_string_header_by_cursor( |
| 925 |
struct aws_array_list *headers, |
| 926 |
struct aws_byte_cursor name, |
| 927 |
struct aws_byte_cursor value) { |
| 928 |
|
| 929 |
AWS_FATAL_PRECONDITION(headers); |
| 930 |
AWS_EVENT_STREAM_VALIDATE_HEADER_NAME_CURSOR(name); |
| 931 |
AWS_RETURN_ERROR_IF( |
| 932 |
value.len <= AWS_EVENT_STREAM_HEADER_VALUE_LEN_MAX, AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN); |
| 933 |
|
| 934 |
struct aws_event_stream_header_value_pair header = { |
| 935 |
.header_name_len = (uint8_t)name.len, |
| 936 |
.header_value_len = (uint16_t)value.len, |
| 937 |
.value_owned = 1, |
| 938 |
.header_value_type = AWS_EVENT_STREAM_HEADER_STRING, |
| 939 |
}; |
| 940 |
|
| 941 |
return s_add_variable_len_header( |
| 942 |
headers, &header, (const char *)name.ptr, (uint8_t)name.len, value.ptr, (uint16_t)value.len, 1); |
| 943 |
} |
| 944 |
|
| 945 |
int aws_event_stream_add_byte_buf_header_by_cursor( |
| 946 |
struct aws_array_list *headers, |
| 947 |
struct aws_byte_cursor name, |
| 948 |
struct aws_byte_cursor value) { |
| 949 |
|
| 950 |
AWS_FATAL_PRECONDITION(headers); |
| 951 |
AWS_EVENT_STREAM_VALIDATE_HEADER_NAME_CURSOR(name); |
| 952 |
AWS_RETURN_ERROR_IF( |
| 953 |
value.len <= AWS_EVENT_STREAM_HEADER_VALUE_LEN_MAX, AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN); |
| 954 |
|
| 955 |
struct aws_event_stream_header_value_pair header = { |
| 956 |
.header_name_len = (uint8_t)name.len, |
| 957 |
.header_value_len = (uint16_t)value.len, |
| 958 |
.value_owned = 1, |
| 959 |
.header_value_type = AWS_EVENT_STREAM_HEADER_BYTE_BUF, |
| 960 |
}; |
| 961 |
|
| 962 |
return s_add_variable_len_header( |
| 963 |
headers, &header, (const char *)name.ptr, (uint8_t)name.len, value.ptr, (uint16_t)value.len, 1); |
| 964 |
} |
| 965 |
|
| 966 |
int aws_event_stream_add_timestamp_header( |
| 967 |
struct aws_array_list *headers, |
| 968 |
const char *name, |
| 969 |
uint8_t name_len, |
| 970 |
int64_t value) { |
| 971 |
|
| 972 |
struct aws_byte_cursor name_cursor = aws_byte_cursor_from_array(name, (size_t)name_len); |
| 973 |
|
| 974 |
return aws_event_stream_add_timestamp_header_by_cursor(headers, name_cursor, value); |
| 975 |
} |
| 976 |
|
| 977 |
int aws_event_stream_add_timestamp_header_by_cursor( |
| 978 |
struct aws_array_list *headers, |
| 979 |
struct aws_byte_cursor name, |
| 980 |
int64_t value) { |
| 981 |
|
| 982 |
AWS_FATAL_PRECONDITION(headers); |
| 983 |
AWS_EVENT_STREAM_VALIDATE_HEADER_NAME_CURSOR(name); |
| 984 |
|
| 985 |
struct aws_event_stream_header_value_pair header = { |
| 986 |
.header_name_len = (uint8_t)name.len, |
| 987 |
.header_value_len = sizeof(value), |
| 988 |
.value_owned = 0, |
| 989 |
.header_value_type = AWS_EVENT_STREAM_HEADER_TIMESTAMP, |
| 990 |
}; |
| 991 |
|
| 992 |
aws_write_u64((uint64_t)value, header.header_value.static_val); |
| 993 |
|
| 994 |
memcpy((void *)header.header_name, (void *)name.ptr, (size_t)name.len); |
| 995 |
|
| 996 |
return aws_array_list_push_back(headers, (void *)&header); |
| 997 |
} |
| 998 |
|
| 999 |
int aws_event_stream_add_uuid_header( |
| 1000 |
struct aws_array_list *headers, |
| 1001 |
const char *name, |
| 1002 |
uint8_t name_len, |
| 1003 |
const uint8_t *value) { |
| 1004 |
|
| 1005 |
struct aws_byte_cursor name_cursor = aws_byte_cursor_from_array(name, (size_t)name_len); |
| 1006 |
struct aws_byte_cursor value_cursor = aws_byte_cursor_from_array(value, (size_t)UUID_LEN); |
| 1007 |
|
| 1008 |
return aws_event_stream_add_uuid_header_by_cursor(headers, name_cursor, value_cursor); |
| 1009 |
} |
| 1010 |
|
| 1011 |
int aws_event_stream_add_uuid_header_by_cursor( |
| 1012 |
struct aws_array_list *headers, |
| 1013 |
struct aws_byte_cursor name, |
| 1014 |
struct aws_byte_cursor value) { |
| 1015 |
|
| 1016 |
AWS_FATAL_PRECONDITION(headers); |
| 1017 |
AWS_EVENT_STREAM_VALIDATE_HEADER_NAME_CURSOR(name); |
| 1018 |
AWS_RETURN_ERROR_IF(value.len == UUID_LEN, AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN); |
| 1019 |
|
| 1020 |
struct aws_event_stream_header_value_pair header = { |
| 1021 |
.header_name_len = (uint8_t)name.len, |
| 1022 |
.header_value_len = UUID_LEN, |
| 1023 |
.value_owned = 0, |
| 1024 |
.header_value_type = AWS_EVENT_STREAM_HEADER_UUID, |
| 1025 |
}; |
| 1026 |
|
| 1027 |
memcpy((void *)header.header_name, (void *)name.ptr, (size_t)name.len); |
| 1028 |
memcpy((void *)header.header_value.static_val, value.ptr, UUID_LEN); |
| 1029 |
|
| 1030 |
return aws_array_list_push_back(headers, (void *)&header); |
| 1031 |
} |
| 1032 |
|
| 1033 |
int aws_event_stream_add_bytebuf_header( |
| 1034 |
struct aws_array_list *headers, |
| 1035 |
const char *name, |
| 1036 |
uint8_t name_len, |
| 1037 |
uint8_t *value, |
| 1038 |
uint16_t value_len, |
| 1039 |
int8_t copy) { |
| 1040 |
AWS_FATAL_PRECONDITION(headers); |
| 1041 |
AWS_FATAL_PRECONDITION(name); |
| 1042 |
AWS_RETURN_ERROR_IF( |
| 1043 |
value_len <= AWS_EVENT_STREAM_HEADER_VALUE_LEN_MAX, AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN); |
| 1044 |
|
| 1045 |
struct aws_event_stream_header_value_pair header = { |
| 1046 |
.header_name_len = name_len, |
| 1047 |
.header_value_len = value_len, |
| 1048 |
.value_owned = copy, |
| 1049 |
.header_value_type = AWS_EVENT_STREAM_HEADER_BYTE_BUF, |
| 1050 |
}; |
| 1051 |
|
| 1052 |
return s_add_variable_len_header(headers, &header, name, name_len, value, value_len, copy); |
| 1053 |
} |
| 1054 |
|
| 1055 |
int aws_event_stream_add_header( |
| 1056 |
struct aws_array_list *headers, |
| 1057 |
const struct aws_event_stream_header_value_pair *header) { |
| 1058 |
AWS_FATAL_PRECONDITION(headers); |
| 1059 |
AWS_FATAL_PRECONDITION(header); |
| 1060 |
|
| 1061 |
struct aws_event_stream_header_value_pair header_copy = *header; |
| 1062 |
|
| 1063 |
if (header->header_value_type == AWS_EVENT_STREAM_HEADER_STRING || |
| 1064 |
header->header_value_type == AWS_EVENT_STREAM_HEADER_BYTE_BUF) { |
| 1065 |
return s_add_variable_len_header( |
| 1066 |
headers, |
| 1067 |
&header_copy, |
| 1068 |
header->header_name, |
| 1069 |
header->header_name_len, |
| 1070 |
header->header_value.variable_len_val, |
| 1071 |
header->header_value_len, |
| 1072 |
1); |
| 1073 |
} |
| 1074 |
|
| 1075 |
return aws_array_list_push_back(headers, (void *)&header_copy); |
| 1076 |
} |
| 1077 |
|
| 1078 |
struct aws_byte_buf aws_event_stream_header_name(struct aws_event_stream_header_value_pair *header) { |
| 1079 |
AWS_FATAL_PRECONDITION(header); |
| 1080 |
|
| 1081 |
return aws_byte_buf_from_array((uint8_t *)header->header_name, header->header_name_len); |
| 1082 |
} |
| 1083 |
|
| 1084 |
int8_t aws_event_stream_header_value_as_byte(struct aws_event_stream_header_value_pair *header) { |
| 1085 |
AWS_FATAL_PRECONDITION(header); |
| 1086 |
|
| 1087 |
return (int8_t)header->header_value.static_val[0]; |
| 1088 |
} |
| 1089 |
|
| 1090 |
struct aws_byte_buf aws_event_stream_header_value_as_string(struct aws_event_stream_header_value_pair *header) { |
| 1091 |
AWS_FATAL_PRECONDITION(header); |
| 1092 |
|
| 1093 |
return aws_event_stream_header_value_as_bytebuf(header); |
| 1094 |
} |
| 1095 |
|
| 1096 |
int8_t aws_event_stream_header_value_as_bool(struct aws_event_stream_header_value_pair *header) { |
| 1097 |
AWS_FATAL_PRECONDITION(header); |
| 1098 |
|
| 1099 |
return header->header_value_type == AWS_EVENT_STREAM_HEADER_BOOL_TRUE ? (int8_t)1 : (int8_t)0; |
| 1100 |
} |
| 1101 |
|
| 1102 |
int16_t aws_event_stream_header_value_as_int16(struct aws_event_stream_header_value_pair *header) { |
| 1103 |
AWS_FATAL_PRECONDITION(header); |
| 1104 |
|
| 1105 |
return (int16_t)aws_read_u16(header->header_value.static_val); |
| 1106 |
} |
| 1107 |
|
| 1108 |
int32_t aws_event_stream_header_value_as_int32(struct aws_event_stream_header_value_pair *header) { |
| 1109 |
AWS_FATAL_PRECONDITION(header); |
| 1110 |
|
| 1111 |
return (int32_t)aws_read_u32(header->header_value.static_val); |
| 1112 |
} |
| 1113 |
|
| 1114 |
int64_t aws_event_stream_header_value_as_int64(struct aws_event_stream_header_value_pair *header) { |
| 1115 |
AWS_FATAL_PRECONDITION(header); |
| 1116 |
return (int64_t)aws_read_u64(header->header_value.static_val); |
| 1117 |
} |
| 1118 |
|
| 1119 |
struct aws_byte_buf aws_event_stream_header_value_as_bytebuf(struct aws_event_stream_header_value_pair *header) { |
| 1120 |
AWS_FATAL_PRECONDITION(header); |
| 1121 |
return aws_byte_buf_from_array(header->header_value.variable_len_val, header->header_value_len); |
| 1122 |
} |
| 1123 |
|
| 1124 |
int64_t aws_event_stream_header_value_as_timestamp(struct aws_event_stream_header_value_pair *header) { |
| 1125 |
AWS_FATAL_PRECONDITION(header); |
| 1126 |
return aws_event_stream_header_value_as_int64(header); |
| 1127 |
} |
| 1128 |
|
| 1129 |
struct aws_byte_buf aws_event_stream_header_value_as_uuid(struct aws_event_stream_header_value_pair *header) { |
| 1130 |
AWS_FATAL_PRECONDITION(header); |
| 1131 |
return aws_byte_buf_from_array(header->header_value.static_val, UUID_LEN); |
| 1132 |
} |
| 1133 |
|
| 1134 |
uint16_t aws_event_stream_header_value_length(struct aws_event_stream_header_value_pair *header) { |
| 1135 |
AWS_FATAL_PRECONDITION(header); |
| 1136 |
|
| 1137 |
return header->header_value_len; |
| 1138 |
} |
| 1139 |
|
| 1140 |
static struct aws_event_stream_message_prelude s_empty_prelude = {.total_len = 0, .headers_len = 0, .prelude_crc = 0}; |
| 1141 |
|
| 1142 |
static void s_reset_header_state(struct aws_event_stream_streaming_decoder *decoder, uint8_t free_header_data) { |
| 1143 |
|
| 1144 |
if (free_header_data && decoder->current_header.value_owned) { |
| 1145 |
aws_mem_release(decoder->alloc, (void *)decoder->current_header.header_value.variable_len_val); |
| 1146 |
} |
| 1147 |
|
| 1148 |
memset((void *)&decoder->current_header, 0, sizeof(struct aws_event_stream_header_value_pair)); |
| 1149 |
} |
| 1150 |
|
| 1151 |
static void s_reset_state(struct aws_event_stream_streaming_decoder *decoder); |
| 1152 |
|
| 1153 |
static int s_headers_state( |
| 1154 |
struct aws_event_stream_streaming_decoder *decoder, |
| 1155 |
const uint8_t *data, |
| 1156 |
size_t len, |
| 1157 |
size_t *processed); |
| 1158 |
|
| 1159 |
static int s_read_header_value( |
| 1160 |
struct aws_event_stream_streaming_decoder *decoder, |
| 1161 |
const uint8_t *data, |
| 1162 |
size_t len, |
| 1163 |
size_t *processed) { |
| 1164 |
|
| 1165 |
size_t current_pos = decoder->message_pos; |
| 1166 |
|
| 1167 |
|
| 1168 |
size_t length_read = current_pos - decoder->current_header_value_offset; |
| 1169 |
struct aws_event_stream_header_value_pair *current_header = &decoder->current_header; |
| 1170 |
|
| 1171 |
if (!length_read && (current_header->header_value_type == AWS_EVENT_STREAM_HEADER_BYTE_BUF || |
| 1172 |
current_header->header_value_type == AWS_EVENT_STREAM_HEADER_STRING)) { |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
if (len >= current_header->header_value_len) { |
| 1178 |
|
| 1179 |
current_header->header_value.variable_len_val = (uint8_t *)data; |
| 1180 |
current_header->value_owned = 0; |
| 1181 |
decoder->on_header(decoder, &decoder->prelude, &decoder->current_header, decoder->user_context); |
| 1182 |
*processed += current_header->header_value_len; |
| 1183 |
decoder->message_pos += current_header->header_value_len; |
| 1184 |
decoder->running_crc = |
| 1185 |
aws_checksums_crc32(data, (int)current_header->header_value_len, decoder->running_crc); |
| 1186 |
|
| 1187 |
s_reset_header_state(decoder, 1); |
| 1188 |
decoder->state = s_headers_state; |
| 1189 |
return AWS_OP_SUCCESS; |
| 1190 |
} |
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
current_header->header_value.variable_len_val = |
| 1195 |
aws_mem_acquire(decoder->alloc, decoder->current_header.header_value_len); |
| 1196 |
|
| 1197 |
current_header->value_owned = 1; |
| 1198 |
} |
| 1199 |
|
| 1200 |
size_t max_read = |
| 1201 |
len >= current_header->header_value_len - length_read ? current_header->header_value_len - length_read : len; |
| 1202 |
|
| 1203 |
const uint8_t *header_value_alias = current_header->header_value_type == AWS_EVENT_STREAM_HEADER_BYTE_BUF || |
| 1204 |
current_header->header_value_type == AWS_EVENT_STREAM_HEADER_STRING |
| 1205 |
? current_header->header_value.variable_len_val |
| 1206 |
: current_header->header_value.static_val; |
| 1207 |
|
| 1208 |
memcpy((void *)(header_value_alias + length_read), data, max_read); |
| 1209 |
decoder->running_crc = aws_checksums_crc32(data, (int)max_read, decoder->running_crc); |
| 1210 |
|
| 1211 |
*processed += max_read; |
| 1212 |
decoder->message_pos += max_read; |
| 1213 |
length_read += max_read; |
| 1214 |
|
| 1215 |
if (length_read == current_header->header_value_len) { |
| 1216 |
decoder->on_header(decoder, &decoder->prelude, current_header, decoder->user_context); |
| 1217 |
s_reset_header_state(decoder, 1); |
| 1218 |
decoder->state = s_headers_state; |
| 1219 |
} |
| 1220 |
|
| 1221 |
return AWS_OP_SUCCESS; |
| 1222 |
} |
| 1223 |
|
| 1224 |
static int s_read_header_value_len( |
| 1225 |
struct aws_event_stream_streaming_decoder *decoder, |
| 1226 |
const uint8_t *data, |
| 1227 |
size_t len, |
| 1228 |
size_t *processed) { |
| 1229 |
size_t current_pos = decoder->message_pos; |
| 1230 |
|
| 1231 |
size_t length_portion_read = current_pos - decoder->current_header_value_offset; |
| 1232 |
|
| 1233 |
if (length_portion_read < sizeof(uint16_t)) { |
| 1234 |
size_t max_to_read = |
| 1235 |
len > sizeof(uint16_t) - length_portion_read ? sizeof(uint16_t) - length_portion_read : len; |
| 1236 |
memcpy(decoder->working_buffer + length_portion_read, data, max_to_read); |
| 1237 |
decoder->running_crc = aws_checksums_crc32(data, (int)max_to_read, decoder->running_crc); |
| 1238 |
|
| 1239 |
*processed += max_to_read; |
| 1240 |
decoder->message_pos += max_to_read; |
| 1241 |
|
| 1242 |
length_portion_read = decoder->message_pos - decoder->current_header_value_offset; |
| 1243 |
} |
| 1244 |
|
| 1245 |
if (length_portion_read == sizeof(uint16_t)) { |
| 1246 |
decoder->current_header.header_value_len = aws_read_u16(decoder->working_buffer); |
| 1247 |
AWS_RETURN_ERROR_IF( |
| 1248 |
decoder->current_header.header_value_len <= AWS_EVENT_STREAM_HEADER_VALUE_LEN_MAX, |
| 1249 |
AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN); |
| 1250 |
decoder->current_header_value_offset = decoder->message_pos; |
| 1251 |
decoder->state = s_read_header_value; |
| 1252 |
} |
| 1253 |
|
| 1254 |
return AWS_OP_SUCCESS; |
| 1255 |
} |
| 1256 |
|
| 1257 |
static int s_read_header_type( |
| 1258 |
struct aws_event_stream_streaming_decoder *decoder, |
| 1259 |
const uint8_t *data, |
| 1260 |
size_t len, |
| 1261 |
size_t *processed) { |
| 1262 |
(void)len; |
| 1263 |
uint8_t type = *data; |
| 1264 |
decoder->running_crc = aws_checksums_crc32(data, 1, decoder->running_crc); |
| 1265 |
*processed += 1; |
| 1266 |
decoder->message_pos++; |
| 1267 |
decoder->current_header_value_offset++; |
| 1268 |
struct aws_event_stream_header_value_pair *current_header = &decoder->current_header; |
| 1269 |
|
| 1270 |
current_header->header_value_type = (enum aws_event_stream_header_value_type)type; |
| 1271 |
|
| 1272 |
switch (type) { |
| 1273 |
case AWS_EVENT_STREAM_HEADER_STRING: |
| 1274 |
case AWS_EVENT_STREAM_HEADER_BYTE_BUF: |
| 1275 |
decoder->state = s_read_header_value_len; |
| 1276 |
break; |
| 1277 |
case AWS_EVENT_STREAM_HEADER_BOOL_FALSE: |
| 1278 |
current_header->header_value_len = 0; |
| 1279 |
current_header->header_value.static_val[0] = 0; |
| 1280 |
decoder->on_header(decoder, &decoder->prelude, current_header, decoder->user_context); |
| 1281 |
s_reset_header_state(decoder, 1); |
| 1282 |
decoder->state = s_headers_state; |
| 1283 |
break; |
| 1284 |
case AWS_EVENT_STREAM_HEADER_BOOL_TRUE: |
| 1285 |
current_header->header_value_len = 0; |
| 1286 |
current_header->header_value.static_val[0] = 1; |
| 1287 |
decoder->on_header(decoder, &decoder->prelude, current_header, decoder->user_context); |
| 1288 |
s_reset_header_state(decoder, 1); |
| 1289 |
decoder->state = s_headers_state; |
| 1290 |
break; |
| 1291 |
case AWS_EVENT_STREAM_HEADER_BYTE: |
| 1292 |
current_header->header_value_len = 1; |
| 1293 |
decoder->state = s_read_header_value; |
| 1294 |
break; |
| 1295 |
case AWS_EVENT_STREAM_HEADER_INT16: |
| 1296 |
current_header->header_value_len = sizeof(uint16_t); |
| 1297 |
decoder->state = s_read_header_value; |
| 1298 |
break; |
| 1299 |
case AWS_EVENT_STREAM_HEADER_INT32: |
| 1300 |
current_header->header_value_len = sizeof(uint32_t); |
| 1301 |
decoder->state = s_read_header_value; |
| 1302 |
break; |
| 1303 |
case AWS_EVENT_STREAM_HEADER_INT64: |
| 1304 |
case AWS_EVENT_STREAM_HEADER_TIMESTAMP: |
| 1305 |
current_header->header_value_len = sizeof(uint64_t); |
| 1306 |
decoder->state = s_read_header_value; |
| 1307 |
break; |
| 1308 |
case AWS_EVENT_STREAM_HEADER_UUID: |
| 1309 |
current_header->header_value_len = 16; |
| 1310 |
decoder->state = s_read_header_value; |
| 1311 |
break; |
| 1312 |
default: |
| 1313 |
return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_UNKNOWN_HEADER_TYPE); |
| 1314 |
} |
| 1315 |
|
| 1316 |
return AWS_OP_SUCCESS; |
| 1317 |
} |
| 1318 |
|
| 1319 |
static int s_read_header_name( |
| 1320 |
struct aws_event_stream_streaming_decoder *decoder, |
| 1321 |
const uint8_t *data, |
| 1322 |
size_t len, |
| 1323 |
size_t *processed) { |
| 1324 |
size_t current_pos = decoder->message_pos; |
| 1325 |
|
| 1326 |
size_t length_read = current_pos - decoder->current_header_name_offset; |
| 1327 |
|
| 1328 |
size_t max_read = len >= decoder->current_header.header_name_len - length_read |
| 1329 |
? decoder->current_header.header_name_len - length_read |
| 1330 |
: len; |
| 1331 |
memcpy((void *)(decoder->current_header.header_name + length_read), data, max_read); |
| 1332 |
decoder->running_crc = aws_checksums_crc32(data, (int)max_read, decoder->running_crc); |
| 1333 |
|
| 1334 |
*processed += max_read; |
| 1335 |
decoder->message_pos += max_read; |
| 1336 |
length_read += max_read; |
| 1337 |
|
| 1338 |
if (length_read == decoder->current_header.header_name_len) { |
| 1339 |
decoder->state = s_read_header_type; |
| 1340 |
decoder->current_header_value_offset = decoder->message_pos; |
| 1341 |
} |
| 1342 |
|
| 1343 |
return AWS_OP_SUCCESS; |
| 1344 |
} |
| 1345 |
|
| 1346 |
static int s_read_header_name_len( |
| 1347 |
struct aws_event_stream_streaming_decoder *decoder, |
| 1348 |
const uint8_t *data, |
| 1349 |
size_t len, |
| 1350 |
size_t *processed) { |
| 1351 |
(void)len; |
| 1352 |
decoder->current_header.header_name_len = *data; |
| 1353 |
decoder->message_pos++; |
| 1354 |
decoder->current_header_name_offset++; |
| 1355 |
*processed += 1; |
| 1356 |
decoder->state = s_read_header_name; |
| 1357 |
decoder->running_crc = aws_checksums_crc32(data, 1, decoder->running_crc); |
| 1358 |
|
| 1359 |
return AWS_OP_SUCCESS; |
| 1360 |
} |
| 1361 |
|
| 1362 |
static int s_start_header( |
| 1363 |
struct aws_event_stream_streaming_decoder *decoder, |
| 1364 |
const uint8_t *data, |
| 1365 |
size_t len, |
| 1366 |
size_t *processed) { |
| 1367 |
(void)data; |
| 1368 |
(void)len; |
| 1369 |
(void)processed; |
| 1370 |
decoder->state = s_read_header_name_len; |
| 1371 |
decoder->current_header_name_offset = decoder->message_pos; |
| 1372 |
|
| 1373 |
return AWS_OP_SUCCESS; |
| 1374 |
} |
| 1375 |
|
| 1376 |
static int s_payload_state( |
| 1377 |
struct aws_event_stream_streaming_decoder *decoder, |
| 1378 |
const uint8_t *data, |
| 1379 |
size_t len, |
| 1380 |
size_t *processed); |
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
static int s_headers_state( |
| 1386 |
struct aws_event_stream_streaming_decoder *decoder, |
| 1387 |
const uint8_t *data, |
| 1388 |
size_t len, |
| 1389 |
size_t *processed) { |
| 1390 |
(void)data; |
| 1391 |
(void)len; |
| 1392 |
(void)processed; |
| 1393 |
|
| 1394 |
size_t current_pos = decoder->message_pos; |
| 1395 |
|
| 1396 |
size_t headers_boundary = decoder->prelude.headers_len + AWS_EVENT_STREAM_PRELUDE_LENGTH; |
| 1397 |
|
| 1398 |
if (current_pos < headers_boundary) { |
| 1399 |
decoder->state = s_start_header; |
| 1400 |
return AWS_OP_SUCCESS; |
| 1401 |
} |
| 1402 |
|
| 1403 |
if (current_pos == headers_boundary) { |
| 1404 |
decoder->state = s_payload_state; |
| 1405 |
return AWS_OP_SUCCESS; |
| 1406 |
} |
| 1407 |
|
| 1408 |
return aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_PARSER_ILLEGAL_STATE); |
| 1409 |
} |
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
static int s_read_trailer_state( |
| 1414 |
struct aws_event_stream_streaming_decoder *decoder, |
| 1415 |
const uint8_t *data, |
| 1416 |
size_t len, |
| 1417 |
size_t *processed) { |
| 1418 |
|
| 1419 |
size_t remaining_amount = decoder->prelude.total_len - decoder->message_pos; |
| 1420 |
size_t segment_length = len > remaining_amount ? remaining_amount : len; |
| 1421 |
size_t offset = sizeof(uint32_t) - remaining_amount; |
| 1422 |
memcpy(decoder->working_buffer + offset, data, segment_length); |
| 1423 |
decoder->message_pos += segment_length; |
| 1424 |
*processed += segment_length; |
| 1425 |
|
| 1426 |
if (decoder->message_pos == decoder->prelude.total_len) { |
| 1427 |
uint32_t message_crc = aws_read_u32(decoder->working_buffer); |
| 1428 |
|
| 1429 |
if (message_crc == decoder->running_crc) { |
| 1430 |
if (decoder->on_complete) { |
| 1431 |
decoder->on_complete(decoder, message_crc, decoder->user_context); |
| 1432 |
} |
| 1433 |
s_reset_state(decoder); |
| 1434 |
} else { |
| 1435 |
char error_message[70]; |
| 1436 |
snprintf( |
| 1437 |
error_message, |
| 1438 |
sizeof(error_message), |
| 1439 |
"CRC Mismatch. message_crc was 0x08%" PRIX32 ", but computed 0x08%" PRIX32, |
| 1440 |
message_crc, |
| 1441 |
decoder->running_crc); |
| 1442 |
aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_CHECKSUM_FAILURE); |
| 1443 |
decoder->on_error( |
| 1444 |
decoder, |
| 1445 |
&decoder->prelude, |
| 1446 |
AWS_ERROR_EVENT_STREAM_MESSAGE_CHECKSUM_FAILURE, |
| 1447 |
error_message, |
| 1448 |
decoder->user_context); |
| 1449 |
return AWS_OP_ERR; |
| 1450 |
} |
| 1451 |
} |
| 1452 |
|
| 1453 |
return AWS_OP_SUCCESS; |
| 1454 |
} |
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
static int s_payload_state( |
| 1459 |
struct aws_event_stream_streaming_decoder *decoder, |
| 1460 |
const uint8_t *data, |
| 1461 |
size_t len, |
| 1462 |
size_t *processed) { |
| 1463 |
|
| 1464 |
if (decoder->message_pos < decoder->prelude.total_len - AWS_EVENT_STREAM_TRAILER_LENGTH) { |
| 1465 |
size_t remaining_amount = decoder->prelude.total_len - decoder->message_pos - AWS_EVENT_STREAM_TRAILER_LENGTH; |
| 1466 |
size_t segment_length = len > remaining_amount ? remaining_amount : len; |
| 1467 |
int8_t final_segment = |
| 1468 |
(segment_length + decoder->message_pos) == (decoder->prelude.total_len - AWS_EVENT_STREAM_TRAILER_LENGTH); |
| 1469 |
struct aws_byte_buf payload_buf = aws_byte_buf_from_array(data, segment_length); |
| 1470 |
decoder->on_payload(decoder, &payload_buf, final_segment, decoder->user_context); |
| 1471 |
decoder->message_pos += segment_length; |
| 1472 |
decoder->running_crc = aws_checksums_crc32(data, (int)segment_length, decoder->running_crc); |
| 1473 |
*processed += segment_length; |
| 1474 |
} |
| 1475 |
|
| 1476 |
if (decoder->message_pos == decoder->prelude.total_len - AWS_EVENT_STREAM_TRAILER_LENGTH) { |
| 1477 |
decoder->state = s_read_trailer_state; |
| 1478 |
} |
| 1479 |
|
| 1480 |
return AWS_OP_SUCCESS; |
| 1481 |
} |
| 1482 |
|
| 1483 |
|
| 1484 |
static int s_verify_prelude_state( |
| 1485 |
struct aws_event_stream_streaming_decoder *decoder, |
| 1486 |
const uint8_t *data, |
| 1487 |
size_t len, |
| 1488 |
size_t *processed) { |
| 1489 |
(void)data; |
| 1490 |
(void)len; |
| 1491 |
(void)processed; |
| 1492 |
|
| 1493 |
decoder->prelude.headers_len = aws_read_u32(decoder->working_buffer + HEADER_LEN_OFFSET); |
| 1494 |
decoder->prelude.prelude_crc = aws_read_u32(decoder->working_buffer + PRELUDE_CRC_OFFSET); |
| 1495 |
decoder->prelude.total_len = aws_read_u32(decoder->working_buffer + TOTAL_LEN_OFFSET); |
| 1496 |
|
| 1497 |
decoder->running_crc = aws_checksums_crc32(decoder->working_buffer, PRELUDE_CRC_OFFSET, 0); |
| 1498 |
|
| 1499 |
if (AWS_LIKELY(decoder->running_crc == decoder->prelude.prelude_crc)) { |
| 1500 |
|
| 1501 |
if (AWS_UNLIKELY( |
| 1502 |
decoder->prelude.headers_len > AWS_EVENT_STREAM_MAX_HEADERS_SIZE || |
| 1503 |
decoder->prelude.total_len > AWS_EVENT_STREAM_MAX_MESSAGE_SIZE)) { |
| 1504 |
aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED); |
| 1505 |
char error_message[] = "Maximum message field size exceeded"; |
| 1506 |
|
| 1507 |
decoder->on_error( |
| 1508 |
decoder, |
| 1509 |
&decoder->prelude, |
| 1510 |
AWS_ERROR_EVENT_STREAM_MESSAGE_FIELD_SIZE_EXCEEDED, |
| 1511 |
error_message, |
| 1512 |
decoder->user_context); |
| 1513 |
return AWS_OP_ERR; |
| 1514 |
} |
| 1515 |
|
| 1516 |
if (decoder->prelude.headers_len > |
| 1517 |
decoder->prelude.total_len - AWS_EVENT_STREAM_PRELUDE_LENGTH - AWS_EVENT_STREAM_TRAILER_LENGTH) { |
| 1518 |
aws_raise_error(AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN); |
| 1519 |
char error_message[] = "Invalid message headers length"; |
| 1520 |
|
| 1521 |
decoder->on_error( |
| 1522 |
decoder, |
| 1523 |
&decoder->prelude, |
| 1524 |
AWS_ERROR_EVENT_STREAM_MESSAGE_INVALID_HEADERS_LEN, |
| 1525 |
error_message, |
| 1526 |
decoder->user_context); |
| 1527 |
|
| 1528 |
return AWS_OP_ERR; |
| 1529 |
} |
| 1530 |
|
| 1531 |
if (decoder->prelude.total_len < AWS_EVENT_STREAM_PRELUDE_LENGTH + AWS_EVENT_STREAM_TRAILER_LENGTH) { |
| 1532 |
aws_raise_error(AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH); |
| 1533 |
char error_message[] = "Message length too short"; |
| 1534 |
|
| 1535 |
decoder->on_error( |
| 1536 |
decoder, |
| 1537 |
&decoder->prelude, |
| 1538 |
AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH, |
| 1539 |
error_message, |
| 1540 |
decoder->user_context); |
| 1541 |
|
| 1542 |
return AWS_OP_ERR; |
| 1543 |
} |
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
decoder->on_prelude(decoder, &decoder->prelude, decoder->user_context); |
| 1548 |
|
| 1549 |
decoder->running_crc = aws_checksums_crc32( |
| 1550 |
decoder->working_buffer + PRELUDE_CRC_OFFSET, |
| 1551 |
(int)sizeof(decoder->prelude.prelude_crc), |
| 1552 |
decoder->running_crc); |
| 1553 |
memset(decoder->working_buffer, 0, sizeof(decoder->working_buffer)); |
| 1554 |
decoder->state = decoder->prelude.headers_len > 0 ? s_headers_state : s_payload_state; |
| 1555 |
} else { |
| 1556 |
char error_message[70]; |
| 1557 |
snprintf( |
| 1558 |
error_message, |
| 1559 |
sizeof(error_message), |
| 1560 |
"CRC Mismatch. prelude_crc was 0x08%" PRIX32 ", but computed 0x08%" PRIX32, |
| 1561 |
decoder->prelude.prelude_crc, |
| 1562 |
decoder->running_crc); |
| 1563 |
|
| 1564 |
aws_raise_error(AWS_ERROR_EVENT_STREAM_PRELUDE_CHECKSUM_FAILURE); |
| 1565 |
decoder->on_error( |
| 1566 |
decoder, |
| 1567 |
&decoder->prelude, |
| 1568 |
AWS_ERROR_EVENT_STREAM_PRELUDE_CHECKSUM_FAILURE, |
| 1569 |
error_message, |
| 1570 |
decoder->user_context); |
| 1571 |
return AWS_OP_ERR; |
| 1572 |
} |
| 1573 |
|
| 1574 |
return AWS_OP_SUCCESS; |
| 1575 |
} |
| 1576 |
|
| 1577 |
|
| 1578 |
static int s_start_state( |
| 1579 |
struct aws_event_stream_streaming_decoder *decoder, |
| 1580 |
const uint8_t *data, |
| 1581 |
size_t len, |
| 1582 |
size_t *processed) { |
| 1583 |
|
| 1584 |
size_t previous_position = decoder->message_pos; |
| 1585 |
if (decoder->message_pos < AWS_EVENT_STREAM_PRELUDE_LENGTH) { |
| 1586 |
if (len >= AWS_EVENT_STREAM_PRELUDE_LENGTH - decoder->message_pos) { |
| 1587 |
memcpy( |
| 1588 |
decoder->working_buffer + decoder->message_pos, |
| 1589 |
data, |
| 1590 |
AWS_EVENT_STREAM_PRELUDE_LENGTH - decoder->message_pos); |
| 1591 |
decoder->message_pos += AWS_EVENT_STREAM_PRELUDE_LENGTH - decoder->message_pos; |
| 1592 |
} else { |
| 1593 |
memcpy(decoder->working_buffer + decoder->message_pos, data, len); |
| 1594 |
decoder->message_pos += len; |
| 1595 |
} |
| 1596 |
|
| 1597 |
*processed += decoder->message_pos - previous_position; |
| 1598 |
} |
| 1599 |
|
| 1600 |
if (decoder->message_pos == AWS_EVENT_STREAM_PRELUDE_LENGTH) { |
| 1601 |
decoder->state = s_verify_prelude_state; |
| 1602 |
} |
| 1603 |
|
| 1604 |
return AWS_OP_SUCCESS; |
| 1605 |
} |
| 1606 |
|
| 1607 |
static void s_reset_state(struct aws_event_stream_streaming_decoder *decoder) { |
| 1608 |
memset(decoder->working_buffer, 0, sizeof(decoder->working_buffer)); |
| 1609 |
decoder->message_pos = 0; |
| 1610 |
decoder->running_crc = 0; |
| 1611 |
decoder->current_header_name_offset = 0; |
| 1612 |
decoder->current_header_value_offset = 0; |
| 1613 |
AWS_ZERO_STRUCT(decoder->current_header); |
| 1614 |
decoder->prelude = s_empty_prelude; |
| 1615 |
decoder->state = s_start_state; |
| 1616 |
} |
| 1617 |
|
| 1618 |
void aws_event_stream_streaming_decoder_init_from_options( |
| 1619 |
struct aws_event_stream_streaming_decoder *decoder, |
| 1620 |
struct aws_allocator *allocator, |
| 1621 |
const struct aws_event_stream_streaming_decoder_options *options) { |
| 1622 |
AWS_ASSERT(decoder); |
| 1623 |
AWS_ASSERT(allocator); |
| 1624 |
AWS_ASSERT(options); |
| 1625 |
AWS_ASSERT(options->on_error); |
| 1626 |
AWS_ASSERT(options->on_header); |
| 1627 |
AWS_ASSERT(options->on_payload_segment); |
| 1628 |
AWS_ASSERT(options->on_prelude); |
| 1629 |
AWS_ASSERT(options->on_prelude); |
| 1630 |
|
| 1631 |
s_reset_state(decoder); |
| 1632 |
decoder->alloc = allocator; |
| 1633 |
decoder->on_error = options->on_error; |
| 1634 |
decoder->on_header = options->on_header; |
| 1635 |
decoder->on_payload = options->on_payload_segment; |
| 1636 |
decoder->on_prelude = options->on_prelude; |
| 1637 |
decoder->on_complete = options->on_complete; |
| 1638 |
decoder->user_context = options->user_data; |
| 1639 |
} |
| 1640 |
|
| 1641 |
void aws_event_stream_streaming_decoder_init( |
| 1642 |
struct aws_event_stream_streaming_decoder *decoder, |
| 1643 |
struct aws_allocator *alloc, |
| 1644 |
aws_event_stream_process_on_payload_segment_fn *on_payload_segment, |
| 1645 |
aws_event_stream_prelude_received_fn *on_prelude, |
| 1646 |
aws_event_stream_header_received_fn *on_header, |
| 1647 |
aws_event_stream_on_error_fn *on_error, |
| 1648 |
void *user_data) { |
| 1649 |
|
| 1650 |
struct aws_event_stream_streaming_decoder_options decoder_options = { |
| 1651 |
.on_payload_segment = on_payload_segment, |
| 1652 |
.on_prelude = on_prelude, |
| 1653 |
.on_header = on_header, |
| 1654 |
.on_error = on_error, |
| 1655 |
.user_data = user_data, |
| 1656 |
}; |
| 1657 |
aws_event_stream_streaming_decoder_init_from_options(decoder, alloc, &decoder_options); |
| 1658 |
} |
| 1659 |
|
| 1660 |
void aws_event_stream_streaming_decoder_clean_up(struct aws_event_stream_streaming_decoder *decoder) { |
| 1661 |
s_reset_state(decoder); |
| 1662 |
decoder->on_error = 0; |
| 1663 |
decoder->on_header = 0; |
| 1664 |
decoder->on_payload = 0; |
| 1665 |
decoder->on_prelude = 0; |
| 1666 |
decoder->user_context = 0; |
| 1667 |
decoder->on_complete = 0; |
| 1668 |
} |
| 1669 |
|
| 1670 |
|
| 1671 |
int aws_event_stream_streaming_decoder_pump( |
| 1672 |
struct aws_event_stream_streaming_decoder *decoder, |
| 1673 |
const struct aws_byte_buf *data) { |
| 1674 |
|
| 1675 |
size_t processed = 0; |
| 1676 |
int err_val = 0; |
| 1677 |
while (!err_val && data->buffer && data->len && processed < data->len) { |
| 1678 |
err_val = decoder->state(decoder, data->buffer + processed, data->len - processed, &processed); |
| 1679 |
} |
| 1680 |
|
| 1681 |
return err_val; |
| 1682 |
} |
| 1683 |
#ifdef _MSC_VER |
| 1684 |
# pragma warning(pop) |
| 1685 |
#endif |