s3_meta_request.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include "aws/s3/private/s3_auto_ranged_get.h"
  6. #include "aws/s3/private/s3_checksums.h"
  7. #include "aws/s3/private/s3_client_impl.h"
  8. #include "aws/s3/private/s3_meta_request_impl.h"
  9. #include "aws/s3/private/s3_request_messages.h"
  10. #include "aws/s3/private/s3_util.h"
  11. #include <aws/auth/signable.h>
  12. #include <aws/auth/signing.h>
  13. #include <aws/auth/signing_config.h>
  14. #include <aws/auth/signing_result.h>
  15. #include <aws/common/encoding.h>
  16. #include <aws/common/string.h>
  17. #include <aws/common/system_info.h>
  18. #include <aws/io/event_loop.h>
  19. #include <aws/io/retry_strategy.h>
  20. #include <aws/io/stream.h>
  21. #include <inttypes.h>
  22. static const size_t s_dynamic_body_initial_buf_size = KB_TO_BYTES(1);
  23. static const size_t s_default_body_streaming_priority_queue_size = 16;
  24. static int s_s3_request_priority_queue_pred(const void *a, const void *b);
  25. static void s_s3_meta_request_destroy(void *user_data);
  26. static void s_s3_meta_request_init_signing_date_time(
  27. struct aws_s3_meta_request *meta_request,
  28. struct aws_date_time *date_time);
  29. static void s_s3_meta_request_sign_request(
  30. struct aws_s3_meta_request *meta_request,
  31. struct aws_s3_request *request,
  32. aws_signing_complete_fn *on_signing_complete,
  33. void *user_data);
  34. static void s_s3_meta_request_request_on_signed(
  35. struct aws_signing_result *signing_result,
  36. int error_code,
  37. void *user_data);
  38. static int s_s3_meta_request_incoming_body(
  39. struct aws_http_stream *stream,
  40. const struct aws_byte_cursor *data,
  41. void *user_data);
  42. static int s_s3_meta_request_incoming_headers(
  43. struct aws_http_stream *stream,
  44. enum aws_http_header_block header_block,
  45. const struct aws_http_header *headers,
  46. size_t headers_count,
  47. void *user_data);
  48. static void s_s3_meta_request_stream_complete(struct aws_http_stream *stream, int error_code, void *user_data);
  49. static void s_s3_meta_request_send_request_finish(
  50. struct aws_s3_connection *connection,
  51. struct aws_http_stream *stream,
  52. int error_code);
  53. void aws_s3_meta_request_lock_synced_data(struct aws_s3_meta_request *meta_request) {
  54. AWS_PRECONDITION(meta_request);
  55. aws_mutex_lock(&meta_request->synced_data.lock);
  56. }
  57. void aws_s3_meta_request_unlock_synced_data(struct aws_s3_meta_request *meta_request) {
  58. AWS_PRECONDITION(meta_request);
  59. aws_mutex_unlock(&meta_request->synced_data.lock);
  60. }
  61. static int s_meta_request_get_response_headers_checksum_callback(
  62. struct aws_s3_meta_request *meta_request,
  63. const struct aws_http_headers *headers,
  64. int response_status,
  65. void *user_data) {
  66. for (int i = AWS_SCA_INIT; i <= AWS_SCA_END; i++) {
  67. if (!aws_s3_meta_request_checksum_config_has_algorithm(meta_request, i)) {
  68. /* If user doesn't select this algorithm, skip */
  69. continue;
  70. }
  71. const struct aws_byte_cursor *algorithm_header_name = aws_get_http_header_name_from_algorithm(i);
  72. if (aws_http_headers_has(headers, *algorithm_header_name)) {
  73. struct aws_byte_cursor header_sum;
  74. aws_http_headers_get(headers, *algorithm_header_name, &header_sum);
  75. size_t encoded_len = 0;
  76. aws_base64_compute_encoded_len(aws_get_digest_size_from_algorithm(i), &encoded_len);
  77. if (header_sum.len == encoded_len - 1) {
  78. /* encoded_len includes the nullptr length. -1 is the expected length. */
  79. aws_byte_buf_init_copy_from_cursor(
  80. &meta_request->meta_request_level_response_header_checksum, aws_default_allocator(), header_sum);
  81. meta_request->meta_request_level_running_response_sum = aws_checksum_new(aws_default_allocator(), i);
  82. }
  83. break;
  84. }
  85. }
  86. if (meta_request->headers_user_callback_after_checksum) {
  87. return meta_request->headers_user_callback_after_checksum(meta_request, headers, response_status, user_data);
  88. } else {
  89. return AWS_OP_SUCCESS;
  90. }
  91. }
  92. /* warning this might get screwed up with retrys/restarts */
  93. static int s_meta_request_get_response_body_checksum_callback(
  94. struct aws_s3_meta_request *meta_request,
  95. const struct aws_byte_cursor *body,
  96. uint64_t range_start,
  97. void *user_data) {
  98. if (meta_request->meta_request_level_running_response_sum) {
  99. aws_checksum_update(meta_request->meta_request_level_running_response_sum, body);
  100. }
  101. if (meta_request->body_user_callback_after_checksum) {
  102. return meta_request->body_user_callback_after_checksum(meta_request, body, range_start, user_data);
  103. } else {
  104. return AWS_OP_SUCCESS;
  105. }
  106. }
  107. static void s_meta_request_get_response_finish_checksum_callback(
  108. struct aws_s3_meta_request *meta_request,
  109. const struct aws_s3_meta_request_result *meta_request_result,
  110. void *user_data) {
  111. struct aws_byte_buf response_body_sum;
  112. struct aws_byte_buf encoded_response_body_sum;
  113. AWS_ZERO_STRUCT(response_body_sum);
  114. AWS_ZERO_STRUCT(encoded_response_body_sum);
  115. struct aws_s3_meta_request_result *mut_meta_request_result =
  116. (struct aws_s3_meta_request_result *)meta_request_result;
  117. if (meta_request_result->error_code == AWS_OP_SUCCESS && meta_request->meta_request_level_running_response_sum) {
  118. mut_meta_request_result->did_validate = true;
  119. mut_meta_request_result->validation_algorithm =
  120. meta_request->meta_request_level_running_response_sum->algorithm;
  121. size_t encoded_checksum_len = 0;
  122. /* what error should I raise for these? */
  123. aws_base64_compute_encoded_len(
  124. meta_request->meta_request_level_running_response_sum->digest_size, &encoded_checksum_len);
  125. aws_byte_buf_init(&encoded_response_body_sum, aws_default_allocator(), encoded_checksum_len);
  126. aws_byte_buf_init(
  127. &response_body_sum,
  128. aws_default_allocator(),
  129. meta_request->meta_request_level_running_response_sum->digest_size);
  130. aws_checksum_finalize(meta_request->meta_request_level_running_response_sum, &response_body_sum, 0);
  131. struct aws_byte_cursor response_body_sum_cursor = aws_byte_cursor_from_buf(&response_body_sum);
  132. aws_base64_encode(&response_body_sum_cursor, &encoded_response_body_sum);
  133. if (!aws_byte_buf_eq(&encoded_response_body_sum, &meta_request->meta_request_level_response_header_checksum)) {
  134. mut_meta_request_result->error_code = AWS_ERROR_S3_RESPONSE_CHECKSUM_MISMATCH;
  135. }
  136. }
  137. if (meta_request->finish_user_callback_after_checksum) {
  138. meta_request->finish_user_callback_after_checksum(meta_request, meta_request_result, user_data);
  139. }
  140. aws_byte_buf_clean_up(&response_body_sum);
  141. aws_byte_buf_clean_up(&encoded_response_body_sum);
  142. aws_checksum_destroy(meta_request->meta_request_level_running_response_sum);
  143. aws_byte_buf_clean_up(&meta_request->meta_request_level_response_header_checksum);
  144. }
  145. int aws_s3_meta_request_init_base(
  146. struct aws_allocator *allocator,
  147. struct aws_s3_client *client,
  148. size_t part_size,
  149. bool should_compute_content_md5,
  150. const struct aws_s3_meta_request_options *options,
  151. void *impl,
  152. struct aws_s3_meta_request_vtable *vtable,
  153. struct aws_s3_meta_request *meta_request) {
  154. AWS_PRECONDITION(allocator);
  155. AWS_PRECONDITION(options);
  156. AWS_PRECONDITION(options->message);
  157. AWS_PRECONDITION(impl);
  158. AWS_PRECONDITION(meta_request);
  159. AWS_ZERO_STRUCT(*meta_request);
  160. AWS_ASSERT(vtable->update);
  161. AWS_ASSERT(vtable->prepare_request);
  162. AWS_ASSERT(vtable->destroy);
  163. AWS_ASSERT(vtable->sign_request);
  164. AWS_ASSERT(vtable->init_signing_date_time);
  165. AWS_ASSERT(vtable->finished_request);
  166. AWS_ASSERT(vtable->send_request_finish);
  167. meta_request->allocator = allocator;
  168. meta_request->type = options->type;
  169. /* Set up reference count. */
  170. aws_ref_count_init(&meta_request->ref_count, meta_request, s_s3_meta_request_destroy);
  171. if (part_size == SIZE_MAX) {
  172. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  173. goto error;
  174. }
  175. if (aws_mutex_init(&meta_request->synced_data.lock)) {
  176. AWS_LOGF_ERROR(
  177. AWS_LS_S3_META_REQUEST, "id=%p Could not initialize mutex for meta request", (void *)meta_request);
  178. goto error;
  179. }
  180. if (aws_priority_queue_init_dynamic(
  181. &meta_request->synced_data.pending_body_streaming_requests,
  182. meta_request->allocator,
  183. s_default_body_streaming_priority_queue_size,
  184. sizeof(struct aws_s3_request *),
  185. s_s3_request_priority_queue_pred)) {
  186. AWS_LOGF_ERROR(
  187. AWS_LS_S3_META_REQUEST, "id=%p Could not initialize priority queue for meta request", (void *)meta_request);
  188. /* Priority queue */
  189. goto error;
  190. }
  191. *((size_t *)&meta_request->part_size) = part_size;
  192. *((bool *)&meta_request->should_compute_content_md5) = should_compute_content_md5;
  193. checksum_config_init(&meta_request->checksum_config, options->checksum_config);
  194. if (options->signing_config) {
  195. meta_request->cached_signing_config = aws_cached_signing_config_new(allocator, options->signing_config);
  196. }
  197. /* Set initial_meta_request */
  198. if (options->send_filepath.len > 0) {
  199. /* Create copy of original message, but with body-stream that reads directly from file */
  200. meta_request->initial_request_message = aws_s3_message_util_copy_http_message_filepath_body_all_headers(
  201. allocator, options->message, options->send_filepath);
  202. if (meta_request->initial_request_message == NULL) {
  203. goto error;
  204. }
  205. } else {
  206. /* Keep a reference to the original message structure passed in. */
  207. meta_request->initial_request_message = aws_http_message_acquire(options->message);
  208. }
  209. /* Client is currently optional to allow spinning up a meta_request without a client in a test. */
  210. if (client != NULL) {
  211. aws_s3_client_acquire(client);
  212. meta_request->client = client;
  213. meta_request->io_event_loop = aws_event_loop_group_get_next_loop(client->body_streaming_elg);
  214. meta_request->synced_data.read_window_running_total = client->initial_read_window;
  215. }
  216. meta_request->synced_data.next_streaming_part = 1;
  217. meta_request->meta_request_level_running_response_sum = NULL;
  218. meta_request->user_data = options->user_data;
  219. meta_request->shutdown_callback = options->shutdown_callback;
  220. meta_request->progress_callback = options->progress_callback;
  221. if (meta_request->checksum_config.validate_response_checksum) {
  222. /* TODO: the validate for auto range get should happen for each response received. */
  223. meta_request->headers_user_callback_after_checksum = options->headers_callback;
  224. meta_request->body_user_callback_after_checksum = options->body_callback;
  225. meta_request->finish_user_callback_after_checksum = options->finish_callback;
  226. meta_request->headers_callback = s_meta_request_get_response_headers_checksum_callback;
  227. meta_request->body_callback = s_meta_request_get_response_body_checksum_callback;
  228. meta_request->finish_callback = s_meta_request_get_response_finish_checksum_callback;
  229. } else {
  230. meta_request->headers_callback = options->headers_callback;
  231. meta_request->body_callback = options->body_callback;
  232. meta_request->finish_callback = options->finish_callback;
  233. }
  234. /* Nothing can fail after here. Leave the impl not affected by failure of initializing base. */
  235. meta_request->impl = impl;
  236. meta_request->vtable = vtable;
  237. return AWS_OP_SUCCESS;
  238. error:
  239. s_s3_meta_request_destroy((void *)meta_request);
  240. return AWS_OP_ERR;
  241. }
  242. void aws_s3_meta_request_increment_read_window(struct aws_s3_meta_request *meta_request, uint64_t bytes) {
  243. AWS_PRECONDITION(meta_request);
  244. if (bytes == 0) {
  245. return;
  246. }
  247. if (!meta_request->client->enable_read_backpressure) {
  248. AWS_LOGF_DEBUG(
  249. AWS_LS_S3_META_REQUEST,
  250. "id=%p: Ignoring call to increment read window. This client has not enabled read backpressure.",
  251. (void *)meta_request);
  252. return;
  253. }
  254. AWS_LOGF_TRACE(AWS_LS_S3_META_REQUEST, "id=%p: Incrementing read window by %" PRIu64, (void *)meta_request, bytes);
  255. /* BEGIN CRITICAL SECTION */
  256. aws_s3_meta_request_lock_synced_data(meta_request);
  257. /* Response will never approach UINT64_MAX, so do a saturating sum instead of worrying about overflow */
  258. meta_request->synced_data.read_window_running_total =
  259. aws_add_u64_saturating(bytes, meta_request->synced_data.read_window_running_total);
  260. aws_s3_meta_request_unlock_synced_data(meta_request);
  261. /* END CRITICAL SECTION */
  262. /* Schedule the work task, to continue processing the meta-request */
  263. aws_s3_client_schedule_process_work(meta_request->client);
  264. }
  265. void aws_s3_meta_request_cancel(struct aws_s3_meta_request *meta_request) {
  266. /* BEGIN CRITICAL SECTION */
  267. aws_s3_meta_request_lock_synced_data(meta_request);
  268. aws_s3_meta_request_set_fail_synced(meta_request, NULL, AWS_ERROR_S3_CANCELED);
  269. aws_s3_meta_request_unlock_synced_data(meta_request);
  270. /* END CRITICAL SECTION */
  271. }
  272. int aws_s3_meta_request_pause(
  273. struct aws_s3_meta_request *meta_request,
  274. struct aws_s3_meta_request_resume_token **out_resume_token) {
  275. AWS_PRECONDITION(meta_request);
  276. AWS_PRECONDITION(meta_request->vtable);
  277. *out_resume_token = NULL;
  278. if (!meta_request->vtable->pause) {
  279. return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
  280. }
  281. return meta_request->vtable->pause(meta_request, out_resume_token);
  282. }
  283. void aws_s3_meta_request_set_fail_synced(
  284. struct aws_s3_meta_request *meta_request,
  285. struct aws_s3_request *failed_request,
  286. int error_code) {
  287. AWS_PRECONDITION(meta_request);
  288. ASSERT_SYNCED_DATA_LOCK_HELD(meta_request);
  289. if (meta_request->synced_data.finish_result_set) {
  290. return;
  291. }
  292. meta_request->synced_data.finish_result_set = true;
  293. if ((error_code == AWS_ERROR_S3_INVALID_RESPONSE_STATUS || error_code == AWS_ERROR_S3_NON_RECOVERABLE_ASYNC_ERROR ||
  294. error_code == AWS_ERROR_S3_OBJECT_MODIFIED) &&
  295. failed_request != NULL) {
  296. aws_s3_meta_request_result_setup(
  297. meta_request,
  298. &meta_request->synced_data.finish_result,
  299. failed_request,
  300. failed_request->send_data.response_status,
  301. error_code);
  302. } else {
  303. AWS_ASSERT(error_code != AWS_ERROR_S3_INVALID_RESPONSE_STATUS);
  304. aws_s3_meta_request_result_setup(meta_request, &meta_request->synced_data.finish_result, NULL, 0, error_code);
  305. }
  306. }
  307. void aws_s3_meta_request_set_success_synced(struct aws_s3_meta_request *meta_request, int response_status) {
  308. AWS_PRECONDITION(meta_request);
  309. ASSERT_SYNCED_DATA_LOCK_HELD(meta_request);
  310. if (meta_request->synced_data.finish_result_set) {
  311. return;
  312. }
  313. meta_request->synced_data.finish_result_set = true;
  314. aws_s3_meta_request_result_setup(
  315. meta_request, &meta_request->synced_data.finish_result, NULL, response_status, AWS_ERROR_SUCCESS);
  316. }
  317. bool aws_s3_meta_request_has_finish_result(struct aws_s3_meta_request *meta_request) {
  318. AWS_PRECONDITION(meta_request);
  319. /* BEGIN CRITICAL SECTION */
  320. aws_s3_meta_request_lock_synced_data(meta_request);
  321. bool is_finishing = aws_s3_meta_request_has_finish_result_synced(meta_request);
  322. aws_s3_meta_request_unlock_synced_data(meta_request);
  323. /* END CRITICAL SECTION */
  324. return is_finishing;
  325. }
  326. bool aws_s3_meta_request_has_finish_result_synced(struct aws_s3_meta_request *meta_request) {
  327. AWS_PRECONDITION(meta_request);
  328. ASSERT_SYNCED_DATA_LOCK_HELD(meta_request);
  329. if (!meta_request->synced_data.finish_result_set) {
  330. return false;
  331. }
  332. return true;
  333. }
  334. struct aws_s3_meta_request *aws_s3_meta_request_acquire(struct aws_s3_meta_request *meta_request) {
  335. AWS_PRECONDITION(meta_request);
  336. aws_ref_count_acquire(&meta_request->ref_count);
  337. return meta_request;
  338. }
  339. struct aws_s3_meta_request *aws_s3_meta_request_release(struct aws_s3_meta_request *meta_request) {
  340. if (meta_request != NULL) {
  341. aws_ref_count_release(&meta_request->ref_count);
  342. }
  343. return NULL;
  344. }
  345. static void s_s3_meta_request_destroy(void *user_data) {
  346. struct aws_s3_meta_request *meta_request = user_data;
  347. AWS_PRECONDITION(meta_request);
  348. AWS_LOGF_DEBUG(AWS_LS_S3_META_REQUEST, "id=%p Cleaning up meta request", (void *)meta_request);
  349. /* Clean up our initial http message */
  350. if (meta_request->initial_request_message != NULL) {
  351. aws_http_message_release(meta_request->initial_request_message);
  352. meta_request->initial_request_message = NULL;
  353. }
  354. void *meta_request_user_data = meta_request->user_data;
  355. aws_s3_meta_request_shutdown_fn *shutdown_callback = meta_request->shutdown_callback;
  356. aws_cached_signing_config_destroy(meta_request->cached_signing_config);
  357. aws_mutex_clean_up(&meta_request->synced_data.lock);
  358. /* endpoint should have already been released and set NULL by the meta request finish call.
  359. * But call release() again, just in case we're tearing down a half-initialized meta request */
  360. aws_s3_endpoint_release(meta_request->endpoint);
  361. meta_request->client = aws_s3_client_release(meta_request->client);
  362. aws_priority_queue_clean_up(&meta_request->synced_data.pending_body_streaming_requests);
  363. aws_s3_meta_request_result_clean_up(meta_request, &meta_request->synced_data.finish_result);
  364. if (meta_request->vtable != NULL) {
  365. AWS_LOGF_TRACE(
  366. AWS_LS_S3_META_REQUEST, "id=%p Calling virtual meta request destroy function.", (void *)meta_request);
  367. meta_request->vtable->destroy(meta_request);
  368. }
  369. meta_request = NULL;
  370. if (shutdown_callback != NULL) {
  371. AWS_LOGF_TRACE(AWS_LS_S3_META_REQUEST, "id=%p Calling meta request shutdown callback.", (void *)meta_request);
  372. shutdown_callback(meta_request_user_data);
  373. }
  374. AWS_LOGF_TRACE(AWS_LS_S3_META_REQUEST, "id=%p Meta request clean up finished.", (void *)meta_request);
  375. }
  376. static int s_s3_request_priority_queue_pred(const void *a, const void *b) {
  377. const struct aws_s3_request **request_a = (const struct aws_s3_request **)a;
  378. AWS_PRECONDITION(request_a);
  379. AWS_PRECONDITION(*request_a);
  380. const struct aws_s3_request **request_b = (const struct aws_s3_request **)b;
  381. AWS_PRECONDITION(request_b);
  382. AWS_PRECONDITION(*request_b);
  383. return (*request_a)->part_number > (*request_b)->part_number;
  384. }
  385. bool aws_s3_meta_request_update(
  386. struct aws_s3_meta_request *meta_request,
  387. uint32_t flags,
  388. struct aws_s3_request **out_request) {
  389. AWS_PRECONDITION(meta_request);
  390. AWS_PRECONDITION(meta_request->vtable);
  391. AWS_PRECONDITION(meta_request->vtable->update);
  392. return meta_request->vtable->update(meta_request, flags, out_request);
  393. }
  394. bool aws_s3_meta_request_is_active(struct aws_s3_meta_request *meta_request) {
  395. AWS_PRECONDITION(meta_request);
  396. /* BEGIN CRITICAL SECTION */
  397. aws_s3_meta_request_lock_synced_data(meta_request);
  398. bool active = meta_request->synced_data.state == AWS_S3_META_REQUEST_STATE_ACTIVE;
  399. aws_s3_meta_request_unlock_synced_data(meta_request);
  400. /* END CRITICAL SECTION */
  401. return active;
  402. }
  403. bool aws_s3_meta_request_is_finished(struct aws_s3_meta_request *meta_request) {
  404. AWS_PRECONDITION(meta_request);
  405. /* BEGIN CRITICAL SECTION */
  406. aws_s3_meta_request_lock_synced_data(meta_request);
  407. bool is_finished = meta_request->synced_data.state == AWS_S3_META_REQUEST_STATE_FINISHED;
  408. aws_s3_meta_request_unlock_synced_data(meta_request);
  409. /* END CRITICAL SECTION */
  410. return is_finished;
  411. }
  412. static void s_s3_meta_request_prepare_request_task(struct aws_task *task, void *arg, enum aws_task_status task_status);
  413. static void s_s3_prepare_request_payload_callback_and_destroy(
  414. struct aws_s3_prepare_request_payload *payload,
  415. int error_code) {
  416. AWS_PRECONDITION(payload);
  417. AWS_PRECONDITION(payload->request);
  418. struct aws_s3_meta_request *meta_request = payload->request->meta_request;
  419. AWS_PRECONDITION(meta_request);
  420. AWS_PRECONDITION(meta_request->client);
  421. struct aws_s3_client *client = aws_s3_client_acquire(meta_request->client);
  422. struct aws_allocator *allocator = client->allocator;
  423. AWS_PRECONDITION(allocator);
  424. if (payload->callback != NULL) {
  425. payload->callback(meta_request, payload->request, error_code, payload->user_data);
  426. }
  427. aws_mem_release(allocator, payload);
  428. aws_s3_client_release(client);
  429. }
  430. static void s_s3_meta_request_schedule_prepare_request_default(
  431. struct aws_s3_meta_request *meta_request,
  432. struct aws_s3_request *request,
  433. aws_s3_meta_request_prepare_request_callback_fn *callback,
  434. void *user_data);
  435. void aws_s3_meta_request_prepare_request(
  436. struct aws_s3_meta_request *meta_request,
  437. struct aws_s3_request *request,
  438. aws_s3_meta_request_prepare_request_callback_fn *callback,
  439. void *user_data) {
  440. AWS_PRECONDITION(meta_request);
  441. AWS_PRECONDITION(meta_request->vtable);
  442. if (meta_request->vtable->schedule_prepare_request) {
  443. meta_request->vtable->schedule_prepare_request(meta_request, request, callback, user_data);
  444. } else {
  445. s_s3_meta_request_schedule_prepare_request_default(meta_request, request, callback, user_data);
  446. }
  447. }
  448. static void s_s3_meta_request_schedule_prepare_request_default(
  449. struct aws_s3_meta_request *meta_request,
  450. struct aws_s3_request *request,
  451. aws_s3_meta_request_prepare_request_callback_fn *callback,
  452. void *user_data) {
  453. AWS_PRECONDITION(meta_request);
  454. AWS_PRECONDITION(request);
  455. struct aws_s3_client *client = meta_request->client;
  456. AWS_PRECONDITION(client);
  457. struct aws_allocator *allocator = client->allocator;
  458. AWS_PRECONDITION(allocator);
  459. struct aws_s3_prepare_request_payload *payload =
  460. aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_prepare_request_payload));
  461. payload->request = request;
  462. payload->callback = callback;
  463. payload->user_data = user_data;
  464. aws_task_init(
  465. &payload->task, s_s3_meta_request_prepare_request_task, payload, "s3_meta_request_prepare_request_task");
  466. aws_event_loop_schedule_task_now(meta_request->io_event_loop, &payload->task);
  467. }
  468. static void s_s3_meta_request_prepare_request_task(struct aws_task *task, void *arg, enum aws_task_status task_status) {
  469. (void)task;
  470. (void)task_status;
  471. struct aws_s3_prepare_request_payload *payload = arg;
  472. AWS_PRECONDITION(payload);
  473. struct aws_s3_request *request = payload->request;
  474. AWS_PRECONDITION(request);
  475. struct aws_s3_meta_request *meta_request = request->meta_request;
  476. AWS_PRECONDITION(meta_request);
  477. struct aws_s3_meta_request_vtable *vtable = meta_request->vtable;
  478. AWS_PRECONDITION(vtable);
  479. /* Client owns this event loop group. A cancel should not be possible. */
  480. AWS_ASSERT(task_status == AWS_TASK_STATUS_RUN_READY);
  481. int error_code = AWS_ERROR_SUCCESS;
  482. if (!request->always_send && aws_s3_meta_request_has_finish_result(meta_request)) {
  483. aws_raise_error(AWS_ERROR_S3_CANCELED);
  484. goto dont_send_clean_up;
  485. }
  486. if (vtable->prepare_request(meta_request, request)) {
  487. ++request->num_times_prepared;
  488. goto dont_send_clean_up;
  489. }
  490. ++request->num_times_prepared;
  491. aws_s3_add_user_agent_header(meta_request->allocator, request->send_data.message);
  492. /* Sign the newly created message. */
  493. s_s3_meta_request_sign_request(meta_request, request, s_s3_meta_request_request_on_signed, payload);
  494. return;
  495. dont_send_clean_up:
  496. error_code = aws_last_error_or_unknown();
  497. AWS_LOGF_ERROR(
  498. AWS_LS_S3_META_REQUEST,
  499. "id=%p Could not prepare request %p due to error %d (%s).",
  500. (void *)meta_request,
  501. (void *)request,
  502. error_code,
  503. aws_error_str(error_code));
  504. /* BEGIN CRITICAL SECTION */
  505. {
  506. aws_s3_meta_request_lock_synced_data(meta_request);
  507. aws_s3_meta_request_set_fail_synced(meta_request, request, error_code);
  508. aws_s3_meta_request_unlock_synced_data(meta_request);
  509. }
  510. /* END CRITICAL SECTION */
  511. s_s3_prepare_request_payload_callback_and_destroy(payload, error_code);
  512. }
  513. static void s_s3_meta_request_init_signing_date_time(
  514. struct aws_s3_meta_request *meta_request,
  515. struct aws_date_time *date_time) {
  516. AWS_PRECONDITION(meta_request);
  517. AWS_PRECONDITION(meta_request->vtable);
  518. AWS_PRECONDITION(meta_request->vtable->init_signing_date_time);
  519. meta_request->vtable->init_signing_date_time(meta_request, date_time);
  520. }
  521. void aws_s3_meta_request_init_signing_date_time_default(
  522. struct aws_s3_meta_request *meta_request,
  523. struct aws_date_time *date_time) {
  524. AWS_PRECONDITION(meta_request);
  525. AWS_PRECONDITION(date_time);
  526. (void)meta_request;
  527. aws_date_time_init_now(date_time);
  528. }
  529. static void s_s3_meta_request_sign_request(
  530. struct aws_s3_meta_request *meta_request,
  531. struct aws_s3_request *request,
  532. aws_signing_complete_fn *on_signing_complete,
  533. void *user_data) {
  534. AWS_PRECONDITION(meta_request);
  535. AWS_PRECONDITION(meta_request->vtable);
  536. AWS_PRECONDITION(meta_request->vtable->sign_request);
  537. meta_request->vtable->sign_request(meta_request, request, on_signing_complete, user_data);
  538. }
  539. /* Handles signing a message for the caller. */
  540. void aws_s3_meta_request_sign_request_default(
  541. struct aws_s3_meta_request *meta_request,
  542. struct aws_s3_request *request,
  543. aws_signing_complete_fn *on_signing_complete,
  544. void *user_data) {
  545. AWS_PRECONDITION(meta_request);
  546. AWS_PRECONDITION(request);
  547. AWS_PRECONDITION(on_signing_complete);
  548. struct aws_s3_client *client = meta_request->client;
  549. AWS_ASSERT(client);
  550. struct aws_signing_config_aws signing_config;
  551. if (meta_request->cached_signing_config != NULL) {
  552. signing_config = meta_request->cached_signing_config->config;
  553. } else if (client->cached_signing_config != NULL) {
  554. signing_config = client->cached_signing_config->config;
  555. } else {
  556. AWS_LOGF_DEBUG(
  557. AWS_LS_S3_META_REQUEST,
  558. "id=%p: No signing config present. Not signing request %p.",
  559. (void *)meta_request,
  560. (void *)request);
  561. on_signing_complete(NULL, AWS_ERROR_SUCCESS, user_data);
  562. return;
  563. }
  564. s_s3_meta_request_init_signing_date_time(meta_request, &signing_config.date);
  565. request->send_data.signable = aws_signable_new_http_request(meta_request->allocator, request->send_data.message);
  566. AWS_LOGF_TRACE(
  567. AWS_LS_S3_META_REQUEST,
  568. "id=%p Created signable %p for request %p with message %p",
  569. (void *)meta_request,
  570. (void *)request->send_data.signable,
  571. (void *)request,
  572. (void *)request->send_data.message);
  573. if (request->send_data.signable == NULL) {
  574. AWS_LOGF_ERROR(
  575. AWS_LS_S3_META_REQUEST,
  576. "id=%p: Could not allocate signable for request %p",
  577. (void *)meta_request,
  578. (void *)request);
  579. on_signing_complete(NULL, aws_last_error_or_unknown(), user_data);
  580. return;
  581. }
  582. /* If the checksum is configured to be added to the trailer, the payload will be aws-chunked encoded. The payload
  583. * will need to be streaming signed/unsigned. */
  584. if (meta_request->checksum_config.location == AWS_SCL_TRAILER &&
  585. aws_byte_cursor_eq(&signing_config.signed_body_value, &g_aws_signed_body_value_unsigned_payload)) {
  586. signing_config.signed_body_value = g_aws_signed_body_value_streaming_unsigned_payload_trailer;
  587. }
  588. /* However the initial request for a multipart upload does not have a trailing checksum and is not chunked so it
  589. * must have an unsigned_payload signed_body value*/
  590. if (request->part_number == 0 &&
  591. aws_byte_cursor_eq(
  592. &signing_config.signed_body_value, &g_aws_signed_body_value_streaming_unsigned_payload_trailer)) {
  593. signing_config.signed_body_value = g_aws_signed_body_value_unsigned_payload;
  594. }
  595. if (aws_sign_request_aws(
  596. meta_request->allocator,
  597. request->send_data.signable,
  598. (struct aws_signing_config_base *)&signing_config,
  599. on_signing_complete,
  600. user_data)) {
  601. AWS_LOGF_ERROR(
  602. AWS_LS_S3_META_REQUEST, "id=%p: Could not sign request %p", (void *)meta_request, (void *)request);
  603. on_signing_complete(NULL, aws_last_error_or_unknown(), user_data);
  604. return;
  605. }
  606. }
  607. /* Handle the signing result, getting an HTTP connection for the request if signing succeeded. */
  608. static void s_s3_meta_request_request_on_signed(
  609. struct aws_signing_result *signing_result,
  610. int error_code,
  611. void *user_data) {
  612. struct aws_s3_prepare_request_payload *payload = user_data;
  613. AWS_PRECONDITION(payload);
  614. struct aws_s3_request *request = payload->request;
  615. AWS_PRECONDITION(request);
  616. struct aws_s3_meta_request *meta_request = request->meta_request;
  617. AWS_PRECONDITION(meta_request);
  618. if (error_code != AWS_ERROR_SUCCESS) {
  619. goto finish;
  620. }
  621. if (signing_result != NULL &&
  622. aws_apply_signing_result_to_http_request(request->send_data.message, meta_request->allocator, signing_result)) {
  623. error_code = aws_last_error_or_unknown();
  624. goto finish;
  625. }
  626. finish:
  627. if (error_code != AWS_ERROR_SUCCESS) {
  628. AWS_LOGF_ERROR(
  629. AWS_LS_S3_META_REQUEST,
  630. "id=%p Meta request could not sign TTP request due to error code %d (%s)",
  631. (void *)meta_request,
  632. error_code,
  633. aws_error_str(error_code));
  634. /* BEGIN CRITICAL SECTION */
  635. {
  636. aws_s3_meta_request_lock_synced_data(meta_request);
  637. aws_s3_meta_request_set_fail_synced(meta_request, request, error_code);
  638. aws_s3_meta_request_unlock_synced_data(meta_request);
  639. }
  640. /* END CRITICAL SECTION */
  641. }
  642. s_s3_prepare_request_payload_callback_and_destroy(payload, error_code);
  643. }
  644. void aws_s3_meta_request_send_request(struct aws_s3_meta_request *meta_request, struct aws_s3_connection *connection) {
  645. AWS_PRECONDITION(meta_request);
  646. AWS_PRECONDITION(connection);
  647. AWS_PRECONDITION(connection->http_connection);
  648. struct aws_s3_request *request = connection->request;
  649. AWS_PRECONDITION(request);
  650. /* Now that we have a signed request and a connection, go ahead and issue the request. */
  651. struct aws_http_make_request_options options;
  652. AWS_ZERO_STRUCT(options);
  653. options.self_size = sizeof(struct aws_http_make_request_options);
  654. options.request = request->send_data.message;
  655. options.user_data = connection;
  656. options.on_response_headers = s_s3_meta_request_incoming_headers;
  657. options.on_response_header_block_done = NULL;
  658. options.on_response_body = s_s3_meta_request_incoming_body;
  659. options.on_complete = s_s3_meta_request_stream_complete;
  660. struct aws_http_stream *stream = aws_http_connection_make_request(connection->http_connection, &options);
  661. if (stream == NULL) {
  662. AWS_LOGF_ERROR(
  663. AWS_LS_S3_META_REQUEST, "id=%p: Could not make HTTP request %p", (void *)meta_request, (void *)request);
  664. goto error_finish;
  665. }
  666. AWS_LOGF_TRACE(AWS_LS_S3_META_REQUEST, "id=%p: Sending request %p", (void *)meta_request, (void *)request);
  667. if (aws_http_stream_activate(stream) != AWS_OP_SUCCESS) {
  668. aws_http_stream_release(stream);
  669. stream = NULL;
  670. AWS_LOGF_ERROR(
  671. AWS_LS_S3_META_REQUEST, "id=%p: Could not activate HTTP stream %p", (void *)meta_request, (void *)request);
  672. goto error_finish;
  673. }
  674. return;
  675. error_finish:
  676. s_s3_meta_request_send_request_finish(connection, NULL, aws_last_error_or_unknown());
  677. }
  678. static int s_s3_meta_request_error_code_from_response_status(int response_status) {
  679. int error_code = AWS_ERROR_UNKNOWN;
  680. switch (response_status) {
  681. case AWS_S3_RESPONSE_STATUS_SUCCESS:
  682. case AWS_S3_RESPONSE_STATUS_RANGE_SUCCESS:
  683. case AWS_S3_RESPONSE_STATUS_NO_CONTENT_SUCCESS:
  684. error_code = AWS_ERROR_SUCCESS;
  685. break;
  686. case AWS_S3_RESPONSE_STATUS_INTERNAL_ERROR:
  687. error_code = AWS_ERROR_S3_INTERNAL_ERROR;
  688. break;
  689. case AWS_S3_RESPONSE_STATUS_SLOW_DOWN:
  690. error_code = AWS_ERROR_S3_SLOW_DOWN;
  691. break;
  692. default:
  693. error_code = AWS_ERROR_S3_INVALID_RESPONSE_STATUS;
  694. break;
  695. }
  696. return error_code;
  697. }
  698. static bool s_header_value_from_list(
  699. const struct aws_http_header *headers,
  700. size_t headers_count,
  701. const struct aws_byte_cursor *name,
  702. struct aws_byte_cursor *out_value) {
  703. for (size_t i = 0; i < headers_count; ++i) {
  704. if (aws_byte_cursor_eq(&headers[i].name, name)) {
  705. *out_value = headers[i].value;
  706. return true;
  707. }
  708. }
  709. return false;
  710. }
  711. static void s_get_part_response_headers_checksum_helper(
  712. struct aws_s3_connection *connection,
  713. struct aws_s3_meta_request *meta_request,
  714. const struct aws_http_header *headers,
  715. size_t headers_count) {
  716. for (int i = AWS_SCA_INIT; i <= AWS_SCA_END; i++) {
  717. if (!aws_s3_meta_request_checksum_config_has_algorithm(meta_request, i)) {
  718. /* If user doesn't select this algorithm, skip */
  719. continue;
  720. }
  721. const struct aws_byte_cursor *algorithm_header_name = aws_get_http_header_name_from_algorithm(i);
  722. struct aws_byte_cursor header_sum;
  723. if (s_header_value_from_list(headers, headers_count, algorithm_header_name, &header_sum)) {
  724. size_t encoded_len = 0;
  725. aws_base64_compute_encoded_len(aws_get_digest_size_from_algorithm(i), &encoded_len);
  726. if (header_sum.len == encoded_len - 1) {
  727. aws_byte_buf_init_copy_from_cursor(
  728. &connection->request->request_level_response_header_checksum, aws_default_allocator(), header_sum);
  729. connection->request->request_level_running_response_sum = aws_checksum_new(aws_default_allocator(), i);
  730. }
  731. break;
  732. }
  733. }
  734. }
  735. /* warning this might get screwed up with retrys/restarts */
  736. static void s_get_part_response_body_checksum_helper(
  737. struct aws_s3_checksum *running_response_sum,
  738. const struct aws_byte_cursor *body) {
  739. if (running_response_sum) {
  740. aws_checksum_update(running_response_sum, body);
  741. }
  742. }
  743. static void s_get_response_part_finish_checksum_helper(struct aws_s3_connection *connection, int error_code) {
  744. struct aws_byte_buf response_body_sum;
  745. struct aws_byte_buf encoded_response_body_sum;
  746. AWS_ZERO_STRUCT(response_body_sum);
  747. AWS_ZERO_STRUCT(encoded_response_body_sum);
  748. struct aws_s3_request *request = connection->request;
  749. if (error_code == AWS_OP_SUCCESS && request->request_level_running_response_sum) {
  750. size_t encoded_checksum_len = 0;
  751. request->did_validate = true;
  752. aws_base64_compute_encoded_len(request->request_level_running_response_sum->digest_size, &encoded_checksum_len);
  753. aws_byte_buf_init(&encoded_response_body_sum, aws_default_allocator(), encoded_checksum_len);
  754. aws_byte_buf_init(
  755. &response_body_sum, aws_default_allocator(), request->request_level_running_response_sum->digest_size);
  756. aws_checksum_finalize(request->request_level_running_response_sum, &response_body_sum, 0);
  757. struct aws_byte_cursor response_body_sum_cursor = aws_byte_cursor_from_buf(&response_body_sum);
  758. aws_base64_encode(&response_body_sum_cursor, &encoded_response_body_sum);
  759. request->checksum_match =
  760. aws_byte_buf_eq(&encoded_response_body_sum, &request->request_level_response_header_checksum);
  761. request->validation_algorithm = request->request_level_running_response_sum->algorithm;
  762. aws_byte_buf_clean_up(&response_body_sum);
  763. aws_byte_buf_clean_up(&encoded_response_body_sum);
  764. aws_checksum_destroy(request->request_level_running_response_sum);
  765. aws_byte_buf_clean_up(&request->request_level_response_header_checksum);
  766. request->request_level_running_response_sum = NULL;
  767. } else {
  768. request->did_validate = false;
  769. }
  770. }
  771. static int s_s3_meta_request_incoming_headers(
  772. struct aws_http_stream *stream,
  773. enum aws_http_header_block header_block,
  774. const struct aws_http_header *headers,
  775. size_t headers_count,
  776. void *user_data) {
  777. (void)header_block;
  778. AWS_PRECONDITION(stream);
  779. struct aws_s3_connection *connection = user_data;
  780. AWS_PRECONDITION(connection);
  781. struct aws_s3_request *request = connection->request;
  782. AWS_PRECONDITION(request);
  783. struct aws_s3_meta_request *meta_request = request->meta_request;
  784. AWS_PRECONDITION(meta_request);
  785. if (aws_http_stream_get_incoming_response_status(stream, &request->send_data.response_status)) {
  786. AWS_LOGF_ERROR(
  787. AWS_LS_S3_META_REQUEST,
  788. "id=%p Could not get incoming response status for request %p",
  789. (void *)meta_request,
  790. (void *)request);
  791. }
  792. bool successful_response =
  793. s_s3_meta_request_error_code_from_response_status(request->send_data.response_status) == AWS_ERROR_SUCCESS;
  794. if (successful_response && meta_request->checksum_config.validate_response_checksum &&
  795. request->request_tag == AWS_S3_AUTO_RANGE_GET_REQUEST_TYPE_PART) {
  796. s_get_part_response_headers_checksum_helper(connection, meta_request, headers, headers_count);
  797. }
  798. /* Only record headers if an error has taken place, or if the request_desc has asked for them. */
  799. bool should_record_headers = !successful_response || request->record_response_headers;
  800. if (should_record_headers) {
  801. if (request->send_data.response_headers == NULL) {
  802. request->send_data.response_headers = aws_http_headers_new(meta_request->allocator);
  803. }
  804. for (size_t i = 0; i < headers_count; ++i) {
  805. const struct aws_byte_cursor *name = &headers[i].name;
  806. const struct aws_byte_cursor *value = &headers[i].value;
  807. aws_http_headers_add(request->send_data.response_headers, *name, *value);
  808. }
  809. }
  810. return AWS_OP_SUCCESS;
  811. }
  812. static int s_s3_meta_request_incoming_body(
  813. struct aws_http_stream *stream,
  814. const struct aws_byte_cursor *data,
  815. void *user_data) {
  816. (void)stream;
  817. struct aws_s3_connection *connection = user_data;
  818. AWS_PRECONDITION(connection);
  819. struct aws_s3_request *request = connection->request;
  820. AWS_PRECONDITION(request);
  821. struct aws_s3_meta_request *meta_request = request->meta_request;
  822. AWS_PRECONDITION(meta_request);
  823. AWS_PRECONDITION(meta_request->vtable);
  824. AWS_LOGF_TRACE(
  825. AWS_LS_S3_META_REQUEST,
  826. "id=%p Incoming body for request %p. Response status: %d. Data Size: %" PRIu64 ". connection: %p.",
  827. (void *)meta_request,
  828. (void *)request,
  829. request->send_data.response_status,
  830. (uint64_t)data->len,
  831. (void *)connection);
  832. if (request->send_data.response_status < 200 || request->send_data.response_status > 299) {
  833. AWS_LOGF_TRACE(AWS_LS_S3_META_REQUEST, "response body: \n" PRInSTR "\n", AWS_BYTE_CURSOR_PRI(*data));
  834. }
  835. if (meta_request->checksum_config.validate_response_checksum) {
  836. s_get_part_response_body_checksum_helper(request->request_level_running_response_sum, data);
  837. }
  838. if (request->send_data.response_body.capacity == 0) {
  839. size_t buffer_size = s_dynamic_body_initial_buf_size;
  840. if (request->part_size_response_body) {
  841. buffer_size = meta_request->part_size;
  842. }
  843. aws_byte_buf_init(&request->send_data.response_body, meta_request->allocator, buffer_size);
  844. }
  845. if (aws_byte_buf_append_dynamic(&request->send_data.response_body, data)) {
  846. AWS_LOGF_ERROR(
  847. AWS_LS_S3_META_REQUEST,
  848. "id=%p: Request %p could not append to response body due to error %d (%s)",
  849. (void *)meta_request,
  850. (void *)request,
  851. aws_last_error_or_unknown(),
  852. aws_error_str(aws_last_error_or_unknown()));
  853. return AWS_OP_ERR;
  854. }
  855. return AWS_OP_SUCCESS;
  856. }
  857. /* Finish up the processing of the request work. */
  858. static void s_s3_meta_request_stream_complete(struct aws_http_stream *stream, int error_code, void *user_data) {
  859. struct aws_s3_connection *connection = user_data;
  860. AWS_PRECONDITION(connection);
  861. if (connection->request->meta_request->checksum_config.validate_response_checksum) {
  862. s_get_response_part_finish_checksum_helper(connection, error_code);
  863. }
  864. s_s3_meta_request_send_request_finish(connection, stream, error_code);
  865. }
  866. static void s_s3_meta_request_send_request_finish(
  867. struct aws_s3_connection *connection,
  868. struct aws_http_stream *stream,
  869. int error_code) {
  870. AWS_PRECONDITION(connection);
  871. struct aws_s3_request *request = connection->request;
  872. AWS_PRECONDITION(request);
  873. struct aws_s3_meta_request *meta_request = request->meta_request;
  874. AWS_PRECONDITION(meta_request);
  875. struct aws_s3_meta_request_vtable *vtable = meta_request->vtable;
  876. AWS_PRECONDITION(vtable);
  877. vtable->send_request_finish(connection, stream, error_code);
  878. }
  879. static int s_s3_meta_request_error_code_from_response_body(struct aws_s3_request *request) {
  880. AWS_PRECONDITION(request);
  881. if (request->send_data.response_body.len == 0) {
  882. /* Empty body is success */
  883. return AWS_ERROR_SUCCESS;
  884. }
  885. struct aws_byte_cursor response_body_cursor = aws_byte_cursor_from_buf(&request->send_data.response_body);
  886. bool root_name_mismatch = false;
  887. struct aws_string *error_code_string = aws_xml_get_top_level_tag_with_root_name(
  888. request->allocator, &g_code_body_xml_name, &g_error_body_xml_name, &root_name_mismatch, &response_body_cursor);
  889. if (error_code_string == NULL) {
  890. if (root_name_mismatch || aws_last_error() == AWS_ERROR_MALFORMED_INPUT_STRING) {
  891. /* The xml body is not Error, we can safely think the request succeed. */
  892. aws_reset_error();
  893. return AWS_ERROR_SUCCESS;
  894. } else {
  895. return aws_last_error();
  896. }
  897. } else {
  898. /* Check the error code. Map the S3 error code to CRT error code. */
  899. int error_code = aws_s3_crt_error_code_from_server_error_code_string(error_code_string);
  900. if (error_code == AWS_ERROR_UNKNOWN) {
  901. /* All error besides of internal error from async error are not recoverable from retry for now. */
  902. error_code = AWS_ERROR_S3_NON_RECOVERABLE_ASYNC_ERROR;
  903. }
  904. aws_string_destroy(error_code_string);
  905. return error_code;
  906. }
  907. }
  908. static void s_s3_meta_request_send_request_finish_helper(
  909. struct aws_s3_connection *connection,
  910. struct aws_http_stream *stream,
  911. int error_code,
  912. bool handle_async_error) {
  913. struct aws_s3_request *request = connection->request;
  914. AWS_PRECONDITION(request);
  915. struct aws_s3_meta_request *meta_request = request->meta_request;
  916. AWS_PRECONDITION(meta_request);
  917. struct aws_s3_client *client = meta_request->client;
  918. AWS_PRECONDITION(client);
  919. int response_status = request->send_data.response_status;
  920. /* If our error code is currently success, then we have some other calls to make that could still indicate a
  921. * failure. */
  922. if (error_code == AWS_ERROR_SUCCESS) {
  923. if (handle_async_error && response_status == AWS_HTTP_STATUS_CODE_200_OK) {
  924. error_code = s_s3_meta_request_error_code_from_response_body(request);
  925. } else {
  926. error_code = s_s3_meta_request_error_code_from_response_status(response_status);
  927. }
  928. if (error_code != AWS_ERROR_SUCCESS) {
  929. aws_raise_error(error_code);
  930. }
  931. }
  932. AWS_LOGF_DEBUG(
  933. AWS_LS_S3_META_REQUEST,
  934. "id=%p: Request %p finished with error code %d (%s) and response status %d",
  935. (void *)meta_request,
  936. (void *)request,
  937. error_code,
  938. aws_error_debug_str(error_code),
  939. response_status);
  940. enum aws_s3_connection_finish_code finish_code = AWS_S3_CONNECTION_FINISH_CODE_FAILED;
  941. if (error_code == AWS_ERROR_SUCCESS) {
  942. if (connection->request->meta_request->type == AWS_S3_META_REQUEST_TYPE_GET_OBJECT && request->did_validate &&
  943. !request->checksum_match) {
  944. finish_code = AWS_S3_CONNECTION_FINISH_CODE_FAILED;
  945. error_code = AWS_ERROR_S3_RESPONSE_CHECKSUM_MISMATCH;
  946. AWS_LOGF_ERROR(
  947. AWS_LS_S3_META_REQUEST,
  948. "id=%p Meta request cannot recover from checksum mismatch. (request=%p, response status=%d)",
  949. (void *)meta_request,
  950. (void *)request,
  951. response_status);
  952. } else {
  953. finish_code = AWS_S3_CONNECTION_FINISH_CODE_SUCCESS;
  954. }
  955. } else {
  956. /* BEGIN CRITICAL SECTION */
  957. aws_s3_meta_request_lock_synced_data(meta_request);
  958. bool meta_request_finishing = aws_s3_meta_request_has_finish_result_synced(meta_request);
  959. aws_s3_meta_request_unlock_synced_data(meta_request);
  960. /* END CRITICAL SECTION */
  961. /* If the request failed due to an invalid (ie: unrecoverable) response status, or the meta request already
  962. * has a result, then make sure that this request isn't retried. */
  963. if (error_code == AWS_ERROR_S3_INVALID_RESPONSE_STATUS ||
  964. error_code == AWS_ERROR_S3_NON_RECOVERABLE_ASYNC_ERROR || meta_request_finishing) {
  965. finish_code = AWS_S3_CONNECTION_FINISH_CODE_FAILED;
  966. AWS_LOGF_ERROR(
  967. AWS_LS_S3_META_REQUEST,
  968. "id=%p Meta request cannot recover from error %d (%s). (request=%p, response status=%d)",
  969. (void *)meta_request,
  970. error_code,
  971. aws_error_str(error_code),
  972. (void *)request,
  973. response_status);
  974. } else {
  975. AWS_LOGF_ERROR(
  976. AWS_LS_S3_META_REQUEST,
  977. "id=%p Meta request failed from error %d (%s). (request=%p, response status=%d). Try to setup a "
  978. "retry.",
  979. (void *)meta_request,
  980. error_code,
  981. aws_error_str(error_code),
  982. (void *)request,
  983. response_status);
  984. /* Otherwise, set this up for a retry if the meta request is active. */
  985. finish_code = AWS_S3_CONNECTION_FINISH_CODE_RETRY;
  986. }
  987. }
  988. if (stream != NULL) {
  989. aws_http_stream_release(stream);
  990. stream = NULL;
  991. }
  992. aws_s3_client_notify_connection_finished(client, connection, error_code, finish_code);
  993. }
  994. void aws_s3_meta_request_send_request_finish_default(
  995. struct aws_s3_connection *connection,
  996. struct aws_http_stream *stream,
  997. int error_code) {
  998. s_s3_meta_request_send_request_finish_helper(connection, stream, error_code, false /*async error*/);
  999. }
  1000. void aws_s3_meta_request_send_request_finish_handle_async_error(
  1001. struct aws_s3_connection *connection,
  1002. struct aws_http_stream *stream,
  1003. int error_code) {
  1004. s_s3_meta_request_send_request_finish_helper(connection, stream, error_code, true /*async error*/);
  1005. }
  1006. void aws_s3_meta_request_finished_request(
  1007. struct aws_s3_meta_request *meta_request,
  1008. struct aws_s3_request *request,
  1009. int error_code) {
  1010. AWS_PRECONDITION(meta_request);
  1011. AWS_PRECONDITION(meta_request->vtable);
  1012. AWS_PRECONDITION(meta_request->vtable->finished_request);
  1013. meta_request->vtable->finished_request(meta_request, request, error_code);
  1014. }
  1015. struct s3_stream_response_body_payload {
  1016. struct aws_s3_meta_request *meta_request;
  1017. struct aws_linked_list requests;
  1018. struct aws_task task;
  1019. };
  1020. /* Pushes a request into the body streaming priority queue. Derived meta request types should not call this--they
  1021. * should instead call aws_s3_meta_request_stream_response_body_synced.*/
  1022. static void s_s3_meta_request_body_streaming_push_synced(
  1023. struct aws_s3_meta_request *meta_request,
  1024. struct aws_s3_request *request);
  1025. /* Pops the next available request from the body streaming priority queue. If the parts previous the next request in
  1026. * the priority queue have not been placed in the priority queue yet, the priority queue will remain the same, and
  1027. * NULL will be returned. (Should not be needed to be called by derived types.) */
  1028. static struct aws_s3_request *s_s3_meta_request_body_streaming_pop_next_synced(
  1029. struct aws_s3_meta_request *meta_request);
  1030. static void s_s3_meta_request_body_streaming_task(struct aws_task *task, void *arg, enum aws_task_status task_status);
  1031. void aws_s3_meta_request_stream_response_body_synced(
  1032. struct aws_s3_meta_request *meta_request,
  1033. struct aws_s3_request *request) {
  1034. ASSERT_SYNCED_DATA_LOCK_HELD(meta_request);
  1035. AWS_PRECONDITION(meta_request);
  1036. AWS_PRECONDITION(request);
  1037. AWS_PRECONDITION(request->part_number > 0);
  1038. struct aws_linked_list streaming_requests;
  1039. aws_linked_list_init(&streaming_requests);
  1040. /* Push it into the priority queue. */
  1041. s_s3_meta_request_body_streaming_push_synced(meta_request, request);
  1042. struct aws_s3_client *client = meta_request->client;
  1043. AWS_PRECONDITION(client);
  1044. aws_atomic_fetch_add(&client->stats.num_requests_stream_queued_waiting, 1);
  1045. /* Grab the next request that can be streamed back to the caller. */
  1046. struct aws_s3_request *next_streaming_request = s_s3_meta_request_body_streaming_pop_next_synced(meta_request);
  1047. uint32_t num_streaming_requests = 0;
  1048. /* Grab any additional requests that could be streamed to the caller. */
  1049. while (next_streaming_request != NULL) {
  1050. aws_atomic_fetch_sub(&client->stats.num_requests_stream_queued_waiting, 1);
  1051. aws_linked_list_push_back(&streaming_requests, &next_streaming_request->node);
  1052. ++num_streaming_requests;
  1053. next_streaming_request = s_s3_meta_request_body_streaming_pop_next_synced(meta_request);
  1054. }
  1055. if (aws_linked_list_empty(&streaming_requests)) {
  1056. return;
  1057. }
  1058. aws_atomic_fetch_add(&client->stats.num_requests_streaming, num_streaming_requests);
  1059. meta_request->synced_data.num_parts_delivery_sent += num_streaming_requests;
  1060. struct s3_stream_response_body_payload *payload =
  1061. aws_mem_calloc(client->allocator, 1, sizeof(struct s3_stream_response_body_payload));
  1062. aws_s3_meta_request_acquire(meta_request);
  1063. payload->meta_request = meta_request;
  1064. aws_linked_list_init(&payload->requests);
  1065. aws_linked_list_swap_contents(&payload->requests, &streaming_requests);
  1066. aws_task_init(
  1067. &payload->task, s_s3_meta_request_body_streaming_task, payload, "s_s3_meta_request_body_streaming_task");
  1068. aws_event_loop_schedule_task_now(meta_request->io_event_loop, &payload->task);
  1069. }
  1070. static void s_s3_meta_request_body_streaming_task(struct aws_task *task, void *arg, enum aws_task_status task_status) {
  1071. (void)task;
  1072. (void)task_status;
  1073. struct s3_stream_response_body_payload *payload = arg;
  1074. AWS_PRECONDITION(payload);
  1075. struct aws_s3_meta_request *meta_request = payload->meta_request;
  1076. AWS_PRECONDITION(meta_request);
  1077. AWS_PRECONDITION(meta_request->vtable);
  1078. struct aws_s3_client *client = meta_request->client;
  1079. AWS_PRECONDITION(client);
  1080. /* Client owns this event loop group. A cancel should not be possible. */
  1081. AWS_ASSERT(task_status == AWS_TASK_STATUS_RUN_READY);
  1082. struct aws_linked_list completed_requests;
  1083. aws_linked_list_init(&completed_requests);
  1084. int error_code = AWS_ERROR_SUCCESS;
  1085. uint32_t num_successful = 0;
  1086. uint32_t num_failed = 0;
  1087. while (!aws_linked_list_empty(&payload->requests)) {
  1088. struct aws_linked_list_node *request_node = aws_linked_list_pop_front(&payload->requests);
  1089. struct aws_s3_request *request = AWS_CONTAINER_OF(request_node, struct aws_s3_request, node);
  1090. AWS_ASSERT(meta_request == request->meta_request);
  1091. struct aws_byte_cursor body_buffer_byte_cursor = aws_byte_cursor_from_buf(&request->send_data.response_body);
  1092. AWS_ASSERT(request->part_number >= 1);
  1093. if (aws_s3_meta_request_has_finish_result(meta_request)) {
  1094. ++num_failed;
  1095. } else {
  1096. if (body_buffer_byte_cursor.len > 0 && error_code == AWS_ERROR_SUCCESS && meta_request->body_callback &&
  1097. meta_request->body_callback(
  1098. meta_request, &body_buffer_byte_cursor, request->part_range_start, meta_request->user_data)) {
  1099. error_code = aws_last_error_or_unknown();
  1100. }
  1101. if (error_code == AWS_ERROR_SUCCESS) {
  1102. ++num_successful;
  1103. } else {
  1104. ++num_failed;
  1105. }
  1106. }
  1107. aws_atomic_fetch_sub(&client->stats.num_requests_streaming, 1);
  1108. aws_s3_request_release(request);
  1109. }
  1110. /* BEGIN CRITICAL SECTION */
  1111. {
  1112. aws_s3_meta_request_lock_synced_data(meta_request);
  1113. if (error_code != AWS_ERROR_SUCCESS) {
  1114. aws_s3_meta_request_set_fail_synced(meta_request, NULL, error_code);
  1115. }
  1116. meta_request->synced_data.num_parts_delivery_completed += (num_failed + num_successful);
  1117. meta_request->synced_data.num_parts_delivery_failed += num_failed;
  1118. meta_request->synced_data.num_parts_delivery_succeeded += num_successful;
  1119. aws_s3_meta_request_unlock_synced_data(meta_request);
  1120. }
  1121. /* END CRITICAL SECTION */
  1122. aws_mem_release(client->allocator, payload);
  1123. payload = NULL;
  1124. aws_s3_client_schedule_process_work(client);
  1125. aws_s3_meta_request_release(meta_request);
  1126. }
  1127. static void s_s3_meta_request_body_streaming_push_synced(
  1128. struct aws_s3_meta_request *meta_request,
  1129. struct aws_s3_request *request) {
  1130. ASSERT_SYNCED_DATA_LOCK_HELD(meta_request);
  1131. AWS_PRECONDITION(meta_request);
  1132. AWS_PRECONDITION(request);
  1133. AWS_ASSERT(request->meta_request == meta_request);
  1134. aws_s3_request_acquire(request);
  1135. aws_priority_queue_push(&meta_request->synced_data.pending_body_streaming_requests, &request);
  1136. }
  1137. static struct aws_s3_request *s_s3_meta_request_body_streaming_pop_next_synced(
  1138. struct aws_s3_meta_request *meta_request) {
  1139. AWS_PRECONDITION(meta_request);
  1140. ASSERT_SYNCED_DATA_LOCK_HELD(meta_request);
  1141. if (0 == aws_priority_queue_size(&meta_request->synced_data.pending_body_streaming_requests)) {
  1142. return NULL;
  1143. }
  1144. struct aws_s3_request **top_request = NULL;
  1145. aws_priority_queue_top(&meta_request->synced_data.pending_body_streaming_requests, (void **)&top_request);
  1146. AWS_ASSERT(top_request);
  1147. AWS_FATAL_ASSERT(*top_request);
  1148. if ((*top_request)->part_number != meta_request->synced_data.next_streaming_part) {
  1149. return NULL;
  1150. }
  1151. struct aws_s3_request *request = NULL;
  1152. aws_priority_queue_pop(&meta_request->synced_data.pending_body_streaming_requests, (void **)&request);
  1153. ++meta_request->synced_data.next_streaming_part;
  1154. return request;
  1155. }
  1156. void aws_s3_meta_request_finish(struct aws_s3_meta_request *meta_request) {
  1157. AWS_PRECONDITION(meta_request);
  1158. AWS_PRECONDITION(meta_request->vtable);
  1159. AWS_PRECONDITION(meta_request->vtable->finish);
  1160. meta_request->vtable->finish(meta_request);
  1161. }
  1162. void aws_s3_meta_request_finish_default(struct aws_s3_meta_request *meta_request) {
  1163. AWS_PRECONDITION(meta_request);
  1164. bool already_finished = false;
  1165. struct aws_linked_list release_request_list;
  1166. aws_linked_list_init(&release_request_list);
  1167. struct aws_s3_meta_request_result finish_result;
  1168. AWS_ZERO_STRUCT(finish_result);
  1169. /* BEGIN CRITICAL SECTION */
  1170. {
  1171. aws_s3_meta_request_lock_synced_data(meta_request);
  1172. if (meta_request->synced_data.state == AWS_S3_META_REQUEST_STATE_FINISHED) {
  1173. already_finished = true;
  1174. goto unlock;
  1175. }
  1176. meta_request->synced_data.state = AWS_S3_META_REQUEST_STATE_FINISHED;
  1177. /* Clean out the pending-stream-to-caller priority queue*/
  1178. while (aws_priority_queue_size(&meta_request->synced_data.pending_body_streaming_requests) > 0) {
  1179. struct aws_s3_request *request = NULL;
  1180. aws_priority_queue_pop(&meta_request->synced_data.pending_body_streaming_requests, (void **)&request);
  1181. AWS_FATAL_ASSERT(request != NULL);
  1182. aws_linked_list_push_back(&release_request_list, &request->node);
  1183. }
  1184. finish_result = meta_request->synced_data.finish_result;
  1185. AWS_ZERO_STRUCT(meta_request->synced_data.finish_result);
  1186. unlock:
  1187. aws_s3_meta_request_unlock_synced_data(meta_request);
  1188. }
  1189. /* END CRITICAL SECTION */
  1190. if (already_finished) {
  1191. return;
  1192. }
  1193. while (!aws_linked_list_empty(&release_request_list)) {
  1194. struct aws_linked_list_node *request_node = aws_linked_list_pop_front(&release_request_list);
  1195. struct aws_s3_request *release_request = AWS_CONTAINER_OF(request_node, struct aws_s3_request, node);
  1196. AWS_FATAL_ASSERT(release_request != NULL);
  1197. aws_s3_request_release(release_request);
  1198. }
  1199. if (meta_request->headers_callback && finish_result.error_response_headers) {
  1200. if (meta_request->headers_callback(
  1201. meta_request,
  1202. finish_result.error_response_headers,
  1203. finish_result.response_status,
  1204. meta_request->user_data)) {
  1205. finish_result.error_code = aws_last_error_or_unknown();
  1206. }
  1207. meta_request->headers_callback = NULL;
  1208. }
  1209. AWS_LOGF_DEBUG(
  1210. AWS_LS_S3_META_REQUEST,
  1211. "id=%p Meta request finished with error code %d (%s)",
  1212. (void *)meta_request,
  1213. finish_result.error_code,
  1214. aws_error_str(finish_result.error_code));
  1215. /* As the meta request has been finished with any HTTP message, we can safely release the http message that hold. So
  1216. * that, the downstream high level language doesn't need to wait for shutdown to clean related resource (eg: input
  1217. * stream) */
  1218. if (meta_request->initial_request_message) {
  1219. aws_http_message_release(meta_request->initial_request_message);
  1220. meta_request->initial_request_message = NULL;
  1221. }
  1222. if (meta_request->finish_callback != NULL) {
  1223. meta_request->finish_callback(meta_request, &finish_result, meta_request->user_data);
  1224. }
  1225. aws_s3_meta_request_result_clean_up(meta_request, &finish_result);
  1226. aws_s3_endpoint_release(meta_request->endpoint);
  1227. meta_request->endpoint = NULL;
  1228. meta_request->io_event_loop = NULL;
  1229. }
  1230. int aws_s3_meta_request_read_body(struct aws_s3_meta_request *meta_request, struct aws_byte_buf *buffer) {
  1231. AWS_PRECONDITION(meta_request);
  1232. AWS_PRECONDITION(buffer);
  1233. struct aws_input_stream *initial_body_stream =
  1234. aws_http_message_get_body_stream(meta_request->initial_request_message);
  1235. AWS_FATAL_ASSERT(initial_body_stream);
  1236. /* Copy it into our buffer. */
  1237. if (aws_input_stream_read(initial_body_stream, buffer)) {
  1238. AWS_LOGF_ERROR(AWS_LS_S3_META_REQUEST, "id=%p Could not read from body stream.", (void *)meta_request);
  1239. return AWS_OP_ERR;
  1240. }
  1241. return AWS_OP_SUCCESS;
  1242. }
  1243. void aws_s3_meta_request_result_setup(
  1244. struct aws_s3_meta_request *meta_request,
  1245. struct aws_s3_meta_request_result *result,
  1246. struct aws_s3_request *request,
  1247. int response_status,
  1248. int error_code) {
  1249. if (request != NULL) {
  1250. if (request->send_data.response_headers != NULL) {
  1251. result->error_response_headers = request->send_data.response_headers;
  1252. aws_http_headers_acquire(result->error_response_headers);
  1253. }
  1254. if (request->send_data.response_body.capacity > 0) {
  1255. result->error_response_body = aws_mem_calloc(meta_request->allocator, 1, sizeof(struct aws_byte_buf));
  1256. aws_byte_buf_init_copy(
  1257. result->error_response_body, meta_request->allocator, &request->send_data.response_body);
  1258. }
  1259. }
  1260. result->response_status = response_status;
  1261. result->error_code = error_code;
  1262. }
  1263. void aws_s3_meta_request_result_clean_up(
  1264. struct aws_s3_meta_request *meta_request,
  1265. struct aws_s3_meta_request_result *result) {
  1266. AWS_PRECONDITION(meta_request);
  1267. AWS_PRECONDITION(result);
  1268. aws_http_headers_release(result->error_response_headers);
  1269. if (result->error_response_body != NULL) {
  1270. aws_byte_buf_clean_up(result->error_response_body);
  1271. aws_mem_release(meta_request->allocator, result->error_response_body);
  1272. }
  1273. AWS_ZERO_STRUCT(*result);
  1274. }
  1275. bool aws_s3_meta_request_checksum_config_has_algorithm(
  1276. struct aws_s3_meta_request *meta_request,
  1277. enum aws_s3_checksum_algorithm algorithm) {
  1278. AWS_PRECONDITION(meta_request);
  1279. switch (algorithm) {
  1280. case AWS_SCA_CRC32C:
  1281. return meta_request->checksum_config.response_checksum_algorithms.crc32c;
  1282. case AWS_SCA_CRC32:
  1283. return meta_request->checksum_config.response_checksum_algorithms.crc32;
  1284. case AWS_SCA_SHA1:
  1285. return meta_request->checksum_config.response_checksum_algorithms.sha1;
  1286. case AWS_SCA_SHA256:
  1287. return meta_request->checksum_config.response_checksum_algorithms.sha256;
  1288. default:
  1289. return false;
  1290. }
  1291. }