/cpw/tomkiewicz/http: 27d0e5dbc2a1: Implement missing getters/se...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Wed Oct 17 04:35:46 EDT 2012


Changeset: 27d0e5dbc2a1907fbb5a76dbf1c9f37e249a2b8b
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2012-10-17 10:35 +0200
Branch:	 default
URL: http://hg.pidgin.im/cpw/tomkiewicz/http/rev/27d0e5dbc2a1

Description:

Implement missing getters/setters and corresponding features

diffstat:

 libpurple/http.c            |  105 ++++++++++++++++++++++++++++++++++++++++---
 libpurple/http.h            |   77 ++++++++++++++++++++++++++++----
 libpurple/protocols/gg/gg.c |    4 +-
 3 files changed, 166 insertions(+), 20 deletions(-)

diffs (truncated from 321 to 300 lines):

diff --git a/libpurple/http.c b/libpurple/http.c
--- a/libpurple/http.c
+++ b/libpurple/http.c
@@ -55,6 +55,7 @@ struct _PurpleHttpRequest
 	int ref_count;
 
 	gchar *url;
+	gchar *method;
 	PurpleHttpHeaders *headers;
 
 	int timeout;
@@ -157,6 +158,8 @@ static void purple_http_headers_free(Pur
 static void purple_http_headers_add(PurpleHttpHeaders *hdrs, const gchar *key,
 	const gchar *value);
 static const GList * purple_http_headers_get_all(PurpleHttpHeaders *hdrs);
+static const GList * purple_http_headers_get_all_by_name(
+	PurpleHttpHeaders *hdrs, const gchar *key);
 static const gchar * purple_http_headers_get(PurpleHttpHeaders *hdrs,
 	const gchar *key);
 static gboolean purple_http_headers_get_int(PurpleHttpHeaders *hdrs,
@@ -249,8 +252,8 @@ static const GList * purple_http_headers
 	return hdrs->list;
 }
 
-static const gchar * purple_http_headers_get(PurpleHttpHeaders *hdrs,
-	const gchar *key)
+static const GList * purple_http_headers_get_all_by_name(
+	PurpleHttpHeaders *hdrs, const gchar *key)
 {
 	GList *values;
 	gchar *key_low;
@@ -261,6 +264,15 @@ static const gchar * purple_http_headers
 	key_low = g_ascii_strdown(key, -1);
 	values = g_hash_table_lookup(hdrs->by_name, key_low);
 	g_free(key_low);
+
+	return values;
+}
+
+static const gchar * purple_http_headers_get(PurpleHttpHeaders *hdrs,
+	const gchar *key)
+{
+	const GList *values = purple_http_headers_get_all_by_name(hdrs, key);
+
 	if (!values)
 		return NULL;
 
@@ -357,30 +369,36 @@ static void _purple_http_gen_headers(Pur
 	PurpleHttpURL *url;
 	PurpleProxyInfo *proxy;
 	const GList *hdr;
+	PurpleHttpRequest *req;
+	PurpleHttpHeaders *hdrs;
 
 	g_return_if_fail(hc != NULL);
 
 	if (hc->request_header != NULL)
 		return;
 
+	req = hc->request;
 	url = hc->url;
+	hdrs = req->headers;
 	proxy = purple_proxy_get_setup(hc->gc ?
 		purple_connection_get_account(hc->gc) : NULL);
 
 	hc->request_header = h = g_string_new("");
 	hc->request_header_written = 0;
 
-	g_string_append_printf(h, "GET %s HTTP/%s\r\n", url->path,
-		hc->request->http11 ? "1.1" : "1.0");
+	g_string_append_printf(h, "%s %s HTTP/%s\r\n",
+		req->method ? req->method : "GET",
+		url->path,
+		req->http11 ? "1.1" : "1.0");
 
-	if (!purple_http_headers_get(hc->request->headers, "host"))
+	if (!purple_http_headers_get(hdrs, "host"))
 		g_string_append_printf(h, "Host: %s\r\n", url->host);
-	if (!purple_http_headers_get(hc->request->headers, "connection"))
+	if (!purple_http_headers_get(hdrs, "connection"))
 		g_string_append_printf(h, "Connection: close\r\n");
-	if (!purple_http_headers_get(hc->request->headers, "accept"))
+	if (!purple_http_headers_get(hdrs, "accept"))
 		g_string_append_printf(h, "Accept: */*\r\n");
 
-	hdr = purple_http_headers_get_all(hc->request->headers);
+	hdr = purple_http_headers_get_all(hdrs);
 	while (hdr) {
 		PurpleKeyValuePair *kvp = hdr->data;
 		hdr = g_list_next(hdr);
@@ -1086,6 +1104,21 @@ gboolean purple_http_conn_is_running(Pur
 	return (NULL != g_hash_table_lookup(purple_http_hc_by_ptr, http_conn));
 }
 
+PurpleHttpRequest * purple_http_conn_get_request(PurpleHttpConnection *http_conn)
+{
+	g_return_val_if_fail(http_conn != NULL, NULL);
+
+	return http_conn->request;
+}
+
+PurpleConnection * purple_http_conn_get_purple_connection(
+	PurpleHttpConnection *http_conn)
+{
+	g_return_val_if_fail(http_conn != NULL, NULL);
+
+	return http_conn->gc;
+}
+
 /*** Request API **************************************************************/
 
 static void purple_http_request_free(PurpleHttpRequest *request);
@@ -1139,6 +1172,37 @@ PurpleHttpRequest * purple_http_request_
 	return NULL;
 }
 
+void purple_http_request_set_url(PurpleHttpRequest *request, const gchar *url)
+{
+	g_return_if_fail(request != NULL);
+	g_return_if_fail(url != NULL);
+
+	g_free(request->url);
+	request->url = g_strdup(url);
+}
+
+const gchar * purple_http_request_get_url(PurpleHttpRequest *request)
+{
+	g_return_val_if_fail(request != NULL, NULL);
+
+	return request->url;
+}
+
+void purple_http_request_set_method(PurpleHttpRequest *request, const gchar *method)
+{
+	g_return_if_fail(request != NULL);
+
+	g_free(request->method);
+	request->method = g_strdup(method);
+}
+
+const gchar * purple_http_request_get_method(PurpleHttpRequest *request)
+{
+	g_return_val_if_fail(request != NULL, NULL);
+
+	return request->method;
+}
+
 void purple_http_request_set_timeout(PurpleHttpRequest *request, int timeout)
 {
 	g_return_if_fail(request != NULL);
@@ -1294,6 +1358,31 @@ const gchar * purple_http_response_get_d
 	return response->contents->str;
 }
 
+const GList * purple_http_response_get_all_headers(PurpleHttpResponse *response)
+{
+	g_return_val_if_fail(response != NULL, NULL);
+
+	return purple_http_headers_get_all(response->headers);
+}
+
+const GList * purple_http_response_get_headers_by_name(
+	PurpleHttpResponse *response, const gchar *name)
+{
+	g_return_val_if_fail(response != NULL, NULL);
+	g_return_val_if_fail(name != NULL, NULL);
+
+	return purple_http_headers_get_all_by_name(response->headers, name);
+}
+
+const gchar * purple_http_response_get_header(PurpleHttpResponse *response,
+	const gchar *name)
+{
+	g_return_val_if_fail(response != NULL, NULL);
+	g_return_val_if_fail(name != NULL, NULL);
+
+	return purple_http_headers_get(response->headers, name);
+}
+
 /*** URL functions ************************************************************/
 
 static PurpleHttpURL * purple_http_url_parse(const char *raw_url)
diff --git a/libpurple/http.h b/libpurple/http.h
--- a/libpurple/http.h
+++ b/libpurple/http.h
@@ -157,11 +157,21 @@ void purple_http_conn_cancel_all(PurpleC
  */
 gboolean purple_http_conn_is_running(PurpleHttpConnection *http_conn);
 
-//TODO
+/**
+ * Gets PurpleHttpRequest used for specified HTTP connection.
+ *
+ * @param http_conn The HTTP connection.
+ * @return          The PurpleHttpRequest object.
+ */
 PurpleHttpRequest * purple_http_conn_get_request(
 	PurpleHttpConnection *http_conn);
 
-//TODO
+/**
+ * Gets PurpleConnection tied with specified HTTP connection.
+ *
+ * @param http_conn The HTTP connection.
+ * @return          The PurpleConnection object.
+ */
 PurpleConnection * purple_http_conn_get_purple_connection(
 	PurpleHttpConnection *http_conn);
 
@@ -221,12 +231,38 @@ void purple_http_request_ref(PurpleHttpR
  */
 PurpleHttpRequest * purple_http_request_unref(PurpleHttpRequest *request);
 
-//TODO
-void purple_http_request_set_url(PurpleHttpRequest *request, const gchar *url); // +get
+/**
+ * Sets URL for HTTP request.
+ *
+ * @param request The request.
+ * @param url     The url.
+ */
+void purple_http_request_set_url(PurpleHttpRequest *request, const gchar *url);
 
-//TODO
+/**
+ * Gets URL set for the HTTP request.
+ *
+ * @param request The request.
+ * @return        URL set for this request.
+ */
+const gchar * purple_http_request_get_url(PurpleHttpRequest *request);
+
+/**
+ * Sets custom HTTP method used for the request.
+ *
+ * @param request The request.
+ * @param method  The method, or NULL for default.
+ */
 void purple_http_request_set_method(PurpleHttpRequest *request,
-	const gchar *method); // +get
+	const gchar *method);
+
+/**
+ * Gets HTTP method set for the request.
+ *
+ * @param request The request.
+ * @return        The method.
+ */
+const gchar * purple_http_request_get_method(PurpleHttpRequest *request);
 
 /**
  * Sets contents of HTTP request (for example, POST data).
@@ -236,7 +272,7 @@ void purple_http_request_set_method(Purp
  * @param length   The length of contents (-1 if it's a NULL-terminated string)
  */
 void purple_http_request_set_contents(PurpleHttpRequest *request,
-	const gchar *contents, int length); // +get
+	const gchar *contents, int length);
 
 /**
  * Sets contents reader for HTTP request, used mainly for possible large
@@ -408,14 +444,35 @@ gsize purple_http_response_get_data_len(
  */
 const gchar * purple_http_response_get_data(PurpleHttpResponse *response);
 
-//TODO
+/**
+ * Gets all headers got with response.
+ *
+ * @param response The response.
+ * @return         GList of PurpleKeyValuePair, which keys are header field
+ *                 names (gchar*) and values are its contents (gchar*).
+ */
 const GList * purple_http_response_get_all_headers(PurpleHttpResponse *response);
 
-//TODO
+/**
+ * Gets all headers with specified name got with response.
+ *
+ * @param response The response.
+ * @param name     The name of header field.
+ * @return         GList of header field records contents (gchar*).
+ */
 const GList * purple_http_response_get_headers_by_name(
 	PurpleHttpResponse *response, const gchar *name);
 
-//TODO
+/**
+ * Gets one header contents with specified name got with response.
+ *
+ * To get all headers with the same name, use
+ * purple_http_response_get_headers_by_name instead.
+ *
+ * @param response The response.
+ * @param name     The name of header field.
+ * @return         Header field contents or NULL, if there is no such one.
+ */
 const gchar * purple_http_response_get_header(PurpleHttpResponse *response,
 	const gchar *name);
 



More information about the Commits mailing list