2010-10-13 Sergio Villar Senin <svillar@igalia.com>
[WebKit-https.git] / WebCore / platform / network / soup / cache / 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 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 #include "soup-request-data.h"
28
29 #include "soup-requester.h"
30 #include <libsoup/soup.h>
31 #include <glib/gi18n.h>
32
33 G_DEFINE_TYPE (WebKitSoupRequestData, webkit_soup_request_data, WEBKIT_TYPE_SOUP_REQUEST)
34
35 struct _WebKitSoupRequestDataPrivate {
36         gsize content_length;
37         char *content_type;
38 };
39
40 static void
41 webkit_soup_request_data_init (WebKitSoupRequestData *data)
42 {
43         data->priv = G_TYPE_INSTANCE_GET_PRIVATE (data, WEBKIT_TYPE_SOUP_REQUEST_DATA, WebKitSoupRequestDataPrivate);
44 }
45
46 static void
47 webkit_soup_request_data_finalize (GObject *object)
48 {
49         WebKitSoupRequestData *data = WEBKIT_SOUP_REQUEST_DATA (object);
50
51         g_free (data->priv->content_type);
52
53         G_OBJECT_CLASS (webkit_soup_request_data_parent_class)->finalize (object);
54 }
55
56 static gboolean
57 webkit_soup_request_data_check_uri (WebKitSoupRequest  *request,
58                                     SoupURI      *uri,
59                                     GError      **error)
60 {
61         return uri->host == NULL;
62 }
63
64 static GInputStream *
65 webkit_soup_request_data_send (WebKitSoupRequest   *request,
66                                GCancellable  *cancellable,
67                                GError       **error)
68 {
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;
74
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;
81
82                 if (semi && !g_ascii_strncasecmp (semi, ";base64", MAX (comma - semi, strlen (";base64"))))
83                         base64 = TRUE;
84
85                 if (end != uristr)
86                         if (base64)
87                                 data->priv->content_type = g_strndup (uristr, end - uristr);
88                         else
89                                 data->priv->content_type =
90                                         webkit_soup_request_uri_decoded_copy (uristr, end - uristr);
91         }
92
93         memstream = g_memory_input_stream_new ();
94
95         start = comma ? comma + 1 : uristr;
96
97         if (*start) {
98                 guchar *buf;
99
100                 if (base64) {
101                         int inlen, state = 0;
102                         guint save = 0;
103
104                         inlen = strlen (start);
105                         buf = g_malloc (inlen * 3 / 4);
106                         data->priv->content_length =
107                                 g_base64_decode_step (start, inlen, buf,
108                                                       &state, &save);
109                         if (state != 0) {
110                                 g_free (buf);
111                                 goto fail;
112                         }
113                 } else {
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));
118                         if (!buf)
119                                 goto fail;
120                         data->priv->content_length = strlen ((char *)buf);
121                 }
122
123                 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (memstream),
124                                                 buf, data->priv->content_length,
125                                                 g_free);
126         }
127         g_free (uristr);
128
129         return memstream;
130
131  fail:
132         g_free (uristr);
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);
136         return NULL;
137 }
138
139 static goffset
140 webkit_soup_request_data_get_content_length (WebKitSoupRequest *request)
141 {
142         WebKitSoupRequestData *data = WEBKIT_SOUP_REQUEST_DATA (request);
143
144         return data->priv->content_length;
145 }
146
147 static const char *
148 webkit_soup_request_data_get_content_type (WebKitSoupRequest *request)
149 {
150         WebKitSoupRequestData *data = WEBKIT_SOUP_REQUEST_DATA (request);
151
152         return data->priv->content_type;
153 }
154
155 static void
156 webkit_soup_request_data_class_init (WebKitSoupRequestDataClass *request_data_class)
157 {
158         GObjectClass *object_class = G_OBJECT_CLASS (request_data_class);
159         WebKitSoupRequestClass *request_class =
160                 WEBKIT_SOUP_REQUEST_CLASS (request_data_class);
161
162         g_type_class_add_private (request_data_class, sizeof (WebKitSoupRequestDataPrivate));
163
164         object_class->finalize = webkit_soup_request_data_finalize;
165
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;
170 }