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