(no commit message)
authorJean-Philippe Orsini <jeanfi@gmail.com>
Mon, 29 Apr 2013 08:31:39 +0000 (08:31 +0000)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Mon, 29 Apr 2013 08:31:39 +0000 (08:31 +0000)
src/http.c
src/http.h
src/main.c
src/ttrss_ws.c

index 12f8b72..0689a84 100644 (file)
 #include "http.h"
 #include "log.h"
 
+struct http_session {
+       CURL *curl;
+       pthread_mutex_t lock;
+};
+
 struct ucontent {
        char *data;
        size_t len;
 };
 
-static CURL *curl;
-static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
-
 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
 {
        size_t realsize;
@@ -55,18 +57,26 @@ static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
        return realsize;
 }
 
-void http_init()
+struct http_session *http_session_new()
 {
-       if (!curl)
-               curl = curl_easy_init();
+       struct http_session *sess;
+
+       sess = malloc(sizeof(struct http_session));
+       sess->curl = curl_easy_init();
+       pthread_mutex_init(&sess->lock, NULL);
+
+       return sess;
 }
 
-void http_cleanup()
+void http_session_free(struct http_session *sess)
 {
-       curl_easy_cleanup(curl);
+       curl_easy_cleanup(sess->curl);
+       pthread_mutex_destroy(&sess->lock);
+       free(sess);
 }
 
-static char *http_get(const char *url, const char *content)
+static char *
+http_get(struct http_session *sess, const char *url, const char *content)
 {
        struct ucontent chunk;
        int result;
@@ -74,22 +84,22 @@ static char *http_get(const char *url, const char *content)
        chunk.data = malloc(1);
        chunk.len = 0;
 
-       pthread_mutex_lock(&lock);
+       pthread_mutex_lock(&sess->lock);
 
-       curl_easy_setopt(curl, CURLOPT_URL, url);
-       curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
+       curl_easy_setopt(sess->curl, CURLOPT_URL, url);
+       curl_easy_setopt(sess->curl, CURLOPT_VERBOSE, 1);
        if (content) {
-               curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content);
-               curl_easy_setopt(curl,
+               curl_easy_setopt(sess->curl, CURLOPT_POSTFIELDS, content);
+               curl_easy_setopt(sess->curl,
                                 CURLOPT_POSTFIELDSIZE,
                                 (long)strlen(content));
        }
-       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
-       curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
+       curl_easy_setopt(sess->curl, CURLOPT_WRITEFUNCTION, cbk_curl);
+       curl_easy_setopt(sess->curl, CURLOPT_WRITEDATA, (void *)&chunk);
 
-       result = curl_easy_perform(curl);
+       result = curl_easy_perform(sess->curl);
 
-       pthread_mutex_unlock(&lock);
+       pthread_mutex_unlock(&sess->lock);
 
        if (result == CURLE_OK)
                return chunk.data;
@@ -100,7 +110,8 @@ static char *http_get(const char *url, const char *content)
        return NULL;
 }
 
-json_object *http_json_get(const char *url, struct json_object *j)
+json_object *
+http_json_get(struct http_session *sess, const char *url, struct json_object *j)
 {
        const char *in;
        char *out;
@@ -111,7 +122,7 @@ json_object *http_json_get(const char *url, struct json_object *j)
                       json_object_to_json_string(j));
 
        in = json_object_to_json_string(j);
-       out = http_get(url, in);
+       out = http_get(sess, url, in);
 
        if (out) {
                result = json_tokener_parse(out);
index dd78640..985187d 100644 (file)
 #ifndef _PRSS_HTTP_H_
 #define _PRSS_HTTP_H_
 
+#include <curl/curl.h>
 #include <json/json.h>
 
-void http_init();
-void http_cleanup();
-json_object *http_json_get(const char *url, struct json_object *);
+struct http_session;
+
+struct http_session *http_session_new();
+void http_session_free(struct http_session *);
+
+json_object *
+http_json_get(struct http_session *, const char *, struct json_object *);
 
 #endif
index 13411c9..6d9aead 100644 (file)
@@ -504,7 +504,6 @@ int main(int argc, char **argv)
 
        log_init();
 
-       http_init();
        ws_init();
 
        gtk_init(NULL, NULL);
index 4fc64e2..51eee15 100644 (file)
 #include "ttrss_ws.h"
 #include "url.h"
 
-static pthread_mutex_t lock;
-
 static char *session_id;
 static char *session_url;
 static char *session_user;
 static char *session_pwd;
 
+static pthread_mutex_t lock;
+static struct http_session *http_session;
+
 void ws_request_add_att_str(json_object *rq, const char *k, const char *str)
 {
        json_object_object_add(rq, k, json_object_new_string(str));
@@ -102,7 +103,7 @@ static struct json_object *execute(struct json_object *rq, char **err)
        struct json_object *rp, *content, *jerror;
        const char *str;
 
-       rp = http_json_get(session_url, rq);
+       rp = http_json_get(http_session, session_url, rq);
 
        content = NULL;
 
@@ -407,7 +408,9 @@ void ws_set_article_unread(int id, int unread)
 
 void ws_init()
 {
-       pthread_mutexattr_t    attr;
+       pthread_mutexattr_t attr;
        pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
        pthread_mutex_init(&lock, &attr);
+
+       http_session = http_session_new();
 }