soup-request-data: return decoded contents for non-base64 data URLs
[platform/upstream/libsoup.git] / libsoup / soup-request-data.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * soup-request-data.c: data: URI request object
4  *
5  * Copyright (C) 2009, 2010 Red Hat, Inc.
6  * Copyright (C) 2010 Igalia, S.L.
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #define LIBSOUP_USE_UNSTABLE_REQUEST_API
29
30 #include "soup-request-data.h"
31
32 #include "soup-requester.h"
33 #include <libsoup/soup.h>
34 #include <glib/gi18n.h>
35
36 G_DEFINE_TYPE (SoupRequestData, soup_request_data, SOUP_TYPE_REQUEST)
37
38 struct _SoupRequestDataPrivate {
39         gsize content_length;
40         char *content_type;
41 };
42
43 static void
44 soup_request_data_init (SoupRequestData *data)
45 {
46         data->priv = G_TYPE_INSTANCE_GET_PRIVATE (data, SOUP_TYPE_REQUEST_DATA, SoupRequestDataPrivate);
47 }
48
49 static void
50 soup_request_data_finalize (GObject *object)
51 {
52         SoupRequestData *data = SOUP_REQUEST_DATA (object);
53
54         g_free (data->priv->content_type);
55
56         G_OBJECT_CLASS (soup_request_data_parent_class)->finalize (object);
57 }
58
59 static gboolean
60 soup_request_data_check_uri (SoupRequest  *request,
61                              SoupURI      *uri,
62                              GError      **error)
63 {
64         return uri->host == NULL;
65 }
66
67 #define BASE64_INDICATOR     ";base64"
68 #define BASE64_INDICATOR_LEN (sizeof (";base64") - 1)
69
70 static GInputStream *
71 soup_request_data_send (SoupRequest   *request,
72                         GCancellable  *cancellable,
73                         GError       **error)
74 {
75         SoupRequestData *data = SOUP_REQUEST_DATA (request);
76         SoupURI *uri = soup_request_get_uri (request);
77         GInputStream *memstream;
78         const char *comma, *start, *end;
79         gboolean base64 = FALSE;
80         char *uristr;
81
82         uristr = soup_uri_to_string (uri, FALSE);
83         start = uristr + 5;
84         comma = strchr (start, ',');
85         if (comma && comma != start) {
86                 /* Deal with MIME type / params */
87                 if (comma > start + BASE64_INDICATOR_LEN && !g_ascii_strncasecmp (comma - BASE64_INDICATOR_LEN, BASE64_INDICATOR, BASE64_INDICATOR_LEN)) {
88                         end = comma - BASE64_INDICATOR_LEN;
89                         base64 = TRUE;
90                 } else
91                         end = comma;
92
93                 if (end != start) {
94                         char *encoded_content_type = g_strndup (start, end - start);
95
96                         if (base64)
97                                 data->priv->content_type = encoded_content_type;
98                         else {
99                                 data->priv->content_type = soup_uri_decode (encoded_content_type);
100                                 g_free (encoded_content_type);
101                         }
102                 }
103         }
104
105         memstream = g_memory_input_stream_new ();
106
107         if (comma)
108                 start = comma + 1;
109
110         if (*start) {
111                 guchar *buf;
112
113                 if (base64) {
114                         int inlen, state = 0;
115                         guint save = 0;
116
117                         inlen = strlen (start);
118                         buf = g_malloc0 (inlen * 3 / 4 + 3);
119                         data->priv->content_length =
120                                 g_base64_decode_step (start, inlen, buf,
121                                                       &state, &save);
122                         if (state != 0) {
123                                 g_free (buf);
124                                 goto fail;
125                         }
126                 } else {
127                         buf = (guchar *) soup_uri_decode (start);
128                         data->priv->content_length = strlen ((const char *) buf);
129                 }
130
131                 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (memstream),
132                                                 buf, data->priv->content_length,
133                                                 g_free);
134         }
135         g_free (uristr);
136
137         return memstream;
138
139  fail:
140         g_free (uristr);
141         g_set_error (error, SOUP_REQUESTER_ERROR, SOUP_REQUESTER_ERROR_BAD_URI,
142                      _("Unable to decode URI: %s"), start);
143         g_object_unref (memstream);
144         return NULL;
145 }
146
147 static goffset
148 soup_request_data_get_content_length (SoupRequest *request)
149 {
150         SoupRequestData *data = SOUP_REQUEST_DATA (request);
151
152         return data->priv->content_length;
153 }
154
155 static const char *
156 soup_request_data_get_content_type (SoupRequest *request)
157 {
158         SoupRequestData *data = SOUP_REQUEST_DATA (request);
159
160         if (data->priv->content_type)
161                 return data->priv->content_type;
162         else
163                 return "text/plain;charset=US-ASCII";
164 }
165
166 static const char *data_schemes[] = { "data", NULL };
167
168 static void
169 soup_request_data_class_init (SoupRequestDataClass *request_data_class)
170 {
171         GObjectClass *object_class = G_OBJECT_CLASS (request_data_class);
172         SoupRequestClass *request_class =
173                 SOUP_REQUEST_CLASS (request_data_class);
174
175         g_type_class_add_private (request_data_class, sizeof (SoupRequestDataPrivate));
176
177         request_class->schemes = data_schemes;
178
179         object_class->finalize = soup_request_data_finalize;
180
181         request_class->check_uri = soup_request_data_check_uri;
182         request_class->send = soup_request_data_send;
183         request_class->get_content_length = soup_request_data_get_content_length;
184         request_class->get_content_type = soup_request_data_get_content_type;
185 }