test_subtitle_core: Add export tests
[libquvi.git] / src / net / fetch.c
bloba0e3340c755f0d397043bfac591b8f0be5ac3013
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 <lauxlib.h>
24 #include <string.h>
25 #include <glib.h>
27 #include "quvi.h"
28 /* -- */
29 #include "_quvi_s.h"
30 #include "_quvi_net_s.h"
31 #include "_quvi_macro.h"
32 /* -- */
33 #include "lua/quvi/opts.h"
34 #include "net/def.h"
35 #include "net/handle.h"
37 extern gchar* to_utf8(const gchar*, const gchar*);
38 extern QuviError c_fetch(_quvi_net_t);
40 void n_fetch(_quvi_t q, _quvi_net_t *n, const gchar *url, GSList *opts)
42 *n = n_new(q, url);
44 if (q->cb.status != NULL)
46 const glong p = q_makelong(QUVI_CALLBACK_STATUS_FETCH, 0);
48 if (q->cb.status(p, (gpointer) url) != QUVI_OK)
50 q->status.rc = QUVI_ERROR_CALLBACK_ABORTED;
51 return;
55 if (q->cb.fetch != NULL)
56 q->status.rc = q->cb.fetch(*n);
57 else /* Fetch using cURL (default). */
58 q->status.rc = c_fetch(*n);
60 if (quvi_ok(q) == QUVI_TRUE && (*n)->status.resp_code == 200)
62 /* To UTF8. */
64 if (opts != NULL)
66 GSList *from;
68 if (l_quvi_object_opts_is_set(q->handle.lua, opts,
69 QUVI_OBJECT_OPTION_FETCH_FROM_CHARSET,
70 &from, NULL, FALSE)
71 == TRUE)
73 l_quvi_object_opt_t o;
74 gchar *c;
76 o = (l_quvi_object_opt_t) from->data;
77 c = to_utf8((*n)->fetch.content->str, o->value.str);
79 if (c != NULL)
81 g_string_assign((*n)->fetch.content, c);
82 g_free(c);
87 /* Update status. */
88 if (q->cb.status != NULL)
90 const glong p = q_makelong(QUVI_CALLBACK_STATUS_FETCH,
91 QUVI_CALLBACK_STATUS_DONE);
93 if (q->cb.status(p, 0) != QUVI_OK)
94 q->status.rc = QUVI_ERROR_CALLBACK_ABORTED;
97 else
99 /* Save returned error message. */
100 if ((*n)->status.errmsg->len >0)
101 g_string_assign(q->status.errmsg, (*n)->status.errmsg->str);
102 else
104 g_string_assign(q->status.errmsg,
105 "unknown error: fetch: callback returned "
106 "empty errmsg");
110 q->status.resp_code = (*n)->status.resp_code;
113 /* vim: set ts=2 sw=2 tw=72 expandtab: */