1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2004 Evan Martin <martine@danga.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
10 #include <libxml/nanohttp.h>
14 #include "network-internal.h"
17 net_post_blocking(const char *url
, GSList
*headers
, GString
*post
,
18 NetStatusCallback cb
, gpointer data
,
22 char *headerstr
= NULL
;
23 GString
*response
= NULL
;
24 gboolean success
= FALSE
;
26 NetStatusProgress progress
;
28 if (conf
.options
.netdump
&& post
)
29 fprintf(stderr
, _("Request: [%s]\n"), post
->str
);
33 if (conf
.options
.useproxy
) {
35 g_set_error(err
, NET_ERROR
, NET_ERROR_GENERIC
, "%s",
36 _("LogJam was built with libxml's NanoHTTP support, "
37 "which does not support proxy authentication."));
40 xmlNanoHTTPScanProxy(conf
.proxy
);
42 /* reset any potentially lingering proxy information. */
43 xmlNanoHTTPScanProxy(NULL
);
47 GString
*hs
= g_string_new(NULL
);
49 for (l
= headers
; l
; l
= l
->next
) {
50 g_string_append(hs
, l
->data
);
51 g_string_append(hs
, "\r\n");
54 g_string_free(hs
, FALSE
);
57 ctx
= xmlNanoHTTPMethod(url
, "POST",
58 post
->str
, NULL
, headerstr
, post
->len
);
60 ctx
= xmlNanoHTTPMethod(url
, "GET", NULL
, NULL
, headerstr
, 0);
63 g_set_error(err
, NET_ERROR
, NET_ERROR_GENERIC
, "%s",
64 _("xmlNanoHTTPMethod error."));
68 switch (xmlNanoHTTPReturnCode(ctx
)) {
70 /* HTTP 404 message: file not found. */
71 g_set_error(err
, NET_ERROR
, NET_ERROR_PROTOCOL
, "%s",
72 _("File not found."));
75 ; /* supress warning */
78 response
= g_string_new(NULL
);
80 len
= xmlNanoHTTPRead(ctx
, buf
, sizeof(buf
));
82 g_set_error(err
, NET_ERROR
, NET_ERROR_GENERIC
, "%s",
83 _("xmlNanoHTTPRead error."));
85 } else if (len
== 0) {
88 g_string_append_len(response
, buf
, len
);
92 progress
.current
= response
->len
;
93 progress
.total
= 0;/* XXX (int)contentlength; */
95 cb(NET_STATUS_PROGRESS
, &progress
, data
);
101 if (conf
.options
.netdump
)
102 fprintf(stderr
, _("Response: [%s]\n"), response
->str
);
107 xmlNanoHTTPClose(ctx
);
108 if (!success
&& response
) {
109 g_string_free(response
, TRUE
);