signable_chunk.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/io/stream.h>
  8. /*
  9. * This is a simple aws_signable wrapper implementation for an s3 chunk
  10. */
  11. struct aws_signable_chunk_impl {
  12. struct aws_input_stream *chunk_data;
  13. struct aws_string *previous_signature;
  14. };
  15. static int s_aws_signable_chunk_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_chunk_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_previous_signature_property_name)) {
  25. *out_value = aws_byte_cursor_from_string(impl->previous_signature);
  26. } else {
  27. return AWS_OP_ERR;
  28. }
  29. return AWS_OP_SUCCESS;
  30. }
  31. static int s_aws_signable_chunk_get_property_list(
  32. const struct aws_signable *signable,
  33. const struct aws_string *name,
  34. struct aws_array_list **out_list) {
  35. (void)signable;
  36. (void)name;
  37. (void)out_list;
  38. return AWS_OP_ERR;
  39. }
  40. static int s_aws_signable_chunk_get_payload_stream(
  41. const struct aws_signable *signable,
  42. struct aws_input_stream **out_input_stream) {
  43. struct aws_signable_chunk_impl *impl = signable->impl;
  44. *out_input_stream = impl->chunk_data;
  45. return AWS_OP_SUCCESS;
  46. }
  47. static void s_aws_signable_chunk_destroy(struct aws_signable *signable) {
  48. if (signable == NULL) {
  49. return;
  50. }
  51. struct aws_signable_chunk_impl *impl = signable->impl;
  52. if (impl == NULL) {
  53. return;
  54. }
  55. aws_input_stream_release(impl->chunk_data);
  56. aws_string_destroy(impl->previous_signature);
  57. aws_mem_release(signable->allocator, signable);
  58. }
  59. static struct aws_signable_vtable s_signable_chunk_vtable = {
  60. .get_property = s_aws_signable_chunk_get_property,
  61. .get_property_list = s_aws_signable_chunk_get_property_list,
  62. .get_payload_stream = s_aws_signable_chunk_get_payload_stream,
  63. .destroy = s_aws_signable_chunk_destroy,
  64. };
  65. struct aws_signable *aws_signable_new_chunk(
  66. struct aws_allocator *allocator,
  67. struct aws_input_stream *chunk_data,
  68. struct aws_byte_cursor previous_signature) {
  69. struct aws_signable *signable = NULL;
  70. struct aws_signable_chunk_impl *impl = NULL;
  71. aws_mem_acquire_many(
  72. allocator, 2, &signable, sizeof(struct aws_signable), &impl, sizeof(struct aws_signable_chunk_impl));
  73. if (signable == NULL || impl == NULL) {
  74. return NULL;
  75. }
  76. AWS_ZERO_STRUCT(*signable);
  77. AWS_ZERO_STRUCT(*impl);
  78. signable->allocator = allocator;
  79. signable->vtable = &s_signable_chunk_vtable;
  80. signable->impl = impl;
  81. impl->chunk_data = aws_input_stream_acquire(chunk_data);
  82. impl->previous_signature = aws_string_new_from_array(allocator, previous_signature.ptr, previous_signature.len);
  83. if (impl->previous_signature == NULL) {
  84. goto on_error;
  85. }
  86. return signable;
  87. on_error:
  88. aws_signable_destroy(signable);
  89. return NULL;
  90. }