Blame src/libostree/ostree-soup-form.c

rpm-build 0fba15
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
rpm-build 0fba15
/* soup-form.c : utility functions for HTML forms */
rpm-build 0fba15
rpm-build 0fba15
/*
rpm-build 0fba15
 * Copyright 2008 Red Hat, Inc.
rpm-build 0fba15
 */
rpm-build 0fba15
rpm-build 0fba15
/* This one is stripped down to only have soup_form_encode_hash()
rpm-build 0fba15
 * and soup_form_encode_valist() which are the only bits that soup-uri.c
rpm-build 0fba15
 * calls.
rpm-build 0fba15
 */
rpm-build 0fba15
rpm-build 0fba15
#include <config.h>
rpm-build 0fba15
rpm-build 0fba15
#include <string.h>
rpm-build 0fba15
rpm-build 0fba15
#include "ostree-soup-uri.h"
rpm-build 0fba15
rpm-build 0fba15
/**
rpm-build 0fba15
 * SECTION:soup-form
rpm-build 0fba15
 * @short_description: HTML form handling
rpm-build 0fba15
 * @see_also: #SoupMultipart
rpm-build 0fba15
 *
rpm-build 0fba15
 * libsoup contains several help methods for processing HTML forms as
rpm-build 0fba15
 * defined by 
rpm-build 0fba15
 * url="http://www.w3.org/TR/html401/interact/forms.html#h-17.13">the
rpm-build 0fba15
 * HTML 4.01 specification</ulink>.
rpm-build 0fba15
 **/
rpm-build 0fba15
rpm-build 0fba15
/**
rpm-build 0fba15
 * SOUP_FORM_MIME_TYPE_URLENCODED:
rpm-build 0fba15
 *
rpm-build 0fba15
 * A macro containing the value
rpm-build 0fba15
 * <literal>"application/x-www-form-urlencoded"</literal>; the default
rpm-build 0fba15
 * MIME type for POSTing HTML form data.
rpm-build 0fba15
 *
rpm-build 0fba15
 * Since: 2.26
rpm-build 0fba15
 **/
rpm-build 0fba15
rpm-build 0fba15
/**
rpm-build 0fba15
 * SOUP_FORM_MIME_TYPE_MULTIPART:
rpm-build 0fba15
 *
rpm-build 0fba15
 * A macro containing the value
rpm-build 0fba15
 * <literal>"multipart/form-data"</literal>; the MIME type used for
rpm-build 0fba15
 * posting form data that contains files to be uploaded.
rpm-build 0fba15
 *
rpm-build 0fba15
 * Since: 2.26
rpm-build 0fba15
 **/
rpm-build 0fba15
rpm-build 0fba15
#define XDIGIT(c) ((c) <= '9' ? (c) - '0' : ((c) & 0x4F) - 'A' + 10)
rpm-build 0fba15
#define HEXCHAR(s) ((XDIGIT (s[1]) << 4) + XDIGIT (s[2]))
rpm-build 0fba15
rpm-build 0fba15
static void
rpm-build 0fba15
append_form_encoded (GString *str, const char *in)
rpm-build 0fba15
{
rpm-build 0fba15
	const unsigned char *s = (const unsigned char *)in;
rpm-build 0fba15
rpm-build 0fba15
	while (*s) {
rpm-build 0fba15
		if (*s == ' ') {
rpm-build 0fba15
			g_string_append_c (str, '+');
rpm-build 0fba15
			s++;
rpm-build 0fba15
		} else if (!g_ascii_isalnum (*s) && (*s != '-') && (*s != '_')
rpm-build 0fba15
			   && (*s != '.'))
rpm-build 0fba15
			g_string_append_printf (str, "%%%02X", (int)*s++);
rpm-build 0fba15
		else
rpm-build 0fba15
			g_string_append_c (str, *s++);
rpm-build 0fba15
	}
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
static void
rpm-build 0fba15
encode_pair (GString *str, const char *name, const char *value)
rpm-build 0fba15
{
rpm-build 0fba15
	g_return_if_fail (name != NULL);
rpm-build 0fba15
	g_return_if_fail (value != NULL);
rpm-build 0fba15
rpm-build 0fba15
	if (str->len)
rpm-build 0fba15
		g_string_append_c (str, '&';;
rpm-build 0fba15
	append_form_encoded (str, name);
rpm-build 0fba15
	g_string_append_c (str, '=');
rpm-build 0fba15
	append_form_encoded (str, value);
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
/**
rpm-build 0fba15
 * soup_form_encode_hash:
rpm-build 0fba15
 * @form_data_set: (element-type utf8 utf8) (transfer none): a hash table containing
rpm-build 0fba15
 * name/value pairs (as strings)
rpm-build 0fba15
 *
rpm-build 0fba15
 * Encodes @form_data_set into a value of type
rpm-build 0fba15
 * "application/x-www-form-urlencoded", as defined in the HTML 4.01
rpm-build 0fba15
 * spec.
rpm-build 0fba15
 *
rpm-build 0fba15
 * Note that the HTML spec states that "The control names/values are
rpm-build 0fba15
 * listed in the order they appear in the document." Since this method
rpm-build 0fba15
 * takes a hash table, it cannot enforce that; if you care about the
rpm-build 0fba15
 * ordering of the form fields, use soup_form_encode_datalist().
rpm-build 0fba15
 *
rpm-build 0fba15
 * Return value: the encoded form
rpm-build 0fba15
 **/
rpm-build 0fba15
char *
rpm-build 0fba15
soup_form_encode_hash (GHashTable *form_data_set)
rpm-build 0fba15
{
rpm-build 0fba15
	GString *str = g_string_new (NULL);
rpm-build 0fba15
	GHashTableIter iter;
rpm-build 0fba15
	gpointer name, value;
rpm-build 0fba15
rpm-build 0fba15
	g_hash_table_iter_init (&iter, form_data_set);
rpm-build 0fba15
	while (g_hash_table_iter_next (&iter, &name, &value))
rpm-build 0fba15
		encode_pair (str, name, value);
rpm-build 0fba15
	return g_string_free (str, FALSE);
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
/**
rpm-build 0fba15
 * soup_form_encode_valist:
rpm-build 0fba15
 * @first_field: name of the first form field
rpm-build 0fba15
 * @args: pointer to additional values, as in soup_form_encode()
rpm-build 0fba15
 *
rpm-build 0fba15
 * See soup_form_encode(). This is mostly an internal method, used by
rpm-build 0fba15
 * various other methods such as soup_uri_set_query_from_fields() and
rpm-build 0fba15
 * soup_form_request_new().
rpm-build 0fba15
 *
rpm-build 0fba15
 * Return value: the encoded form
rpm-build 0fba15
 **/
rpm-build 0fba15
char *
rpm-build 0fba15
soup_form_encode_valist (const char *first_field, va_list args)
rpm-build 0fba15
{
rpm-build 0fba15
	GString *str = g_string_new (NULL);
rpm-build 0fba15
	const char *name, *value;
rpm-build 0fba15
rpm-build 0fba15
	name = first_field;
rpm-build 0fba15
	value = va_arg (args, const char *);
rpm-build 0fba15
	while (name && value) {
rpm-build 0fba15
		encode_pair (str, name, value);
rpm-build 0fba15
rpm-build 0fba15
		name = va_arg (args, const char *);
rpm-build 0fba15
		if (name)
rpm-build 0fba15
			value = va_arg (args, const char *);
rpm-build 0fba15
	}
rpm-build 0fba15
rpm-build 0fba15
	return g_string_free (str, FALSE);
rpm-build 0fba15
}