summaryrefslogtreecommitdiff
path: root/libsoup/soup-form.c
diff options
context:
space:
mode:
authorDan Winship <danw@src.gnome.org>2008-02-01 15:23:31 +0000
committerDan Winship <danw@src.gnome.org>2008-02-01 15:23:31 +0000
commit9adc9e9d0dea849093e4520091dd06c9fb850e6f (patch)
treeca28ed1edb70e88d6bd4a957fa6ec6d1628ccbd1 /libsoup/soup-form.c
parenta49e0bcee33eeede3a2764277ae5096192d9d726 (diff)
downloadlibsoup-9adc9e9d0dea849093e4520091dd06c9fb850e6f.tar.gz
new
* libsoup/soup-date.c (soup_date_to_time_t): new * libsoup/soup-form.c (soup_form_decode): Remove "_urlencoded" from name. (And add back-compat #define.) (soup_form_encode): New, takes varargs parameters for each form construction. (soup_form_encode_hash, soup_form_encode_datalist): renamed, with back-compat #defines (soup_form_request_new, soup_form_request_new_from_hash) (soup_form_request_new_from_datalist): New methods to construct a GET or POST message with form data. * libsoup/soup-uri.c (soup_uri_set_query_from_fields): New, takes varargs like soup_form_encode(). * libsoup/soup-value-utils.c (soup_value_hash_new_with_vals) (soup_value_hash_insert_vals, soup_value_hash_lookup_vals): New routines to work with multiple value hash values at once. (soup_value_array_new): tiny wrapper, for naming consistency (soup_value_array_new_with_vals, soup_value_array_append_vals): New routines to work with multiple value array values at once. svn path=/trunk/; revision=1063
Diffstat (limited to 'libsoup/soup-form.c')
-rw-r--r--libsoup/soup-form.c194
1 files changed, 178 insertions, 16 deletions
diff --git a/libsoup/soup-form.c b/libsoup/soup-form.c
index 299fc884..0984ad85 100644
--- a/libsoup/soup-form.c
+++ b/libsoup/soup-form.c
@@ -12,6 +12,8 @@
#include <string.h>
#include "soup-form.h"
+#include "soup-message.h"
+#include "soup-uri.h"
#define XDIGIT(c) ((c) <= '9' ? (c) - '0' : ((c) & 0x4F) - 'A' + 10)
#define HEXCHAR(s) ((XDIGIT (s[1]) << 4) + XDIGIT (s[2]))
@@ -39,7 +41,7 @@ form_decode (char *part)
}
/**
- * soup_form_decode_urlencoded:
+ * soup_form_decode:
* @encoded_form: data of type "application/x-www-form-urlencoded"
*
* Decodes @form, which is an urlencoded dataset as defined in the
@@ -49,7 +51,7 @@ form_decode (char *part)
* @encoded_form, which you can free with g_hash_table_destroy().
**/
GHashTable *
-soup_form_decode_urlencoded (const char *encoded_form)
+soup_form_decode (const char *encoded_form)
{
GHashTable *form_data_set;
char **pairs, *eq, *name, *value;
@@ -111,23 +113,52 @@ hash_encode_foreach (gpointer name, gpointer value, gpointer str)
}
/**
- * soup_form_encode_urlencoded:
- * @form_data_set: a hash table containing name/value pairs
+ * soup_form_encode:
+ * @first_field: name of the first form field
+ * @...: value of @first_field, followed by additional field names
+ * and values, terminated by %NULL.
+ *
+ * Encodes the given field names and values into a value of type
+ * "application/x-www-form-urlencoded", as defined in the HTML 4.01
+ * spec.
+ *
+ * This method requires you to know the names of the form fields (or
+ * at the very least, the total number of fields) at compile time; for
+ * working with dynamic forms, use soup_form_encode_hash() or
+ * soup_form_encode_datalist().
+ *
+ * Return value: the encoded form
+ **/
+char *
+soup_form_encode (const char *first_field, ...)
+{
+ va_list args;
+ char *encoded;
+
+ va_start (args, first_field);
+ encoded = soup_form_encode_valist (first_field, args);
+ va_end (args);
+
+ return encoded;
+}
+
+/**
+ * soup_form_encode_hash:
+ * @form_data_set: a hash table containing name/value pairs (as strings)
*
* Encodes @form_data_set into a value of type
* "application/x-www-form-urlencoded", as defined in the HTML 4.01
* spec.
*
- * Note that the spec states that "The control names/values are listed
- * in the order they appear in the document." Since
- * soup_form_encode_urlencoded() takes a hash table, this cannot be
- * enforced; if you care about the ordering of the form fields, use
- * soup_form_encode_urlencoded_list().
+ * Note that the HTML spec states that "The control names/values are
+ * listed in the order they appear in the document." Since this method
+ * takes a hash table, it cannot enforce that; if you care about the
+ * ordering of the form fields, use soup_form_encode_datalist().
*
* Return value: the encoded form
**/
char *
-soup_form_encode_urlencoded (GHashTable *form_data_set)
+soup_form_encode_hash (GHashTable *form_data_set)
{
GString *str = g_string_new (NULL);
@@ -142,22 +173,153 @@ datalist_encode_foreach (GQuark key_id, gpointer value, gpointer str)
}
/**
- * soup_form_encode_urlencoded_list:
- * @form_data_set: a hash table containing name/value pairs
+ * soup_form_encode_datalist:
+ * @form_data_set: a datalist containing name/value pairs
*
* Encodes @form_data_set into a value of type
* "application/x-www-form-urlencoded", as defined in the HTML 4.01
- * spec. Unlike soup_form_encode_urlencoded(), this preserves the
- * ordering of the form elements, which may be required in some
- * situations.
+ * spec. Unlike soup_form_encode_hash(), this preserves the ordering
+ * of the form elements, which may be required in some situations.
*
* Return value: the encoded form
**/
char *
-soup_form_encode_urlencoded_list (GData **form_data_set)
+soup_form_encode_datalist (GData **form_data_set)
{
GString *str = g_string_new (NULL);
g_datalist_foreach (form_data_set, datalist_encode_foreach, str);
return g_string_free (str, FALSE);
}
+
+/**
+ * soup_form_encode_valist:
+ * @first_field: name of the first form field
+ * @args: pointer to additional values, as in soup_form_encode()
+ *
+ * See soup_form_encode(). This is mostly an internal method, used by
+ * various other methods such as soup_uri_set_query_from_fields() and
+ * soup_form_request_new().
+ *
+ * Return value: the encoded form
+ **/
+char *
+soup_form_encode_valist (const char *first_field, va_list args)
+{
+ GString *str = g_string_new (NULL);
+ const char *name, *value;
+
+ name = first_field;
+ value = va_arg (args, const char *);
+ while (name && value) {
+ encode_pair (str, name, value);
+
+ name = va_arg (args, const char *);
+ if (name)
+ value = va_arg (args, const char *);
+ }
+
+ return g_string_free (str, FALSE);
+}
+
+static SoupMessage *
+soup_form_request_for_data (const char *method, const char *uri_string,
+ char *form_data)
+{
+ SoupMessage *msg;
+ SoupURI *uri;
+
+ uri = soup_uri_new (uri_string);
+ if (!uri)
+ return NULL;
+
+ if (!strcmp (method, "GET")) {
+ g_free (uri->query);
+ uri->query = form_data;
+ form_data = NULL;
+ }
+
+ msg = soup_message_new_from_uri (method, uri);
+
+ if (!strcmp (method, "POST")) {
+ soup_message_set_request (
+ msg, "application/x-www-form-urlencoded",
+ SOUP_MEMORY_TAKE,
+ form_data, strlen (form_data));
+ form_data = NULL;
+ }
+
+ if (form_data) {
+ g_warning ("invalid method passed to soup_form_request_new");
+ g_free (form_data);
+ }
+
+ return msg;
+}
+
+/**
+ * soup_form_request_new:
+ * @method: the HTTP method, either "GET" or "POST"
+ * @uri: the URI to send the form data to
+ * @first_field: name of the first form field
+ * @...: value of @first_field, followed by additional field names
+ * and values, terminated by %NULL.
+ *
+ * Creates a new %SoupMessage and sets it up to send the given data
+ * to @uri via @method. (That is, if @method is "GET", it will encode
+ * the form data into @uri's query field, and if @method is "POST", it
+ * will encode it into the %SoupMessage's request_body.)
+ *
+ * Return value: the new %SoupMessage
+ **/
+SoupMessage *
+soup_form_request_new (const char *method, const char *uri,
+ const char *first_field, ...)
+{
+ va_list args;
+ char *form_data;
+
+ va_start (args, first_field);
+ form_data = soup_form_encode_valist (first_field, args);
+ va_end (args);
+
+ return soup_form_request_for_data (method, uri, form_data);
+}
+
+/**
+ * soup_form_request_new_from_hash:
+ * @method: the HTTP method, either "GET" or "POST"
+ * @uri: the URI to send the form data to
+ * @form_data_set: the data to send to @uri
+ *
+ * Creates a new %SoupMessage and sets it up to send @form_data_set to
+ * @uri via @method, as with soup_form_request_new().
+ *
+ * Return value: the new %SoupMessage
+ **/
+SoupMessage *
+soup_form_request_new_from_hash (const char *method, const char *uri,
+ GHashTable *form_data_set)
+{
+ return soup_form_request_for_data (
+ method, uri, soup_form_encode_hash (form_data_set));
+}
+
+/**
+ * soup_form_request_new_from_datalist:
+ * @method: the HTTP method, either "GET" or "POST"
+ * @uri: the URI to send the form data to
+ * @form_data_set: the data to send to @uri
+ *
+ * Creates a new %SoupMessage and sets it up to send @form_data_set to
+ * @uri via @method, as with soup_form_request_new().
+ *
+ * Return value: the new %SoupMessage
+ **/
+SoupMessage *
+soup_form_request_new_from_datalist (const char *method, const char *uri,
+ GData **form_data_set)
+{
+ return soup_form_request_for_data (
+ method, uri, soup_form_encode_datalist (form_data_set));
+}