curl/verify.c: gettextize
[libquvi.git] / src / curl / verify.c
blobbcb2bc1710a16aa5fff69f945728948fff5660aa
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_opts(_quvi_net_t n, _c_temp_t t, CURL *c)
39 typedef curl_write_callback cwc;
41 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, (cwc) c_temp_wrcb);
42 curl_easy_setopt(c, CURLOPT_URL, n->url.addr->str);
43 curl_easy_setopt(c, CURLOPT_WRITEDATA, t);
44 curl_easy_setopt(c, CURLOPT_NOBODY, 1L); /* GET -> HEAD */
46 c_autoproxy(n->handle.quvi, n->url.addr->str);
49 static const gchar *_EOK = N_("The server responded with the code %03ld");
51 static QuviError _verify(_quvi_net_t n, CURL *c)
53 CURLcode curlcode;
54 QuviError rc;
56 curlcode = curl_easy_perform(c);
58 curl_easy_setopt(c, CURLOPT_HTTPGET, 1L); /* HEAD -> GET */
59 curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &n->status.resp_code);
61 rc = QUVI_OK;
63 if (curlcode == CURLE_OK)
65 if (n->status.resp_code == 200 || n->status.resp_code == 206)
67 gdouble *l = &n->verify.content_length;
68 gchar *s = NULL;
70 curl_easy_getinfo(c, CURLINFO_CONTENT_TYPE, &s);
71 curl_easy_getinfo(c, CURLINFO_CONTENT_LENGTH_DOWNLOAD, l);
73 g_string_assign(n->verify.content_type, s);
75 else
77 g_string_printf(n->status.errmsg,
78 g_dgettext(GETTEXT_PACKAGE, _EOK),
79 n->status.resp_code);
80 rc = QUVI_ERROR_CALLBACK;
83 else
85 const gchar *s = curl_easy_strerror(curlcode);
86 const gint r = n->status.resp_code;
87 const gint c = curlcode;
88 #define _ENO "%s (HTTP/%03d, cURL=0x%03x)"
89 g_string_printf(n->status.errmsg, _ENO, s, r, c);
90 #undef _ENO
91 rc = QUVI_ERROR_CALLBACK;
93 return (rc);
96 QuviError c_verify(_quvi_t q, _quvi_net_t n)
98 QuviError rc;
99 _c_temp_t t;
100 CURL *c;
102 c = q->handle.curl;
103 t = c_temp_new();
105 _set_opts(n, t, c);
106 rc = _verify(n, c);
108 c_temp_free(t);
109 t = NULL;
111 return (rc);
114 /* vim: set ts=2 sw=2 tw=72 expandtab: */