| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "aws/s3/private/s3_checksum_context.h" |
| 7 |
#include "aws/s3/private/s3_checksums.h" |
| 8 |
#include <aws/common/encoding.h> |
| 9 |
#include <aws/common/string.h> |
| 10 |
#include <aws/io/stream.h> |
| 11 |
#include <inttypes.h> |
| 12 |
|
| 13 |
AWS_STATIC_STRING_FROM_LITERAL(s_carriage_return, "\r\n"); |
| 14 |
AWS_STATIC_STRING_FROM_LITERAL(s_empty_chunk, "0\r\n"); |
| 15 |
AWS_STATIC_STRING_FROM_LITERAL(s_final_chunk, "\r\n0\r\n"); |
| 16 |
AWS_STATIC_STRING_FROM_LITERAL(s_colon, ":"); |
| 17 |
AWS_STATIC_STRING_FROM_LITERAL(s_post_trailer, "\r\n\r\n"); |
| 18 |
|
| 19 |
struct aws_chunk_stream; |
| 20 |
|
| 21 |
typedef int(set_stream_fn)(struct aws_chunk_stream *parent_stream); |
| 22 |
|
| 23 |
struct aws_chunk_stream { |
| 24 |
struct aws_input_stream base; |
| 25 |
struct aws_allocator *allocator; |
| 26 |
|
| 27 |
|
| 28 |
struct aws_input_stream *current_stream; |
| 29 |
struct aws_input_stream *chunk_body_stream; |
| 30 |
|
| 31 |
struct aws_s3_upload_request_checksum_context *checksum_context; |
| 32 |
struct aws_byte_buf pre_chunk_buffer; |
| 33 |
struct aws_byte_buf post_chunk_buffer; |
| 34 |
struct aws_byte_cursor checksum_header_name; |
| 35 |
int64_t length; |
| 36 |
set_stream_fn *set_current_stream_fn; |
| 37 |
}; |
| 38 |
|
| 39 |
static int s_set_null_stream(struct aws_chunk_stream *parent_stream) { |
| 40 |
aws_input_stream_release(parent_stream->current_stream); |
| 41 |
parent_stream->current_stream = NULL; |
| 42 |
parent_stream->set_current_stream_fn = NULL; |
| 43 |
aws_byte_buf_clean_up(&parent_stream->post_chunk_buffer); |
| 44 |
return AWS_OP_SUCCESS; |
| 45 |
} |
| 46 |
|
| 47 |
static int s_set_post_chunk_stream(struct aws_chunk_stream *parent_stream) { |
| 48 |
int64_t current_stream_length; |
| 49 |
if (aws_input_stream_get_length(parent_stream->current_stream, ¤t_stream_length)) { |
| 50 |
aws_input_stream_release(parent_stream->current_stream); |
| 51 |
return AWS_OP_ERR; |
| 52 |
} |
| 53 |
aws_input_stream_release(parent_stream->current_stream); |
| 54 |
|
| 55 |
struct aws_byte_cursor final_chunk_cursor; |
| 56 |
|
| 57 |
if (current_stream_length > 0) { |
| 58 |
final_chunk_cursor = aws_byte_cursor_from_string(s_final_chunk); |
| 59 |
} else { |
| 60 |
final_chunk_cursor = aws_byte_cursor_from_string(s_empty_chunk); |
| 61 |
} |
| 62 |
struct aws_byte_cursor post_trailer_cursor = aws_byte_cursor_from_string(s_post_trailer); |
| 63 |
struct aws_byte_cursor colon_cursor = aws_byte_cursor_from_string(s_colon); |
| 64 |
struct aws_byte_cursor checksum_result_cursor = |
| 65 |
aws_s3_upload_request_checksum_context_get_checksum_cursor(parent_stream->checksum_context); |
| 66 |
AWS_ASSERT(parent_stream->checksum_context->encoded_checksum_size == checksum_result_cursor.len); |
| 67 |
|
| 68 |
if (aws_byte_buf_init( |
| 69 |
&parent_stream->post_chunk_buffer, |
| 70 |
parent_stream->allocator, |
| 71 |
final_chunk_cursor.len + parent_stream->checksum_header_name.len + colon_cursor.len + |
| 72 |
checksum_result_cursor.len + post_trailer_cursor.len)) { |
| 73 |
goto error; |
| 74 |
} |
| 75 |
if (aws_byte_buf_append(&parent_stream->post_chunk_buffer, &final_chunk_cursor) || |
| 76 |
aws_byte_buf_append(&parent_stream->post_chunk_buffer, &parent_stream->checksum_header_name) || |
| 77 |
aws_byte_buf_append(&parent_stream->post_chunk_buffer, &colon_cursor) || |
| 78 |
aws_byte_buf_append(&parent_stream->post_chunk_buffer, &checksum_result_cursor) || |
| 79 |
aws_byte_buf_append(&parent_stream->post_chunk_buffer, &post_trailer_cursor)) { |
| 80 |
goto error; |
| 81 |
} |
| 82 |
struct aws_byte_cursor post_chunk_cursor = aws_byte_cursor_from_buf(&parent_stream->post_chunk_buffer); |
| 83 |
parent_stream->current_stream = aws_input_stream_new_from_cursor(parent_stream->allocator, &post_chunk_cursor); |
| 84 |
parent_stream->set_current_stream_fn = s_set_null_stream; |
| 85 |
return AWS_OP_SUCCESS; |
| 86 |
error: |
| 87 |
aws_byte_buf_clean_up(&parent_stream->post_chunk_buffer); |
| 88 |
return AWS_OP_ERR; |
| 89 |
} |
| 90 |
|
| 91 |
static int s_set_chunk_stream(struct aws_chunk_stream *parent_stream) { |
| 92 |
aws_input_stream_release(parent_stream->current_stream); |
| 93 |
parent_stream->current_stream = parent_stream->chunk_body_stream; |
| 94 |
aws_byte_buf_clean_up(&parent_stream->pre_chunk_buffer); |
| 95 |
parent_stream->chunk_body_stream = NULL; |
| 96 |
parent_stream->set_current_stream_fn = s_set_post_chunk_stream; |
| 97 |
return AWS_OP_SUCCESS; |
| 98 |
} |
| 99 |
|
| 100 |
static int s_aws_input_chunk_stream_seek( |
| 101 |
struct aws_input_stream *stream, |
| 102 |
int64_t offset, |
| 103 |
enum aws_stream_seek_basis basis) { |
| 104 |
(void)stream; |
| 105 |
(void)offset; |
| 106 |
(void)basis; |
| 107 |
AWS_LOGF_ERROR( |
| 108 |
AWS_LS_S3_CLIENT, |
| 109 |
"Cannot seek on chunk stream, as it will cause the checksum output to mismatch the checksum of the stream" |
| 110 |
"contents"); |
| 111 |
AWS_ASSERT(false); |
| 112 |
return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION); |
| 113 |
} |
| 114 |
|
| 115 |
static int s_aws_input_chunk_stream_read(struct aws_input_stream *stream, struct aws_byte_buf *dest) { |
| 116 |
struct aws_chunk_stream *impl = AWS_CONTAINER_OF(stream, struct aws_chunk_stream, base); |
| 117 |
|
| 118 |
struct aws_stream_status status; |
| 119 |
AWS_ZERO_STRUCT(status); |
| 120 |
while (impl->current_stream != NULL && dest->len < dest->capacity) { |
| 121 |
int err = aws_input_stream_read(impl->current_stream, dest); |
| 122 |
if (err) { |
| 123 |
return err; |
| 124 |
} |
| 125 |
if (aws_input_stream_get_status(impl->current_stream, &status)) { |
| 126 |
return AWS_OP_ERR; |
| 127 |
} |
| 128 |
if (status.is_end_of_stream && impl->set_current_stream_fn(impl)) { |
| 129 |
return AWS_OP_ERR; |
| 130 |
} |
| 131 |
} |
| 132 |
return AWS_OP_SUCCESS; |
| 133 |
} |
| 134 |
|
| 135 |
static int s_aws_input_chunk_stream_get_status(struct aws_input_stream *stream, struct aws_stream_status *status) { |
| 136 |
struct aws_chunk_stream *impl = AWS_CONTAINER_OF(stream, struct aws_chunk_stream, base); |
| 137 |
|
| 138 |
if (impl->current_stream == NULL) { |
| 139 |
status->is_end_of_stream = true; |
| 140 |
status->is_valid = true; |
| 141 |
return AWS_OP_SUCCESS; |
| 142 |
} |
| 143 |
int res = aws_input_stream_get_status(impl->current_stream, status); |
| 144 |
if (res != AWS_OP_SUCCESS) { |
| 145 |
|
| 146 |
|
| 147 |
status->is_end_of_stream = false; |
| 148 |
} |
| 149 |
return res; |
| 150 |
} |
| 151 |
|
| 152 |
static int s_aws_input_chunk_stream_get_length(struct aws_input_stream *stream, int64_t *out_length) { |
| 153 |
struct aws_chunk_stream *impl = AWS_CONTAINER_OF(stream, struct aws_chunk_stream, base); |
| 154 |
*out_length = impl->length; |
| 155 |
return AWS_OP_SUCCESS; |
| 156 |
} |
| 157 |
|
| 158 |
static void s_aws_input_chunk_stream_destroy(struct aws_chunk_stream *impl) { |
| 159 |
if (impl) { |
| 160 |
aws_input_stream_release(impl->current_stream); |
| 161 |
aws_input_stream_release(impl->chunk_body_stream); |
| 162 |
aws_byte_buf_clean_up(&impl->pre_chunk_buffer); |
| 163 |
aws_byte_buf_clean_up(&impl->post_chunk_buffer); |
| 164 |
|
| 165 |
aws_s3_upload_request_checksum_context_release(impl->checksum_context); |
| 166 |
aws_mem_release(impl->allocator, impl); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
static struct aws_input_stream_vtable s_aws_input_chunk_stream_vtable = { |
| 171 |
.seek = s_aws_input_chunk_stream_seek, |
| 172 |
.read = s_aws_input_chunk_stream_read, |
| 173 |
.get_status = s_aws_input_chunk_stream_get_status, |
| 174 |
.get_length = s_aws_input_chunk_stream_get_length, |
| 175 |
}; |
| 176 |
|
| 177 |
struct aws_input_stream *aws_chunk_stream_new( |
| 178 |
struct aws_allocator *allocator, |
| 179 |
struct aws_input_stream *existing_stream, |
| 180 |
struct aws_s3_upload_request_checksum_context *checksum_context) { |
| 181 |
AWS_PRECONDITION(allocator); |
| 182 |
AWS_PRECONDITION(existing_stream); |
| 183 |
AWS_PRECONDITION(checksum_context); |
| 184 |
|
| 185 |
struct aws_chunk_stream *impl = aws_mem_calloc(allocator, 1, sizeof(struct aws_chunk_stream)); |
| 186 |
|
| 187 |
impl->allocator = allocator; |
| 188 |
impl->base.vtable = &s_aws_input_chunk_stream_vtable; |
| 189 |
|
| 190 |
|
| 191 |
enum aws_s3_checksum_algorithm algorithm = AWS_SCA_NONE; |
| 192 |
|
| 193 |
impl->checksum_context = aws_s3_upload_request_checksum_context_acquire(checksum_context); |
| 194 |
|
| 195 |
algorithm = checksum_context->algorithm; |
| 196 |
bool should_calculate_checksum = aws_s3_upload_request_checksum_context_should_calculate(impl->checksum_context); |
| 197 |
|
| 198 |
int64_t stream_length = 0; |
| 199 |
int64_t final_chunk_len = 0; |
| 200 |
if (aws_input_stream_get_length(existing_stream, &stream_length)) { |
| 201 |
goto error; |
| 202 |
} |
| 203 |
struct aws_byte_cursor pre_chunk_cursor = aws_byte_cursor_from_string(s_carriage_return); |
| 204 |
char stream_length_string[32]; |
| 205 |
AWS_ZERO_ARRAY(stream_length_string); |
| 206 |
snprintf(stream_length_string, AWS_ARRAY_SIZE(stream_length_string), "%" PRIX64, stream_length); |
| 207 |
struct aws_string *stream_length_aws_string = aws_string_new_from_c_str(allocator, stream_length_string); |
| 208 |
struct aws_byte_cursor stream_length_cursor = aws_byte_cursor_from_string(stream_length_aws_string); |
| 209 |
if (aws_byte_buf_init(&impl->pre_chunk_buffer, allocator, stream_length_cursor.len + pre_chunk_cursor.len)) { |
| 210 |
goto error; |
| 211 |
} |
| 212 |
if (aws_byte_buf_append(&impl->pre_chunk_buffer, &stream_length_cursor)) { |
| 213 |
goto error; |
| 214 |
} |
| 215 |
aws_string_destroy(stream_length_aws_string); |
| 216 |
if (aws_byte_buf_append(&impl->pre_chunk_buffer, &pre_chunk_cursor)) { |
| 217 |
goto error; |
| 218 |
} |
| 219 |
if (should_calculate_checksum) { |
| 220 |
|
| 221 |
impl->chunk_body_stream = |
| 222 |
aws_checksum_stream_new_with_context(allocator, existing_stream, impl->checksum_context); |
| 223 |
if (impl->chunk_body_stream == NULL) { |
| 224 |
goto error; |
| 225 |
} |
| 226 |
} else { |
| 227 |
|
| 228 |
impl->chunk_body_stream = aws_input_stream_acquire(existing_stream); |
| 229 |
} |
| 230 |
int64_t prechunk_stream_len = 0; |
| 231 |
int64_t colon_len = s_colon->len; |
| 232 |
int64_t post_trailer_len = s_post_trailer->len; |
| 233 |
|
| 234 |
struct aws_byte_cursor complete_pre_chunk_cursor = aws_byte_cursor_from_buf(&impl->pre_chunk_buffer); |
| 235 |
|
| 236 |
if (stream_length > 0) { |
| 237 |
impl->current_stream = aws_input_stream_new_from_cursor(allocator, &complete_pre_chunk_cursor); |
| 238 |
final_chunk_len = s_final_chunk->len; |
| 239 |
if (impl->current_stream == NULL) { |
| 240 |
goto error; |
| 241 |
} |
| 242 |
impl->set_current_stream_fn = s_set_chunk_stream; |
| 243 |
} else { |
| 244 |
impl->current_stream = impl->chunk_body_stream; |
| 245 |
final_chunk_len = s_empty_chunk->len; |
| 246 |
impl->chunk_body_stream = NULL; |
| 247 |
impl->set_current_stream_fn = s_set_post_chunk_stream; |
| 248 |
} |
| 249 |
|
| 250 |
impl->checksum_header_name = aws_get_http_header_name_from_checksum_algorithm(algorithm); |
| 251 |
|
| 252 |
if (aws_input_stream_get_length(impl->current_stream, &prechunk_stream_len)) { |
| 253 |
goto error; |
| 254 |
} |
| 255 |
|
| 256 |
impl->length = prechunk_stream_len + stream_length + final_chunk_len + impl->checksum_header_name.len + colon_len + |
| 257 |
checksum_context->encoded_checksum_size + post_trailer_len; |
| 258 |
|
| 259 |
AWS_ASSERT(impl->current_stream); |
| 260 |
aws_ref_count_init(&impl->base.ref_count, impl, (aws_simple_completion_callback *)s_aws_input_chunk_stream_destroy); |
| 261 |
return &impl->base; |
| 262 |
|
| 263 |
error: |
| 264 |
aws_input_stream_release(impl->chunk_body_stream); |
| 265 |
aws_input_stream_release(impl->current_stream); |
| 266 |
aws_byte_buf_clean_up(&impl->pre_chunk_buffer); |
| 267 |
aws_mem_release(impl->allocator, impl); |
| 268 |
return NULL; |
| 269 |
} |