Mastodont flags

FossilOrigin-Name: 14aa1ded0d597377b0ca78979e133b164cd2ae067eb4a4b149313f1da51f56c9
This commit is contained in:
me@ow.nekobit.net 2022-03-08 04:50:53 +00:00
parent 5d9f310891
commit 9d3ad8297a
6 changed files with 21 additions and 6 deletions

View file

@ -24,7 +24,7 @@
void mastodont_global_curl_init();
void mastodont_global_curl_cleanup();
int mastodont_init(mastodont_t* data);
int mastodont_init(mastodont_t* data, uint16_t flags);
int mastodont_set_token(mastodont_t* data, char* token);
void mastodont_free(mastodont_t* data);

View file

@ -17,18 +17,28 @@
#define MASTODONT_TYPES_H
#include <curl/curl.h>
#include <cjson/cJSON.h>
#include <stdint.h>
#define _mstdnt_arr_len(arr) (sizeof(arr)/sizeof(arr[0]))
#define MSTDNT_URLSIZE 2048
#define MSTDNT_URISIZE 512
typedef unsigned char mstdnt_bool;
#define MSTDNT_FLAG_NO_URI_SANITIZE (1<<0)
#define MSTDNT_FLAG_SSL_NONE (1<<1)
#define MSTDNT_FLAG_SSL_UNVERIFIED (1<<2)
#define MSTDNT_FLAG_SSL_EXPIRED (1<<3)
#define MSTDNT_FLAG_SSL_SELFSIGNED (1<<4)
#define MSTDN_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

View file

@ -100,7 +100,7 @@ int mastodont_register_app(mastodont_t* data,
{ _MSTDNT_QUERY_STRING, "website", u_website },
};
char* post = _mstdnt_query_string(NULL, params, _mstdnt_arr_len(params));
char* post = _mstdnt_query_string(data, NULL, params, _mstdnt_arr_len(params));
curl_easy_setopt(data->curl, CURLOPT_POSTFIELDS, post);
@ -161,7 +161,7 @@ int mastodont_obtain_oauth_token(mastodont_t* data,
{ _MSTDNT_QUERY_STRING, "password", u_password },
};
char* post = _mstdnt_query_string(NULL, params, _mstdnt_arr_len(params));
char* post = _mstdnt_query_string(data, NULL, params, _mstdnt_arr_len(params));
curl_easy_setopt(data->curl, CURLOPT_POSTFIELDS, post);
if (mastodont_fetch_curl(data, "oauth/token", &results, CURLOPT_POST) != CURLE_OK)

View file

@ -13,11 +13,12 @@ void mastodont_global_curl_cleanup()
curl_global_cleanup();
}
int mastodont_init(mastodont_t* data)
int mastodont_init(mastodont_t* data, uint16_t flags)
{
data->curl = curl_easy_init();
data->token = NULL;
data->token_heap = 0;
data->flags = flags;
return data->curl == NULL;
}

View file

@ -51,7 +51,7 @@ int mastodont_get_notifications(mastodont_t* data,
{ _MSTDNT_QUERY_INT, "limit", u_limit },
};
char* post = _mstdnt_query_string(NULL, params, _mstdnt_arr_len(params));
char* post = _mstdnt_query_string(data, NULL, params, _mstdnt_arr_len(params));
curl_easy_setopt(data->curl, CURLOPT_POSTFIELDS, post);

View file

@ -75,7 +75,8 @@ char* _mstdnt_query_string(mastodont_t* data,
else /* Point to it, it's a string */
{
/* First, let's encode it */
escape_str = curl_easy_escape(data->curl, params[i].value.s, 0);
escape_str = MSTDNT_T_FLAG_ISSET(data, MSTDNT_FLAG_NO_URI_SANITIZE) ?
params[i].value.s : curl_easy_escape(data->curl, params[i].value.s, 0);
val_ptr = escape_str;
}
@ -101,6 +102,9 @@ char* _mstdnt_query_string(mastodont_t* data,
strcpy(result + res_prev, params[i].key);
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))
curl_free(escape_str);
}
}