(no commit message)
authorJean-Philippe Orsini <jeanfi@gmail.com>
Tue, 23 Apr 2013 17:49:37 +0000 (17:49 +0000)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Tue, 23 Apr 2013 17:49:37 +0000 (17:49 +0000)
src/http.c
src/http.h
src/ttrss.c

index ba4d51a..e663b86 100644 (file)
@@ -25,9 +25,8 @@
 #include <string.h>
 
 #include <curl/curl.h>
-#include <json/json.h>
 
-#include "url.h"
+#include "http.h"
 
 struct ucontent {
        char *data;
@@ -58,68 +57,52 @@ void http_init()
        curl = curl_easy_init();
 }
 
-void phttp_cleanup()
+void http_cleanup()
 {
        curl_easy_cleanup(curl);
 }
 
-json_object *get_json_object(const char *url)
+char *http_get(const char *url, const char *content)
 {
        struct ucontent chunk;
-       json_object *obj;
-
-       obj = NULL;
-
-       if (!curl)
-               return NULL;
 
        chunk.data = malloc(1);
        chunk.len = 0;
 
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
+       if (content) {
+               curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content);
+               curl_easy_setopt(curl,
+                                CURLOPT_POSTFIELDSIZE, 
+                                (long)strlen(content));
+       }
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
 
        if (curl_easy_perform(curl) == CURLE_OK)
-               obj = json_tokener_parse(chunk.data);
-       else
-               fprintf(stderr, _("Fail to connect to: %s"), url);
+               return chunk.data;
 
        free(chunk.data);
+       fprintf(stderr, _("HTTP request fail url=%s"), url);
 
-       return obj;
+       return NULL;
 }
 
-static json_object *post(const char *url, const char *body)
+json_object *http_json_get(const char *url, struct json_object *j)
 {
-       struct ucontent chunk;
-       json_object *obj;
-
-       obj = NULL;
-
-       chunk.data = malloc(1);
-       chunk.len = 0;
-
-       curl_easy_setopt(curl, CURLOPT_URL, url);
-       curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
-       if (body) {
-               curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body);
-               curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(body));
+       const char *in;
+       char *out;
+       struct json_object *result;
+
+       in = json_object_to_json_string(j);
+       out = http_get(url, in);
+       
+       if (out) {
+               result = json_tokener_parse(out);
+               free(out);
+               return result;
        }
-       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
-       curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
 
-       if (curl_easy_perform(curl) == CURLE_OK)
-               obj = json_tokener_parse(chunk.data);
-       else
-               fprintf(stderr, _("Fail to connect to: %s"), url);
-       free(chunk.data);
-
-       return obj;
-}
-
-json_object *post_json_object(const char *url, struct json_object *j)
-{
-       return post(url, json_object_to_json_string(j));
+       return NULL;
 }
index 16c25cc..dd78640 100644 (file)
 #ifndef _PRSS_HTTP_H_
 #define _PRSS_HTTP_H_
 
+#include <json/json.h>
+
 void http_init();
 void http_cleanup();
-json_object *get_json_object(const char *url);
-json_object *post_json_object(const char *url, struct json_object *);
+json_object *http_json_get(const char *url, struct json_object *);
 
 #endif
index 7f9e8df..18b6369 100644 (file)
@@ -65,7 +65,7 @@ void ttrss_login(const char *url, const char *user, const char *password)
                               "password",
                               json_object_new_string(password));
 
-       rp = post_json_object(session_url, rq);
+       rp = http_json_get(session_url, rq);
        json_object_put(rq);
 
        content = json_object_object_get(rp, "content");
@@ -105,7 +105,7 @@ const char *ttrss_get_headline_content(struct headline *h)
                json_object_object_add(rq, "article_id",
                                       json_object_new_int(h->id));
                
-               rp = post_json_object(session_url, rq);
+               rp = http_json_get(session_url, rq);
                
                json_object_put(rq);
                
@@ -140,7 +140,7 @@ static struct headline **get_headlines(int feed_id)
        rq = create_op("getHeadlines");
        json_object_object_add(rq, "feed_id", json_object_new_int(feed_id));
 
-       rp = post_json_object(session_url, rq);
+       rp = http_json_get(session_url, rq);
 
        json_object_put(rq);
 
@@ -227,7 +227,7 @@ struct feed **ttrss_get_feeds()
 
        rq = create_op("getFeeds");
 
-       rp = post_json_object(session_url, rq);
+       rp = http_json_get(session_url, rq);
        json_object_put(rq);
 
        content = json_object_object_get(rp, "content");
@@ -289,7 +289,7 @@ void ttrss_set_article_unread(int id, int unread)
        json_object_object_add(rq, "field", json_object_new_int(2));
        json_object_object_add(rq, "mode", json_object_new_int(unread));
 
-       rp = post_json_object(session_url, rq);
+       rp = http_json_get(session_url, rq);
        
        json_object_put(rq);
        json_object_put(rp);