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