Remove pornhub.lua
[quvi.git] / src / libquvi / net_api.c
blob12f6d322df7fac0ede8673bb11a14d659a91af2a
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 #include "config.h"
22 #include "quvi/quvi.h"
23 #include "quvi/net.h"
24 #include "quvi/llst.h"
26 #include "internal.h"
28 extern const char empty[]; /* quvi_api.c */
30 /* Title: quvi Network API */
32 static QUVIcode _net_getprop(_quvi_net_t n, QUVInetProperty p, ...)
34 QUVIcode rc;
35 va_list arg;
36 double *dp;
37 char **sp;
38 void **vp;
39 long *lp;
40 int type;
42 rc = QUVI_OK;
43 dp = 0;
44 sp = 0;
45 vp = 0;
46 lp = 0;
48 va_start(arg, p);
49 type = QUVIPROPERTY_TYPEMASK & (int)p;
51 switch (type)
53 case QUVIPROPERTY_DOUBLE:
54 _init_from_arg(dp, double*);
55 case QUVIPROPERTY_STRING:
56 _init_from_arg(sp, char**);
57 case QUVIPROPERTY_LONG:
58 _init_from_arg(lp, long*);
59 case QUVIPROPERTY_VOID:
60 _init_from_arg(vp, void **);
61 default:
62 rc = QUVI_INVARG;
64 va_end(arg);
66 if (rc != QUVI_OK)
67 return (rc);
69 switch (p)
71 case QUVI_NET_PROPERTY_URL:
72 _set_s(n->url);
73 case QUVI_NET_PROPERTY_REDIRECTURL:
74 _set_s(n->redirect.url);
75 case QUVI_NET_PROPERTY_CONTENT:
76 _set_s(n->fetch.content);
77 case QUVI_NET_PROPERTY_CONTENTTYPE:
78 _set_s(n->verify.content_type);
79 case QUVI_NET_PROPERTY_CONTENTLENGTH:
80 _set_from_value_n(dp, n->verify.content_length);
81 case QUVI_NET_PROPERTY_RESPONSECODE:
82 _set_from_value_n(lp, n->resp_code);
83 case QUVI_NET_PROPERTY_FEATURES:
84 _set_v(n->features);
85 default:
86 rc = QUVI_INVARG;
88 return (rc);
92 * Function: quvi_net_getprop
94 * Returns a network property.
96 * Parameters:
97 * net - Network handle
98 * property - Property ID
100 * Returns:
101 * Non-zero value if an error occurred.
103 QUVIcode quvi_net_getprop(quvi_net_propfeat_t net,
104 QUVInetProperty property,
105 ...)
107 va_list arg;
108 void *p;
110 _is_badhandle(net);
112 va_start(arg, property);
113 p = va_arg(arg, void*);
114 va_end(arg);
116 return (_net_getprop(net, property, p));
119 static QUVIcode _net_setprop(_quvi_net_t n, QUVInetProperty p, va_list arg)
121 switch (p)
123 case QUVI_NET_PROPERTY_URL:
124 _set_alloc_s(n->url);
125 case QUVI_NET_PROPERTY_FEATURES:
126 break; /* Ignored: read-only */
127 case QUVI_NET_PROPERTY_REDIRECTURL:
128 _set_alloc_s(n->redirect.url);
129 case QUVI_NET_PROPERTY_CONTENT:
130 _set_alloc_s(n->fetch.content);
131 case QUVI_NET_PROPERTY_CONTENTTYPE:
132 _set_alloc_s(n->verify.content_type);
133 case QUVI_NET_PROPERTY_CONTENTLENGTH:
134 _set_from_arg_n(n->verify.content_length);
135 case QUVI_NET_PROPERTY_RESPONSECODE:
136 _set_from_arg_n(n->resp_code);
137 default:
138 return (QUVI_INVARG);
140 return (QUVI_OK);
144 * Function: quvi_net_setprop
146 * Sets a network property.
148 * Parameters:
149 * net - Network handle
150 * property - Property ID
151 * ... - Parameter
153 * Returns:
154 * Non-zero value if an error occurred.
156 QUVIcode quvi_net_setprop(quvi_net_t net, QUVInetProperty property, ...)
158 va_list arg;
159 QUVIcode rc;
161 _is_badhandle(net);
163 va_start(arg, property);
164 rc = _net_setprop(net, property, arg);
165 va_end(arg);
167 return (rc);
170 static QUVIcode _net_getprop_feat(_quvi_net_propfeat_t n,
171 QUVInetPropertyFeature feature,
172 ...)
174 QUVIcode rc;
175 va_list arg;
176 double *dp;
177 char **sp;
178 #ifdef _UNUSED
179 void **vp;
180 long *lp;
181 #endif
182 int type;
184 rc = QUVI_OK;
185 dp = 0;
186 sp = 0;
187 #ifdef _UNUSED
188 vp = 0;
189 lp = 0;
190 #endif
192 va_start(arg, feature);
193 type = QUVIPROPERTY_TYPEMASK & (int)feature;
195 switch (type)
197 case QUVIPROPERTY_DOUBLE:
198 _init_from_arg(dp, double*);
199 case QUVIPROPERTY_STRING:
200 _init_from_arg(sp, char**);
201 default:
202 rc = QUVI_INVARG;
204 va_end(arg);
206 if (rc != QUVI_OK)
207 return (rc);
209 switch (feature)
211 case QUVI_NET_PROPERTY_FEATURE_NAME:
212 _set_s(n->name);
213 case QUVI_NET_PROPERTY_FEATURE_VALUE:
214 _set_s(n->value);
215 default:
216 rc = QUVI_INVARG;
218 return (rc);
222 * Function: quvi_net_getprop_feat
224 * Returns a network property feature.
226 * Parameters:
227 * handle - Property feature handle
228 * feature - Property feature ID
229 * ... - Parameter
231 * Returns:
232 * Non-zero value if an error occurred.
234 QUVIcode quvi_net_getprop_feat(quvi_net_propfeat_t handle,
235 QUVInetPropertyFeature feature,
236 ...)
238 va_list arg;
239 void *p;
241 _is_badhandle(handle);
243 va_start(arg, feature);
244 p = va_arg(arg, void*);
245 va_end(arg);
247 return (_net_getprop_feat(handle, feature, p));
251 * Function: quvi_net_seterr
253 * Sets a network error message.
255 * Parameters:
256 * net - Network handle
257 * fmt - Format string
258 * ... - Parameter
260 * Returns:
261 * Non-zero value if an error occurred.
263 QUVIcode quvi_net_seterr(quvi_net_t net, const char *fmt, ...)
265 _quvi_net_t n;
266 va_list args;
268 _is_badhandle(net);
269 n = (_quvi_net_t) net;
271 va_start(args, fmt);
272 vafreprintf(&n->errmsg, fmt, args);
274 return (QUVI_OK);
277 /* Title: Convenience functions */
279 extern const char *net_prop_feats[];
281 static const char *_feat_to_str(QUVInetPropertyFeatureName id)
283 const char *s = NULL;
285 if (id > QUVI_NET_PROPERTY_FEATURE_NAME_NONE
286 && id < _QUVI_NET_PROPERTY_FEATURE_NAME_LAST)
288 s = net_prop_feats[id];
290 return (s);
294 * Function: quvi_net_get_one_prop_feat
296 * Returns the first matching property feature from the list. A
297 * convenience function that allows finding the feature by an ID rather
298 * than a string.
300 * Parameters:
301 * net - Network handle
302 * name - Property feature ID
304 * Returns:
305 * A null-terminated string, otherwise NULL.
307 * See Also:
308 * <quvi_net_getprop_feat>
310 char *quvi_net_get_one_prop_feat(quvi_net_t net,
311 QUVInetPropertyFeatureName name)
313 quvi_llst_node_t opt;
315 quvi_net_getprop(net, QUVI_NET_PROPERTY_FEATURES, &opt);
317 while (opt)
319 const char *feat_name, *feat_value, *s;
320 quvi_net_propfeat_t popt;
322 popt = (quvi_net_propfeat_t) quvi_llst_data(opt);
324 quvi_net_getprop_feat(popt,
325 QUVI_NET_PROPERTY_FEATURE_NAME, &feat_name);
327 quvi_net_getprop_feat(popt,
328 QUVI_NET_PROPERTY_FEATURE_VALUE, &feat_value);
330 s = _feat_to_str(name);
332 if (s && !strcmp(feat_name,s))
333 return ((char*)feat_value);
335 opt = quvi_llst_next(opt);
337 return (NULL);
341 /* vim: set ts=2 sw=2 tw=72 expandtab: */