Moved curl convenience functions into fetch.c

This commit is contained in:
Micheal Smith 2026-01-09 01:42:40 -06:00
parent 5a0ffa95fe
commit 2134029904
Signed by: xulfer
GPG key ID: E40750BFE6702504
3 changed files with 52 additions and 48 deletions

47
fetch.c
View file

@ -7,6 +7,49 @@
static char curl_errbuf[CURL_ERROR_SIZE] = {'\0'}; static char curl_errbuf[CURL_ERROR_SIZE] = {'\0'};
CURLU *init_curl_url(const char *restrict ustr) {
/* Just validate and add options directly to the request now. */
CURLU *url = curl_url();
CURLUcode ures;
if (url == NULL) {
fputs("Out of memory.\n", stderr);
exit(EXIT_FAILURE);
}
if ((ures = curl_url_set(url, CURLUPART_URL, ustr, 0)) != CURLUE_OK) {
fprintf(stderr, "Couldn't set url: %s\n", curl_url_strerror(ures));
return NULL;
}
return url;
}
/* Set a URL query parameter. */
void set_param(CURLU *restrict url, const char *restrict key,
char *restrict value) {
CURLUcode err;
char param[256];
snprintf(param, sizeof param, "%s=%s", key, value);
if ((err = curl_url_set(url, CURLUPART_QUERY, param,
CURLU_APPENDQUERY | CURLU_URLENCODE)) != CURLUE_OK) {
fprintf(stderr, "Error setting url param '%s': %s\n", key,
curl_url_strerror(err));
exit(EXIT_FAILURE);
}
}
/* Just print the url if needed for debugging. */
void print_url(CURLU *restrict url) {
char *url_string = NULL;
curl_url_get(url, CURLUPART_URL, &url_string, 0);
printf("%s\n", url_string);
curl_free(url_string);
}
struct fetch *fetch_init(void) { struct fetch *fetch_init(void) {
CURLcode res; CURLcode res;
CURL *handle; CURL *handle;
@ -59,7 +102,7 @@ size_t fetch_cb(void *ptr, size_t size, size_t nmemb, void *data) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
printf("Wrote %zd bytes\n", real_size); // printf("Wrote %zd bytes\n", real_size);
memcpy(resp->data + resp->size, ptr, real_size); memcpy(resp->data + resp->size, ptr, real_size);
resp->size += real_size; resp->size += real_size;
@ -94,7 +137,7 @@ struct response *fetch(struct fetch *restrict f, CURLU *restrict url) {
&resp->content_type) != CURLE_OK) &resp->content_type) != CURLE_OK)
goto call_fetch_fail; goto call_fetch_fail;
printf("CONTENT TYPE: %s\n", resp->content_type); // printf("CONTENT TYPE: %s\n", resp->content_type);
return resp; return resp;

View file

@ -19,6 +19,10 @@ struct response {
typedef size_t (*CurlWriteCallback)(void *ptr, size_t size, size_t nmemb, typedef size_t (*CurlWriteCallback)(void *ptr, size_t size, size_t nmemb,
void *userdata); void *userdata);
CURLU *init_curl_url(const char *restrict ustr);
void set_param(CURLU *restrict url, const char *restrict key,
char *restrict value);
void print_url(CURLU *restrict url);
struct fetch *fetch_init(void); struct fetch *fetch_init(void);
struct response *fetch(struct fetch *restrict f, CURLU *restrict url); struct response *fetch(struct fetch *restrict f, CURLU *restrict url);
void fetch_cleanup(struct fetch *f); void fetch_cleanup(struct fetch *f);

49
omdb.c
View file

@ -50,49 +50,6 @@ void print_usage(void) {
"\t-y/--year: Year of release.\n"); "\t-y/--year: Year of release.\n");
} }
CURLU *init_curl_url(const char *restrict ustr) {
/* Just validate and add options directly to the request now. */
CURLU *url = curl_url();
CURLUcode ures;
if (url == NULL) {
fputs("Out of memory.\n", stderr);
exit(EXIT_FAILURE);
}
if ((ures = curl_url_set(url, CURLUPART_URL, ustr, 0)) != CURLUE_OK) {
fprintf(stderr, "Couldn't set url: %s\n", curl_url_strerror(ures));
return NULL;
}
return url;
}
/* Set a URL query parameter. */
void set_param(CURLU *restrict url, const char *restrict key,
char *restrict value) {
CURLUcode err;
char param[256];
snprintf(param, sizeof param, "%s=%s", key, value);
if ((err = curl_url_set(url, CURLUPART_QUERY, param,
CURLU_APPENDQUERY | CURLU_URLENCODE)) != CURLUE_OK) {
fprintf(stderr, "Error setting url param '%s': %s\n", key,
curl_url_strerror(err));
exit(EXIT_FAILURE);
}
}
/* Just print the url if needed for debugging. */
void print_url(CURLU *restrict url) {
char *url_string = NULL;
curl_url_get(url, CURLUPART_URL, &url_string, 0);
printf("%s\n", url_string);
curl_free(url_string);
}
/* Print entire JSON response as is. */ /* Print entire JSON response as is. */
int print_title_json(const cJSON *const json) { int print_title_json(const cJSON *const json) {
char *json_str = cJSON_Print(json); char *json_str = cJSON_Print(json);
@ -288,16 +245,16 @@ int main(int argc, char **argv) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
printf("Looking for: %s\n", argv[optind]); // printf("Looking for: %s\n", argv[optind]);
/* Set remaining params if any... */ /* Set remaining params if any... */
set_param(url, "type", search_mode); set_param(url, "type", search_mode);
set_param(url, "r", "json"); set_param(url, "r", "json");
/* Just testing... */ /* Search for title... */
set_param(url, "t", argv[optind]); set_param(url, "t", argv[optind]);
/* Print it... */ /* Print it... */
print_url(url); // print_url(url);
/* /*
* All of the call setup should be done via the URL so all that's left is to * All of the call setup should be done via the URL so all that's left is to