|
Packit |
3ff1e7 |
/* libquvi
|
|
Packit |
3ff1e7 |
* Copyright (C) 2012,2013 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 <lauxlib.h>
|
|
Packit |
3ff1e7 |
#include <string.h>
|
|
Packit |
3ff1e7 |
#include <glib.h>
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
#include "quvi.h"
|
|
Packit |
3ff1e7 |
/* -- */
|
|
Packit |
3ff1e7 |
#include "_quvi_s.h"
|
|
Packit |
3ff1e7 |
#include "_quvi_net_s.h"
|
|
Packit |
3ff1e7 |
#include "_quvi_macro.h"
|
|
Packit |
3ff1e7 |
/* -- */
|
|
Packit |
3ff1e7 |
#include "lua/quvi/opts.h"
|
|
Packit |
3ff1e7 |
#include "net/handle.h"
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
extern gchar* to_utf8(const gchar*, const gchar*);
|
|
Packit |
3ff1e7 |
extern QuviError c_fetch(_quvi_net_t);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
void n_fetch(_quvi_t q, _quvi_net_t *n, const gchar *url, GSList *opts)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
*n = n_new(q, url);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (q->cb.status != NULL)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
const glong p = q_makelong(QUVI_CALLBACK_STATUS_FETCH, 0);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (q->cb.status(p, (gpointer) url, q->cb.userdata.status) != QUVI_OK)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
q->status.rc = QUVI_ERROR_CALLBACK_ABORTED;
|
|
Packit |
3ff1e7 |
return;
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (q->cb.fetch != NULL)
|
|
Packit |
3ff1e7 |
q->status.rc = q->cb.fetch(*n);
|
|
Packit |
3ff1e7 |
else /* Fetch using cURL (default). */
|
|
Packit |
3ff1e7 |
q->status.rc = c_fetch(*n);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (quvi_ok(q) == QUVI_TRUE && (*n)->status.resp_code == 200)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
/* To UTF8. */
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (opts != NULL)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
GSList *from;
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (l_quvi_object_opts_is_set(q->handle.lua, opts,
|
|
Packit |
3ff1e7 |
QUVI_OBJECT_OPTION_FETCH_FROM_CHARSET,
|
|
Packit |
3ff1e7 |
&from, NULL, FALSE)
|
|
Packit |
3ff1e7 |
== TRUE)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
l_quvi_object_opt_t o;
|
|
Packit |
3ff1e7 |
gchar *c;
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
o = (l_quvi_object_opt_t) from->data;
|
|
Packit |
3ff1e7 |
c = to_utf8((*n)->fetch.content->str, o->value.str);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (c != NULL)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
g_string_assign((*n)->fetch.content, c);
|
|
Packit |
3ff1e7 |
g_free(c);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
/* Update status. */
|
|
Packit |
3ff1e7 |
if (q->cb.status != NULL)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
const glong p = q_makelong(QUVI_CALLBACK_STATUS_FETCH,
|
|
Packit |
3ff1e7 |
QUVI_CALLBACK_STATUS_DONE);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (q->cb.status(p, 0, q->cb.userdata.status) != QUVI_OK)
|
|
Packit |
3ff1e7 |
q->status.rc = QUVI_ERROR_CALLBACK_ABORTED;
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
else
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
/* Save returned error message. */
|
|
Packit |
3ff1e7 |
if ((*n)->status.errmsg->len >0)
|
|
Packit |
3ff1e7 |
g_string_assign(q->status.errmsg, (*n)->status.errmsg->str);
|
|
Packit |
3ff1e7 |
else
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
g_string_assign(q->status.errmsg,
|
|
Packit |
3ff1e7 |
"unknown error: fetch: callback returned "
|
|
Packit |
3ff1e7 |
"empty errmsg");
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
q->status.resp_code = (*n)->status.resp_code;
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
/* vim: set ts=2 sw=2 tw=72 expandtab: */
|