it builds! (note that some autogenerated files are hardcoded now, and you can't build...
[k8lowj.git] / src / network-soup.c
blob52690755738bb06477e6e1ba21243486c4a39247
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2005 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
5 */
6 #ifdef HAVE_SOUP
8 #include "glib-all.h"
10 #include <libsoup/soup.h>
11 #include <string.h>
13 #include "conf.h"
14 #include "network.h"
15 #include "network-internal.h"
17 typedef struct {
18 NetStatusCallback user_cb;
19 gpointer user_data;
20 int current, total;
21 } CallbackInfo;
23 static void
24 got_chunk_cb(SoupMessage *msg, CallbackInfo *info) {
25 NetStatusProgress progress = {0};
26 const char* clen;
28 if (info->total == 0) {
29 clen = soup_message_get_header(msg->response_headers,
30 "Content-length");
31 if (!clen)
32 return;
33 info->total = atoi(clen);
35 info->current += msg->response.length;
37 progress.current = info->current;
38 progress.total = info->total;
39 info->user_cb(NET_STATUS_PROGRESS, &progress, info->user_data);
42 GString*
43 net_post_blocking(const char *url, GSList *headers, GString *post,
44 NetStatusCallback cb, gpointer data,
45 GError **err) {
46 SoupUri* suri = NULL;
47 SoupMessage *req = NULL;
48 SoupSocket *sock = NULL;
49 guint status = 0;
50 GString *response = NULL;
51 CallbackInfo info = { cb, data, 0, 0 };
53 suri = soup_uri_new(conf.options.useproxy ? conf.proxy : url);
54 sock = soup_socket_client_new_sync(suri->host, suri->port, NULL, &status);
55 if (status != SOUP_STATUS_OK) {
56 g_set_error(err, NET_ERROR, NET_ERROR_GENERIC,
57 soup_status_get_phrase(status));
58 goto out;
60 g_free(suri);
61 suri = NULL;
63 suri = soup_uri_new(url);
64 if (conf.options.useproxy && conf.options.useproxyauth) {
65 g_free(suri->user);
66 g_free(suri->passwd);
67 suri->user = g_strdup(conf.proxyuser);
68 suri->passwd = g_strdup(conf.proxypass);
70 req = soup_message_new_from_uri(post ? "POST" : "GET", suri);
71 g_signal_connect(G_OBJECT(req), "got-chunk",
72 G_CALLBACK(got_chunk_cb), &info);
73 for (; headers; headers = headers->next) {
74 char *header = headers->data;
75 /* soup wants the key and value separate, so we have to munge this
76 * a bit. */
77 char *colonpos = strchr(header, ':');
78 *colonpos = 0;
79 soup_message_add_header(req->request_headers, header, colonpos+1);
80 *colonpos = ':';
82 soup_message_set_request(req, "application/x-www-form-urlencoded",
83 SOUP_BUFFER_USER_OWNED, post->str, post->len);
85 soup_message_send_request(req, sock, conf.options.useproxy);
86 if (status != SOUP_STATUS_OK) {
87 g_set_error(err, NET_ERROR, NET_ERROR_GENERIC,
88 soup_status_get_phrase(status));
89 goto out;
92 response = g_string_new_len(req->response.body, req->response.length);
94 if (conf.options.netdump)
95 fprintf(stderr, _("Response: [%s]\n"), response->str);
97 out:
98 if (suri) soup_uri_free(suri);
99 if (sock) {
100 soup_socket_disconnect(sock);
101 g_object_unref(G_OBJECT(sock));
103 if (req) g_object_unref(G_OBJECT(req));
105 return response;
108 #endif