/cpw/tomkiewicz/http: dd0c0860e293: Basic API and testing action...
Tomasz Wasilczyk
tomkiewicz at cpw.pidgin.im
Fri Oct 12 19:17:10 EDT 2012
Changeset: dd0c0860e29392ba302a53cd35eef88839955929
Author: Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date: 2012-10-13 01:17 +0200
Branch: default
URL: http://hg.pidgin.im/cpw/tomkiewicz/http/rev/dd0c0860e293
Description:
Basic API and testing actions for it
diffstat:
libpurple/connection.c | 2 +
libpurple/http.c | 211 ++++++++++++++++++++++++++++++++++++++++++++
libpurple/http.h | 47 +++++++++-
libpurple/protocols/gg/gg.c | 20 +++-
4 files changed, 278 insertions(+), 2 deletions(-)
diffs (truncated from 346 to 300 lines):
diff --git a/libpurple/connection.c b/libpurple/connection.c
--- a/libpurple/connection.c
+++ b/libpurple/connection.c
@@ -31,6 +31,7 @@
#include "connection.h"
#include "dbus-maybe.h"
#include "debug.h"
+#include "http.h"
#include "log.h"
#include "notify.h"
#include "prefs.h"
@@ -251,6 +252,7 @@ void
update_keepalive(gc, FALSE);
+ purple_http_conn_cancel_all(gc);
purple_proxy_connect_cancel_with_handle(gc);
prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl);
diff --git a/libpurple/http.c b/libpurple/http.c
--- a/libpurple/http.c
+++ b/libpurple/http.c
@@ -26,3 +26,214 @@
#include "http.h"
+#include "internal.h"
+#include "debug.h"
+
+struct _PurpleHttpRequest
+{
+ int ref_count;
+
+ gchar *url;
+};
+
+struct _PurpleHttpConnection
+{
+ PurpleConnection *gc;
+ PurpleHttpCallback callback;
+ gpointer user_data;
+
+ PurpleHttpResponse *response;
+};
+
+struct _PurpleHttpResponse
+{
+ int code;
+
+ gchar *data;
+ gsize data_len;
+};
+
+static PurpleHttpConnection * purple_http_connection_new(void);
+static void purple_http_connection_terminate(PurpleHttpConnection *hc);
+
+static PurpleHttpResponse * purple_http_response_new(void);
+static void purple_http_response_free(PurpleHttpResponse *response);
+
+/*** Performing HTTP requests *************************************************/
+
+PurpleHttpConnection * purple_http_get(PurpleConnection *gc, const gchar *url,
+ PurpleHttpCallback callback, gpointer user_data)
+{
+ PurpleHttpRequest *request;
+ PurpleHttpConnection *hc;
+
+ g_return_val_if_fail(url != NULL, NULL);
+
+ request = purple_http_request_new(url);
+ hc = purple_http_request(gc, request, callback, user_data);
+ purple_http_request_unref(request);
+
+ return hc;
+}
+
+static gboolean purple_http_request_dummy_timeout(gpointer user_data)
+{
+ PurpleHttpConnection *hc = user_data;
+ PurpleHttpResponse *response = hc->response;
+
+ response->code = 200;
+ response->data = g_strdup("[dummy reply]");
+ response->data_len = strlen(response->data);
+
+ purple_http_connection_terminate(hc);
+
+ return FALSE;
+}
+
+PurpleHttpConnection * purple_http_request(PurpleConnection *gc,
+ PurpleHttpRequest *request, PurpleHttpCallback callback,
+ gpointer user_data)
+{
+ PurpleHttpConnection *hc;
+
+ g_return_val_if_fail(request != NULL, NULL);
+
+ hc = purple_http_connection_new();
+ hc->gc = gc;
+ hc->callback = callback;
+ hc->user_data = user_data;
+
+ purple_debug_misc("http", "Performing new request %p for %s.\n",
+ hc, request->url);
+
+ purple_timeout_add_seconds(1, purple_http_request_dummy_timeout, hc);
+
+ return hc;
+}
+
+/*** HTTP connection API ******************************************************/
+
+static void purple_http_connection_free(PurpleHttpConnection *hc);
+
+static PurpleHttpConnection * purple_http_connection_new(void)
+{
+ PurpleHttpConnection *hc = g_new0(PurpleHttpConnection, 1);
+
+ hc->response = purple_http_response_new();
+
+ return hc;
+}
+
+static void purple_http_connection_free(PurpleHttpConnection *hc)
+{
+ purple_http_response_free(hc->response);
+ g_free(hc);
+}
+
+/* call callback and do the cleanup */
+static void purple_http_connection_terminate(PurpleHttpConnection *hc)
+{
+ g_return_if_fail(hc != NULL);
+
+ purple_debug_misc("http", "Request %p performed %s.\n", hc,
+ purple_http_response_is_successfull(hc->response) ?
+ "successfully" : "without success");
+
+ if (hc->callback)
+ hc->callback(hc, hc->response, hc->user_data);
+
+ purple_http_connection_free(hc);
+}
+
+void purple_http_conn_cancel_all(PurpleConnection *gc)
+{
+ purple_debug_warning("http", "purple_http_conn_cancel_all: To be implemented\n");
+}
+
+/*** Request API **************************************************************/
+
+static void purple_http_request_free(PurpleHttpRequest *request);
+
+PurpleHttpRequest * purple_http_request_new(const gchar *url)
+{
+ PurpleHttpRequest *request;
+
+ g_return_val_if_fail(url != NULL, NULL);
+
+ request = g_new0(PurpleHttpRequest, 1);
+
+ request->ref_count = 1;
+ request->url = g_strdup(url);
+
+ return request;
+}
+
+static void purple_http_request_free(PurpleHttpRequest *request)
+{
+ g_free(request->url);
+ g_free(request);
+}
+
+void purple_http_request_ref(PurpleHttpRequest *request)
+{
+ g_return_if_fail(request != NULL);
+
+ request->ref_count++;
+}
+
+PurpleHttpRequest * purple_http_request_unref(PurpleHttpRequest *request)
+{
+ if (request == NULL)
+ return NULL;
+
+ g_return_val_if_fail(request->ref_count > 0, NULL);
+
+ request->ref_count--;
+ if (request->ref_count > 0)
+ return request;
+
+ purple_http_request_free(request);
+ return NULL;
+}
+
+/*** HTTP response API ********************************************************/
+
+static PurpleHttpResponse * purple_http_response_new(void)
+{
+ PurpleHttpResponse *response = g_new0(PurpleHttpResponse, 1);
+
+ return response;
+}
+
+static void purple_http_response_free(PurpleHttpResponse *response)
+{
+ g_free(response);
+}
+
+gboolean purple_http_response_is_successfull(PurpleHttpResponse *response)
+{
+ g_return_val_if_fail(response != NULL, FALSE);
+
+ return response->code == 200; /* just temporary */
+}
+
+int purple_http_response_get_code(PurpleHttpResponse *response)
+{
+ g_return_val_if_fail(response != NULL, 0);
+
+ return response->code;
+}
+
+gsize purple_http_response_get_data_len(PurpleHttpResponse *response)
+{
+ g_return_val_if_fail(response != NULL, 0);
+
+ return response->data_len;
+}
+
+const gchar * purple_http_response_get_data(PurpleHttpResponse *response)
+{
+ g_return_val_if_fail(response != NULL, NULL);
+
+ return response->data;
+}
diff --git a/libpurple/http.h b/libpurple/http.h
--- a/libpurple/http.h
+++ b/libpurple/http.h
@@ -189,11 +189,30 @@ void purple_http_cookie_jar_remove(Purpl
/**************************************************************************/
/*@{*/
+/**
+ * Creates the new instance of HTTP request configuration.
+ *
+ * @param url The URL to request for.
+ * @return The new instance of HTTP request struct.
+ */
PurpleHttpRequest * purple_http_request_new(const gchar *url);
+/**
+ * Increment the reference count.
+ *
+ * @param request The request.
+ */
void purple_http_request_ref(PurpleHttpRequest *request);
-void purple_http_request_unref(PurpleHttpRequest *request); // instead of free
+/**
+ * Decrement the reference count.
+ *
+ * If the reference count reaches zero, the http request struct will be freed.
+ *
+ * @param request The request.
+ * @return @a request or @c NULL if the reference count reached zero.
+ */
+PurpleHttpRequest * purple_http_request_unref(PurpleHttpRequest *request);
void purple_http_request_set_url(PurpleHttpRequest *request, const gchar *url); // +get
@@ -286,14 +305,40 @@ void purple_http_request_header_add(Purp
/**************************************************************************/
/*@{*/
+/**
+ * Checks, if HTTP request was performed successfully.
+ *
+ * @param response The response.
+ * @return TRUE, if request was performed successfully.
+ */
gboolean purple_http_response_is_successfull(PurpleHttpResponse *response);
+/**
+ * Gets HTTP response code.
+ *
+ * @param response The response.
+ * @return HTTP response code.
+ */
int purple_http_response_get_code(PurpleHttpResponse *response);
const gchar * purple_http_response_get_error(PurpleHttpResponse *response);
+/**
+ * Gets HTTP response data length.
+ *
+ * @param response The response.
+ * @return Data length;
+ */
More information about the Commits
mailing list