diff --git a/include/mastodont_fetch.h b/include/mastodont_fetch.h new file mode 100644 index 0000000..9e33e22 --- /dev/null +++ b/include/mastodont_fetch.h @@ -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 . + */ + +#include +#include + +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();*/ diff --git a/src/fetch.c b/src/fetch.c new file mode 100644 index 0000000..4c95eea --- /dev/null +++ b/src/fetch.c @@ -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 . + */ + +#include +#include +#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; +} diff --git a/src/timeline.c b/src/timeline.c index 44bb50f..0d3070b 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -14,6 +14,8 @@ */ #include +#include +#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; }