signable_http_request.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/auth/signable.h>
  6. #include <aws/common/string.h>
  7. #include <aws/http/request_response.h>
  8. /*
  9. * This is a simple aws_signable wrapper implementation for the aws_http_message struct
  10. */
  11. struct aws_signable_http_request_impl {
  12. struct aws_http_message *request;
  13. struct aws_array_list headers;
  14. };
  15. static int s_aws_signable_http_request_get_property(
  16. const struct aws_signable *signable,
  17. const struct aws_string *name,
  18. struct aws_byte_cursor *out_value) {
  19. struct aws_signable_http_request_impl *impl = signable->impl;
  20. AWS_ZERO_STRUCT(*out_value);
  21. /*
  22. * uri and method can be queried directly from the wrapper request
  23. */
  24. if (aws_string_eq(name, g_aws_http_uri_property_name)) {
  25. aws_http_message_get_request_path(impl->request, out_value);
  26. } else if (aws_string_eq(name, g_aws_http_method_property_name)) {
  27. aws_http_message_get_request_method(impl->request, out_value);
  28. } else {
  29. return AWS_OP_ERR;
  30. }
  31. return AWS_OP_SUCCESS;
  32. }
  33. static int s_aws_signable_http_request_get_property_list(
  34. const struct aws_signable *signable,
  35. const struct aws_string *name,
  36. struct aws_array_list **out_list) {
  37. struct aws_signable_http_request_impl *impl = signable->impl;
  38. *out_list = NULL;
  39. if (aws_string_eq(name, g_aws_http_headers_property_list_name)) {
  40. *out_list = &impl->headers;
  41. } else {
  42. return AWS_OP_ERR;
  43. }
  44. return AWS_OP_SUCCESS;
  45. }
  46. static int s_aws_signable_http_request_get_payload_stream(
  47. const struct aws_signable *signable,
  48. struct aws_input_stream **out_input_stream) {
  49. struct aws_signable_http_request_impl *impl = signable->impl;
  50. *out_input_stream = aws_http_message_get_body_stream(impl->request);
  51. return AWS_OP_SUCCESS;
  52. }
  53. static void s_aws_signable_http_request_destroy(struct aws_signable *signable) {
  54. if (signable == NULL) {
  55. return;
  56. }
  57. struct aws_signable_http_request_impl *impl = signable->impl;
  58. if (impl == NULL) {
  59. return;
  60. }
  61. aws_array_list_clean_up(&impl->headers);
  62. aws_mem_release(signable->allocator, signable);
  63. }
  64. static struct aws_signable_vtable s_signable_http_request_vtable = {
  65. .get_property = s_aws_signable_http_request_get_property,
  66. .get_property_list = s_aws_signable_http_request_get_property_list,
  67. .get_payload_stream = s_aws_signable_http_request_get_payload_stream,
  68. .destroy = s_aws_signable_http_request_destroy,
  69. };
  70. struct aws_signable *aws_signable_new_http_request(struct aws_allocator *allocator, struct aws_http_message *request) {
  71. struct aws_signable *signable = NULL;
  72. struct aws_signable_http_request_impl *impl = NULL;
  73. aws_mem_acquire_many(
  74. allocator, 2, &signable, sizeof(struct aws_signable), &impl, sizeof(struct aws_signable_http_request_impl));
  75. AWS_ZERO_STRUCT(*signable);
  76. AWS_ZERO_STRUCT(*impl);
  77. signable->allocator = allocator;
  78. signable->vtable = &s_signable_http_request_vtable;
  79. signable->impl = impl;
  80. /*
  81. * Copy the headers since they're not different types
  82. */
  83. size_t header_count = aws_http_message_get_header_count(request);
  84. if (aws_array_list_init_dynamic(
  85. &impl->headers, allocator, header_count, sizeof(struct aws_signable_property_list_pair))) {
  86. goto on_error;
  87. }
  88. for (size_t i = 0; i < header_count; ++i) {
  89. struct aws_http_header header;
  90. aws_http_message_get_header(request, &header, i);
  91. struct aws_signable_property_list_pair property = {.name = header.name, .value = header.value};
  92. aws_array_list_push_back(&impl->headers, &property);
  93. }
  94. impl->request = request;
  95. return signable;
  96. on_error:
  97. aws_signable_destroy(signable);
  98. return NULL;
  99. }