Fetch functions, cleanup code

FossilOrigin-Name: 174e1794cad7f4a236ca0728d57ae821fe27d29bc5ef04e3435af51048345a26
This commit is contained in:
me@ow.nekobit.net 2022-01-21 03:45:22 +00:00
parent f3ee4033e2
commit 940138f370
3 changed files with 102 additions and 8 deletions

29
include/mastodont_fetch.h Normal file
View file

@ -0,0 +1,29 @@
/*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cjson/cJSON.h>
#include <mastodont_types.h>
struct mastodont_fetch_results
{
char* response;
size_t size;
};
void mastodont_fetch_results_cleanup(struct mastodont_fetch_results* res);
int mastodont_fetch_curl(mastodont_t* mstdnt,
char* url,
struct mastodont_fetch_results* results);
/*cJSON* mastodont_call_json();*/

65
src/fetch.c Normal file
View file

@ -0,0 +1,65 @@
/*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "mastodont_fetch.h"
/* For use with libcurl */
static size_t write_callback(char* ptr, size_t _size, size_t nmemb, void* _content)
{
size_t size = nmemb * _size; /* Mostly pointless, but portable */
struct mastodont_fetch_results* res = _content; /* Cast */
char* data;
if ((data = realloc(res->response, res->size + size + 1)) == NULL)
{
perror("realloc");
return 0;
}
res->response = data;
memcpy(&(res->response[res->size]), data, size);
res->size += size;
res->response[res->size] = 0;
return size;
}
void mastodont_fetch_results_cleanup(struct mastodont_fetch_results* res)
{
free(res->response);
}
int mastodont_fetch_curl(mastodont_t* mstdnt,
char* _url,
struct mastodont_fetch_results* results)
{
int res;
/* Setup URL */
char url[MSTDNT_URLSIZE];
strncpy(url, mstdnt->url, MSTDNT_URLSIZE-1);
strncat(url, _url, MSTDNT_URLSIZE-1);
/* Set options */
curl_easy_setopt(mstdnt->curl, CURLOPT_URL, url);
curl_easy_setopt(mstdnt->curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(mstdnt->curl, CURLOPT_WRITEDATA, results);
res = curl_easy_perform(mstdnt->curl);
return res;
}

View file

@ -14,6 +14,8 @@
*/
#include <string.h>
#include <stdlib.h>
#include "mastodont_fetch.h"
#include "mastodont_timeline.h"
int mastodont_timeline_public(mastodont_t* data,
@ -21,8 +23,7 @@ int mastodont_timeline_public(mastodont_t* data,
struct mstdnt_response* response)
{
int res;
char url[MSTDNT_URLSIZE];
strncpy(url, data->url, MSTDNT_URLSIZE-1);
struct mastodont_fetch_results results = { 0 };
/* Default args */
struct mstdnt_timeline_public_args _args;
if (args == NULL)
@ -37,12 +38,11 @@ int mastodont_timeline_public(mastodont_t* data,
args = &_args;
}
/* Copy string */
strncat(url, "/api/v1/timelines/public", MSTDNT_URLSIZE-1);
curl_easy_setopt(data->curl, CURLOPT_URL, url);
res = curl_easy_perform(data->curl);
res = mastodont_fetch_curl(data, "/api/v1/timelines/public", &results);
curl_easy_cleanup(data->curl);
printf("Size of data: %zd\n", results.size);
mastodont_fetch_results_cleanup(&results);
return res;
}