2010-10-13 Sergio Villar Senin <svillar@igalia.com>
[WebKit-https.git] / WebCore / platform / network / soup / cache / soup-requester.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * soup-requester.c:
4  *
5  * Copyright (C) 2010, Igalia S.L.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #include "config.h"
24 #include "soup-requester.h"
25
26 #include "soup-request-data.h"
27 #include "soup-request-file.h"
28 #include "soup-request-http.h"
29 #include <glib/gi18n.h>
30 #include <libsoup/soup.h>
31
32 struct _WebKitSoupRequesterPrivate {
33         GHashTable *request_types;
34 };
35
36 #define WEBKIT_SOUP_REQUESTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), WEBKIT_TYPE_SOUP_REQUESTER, WebKitSoupRequesterPrivate))
37
38 G_DEFINE_TYPE (WebKitSoupRequester, webkit_soup_requester, G_TYPE_OBJECT)
39
40 static void webkit_soup_requester_init (WebKitSoupRequester *requester)
41 {
42         requester->priv = WEBKIT_SOUP_REQUESTER_GET_PRIVATE (requester);
43
44         requester->priv->request_types = 0;
45 }
46
47 static void finalize (GObject *object)
48 {
49         WebKitSoupRequester *requester = WEBKIT_SOUP_REQUESTER (object);
50
51         if (requester->priv->request_types)
52                 g_hash_table_destroy (requester->priv->request_types);
53
54         G_OBJECT_CLASS (webkit_soup_requester_parent_class)->finalize (object);
55 }
56
57 static void webkit_soup_requester_class_init (WebKitSoupRequesterClass *requester_class)
58 {
59         GObjectClass *object_class = G_OBJECT_CLASS (requester_class);
60
61         g_type_class_add_private (requester_class, sizeof (WebKitSoupRequesterPrivate));
62
63         /* virtual method override */
64         object_class->finalize = finalize;
65 }
66
67 static void init_request_types (WebKitSoupRequesterPrivate *priv)
68 {
69         if (priv->request_types)
70                 return;
71
72         priv->request_types = g_hash_table_new_full (soup_str_case_hash,
73                                                      soup_str_case_equal,
74                                                      g_free, 0);
75         g_hash_table_insert (priv->request_types, g_strdup ("file"),
76                              GSIZE_TO_POINTER (WEBKIT_TYPE_SOUP_REQUEST_FILE));
77         g_hash_table_insert (priv->request_types, g_strdup ("data"),
78                              GSIZE_TO_POINTER (WEBKIT_TYPE_SOUP_REQUEST_DATA));
79         g_hash_table_insert (priv->request_types, g_strdup ("http"),
80                              GSIZE_TO_POINTER (WEBKIT_TYPE_SOUP_REQUEST_HTTP));
81         g_hash_table_insert (priv->request_types, g_strdup ("https"),
82                              GSIZE_TO_POINTER (WEBKIT_TYPE_SOUP_REQUEST_HTTP));
83         g_hash_table_insert (priv->request_types, g_strdup ("ftp"),
84                              GSIZE_TO_POINTER (WEBKIT_TYPE_SOUP_REQUEST_FILE));
85 }
86
87 WebKitSoupRequester *webkit_soup_requester_new (void)
88 {
89         return (WebKitSoupRequester *)g_object_new (WEBKIT_TYPE_SOUP_REQUESTER, 0);
90 }
91
92 WebKitSoupRequest *webkit_soup_requester_request (WebKitSoupRequester *requester, const char *uriString, SoupSession *session, GError **error)
93 {
94         SoupURI *uri = NULL;
95         WebKitSoupRequest *req;
96
97         uri = soup_uri_new (uriString);
98         if (!uri) {
99                 g_set_error (error, WEBKIT_SOUP_ERROR, WEBKIT_SOUP_ERROR_BAD_URI,
100                              _ ("Could not parse URI '%s'"), uriString);
101                 return 0;
102         }
103
104         req = webkit_soup_requester_request_uri (requester, uri, session, error);
105         soup_uri_free (uri);
106         return req;
107 }
108
109 WebKitSoupRequest *webkit_soup_requester_request_uri (WebKitSoupRequester *requester, SoupURI *uri, SoupSession *session, GError **error)
110 {
111         GType requestType;
112
113         g_return_val_if_fail (WEBKIT_IS_SOUP_REQUESTER (requester), 0);
114
115         init_request_types (requester->priv);
116         requestType = (GType)GPOINTER_TO_SIZE (g_hash_table_lookup (requester->priv->request_types, uri->scheme));
117         if (!requestType) {
118                 g_set_error (error, WEBKIT_SOUP_ERROR, WEBKIT_SOUP_ERROR_UNSUPPORTED_URI_SCHEME,
119                              _ ("Unsupported URI scheme '%s'"), uri->scheme);
120                 return 0;
121         }
122
123         if (g_type_is_a (requestType, G_TYPE_INITABLE)) {
124                 return (WebKitSoupRequest *)g_initable_new (requestType, 0, error,
125                                                             "uri", uri,
126                                                             "session", session,
127                                                             0);
128         } else {
129                 return (WebKitSoupRequest *)g_object_new (requestType,
130                                                           "uri", uri,
131                                                           "session", session,
132                                                           0);
133         }
134 }
135
136 /* RFC 2396, 3.1 */
137 static gboolean
138 soup_scheme_is_valid (const char *scheme)
139 {
140         if (scheme == NULL ||
141             !g_ascii_isalpha (*scheme))
142                 return FALSE;
143
144         scheme++;
145         while (*scheme) {
146                 if (!g_ascii_isalpha (*scheme) &&
147                     !g_ascii_isdigit (*scheme) &&
148                     *scheme != '+' &&
149                     *scheme != '-' &&
150                     *scheme != '.')
151                         return FALSE;
152                 scheme++;
153         }
154         return TRUE;
155 }
156
157 void
158 webkit_soup_requester_add_protocol (WebKitSoupRequester *requester,
159                                     const char  *scheme,
160                                     GType request_type)
161 {
162         g_return_if_fail (WEBKIT_IS_SOUP_REQUESTER (requester));
163         g_return_if_fail (soup_scheme_is_valid (scheme));
164
165         init_request_types (requester->priv);
166         g_hash_table_insert (requester->priv->request_types, g_strdup (scheme),
167                              GSIZE_TO_POINTER (request_type));
168 }
169
170 void
171 webkit_soup_requester_remove_protocol (WebKitSoupRequester *requester,
172                                        const char  *scheme)
173 {
174         g_return_if_fail (WEBKIT_IS_SOUP_REQUESTER (requester));
175         g_return_if_fail (soup_scheme_is_valid (scheme));
176
177         init_request_types (requester->priv);
178         g_hash_table_remove (requester->priv->request_types, scheme);
179 }
180
181 GQuark
182 webkit_soup_error_quark (void)
183 {
184         static GQuark error;
185         if (!error)
186                 error = g_quark_from_static_string ("webkit_soup_error_quark");
187         return error;
188 }