Comment out l_quvi_object_opts_curl
[libquvi.git] / src / net / fetch.c
blob98ce74d3e070f98ba027cc29cc5bb129ed78e641
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(opts,
69 QUVI_OBJECT_OPTION_FETCH_FROM_CHARSET,
70 &from) == 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) != 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: */