updated log api
[prss.git] / src / http.c
1 /*
2  * Copyright (C) 2010-2013 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #include <locale.h>
20 #include <libintl.h>
21 #define _(str) gettext(str)
22
23 #include <pthread.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <curl/curl.h>
29
30 #include "http.h"
31 #include <plog.h>
32
33 struct http_session {
34         CURL *curl;
35         pthread_mutex_t lock;
36 };
37
38 struct ucontent {
39         char *data;
40         size_t len;
41 };
42
43 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
44 {
45         size_t realsize;
46         struct ucontent *mem;
47
48         realsize = size * nmemb;
49         mem = (struct ucontent *)userp;
50
51         mem->data = realloc(mem->data, mem->len + realsize + 1);
52
53         memcpy(&(mem->data[mem->len]), buffer, realsize);
54         mem->len += realsize;
55         mem->data[mem->len] = 0;
56
57         return realsize;
58 }
59
60 struct http_session *http_session_new()
61 {
62         struct http_session *sess;
63
64         sess = malloc(sizeof(struct http_session));
65         sess->curl = curl_easy_init();
66         pthread_mutex_init(&sess->lock, NULL);
67
68         return sess;
69 }
70
71 void http_session_free(struct http_session *sess)
72 {
73         curl_easy_cleanup(sess->curl);
74         pthread_mutex_destroy(&sess->lock);
75         free(sess);
76 }
77
78 static char *
79 http_get(struct http_session *sess, const char *url, const char *content)
80 {
81         struct ucontent chunk;
82         int result;
83
84         chunk.data = malloc(1);
85         chunk.len = 0;
86
87         pthread_mutex_lock(&sess->lock);
88
89         curl_easy_setopt(sess->curl, CURLOPT_URL, url);
90         curl_easy_setopt(sess->curl, CURLOPT_VERBOSE, 0);
91         if (content) {
92                 curl_easy_setopt(sess->curl, CURLOPT_POSTFIELDS, content);
93                 curl_easy_setopt(sess->curl,
94                                  CURLOPT_POSTFIELDSIZE,
95                                  (long)strlen(content));
96         }
97         curl_easy_setopt(sess->curl, CURLOPT_WRITEFUNCTION, cbk_curl);
98         curl_easy_setopt(sess->curl, CURLOPT_WRITEDATA, (void *)&chunk);
99
100         result = curl_easy_perform(sess->curl);
101
102         pthread_mutex_unlock(&sess->lock);
103
104         if (result == CURLE_OK)
105                 return chunk.data;
106
107         free(chunk.data);
108         log_err(_("HTTP request fail url=%s"), url);
109
110         return NULL;
111 }
112
113 json_object *
114 http_json_get(struct http_session *sess, const char *url, struct json_object *j)
115 {
116         const char *in;
117         char *out;
118         struct json_object *result;
119
120         if (log_level >= LOG_DEBUG)
121                 log_debug("HTTP request= %s",
122                        json_object_to_json_string(j));
123
124         in = json_object_to_json_string(j);
125         out = http_get(sess, url, in);
126
127         if (out) {
128                 result = json_tokener_parse(out);
129
130                 if (log_level >= LOG_DEBUG)
131                         log_debug("HTTP reply= %s", out);
132
133                 free(out);
134                 return result;
135         }
136
137         return NULL;
138 }