1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * soup-request-data.c: data: URI request object
5 * Copyright (C) 2009 Red Hat, Inc.
6 * Copyright (C) 2010 Igalia, S.L.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
27 #include "soup-request-data.h"
29 #include "soup-requester.h"
30 #include <libsoup/soup.h>
31 #include <glib/gi18n.h>
33 G_DEFINE_TYPE (WebKitSoupRequestData, webkit_soup_request_data, WEBKIT_TYPE_SOUP_REQUEST)
35 struct _WebKitSoupRequestDataPrivate {
41 webkit_soup_request_data_init (WebKitSoupRequestData *data)
43 data->priv = G_TYPE_INSTANCE_GET_PRIVATE (data, WEBKIT_TYPE_SOUP_REQUEST_DATA, WebKitSoupRequestDataPrivate);
47 webkit_soup_request_data_finalize (GObject *object)
49 WebKitSoupRequestData *data = WEBKIT_SOUP_REQUEST_DATA (object);
51 g_free (data->priv->content_type);
53 G_OBJECT_CLASS (webkit_soup_request_data_parent_class)->finalize (object);
57 webkit_soup_request_data_check_uri (WebKitSoupRequest *request,
61 return uri->host == NULL;
65 webkit_soup_request_data_send (WebKitSoupRequest *request,
66 GCancellable *cancellable,
69 WebKitSoupRequestData *data = WEBKIT_SOUP_REQUEST_DATA (request);
70 SoupURI *uri = webkit_soup_request_get_uri (request);
71 GInputStream *memstream;
72 const char *comma, *semi, *start, *end;
73 gboolean base64 = FALSE;
75 gchar *uristr = soup_uri_to_string (uri, FALSE);
76 comma = strchr (uristr, ',');
77 if (comma && comma != uristr) {
78 /* Deal with MIME type / params */
79 semi = memchr (uristr, ';', comma - uristr);
80 end = semi ? semi : comma;
82 if (semi && !g_ascii_strncasecmp (semi, ";base64", MAX (comma - semi, strlen (";base64"))))
87 data->priv->content_type = g_strndup (uristr, end - uristr);
89 data->priv->content_type =
90 webkit_soup_request_uri_decoded_copy (uristr, end - uristr);
93 memstream = g_memory_input_stream_new ();
95 start = comma ? comma + 1 : uristr;
101 int inlen, state = 0;
104 inlen = strlen (start);
105 buf = g_malloc (inlen * 3 / 4);
106 data->priv->content_length =
107 g_base64_decode_step (start, inlen, buf,
114 /* Cannot use g_uri_unescape_string nor
115 soup_uri_decode because we don't want to
116 fail for things like "%3E%%3C" -> ">%<" */
117 buf = (guchar *)webkit_soup_request_uri_decoded_copy (start, strlen (start));
120 data->priv->content_length = strlen ((char *)buf);
123 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (memstream),
124 buf, data->priv->content_length,
133 g_set_error (error, WEBKIT_SOUP_ERROR, WEBKIT_SOUP_ERROR_BAD_URI,
134 _ ("Unable to decode URI: %s"), start);
135 g_object_unref (memstream);
140 webkit_soup_request_data_get_content_length (WebKitSoupRequest *request)
142 WebKitSoupRequestData *data = WEBKIT_SOUP_REQUEST_DATA (request);
144 return data->priv->content_length;
148 webkit_soup_request_data_get_content_type (WebKitSoupRequest *request)
150 WebKitSoupRequestData *data = WEBKIT_SOUP_REQUEST_DATA (request);
152 return data->priv->content_type;
156 webkit_soup_request_data_class_init (WebKitSoupRequestDataClass *request_data_class)
158 GObjectClass *object_class = G_OBJECT_CLASS (request_data_class);
159 WebKitSoupRequestClass *request_class =
160 WEBKIT_SOUP_REQUEST_CLASS (request_data_class);
162 g_type_class_add_private (request_data_class, sizeof (WebKitSoupRequestDataPrivate));
164 object_class->finalize = webkit_soup_request_data_finalize;
166 request_class->check_uri = webkit_soup_request_data_check_uri;
167 request_class->send = webkit_soup_request_data_send;
168 request_class->get_content_length = webkit_soup_request_data_get_content_length;
169 request_class->get_content_type = webkit_soup_request_data_get_content_type;