From bc1076c4337d76e0862eb4d11230ed3e702341dc Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Sun, 20 Jul 2014 19:18:54 +0200 Subject: [PATCH] Add http post (thx to Ben Kibbey) --- src/Tools/gmpc_easy_download.c | 85 +++++++++++++++++++++++++++++++++++++++--- src/Tools/gmpc_easy_download.h | 19 ++++++++++ 2 files changed, 98 insertions(+), 6 deletions(-) diff --git a/src/Tools/gmpc_easy_download.c b/src/Tools/gmpc_easy_download.c index 0b18f198..7ed6c61b 100644 --- a/src/Tools/gmpc_easy_download.c +++ b/src/Tools/gmpc_easy_download.c @@ -530,12 +530,27 @@ GEADAsyncHandler *gmpc_easy_async_downloader(const gchar * uri, GEADAsyncCallbac return gmpc_easy_async_downloader_with_headers(uri, callback, user_data, NULL); } -GEADAsyncHandler *gmpc_easy_async_downloader_with_headers(const gchar * uri, GEADAsyncCallback callback, - gpointer user_data, ...) +GEADAsyncHandler *gmpc_easy_async_downloader2(const gchar *uri, + GEADMethod method, + const gchar *post_data, + const gchar *content_type, + GEADAsyncCallback callback, + gpointer user_data) +{ + if (uri == NULL) + { + g_log(LOG_DOMAIN,G_LOG_LEVEL_WARNING, "No download uri specified."); + return NULL; + } + return gmpc_easy_async_downloader_with_headers2(uri, method, post_data, + content_type, callback, + user_data, NULL); +} + +static GEADAsyncHandler *gmpc_easy_async_downloader_with_headers_common(const gchar * uri, GEADMethod method, const gchar *post_data, const gchar *content_type, GEADAsyncCallback callback, gpointer user_data, va_list ap) { SoupMessage *msg; _GEADAsyncHandler *d; - va_list ap; char *va_entry; if (soup_session == NULL) { @@ -546,12 +561,17 @@ GEADAsyncHandler *gmpc_easy_async_downloader_with_headers(const gchar * uri, GEA g_object_set(soup_session, "user-agent", "gmpc ",NULL); } - msg = soup_message_new("GET", uri); + msg = soup_message_new(method == GEAD_GET ? "GET" : "POST", uri); if (!msg) return NULL; + if (method == GEAD_POST) + { + soup_message_set_request(msg, content_type, SOUP_MEMORY_COPY, + post_data, strlen(post_data)); + } + soup_message_headers_append(msg->request_headers, "Accept-Encoding", "deflate,gzip"); - va_start(ap, user_data); va_entry = va_arg(ap, typeof(va_entry)); while (va_entry) { @@ -559,7 +579,6 @@ GEADAsyncHandler *gmpc_easy_async_downloader_with_headers(const gchar * uri, GEA soup_message_headers_append(msg->request_headers, va_entry, value); va_entry = va_arg(ap, typeof(va_entry)); } - va_end(ap); d = g_malloc0(sizeof(*d)); d->uid = ++uid; @@ -581,6 +600,45 @@ GEADAsyncHandler *gmpc_easy_async_downloader_with_headers(const gchar * uri, GEA return (GEADAsyncHandler *) d; } +GEADAsyncHandler *gmpc_easy_async_downloader_with_headers2(const gchar *uri, + GEADMethod method, + const gchar *post_data, + const gchar *content_type, + GEADAsyncCallback callback, + gpointer user_data, + ...) +{ + va_list ap, cp; + GEADAsyncHandler *ret; + + va_start(ap, user_data); + va_copy(cp, ap); + ret = gmpc_easy_async_downloader_with_headers_common(uri, method, post_data, + content_type, callback, + user_data, cp); + va_end(ap); + va_end(cp); + return ret; +} + +GEADAsyncHandler *gmpc_easy_async_downloader_with_headers(const gchar *uri, + GEADAsyncCallback callback, + gpointer user_data, + ...) +{ + va_list ap, cp; + GEADAsyncHandler *ret; + + va_start(ap, user_data); + va_copy(cp, ap); + ret = gmpc_easy_async_downloader_with_headers_common(uri, GEAD_GET, NULL, + NULL, callback, + user_data, cp); + va_end(ap); + va_end(cp); + return ret; +} + void gmpc_easy_async_quit(void) { if (soup_session) @@ -622,5 +680,20 @@ GEADAsyncHandler * gmpc_easy_async_downloader_vala(const char *path, gpointer us return gmpc_easy_async_downloader(path, temp_callback, f); } +GEADAsyncHandler *gmpc_easy_async_downloader_vala2(const char *path, + GEADMethod method, + const gchar *post_data, + const gchar *content_type, + gpointer user_data2, + GEADAsyncCallbackVala callback, + gpointer user_data) +{ + valaf *f = g_malloc0(sizeof(*f)); + f->a = user_data; + f->b =user_data2; + f->callback = callback; + return gmpc_easy_async_downloader2(path, method, post_data, content_type, + temp_callback, f); +} /* vim: set noexpandtab ts=4 sw=4 sts=4 tw=120: */ diff --git a/src/Tools/gmpc_easy_download.h b/src/Tools/gmpc_easy_download.h index 1bc41125..dc4ef6c9 100644 --- a/src/Tools/gmpc_easy_download.h +++ b/src/Tools/gmpc_easy_download.h @@ -24,6 +24,11 @@ * This is based on libsoup */ +typedef enum { + GEAD_GET, + GEAD_POST +} GEADMethod; + typedef struct _GEADAsyncHandler GEADAsyncHandler; typedef enum { GEAD_DONE, @@ -44,8 +49,21 @@ gpointer userdata_callback); */ GEADAsyncHandler *gmpc_easy_async_downloader(const gchar * uri, GEADAsyncCallback callback, gpointer user_data); +/** + * @param uri the http uri to download + * @param method the http method to use + * @param post_data when method is GEAD_POST; the data to send + * @param content_type the content-type of post_data + * @param callback the callback function. Giving status updates on the download. + * @param user_data Data to pass along to callback. + * + * returns: a GEADAsyncHandler (or NULL on failure), remember you need to free this. This can be done f.e. in the callback. (same Handler get passed) + */ +GEADAsyncHandler *gmpc_easy_async_downloader2(const gchar * uri, GEADMethod method, const gchar *post_data, const gchar *content_type, GEADAsyncCallback callback, gpointer user_data); + GEADAsyncHandler *gmpc_easy_async_downloader_with_headers(const gchar * uri, GEADAsyncCallback callback, gpointer user_data, ...); +GEADAsyncHandler *gmpc_easy_async_downloader_with_headers2(const gchar * uri, GEADMethod method, const gchar *post_data, const gchar *content_type, GEADAsyncCallback callback, gpointer user_data, ...); /** * Cancel download, triggers GEAD_CANCEL in callback */ @@ -77,5 +95,6 @@ char *gmpc_easy_download_uri_escape(const char *part); GEADAsyncHandler * gmpc_easy_async_downloader_vala(const char *path, gpointer user_data2, GEADAsyncCallbackVala callback, gpointer user_data ); +GEADAsyncHandler * gmpc_easy_async_downloader_vala2(const char *path, GEADMethod method, const gchar *post_data, const gchar *content_type, gpointer user_data2, GEADAsyncCallbackVala callback, gpointer user_data); #endif /* vim: set noexpandtab ts=4 sw=4 sts=4 tw=120: */ -- 2.11.4.GIT