FIX: tests/lib/fetch.c: include "tests.h"
[libquvi-scripts.git] / tests / lib / fetch.c
blobcc660e87937acf0c0564d9f6d6b5e29ce22c7383
1 /* libquvi-scripts
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi-scripts <http://quvi.sourceforge.net>.
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <string.h>
24 #include <glib.h>
25 #include <quvi.h>
26 #include <curl/curl.h>
28 #include "tests.h"
30 struct temp_s
32 gsize size;
33 gchar *p;
35 typedef struct temp_s *temp_t;
37 static gpointer temp_new()
39 return (g_new0(struct temp_s, 1));
42 static void temp_free(temp_t t)
44 if (t == NULL)
45 return;
47 g_free(t->p);
48 t->p = NULL;
50 g_free(t);
51 t = NULL;
54 /* cURL write callback. */
55 static gsize temp_wrcb(gpointer p, gsize sz, gsize nmemb, gpointer d)
57 const gsize rsize = sz*nmemb;
58 gpointer *np;
59 temp_t t;
61 t = (temp_t) d;
62 np = g_realloc(t->p, t->size+rsize+1);
64 if (np != NULL)
66 t->p = (gchar*) np;
67 memcpy(&(t->p[t->size]), p, rsize);
68 t->size += rsize;
69 t->p[t->size] = '\0';
71 return (rsize);
74 static void set_opts(CURL *c, temp_t t, const gchar *url)
76 typedef curl_write_callback cwc;
78 curl_easy_setopt(c, CURLOPT_USERAGENT, "Mozilla/5.0");
79 curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L);
80 curl_easy_setopt(c, CURLOPT_MAXREDIRS, 5L); /* http://is.gd/kFsvE4 */
81 curl_easy_setopt(c, CURLOPT_NOBODY, 0L);
83 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, (cwc) temp_wrcb);
84 curl_easy_setopt(c, CURLOPT_URL, url);
85 curl_easy_setopt(c, CURLOPT_WRITEDATA, t);
86 /* CURLOPT_ENCODING -> CURLOPT_ACCEPT_ENCODING 7.21.6+ */
87 curl_easy_setopt(c, CURLOPT_ENCODING, "");
89 if (chk_env("TEST_VERBOSE", NULL) == TRUE)
90 curl_easy_setopt(c, CURLOPT_VERBOSE, 1L);
93 static void reset_opts(CURL *c)
95 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, NULL);
96 curl_easy_setopt(c, CURLOPT_WRITEDATA, NULL);
99 static gint _fetch(CURL *c)
101 CURLcode curlcode;
102 glong respcode;
103 gint rc;
105 curlcode = curl_easy_perform(c);
106 curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &respcode);
108 rc = 0;
110 if (curlcode == CURLE_OK && respcode == 200)
112 else
114 if (curlcode == CURLE_OK)
116 #define _EOK "server responded with code %03ld"
117 g_test_message(_EOK, respcode);
118 #undef _EOK
120 else
122 const gchar *s = curl_easy_strerror(curlcode);
123 const glong c = respcode;
124 const gint cc = curlcode;
125 #define _ENO "%s (HTTP/%03ld, cURL=0x%03x)"
126 g_test_message(_ENO, s, c, cc);
127 #undef _ENO
129 rc = 1;
131 return (rc);
134 gchar *fetch(const gchar *url)
136 gchar *r;
137 temp_t t;
138 CURL *c;
139 gint rc;
141 curl_global_init(CURL_GLOBAL_ALL);
143 c = curl_easy_init();
144 if (c == NULL)
146 g_message("curl_easy_init returned NULL");
147 return (NULL);
150 t = temp_new();
151 r = NULL;
153 set_opts(c, t, url);
154 rc = _fetch(c);
155 reset_opts(c);
157 if (rc == 0)
158 r = g_strdup(t->p);
160 temp_free(t);
161 t = NULL;
163 curl_easy_cleanup(c);
164 c = NULL;
166 curl_global_cleanup();
168 return (r);
171 /* vim: set ts=2 sw=2 tw=72 expandtab: */