src/curl/: Use intended license header
[libquvi.git] / src / curl / resolve.c
blob3e073b5b0630c5eb62476fc27ce9cae80281afc1
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 <curl/curl.h>
23 #include <glib.h>
25 #include "quvi.h"
26 /* -- */
27 #include "_quvi_s.h"
28 #include "_quvi_net_resolve_s.h"
29 /* -- */
30 #include "curl/autoproxy.h"
32 /* Set cURL options. */
33 static void _set_opts(_quvi_net_resolve_t r, CURL *c)
35 curl_easy_setopt(c, CURLOPT_URL, r->url.addr->str);
36 /* Use HEAD request: we're only interested in the header metadata. */
37 curl_easy_setopt(c, CURLOPT_NOBODY, 1L); /* GET -> HEAD. */
39 c_autoproxy(r->handle.quvi, r->url.addr->str);
42 static void _reset_opts(CURL *c)
44 curl_easy_setopt(c, CURLOPT_HTTPGET, 1L); /* HEAD -> GET. */
47 /* Check whether the server returned a redirection URL. */
48 static QuviError _chk_redir(_quvi_net_resolve_t r, CURL *c)
50 CURLcode curlcode;
51 QuviError rc;
53 curlcode = curl_easy_perform(c);
54 curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &r->status.resp_code);
56 rc = QUVI_OK;
58 if (curlcode == CURLE_OK && r->status.resp_code == 200)
60 gchar *u = NULL;
61 curl_easy_getinfo(c, CURLINFO_EFFECTIVE_URL, &u);
62 /* Leave comparison for resolve_redirections.lua */
63 g_string_assign(r->url.dst, u);
65 else
67 if (curlcode == CURLE_OK)
69 #define _EOK "server responded with code %03ld"
70 g_string_printf(r->status.errmsg, _EOK, r->status.resp_code);
71 #undef _EOK
73 else
75 const gchar *s = curl_easy_strerror(curlcode);
76 const glong c = r->status.resp_code;
77 const gint cc = curlcode;
78 #define _ENO "%s (HTTP/%03ld, cURL=0x%03x)"
79 g_string_printf(r->status.errmsg, _ENO, s, c, cc);
80 #undef _ENO
82 rc = QUVI_ERROR_CALLBACK;
84 return (rc);
87 /* Resolve an URL redirection if needed. */
88 QuviError c_resolve(_quvi_t q, _quvi_net_resolve_t r)
90 QuviError rc;
91 CURL *c;
93 c = q->handle.curl;
95 _set_opts(r, c);
96 rc = _chk_redir(r, c);
97 _reset_opts(c);
99 return (rc);
102 /* vim: set ts=2 sw=2 tw=72 expandtab: */