Skip to content

Commit 6af2c8e

Browse files
authored
Can make new request with existing headers (#162)
1 parent 98edb2e commit 6af2c8e

File tree

4 files changed

+64
-8
lines changed

4 files changed

+64
-8
lines changed

include/aws/http/request_response.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,15 @@ void aws_http_headers_clear(struct aws_http_headers *headers);
419419
AWS_HTTP_API
420420
struct aws_http_message *aws_http_message_new_request(struct aws_allocator *allocator);
421421

422+
/**
423+
* Like aws_http_message_new_request(), but uses existing aws_http_headers instead of creating a new one.
424+
* Acquires a hold on the headers, and releases it when the request is destroyed.
425+
*/
426+
AWS_HTTP_API
427+
struct aws_http_message *aws_http_message_new_request_with_headers(
428+
struct aws_allocator *allocator,
429+
struct aws_http_headers *existing_headers);
430+
422431
/**
423432
* Create a new response message.
424433
* The message is blank, all properties (status, headers, etc) must be set individually.

source/request_response.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,10 @@ static int s_set_string_from_cursor(
367367
*dst = new_str;
368368
return AWS_OP_SUCCESS;
369369
}
370-
static struct aws_http_message *s_message_new_common(struct aws_allocator *allocator) {
370+
static struct aws_http_message *s_message_new_common(
371+
struct aws_allocator *allocator,
372+
struct aws_http_headers *existing_headers) {
373+
371374
struct aws_http_message *message = aws_mem_calloc(allocator, 1, sizeof(struct aws_http_message));
372375
if (!message) {
373376
goto error;
@@ -376,9 +379,14 @@ static struct aws_http_message *s_message_new_common(struct aws_allocator *alloc
376379
message->allocator = allocator;
377380
aws_atomic_init_int(&message->refcount, 1);
378381

379-
message->headers = aws_http_headers_new(allocator);
380-
if (!message->headers) {
381-
goto error;
382+
if (existing_headers) {
383+
message->headers = existing_headers;
384+
aws_http_headers_acquire(message->headers);
385+
} else {
386+
message->headers = aws_http_headers_new(allocator);
387+
if (!message->headers) {
388+
goto error;
389+
}
382390
}
383391

384392
return message;
@@ -387,20 +395,36 @@ static struct aws_http_message *s_message_new_common(struct aws_allocator *alloc
387395
return NULL;
388396
}
389397

390-
struct aws_http_message *aws_http_message_new_request(struct aws_allocator *allocator) {
391-
AWS_PRECONDITION(allocator);
398+
static struct aws_http_message *s_message_new_request_common(
399+
struct aws_allocator *allocator,
400+
struct aws_http_headers *existing_headers) {
392401

393-
struct aws_http_message *message = s_message_new_common(allocator);
402+
struct aws_http_message *message = s_message_new_common(allocator, existing_headers);
394403
if (message) {
395404
message->request_data = &message->subclass_data.request;
396405
}
397406
return message;
398407
}
399408

409+
struct aws_http_message *aws_http_message_new_request_with_headers(
410+
struct aws_allocator *allocator,
411+
struct aws_http_headers *existing_headers) {
412+
413+
AWS_PRECONDITION(allocator);
414+
AWS_PRECONDITION(existing_headers);
415+
416+
return s_message_new_request_common(allocator, existing_headers);
417+
}
418+
419+
struct aws_http_message *aws_http_message_new_request(struct aws_allocator *allocator) {
420+
AWS_PRECONDITION(allocator);
421+
return s_message_new_request_common(allocator, NULL);
422+
}
423+
400424
struct aws_http_message *aws_http_message_new_response(struct aws_allocator *allocator) {
401425
AWS_PRECONDITION(allocator);
402426

403-
struct aws_http_message *message = s_message_new_common(allocator);
427+
struct aws_http_message *message = s_message_new_common(allocator, NULL);
404428
if (message) {
405429
message->response_data = &message->subclass_data.response;
406430
message->response_data->status = AWS_HTTP_STATUS_UNKNOWN;

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ add_test_case(message_request_method)
1919
add_test_case(message_request_path)
2020
add_test_case(message_response_status)
2121
add_test_case(message_refcounts)
22+
add_test_case(message_with_existing_headers)
2223
add_test_case(message_handles_oom)
2324

2425
add_test_case(h1_test_get_request)

tests/test_message.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,28 @@ TEST_CASE(message_refcounts) {
355355
return AWS_OP_SUCCESS;
356356
}
357357

358+
TEST_CASE(message_with_existing_headers) {
359+
(void)ctx;
360+
struct aws_http_headers *headers = aws_http_headers_new(allocator);
361+
ASSERT_NOT_NULL(headers);
362+
363+
struct aws_http_message *message = aws_http_message_new_request_with_headers(allocator, headers);
364+
ASSERT_NOT_NULL(message);
365+
366+
ASSERT_PTR_EQUALS(headers, aws_http_message_get_headers(message));
367+
368+
/* assert message has acquired hold on headers */
369+
aws_http_headers_release(headers);
370+
371+
/* still valid, right? */
372+
struct aws_http_header new_header = {aws_byte_cursor_from_c_str("Host"), aws_byte_cursor_from_c_str("example.com")};
373+
ASSERT_SUCCESS(aws_http_message_add_header(message, new_header));
374+
375+
/* clean up*/
376+
aws_http_message_release(message);
377+
return AWS_OP_SUCCESS;
378+
}
379+
358380
/* Do every operation that involves allocating some memory */
359381
int s_message_handles_oom_attempt(struct aws_http_message *request) {
360382
ASSERT_NOT_NULL(request);

0 commit comments

Comments
 (0)