Blame libsoup/soup-request-http.c

rpm-build 4f3c61
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
rpm-build 4f3c61
/*
rpm-build 4f3c61
 * soup-request-http.c: http: URI request object
rpm-build 4f3c61
 *
rpm-build 4f3c61
 * Copyright (C) 2009, 2010 Red Hat, Inc.
rpm-build 4f3c61
 * Copyright (C) 2010 Igalia, S.L.
rpm-build 4f3c61
 *
rpm-build 4f3c61
 * This library is free software; you can redistribute it and/or
rpm-build 4f3c61
 * modify it under the terms of the GNU Library General Public
rpm-build 4f3c61
 * License as published by the Free Software Foundation; either
rpm-build 4f3c61
 * version 2 of the License, or (at your option) any later version.
rpm-build 4f3c61
 *
rpm-build 4f3c61
 * This library is distributed in the hope that it will be useful,
rpm-build 4f3c61
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 4f3c61
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 4f3c61
 * Library General Public License for more details.
rpm-build 4f3c61
 *
rpm-build 4f3c61
 * You should have received a copy of the GNU Library General Public License
rpm-build 4f3c61
 * along with this library; see the file COPYING.LIB.  If not, write to
rpm-build 4f3c61
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
rpm-build 4f3c61
 * Boston, MA 02110-1301, USA.
rpm-build 4f3c61
 */
rpm-build 4f3c61
rpm-build 4f3c61
#ifdef HAVE_CONFIG_H
rpm-build 4f3c61
#include <config.h>
rpm-build 4f3c61
#endif
rpm-build 4f3c61
rpm-build 4f3c61
#include <glib/gi18n-lib.h>
rpm-build 4f3c61
rpm-build 4f3c61
#include "soup-request-http.h"
rpm-build 4f3c61
#include "soup.h"
rpm-build 4f3c61
#include "soup-message-private.h"
rpm-build 4f3c61
#include "soup-session-private.h"
rpm-build 4f3c61
rpm-build 4f3c61
/**
rpm-build 4f3c61
 * SECTION:soup-request-http
rpm-build 4f3c61
 * @short_description: SoupRequest support for "http" and "https" URIs
rpm-build 4f3c61
 *
rpm-build 4f3c61
 * #SoupRequestHTTP implements #SoupRequest for "http" and "https"
rpm-build 4f3c61
 * URIs.
rpm-build 4f3c61
 *
rpm-build 4f3c61
 * To do more complicated HTTP operations using the #SoupRequest APIs,
rpm-build 4f3c61
 * call soup_request_http_get_message() to get the request's
rpm-build 4f3c61
 * #SoupMessage.
rpm-build 4f3c61
 */
rpm-build 4f3c61
rpm-build 4f3c61
struct _SoupRequestHTTPPrivate {
rpm-build 4f3c61
	SoupMessage *msg;
rpm-build 4f3c61
	char *content_type;
rpm-build 4f3c61
};
rpm-build 4f3c61
rpm-build 4f3c61
G_DEFINE_TYPE_WITH_PRIVATE (SoupRequestHTTP, soup_request_http, SOUP_TYPE_REQUEST)
rpm-build 4f3c61
rpm-build 4f3c61
static void content_sniffed (SoupMessage *msg,
rpm-build 4f3c61
			     const char  *content_type,
rpm-build 4f3c61
			     GHashTable  *params,
rpm-build 4f3c61
			     gpointer     user_data);
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
soup_request_http_init (SoupRequestHTTP *http)
rpm-build 4f3c61
{
rpm-build 4f3c61
	http->priv = soup_request_http_get_instance_private (http);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static gboolean
rpm-build 4f3c61
soup_request_http_check_uri (SoupRequest  *request,
rpm-build 4f3c61
			     SoupURI      *uri,
rpm-build 4f3c61
			     GError      **error)
rpm-build 4f3c61
{
rpm-build 4f3c61
	SoupRequestHTTP *http = SOUP_REQUEST_HTTP (request);
rpm-build 4f3c61
rpm-build 4f3c61
	if (!SOUP_URI_VALID_FOR_HTTP (uri))
rpm-build 4f3c61
		return FALSE;
rpm-build 4f3c61
rpm-build 4f3c61
	http->priv->msg = soup_message_new_from_uri (SOUP_METHOD_GET, uri);
rpm-build 4f3c61
	soup_message_set_soup_request (http->priv->msg, request);
rpm-build 4f3c61
rpm-build 4f3c61
	g_signal_connect (http->priv->msg, "content-sniffed",
rpm-build 4f3c61
			  G_CALLBACK (content_sniffed), http);
rpm-build 4f3c61
	return TRUE;
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
soup_request_http_finalize (GObject *object)
rpm-build 4f3c61
{
rpm-build 4f3c61
	SoupRequestHTTP *http = SOUP_REQUEST_HTTP (object);
rpm-build 4f3c61
rpm-build 4f3c61
	if (http->priv->msg) {
rpm-build 4f3c61
		g_signal_handlers_disconnect_by_func (http->priv->msg,
rpm-build 4f3c61
						      G_CALLBACK (content_sniffed),
rpm-build 4f3c61
						      http);
rpm-build 4f3c61
		g_object_unref (http->priv->msg);
rpm-build 4f3c61
	}
rpm-build 4f3c61
rpm-build 4f3c61
	g_free (http->priv->content_type);
rpm-build 4f3c61
rpm-build 4f3c61
	G_OBJECT_CLASS (soup_request_http_parent_class)->finalize (object);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static GInputStream *
rpm-build 4f3c61
soup_request_http_send (SoupRequest          *request,
rpm-build 4f3c61
			GCancellable         *cancellable,
rpm-build 4f3c61
			GError              **error)
rpm-build 4f3c61
{
rpm-build 4f3c61
	SoupRequestHTTP *http = SOUP_REQUEST_HTTP (request);
rpm-build 4f3c61
	SoupSession *session = soup_request_get_session (request);
rpm-build 4f3c61
rpm-build 4f3c61
	g_return_val_if_fail (!SOUP_IS_SESSION_ASYNC (session), NULL);
rpm-build 4f3c61
rpm-build 4f3c61
	return soup_session_send (session, http->priv->msg,
rpm-build 4f3c61
				  cancellable, error);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
http_input_stream_ready_cb (GObject *source, GAsyncResult *result, gpointer user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GTask *task = user_data;
rpm-build 4f3c61
	GError *error = NULL;
rpm-build 4f3c61
	GInputStream *stream;
rpm-build 4f3c61
rpm-build 4f3c61
	stream = soup_session_send_finish (SOUP_SESSION (source), result, &error);
rpm-build 4f3c61
	if (stream)
rpm-build 4f3c61
		g_task_return_pointer (task, stream, g_object_unref);
rpm-build 4f3c61
	else
rpm-build 4f3c61
		g_task_return_error (task, error);
rpm-build 4f3c61
	g_object_unref (task);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
soup_request_http_send_async (SoupRequest          *request,
rpm-build 4f3c61
			      GCancellable         *cancellable,
rpm-build 4f3c61
			      GAsyncReadyCallback   callback,
rpm-build 4f3c61
			      gpointer              user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	SoupRequestHTTP *http = SOUP_REQUEST_HTTP (request);
rpm-build 4f3c61
	SoupSession *session = soup_request_get_session (request);
rpm-build 4f3c61
	GTask *task;
rpm-build 4f3c61
rpm-build 4f3c61
	g_return_if_fail (!SOUP_IS_SESSION_SYNC (session));
rpm-build 4f3c61
rpm-build 4f3c61
	task = g_task_new (request, cancellable, callback, user_data);
rpm-build 4f3c61
	soup_session_send_async (session, http->priv->msg, cancellable,
rpm-build 4f3c61
				 http_input_stream_ready_cb, task);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static GInputStream *
rpm-build 4f3c61
soup_request_http_send_finish (SoupRequest   *request,
rpm-build 4f3c61
			       GAsyncResult  *result,
rpm-build 4f3c61
			       GError       **error)
rpm-build 4f3c61
{
rpm-build 4f3c61
	g_return_val_if_fail (g_task_is_valid (result, request), NULL);
rpm-build 4f3c61
rpm-build 4f3c61
	return g_task_propagate_pointer (G_TASK (result), error);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static goffset
rpm-build 4f3c61
soup_request_http_get_content_length (SoupRequest *request)
rpm-build 4f3c61
{
rpm-build 4f3c61
	SoupRequestHTTP *http = SOUP_REQUEST_HTTP (request);
rpm-build 4f3c61
rpm-build 4f3c61
	return soup_message_headers_get_content_length (http->priv->msg->response_headers);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
content_sniffed (SoupMessage *msg,
rpm-build 4f3c61
		 const char  *content_type,
rpm-build 4f3c61
		 GHashTable  *params,
rpm-build 4f3c61
		 gpointer     user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	SoupRequestHTTP *http = user_data;
rpm-build 4f3c61
	GString *sniffed_type;
rpm-build 4f3c61
rpm-build 4f3c61
	sniffed_type = g_string_new (content_type);
rpm-build 4f3c61
	if (params) {
rpm-build 4f3c61
		GHashTableIter iter;
rpm-build 4f3c61
		gpointer key, value;
rpm-build 4f3c61
rpm-build 4f3c61
		g_hash_table_iter_init (&iter, params);
rpm-build 4f3c61
		while (g_hash_table_iter_next (&iter, &key, &value)) {
rpm-build 4f3c61
			g_string_append (sniffed_type, "; ");
rpm-build 4f3c61
			soup_header_g_string_append_param (sniffed_type, key, value);
rpm-build 4f3c61
		}
rpm-build 4f3c61
	}
rpm-build 4f3c61
	g_free (http->priv->content_type);
rpm-build 4f3c61
	http->priv->content_type = g_string_free (sniffed_type, FALSE);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static const char *
rpm-build 4f3c61
soup_request_http_get_content_type (SoupRequest *request)
rpm-build 4f3c61
{
rpm-build 4f3c61
	SoupRequestHTTP *http = SOUP_REQUEST_HTTP (request);
rpm-build 4f3c61
rpm-build 4f3c61
	return http->priv->content_type;
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static const char *http_schemes[] = { "http", "https", NULL };
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
soup_request_http_class_init (SoupRequestHTTPClass *request_http_class)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GObjectClass *object_class = G_OBJECT_CLASS (request_http_class);
rpm-build 4f3c61
	SoupRequestClass *request_class =
rpm-build 4f3c61
		SOUP_REQUEST_CLASS (request_http_class);
rpm-build 4f3c61
rpm-build 4f3c61
	request_class->schemes = http_schemes;
rpm-build 4f3c61
rpm-build 4f3c61
	object_class->finalize = soup_request_http_finalize;
rpm-build 4f3c61
rpm-build 4f3c61
	request_class->check_uri = soup_request_http_check_uri;
rpm-build 4f3c61
	request_class->send = soup_request_http_send;
rpm-build 4f3c61
	request_class->send_async = soup_request_http_send_async;
rpm-build 4f3c61
	request_class->send_finish = soup_request_http_send_finish;
rpm-build 4f3c61
	request_class->get_content_length = soup_request_http_get_content_length;
rpm-build 4f3c61
	request_class->get_content_type = soup_request_http_get_content_type;
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
/**
rpm-build 4f3c61
 * soup_request_http_get_message:
rpm-build 4f3c61
 * @http: a #SoupRequestHTTP object
rpm-build 4f3c61
 *
rpm-build 4f3c61
 * Gets a new reference to the #SoupMessage associated to this SoupRequest
rpm-build 4f3c61
 *
rpm-build 4f3c61
 * Returns: (transfer full): a new reference to the #SoupMessage
rpm-build 4f3c61
 *
rpm-build 4f3c61
 * Since: 2.40
rpm-build 4f3c61
 */
rpm-build 4f3c61
SoupMessage *
rpm-build 4f3c61
soup_request_http_get_message (SoupRequestHTTP *http)
rpm-build 4f3c61
{
rpm-build 4f3c61
	g_return_val_if_fail (SOUP_IS_REQUEST_HTTP (http), NULL);
rpm-build 4f3c61
rpm-build 4f3c61
	return g_object_ref (http->priv->msg);
rpm-build 4f3c61
}