From 8e82944be5af0bb43f3717f33e86d5cd9bcd3e88 Mon Sep 17 00:00:00 2001 From: Andrew Hutchings Date: Wed, 11 Sep 2024 15:07:49 +0100 Subject: [PATCH] Fix ms3_get_content_type() Now uses the legacy curl method of header callbacks. #ifdef guard removed. --- docs/api/functions.rst | 7 +++--- src/marias3.c | 3 +-- src/request.c | 51 +++++++++++++++++++++++++++++------------- src/structs.h | 4 +--- tests/content_type.c | 10 --------- 5 files changed, 41 insertions(+), 34 deletions(-) diff --git a/docs/api/functions.rst b/docs/api/functions.rst index a5eef29..abc7c07 100644 --- a/docs/api/functions.rst +++ b/docs/api/functions.rst @@ -410,9 +410,10 @@ ms3_get_content_type() .. c:function:: const char *ms3_get_content_type(ms3_st *ms3) - Gets the ``Content-Type:`` header for the previous response from the S3 server. - This will automatically freed as part of the request handling, it will lose - scope on the next request and should not be freed by the application. + Gets the ``Content-Type:`` header for the previous GET request response + from the S3 server. + The memory for this is part of the :c:type:`ms3_st` object and should not be + freed by the application. The contents will be reset on each GET request. :param ms3: The marias3 object :param content_type: The mime type for the previous request diff --git a/src/marias3.c b/src/marias3.c index af9f28a..499271d 100644 --- a/src/marias3.c +++ b/src/marias3.c @@ -752,7 +752,7 @@ void ms3_set_content_type(ms3_st *ms3, const char *content_type) ms3->content_type_out = content_type; } -#ifdef HAVE_NEW_CURL_API + const char *ms3_get_content_type(ms3_st *ms3) { if (!ms3) @@ -761,4 +761,3 @@ const char *ms3_get_content_type(ms3_st *ms3) } return ms3->content_type_in; } -#endif diff --git a/src/request.c b/src/request.c index 5b574cf..f19584f 100644 --- a/src/request.c +++ b/src/request.c @@ -25,6 +25,7 @@ #include #include #include +#include const char *default_domain = "s3.amazonaws.com"; @@ -644,8 +645,8 @@ static uint8_t build_request_headers(CURL *curl, struct curl_slist **head, return 0; } -static size_t header_callback(char *buffer, size_t size, - size_t nitems, void *userdata) +static size_t head_header_callback(char *buffer, size_t size, + size_t nitems, void *userdata) { ms3debug("%.*s\n", (int)(nitems * size), buffer); @@ -671,6 +672,30 @@ static size_t header_callback(char *buffer, size_t size, return nitems * size; } +static size_t get_header_callback(char *buffer, size_t size, + size_t nitems, void *userdata) +{ + ms3debug("%.*s\n", (int)(nitems * size), buffer); + + if (userdata) + { + if (!strncasecmp(buffer, "Content-Type", 12)) + { + size_t i; + ms3_st *ms3 = (ms3_st *) userdata; + snprintf(ms3->content_type_in, 127, "%s", (char*)buffer + 14); + for (i = 0; i < strlen(ms3->content_type_in); i++) + { + if (isspace( (unsigned char) ms3->content_type_in[i] )) + break; + } + ms3->content_type_in[i] = '\0'; + } + } + + return nitems * size; +} + static size_t body_callback(void *buffer, size_t size, size_t nitems, void *userdata) { @@ -717,10 +742,6 @@ uint8_t execute_request(ms3_st *ms3, command_t cmd, const char *bucket, void *ret_ptr) { CURL *curl = NULL; -#ifdef HAVE_NEW_CURL_API - CURLHcode curl_hret; - struct curl_header *content_type_in; -#endif struct curl_slist *headers = NULL; uint8_t res = 0; struct memory_buffer_st mem; @@ -788,11 +809,18 @@ uint8_t execute_request(ms3_st *ms3, command_t cmd, const char *bucket, case MS3_CMD_HEAD: method = MS3_HEAD; curl_easy_setopt(curl, CURLOPT_HEADERDATA, ret_ptr); + curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, head_header_callback); + break; + + case MS3_CMD_GET: + ms3->content_type_in[0] = '\0'; + curl_easy_setopt(curl, CURLOPT_HEADERDATA, ms3); + curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, get_header_callback); + method = MS3_GET; break; case MS3_CMD_LIST: case MS3_CMD_LIST_RECURSIVE: - case MS3_CMD_GET: case MS3_CMD_LIST_ROLE: method = MS3_GET; break; @@ -872,7 +900,6 @@ uint8_t execute_request(ms3_st *ms3, command_t cmd, const char *bucket, } curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, ms3->buffer_chunk_size); - curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); curl_res = curl_easy_perform(curl); @@ -885,14 +912,6 @@ uint8_t execute_request(ms3_st *ms3, command_t cmd, const char *bucket, return MS3_ERR_REQUEST_ERROR; } -#ifdef HAVE_NEW_CURL_API - curl_hret = curl_easy_header(curl, "content-type", 0, CURLH_HEADER, -1, - &content_type_in); - if (!curl_hret && content_type_in) - { - ms3->content_type_in = content_type_in->value; - } -#endif curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); ms3debug("Response code: %ld", response_code); diff --git a/src/structs.h b/src/structs.h index 6a128c4..5df7f5a 100644 --- a/src/structs.h +++ b/src/structs.h @@ -70,9 +70,7 @@ struct ms3_st void *read_cb; void *user_data; const char *content_type_out; -#ifdef HAVE_NEW_CURL_API - const char *content_type_in; -#endif + char content_type_in[128]; // max length allowed for mime types struct ms3_list_container_st list_container; }; diff --git a/tests/content_type.c b/tests/content_type.c index 84890a5..724aa6e 100644 --- a/tests/content_type.c +++ b/tests/content_type.c @@ -22,15 +22,6 @@ /* Tests basic put, list, get, status, delete using the thread calls */ -#ifndef HAVE_NEW_CURL_API -int main(int argc, char *argv[]) -{ - (void) argc; - (void) argv; - SKIP_IF_(1, "Requires HAVE_NEW_CURL_API to be defined"); - return 0; -} -#else int main(int argc, char *argv[]) { int res; @@ -155,4 +146,3 @@ int main(int argc, char *argv[]) ms3_library_deinit(); return 0; } -#endif