Migrate certificates, icons, logs to XDG dirs
[pidgin-git.git] / libpurple / protocols / facebook / http.c
blob0f413ecfad3f90b684f8042b8b30b760390a38c6
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
22 #include "internal.h"
24 #include <string.h>
26 #include "http.h"
28 struct _FbHttpConns
30 GHashTable *cons;
31 gboolean canceled;
34 GQuark
35 fb_http_error_quark(void)
37 static GQuark q = 0;
39 if (G_UNLIKELY(q == 0)) {
40 q = g_quark_from_static_string("fb-http-error-quark");
43 return q;
46 FbHttpConns *
47 fb_http_conns_new(void)
49 FbHttpConns *cons;
51 cons = g_new0(FbHttpConns, 1);
52 cons->cons = g_hash_table_new(g_direct_hash, g_direct_equal);
53 return cons;
56 void
57 fb_http_conns_free(FbHttpConns *cons)
59 g_return_if_fail(cons != NULL);
61 g_hash_table_destroy(cons->cons);
62 g_free(cons);
65 void
66 fb_http_conns_cancel_all(FbHttpConns *cons)
68 GHashTableIter iter;
69 gpointer con;
71 g_return_if_fail(cons != NULL);
72 g_return_if_fail(!cons->canceled);
74 cons->canceled = TRUE;
75 g_hash_table_iter_init(&iter, cons->cons);
77 while (g_hash_table_iter_next(&iter, &con, NULL)) {
78 g_hash_table_iter_remove(&iter);
79 purple_http_conn_cancel(con);
83 gboolean
84 fb_http_conns_is_canceled(FbHttpConns *cons)
86 g_return_val_if_fail(cons != NULL, TRUE);
87 return cons->canceled;
90 void
91 fb_http_conns_add(FbHttpConns *cons, PurpleHttpConnection *con)
93 g_return_if_fail(cons != NULL);
94 g_return_if_fail(!cons->canceled);
95 g_hash_table_replace(cons->cons, con, con);
98 void
99 fb_http_conns_remove(FbHttpConns *cons, PurpleHttpConnection *con)
101 g_return_if_fail(cons != NULL);
102 g_return_if_fail(!cons->canceled);
103 g_hash_table_remove(cons->cons, con);
106 void
107 fb_http_conns_reset(FbHttpConns *cons)
109 g_return_if_fail(cons != NULL);
110 cons->canceled = FALSE;
111 g_hash_table_remove_all(cons->cons);
114 gboolean
115 fb_http_error_chk(PurpleHttpResponse *res, GError **error)
117 const gchar *msg;
118 gint code;
120 if (purple_http_response_is_successful(res)) {
121 return TRUE;
124 msg = purple_http_response_get_error(res);
125 code = purple_http_response_get_code(res);
126 g_set_error(error, FB_HTTP_ERROR, code, "%s", msg);
127 return FALSE;
130 FbHttpParams *
131 fb_http_params_new(void)
133 return g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
136 FbHttpParams *
137 fb_http_params_new_parse(const gchar *data, gboolean isurl)
139 const gchar *tail;
140 gchar *key;
141 gchar **ps;
142 gchar *val;
143 guint i;
144 FbHttpParams *params;
146 params = fb_http_params_new();
148 if (data == NULL) {
149 return params;
152 if (isurl) {
153 data = strchr(data, '?');
155 if (data == NULL) {
156 return params;
159 tail = strchr(++data, '#');
161 if (tail != NULL) {
162 data = g_strndup(data, tail - data);
163 } else {
164 data = g_strdup(data);
168 ps = g_strsplit(data, "&", 0);
170 for (i = 0; ps[i] != NULL; i++) {
171 key = ps[i];
172 val = strchr(ps[i], '=');
174 if (val == NULL) {
175 continue;
178 *(val++) = 0;
179 key = g_uri_unescape_string(key, NULL);
180 val = g_uri_unescape_string(val, NULL);
181 g_hash_table_replace(params, key, val);
184 if (isurl) {
185 g_free((gchar *) data);
188 g_strfreev(ps);
189 return params;
192 void
193 fb_http_params_free(FbHttpParams *params)
195 g_hash_table_destroy(params);
198 gchar *
199 fb_http_params_close(FbHttpParams *params, const gchar *url)
201 GHashTableIter iter;
202 gpointer key;
203 gpointer val;
204 GString *ret;
206 g_hash_table_iter_init(&iter, params);
207 ret = g_string_new(NULL);
209 while (g_hash_table_iter_next(&iter, &key, &val)) {
210 if (val == NULL) {
211 g_hash_table_iter_remove(&iter);
212 continue;
215 if (ret->len > 0) {
216 g_string_append_c(ret, '&');
219 g_string_append_uri_escaped(ret, key, NULL, TRUE);
220 g_string_append_c(ret, '=');
221 g_string_append_uri_escaped(ret, val, NULL, TRUE);
224 if (url != NULL) {
225 g_string_prepend_c(ret, '?');
226 g_string_prepend(ret, url);
229 fb_http_params_free(params);
230 return g_string_free(ret, FALSE);
233 static const gchar *
234 fb_http_params_get(FbHttpParams *params, const gchar *name, GError **error)
236 const gchar *ret;
238 ret = g_hash_table_lookup(params, name);
240 if (ret == NULL) {
241 g_set_error(error, FB_HTTP_ERROR, FB_HTTP_ERROR_NOMATCH,
242 _("No matches for %s"), name);
243 return NULL;
246 return ret;
249 gboolean
250 fb_http_params_get_bool(FbHttpParams *params, const gchar *name,
251 GError **error)
253 const gchar *val;
255 val = fb_http_params_get(params, name, error);
257 if (val == NULL) {
258 return FALSE;
261 return g_ascii_strcasecmp(val, "TRUE") == 0;
264 gdouble
265 fb_http_params_get_dbl(FbHttpParams *params, const gchar *name,
266 GError **error)
268 const gchar *val;
270 val = fb_http_params_get(params, name, error);
272 if (val == NULL) {
273 return 0.0;
276 return g_ascii_strtod(val, NULL);
279 gint64
280 fb_http_params_get_int(FbHttpParams *params, const gchar *name,
281 GError **error)
283 const gchar *val;
285 val = fb_http_params_get(params, name, error);
287 if (val == NULL) {
288 return 0;
291 return g_ascii_strtoll(val, NULL, 10);
294 const gchar *
295 fb_http_params_get_str(FbHttpParams *params, const gchar *name,
296 GError **error)
298 return fb_http_params_get(params, name, error);
301 gchar *
302 fb_http_params_dup_str(FbHttpParams *params, const gchar *name,
303 GError **error)
305 const gchar *str;
307 str = fb_http_params_get(params, name, error);
308 return g_strdup(str);
311 static void
312 fb_http_params_set(FbHttpParams *params, const gchar *name, gchar *value)
314 gchar *key;
316 key = g_strdup(name);
317 g_hash_table_replace(params, key, value);
320 void
321 fb_http_params_set_bool(FbHttpParams *params, const gchar *name,
322 gboolean value)
324 gchar *val;
326 val = g_strdup(value ? "true" : "false");
327 fb_http_params_set(params, name, val);
330 void
331 fb_http_params_set_dbl(FbHttpParams *params, const gchar *name, gdouble value)
333 gchar *val;
335 val = g_strdup_printf("%f", value);
336 fb_http_params_set(params, name, val);
339 void
340 fb_http_params_set_int(FbHttpParams *params, const gchar *name, gint64 value)
342 gchar *val;
344 val = g_strdup_printf("%" G_GINT64_FORMAT, value);
345 fb_http_params_set(params, name, val);
348 void
349 fb_http_params_set_str(FbHttpParams *params, const gchar *name,
350 const gchar *value)
352 gchar *val;
354 val = g_strdup(value);
355 fb_http_params_set(params, name, val);
358 void
359 fb_http_params_set_strf(FbHttpParams *params, const gchar *name,
360 const gchar *format, ...)
362 gchar *val;
363 va_list ap;
365 va_start(ap, format);
366 val = g_strdup_vprintf(format, ap);
367 va_end(ap);
369 fb_http_params_set(params, name, val);
372 gboolean
373 fb_http_urlcmp(const gchar *url1, const gchar *url2, gboolean protocol)
375 const gchar *str1;
376 const gchar *str2;
377 gboolean ret = TRUE;
378 gint int1;
379 gint int2;
380 guint i;
381 PurpleHttpURL *purl1;
382 PurpleHttpURL *purl2;
384 static const const gchar * (*funcs[]) (const PurpleHttpURL *url) = {
385 /* Always first so it can be skipped */
386 purple_http_url_get_protocol,
388 purple_http_url_get_fragment,
389 purple_http_url_get_host,
390 purple_http_url_get_password,
391 purple_http_url_get_path,
392 purple_http_url_get_username
395 if ((url1 == NULL) || (url2 == NULL)) {
396 return url1 == url2;
399 purl1 = purple_http_url_parse(url1);
401 if (purl1 == NULL) {
402 return g_ascii_strcasecmp(url1, url2) == 0;
405 purl2 = purple_http_url_parse(url2);
407 if (purl2 == NULL) {
408 purple_http_url_free(purl1);
409 return g_ascii_strcasecmp(url1, url2) == 0;
412 for (i = protocol ? 0 : 1; i < G_N_ELEMENTS(funcs); i++) {
413 str1 = funcs[i](purl1);
414 str2 = funcs[i](purl2);
416 if (!purple_strequal(str1, str2)) {
417 ret = FALSE;
418 break;
422 if (ret && protocol) {
423 int1 = purple_http_url_get_port(purl1);
424 int2 = purple_http_url_get_port(purl2);
426 if (int1 != int2) {
427 ret = FALSE;
431 purple_http_url_free(purl1);
432 purple_http_url_free(purl2);
433 return ret;