Git init
[profile/ivi/libsoup2.4.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 "soup-misc-private.h"
34 #include <libsoup/soup.h>
35 #include <glib/gi18n.h>
36
37 G_DEFINE_TYPE (SoupRequestData, soup_request_data, SOUP_TYPE_REQUEST)
38
39 struct _SoupRequestDataPrivate {
40         gsize content_length;
41         char *content_type;
42 };
43
44 static void
45 soup_request_data_init (SoupRequestData *data)
46 {
47         data->priv = G_TYPE_INSTANCE_GET_PRIVATE (data, SOUP_TYPE_REQUEST_DATA, SoupRequestDataPrivate);
48 }
49
50 static void
51 soup_request_data_finalize (GObject *object)
52 {
53         SoupRequestData *data = SOUP_REQUEST_DATA (object);
54
55         g_free (data->priv->content_type);
56
57         G_OBJECT_CLASS (soup_request_data_parent_class)->finalize (object);
58 }
59
60 static gboolean
61 soup_request_data_check_uri (SoupRequest  *request,
62                              SoupURI      *uri,
63                              GError      **error)
64 {
65         return uri->host == NULL;
66 }
67
68 #define BASE64_INDICATOR     ";base64"
69 #define BASE64_INDICATOR_LEN (sizeof (";base64") - 1)
70
71 static GInputStream *
72 soup_request_data_send (SoupRequest   *request,
73                         GCancellable  *cancellable,
74                         GError       **error)
75 {
76         SoupRequestData *data = SOUP_REQUEST_DATA (request);
77         SoupURI *uri = soup_request_get_uri (request);
78         GInputStream *memstream;
79         const char *comma, *start, *end;
80         gboolean base64 = FALSE;
81         char *uristr;
82
83         uristr = soup_uri_to_string (uri, FALSE);
84         start = uristr + 5;
85         comma = strchr (start, ',');
86         if (comma && comma != start) {
87                 /* Deal with MIME type / params */
88                 if (comma > start + BASE64_INDICATOR_LEN && !g_ascii_strncasecmp (comma - BASE64_INDICATOR_LEN, BASE64_INDICATOR, BASE64_INDICATOR_LEN)) {
89                         end = comma - BASE64_INDICATOR_LEN;
90                         base64 = TRUE;
91                 } else
92                         end = comma;
93
94                 if (end != start)
95                         data->priv->content_type = uri_decoded_copy (start, end - start);
96         }
97
98         memstream = g_memory_input_stream_new ();
99
100         if (comma)
101                 start = comma + 1;
102
103         if (*start) {
104                 guchar *buf = (guchar *) soup_uri_decode (start);
105
106                 if (base64)
107                         buf = g_base64_decode_inplace ((gchar*) buf, &data->priv->content_length);
108                 else
109                         data->priv->content_length = strlen ((const char *) buf);
110
111                 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (memstream),
112                                                 buf, data->priv->content_length,
113                                                 g_free);
114         }
115         g_free (uristr);
116
117         return memstream;
118 }
119
120 static goffset
121 soup_request_data_get_content_length (SoupRequest *request)
122 {
123         SoupRequestData *data = SOUP_REQUEST_DATA (request);
124
125         return data->priv->content_length;
126 }
127
128 static const char *
129 soup_request_data_get_content_type (SoupRequest *request)
130 {
131         SoupRequestData *data = SOUP_REQUEST_DATA (request);
132
133         if (data->priv->content_type)
134                 return data->priv->content_type;
135         else
136                 return "text/plain;charset=US-ASCII";
137 }
138
139 static const char *data_schemes[] = { "data", NULL };
140
141 static void
142 soup_request_data_class_init (SoupRequestDataClass *request_data_class)
143 {
144         GObjectClass *object_class = G_OBJECT_CLASS (request_data_class);
145         SoupRequestClass *request_class =
146                 SOUP_REQUEST_CLASS (request_data_class);
147
148         g_type_class_add_private (request_data_class, sizeof (SoupRequestDataPrivate));
149
150         request_class->schemes = data_schemes;
151
152         object_class->finalize = soup_request_data_finalize;
153
154         request_class->check_uri = soup_request_data_check_uri;
155         request_class->send = soup_request_data_send;
156         request_class->get_content_length = soup_request_data_get_content_length;
157         request_class->get_content_type = soup_request_data_get_content_type;
158 }