tests/lib/: Use the intended license
[libquvi-scripts.git] / tests / lib / fetch.c
blobfc88076ea674a8592175bdf06959695d6eda60c2
1 /* libquvi-scripts
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
20 #include "config.h"
22 #include <string.h>
23 #include <glib.h>
24 #include <quvi.h>
25 #include <curl/curl.h>
27 struct temp_s
29 gsize size;
30 gchar *p;
32 typedef struct temp_s *temp_t;
34 static gpointer temp_new()
36 return (g_new0(struct temp_s, 1));
39 static void temp_free(temp_t t)
41 if (t == NULL)
42 return;
44 g_free(t->p);
45 t->p = NULL;
47 g_free(t);
48 t = NULL;
51 /* cURL write callback. */
52 static gsize temp_wrcb(gpointer p, gsize sz, gsize nmemb, gpointer d)
54 const gsize rsize = sz*nmemb;
55 gpointer *np;
56 temp_t t;
58 t = (temp_t) d;
59 np = g_realloc(t->p, t->size+rsize+1);
61 if (np != NULL)
63 t->p = (gchar*) np;
64 memcpy(&(t->p[t->size]), p, rsize);
65 t->size += rsize;
66 t->p[t->size] = '\0';
68 return (rsize);
71 static void set_opts(CURL *c, temp_t t, const gchar *url)
73 typedef curl_write_callback cwc;
75 curl_easy_setopt(c, CURLOPT_USERAGENT, "Mozilla/5.0");
76 curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L);
77 curl_easy_setopt(c, CURLOPT_MAXREDIRS, 5L); /* http://is.gd/kFsvE4 */
78 curl_easy_setopt(c, CURLOPT_NOBODY, 0L);
80 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, (cwc) temp_wrcb);
81 curl_easy_setopt(c, CURLOPT_URL, url);
82 curl_easy_setopt(c, CURLOPT_WRITEDATA, t);
83 /* CURLOPT_ENCODING -> CURLOPT_ACCEPT_ENCODING 7.21.6+ */
84 curl_easy_setopt(c, CURLOPT_ENCODING, "");
86 if (chk_env("TEST_VERBOSE", NULL) == TRUE)
87 curl_easy_setopt(c, CURLOPT_VERBOSE, 1L);
90 static void reset_opts(CURL *c)
92 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, NULL);
93 curl_easy_setopt(c, CURLOPT_WRITEDATA, NULL);
96 static gint _fetch(CURL *c)
98 CURLcode curlcode;
99 glong respcode;
100 gint rc;
102 curlcode = curl_easy_perform(c);
103 curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &respcode);
105 rc = 0;
107 if (curlcode == CURLE_OK && respcode == 200)
109 else
111 if (curlcode == CURLE_OK)
113 #define _EOK "server responded with code %03ld"
114 g_test_message(_EOK, respcode);
115 #undef _EOK
117 else
119 const gchar *s = curl_easy_strerror(curlcode);
120 const glong c = respcode;
121 const gint cc = curlcode;
122 #define _ENO "%s (HTTP/%03ld, cURL=0x%03x)"
123 g_test_message(_ENO, s, c, cc);
124 #undef _ENO
126 rc = 1;
128 return (rc);
131 gchar *fetch(const gchar *url)
133 gchar *r;
134 temp_t t;
135 CURL *c;
136 gint rc;
138 curl_global_init(CURL_GLOBAL_ALL);
140 c = curl_easy_init();
141 if (c == NULL)
143 g_message("curl_easy_init returned NULL");
144 return (NULL);
147 t = temp_new();
148 r = NULL;
150 set_opts(c, t, url);
151 rc = _fetch(c);
152 reset_opts(c);
154 if (rc == 0)
155 r = g_strdup(t->p);
157 temp_free(t);
158 t = NULL;
160 curl_easy_cleanup(c);
161 c = NULL;
163 curl_global_cleanup();
165 return (r);
168 /* vim: set ts=2 sw=2 tw=72 expandtab: */