From e9c376174d6ecb35a5b5542aff75553585c49963 Mon Sep 17 00:00:00 2001 From: nekobit Date: Thu, 23 Jun 2022 06:22:15 +0000 Subject: [PATCH] Mstdnt args FossilOrigin-Name: 878f7711521cefffe0188a78d6217f3205e2cd98414e0cd3bc4a5393ae74d5d9 --- include/mastodont_fetch.h | 1 + include/mastodont_query.h | 1 + include/mastodont_request.h | 4 +++- include/mastodont_types.h | 14 +++++++++----- src/fetch.c | 11 ++++++----- src/list.c | 4 ++-- src/query.c | 5 +++-- src/request.c | 14 +++++++++++--- 8 files changed, 36 insertions(+), 18 deletions(-) diff --git a/include/mastodont_fetch.h b/include/mastodont_fetch.h index 7a3f97f..7372abb 100644 --- a/include/mastodont_fetch.h +++ b/include/mastodont_fetch.h @@ -28,6 +28,7 @@ struct mstdnt_fetch_results size_t mstdnt_curl_write_callback(char* ptr, size_t _size, size_t nmemb, void* _content); void mastodont_fetch_results_cleanup(struct mstdnt_fetch_results* res); int mastodont_fetch_curl(mastodont_t* mstdnt, + struct mstdnt_args* args, char* url, struct mstdnt_fetch_results* results, CURLoption request_t, diff --git a/include/mastodont_query.h b/include/mastodont_query.h index 8783ed3..7624272 100644 --- a/include/mastodont_query.h +++ b/include/mastodont_query.h @@ -45,6 +45,7 @@ struct _mstdnt_query_param }; char* _mstdnt_query_string(mastodont_t* data, + struct mstdnt_args* args, char* src, struct _mstdnt_query_param* params, size_t param_len); diff --git a/include/mastodont_request.h b/include/mastodont_request.h index f6efa37..8467e82 100644 --- a/include/mastodont_request.h +++ b/include/mastodont_request.h @@ -34,6 +34,8 @@ struct mastodont_request_args int (*callback)(cJSON*, void*); }; -int mastodont_request(mastodont_t* data, struct mastodont_request_args* args); +int mastodont_request(mastodont_t* data, + struct mstdnt_args* m_args, + struct mastodont_request_args* args); #endif /* MASTODONT_REQUEST_H */ diff --git a/include/mastodont_types.h b/include/mastodont_types.h index bdda2c4..23d53e4 100644 --- a/include/mastodont_types.h +++ b/include/mastodont_types.h @@ -24,18 +24,15 @@ #define MSTDNT_URISIZE 512 typedef unsigned char mstdnt_bool; +// It's more logical to not sanitize than to sanitize data #define MSTDNT_FLAG_NO_URI_SANITIZE (1<<0) -#define MSTDNT_FLAG_SSL_UNVERIFIED (1<<2) +#define MSTDNT_FLAG_SSL_UNVERIFIED (1<<1) #define MSTDNT_FLAG_ISSET(flags, flag) (((flags) & (flag)) == (flag)) #define MSTDNT_T_FLAG_ISSET(flag_ref, flag) (((flag_ref->flags) & (flag)) == (flag)) typedef struct mastodont { - char* url; CURL* curl; - char* token; - mstdnt_bool token_heap; - uint16_t flags; } mastodont_t; struct mstdnt_storage @@ -46,6 +43,13 @@ struct mstdnt_storage char* error_description; }; +struct mstdnt_args +{ + char* url; + char* token; + uint8_t flags; +}; + struct mstdnt_file { char* file; diff --git a/src/fetch.c b/src/fetch.c index 5e7e35c..c73bf95 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -45,6 +45,7 @@ void mastodont_fetch_results_cleanup(struct mstdnt_fetch_results* res) #define TOKEN_STR_SIZE 512 int mastodont_fetch_curl(mastodont_t* mstdnt, + struct mstdnt_args* m_args, char* _url, struct mstdnt_fetch_results* results, CURLoption request_t, @@ -57,14 +58,14 @@ int mastodont_fetch_curl(mastodont_t* mstdnt, /* Setup URL */ char url[MSTDNT_URLSIZE] = { 0 }; - strncpy(url, mstdnt->url, MSTDNT_URLSIZE-1); + strncpy(url, m_args->url, MSTDNT_URLSIZE-1); strncat(url, _url, MSTDNT_URLSIZE-1); /* Setup token */ - if (mstdnt->token) + if (m_args->token) { snprintf(token, TOKEN_STR_SIZE, "Authorization: Bearer %s", - mstdnt->token); + m_args->token); list = curl_slist_append(list, token); curl_easy_setopt(mstdnt->curl, CURLOPT_HTTPHEADER, list); } @@ -75,9 +76,9 @@ int mastodont_fetch_curl(mastodont_t* mstdnt, curl_easy_setopt(mstdnt->curl, CURLOPT_WRITEDATA, results); /* Should we verify the peer's SSL cert? */ curl_easy_setopt(mstdnt->curl, CURLOPT_SSL_VERIFYPEER, - !MSTDNT_T_FLAG_ISSET(mstdnt, MSTDNT_FLAG_SSL_UNVERIFIED)); + !MSTDNT_T_FLAG_ISSET(m_args, MSTDNT_FLAG_SSL_UNVERIFIED)); curl_easy_setopt(mstdnt->curl, CURLOPT_SSL_VERIFYHOST, - !MSTDNT_T_FLAG_ISSET(mstdnt, MSTDNT_FLAG_SSL_UNVERIFIED)); + !MSTDNT_T_FLAG_ISSET(m_args, MSTDNT_FLAG_SSL_UNVERIFIED)); /* PUT, POST, GET, Custom */ /* Mimes are expected to be set beforehand manually */ if (is_custom) diff --git a/src/list.c b/src/list.c index b0566fa..7fa66cb 100644 --- a/src/list.c +++ b/src/list.c @@ -175,8 +175,8 @@ int mastodont_update_list(mastodont_t* data, url, NULL, 0, params, _mstdnt_arr_len(params), - CURLOPT_PUT, - NULL, + CURLOPT_CUSTOMREQUEST, + "PUT", list, mstdnt_list_json_callback }; diff --git a/src/query.c b/src/query.c index a286483..37bedb3 100644 --- a/src/query.c +++ b/src/query.c @@ -21,6 +21,7 @@ /* TODO audit this function for overflows */ char* _mstdnt_query_string(mastodont_t* data, + struct mstdnt_args* args, char* src, struct _mstdnt_query_param* params, size_t param_len) @@ -102,7 +103,7 @@ char* _mstdnt_query_string(mastodont_t* data, else /* Point to it, it's a string */ { /* First, let's encode it */ - escape_str = MSTDNT_T_FLAG_ISSET(data, MSTDNT_FLAG_NO_URI_SANITIZE) ? + escape_str = MSTDNT_T_FLAG_ISSET(args, MSTDNT_FLAG_NO_URI_SANITIZE) ? params[i].value.s : curl_easy_escape(data->curl, params[i].value.s, 0); val_ptr = escape_str; } @@ -133,7 +134,7 @@ char* _mstdnt_query_string(mastodont_t* data, result[res_prev + key_len] = '='; strcpy(result + res_prev + 1 + key_len, val_ptr); /* Only free if flag is set, meaning it needs to be free'd */ - if (!MSTDNT_T_FLAG_ISSET(data, MSTDNT_FLAG_NO_URI_SANITIZE)) + if (!MSTDNT_T_FLAG_ISSET(args, MSTDNT_FLAG_NO_URI_SANITIZE)) curl_free(escape_str); } diff --git a/src/request.c b/src/request.c index 15e6483..38a8130 100644 --- a/src/request.c +++ b/src/request.c @@ -70,7 +70,9 @@ static void mime_params_post(curl_mime* mime, } -int mastodont_request(mastodont_t* data, struct mastodont_request_args* args) +int mastodont_request(mastodont_t* data, + struct mstdnt_args* m_args, + struct mastodont_request_args* args) { int res = 0, curlerror = 0; struct mstdnt_storage* storage = args->storage; @@ -87,7 +89,9 @@ int mastodont_request(mastodont_t* data, struct mastodont_request_args* args) memset(storage, 0, sizeof(struct mstdnt_storage)); storage->needs_cleanup = 0; - if (args->params_post && args->request_type == CURLOPT_POST) + if (args->params_post && + (args->request_type == CURLOPT_POST || + args->request_type == CURLOPT_CUSTOMREQUEST)) { post = _mstdnt_query_string(data, NULL, args->params_post, args->params_post_len); curl_easy_setopt(data->curl, CURLOPT_POSTFIELDS, post); @@ -106,7 +110,11 @@ int mastodont_request(mastodont_t* data, struct mastodont_request_args* args) else if (args->request_type == CURLOPT_POST) curl_easy_setopt(data->curl, CURLOPT_POSTFIELDS, ""); - curlerror = mastodont_fetch_curl(data, url_query, &results, args->request_type, + curlerror = mastodont_fetch_curl(data, + m_args, + url_query, + &results, + args->request_type, args->request_type_custom); if (mime) curl_mime_free(mime);