Tweak quvi.h doxy comments, rename net opts to feats
[quvi.git] / examples / callback_libsoup.c
blobe1fcf34b7ceb0a35a6ee1a37712c3e2d042bb5a1
1 /* quvi
2 * Copyright (C) 2011 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 /* callback_libsoup.c -- Use libsoup instead of libcurl */
22 #include "config.h"
24 #include <stdio.h>
25 #include <string.h>
27 #include <quvi/quvi.h>
29 #ifdef HAVE_LIBSOUP_GNOME
30 #include <libsoup/soup-gnome.h>
31 #else
32 #include <libsoup/soup.h>
33 #endif
35 #include "common.h"
37 static QUVIcode status_callback(long param, void *data)
39 quvi_word status, type;
41 status = quvi_loword(param);
42 type = quvi_hiword(param);
44 switch (status)
46 case QUVISTATUS_RESOLVE:
47 handle_resolve_status(type);
48 break;
50 case QUVISTATUS_FETCH:
51 handle_fetch_status(type, data);
52 break;
54 case QUVISTATUS_VERIFY:
55 handle_verify_status(type);
56 break;
59 fflush(stderr);
61 return (QUVI_OK);
64 static void set_feat(quvi_net_t n, SoupMessage *m,
65 QUVInetPropertyFeatureName feature,
66 const char *hdr)
68 char *s = quvi_net_get_one_prop_feat(n, feature);
69 if (s)
70 soup_message_headers_append(m->request_headers, hdr, s);
73 static void set_feats_from_lua_script(quvi_net_t n, SoupMessage *m)
75 set_feat(n, m, QUVI_NET_PROPERTY_FEATURE_ARBITRARYCOOKIE, "Cookie");
76 set_feat(n, m, QUVI_NET_PROPERTY_FEATURE_USERAGENT, "User-Agent");
77 #ifdef _0
78 /* Same as above. */
79 quvi_llst_node_t feature;
80 quvi_net_getprop(n, QUVI_NET_PROPERTY_FEATURES, &feature);
82 while (feature)
84 char *feature_name, *feature_value;
85 quvi_net_propfeat_t pfeat;
87 pfeat = (quvi_net_propfeat_t) quvi_llst_data(feature);
89 quvi_net_getprop_feat(pfeat,
90 QUVI_NET_PROPERTY_FEATURE_NAME, &feature_name);
92 quvi_net_getprop_feat(pfeat,
93 QUVI_NET_PROPERTY_FEATURE_VALUE, &feature_value);
95 if (strcmp(feature_name, "arbitrary_cookie") == 0)
97 soup_message_headers_append(
98 m->request_headers, "Cookie", feature_value);
101 if (strcmp(feature_name, "user_agent") == 0)
103 soup_message_headers_append(
104 m->request_headers, "User-Agent", feature_value);
107 feature = quvi_llst_next(feature);
109 #endif
112 static SoupSession *session = NULL;
114 static void send_message(quvi_net_t n, SoupMessage **message,
115 guint *status, SoupMessageFlags msg_flags,
116 const int head_flag, const int lua_feats)
118 char *url = NULL;
120 quvi_net_getprop(n, QUVI_NET_PROPERTY_URL, &url);
122 *message = soup_message_new(head_flag ? "HEAD":"GET", url);
124 if (msg_flags)
125 soup_message_set_flags(*message, msg_flags);
127 /* Even if this is conditional here, the current library design sets
128 * these options (set in the LUA website scripts with quvi.fetch
129 * call) for fetch callback only. */
130 if (lua_feats)
131 set_feats_from_lua_script(n, *message);
133 *status = soup_session_send_message(session, *message);
135 quvi_net_setprop(n, QUVI_NET_PROPERTY_RESPONSECODE, *status);
138 static QUVIcode fetch_callback(quvi_net_t n)
140 SoupMessage *m;
141 guint status;
143 send_message(n, &m, &status, 0, 0, 1 /* Set LUA opts flag */);
145 if (SOUP_STATUS_IS_SUCCESSFUL(status))
147 quvi_net_setprop(n, QUVI_NET_PROPERTY_CONTENT, m->response_body->data);
148 return (QUVI_OK);
151 quvi_net_seterr(n, "%s (http/%d)", m->reason_phrase, status);
153 return (QUVI_CALLBACK);
156 static QUVIcode resolve_callback(quvi_net_t n)
158 SoupMessage *m;
159 guint status;
161 send_message(n, &m, &status, SOUP_MESSAGE_NO_REDIRECT, 0, 0);
163 if (SOUP_STATUS_IS_REDIRECTION(status))
165 const char *r_url =
166 soup_message_headers_get_one(m->response_headers, "Location");
168 quvi_net_setprop(n, QUVI_NET_PROPERTY_REDIRECTURL, r_url);
170 else if (!SOUP_STATUS_IS_SUCCESSFUL(status))
172 quvi_net_seterr(n, "%s (http/%d)", m->reason_phrase, status);
173 return (QUVI_CALLBACK);
176 return (QUVI_OK);
179 static QUVIcode verify_callback(quvi_net_t n)
181 SoupMessage *m;
182 guint status;
184 send_message(n, &m, &status, 0, 1 /* HEAD */, 0);
186 if (SOUP_STATUS_IS_SUCCESSFUL(status))
188 goffset cl =
189 soup_message_headers_get_content_length(m->response_headers);
191 const char *ct =
192 soup_message_headers_get_content_type(m->response_headers, NULL);
194 quvi_net_setprop(n, QUVI_NET_PROPERTY_CONTENTTYPE, ct);
195 quvi_net_setprop(n, QUVI_NET_PROPERTY_CONTENTLENGTH, cl);
197 return (QUVI_OK);
200 quvi_net_seterr(n, "%s (http/%d)", m->reason_phrase, status);
202 return (QUVI_CALLBACK);
205 static void help()
207 printf(
208 "Usage: callback_libsoup [--help|--log] [URL]\n\n"
209 " -h,--help .. Print help and exit\n"
210 " -l,--logger .. Enable logger\n\n"
211 "Note: Unless URL is specified, the default URL will be used\n");
212 exit(0);
215 int main (int argc, char *argv[])
217 char *url = "http://is.gd/yFNPMR";
218 quvi_media_t m;
219 int flag_log;
220 QUVIcode rc;
221 quvi_t q;
223 flag_log = 0;
225 if (argc > 1)
227 int i;
228 for (i=1; i<argc; ++i)
230 if (strcmp(argv[i], "-l") == 0
231 || strcmp(argv[i], "--logger") == 0)
233 flag_log = 1;
235 else if (strcmp(argv[i], "-h") == 0
236 || strcmp(argv[i], "--help") == 0)
238 help();
240 else
241 url = argv[i];
245 rc = quvi_init(&q);
246 check_error(q,rc);
248 quvi_setopt(q, QUVIOPT_STATUSFUNCTION, &status_callback);
249 quvi_setopt(q, QUVIOPT_FETCHFUNCTION, &fetch_callback);
250 quvi_setopt(q, QUVIOPT_RESOLVEFUNCTION, &resolve_callback);
251 quvi_setopt(q, QUVIOPT_VERIFYFUNCTION, &verify_callback);
253 g_type_init();
255 #ifdef HAVE_LIBSOUP_GNOME
256 session = soup_session_async_new_with_options(
257 SOUP_SESSION_ADD_FEATURE_BY_TYPE,
258 SOUP_TYPE_PROXY_RESOLVER_GNOME,
259 NULL
261 #else
262 session = soup_session_async_new();
263 #endif
265 if (flag_log)
267 SoupLogger *log = soup_logger_new(SOUP_LOGGER_LOG_HEADERS, -1);
268 soup_session_add_feature(session, SOUP_SESSION_FEATURE(log));
269 g_object_unref(log);
272 rc = quvi_parse(q, url, &m);
273 check_error(q, rc);
275 quvi_parse_close(&m);
276 quvi_close(&q);
278 return (0);
281 /* vim: set ts=2 sw=2 tw=72 expandtab: */