Fix query string generator

FossilOrigin-Name: 7bc838fbb628e77c59da85f238f6b2668a2d5caa82dcfcaeadc0442be9674f3f
This commit is contained in:
me@ow.nekobit.net 2022-02-15 15:39:34 +00:00
parent a321593346
commit 2ffe977836
6 changed files with 57 additions and 26 deletions

View file

@ -1,5 +1,5 @@
CC ?= cc
CFLAGS = -ansi -I ./include/
CFLAGS = -g -ansi -I ./include/
SRC = $(wildcard src/*.c)
OBJ = $(patsubst %.c,%.o,$(SRC))
TARGET = libmastodont.a # shared

View file

@ -17,7 +17,7 @@
#define MASTODONT_APPLICATION
#include "mastodont_types.h"
#include <cjson/cJSON.h>
#include <mastodont_status.h>
#include <mastodont_fetch.h>
/* Status: Complete */

View file

@ -22,7 +22,7 @@ enum _mstdnt_query_type
{
_MSTDNT_QUERY_STRING,
_MSTDNT_QUERY_INT
}
};
struct _mstdnt_query_param
{

View file

@ -23,16 +23,13 @@ int mastodont_register_app(mastodont_t* data,
struct mstdnt_fetch_results results = { 0 };
/* Default args */
struct mstdnt_timeline_public_args _args;
struct mstdnt_app_register_args _args;
if (args == NULL)
{
_args.local = 0; /* Defaults to false */
_args.remote = 0;
_args.only_media = 0;
_args.max_id = NULL;
_args.since_id = NULL;
_args.min_id = NULL;
_args.limit = 20;
_args.client_name = NULL; /* Defaults to false */
_args.redirect_uris = NULL;
_args.scopes = NULL;
_args.website = NULL;
args = &_args;
}
storage->needs_cleanup = 0;
@ -40,7 +37,7 @@ int mastodont_register_app(mastodont_t* data,
if (mastodont_fetch_curl(data, "api/v1/apps", &results) != CURLE_OK)
return 1;
res = mstdnt_load_statuses_from_result(statuses, storage, &results, size);
/*res = mstdnt_load_statuses_from_result(statuses, storage, &results, size);*/
mastodont_fetch_results_cleanup(&results);

View file

@ -19,6 +19,7 @@
#define CONV_SIZE 64
/* TODO audit this function for overflows */
char* _mstdnt_query_string(char* src,
struct _mstdnt_query_param* params,
size_t param_len)
@ -26,9 +27,14 @@ char* _mstdnt_query_string(char* src,
size_t i;
int res_prev;
/* 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);
/* Key values */
size_t key_len;
size_t val_len;
/* Result */
size_t res_len = src_l+1;
char* result = malloc(res_len);
@ -42,31 +48,43 @@ char* _mstdnt_query_string(char* src,
if (params[i].key)
{
if (res_count++ == 0)
{
result = realloc(result, ++res_len);
/* Replaces Null terminator */
result[res_len-1] = '?';
}
/* Convert value */
if (params[i].type == _MSTDNT_QUERY_INT)
{
snprintf(conv_val, CONV_SIZE, "%d", params[i].value.i);
val_ptr = conv_val;
}
else /* Point to it */
conv_val = params[i].value.s;
val_ptr = params[i].value.s;
/* Get lengths */
key_len = strlen(params[i].key);
val_len = strlen(val_ptr);
res_prev = res_len;
/* |v| & character |v| account for '=' */
res_len += 1 + strlen(params[i].key) + 1 + strlen(conv_val);
/* |v| & character |v| account for '=' */
res_len += 1 + key_len + 1 + val_len;
result = realloc(result, res_len + 1);
if (res_count) result[res_prev] = '&';
if (res_count) result[res_len-2] = '\0';
else result[res_len-1] = '\0';
/* TODO */
result = realloc(result, res_len + 1); /* NULL terminator space */
if (res_count - 1 != 0)
result[res_prev-1] = '&';
/* Leave an extra byte, doesn't hurt */
if (res_count - 1 != 0)
result[res_len-1] = '\0';
else result[res_len] = '\0';
/* Copy over strings (skip & sign ;; +1) */
strcpy(result + res_prev, params[i].key);
result[res_prev - (res_count - 1 != 0) + key_len] = '=';
strcpy(result + res_prev + 1 + key_len - (res_count - 1 != 0), val_ptr);
}
}
return result;
}

View file

@ -17,6 +17,7 @@
#include <stdlib.h>
#include "mastodont_fetch.h"
#include "mastodont_timeline.h"
#include "mastodont_query.h"
int mastodont_timeline_public(mastodont_t* data,
struct mstdnt_timeline_public_args* args,
@ -42,6 +43,21 @@ int mastodont_timeline_public(mastodont_t* data,
}
storage->needs_cleanup = 0;
struct _mstdnt_query_param params[] = {
{ _MSTDNT_QUERY_STRING, NULL, "rat" },
{ _MSTDNT_QUERY_STRING, "lolled", "ratted" },
};
char* url = _mstdnt_query_string("api/v1/timelines/public",
params, 2);
puts(url);
free (url);
return 1;
if (mastodont_fetch_curl(data, "api/v1/timelines/public", &results) != CURLE_OK)
return 1;