1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
5 * Copyright (C) 2010, Igalia S.L.
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.
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.
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.
24 #include "soup-requester.h"
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>
32 struct _WebKitSoupRequesterPrivate {
33 GHashTable *request_types;
36 #define WEBKIT_SOUP_REQUESTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), WEBKIT_TYPE_SOUP_REQUESTER, WebKitSoupRequesterPrivate))
38 G_DEFINE_TYPE (WebKitSoupRequester, webkit_soup_requester, G_TYPE_OBJECT)
40 static void webkit_soup_requester_init (WebKitSoupRequester *requester)
42 requester->priv = WEBKIT_SOUP_REQUESTER_GET_PRIVATE (requester);
44 requester->priv->request_types = 0;
47 static void finalize (GObject *object)
49 WebKitSoupRequester *requester = WEBKIT_SOUP_REQUESTER (object);
51 if (requester->priv->request_types)
52 g_hash_table_destroy (requester->priv->request_types);
54 G_OBJECT_CLASS (webkit_soup_requester_parent_class)->finalize (object);
57 static void webkit_soup_requester_class_init (WebKitSoupRequesterClass *requester_class)
59 GObjectClass *object_class = G_OBJECT_CLASS (requester_class);
61 g_type_class_add_private (requester_class, sizeof (WebKitSoupRequesterPrivate));
63 /* virtual method override */
64 object_class->finalize = finalize;
67 static void init_request_types (WebKitSoupRequesterPrivate *priv)
69 if (priv->request_types)
72 priv->request_types = g_hash_table_new_full (soup_str_case_hash,
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));
87 WebKitSoupRequester *webkit_soup_requester_new (void)
89 return (WebKitSoupRequester *)g_object_new (WEBKIT_TYPE_SOUP_REQUESTER, 0);
92 WebKitSoupRequest *webkit_soup_requester_request (WebKitSoupRequester *requester, const char *uriString, SoupSession *session, GError **error)
95 WebKitSoupRequest *req;
97 uri = soup_uri_new (uriString);
99 g_set_error (error, WEBKIT_SOUP_ERROR, WEBKIT_SOUP_ERROR_BAD_URI,
100 _ ("Could not parse URI '%s'"), uriString);
104 req = webkit_soup_requester_request_uri (requester, uri, session, error);
109 WebKitSoupRequest *webkit_soup_requester_request_uri (WebKitSoupRequester *requester, SoupURI *uri, SoupSession *session, GError **error)
113 g_return_val_if_fail (WEBKIT_IS_SOUP_REQUESTER (requester), 0);
115 init_request_types (requester->priv);
116 requestType = (GType)GPOINTER_TO_SIZE (g_hash_table_lookup (requester->priv->request_types, uri->scheme));
118 g_set_error (error, WEBKIT_SOUP_ERROR, WEBKIT_SOUP_ERROR_UNSUPPORTED_URI_SCHEME,
119 _ ("Unsupported URI scheme '%s'"), uri->scheme);
123 if (g_type_is_a (requestType, G_TYPE_INITABLE)) {
124 return (WebKitSoupRequest *)g_initable_new (requestType, 0, error,
129 return (WebKitSoupRequest *)g_object_new (requestType,
138 soup_scheme_is_valid (const char *scheme)
140 if (scheme == NULL ||
141 !g_ascii_isalpha (*scheme))
146 if (!g_ascii_isalpha (*scheme) &&
147 !g_ascii_isdigit (*scheme) &&
158 webkit_soup_requester_add_protocol (WebKitSoupRequester *requester,
162 g_return_if_fail (WEBKIT_IS_SOUP_REQUESTER (requester));
163 g_return_if_fail (soup_scheme_is_valid (scheme));
165 init_request_types (requester->priv);
166 g_hash_table_insert (requester->priv->request_types, g_strdup (scheme),
167 GSIZE_TO_POINTER (request_type));
171 webkit_soup_requester_remove_protocol (WebKitSoupRequester *requester,
174 g_return_if_fail (WEBKIT_IS_SOUP_REQUESTER (requester));
175 g_return_if_fail (soup_scheme_is_valid (scheme));
177 init_request_types (requester->priv);
178 g_hash_table_remove (requester->priv->request_types, scheme);
182 webkit_soup_error_quark (void)
186 error = g_quark_from_static_string ("webkit_soup_error_quark");