fixed some clang warnings
[k8lowj.git] / src / network-curl.c
blobf9a2bc12597610b2f2c787123041037ba67743cc
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
3 */
4 #ifdef HAVE_CURL
6 #include "glib-all.h"
8 #include <unistd.h>
10 #include <curl/curl.h>
12 #include "conf.h"
13 #include "network.h"
14 #include "network-internal.h"
17 typedef struct {
18 CURL *curl;
19 GString *response;
20 NetStatusCallback user_cb;
21 gpointer user_data;
22 } RequestData;
25 static size_t curlwrite_cb (void *ptr, size_t size, size_t nmemb, void *data) {
26 RequestData *requestdata = data;
27 double contentlength;
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);
36 return size *nmemb;
41 * see comment in run_curl_request().
42 static int
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);
45 return 0;
46 }*/
49 GString *net_post_blocking (const char *url, GSList *headers, GString *post, NetStatusCallback cb, gpointer data, GError **err) {
50 CURL *curl;
51 CURLcode curlres;
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();
58 if (curl == NULL) {
59 g_set_error(err, NET_ERROR, NET_ERROR_GENERIC, "%s", _("Unable to intialize CURL"));
60 return NULL;
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);
82 if (headers) {
83 for (GSList *l = headers; l; l = l->next) curlheaders = curl_slist_append(curlheaders, l->data);
84 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curlheaders);
87 if (post) {
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);
109 return NULL;
112 if (conf.options.netdump) fprintf(stderr, _("Response: [%s]\n"), requestdata.response->str);
114 return requestdata.response;
117 #endif