diff --git a/include/mastodont_query.h b/include/mastodont_query.h index b0b10b5..072b0a4 100644 --- a/include/mastodont_query.h +++ b/include/mastodont_query.h @@ -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); diff --git a/src/query.c b/src/query.c index 0180eef..70cc8bb 100644 --- a/src/query.c +++ b/src/query.c @@ -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); diff --git a/src/status.c b/src/status.c index a6b8756..5368e3a 100644 --- a/src/status.c +++ b/src/status.c @@ -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); diff --git a/src/timeline.c b/src/timeline.c index 05b9337..339d9a9 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -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) {