cosmetix
[k8lowj.git] / src / network-libxml.c
blobb3afd0bc70e7e0ee3cd669aec27f2241e7120b36
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2004 Evan Martin <martine@danga.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
5 */
6 #if !defined(HAVE_CURL) && !defined(HAVE_LIBSOUP)
8 #include "glib-all.h"
10 #include <libxml/nanohttp.h>
12 #include "conf.h"
13 #include "network.h"
14 #include "network-internal.h"
17 GString *net_post_blocking (const char *url, GSList *headers, GString *post, NetStatusCallback cb, gpointer data, GError **err) {
18 void *ctx = NULL;
19 char buf[2048];
20 char *headerstr = NULL;
21 GString *response = NULL;
22 gboolean success = FALSE;
23 int len;
24 NetStatusProgress progress;
26 if (conf.options.netdump && post) fprintf(stderr, _("Request: [%s]\n"), post->str);
28 xmlNanoHTTPInit();
30 if (conf.options.useproxy) {
31 if (conf.proxyuser) {
32 g_set_error(err, NET_ERROR, NET_ERROR_GENERIC, "%s", _("LogJam was built with libxml's NanoHTTP support, " "which does not support proxy authentication."));
33 goto out;
35 xmlNanoHTTPScanProxy(conf.proxy);
36 } else {
37 /* reset any potentially lingering proxy information. */
38 xmlNanoHTTPScanProxy(NULL);
41 if (headers) {
42 GString *hs = g_string_new(NULL);
43 GSList *l;
44 for (l = headers; l; l = l->next) {
45 g_string_append(hs, l->data);
46 g_string_append(hs, "\r\n");
48 headerstr = hs->str;
49 g_string_free(hs, FALSE);
51 if (post) {
52 ctx = xmlNanoHTTPMethod(url, "POST", post->str, NULL, headerstr, post->len);
53 } else {
54 ctx = xmlNanoHTTPMethod(url, "GET", NULL, NULL, headerstr, 0);
57 if (!ctx) {
58 g_set_error(err, NET_ERROR, NET_ERROR_GENERIC, "%s", _("xmlNanoHTTPMethod error."));
59 goto out;
62 switch (xmlNanoHTTPReturnCode(ctx)) {
63 case 404: /* HTTP 404 message: file not found. */
64 g_set_error(err, NET_ERROR, NET_ERROR_PROTOCOL, "%s", _("File not found."));
65 goto out;
66 default: ; /* supress warning */
69 response = g_string_new(NULL);
70 for (;;) {
71 len = xmlNanoHTTPRead(ctx, buf, sizeof(buf));
72 if (len < 0) {
73 g_set_error(err, NET_ERROR, NET_ERROR_GENERIC, "%s", _("xmlNanoHTTPRead error."));
74 goto out;
75 } else if (len == 0) {
76 break;
77 } else {
78 g_string_append_len(response, buf, len);
80 if (cb) {
81 progress.current = response->len;
82 progress.total = 0; /* XXX (int)contentlength; */
83 cb(NET_STATUS_PROGRESS, &progress, data);
87 success = TRUE;
89 if (conf.options.netdump) fprintf(stderr, _("Response: [%s]\n"), response->str);
91 out:
92 g_free(headerstr);
93 if (ctx) xmlNanoHTTPClose(ctx);
94 if (!success && response) {
95 g_string_free(response, TRUE);
96 response = NULL;
98 return response;
101 #endif