1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
10 #include <curl/curl.h>
14 #include "network-internal.h"
20 NetStatusCallback user_cb
;
25 static size_t curlwrite_cb (void *ptr
, size_t size
, size_t nmemb
, void *data
) {
26 RequestData
*requestdata
= data
;
28 g_string_append_len(requestdata
->response
, ptr
, size
*nmemb
);
29 curl_easy_getinfo(requestdata
->curl
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &contentlength
);
30 if (requestdata
->user_cb
) {
31 NetStatusProgress progress
= { 0 };
32 progress
.current
= requestdata
->response
->len
;
33 progress
.total
= (int)contentlength
;
34 requestdata
->user_cb(NET_STATUS_PROGRESS
, &progress
, requestdata
->user_data
);
41 * see comment in run_curl_request().
43 curl_progress_cb(void *cp, size_t dlt, size_t dln, size_t ult, size_t uln) {
44 g_print("progress: %d %d %d %d\n", dlt, dln, ult, uln);
49 GString
*net_post_blocking (const char *url
, GSList
*headers
, GString
*post
, NetStatusCallback cb
, gpointer data
, GError
**err
) {
52 char proxyuserpass
[1024];
53 char errorbuf
[CURL_ERROR_SIZE
];
54 RequestData requestdata
= { 0 };
55 struct curl_slist
*curlheaders
= NULL
;
57 curl
= curl_easy_init();
59 g_set_error(err
, NET_ERROR
, NET_ERROR_GENERIC
, "%s", _("Unable to intialize CURL"));
63 curl_easy_setopt(curl
, CURLOPT_VERBOSE
, conf
.options
.netdump
!= 0);
64 curl_easy_setopt(curl
, CURLOPT_NOPROGRESS
, 1);
65 curl_easy_setopt(curl
, CURLOPT_ERRORBUFFER
, errorbuf
);
67 * curl's progress function is mostly useless; we instead track writes.
69 * curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, curl_progress_cb);
70 * curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, nr);
72 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
74 if (conf
.options
.useproxy
) {
75 curl_easy_setopt(curl
, CURLOPT_PROXY
, conf
.proxy
);
76 if (conf
.options
.useproxyauth
) {
77 g_snprintf(proxyuserpass
, sizeof(proxyuserpass
), "%s:%s", conf
.proxyuser
, conf
.proxypass
);
78 curl_easy_setopt(curl
, CURLOPT_PROXYUSERPWD
, proxyuserpass
);
83 for (GSList
*l
= headers
; l
; l
= l
->next
) curlheaders
= curl_slist_append(curlheaders
, l
->data
);
84 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, curlheaders
);
88 curl_easy_setopt(curl
, CURLOPT_POSTFIELDS
, post
->str
);
89 curl_easy_setopt(curl
, CURLOPT_POSTFIELDSIZE
, post
->len
);
90 curl_easy_setopt(curl
, CURLOPT_POST
, 1);
93 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, curlwrite_cb
);
94 requestdata
.curl
= curl
;
95 requestdata
.response
= g_string_sized_new(READ_BLOCK_SIZE
);
96 requestdata
.user_cb
= cb
;
97 requestdata
.user_data
= data
;
98 curl_easy_setopt(curl
, CURLOPT_FILE
, &requestdata
);
100 if (conf
.options
.netdump
&& post
) fprintf(stderr
, _("Request: [%s]\n"), post
->str
);
102 curlres
= curl_easy_perform(curl
);
103 if (curlheaders
) curl_slist_free_all(curlheaders
);
104 curl_easy_cleanup(curl
);
106 if (curlres
!= CURLE_OK
) {
107 g_set_error(err
, NET_ERROR
, NET_ERROR_GENERIC
, _("cURL error: %s."), errorbuf
);
108 g_string_free(requestdata
.response
, TRUE
);
112 if (conf
.options
.netdump
) fprintf(stderr
, _("Response: [%s]\n"), requestdata
.response
->str
);
114 return requestdata
.response
;