/* logjam - a GTK client for LiveJournal.
 * Copyright (C) 2004 Evan Martin <martine@danga.com>
 *
 * vim: tabstop=4 shiftwidth=4 noexpandtab :
 */

#include "config.h"
#include <glib.h>

#include <libxml/nanohttp.h>

#include "util.h"
#include "conf.h"
#include "network.h"
#include "network-internal.h"

GString*
net_post_blocking(const char *url, GSList *headers, GString *post,
                  NetStatusCallback cb, gpointer data,
                  GError **err) {
	void *ctx = NULL;
	char *urlreq;
	char buf[2048];
	char *headerstr = NULL;
	GString *response = NULL;
	gboolean success = FALSE;
	int len;
	NetStatusProgress progress;

	if (conf.options.netdump) 
		fprintf(stderr, _("Request: [%s]\n"), post->str);

	xmlNanoHTTPInit();
	
	if (conf.options.useproxy) {
		if (conf.proxyuser) {
			g_set_error(err, NET_ERROR, NET_ERROR_GENERIC,
					_("LogJam was built with libxml's NanoHTTP support, "
						"which does not support proxy authentication."));
			goto out;
		}
		xmlNanoHTTPScanProxy(conf.proxy);
	} else {
		/* reset any potentially lingering proxy information. */
		xmlNanoHTTPScanProxy(NULL);
	}

	if (headers) {
		GString *hs = g_string_new(NULL);
		GSList *l;
		for (l = headers; l; l = l->next) {
			g_string_append(hs, l->data);
			g_string_append(hs, "\r\n");
		}
		headerstr = hs->str;
		g_string_free(hs, FALSE);
	}
	urlreq = g_strdup_printf("%s/interface/flat", url);
	ctx = xmlNanoHTTPMethod(urlreq, "POST",
			post->str, NULL, headerstr, post->len);

	if (!ctx) {
		g_set_error(err, NET_ERROR, NET_ERROR_GENERIC,
				_("xmlNanoHTTPOpen error."));
		goto out;
	}

	response = g_string_new(NULL);
	for (;;) {
		len = xmlNanoHTTPRead(ctx, buf, sizeof(buf));
		if (len < 0) {
			g_set_error(err, NET_ERROR, NET_ERROR_GENERIC,
					_("xmlNanoHTTPOpen error."));
			goto out;
		} else if (len == 0) {
			break;
		} else {
			g_string_append_len(response, buf, len);
		}

		if (cb) {
			progress.current = response->len;
			progress.total = 0;/* XXX (int)contentlength; */

			cb(NET_STATUS_PROGRESS, &progress, data);
		}
	}

	success = TRUE;

	if (conf.options.netdump) 
		fprintf(stderr, _("Response: [%s]\n"), response->str);

out:
	g_free(headerstr);
	g_free(urlreq);
	if (ctx)
		xmlNanoHTTPClose(ctx);
	if (!success && response) {
		g_string_free(response, TRUE);
		response = NULL;
	}
	return response;
}

