Skip to content

Commit

Permalink
transfer encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
TingDaoK committed Dec 13, 2024
1 parent 4f400a8 commit 9569b69
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/aws/s3/private/s3_meta_request_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ struct aws_s3_meta_request {
void *impl;

struct aws_s3_meta_request_vtable *vtable;
struct aws_input_stream *stream;

/* Initial HTTP Message that this meta request is based on. */
struct aws_http_message *initial_request_message;
Expand Down
1 change: 1 addition & 0 deletions samples/s3/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ static void s_parse_app_ctx(int argc, char *const argv[], struct app_ctx *app_ct
client_config.client_bootstrap = app_ctx->client_bootstrap;
client_config.region = aws_byte_cursor_from_c_str(app_ctx->region);
client_config.signing_config = &app_ctx->signing_config;
client_config.tls_mode = AWS_MR_TLS_DISABLED;
app_ctx->client = aws_s3_client_new(app_ctx->allocator, &client_config);
}

Expand Down
10 changes: 10 additions & 0 deletions samples/s3/s3-cp.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,10 @@ static int s_kickoff_put_object(
.name = aws_byte_cursor_from_c_str("content-length"),
.value = aws_byte_cursor_from_c_str(content_length),
};
// struct aws_http_header transfer_encoding_header = {
// .name = aws_byte_cursor_from_c_str("transfer-encoding"),
// .value = aws_byte_cursor_from_c_str("chunked"),
// };

request_options.message = aws_http_message_new_request(cp_app_ctx->app_ctx->allocator);
aws_http_message_add_header(request_options.message, host_header);
Expand Down Expand Up @@ -708,10 +712,16 @@ static int s_kickoff_get_object(
.value = aws_byte_cursor_from_c_str("AWS common runtime command-line client"),
};

struct aws_http_header if_match_header = {
.name = aws_byte_cursor_from_c_str("if-match"),
.value = aws_byte_cursor_from_c_str("123"),
};

request_options.message = aws_http_message_new_request(cp_app_ctx->app_ctx->allocator);
aws_http_message_add_header(request_options.message, host_header);
aws_http_message_add_header(request_options.message, accept_header);
aws_http_message_add_header(request_options.message, user_agent_header);
aws_http_message_add_header(request_options.message, if_match_header);
aws_http_message_set_request_method(request_options.message, aws_http_method_get);

struct aws_byte_buf path_buf;
Expand Down
10 changes: 9 additions & 1 deletion source/s3_default_meta_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,15 @@ static void s_s3_default_prepare_request_finish(
struct aws_http_headers *headers = aws_http_message_get_headers(message);
aws_http_headers_set(headers, g_request_validation_mode, g_enabled);
}
aws_s3_message_util_assign_body(

struct aws_http_headers *headers = aws_http_message_get_headers(message);
struct aws_http_header transfer_encoding_header = {
.name = aws_byte_cursor_from_c_str("transfer-encoding"),
.value = aws_byte_cursor_from_c_str("chunked"),
};
aws_http_headers_erase(headers, aws_byte_cursor_from_c_str("content-length"));
aws_http_headers_add_header(headers, &transfer_encoding_header);
meta_request->stream = aws_s3_message_util_assign_body(
meta_request->allocator,
&request->request_body,
message,
Expand Down
22 changes: 22 additions & 0 deletions source/s3_meta_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,13 @@ static void s_s3_meta_request_request_on_signed(
s_s3_prepare_request_payload_callback_and_destroy(payload, error_code);
}

static void s_destroy_stream_on_complete(struct aws_http_stream *stream, int error_code, void *user_data) {
(void)stream;
(void)error_code;
struct aws_input_stream *data_stream = user_data;
aws_input_stream_release(data_stream);
}

void aws_s3_meta_request_send_request(struct aws_s3_meta_request *meta_request, struct aws_s3_connection *connection) {
AWS_PRECONDITION(meta_request);
AWS_PRECONDITION(connection);
Expand Down Expand Up @@ -1179,6 +1186,21 @@ void aws_s3_meta_request_send_request(struct aws_s3_meta_request *meta_request,
goto error_finish;
}
}
int64_t length = 0;
aws_input_stream_get_length(meta_request->stream, &length);
struct aws_http1_chunk_options chunk_options;
AWS_ZERO_STRUCT(chunk_options);
chunk_options.chunk_data = meta_request->stream;
chunk_options.chunk_data_size = length;
chunk_options.on_complete = s_destroy_stream_on_complete;
chunk_options.user_data = meta_request->stream;
aws_http1_stream_write_chunk(stream, &chunk_options);
static const struct aws_byte_cursor empty_str = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("");
struct aws_input_stream *termination_marker = aws_input_stream_new_from_cursor(meta_request->allocator, &empty_str);
chunk_options.chunk_data = termination_marker;
chunk_options.chunk_data_size = empty_str.len;
chunk_options.user_data = termination_marker;
aws_http1_stream_write_chunk(stream, &chunk_options);
return;

error_finish:
Expand Down
10 changes: 5 additions & 5 deletions source/s3_request_messages.c
Original file line number Diff line number Diff line change
Expand Up @@ -979,13 +979,13 @@ struct aws_input_stream *aws_s3_message_util_assign_body(
snprintf(content_length_buffer, sizeof(content_length_buffer), "%" PRIu64, (uint64_t)stream_length);
struct aws_byte_cursor content_length_cursor =
aws_byte_cursor_from_array(content_length_buffer, strlen(content_length_buffer));
if (aws_http_headers_set(headers, g_content_length_header_name, content_length_cursor)) {
goto error_clean_up;
}
// if (aws_http_headers_set(headers, g_content_length_header_name, content_length_cursor)) {
// goto error_clean_up;
// }

aws_http_message_set_body_stream(out_message, input_stream);
// aws_http_message_set_body_stream(out_message, input_stream);
/* Let the message take the full ownership */
aws_input_stream_release(input_stream);
// aws_input_stream_release(input_stream);
aws_byte_buf_clean_up(&content_encoding_header_buf);
return input_stream;

Expand Down

0 comments on commit 9569b69

Please sign in to comment.