tests/verify.c: chk_val: Test for >0
[libquvi.git] / src / net / fetch.c
blob34405cf991259988efebf4c491e5ac8d1f783101
1 /* libquvi
2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
20 #include "config.h"
22 #include <lauxlib.h>
23 #include <string.h>
24 #include <glib.h>
26 #include "quvi.h"
27 /* -- */
28 #include "_quvi_s.h"
29 #include "_quvi_net_s.h"
30 #include "_quvi_net_opt_s.h"
31 #include "_quvi_macro.h"
32 /* -- */
33 #include "net/def.h"
34 #include "net/handle.h"
35 #include "net/opt.h"
37 /* Return status type. */
38 static glong _cb_status_type(_quvi_net_t n)
40 _quvi_net_opt_t o;
41 GSList *curr;
42 glong st;
44 st = QUVI_CALLBACK_STATUS_FETCH_URL; /* default */
45 curr = n->options;
47 while (curr != NULL)
49 o = (_quvi_net_opt_t) curr->data;
50 if (o->id == QUVI_FETCH_OPTION_TYPE)
52 st = (glong) o->value.n;
53 break;
55 curr = g_slist_next(curr);
57 return (st);
60 static gchar *_chk_opt_from_charset(_quvi_net_t n)
62 _quvi_net_opt_t o;
63 GSList *curr;
65 curr = n->options;
66 while (curr != NULL)
68 o = (_quvi_net_opt_t) curr->data;
70 if (o->id == QUVI_FETCH_OPTION_FROM_CHARSET)
71 return (g_strdup(o->value.s->str));
73 curr = g_slist_next(curr);
75 return (NULL);
78 extern gchar* to_utf8(const gchar*, const gchar*);
79 extern QuviError c_fetch(_quvi_net_t);
81 void n_fetch(_quvi_t q, _quvi_net_t *n, const gchar *url)
83 *n = n_new(q, url);
84 n_chk_callback_opts(*n, q->handle.lua);
86 if (q->cb.status != NULL)
88 const glong st = _cb_status_type(*n);
89 const glong p = q_makelong(QUVI_CALLBACK_STATUS_FETCH, st);
91 if (q->cb.status(p, (gpointer) url) != QUVI_OK)
93 q->status.rc = QUVI_ERROR_CALLBACK_ABORTED;
94 return;
98 if (q->cb.fetch != NULL)
99 q->status.rc = q->cb.fetch(*n);
100 else /* Fetch using cURL (default). */
101 q->status.rc = c_fetch(*n);
103 if (quvi_ok(q) == QUVI_TRUE && (*n)->status.resp_code == 200)
105 /* To UTF8. */
106 gchar *from = _chk_opt_from_charset(*n);
107 gchar *c = to_utf8((*n)->fetch.content->str, from);
109 if (c != NULL)
111 g_string_assign((*n)->fetch.content, c);
112 g_free(c);
113 c = NULL;
116 if (from != NULL)
118 g_free(from);
119 from = NULL;
122 /* Update status. */
123 if (q->cb.status != NULL)
125 const glong p = q_makelong(QUVI_CALLBACK_STATUS_FETCH,
126 QUVI_CALLBACK_STATUS_DONE);
128 if (q->cb.status(p, 0) != QUVI_OK)
129 q->status.rc = QUVI_ERROR_CALLBACK_ABORTED;
132 else
134 /* Save returned error message. */
135 if ((*n)->status.errmsg->len >0)
136 g_string_assign(q->status.errmsg, (*n)->status.errmsg->str);
137 else
139 g_string_assign(q->status.errmsg,
140 "unknown error: fetch: callback returned "
141 "empty errmsg");
145 q->status.resp_code = (*n)->status.resp_code;
148 /* vim: set ts=2 sw=2 tw=72 expandtab: */