Update NEWS for v0.9.4
[libquvi.git] / src / net / fetch.c
blob47d98ed35795b3fc566d68f8d772cf33fbf866a7
1 /* libquvi
2 * Copyright (C) 2012,2013 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/handle.h"
36 extern gchar* to_utf8(const gchar*, const gchar*);
37 extern QuviError c_fetch(_quvi_net_t);
39 void n_fetch(_quvi_t q, _quvi_net_t *n, const gchar *url, GSList *opts)
41 *n = n_new(q, url);
43 if (q->cb.status != NULL)
45 const glong p = q_makelong(QUVI_CALLBACK_STATUS_FETCH, 0);
47 if (q->cb.status(p, (gpointer) url, q->cb.userdata.status) != QUVI_OK)
49 q->status.rc = QUVI_ERROR_CALLBACK_ABORTED;
50 return;
54 if (q->cb.fetch != NULL)
55 q->status.rc = q->cb.fetch(*n);
56 else /* Fetch using cURL (default). */
57 q->status.rc = c_fetch(*n);
59 if (quvi_ok(q) == QUVI_TRUE && (*n)->status.resp_code == 200)
61 /* To UTF8. */
63 if (opts != NULL)
65 GSList *from;
67 if (l_quvi_object_opts_is_set(q->handle.lua, opts,
68 QUVI_OBJECT_OPTION_FETCH_FROM_CHARSET,
69 &from, NULL, FALSE)
70 == TRUE)
72 l_quvi_object_opt_t o;
73 gchar *c;
75 o = (l_quvi_object_opt_t) from->data;
76 c = to_utf8((*n)->fetch.content->str, o->value.str);
78 if (c != NULL)
80 g_string_assign((*n)->fetch.content, c);
81 g_free(c);
86 /* Update status. */
87 if (q->cb.status != NULL)
89 const glong p = q_makelong(QUVI_CALLBACK_STATUS_FETCH,
90 QUVI_CALLBACK_STATUS_DONE);
92 if (q->cb.status(p, 0, q->cb.userdata.status) != QUVI_OK)
93 q->status.rc = QUVI_ERROR_CALLBACK_ABORTED;
96 else
98 /* Save returned error message. */
99 if ((*n)->status.errmsg->len >0)
100 g_string_assign(q->status.errmsg, (*n)->status.errmsg->str);
101 else
103 g_string_assign(q->status.errmsg,
104 "unknown error: fetch: callback returned "
105 "empty errmsg");
109 q->status.resp_code = (*n)->status.resp_code;
112 /* vim: set ts=2 sw=2 tw=72 expandtab: */