1 #ifndef AWS_S3_UTIL_H
2 #define AWS_S3_UTIL_H
3 
4 /**
5  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6  * SPDX-License-Identifier: Apache-2.0.
7  */
8 
9 /* This file provides access to useful constants and simple utility functions. */
10 
11 #include <aws/auth/signing_config.h>
12 #include <aws/common/byte_buf.h>
13 #include <aws/s3/s3.h>
14 
15 #if DEBUG_BUILD
16 #    define ASSERT_SYNCED_DATA_LOCK_HELD(object)                                                                       \
17         {                                                                                                              \
18             int cached_error = aws_last_error();                                                                       \
19             AWS_ASSERT(aws_mutex_try_lock(&(object)->synced_data.lock) == AWS_OP_ERR);                                 \
20             aws_raise_error(cached_error);                                                                             \
21         }
22 #else
23 #    define ASSERT_SYNCED_DATA_LOCK_HELD(object)
24 #endif
25 #define KB_TO_BYTES(kb) ((kb)*1024)
26 #define MB_TO_BYTES(mb) ((mb)*1024 * 1024)
27 
28 struct aws_allocator;
29 struct aws_http_stream;
30 struct aws_http_headers;
31 struct aws_http_message;
32 struct aws_event_loop;
33 
34 enum aws_s3_response_status {
35     AWS_S3_RESPONSE_STATUS_SUCCESS = 200,
36     AWS_S3_RESPONSE_STATUS_NO_CONTENT_SUCCESS = 204,
37     AWS_S3_RESPONSE_STATUS_RANGE_SUCCESS = 206,
38     AWS_S3_RESPONSE_STATUS_INTERNAL_ERROR = 500,
39     AWS_S3_RESPONSE_STATUS_SLOW_DOWN = 503,
40 };
41 
42 struct aws_cached_signing_config_aws {
43     struct aws_allocator *allocator;
44     struct aws_string *service;
45     struct aws_string *region;
46     struct aws_string *signed_body_value;
47 
48     struct aws_signing_config_aws config;
49 };
50 
51 AWS_EXTERN_C_BEGIN
52 
53 AWS_S3_API
54 extern const struct aws_byte_cursor g_content_md5_header_name;
55 
56 AWS_S3_API
57 extern const struct aws_byte_cursor g_s3_client_version;
58 
59 AWS_S3_API
60 extern const struct aws_byte_cursor g_user_agent_header_name;
61 
62 AWS_S3_API
63 extern const struct aws_byte_cursor g_user_agent_header_product_name;
64 
65 AWS_S3_API
66 extern const struct aws_byte_cursor g_acl_header_name;
67 
68 AWS_S3_API
69 extern const struct aws_byte_cursor g_host_header_name;
70 
71 AWS_S3_API
72 extern const struct aws_byte_cursor g_content_type_header_name;
73 
74 AWS_S3_API
75 extern const struct aws_byte_cursor g_content_length_header_name;
76 
77 AWS_S3_API
78 extern const struct aws_byte_cursor g_etag_header_name;
79 
80 AWS_S3_API
81 extern const size_t g_s3_min_upload_part_size;
82 
83 AWS_S3_API
84 extern const struct aws_byte_cursor g_s3_service_name;
85 
86 AWS_S3_API
87 extern const struct aws_byte_cursor g_range_header_name;
88 
89 AWS_S3_API
90 extern const struct aws_byte_cursor g_content_range_header_name;
91 
92 AWS_S3_API
93 extern const struct aws_byte_cursor g_accept_ranges_header_name;
94 
95 AWS_S3_API
96 extern const struct aws_byte_cursor g_post_method;
97 
98 AWS_S3_API
99 extern const struct aws_byte_cursor g_head_method;
100 
101 AWS_S3_API
102 extern const struct aws_byte_cursor g_delete_method;
103 
104 AWS_S3_API
105 extern const uint32_t g_s3_max_num_upload_parts;
106 
107 struct aws_cached_signing_config_aws *aws_cached_signing_config_new(
108     struct aws_allocator *allocator,
109     const struct aws_signing_config_aws *signing_config);
110 
111 void aws_cached_signing_config_destroy(struct aws_cached_signing_config_aws *cached_signing_config);
112 
113 /* Sets all headers specified for src on dest */
114 AWS_S3_API
115 void copy_http_headers(const struct aws_http_headers *src, struct aws_http_headers *dest);
116 
117 /* Get a top-level (exists directly under the root tag) tag value. */
118 struct aws_string *get_top_level_xml_tag_value(
119     struct aws_allocator *allocator,
120     const struct aws_byte_cursor *tag_name,
121     struct aws_byte_cursor *xml_body);
122 
123 AWS_S3_API
124 void replace_quote_entities(struct aws_allocator *allocator, struct aws_string *str, struct aws_byte_buf *out_buf);
125 
126 /* TODO could be moved to aws-c-common. */
127 AWS_S3_API
128 int aws_last_error_or_unknown(void);
129 
130 AWS_S3_API
131 void aws_s3_add_user_agent_header(struct aws_allocator *allocator, struct aws_http_message *message);
132 
133 /* Given the response headers list, finds the Content-Range header and parses the range-start, range-end and
134  * object-size. All output arguments are optional.*/
135 AWS_S3_API
136 int aws_s3_parse_content_range_response_header(
137     struct aws_allocator *allocator,
138     struct aws_http_headers *response_headers,
139     uint64_t *out_range_start,
140     uint64_t *out_range_end,
141     uint64_t *out_object_size);
142 
143 /* Given response headers, parses the content-length from a content-length respone header.*/
144 AWS_S3_API
145 int aws_s3_parse_content_length_response_header(
146     struct aws_allocator *allocator,
147     struct aws_http_headers *response_headers,
148     uint64_t *out_content_length);
149 
150 /* Calculate the number of parts based on overall object-range and part_size. This takes into account aligning
151  * part-ranges on part_size. (ie: if object_range_start is not evenly divisible by part_size, it is considered in the
152  * middle of a contiguous part, and that first part will be smaller than part_size.) */
153 AWS_S3_API
154 uint32_t aws_s3_get_num_parts(size_t part_size, uint64_t object_range_start, uint64_t object_range_end);
155 
156 /* Calculates the part range for a part given overall object range, size of each part, and the part's number. Note: part
157  * numbers begin at one. This takes into account aligning part-ranges on part_size. Intended to be used in conjunction
158  * with aws_s3_get_num_parts. part_number should be less than or equal to the result of aws_s3_get_num_parts. */
159 AWS_S3_API
160 void aws_s3_get_part_range(
161     uint64_t object_range_start,
162     uint64_t object_range_end,
163     size_t part_size,
164     uint32_t part_number,
165     uint64_t *out_part_range_start,
166     uint64_t *out_part_range_end);
167 
168 AWS_EXTERN_C_END
169 
170 #endif /* AWS_S3_UTIL_H */
171