|
Packit |
3ff1e7 |
/* libquvi
|
|
Packit |
3ff1e7 |
* Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
|
|
Packit |
3ff1e7 |
*
|
|
Packit |
3ff1e7 |
* This file is part of libquvi <http://quvi.sourceforge.net/>.
|
|
Packit |
3ff1e7 |
*
|
|
Packit |
3ff1e7 |
* This library is free software: you can redistribute it and/or
|
|
Packit |
3ff1e7 |
* modify it under the terms of the GNU Affero General Public
|
|
Packit |
3ff1e7 |
* License as published by the Free Software Foundation, either
|
|
Packit |
3ff1e7 |
* version 3 of the License, or (at your option) any later version.
|
|
Packit |
3ff1e7 |
*
|
|
Packit |
3ff1e7 |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
3ff1e7 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
3ff1e7 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
3ff1e7 |
* GNU Affero General Public License for more details.
|
|
Packit |
3ff1e7 |
*
|
|
Packit |
3ff1e7 |
* You should have received a copy of the GNU Affero General
|
|
Packit |
3ff1e7 |
* Public License along with this library. If not, see
|
|
Packit |
3ff1e7 |
* <http://www.gnu.org/licenses/>.
|
|
Packit |
3ff1e7 |
*/
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
#include "config.h"
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
#include <glib/gi18n-lib.h>
|
|
Packit |
3ff1e7 |
#include <glib.h>
|
|
Packit |
3ff1e7 |
#include <curl/curl.h>
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
#include "quvi.h"
|
|
Packit |
3ff1e7 |
/* -- */
|
|
Packit |
3ff1e7 |
#include "_quvi_s.h"
|
|
Packit |
3ff1e7 |
#include "_quvi_net_resolve_s.h"
|
|
Packit |
3ff1e7 |
/* -- */
|
|
Packit |
3ff1e7 |
#include "curl/autoproxy.h"
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
/* Set cURL options. */
|
|
Packit |
3ff1e7 |
static void _set_opts(_quvi_net_resolve_t r, CURL *c)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
curl_easy_setopt(c, CURLOPT_URL, r->url.addr->str);
|
|
Packit |
3ff1e7 |
/* Use HEAD request: we're only interested in the header metadata. */
|
|
Packit |
3ff1e7 |
curl_easy_setopt(c, CURLOPT_NOBODY, 1L); /* GET -> HEAD. */
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
c_autoproxy(r->handle.quvi, r->url.addr->str);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
static void _reset_opts(CURL *c)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
curl_easy_setopt(c, CURLOPT_HTTPGET, 1L); /* HEAD -> GET. */
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
static const gchar *_EOK = N_("The server responded with the code %03ld");
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
/* Check whether the server returned a redirection URL. */
|
|
Packit |
3ff1e7 |
static QuviError _chk_redir(_quvi_net_resolve_t r, CURL *c)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
CURLcode curlcode;
|
|
Packit |
3ff1e7 |
QuviError rc;
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
curlcode = curl_easy_perform(c);
|
|
Packit |
3ff1e7 |
curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &r->status.resp_code);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
rc = QUVI_OK;
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (curlcode == CURLE_OK && r->status.resp_code == 200)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
gchar *u = NULL;
|
|
Packit |
3ff1e7 |
curl_easy_getinfo(c, CURLINFO_EFFECTIVE_URL, &u);
|
|
Packit |
3ff1e7 |
/* Leave comparison for resolve_redirections.lua */
|
|
Packit |
3ff1e7 |
g_string_assign(r->url.dst, u);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
else
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
if (curlcode == CURLE_OK)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
g_string_printf(r->status.errmsg,
|
|
Packit |
3ff1e7 |
g_dgettext(GETTEXT_PACKAGE, _EOK),
|
|
Packit |
3ff1e7 |
r->status.resp_code);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
else
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
const gchar *s = curl_easy_strerror(curlcode);
|
|
Packit |
3ff1e7 |
const glong c = r->status.resp_code;
|
|
Packit |
3ff1e7 |
const gint cc = curlcode;
|
|
Packit |
3ff1e7 |
#define _ENO "%s (HTTP/%03ld, cURL=0x%03x)"
|
|
Packit |
3ff1e7 |
g_string_printf(r->status.errmsg, _ENO, s, c, cc);
|
|
Packit |
3ff1e7 |
#undef _ENO
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
rc = QUVI_ERROR_CALLBACK;
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
return (rc);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
/* Resolve an URL redirection if needed. */
|
|
Packit |
3ff1e7 |
QuviError c_resolve(_quvi_t q, _quvi_net_resolve_t r)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
QuviError rc;
|
|
Packit |
3ff1e7 |
CURL *c;
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
c = q->handle.curl;
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
_set_opts(r, c);
|
|
Packit |
3ff1e7 |
rc = _chk_redir(r, c);
|
|
Packit |
3ff1e7 |
_reset_opts(c);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
return (rc);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
/* vim: set ts=2 sw=2 tw=72 expandtab: */
|