Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ms3_get_content_type() #128

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/api/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/marias3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -761,4 +761,3 @@ const char *ms3_get_content_type(ms3_st *ms3)
}
return ms3->content_type_in;
}
#endif
51 changes: 35 additions & 16 deletions src/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <curl/curl.h>
#include <curl/easy.h>
#include <math.h>
#include <ctype.h>

const char *default_domain = "s3.amazonaws.com";

Expand Down Expand Up @@ -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);

Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
12 changes: 1 addition & 11 deletions tests/content_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,7 +66,7 @@ int main(int argc, char *argv[])

if (s3port)
{
int port = atol(s3port);
int port = atoi(s3port);
ms3_set_option(ms3, MS3_OPT_PORT_NUMBER, &port);
}

Expand Down Expand Up @@ -155,4 +146,3 @@ int main(int argc, char *argv[])
ms3_library_deinit();
return 0;
}
#endif