Post parsing

FossilOrigin-Name: b00574a7079b1f4e670fc6ff34bff336b251a9fedb31ed4475fa7adba27a9ad7
This commit is contained in:
me@ow.nekobit.net 2022-02-15 18:17:11 +00:00
parent a6f5184cfe
commit 828a7dfa8d
2 changed files with 23 additions and 8 deletions

View file

@ -59,7 +59,7 @@ int mastodont_register_app(mastodont_t* data,
struct mstdnt_app_register_args _args;
if (args == NULL)
{
_args.client_name = NULL; /* Defaults to false */
_args.client_name = "mastodont-c"; /* Defaults to false */
_args.redirect_uris = NULL;
_args.scopes = NULL;
_args.website = NULL;
@ -69,6 +69,10 @@ int mastodont_register_app(mastodont_t* data,
union param_value u_client_name, u_redirect_uris,
u_scopes, u_website;
u_client_name.s = args->client_name;
u_redirect_uris.s = args->redirect_uris;
u_scopes.s = args->scopes;
u_website.s = args->website;
struct _mstdnt_query_param params[] = {
{ _MSTDNT_QUERY_STRING, "client_name", u_client_name },
@ -77,9 +81,11 @@ int mastodont_register_app(mastodont_t* data,
{ _MSTDNT_QUERY_STRING, "website", u_website },
};
char* url = _mstdnt_query_string("api/v1/apps", params, _mstdnt_arr_len(params));
char* post = _mstdnt_query_string(NULL, params, _mstdnt_arr_len(params));
if (mastodont_fetch_curl(data, url, &results) != CURLE_OK)
curl_easy_setopt(data->curl, CURLOPT_POSTFIELDS, post);
if (mastodont_fetch_curl(data, "api/v1/apps", &results) != CURLE_OK)
{
res = 1;
goto cleanup;
@ -90,7 +96,7 @@ int mastodont_register_app(mastodont_t* data,
mastodont_fetch_results_cleanup(&results);
cleanup:
free(url);
free(post);
return res;
}

View file

@ -29,16 +29,25 @@ char* _mstdnt_query_string(char* src,
/* If value type is an int, convert it with int->str */
char* val_ptr = NULL;
char conv_val[CONV_SIZE];
size_t src_l = strlen(src);
size_t src_l;
if (src)
src_l = strlen(src);
else
src_l = 0;
/* Key values */
size_t key_len;
size_t val_len;
/* Result */
size_t res_len = src_l+1;
size_t res_len;
if (src_l)
res_len = src_l+1;
else
res_len = 0;
char* result = malloc(res_len);
strncpy(result, src, res_len);
if (src_l)
strncpy(result, src, res_len);
/* We'll call them res to represent the query parameters */
int res_count = 0;
@ -49,7 +58,7 @@ char* _mstdnt_query_string(char* src,
!(params[i].type == _MSTDNT_QUERY_STRING &&
params[i].value.s == NULL))
{
if (res_count++ == 0)
if (res_count++ == 0 && src_l)
/* Replaces Null terminator */
result[res_len-1] = '?';