Mstdnt args

FossilOrigin-Name: 878f7711521cefffe0188a78d6217f3205e2cd98414e0cd3bc4a5393ae74d5d9
This commit is contained in:
nekobit 2022-06-23 06:22:15 +00:00
parent 98917fcfcb
commit e9c376174d
8 changed files with 36 additions and 18 deletions

View file

@ -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,

View file

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

View file

@ -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 */

View file

@ -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;

View file

@ -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)

View file

@ -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
};

View file

@ -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);
}

View file

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