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
22 #include <curl/curl.h>
28 #include "_quvi_net_resolve_s.h"
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
)
53 curlcode
= curl_easy_perform(c
);
54 curl_easy_getinfo(c
, CURLINFO_RESPONSE_CODE
, &r
->status
.resp_code
);
58 if (curlcode
== CURLE_OK
&& r
->status
.resp_code
== 200)
61 curl_easy_getinfo(c
, CURLINFO_EFFECTIVE_URL
, &u
);
62 /* Leave comparison for resolve_redirections.lua */
63 g_string_assign(r
->url
.dst
, u
);
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
);
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
);
82 rc
= QUVI_ERROR_CALLBACK
;
87 /* Resolve an URL redirection if needed. */
88 QuviError
c_resolve(_quvi_t q
, _quvi_net_resolve_t r
)
96 rc
= _chk_redir(r
, c
);
102 /* vim: set ts=2 sw=2 tw=72 expandtab: */