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
29 #include "_quvi_net_s.h"
30 #include "_quvi_net_opt_s.h"
31 #include "_quvi_macro.h"
34 #include "net/handle.h"
37 /* Return status type. */
38 static glong
_cb_status_type(_quvi_net_t n
)
44 st
= QUVI_CALLBACK_STATUS_FETCH_URL
; /* default */
49 o
= (_quvi_net_opt_t
) curr
->data
;
50 if (o
->id
== QUVI_FETCH_OPTION_TYPE
)
52 st
= (glong
) o
->value
.n
;
55 curr
= g_slist_next(curr
);
60 static gchar
*_chk_opt_from_charset(_quvi_net_t n
)
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
);
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
)
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
;
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)
106 gchar
*from
= _chk_opt_from_charset(*n
);
107 gchar
*c
= to_utf8((*n
)->fetch
.content
->str
, from
);
111 g_string_assign((*n
)->fetch
.content
, c
);
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
;
134 /* Save returned error message. */
135 if ((*n
)->status
.errmsg
->len
>0)
136 g_string_assign(q
->status
.errmsg
, (*n
)->status
.errmsg
->str
);
139 g_string_assign(q
->status
.errmsg
,
140 "unknown error: fetch: callback returned "
145 q
->status
.resp_code
= (*n
)->status
.resp_code
;
148 /* vim: set ts=2 sw=2 tw=72 expandtab: */