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/>.
30 #include "_quvi_net_s.h"
31 #include "_quvi_macro.h"
33 #include "lua/quvi/opts.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
)
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
;
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)
68 if (l_quvi_object_opts_is_set(q
->handle
.lua
, opts
,
69 QUVI_OBJECT_OPTION_FETCH_FROM_CHARSET
,
73 l_quvi_object_opt_t o
;
76 o
= (l_quvi_object_opt_t
) from
->data
;
77 c
= to_utf8((*n
)->fetch
.content
->str
, o
->value
.str
);
81 g_string_assign((*n
)->fetch
.content
, c
);
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
;
99 /* Save returned error message. */
100 if ((*n
)->status
.errmsg
->len
>0)
101 g_string_assign(q
->status
.errmsg
, (*n
)->status
.errmsg
->str
);
104 g_string_assign(q
->status
.errmsg
,
105 "unknown error: fetch: callback returned "
110 q
->status
.resp_code
= (*n
)->status
.resp_code
;
113 /* vim: set ts=2 sw=2 tw=72 expandtab: */