m_subtitle_new: Store input URL in unescaped form
[libquvi.git] / src / curl / resolve.c
blobc5ddec513905c0215bcaa6bb0525d68ee77f1383
1 /* libquvi
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi <http://quvi.sourceforge.net/>.
6 * This library 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 library 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 library. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <glib/gi18n-lib.h>
24 #include <glib.h>
25 #include <curl/curl.h>
27 #include "quvi.h"
28 /* -- */
29 #include "_quvi_s.h"
30 #include "_quvi_net_resolve_s.h"
31 /* -- */
32 #include "curl/autoproxy.h"
34 /* Set cURL options. */
35 static void _set_opts(_quvi_net_resolve_t r, CURL *c)
37 curl_easy_setopt(c, CURLOPT_URL, r->url.addr->str);
38 /* Use HEAD request: we're only interested in the header metadata. */
39 curl_easy_setopt(c, CURLOPT_NOBODY, 1L); /* GET -> HEAD. */
41 c_autoproxy(r->handle.quvi, r->url.addr->str);
44 static void _reset_opts(CURL *c)
46 curl_easy_setopt(c, CURLOPT_HTTPGET, 1L); /* HEAD -> GET. */
49 static const gchar *_EOK = N_("The server responded with the code %03ld");
51 /* Check whether the server returned a redirection URL. */
52 static QuviError _chk_redir(_quvi_net_resolve_t r, CURL *c)
54 CURLcode curlcode;
55 QuviError rc;
57 curlcode = curl_easy_perform(c);
58 curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &r->status.resp_code);
60 rc = QUVI_OK;
62 if (curlcode == CURLE_OK && r->status.resp_code == 200)
64 gchar *u = NULL;
65 curl_easy_getinfo(c, CURLINFO_EFFECTIVE_URL, &u);
66 /* Leave comparison for resolve_redirections.lua */
67 g_string_assign(r->url.dst, u);
69 else
71 if (curlcode == CURLE_OK)
73 g_string_printf(r->status.errmsg,
74 g_dgettext(GETTEXT_PACKAGE, _EOK),
75 r->status.resp_code);
77 else
79 const gchar *s = curl_easy_strerror(curlcode);
80 const glong c = r->status.resp_code;
81 const gint cc = curlcode;
82 #define _ENO "%s (HTTP/%03ld, cURL=0x%03x)"
83 g_string_printf(r->status.errmsg, _ENO, s, c, cc);
84 #undef _ENO
86 rc = QUVI_ERROR_CALLBACK;
88 return (rc);
91 /* Resolve an URL redirection if needed. */
92 QuviError c_resolve(_quvi_t q, _quvi_net_resolve_t r)
94 QuviError rc;
95 CURL *c;
97 c = q->handle.curl;
99 _set_opts(r, c);
100 rc = _chk_redir(r, c);
101 _reset_opts(c);
103 return (rc);
106 /* vim: set ts=2 sw=2 tw=72 expandtab: */