| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/http/private/h1_stream.h> |
| 6 |
|
| 7 |
#include <aws/http/private/h1_connection.h> |
| 8 |
#include <aws/http/private/h1_encoder.h> |
| 9 |
|
| 10 |
#include <aws/http/status_code.h> |
| 11 |
#include <aws/io/logging.h> |
| 12 |
#include <aws/io/stream.h> |
| 13 |
|
| 14 |
#include <inttypes.h> |
| 15 |
|
| 16 |
static struct aws_h1_data_write *s_data_write_new( |
| 17 |
struct aws_allocator *allocator, |
| 18 |
const struct aws_http_stream_write_data_options *options) { |
| 19 |
struct aws_h1_data_write *data_write = aws_mem_calloc(allocator, 1, sizeof(struct aws_h1_data_write)); |
| 20 |
data_write->allocator = allocator; |
| 21 |
data_write->data = aws_input_stream_acquire(options->data); |
| 22 |
data_write->on_complete = options->on_complete; |
| 23 |
data_write->user_data = options->user_data; |
| 24 |
data_write->is_end_stream = options->end_stream; |
| 25 |
return data_write; |
| 26 |
} |
| 27 |
|
| 28 |
static void s_data_write_destroy(struct aws_h1_data_write *data_write) { |
| 29 |
aws_input_stream_release(data_write->data); |
| 30 |
aws_mem_release(data_write->allocator, data_write); |
| 31 |
} |
| 32 |
|
| 33 |
void aws_h1_data_write_complete_and_destroy( |
| 34 |
struct aws_h1_data_write *data_write, |
| 35 |
struct aws_http_stream *http_stream, |
| 36 |
int error_code) { |
| 37 |
AWS_PRECONDITION(data_write); |
| 38 |
aws_http_stream_write_complete_fn *on_complete = data_write->on_complete; |
| 39 |
void *user_data = data_write->user_data; |
| 40 |
s_data_write_destroy(data_write); |
| 41 |
if (on_complete) { |
| 42 |
on_complete(http_stream, error_code, user_data); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
static void s_stream_destroy(struct aws_http_stream *stream_base) { |
| 47 |
struct aws_h1_stream *stream = AWS_CONTAINER_OF(stream_base, struct aws_h1_stream, base); |
| 48 |
AWS_ASSERT( |
| 49 |
stream->synced_data.api_state != AWS_H1_STREAM_API_STATE_ACTIVE && |
| 50 |
"Stream should be complete (or never-activated) when stream destroyed"); |
| 51 |
AWS_ASSERT( |
| 52 |
aws_linked_list_empty(&stream->thread_data.pending_chunk_list) && |
| 53 |
aws_linked_list_empty(&stream->synced_data.pending_chunk_list) && |
| 54 |
"Chunks should be marked complete before stream destroyed"); |
| 55 |
AWS_ASSERT( |
| 56 |
aws_linked_list_empty(&stream->thread_data.pending_data_write_list) && |
| 57 |
aws_linked_list_empty(&stream->synced_data.pending_data_write_list) && |
| 58 |
"Data writes should be marked complete before stream destroyed"); |
| 59 |
|
| 60 |
aws_h1_encoder_message_clean_up(&stream->thread_data.encoder_message); |
| 61 |
aws_h1_encoder_message_clean_up(&stream->synced_data.pending_outgoing_response); |
| 62 |
aws_byte_buf_clean_up(&stream->thread_data.incoming_storage_buf); |
| 63 |
aws_mem_release(stream->base.alloc, stream); |
| 64 |
} |
| 65 |
|
| 66 |
static struct aws_h1_connection *s_get_h1_connection(const struct aws_h1_stream *stream) { |
| 67 |
return AWS_CONTAINER_OF(stream->base.owning_connection, struct aws_h1_connection, base); |
| 68 |
} |
| 69 |
|
| 70 |
static void s_stream_lock_synced_data(struct aws_h1_stream *stream) { |
| 71 |
aws_h1_connection_lock_synced_data(s_get_h1_connection(stream)); |
| 72 |
} |
| 73 |
|
| 74 |
static void s_stream_unlock_synced_data(struct aws_h1_stream *stream) { |
| 75 |
aws_h1_connection_unlock_synced_data(s_get_h1_connection(stream)); |
| 76 |
} |
| 77 |
|
| 78 |
static void s_stream_cross_thread_work_task(struct aws_channel_task *task, void *arg, enum aws_task_status status) { |
| 79 |
(void)task; |
| 80 |
struct aws_h1_stream *stream = arg; |
| 81 |
struct aws_h1_connection *connection = s_get_h1_connection(stream); |
| 82 |
|
| 83 |
if (status != AWS_TASK_STATUS_RUN_READY) { |
| 84 |
goto done; |
| 85 |
} |
| 86 |
|
| 87 |
AWS_LOGF_TRACE(AWS_LS_HTTP_STREAM, "id=%p: Running stream cross-thread work task.", (void *)&stream->base); |
| 88 |
|
| 89 |
|
| 90 |
s_stream_lock_synced_data(stream); |
| 91 |
|
| 92 |
stream->synced_data.is_cross_thread_work_task_scheduled = false; |
| 93 |
|
| 94 |
int api_state = stream->synced_data.api_state; |
| 95 |
|
| 96 |
|
| 97 |
bool new_outgoing_data = !aws_linked_list_empty(&stream->synced_data.pending_chunk_list) || |
| 98 |
!aws_linked_list_empty(&stream->synced_data.pending_data_write_list); |
| 99 |
aws_linked_list_move_all_back(&stream->thread_data.pending_chunk_list, &stream->synced_data.pending_chunk_list); |
| 100 |
aws_linked_list_move_all_back( |
| 101 |
&stream->thread_data.pending_data_write_list, &stream->synced_data.pending_data_write_list); |
| 102 |
|
| 103 |
|
| 104 |
if (stream->synced_data.has_outgoing_response && !stream->thread_data.has_outgoing_response) { |
| 105 |
stream->thread_data.has_outgoing_response = true; |
| 106 |
new_outgoing_data = true; |
| 107 |
|
| 108 |
stream->thread_data.encoder_message = stream->synced_data.pending_outgoing_response; |
| 109 |
AWS_ZERO_STRUCT(stream->synced_data.pending_outgoing_response); |
| 110 |
|
| 111 |
if (stream->thread_data.encoder_message.has_connection_close_header) { |
| 112 |
|
| 113 |
stream->thread_data.is_final_stream = true; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
stream->thread_data.encoder_message.trailer = stream->synced_data.pending_trailer; |
| 118 |
stream->synced_data.pending_trailer = NULL; |
| 119 |
|
| 120 |
uint64_t pending_window_update = stream->synced_data.pending_window_update; |
| 121 |
stream->synced_data.pending_window_update = 0; |
| 122 |
|
| 123 |
s_stream_unlock_synced_data(stream); |
| 124 |
|
| 125 |
|
| 126 |
if (new_outgoing_data && (api_state == AWS_H1_STREAM_API_STATE_ACTIVE)) { |
| 127 |
aws_h1_connection_try_write_outgoing_stream(connection); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
stream->thread_data.stream_window = |
| 133 |
aws_add_u64_saturating(stream->thread_data.stream_window, pending_window_update); |
| 134 |
if ((pending_window_update > 0) && (api_state == AWS_H1_STREAM_API_STATE_ACTIVE)) { |
| 135 |
|
| 136 |
|
| 137 |
aws_h1_connection_try_process_read_messages(connection); |
| 138 |
} |
| 139 |
|
| 140 |
done: |
| 141 |
|
| 142 |
aws_http_stream_release(&stream->base); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
static void s_stream_update_window(struct aws_http_stream *stream_base, size_t increment_size) { |
| 147 |
if (increment_size == 0) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
if (!stream_base->owning_connection->stream_manual_window_management) { |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
struct aws_h1_stream *stream = AWS_CONTAINER_OF(stream_base, struct aws_h1_stream, base); |
| 156 |
bool should_schedule_task = false; |
| 157 |
|
| 158 |
{ |
| 159 |
s_stream_lock_synced_data(stream); |
| 160 |
|
| 161 |
|
| 162 |
stream->synced_data.pending_window_update = |
| 163 |
aws_add_u64_saturating(stream->synced_data.pending_window_update, increment_size); |
| 164 |
|
| 165 |
|
| 166 |
if (stream->synced_data.api_state == AWS_H1_STREAM_API_STATE_ACTIVE) { |
| 167 |
if (!stream->synced_data.is_cross_thread_work_task_scheduled) { |
| 168 |
stream->synced_data.is_cross_thread_work_task_scheduled = true; |
| 169 |
should_schedule_task = true; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
s_stream_unlock_synced_data(stream); |
| 174 |
} |
| 175 |
|
| 176 |
if (should_schedule_task) { |
| 177 |
|
| 178 |
aws_atomic_fetch_add(&stream->base.refcount, 1); |
| 179 |
AWS_LOGF_TRACE(AWS_LS_HTTP_STREAM, "id=%p: Scheduling stream cross-thread work task.", (void *)stream_base); |
| 180 |
aws_channel_schedule_task_now( |
| 181 |
stream->base.owning_connection->channel_slot->channel, &stream->cross_thread_work_task); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
static int s_stream_write_chunk(struct aws_http_stream *stream_base, const struct aws_http1_chunk_options *options) { |
| 186 |
AWS_PRECONDITION(stream_base); |
| 187 |
AWS_PRECONDITION(options); |
| 188 |
struct aws_h1_stream *stream = AWS_CONTAINER_OF(stream_base, struct aws_h1_stream, base); |
| 189 |
|
| 190 |
if (options->chunk_data == NULL && options->chunk_data_size > 0) { |
| 191 |
AWS_LOGF_ERROR( |
| 192 |
AWS_LS_HTTP_STREAM, "id=%p: Chunk data cannot be NULL if data size is non-zero", (void *)stream_base); |
| 193 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 194 |
} |
| 195 |
|
| 196 |
struct aws_h1_chunk *chunk = aws_h1_chunk_new(stream_base->alloc, options); |
| 197 |
if (AWS_UNLIKELY(NULL == chunk)) { |
| 198 |
AWS_LOGF_ERROR( |
| 199 |
AWS_LS_HTTP_STREAM, |
| 200 |
"id=%p: Failed to initialize streamed chunk, error %d (%s).", |
| 201 |
(void *)stream_base, |
| 202 |
aws_last_error(), |
| 203 |
aws_error_name(aws_last_error())); |
| 204 |
return AWS_OP_ERR; |
| 205 |
} |
| 206 |
|
| 207 |
int error_code = 0; |
| 208 |
bool should_schedule_task = false; |
| 209 |
|
| 210 |
{ |
| 211 |
s_stream_lock_synced_data(stream); |
| 212 |
|
| 213 |
|
| 214 |
if (stream->synced_data.api_state != AWS_H1_STREAM_API_STATE_ACTIVE) { |
| 215 |
error_code = (stream->synced_data.api_state == AWS_H1_STREAM_API_STATE_INIT) |
| 216 |
? AWS_ERROR_HTTP_STREAM_NOT_ACTIVATED |
| 217 |
: AWS_ERROR_HTTP_STREAM_HAS_COMPLETED; |
| 218 |
goto unlock; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
if (!stream->synced_data.using_chunked_encoding) { |
| 224 |
AWS_LOGF_ERROR( |
| 225 |
AWS_LS_HTTP_STREAM, |
| 226 |
"id=%p: Cannot write chunks without 'transfer-encoding: chunked' header.", |
| 227 |
(void *)stream_base); |
| 228 |
error_code = AWS_ERROR_INVALID_STATE; |
| 229 |
goto unlock; |
| 230 |
} |
| 231 |
|
| 232 |
if (stream->synced_data.has_final_chunk) { |
| 233 |
AWS_LOGF_ERROR( |
| 234 |
AWS_LS_HTTP_STREAM, "id=%p: Cannot write additional chunk after final chunk.", (void *)stream_base); |
| 235 |
error_code = AWS_ERROR_INVALID_STATE; |
| 236 |
goto unlock; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
if (chunk->data_size == 0) { |
| 241 |
stream->synced_data.has_final_chunk = true; |
| 242 |
} |
| 243 |
aws_linked_list_push_back(&stream->synced_data.pending_chunk_list, &chunk->node); |
| 244 |
should_schedule_task = !stream->synced_data.is_cross_thread_work_task_scheduled; |
| 245 |
stream->synced_data.is_cross_thread_work_task_scheduled = true; |
| 246 |
|
| 247 |
unlock: |
| 248 |
s_stream_unlock_synced_data(stream); |
| 249 |
} |
| 250 |
|
| 251 |
if (error_code) { |
| 252 |
AWS_LOGF_ERROR( |
| 253 |
AWS_LS_HTTP_STREAM, |
| 254 |
"id=%p: Failed to add chunk, error %d (%s)", |
| 255 |
(void *)stream_base, |
| 256 |
error_code, |
| 257 |
aws_error_name(error_code)); |
| 258 |
|
| 259 |
aws_h1_chunk_destroy(chunk); |
| 260 |
return aws_raise_error(error_code); |
| 261 |
} |
| 262 |
|
| 263 |
AWS_LOGF_TRACE( |
| 264 |
AWS_LS_HTTP_STREAM, |
| 265 |
"id=%p: Adding chunk with size %" PRIu64 " to stream", |
| 266 |
(void *)stream, |
| 267 |
options->chunk_data_size); |
| 268 |
|
| 269 |
if (should_schedule_task) { |
| 270 |
|
| 271 |
aws_atomic_fetch_add(&stream->base.refcount, 1); |
| 272 |
AWS_LOGF_TRACE(AWS_LS_HTTP_STREAM, "id=%p: Scheduling stream cross-thread work task.", (void *)stream_base); |
| 273 |
aws_channel_schedule_task_now( |
| 274 |
stream->base.owning_connection->channel_slot->channel, &stream->cross_thread_work_task); |
| 275 |
} else { |
| 276 |
AWS_LOGF_TRACE( |
| 277 |
AWS_LS_HTTP_STREAM, "id=%p: Stream cross-thread work task was already scheduled.", (void *)stream_base); |
| 278 |
} |
| 279 |
|
| 280 |
return AWS_OP_SUCCESS; |
| 281 |
} |
| 282 |
|
| 283 |
static int s_stream_add_trailer(struct aws_http_stream *stream_base, const struct aws_http_headers *trailing_headers) { |
| 284 |
AWS_PRECONDITION(stream_base); |
| 285 |
AWS_PRECONDITION(trailing_headers); |
| 286 |
struct aws_h1_stream *stream = AWS_CONTAINER_OF(stream_base, struct aws_h1_stream, base); |
| 287 |
|
| 288 |
struct aws_h1_trailer *trailer = aws_h1_trailer_new(stream_base->alloc, trailing_headers); |
| 289 |
if (AWS_UNLIKELY(NULL == trailer)) { |
| 290 |
AWS_LOGF_ERROR( |
| 291 |
AWS_LS_HTTP_STREAM, |
| 292 |
"id=%p: Failed to initialize streamed trailer, error %d (%s).", |
| 293 |
(void *)stream_base, |
| 294 |
aws_last_error(), |
| 295 |
aws_error_name(aws_last_error())); |
| 296 |
return AWS_OP_ERR; |
| 297 |
} |
| 298 |
|
| 299 |
int error_code = 0; |
| 300 |
bool should_schedule_task = false; |
| 301 |
|
| 302 |
{ |
| 303 |
s_stream_lock_synced_data(stream); |
| 304 |
|
| 305 |
if (stream->synced_data.api_state != AWS_H1_STREAM_API_STATE_ACTIVE) { |
| 306 |
error_code = (stream->synced_data.api_state == AWS_H1_STREAM_API_STATE_INIT) |
| 307 |
? AWS_ERROR_HTTP_STREAM_NOT_ACTIVATED |
| 308 |
: AWS_ERROR_HTTP_STREAM_HAS_COMPLETED; |
| 309 |
goto unlock; |
| 310 |
} |
| 311 |
|
| 312 |
if (!stream->synced_data.using_chunked_encoding) { |
| 313 |
AWS_LOGF_ERROR( |
| 314 |
AWS_LS_HTTP_STREAM, |
| 315 |
"id=%p: Cannot write trailers without 'transfer-encoding: chunked' header.", |
| 316 |
(void *)stream_base); |
| 317 |
error_code = AWS_ERROR_INVALID_STATE; |
| 318 |
goto unlock; |
| 319 |
} |
| 320 |
|
| 321 |
if (stream->synced_data.has_added_trailer) { |
| 322 |
AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Cannot write trailers twice.", (void *)stream_base); |
| 323 |
error_code = AWS_ERROR_INVALID_STATE; |
| 324 |
goto unlock; |
| 325 |
} |
| 326 |
|
| 327 |
if (stream->synced_data.has_final_chunk) { |
| 328 |
AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Cannot write trailers after final chunk.", (void *)stream_base); |
| 329 |
error_code = AWS_ERROR_INVALID_STATE; |
| 330 |
goto unlock; |
| 331 |
} |
| 332 |
|
| 333 |
stream->synced_data.has_added_trailer = true; |
| 334 |
stream->synced_data.pending_trailer = trailer; |
| 335 |
should_schedule_task = !stream->synced_data.is_cross_thread_work_task_scheduled; |
| 336 |
stream->synced_data.is_cross_thread_work_task_scheduled = true; |
| 337 |
|
| 338 |
unlock: |
| 339 |
s_stream_unlock_synced_data(stream); |
| 340 |
} |
| 341 |
|
| 342 |
if (error_code) { |
| 343 |
AWS_LOGF_ERROR( |
| 344 |
AWS_LS_HTTP_STREAM, |
| 345 |
"id=%p: Failed to add trailer, error %d (%s)", |
| 346 |
(void *)stream_base, |
| 347 |
error_code, |
| 348 |
aws_error_name(error_code)); |
| 349 |
|
| 350 |
aws_h1_trailer_destroy(trailer); |
| 351 |
return aws_raise_error(error_code); |
| 352 |
} |
| 353 |
|
| 354 |
AWS_LOGF_TRACE(AWS_LS_HTTP_STREAM, "id=%p: Adding trailer to stream", (void *)stream); |
| 355 |
|
| 356 |
if (should_schedule_task) { |
| 357 |
|
| 358 |
aws_atomic_fetch_add(&stream->base.refcount, 1); |
| 359 |
AWS_LOGF_TRACE(AWS_LS_HTTP_STREAM, "id=%p: Scheduling stream cross-thread work task.", (void *)stream_base); |
| 360 |
aws_channel_schedule_task_now( |
| 361 |
stream->base.owning_connection->channel_slot->channel, &stream->cross_thread_work_task); |
| 362 |
} else { |
| 363 |
AWS_LOGF_TRACE( |
| 364 |
AWS_LS_HTTP_STREAM, "id=%p: Stream cross-thread work task was already scheduled.", (void *)stream_base); |
| 365 |
} |
| 366 |
|
| 367 |
return AWS_OP_SUCCESS; |
| 368 |
} |
| 369 |
|
| 370 |
static int s_stream_write_data( |
| 371 |
struct aws_http_stream *stream_base, |
| 372 |
const struct aws_http_stream_write_data_options *options) { |
| 373 |
AWS_PRECONDITION(stream_base); |
| 374 |
AWS_PRECONDITION(options); |
| 375 |
struct aws_h1_stream *stream = AWS_CONTAINER_OF(stream_base, struct aws_h1_stream, base); |
| 376 |
|
| 377 |
|
| 378 |
if (!options->data && !options->end_stream) { |
| 379 |
if (options->on_complete) { |
| 380 |
options->on_complete(stream_base, AWS_ERROR_SUCCESS, options->user_data); |
| 381 |
} |
| 382 |
return AWS_OP_SUCCESS; |
| 383 |
} |
| 384 |
|
| 385 |
if (!stream->using_manual_data_writes) { |
| 386 |
AWS_LOGF_ERROR( |
| 387 |
AWS_LS_HTTP_STREAM, |
| 388 |
"id=%p: Manual writes not enabled. Set 'use_manual_data_writes' in aws_http_make_request_options.", |
| 389 |
(void *)stream_base); |
| 390 |
return aws_raise_error(AWS_ERROR_HTTP_MANUAL_WRITE_NOT_ENABLED); |
| 391 |
} |
| 392 |
|
| 393 |
bool should_schedule_task = false; |
| 394 |
bool is_chunked = false; |
| 395 |
int error_code = AWS_ERROR_SUCCESS; |
| 396 |
|
| 397 |
{ |
| 398 |
s_stream_lock_synced_data(stream); |
| 399 |
|
| 400 |
if (stream->synced_data.api_state != AWS_H1_STREAM_API_STATE_ACTIVE) { |
| 401 |
error_code = (stream->synced_data.api_state == AWS_H1_STREAM_API_STATE_INIT) |
| 402 |
? AWS_ERROR_HTTP_STREAM_NOT_ACTIVATED |
| 403 |
: AWS_ERROR_HTTP_STREAM_HAS_COMPLETED; |
| 404 |
goto unlock; |
| 405 |
} |
| 406 |
|
| 407 |
if (stream->synced_data.has_final_data_write) { |
| 408 |
AWS_LOGF_ERROR( |
| 409 |
AWS_LS_HTTP_STREAM, |
| 410 |
"id=%p: Cannot write data after final write (end_stream=true).", |
| 411 |
(void *)stream_base); |
| 412 |
error_code = AWS_ERROR_HTTP_MANUAL_WRITE_HAS_COMPLETED; |
| 413 |
goto unlock; |
| 414 |
} |
| 415 |
|
| 416 |
is_chunked = stream->synced_data.using_chunked_encoding; |
| 417 |
|
| 418 |
if (!is_chunked) { |
| 419 |
struct aws_h1_data_write *data_write = s_data_write_new(stream_base->alloc, options); |
| 420 |
|
| 421 |
aws_linked_list_push_back(&stream->synced_data.pending_data_write_list, &data_write->node); |
| 422 |
should_schedule_task = !stream->synced_data.is_cross_thread_work_task_scheduled; |
| 423 |
stream->synced_data.is_cross_thread_work_task_scheduled = true; |
| 424 |
} |
| 425 |
|
| 426 |
stream->synced_data.has_final_data_write = options->end_stream; |
| 427 |
|
| 428 |
} |
| 429 |
|
| 430 |
unlock: |
| 431 |
s_stream_unlock_synced_data(stream); |
| 432 |
|
| 433 |
if (error_code != AWS_ERROR_SUCCESS) { |
| 434 |
if (error_code == AWS_ERROR_HTTP_OUTGOING_STREAM_LENGTH_INCORRECT) { |
| 435 |
aws_h1_stream_cancel(stream_base, AWS_ERROR_HTTP_OUTGOING_STREAM_LENGTH_INCORRECT); |
| 436 |
return AWS_OP_SUCCESS; |
| 437 |
} |
| 438 |
AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Could not complete write data successfully.", (void *)stream_base); |
| 439 |
return aws_raise_error(error_code); |
| 440 |
} |
| 441 |
|
| 442 |
if (is_chunked) { |
| 443 |
|
| 444 |
if (options->data) { |
| 445 |
int64_t data_len = 0; |
| 446 |
if (aws_input_stream_get_length(options->data, &data_len)) { |
| 447 |
AWS_LOGF_ERROR( |
| 448 |
AWS_LS_HTTP_STREAM, |
| 449 |
"id=%p: Failed to get data stream length for chunked conversion", |
| 450 |
(void *)stream_base); |
| 451 |
return AWS_OP_ERR; |
| 452 |
} |
| 453 |
|
| 454 |
struct aws_http1_chunk_options chunk_opts = { |
| 455 |
.chunk_data = options->data, |
| 456 |
.chunk_data_size = (uint64_t)data_len, |
| 457 |
.on_complete = options->end_stream ? NULL : options->on_complete, |
| 458 |
.user_data = options->end_stream ? NULL : options->user_data, |
| 459 |
}; |
| 460 |
|
| 461 |
if (aws_http1_stream_write_chunk(stream_base, &chunk_opts)) { |
| 462 |
AWS_LOGF_ERROR( |
| 463 |
AWS_LS_HTTP_STREAM, |
| 464 |
"id=%p: Failed to write chunk to stream, error %d (%s).", |
| 465 |
(void *)stream_base, |
| 466 |
aws_last_error(), |
| 467 |
aws_error_name(aws_last_error())); |
| 468 |
return AWS_OP_ERR; |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
if (options->end_stream) { |
| 474 |
struct aws_http1_chunk_options termination_opts; |
| 475 |
AWS_ZERO_STRUCT(termination_opts); |
| 476 |
termination_opts.on_complete = options->on_complete; |
| 477 |
termination_opts.user_data = options->user_data; |
| 478 |
if (aws_http1_stream_write_chunk(stream_base, &termination_opts)) { |
| 479 |
AWS_LOGF_ERROR( |
| 480 |
AWS_LS_HTTP_STREAM, |
| 481 |
"id=%p: Failed to write termination chunk, error %d (%s).", |
| 482 |
(void *)stream_base, |
| 483 |
aws_last_error(), |
| 484 |
aws_error_name(aws_last_error())); |
| 485 |
return AWS_OP_ERR; |
| 486 |
} |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
if (should_schedule_task) { |
| 491 |
aws_atomic_fetch_add(&stream->base.refcount, 1); |
| 492 |
AWS_LOGF_TRACE(AWS_LS_HTTP_STREAM, "id=%p: Scheduling stream cross-thread work task.", (void *)stream_base); |
| 493 |
aws_channel_schedule_task_now( |
| 494 |
stream->base.owning_connection->channel_slot->channel, &stream->cross_thread_work_task); |
| 495 |
} |
| 496 |
|
| 497 |
return AWS_OP_SUCCESS; |
| 498 |
} |
| 499 |
|
| 500 |
static const struct aws_http_stream_vtable s_stream_vtable = { |
| 501 |
.destroy = s_stream_destroy, |
| 502 |
.update_window = s_stream_update_window, |
| 503 |
.activate = aws_h1_stream_activate, |
| 504 |
.cancel = aws_h1_stream_cancel, |
| 505 |
.http1_write_chunk = s_stream_write_chunk, |
| 506 |
.http1_add_trailer = s_stream_add_trailer, |
| 507 |
.http2_reset_stream = NULL, |
| 508 |
.http2_get_received_error_code = NULL, |
| 509 |
.http2_get_sent_error_code = NULL, |
| 510 |
.write_data = s_stream_write_data, |
| 511 |
}; |
| 512 |
|
| 513 |
static struct aws_h1_stream *s_stream_new_common( |
| 514 |
struct aws_http_connection *connection_base, |
| 515 |
void *user_data, |
| 516 |
aws_http_on_incoming_headers_fn *on_incoming_headers, |
| 517 |
aws_http_on_incoming_header_block_done_fn *on_incoming_header_block_done, |
| 518 |
aws_http_on_incoming_body_fn *on_incoming_body, |
| 519 |
aws_http_on_stream_complete_fn *on_complete, |
| 520 |
aws_http_on_stream_destroy_fn *on_destroy) { |
| 521 |
|
| 522 |
struct aws_h1_connection *connection = AWS_CONTAINER_OF(connection_base, struct aws_h1_connection, base); |
| 523 |
|
| 524 |
struct aws_h1_stream *stream = aws_mem_calloc(connection_base->alloc, 1, sizeof(struct aws_h1_stream)); |
| 525 |
if (!stream) { |
| 526 |
return NULL; |
| 527 |
} |
| 528 |
|
| 529 |
stream->base.vtable = &s_stream_vtable; |
| 530 |
stream->base.alloc = connection_base->alloc; |
| 531 |
stream->base.owning_connection = connection_base; |
| 532 |
stream->base.user_data = user_data; |
| 533 |
stream->base.on_incoming_headers = on_incoming_headers; |
| 534 |
stream->base.on_incoming_header_block_done = on_incoming_header_block_done; |
| 535 |
stream->base.on_incoming_body = on_incoming_body; |
| 536 |
stream->base.on_complete = on_complete; |
| 537 |
stream->base.on_destroy = on_destroy; |
| 538 |
stream->base.metrics.send_start_timestamp_ns = -1; |
| 539 |
stream->base.metrics.send_end_timestamp_ns = -1; |
| 540 |
stream->base.metrics.sending_duration_ns = -1; |
| 541 |
stream->base.metrics.receive_start_timestamp_ns = -1; |
| 542 |
stream->base.metrics.receive_end_timestamp_ns = -1; |
| 543 |
stream->base.metrics.receiving_duration_ns = -1; |
| 544 |
|
| 545 |
aws_channel_task_init( |
| 546 |
&stream->cross_thread_work_task, s_stream_cross_thread_work_task, stream, "http1_stream_cross_thread_work"); |
| 547 |
|
| 548 |
aws_linked_list_init(&stream->thread_data.pending_chunk_list); |
| 549 |
aws_linked_list_init(&stream->synced_data.pending_chunk_list); |
| 550 |
aws_linked_list_init(&stream->thread_data.pending_data_write_list); |
| 551 |
aws_linked_list_init(&stream->synced_data.pending_data_write_list); |
| 552 |
|
| 553 |
stream->thread_data.stream_window = connection->initial_stream_window_size; |
| 554 |
|
| 555 |
|
| 556 |
aws_atomic_init_int(&stream->base.refcount, 1); |
| 557 |
|
| 558 |
return stream; |
| 559 |
} |
| 560 |
|
| 561 |
struct aws_h1_stream *aws_h1_stream_new_request( |
| 562 |
struct aws_http_connection *client_connection, |
| 563 |
const struct aws_http_make_request_options *options) { |
| 564 |
|
| 565 |
struct aws_h1_stream *stream = s_stream_new_common( |
| 566 |
client_connection, |
| 567 |
options->user_data, |
| 568 |
options->on_response_headers, |
| 569 |
options->on_response_header_block_done, |
| 570 |
options->on_response_body, |
| 571 |
options->on_complete, |
| 572 |
options->on_destroy); |
| 573 |
if (!stream) { |
| 574 |
return NULL; |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
if (client_connection->proxy_request_transform) { |
| 579 |
if (client_connection->proxy_request_transform(options->request, client_connection->user_data)) { |
| 580 |
goto error; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
stream->base.client_data = &stream->base.client_or_server_data.client; |
| 585 |
stream->base.client_data->response_status = AWS_HTTP_STATUS_CODE_UNKNOWN; |
| 586 |
stream->base.client_data->response_first_byte_timeout_ms = options->response_first_byte_timeout_ms; |
| 587 |
stream->base.on_metrics = options->on_metrics; |
| 588 |
|
| 589 |
|
| 590 |
stream->using_manual_data_writes = options->use_manual_data_writes; |
| 591 |
|
| 592 |
|
| 593 |
if (aws_h1_encoder_message_init_from_request( |
| 594 |
&stream->thread_data.encoder_message, |
| 595 |
client_connection->alloc, |
| 596 |
options->request, |
| 597 |
&stream->thread_data.pending_chunk_list, |
| 598 |
&stream->thread_data.pending_data_write_list, |
| 599 |
stream->using_manual_data_writes)) { |
| 600 |
goto error; |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
if (stream->thread_data.encoder_message.has_connection_close_header) { |
| 606 |
stream->thread_data.is_final_stream = true; |
| 607 |
} |
| 608 |
|
| 609 |
struct aws_http_headers *headers = aws_http_message_get_headers(options->request); |
| 610 |
|
| 611 |
|
| 612 |
for (size_t i = 0; i < aws_http_headers_count(headers); i++) { |
| 613 |
struct aws_http_header header; |
| 614 |
aws_http_headers_get_index(headers, i, &header); |
| 615 |
enum aws_http_header_name name_enum = aws_http_str_to_header_name(header.name); |
| 616 |
switch (name_enum) { |
| 617 |
case AWS_HTTP_HEADER_AUTHORIZATION: |
| 618 |
case AWS_HTTP_HEADER_SIGNING_SECURITY_TOKEN: |
| 619 |
case AWS_HTTP_HEADER_SIGNING_S3SESSION_TOKEN: |
| 620 |
|
| 621 |
|
| 622 |
AWS_LOGF_TRACE( |
| 623 |
AWS_LS_HTTP_STREAM, |
| 624 |
"id=%p: Sending header: " PRInSTR ": ***", |
| 625 |
(void *)&stream->base, |
| 626 |
AWS_BYTE_CURSOR_PRI(header.name)); |
| 627 |
break; |
| 628 |
default: |
| 629 |
|
| 630 |
AWS_LOGF_TRACE( |
| 631 |
AWS_LS_HTTP_STREAM, |
| 632 |
"id=%p: Sending header: " PRInSTR ": " PRInSTR "", |
| 633 |
(void *)&stream->base, |
| 634 |
AWS_BYTE_CURSOR_PRI(header.name), |
| 635 |
AWS_BYTE_CURSOR_PRI(header.value)); |
| 636 |
break; |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
stream->synced_data.using_chunked_encoding = stream->thread_data.encoder_message.has_chunked_encoding_header; |
| 641 |
|
| 642 |
return stream; |
| 643 |
|
| 644 |
error: |
| 645 |
s_stream_destroy(&stream->base); |
| 646 |
return NULL; |
| 647 |
} |
| 648 |
|
| 649 |
struct aws_h1_stream *aws_h1_stream_new_request_handler(const struct aws_http_request_handler_options *options) { |
| 650 |
struct aws_h1_stream *stream = s_stream_new_common( |
| 651 |
options->server_connection, |
| 652 |
options->user_data, |
| 653 |
options->on_request_headers, |
| 654 |
options->on_request_header_block_done, |
| 655 |
options->on_request_body, |
| 656 |
options->on_complete, |
| 657 |
options->on_destroy); |
| 658 |
if (!stream) { |
| 659 |
return NULL; |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
stream->base.id = aws_http_connection_get_next_stream_id(options->server_connection); |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
stream->synced_data.api_state = AWS_H1_STREAM_API_STATE_ACTIVE; |
| 671 |
|
| 672 |
stream->base.server_data = &stream->base.client_or_server_data.server; |
| 673 |
stream->base.server_data->on_request_done = options->on_request_done; |
| 674 |
aws_atomic_fetch_add(&stream->base.refcount, 1); |
| 675 |
|
| 676 |
return stream; |
| 677 |
} |
| 678 |
|
| 679 |
int aws_h1_stream_send_response(struct aws_h1_stream *stream, struct aws_http_message *response) { |
| 680 |
struct aws_h1_connection *connection = s_get_h1_connection(stream); |
| 681 |
int error_code = 0; |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
struct aws_h1_encoder_message encoder_message; |
| 686 |
bool body_headers_ignored = stream->base.request_method == AWS_HTTP_METHOD_HEAD; |
| 687 |
if (aws_h1_encoder_message_init_from_response( |
| 688 |
&encoder_message, |
| 689 |
stream->base.alloc, |
| 690 |
response, |
| 691 |
body_headers_ignored, |
| 692 |
&stream->thread_data.pending_chunk_list)) { |
| 693 |
error_code = aws_last_error(); |
| 694 |
goto error; |
| 695 |
} |
| 696 |
|
| 697 |
bool should_schedule_task = false; |
| 698 |
{ |
| 699 |
s_stream_lock_synced_data(stream); |
| 700 |
if (stream->synced_data.api_state == AWS_H1_STREAM_API_STATE_COMPLETE) { |
| 701 |
error_code = AWS_ERROR_HTTP_STREAM_HAS_COMPLETED; |
| 702 |
} else if (stream->synced_data.has_outgoing_response) { |
| 703 |
AWS_LOGF_ERROR(AWS_LS_HTTP_STREAM, "id=%p: Response already created on the stream", (void *)&stream->base); |
| 704 |
error_code = AWS_ERROR_INVALID_STATE; |
| 705 |
} else { |
| 706 |
stream->synced_data.has_outgoing_response = true; |
| 707 |
stream->synced_data.pending_outgoing_response = encoder_message; |
| 708 |
if (encoder_message.has_connection_close_header) { |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
connection->synced_data.new_stream_error_code = AWS_ERROR_HTTP_CONNECTION_CLOSED; |
| 714 |
} |
| 715 |
stream->synced_data.using_chunked_encoding = encoder_message.has_chunked_encoding_header; |
| 716 |
|
| 717 |
should_schedule_task = !stream->synced_data.is_cross_thread_work_task_scheduled; |
| 718 |
stream->synced_data.is_cross_thread_work_task_scheduled = true; |
| 719 |
} |
| 720 |
s_stream_unlock_synced_data(stream); |
| 721 |
} |
| 722 |
|
| 723 |
if (error_code) { |
| 724 |
goto error; |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
AWS_LOGF_DEBUG( |
| 729 |
AWS_LS_HTTP_STREAM, "id=%p: Created response on connection=%p: ", (void *)stream, (void *)connection); |
| 730 |
|
| 731 |
if (should_schedule_task) { |
| 732 |
|
| 733 |
aws_atomic_fetch_add(&stream->base.refcount, 1); |
| 734 |
AWS_LOGF_TRACE(AWS_LS_HTTP_STREAM, "id=%p: Scheduling stream cross-thread work task.", (void *)&stream->base); |
| 735 |
aws_channel_schedule_task_now( |
| 736 |
stream->base.owning_connection->channel_slot->channel, &stream->cross_thread_work_task); |
| 737 |
} else { |
| 738 |
AWS_LOGF_TRACE( |
| 739 |
AWS_LS_HTTP_STREAM, "id=%p: Stream cross-thread work task was already scheduled.", (void *)&stream->base); |
| 740 |
} |
| 741 |
|
| 742 |
return AWS_OP_SUCCESS; |
| 743 |
|
| 744 |
error: |
| 745 |
AWS_LOGF_ERROR( |
| 746 |
AWS_LS_HTTP_STREAM, |
| 747 |
"id=%p: Sending response on the stream failed, error %d (%s)", |
| 748 |
(void *)&stream->base, |
| 749 |
error_code, |
| 750 |
aws_error_name(error_code)); |
| 751 |
|
| 752 |
aws_h1_encoder_message_clean_up(&encoder_message); |
| 753 |
return aws_raise_error(error_code); |
| 754 |
} |