1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/* libquvi-scripts
* Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
*
* This file is part of libquvi-scripts <http://quvi.sourceforge.net>.
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General
* Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <glib.h>
#include <quvi.h>
#include <curl/curl.h>
#include "tests.h"
struct temp_s
{
gsize size;
gchar *p;
};
typedef struct temp_s *temp_t;
static gpointer temp_new()
{
return (g_new0(struct temp_s, 1));
}
static void temp_free(temp_t t)
{
if (t == NULL)
return;
g_free(t->p);
t->p = NULL;
g_free(t);
t = NULL;
}
/* cURL write callback. */
static gsize temp_wrcb(gpointer p, gsize sz, gsize nmemb, gpointer d)
{
const gsize rsize = sz*nmemb;
gpointer *np;
temp_t t;
t = (temp_t) d;
np = g_realloc(t->p, t->size+rsize+1);
if (np != NULL)
{
t->p = (gchar*) np;
memcpy(&(t->p[t->size]), p, rsize);
t->size += rsize;
t->p[t->size] = '\0';
}
return (rsize);
}
static void set_opts(CURL *c, temp_t t, const gchar *url)
{
typedef curl_write_callback cwc;
curl_easy_setopt(c, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(c, CURLOPT_MAXREDIRS, 5L); /* http://is.gd/kFsvE4 */
curl_easy_setopt(c, CURLOPT_NOBODY, 0L);
curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, (cwc) temp_wrcb);
curl_easy_setopt(c, CURLOPT_URL, url);
curl_easy_setopt(c, CURLOPT_WRITEDATA, t);
/* CURLOPT_ENCODING -> CURLOPT_ACCEPT_ENCODING 7.21.6+ */
curl_easy_setopt(c, CURLOPT_ENCODING, "");
if (chk_env("TEST_VERBOSE", NULL) == TRUE)
curl_easy_setopt(c, CURLOPT_VERBOSE, 1L);
}
static void reset_opts(CURL *c)
{
curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(c, CURLOPT_WRITEDATA, NULL);
}
static gint _fetch(CURL *c)
{
CURLcode curlcode;
glong respcode;
gint rc;
curlcode = curl_easy_perform(c);
curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &respcode);
rc = 0;
if (curlcode == CURLE_OK && respcode == 200)
;
else
{
if (curlcode == CURLE_OK)
{
#define _EOK "server responded with code %03ld"
g_test_message(_EOK, respcode);
#undef _EOK
}
else
{
const gchar *s = curl_easy_strerror(curlcode);
const glong c = respcode;
const gint cc = curlcode;
#define _ENO "%s (HTTP/%03ld, cURL=0x%03x)"
g_test_message(_ENO, s, c, cc);
#undef _ENO
}
rc = 1;
}
return (rc);
}
gchar *fetch(const gchar *url)
{
gchar *r;
temp_t t;
CURL *c;
gint rc;
curl_global_init(CURL_GLOBAL_ALL);
c = curl_easy_init();
if (c == NULL)
{
g_message("curl_easy_init returned NULL");
return (NULL);
}
t = temp_new();
r = NULL;
set_opts(c, t, url);
rc = _fetch(c);
reset_opts(c);
if (rc == 0)
r = g_strdup(t->p);
temp_free(t);
t = NULL;
curl_easy_cleanup(c);
c = NULL;
curl_global_cleanup();
return (r);
}
/* vim: set ts=2 sw=2 tw=72 expandtab: */
|