summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Garcia Campos <cgarcia@igalia.com>2020-11-04 08:02:01 +0100
committerCarlos Garcia Campos <cgarcia@igalia.com>2020-11-04 08:02:01 +0100
commit7794572ab1b8aceac43756b6d4f006377a1442ae (patch)
treeff0d6bda40b01ffb76c65e8861df2110e808928e
parent70c1b907432b8ccf2b5fa161448855f6bc88c663 (diff)
downloadlibsoup-carlosgc/form-message.tar.gz
forms: remove soup_form_request_new APIscarlosgc/form-message
Add soup_message_new_from_encoded_form() and soup_message_new_from_multipart() instead.
-rw-r--r--docs/reference/libsoup-3.0-sections.txt6
-rw-r--r--libsoup/soup-form.c142
-rw-r--r--libsoup/soup-form.h17
-rw-r--r--libsoup/soup-message.c94
-rw-r--r--libsoup/soup-message.h18
-rw-r--r--tests/forms-test.c61
6 files changed, 165 insertions, 173 deletions
diff --git a/docs/reference/libsoup-3.0-sections.txt b/docs/reference/libsoup-3.0-sections.txt
index 8059d6ef..2c590efe 100644
--- a/docs/reference/libsoup-3.0-sections.txt
+++ b/docs/reference/libsoup-3.0-sections.txt
@@ -6,6 +6,8 @@ SoupMessage
<SUBSECTION>
soup_message_new
soup_message_new_from_uri
+soup_message_new_from_encoded_form
+soup_message_new_from_multipart
soup_message_set_request_body
soup_message_set_request_body_from_bytes
<SUBSECTION>
@@ -582,10 +584,6 @@ soup_form_encode
soup_form_encode_datalist
soup_form_encode_hash
soup_form_encode_valist
-soup_form_request_new
-soup_form_request_new_from_datalist
-soup_form_request_new_from_hash
-soup_form_request_new_from_multipart
</SECTION>
<SECTION>
diff --git a/libsoup/soup-form.c b/libsoup/soup-form.c
index 23cd007e..c0391444 100644
--- a/libsoup/soup-form.c
+++ b/libsoup/soup-form.c
@@ -346,145 +346,3 @@ soup_form_encode_valist (const char *first_field, va_list args)
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;
-
- msg = soup_message_new_from_uri (method, uri);
- } else if (!strcmp (method, "POST") || !strcmp (method, "PUT")) {
- GBytes *body;
-
- msg = soup_message_new_from_uri (method, uri);
-
- body = g_bytes_new_take (form_data, strlen (form_data));
- soup_message_set_request_body_from_bytes (msg, SOUP_FORM_MIME_TYPE_URLENCODED, body);
- g_bytes_unref (body);
- } else {
- g_warning ("invalid method passed to soup_form_request_new");
- g_free (form_data);
-
- /* Don't crash */
- msg = soup_message_new_from_uri (method, uri);
- }
- soup_uri_free (uri);
-
- 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: (transfer full): 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: (element-type utf8 utf8): 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: (transfer full): 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: (transfer full): 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));
-}
-
-/**
- * soup_form_request_new_from_multipart:
- * @uri: the URI to send the form data to
- * @multipart: a "multipart/form-data" #SoupMultipart
- *
- * Creates a new %SoupMessage and sets it up to send @multipart to
- * @uri via POST.
- *
- * To send a <literal>"multipart/form-data"</literal> POST, first
- * create a #SoupMultipart, using %SOUP_FORM_MIME_TYPE_MULTIPART as
- * the MIME type. Then use soup_multipart_append_form_string() and
- * soup_multipart_append_form_file() to add the value of each form
- * control to the multipart. (These are just convenience methods, and
- * you can use soup_multipart_append_part() if you need greater
- * control over the part headers.) Finally, call
- * soup_form_request_new_from_multipart() to serialize the multipart
- * structure and create a #SoupMessage.
- *
- * Return value: (transfer full): the new %SoupMessage
- *
- * Since: 2.26
- **/
-SoupMessage *
-soup_form_request_new_from_multipart (const char *uri,
- SoupMultipart *multipart)
-{
- SoupMessage *msg;
- GBytes *body = NULL;
-
- msg = soup_message_new ("POST", uri);
- soup_multipart_to_message (multipart, soup_message_get_request_headers (msg), &body);
- soup_message_set_request_body_from_bytes (msg,
- soup_message_headers_get_content_type (soup_message_get_request_headers (msg), NULL),
- body);
- g_bytes_unref (body);
-
- return msg;
-}
diff --git a/libsoup/soup-form.h b/libsoup/soup-form.h
index 27ca8aa1..c841512b 100644
--- a/libsoup/soup-form.h
+++ b/libsoup/soup-form.h
@@ -34,23 +34,6 @@ SOUP_AVAILABLE_IN_2_4
char *soup_form_encode_valist (const char *first_field,
va_list args);
-SOUP_AVAILABLE_IN_2_4
-SoupMessage *soup_form_request_new (const char *method,
- const char *uri,
- const char *first_field,
- ...) G_GNUC_NULL_TERMINATED;
-SOUP_AVAILABLE_IN_2_4
-SoupMessage *soup_form_request_new_from_hash (const char *method,
- const char *uri,
- GHashTable *form_data_set);
-SOUP_AVAILABLE_IN_2_4
-SoupMessage *soup_form_request_new_from_datalist (const char *method,
- const char *uri,
- GData **form_data_set);
-SOUP_AVAILABLE_IN_2_26
-SoupMessage *soup_form_request_new_from_multipart (const char *uri,
- SoupMultipart *multipart);
-
G_END_DECLS
#endif /* __SOUP_FORM_H__ */
diff --git a/libsoup/soup-message.c b/libsoup/soup-message.c
index 4a68af2f..19d6b1fd 100644
--- a/libsoup/soup-message.c
+++ b/libsoup/soup-message.c
@@ -797,6 +797,100 @@ soup_message_new_from_uri (const char *method, SoupURI *uri)
}
/**
+ * soup_message_new_from_encoded_form:
+ * @method: the HTTP method for the created request (GET, POST or PUT)
+ * @uri_string: the destination endpoint (as a string)
+ * @encoded_form: (transfer full): a encoded form
+ *
+ * Creates a new #SoupMessage and sets it up to send the given @encoded_form
+ * to @uri via @method. If @method is "GET", it will include the form data
+ * into @uri's query field, and if @method is "POST" or "PUT", it will be set as
+ * request body.
+ * This function takes the ownership of @encoded_form, that will be released
+ * with g_free() when no longer in use. See also soup_form_encode(),
+ * soup_form_encode_hash() and soup_form_encode_datalist().
+ *
+ * Returns: (transfer full) (nullable): the new #SoupMessage, or %NULL if @uri_string
+ * could not be parsed or @method is not "GET, "POST" or "PUT"
+ */
+SoupMessage *
+soup_message_new_from_encoded_form (const char *method,
+ const char *uri_string,
+ char *encoded_form)
+{
+ SoupMessage *msg = NULL;
+ SoupURI *uri;
+
+ g_return_val_if_fail (method != NULL, NULL);
+ g_return_val_if_fail (uri_string != NULL, NULL);
+ g_return_val_if_fail (encoded_form != NULL, NULL);
+
+ uri = soup_uri_new (uri_string);
+ if (!uri || !uri->host) {
+ g_free (encoded_form);
+ soup_uri_free (uri);
+ return NULL;
+ }
+
+ if (strcmp (method, "GET") == 0) {
+ g_free (uri->query);
+ uri->query = encoded_form;
+ msg = soup_message_new_from_uri (method, uri);
+ } else if (strcmp (method, "POST") == 0 || strcmp (method, "PUT") == 0) {
+ GBytes *body;
+
+ msg = soup_message_new_from_uri (method, uri);
+ body = g_bytes_new_take (encoded_form, strlen (encoded_form));
+ soup_message_set_request_body_from_bytes (msg, SOUP_FORM_MIME_TYPE_URLENCODED, body);
+ g_bytes_unref (body);
+ } else {
+ g_free (encoded_form);
+ }
+
+ soup_uri_free (uri);
+
+ return msg;
+}
+
+/**
+ * soup_message_new_from_multipart:
+ * @uri_string: the destination endpoint (as a string)
+ * @multipart: a #SoupMultipart
+ *
+ * Creates a new #SoupMessage and sets it up to send @multipart to
+ * @uri_string via POST.
+ *
+ * Returns: (transfer full) (nullable): the new #SoupMessage, or %NULL if @uri_string
+ * could not be parsed
+ */
+SoupMessage *
+soup_message_new_from_multipart (const char *uri_string,
+ SoupMultipart *multipart)
+{
+ SoupMessage *msg = NULL;
+ SoupURI *uri;
+ GBytes *body = NULL;
+
+ g_return_val_if_fail (uri_string != NULL, NULL);
+ g_return_val_if_fail (multipart != NULL, NULL);
+
+ uri = soup_uri_new (uri_string);
+ if (!uri || !uri->host) {
+ soup_uri_free (uri);
+ return NULL;
+ }
+
+ msg = soup_message_new_from_uri ("POST", uri);
+ soup_multipart_to_message (multipart, soup_message_get_request_headers (msg), &body);
+ soup_message_set_request_body_from_bytes (msg,
+ soup_message_headers_get_content_type (soup_message_get_request_headers (msg), NULL),
+ body);
+ g_bytes_unref (body);
+
+ return msg;
+}
+
+/**
* soup_message_set_request_body:
* @msg: the message
* @content_type: (allow-none): MIME Content-Type of the body
diff --git a/libsoup/soup-message.h b/libsoup/soup-message.h
index 3e70dfbc..cdb1d014 100644
--- a/libsoup/soup-message.h
+++ b/libsoup/soup-message.h
@@ -9,6 +9,7 @@
#include "soup-message-body.h"
#include "soup-message-headers.h"
#include "soup-method.h"
+#include "soup-multipart.h"
G_BEGIN_DECLS
@@ -17,11 +18,20 @@ SOUP_AVAILABLE_IN_ALL
G_DECLARE_FINAL_TYPE (SoupMessage, soup_message, SOUP, MESSAGE, GObject)
SOUP_AVAILABLE_IN_2_4
-SoupMessage *soup_message_new (const char *method,
- const char *uri_string);
+SoupMessage *soup_message_new (const char *method,
+ const char *uri_string);
SOUP_AVAILABLE_IN_2_4
-SoupMessage *soup_message_new_from_uri (const char *method,
- SoupURI *uri);
+SoupMessage *soup_message_new_from_uri (const char *method,
+ SoupURI *uri);
+
+SOUP_AVAILABLE_IN_ALL
+SoupMessage *soup_message_new_from_encoded_form (const char *method,
+ const char *uri_string,
+ char *encoded_form);
+
+SOUP_AVAILABLE_IN_ALL
+SoupMessage *soup_message_new_from_multipart (const char *uri_string,
+ SoupMultipart *multipart);
SOUP_AVAILABLE_IN_ALL
void soup_message_set_request_body (SoupMessage *msg,
diff --git a/tests/forms-test.c b/tests/forms-test.c
index 9d069a4c..f7d15630 100644
--- a/tests/forms-test.c
+++ b/tests/forms-test.c
@@ -36,7 +36,7 @@ static struct {
};
static void
-do_hello_test (int n, gboolean extra, const char *uri)
+do_hello_test_curl (int n, gboolean extra, const char *uri)
{
GPtrArray *args;
char *title_arg = NULL, *name_arg = NULL;
@@ -86,7 +86,7 @@ do_hello_test (int n, gboolean extra, const char *uri)
}
static void
-do_hello_tests (gconstpointer uri)
+do_hello_tests_curl (gconstpointer uri)
{
int n;
@@ -96,8 +96,56 @@ do_hello_tests (gconstpointer uri)
}
for (n = 0; n < G_N_ELEMENTS (tests); n++) {
- do_hello_test (n, FALSE, uri);
- do_hello_test (n, TRUE, uri);
+ do_hello_test_curl (n, FALSE, uri);
+ do_hello_test_curl (n, TRUE, uri);
+ }
+}
+
+static void
+do_hello_test_libsoup (int n, gboolean extra, const char *uri)
+{
+ SoupSession *session;
+ SoupMessage *msg;
+ GData *data;
+ GBytes *body;
+
+ debug_printf (1, "%2d. '%s' '%s'%s: ", n * 2 + (extra ? 2 : 1),
+ tests[n].title ? tests[n].title : "(null)",
+ tests[n].name ? tests[n].name : "(null)",
+ extra ? " + extra" : "");
+
+ g_datalist_init (&data);
+ if (tests[n].title)
+ g_datalist_set_data (&data, "title", (gpointer)tests[n].title);
+ if (tests[n].name)
+ g_datalist_set_data (&data, "n@me", (gpointer)tests[n].name);
+ if (extra)
+ g_datalist_set_data (&data, "extra", (gpointer)"something");
+
+ session = soup_test_session_new (SOUP_TYPE_SESSION, NULL);
+
+ msg = soup_message_new_from_encoded_form ("GET",
+ uri,
+ soup_form_encode_datalist (&data));
+ g_datalist_clear (&data);
+
+ body = soup_test_session_send (session, msg, NULL, NULL);
+ soup_test_assert_message_status (msg, SOUP_STATUS_OK);
+ g_assert_cmpmem (tests[n].result, strlen (tests[n].result), g_bytes_get_data (body, NULL), g_bytes_get_size (body));
+
+ g_bytes_unref (body);
+ g_object_unref (msg);
+ soup_test_session_abort_unref (session);
+}
+
+static void
+do_hello_tests_libsoup (gconstpointer uri)
+{
+ int n;
+
+ for (n = 0; n < G_N_ELEMENTS (tests); n++) {
+ do_hello_test_libsoup (n, FALSE, uri);
+ do_hello_test_libsoup (n, TRUE, uri);
}
}
@@ -204,7 +252,7 @@ do_md5_test_libsoup (gconstpointer data)
g_bytes_unref (buffer);
soup_multipart_append_form_string (multipart, "fmt", "text");
- msg = soup_form_request_new_from_multipart (uri, multipart);
+ msg = soup_message_new_from_multipart (uri, multipart);
soup_multipart_free (multipart);
session = soup_test_session_new (SOUP_TYPE_SESSION, NULL);
@@ -456,7 +504,8 @@ main (int argc, char **argv)
if (run_tests) {
uri = soup_uri_new_with_base (base_uri, "/hello");
- g_test_add_data_func_full ("/forms/hello", soup_uri_to_string (uri, FALSE), do_hello_tests, g_free);
+ g_test_add_data_func_full ("/forms/hello/curl", soup_uri_to_string (uri, FALSE), do_hello_tests_curl, g_free);
+ g_test_add_data_func_full ("/forms/hello/libsoup", soup_uri_to_string (uri, FALSE), do_hello_tests_libsoup, g_free);
soup_uri_free (uri);
uri = soup_uri_new_with_base (base_uri, "/md5");