Serialize URL

FossilOrigin-Name: 665c02d5710a998a6ff2018fee4999104a8a93423eae7b0379bdc7815c77dc1e
This commit is contained in:
me@ow.nekobit.net 2022-03-04 04:11:57 +00:00
parent c9c59ae4a2
commit 5d9f310891
4 changed files with 16 additions and 7 deletions

View file

@ -34,7 +34,8 @@ struct _mstdnt_query_param
} value;
};
char* _mstdnt_query_string(char* src,
char* _mstdnt_query_string(mastodont_t* data,
char* src,
struct _mstdnt_query_param* params,
size_t param_len);

View file

@ -20,12 +20,14 @@
#define CONV_SIZE 64
/* TODO audit this function for overflows */
char* _mstdnt_query_string(char* src,
char* _mstdnt_query_string(mastodont_t* data,
char* src,
struct _mstdnt_query_param* params,
size_t param_len)
{
size_t i;
int res_prev;
char* escape_str;
/* If value type is an int, convert it with int->str */
char* val_ptr = NULL;
char conv_val[CONV_SIZE];
@ -54,10 +56,12 @@ char* _mstdnt_query_string(char* src,
for (i = 0; i < param_len; ++i)
{
escape_str = NULL;
if (params[i].key &&
!(params[i].type == _MSTDNT_QUERY_STRING &&
params[i].value.s == NULL))
{
if (res_count++ == 0 && src_l)
/* Replaces Null terminator */
result[res_len-1] = '?';
@ -68,8 +72,12 @@ char* _mstdnt_query_string(char* src,
snprintf(conv_val, CONV_SIZE, "%d", params[i].value.i);
val_ptr = conv_val;
}
else /* Point to it */
val_ptr = params[i].value.s;
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);
val_ptr = escape_str;
}
/* Get lengths */
key_len = strlen(params[i].key);

View file

@ -169,7 +169,7 @@ int mastodont_create_status(mastodont_t* data,
{ _MSTDNT_QUERY_STRING, "visibility", u_visibility },
};
post = _mstdnt_query_string(NULL, params, _mstdnt_arr_len(params));
post = _mstdnt_query_string(data, NULL, params, _mstdnt_arr_len(params));
curl_easy_setopt(data->curl, CURLOPT_POSTFIELDS, post);

View file

@ -48,7 +48,7 @@ int mastodont_timeline_list(mastodont_t* data,
{ _MSTDNT_QUERY_INT, "limit", u_limit },
};
char* url_query = _mstdnt_query_string(url, params, _mstdnt_arr_len(params));
char* url_query = _mstdnt_query_string(data, url, params, _mstdnt_arr_len(params));
if (mastodont_fetch_curl(data, url_query, &results, CURLOPT_HTTPGET) != CURLE_OK)
{
@ -97,7 +97,7 @@ int mastodont_timeline_public(mastodont_t* data,
{ _MSTDNT_QUERY_INT, "limit", u_limit },
};
char* url = _mstdnt_query_string("api/v1/timelines/public", params, _mstdnt_arr_len(params));
char* url = _mstdnt_query_string(data, "api/v1/timelines/public", params, _mstdnt_arr_len(params));
if (mastodont_fetch_curl(data, url, &results, CURLOPT_HTTPGET) != CURLE_OK)
{