Skip to content

Commit ed627b7

Browse files
committed
read stream only if stream exists
1 parent e5350ed commit ed627b7

2 files changed

Lines changed: 34 additions & 35 deletions

File tree

source/h1_encoder.c

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -765,9 +765,7 @@ static int s_state_fn_head(struct aws_h1_encoder *encoder, struct aws_byte_buf *
765765
aws_byte_buf_clean_up(&encoder->message->outgoing_head_buf);
766766

767767
/* Pick next state */
768-
if (!encoder->message->has_chunked_encoding_header && encoder->message->has_manual_data_writes &&
769-
encoder->message->content_length) {
770-
/* Manual data writes with Content-Length */
768+
if (!encoder->message->has_chunked_encoding_header && encoder->message->has_manual_data_writes) {
771769
return s_switch_state(encoder, AWS_H1_ENCODER_STATE_DATA_WRITE_NEXT);
772770

773771
} else if (encoder->message->body && encoder->message->content_length) {
@@ -1055,19 +1053,24 @@ static int s_encoder_state_data_write_body(struct aws_h1_encoder *encoder, struc
10551053
/* Read from stream */
10561054
ENCODER_LOG(TRACE, encoder, "Reading from manual data write stream");
10571055
const size_t prev_len = dst->len;
1058-
int err = aws_input_stream_read(data_write->data, dst);
1059-
const size_t amount_read = dst->len - prev_len;
1056+
size_t amount_read = 0;
10601057
int error_code = AWS_OP_ERR;
10611058

1062-
if (err) {
1063-
ENCODER_LOGF(
1064-
ERROR,
1065-
encoder,
1066-
"Failed to read data write stream, error %d (%s)",
1067-
aws_last_error(),
1068-
aws_error_name(aws_last_error()));
1069-
error_code = aws_last_error();
1070-
goto error;
1059+
if (data_write->data) {
1060+
int err = aws_input_stream_read(data_write->data, dst);
1061+
amount_read = dst->len - prev_len;
1062+
int error_code = AWS_OP_ERR;
1063+
1064+
if (err) {
1065+
ENCODER_LOGF(
1066+
ERROR,
1067+
encoder,
1068+
"Failed to read data write stream, error %d (%s)",
1069+
aws_last_error(),
1070+
aws_error_name(aws_last_error()));
1071+
error_code = aws_last_error();
1072+
goto error;
1073+
}
10711074
}
10721075

10731076
/* Increment progress_bytes and check we haven't exceeded Content-Length */
@@ -1091,22 +1094,24 @@ static int s_encoder_state_data_write_body(struct aws_h1_encoder *encoder, struc
10911094
encoder->message->content_length);
10921095

10931096
/* If we read something or reached end of stream, check if stream is complete */
1094-
struct aws_stream_status status;
1095-
err = aws_input_stream_get_status(data_write->data, &status);
1096-
if (err) {
1097-
ENCODER_LOGF(
1098-
ERROR,
1099-
encoder,
1100-
"Failed to query data write stream status, error %d (%s)",
1101-
aws_last_error(),
1102-
aws_error_name(aws_last_error()));
1103-
error_code = aws_last_error();
1104-
goto error;
1105-
}
1097+
if (data_write->data) {
1098+
struct aws_stream_status status;
1099+
int err = aws_input_stream_get_status(data_write->data, &status);
1100+
if (err) {
1101+
ENCODER_LOGF(
1102+
ERROR,
1103+
encoder,
1104+
"Failed to query data write stream status, error %d (%s)",
1105+
aws_last_error(),
1106+
aws_error_name(aws_last_error()));
1107+
error_code = aws_last_error();
1108+
goto error;
1109+
}
11061110

1107-
if (!status.is_end_of_stream) {
1108-
/* Stream not done yet, remain in state */
1109-
return AWS_OP_SUCCESS;
1111+
if (!status.is_end_of_stream) {
1112+
/* Stream not done yet, remain in state */
1113+
return AWS_OP_SUCCESS;
1114+
}
11101115
}
11111116

11121117
/* This data write is complete */

source/h1_stream.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,6 @@ static int s_stream_write_data(
443443
.chunk_data_size = (uint64_t)data_len,
444444
};
445445

446-
/* If no termination chunk follows, put callback on this chunk */
447-
if (!options->end_stream) {
448-
chunk_opts.on_complete = options->on_complete;
449-
chunk_opts.user_data = options->user_data;
450-
}
451-
452446
if (aws_http1_stream_write_chunk(stream_base, &chunk_opts)) {
453447
AWS_LOGF_ERROR(
454448
AWS_LS_HTTP_STREAM,

0 commit comments

Comments
 (0)