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
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
35 fb_http_error_quark(void)
39 if (G_UNLIKELY(q
== 0)) {
40 q
= g_quark_from_static_string("fb-http-error-quark");
47 fb_http_conns_new(void)
51 cons
= g_new0(FbHttpConns
, 1);
52 cons
->cons
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
57 fb_http_conns_free(FbHttpConns
*cons
)
59 g_return_if_fail(cons
!= NULL
);
61 g_hash_table_destroy(cons
->cons
);
66 fb_http_conns_cancel_all(FbHttpConns
*cons
)
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
);
84 fb_http_conns_is_canceled(FbHttpConns
*cons
)
86 g_return_val_if_fail(cons
!= NULL
, TRUE
);
87 return cons
->canceled
;
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
);
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
);
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
);
115 fb_http_error_chk(PurpleHttpResponse
*res
, GError
**error
)
120 if (purple_http_response_is_successful(res
)) {
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
);
131 fb_http_params_new(void)
133 return g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
137 fb_http_params_new_parse(const gchar
*data
, gboolean isurl
)
144 FbHttpParams
*params
;
146 params
= fb_http_params_new();
153 data
= strchr(data
, '?');
159 tail
= strchr(++data
, '#');
162 data
= g_strndup(data
, tail
- data
);
164 data
= g_strdup(data
);
168 ps
= g_strsplit(data
, "&", 0);
170 for (i
= 0; ps
[i
] != NULL
; i
++) {
172 val
= strchr(ps
[i
], '=');
179 key
= g_uri_unescape_string(key
, NULL
);
180 val
= g_uri_unescape_string(val
, NULL
);
181 g_hash_table_replace(params
, key
, val
);
185 g_free((gchar
*) data
);
193 fb_http_params_free(FbHttpParams
*params
)
195 g_hash_table_destroy(params
);
199 fb_http_params_close(FbHttpParams
*params
, const gchar
*url
)
206 g_hash_table_iter_init(&iter
, params
);
207 ret
= g_string_new(NULL
);
209 while (g_hash_table_iter_next(&iter
, &key
, &val
)) {
211 g_hash_table_iter_remove(&iter
);
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
);
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
);
234 fb_http_params_get(FbHttpParams
*params
, const gchar
*name
, GError
**error
)
238 ret
= g_hash_table_lookup(params
, name
);
241 g_set_error(error
, FB_HTTP_ERROR
, FB_HTTP_ERROR_NOMATCH
,
242 _("No matches for %s"), name
);
250 fb_http_params_get_bool(FbHttpParams
*params
, const gchar
*name
,
255 val
= fb_http_params_get(params
, name
, error
);
261 return g_ascii_strcasecmp(val
, "TRUE") == 0;
265 fb_http_params_get_dbl(FbHttpParams
*params
, const gchar
*name
,
270 val
= fb_http_params_get(params
, name
, error
);
276 return g_ascii_strtod(val
, NULL
);
280 fb_http_params_get_int(FbHttpParams
*params
, const gchar
*name
,
285 val
= fb_http_params_get(params
, name
, error
);
291 return g_ascii_strtoll(val
, NULL
, 10);
295 fb_http_params_get_str(FbHttpParams
*params
, const gchar
*name
,
298 return fb_http_params_get(params
, name
, error
);
302 fb_http_params_dup_str(FbHttpParams
*params
, const gchar
*name
,
307 str
= fb_http_params_get(params
, name
, error
);
308 return g_strdup(str
);
312 fb_http_params_set(FbHttpParams
*params
, const gchar
*name
, gchar
*value
)
316 key
= g_strdup(name
);
317 g_hash_table_replace(params
, key
, value
);
321 fb_http_params_set_bool(FbHttpParams
*params
, const gchar
*name
,
326 val
= g_strdup(value
? "true" : "false");
327 fb_http_params_set(params
, name
, val
);
331 fb_http_params_set_dbl(FbHttpParams
*params
, const gchar
*name
, gdouble value
)
335 val
= g_strdup_printf("%f", value
);
336 fb_http_params_set(params
, name
, val
);
340 fb_http_params_set_int(FbHttpParams
*params
, const gchar
*name
, gint64 value
)
344 val
= g_strdup_printf("%" G_GINT64_FORMAT
, value
);
345 fb_http_params_set(params
, name
, val
);
349 fb_http_params_set_str(FbHttpParams
*params
, const gchar
*name
,
354 val
= g_strdup(value
);
355 fb_http_params_set(params
, name
, val
);
359 fb_http_params_set_strf(FbHttpParams
*params
, const gchar
*name
,
360 const gchar
*format
, ...)
365 va_start(ap
, format
);
366 val
= g_strdup_vprintf(format
, ap
);
369 fb_http_params_set(params
, name
, val
);
373 fb_http_urlcmp(const gchar
*url1
, const gchar
*url2
, gboolean protocol
)
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
)) {
399 purl1
= purple_http_url_parse(url1
);
402 return g_ascii_strcasecmp(url1
, url2
) == 0;
405 purl2
= purple_http_url_parse(url2
);
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
)) {
422 if (ret
&& protocol
) {
423 int1
= purple_http_url_get_port(purl1
);
424 int2
= purple_http_url_get_port(purl2
);
431 purple_http_url_free(purl1
);
432 purple_http_url_free(purl2
);