curl/fetch.c: gettextize
[libquvi.git] / src / curl / fetch.c
blobd24143948bd528798fa839da595adee883fed9aa
1 /* libquvi
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 <glib/gi18n-lib.h>
23 #include <glib.h>
24 #include <curl/curl.h>
26 #include "quvi.h"
27 /* -- */
28 #include "_quvi_s.h"
29 #include "_quvi_media_s.h"
30 #include "_quvi_net_s.h"
31 #include "_quvi_net_opt_s.h"
32 /* -- */
33 #include "curl/autoproxy.h"
34 #include "curl/temp.h"
35 #include "net/def.h"
37 static void _set_opt(gpointer p, gpointer userdata)
39 _quvi_net_opt_t o;
40 CURL *c;
42 o = (_quvi_net_opt_t) p;
43 c = userdata;
45 switch ((glong) o->id)
47 case QUVI_FETCH_OPTION_USER_AGENT:
48 curl_easy_setopt(c, CURLOPT_USERAGENT, o->value.s->str);
49 break;
50 case QUVI_FETCH_OPTION_COOKIE:
51 curl_easy_setopt(c, CURLOPT_COOKIE, o->value.s->str);
52 break;
53 default:
54 break;
58 static void _set_opts(_quvi_net_t n, _c_temp_t t, CURL *c)
60 typedef curl_write_callback cwc;
62 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, (cwc) c_temp_wrcb);
63 curl_easy_setopt(c, CURLOPT_URL, n->url.addr->str);
64 curl_easy_setopt(c, CURLOPT_WRITEDATA, t);
65 /* CURLOPT_ENCODING -> CURLOPT_ACCEPT_ENCODING 7.21.6+ */
66 curl_easy_setopt(c, CURLOPT_ENCODING, "");
67 /* Set cURL options from script fetch args (if any), e.g. user-agent */
68 g_slist_foreach(n->options, _set_opt, c);
70 c_autoproxy(n->handle.quvi, n->url.addr->str);
73 static void _reset_opts(CURL *c)
75 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, NULL);
76 curl_easy_setopt(c, CURLOPT_WRITEDATA, NULL);
79 static const gchar *_EOK = N_("The server responded with the code %03ld");
81 static QuviError _fetch(_quvi_net_t n, CURL *c)
83 CURLcode curlcode;
84 QuviError rc;
86 curlcode = curl_easy_perform(c);
87 curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &n->status.resp_code);
89 rc = QUVI_OK;
91 if (curlcode == CURLE_OK && n->status.resp_code == 200)
93 else
95 if (curlcode == CURLE_OK)
97 g_string_printf(n->status.errmsg,
98 g_dgettext(GETTEXT_PACKAGE, _EOK),
99 n->status.resp_code);
101 else
103 const gchar *s = curl_easy_strerror(curlcode);
104 const glong c = n->status.resp_code;
105 const gint cc = curlcode;
106 #define _ENO "%s (HTTP/%03ld, cURL=0x%03x)"
107 g_string_printf(n->status.errmsg, _ENO, s, c, cc);
108 #undef _ENO
110 rc = QUVI_ERROR_CALLBACK;
112 return (rc);
115 QuviError c_fetch(_quvi_net_t n)
117 QuviError rc;
118 _c_temp_t t;
119 CURL *c;
121 c = n->handle.quvi->handle.curl;
122 t = c_temp_new();
124 _set_opts(n, t, c);
125 rc = _fetch(n, c);
126 _reset_opts(c);
128 if (rc == QUVI_OK)
129 g_string_assign(n->fetch.content, t->p);
131 c_temp_free(t);
132 t = NULL;
134 return (rc);
137 /* vim: set ts=2 sw=2 tw=72 expandtab: */