1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2005 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
10 #include <libsoup/soup.h>
15 #include "network-internal.h"
18 NetStatusCallback user_cb
;
24 got_chunk_cb(SoupMessage
*msg
, CallbackInfo
*info
) {
25 NetStatusProgress progress
= {0};
28 if (info
->total
== 0) {
29 clen
= soup_message_get_header(msg
->response_headers
,
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
);
43 net_post_blocking(const char *url
, GSList
*headers
, GString
*post
,
44 NetStatusCallback cb
, gpointer data
,
47 SoupMessage
*req
= NULL
;
48 SoupSocket
*sock
= NULL
;
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
));
63 suri
= soup_uri_new(url
);
64 if (conf
.options
.useproxy
&& conf
.options
.useproxyauth
) {
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
77 char *colonpos
= strchr(header
, ':');
79 soup_message_add_header(req
->request_headers
, header
, colonpos
+1);
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
));
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
);
98 if (suri
) soup_uri_free(suri
);
100 soup_socket_disconnect(sock
);
101 g_object_unref(G_OBJECT(sock
));
103 if (req
) g_object_unref(G_OBJECT(req
));