1 /*
2 * Copyright (C) 2007, 2008 Holger Hans Peter Freyther
3 * Copyright (C) 2009 Gustavo Noronha Silva
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #include "config.h"
22 #include "webkitnetworkrequest.h"
23
24 #include "GRefPtr.h"
25 #include "ResourceRequest.h"
26 #include "webkitglobalsprivate.h"
27 #include <glib/gi18n-lib.h>
28 #include <wtf/text/CString.h>
29
30 /**
31 * SECTION:webkitnetworkrequest
32 * @short_description: The target of a navigation request
33 * @see_also: #WebKitWebView::navigation-policy-decision-requested
34 *
35 * This class represents the network related aspects of a navigation
36 * request. It is used whenever WebKit wants to provide information
37 * about a request that will be sent, or has been sent. Inside it you
38 * can find the URI of the request, and, for valid URIs, a
39 * #SoupMessage object, which provides access to further information
40 * such as headers.
41 *
42 */
43
44 G_DEFINE_TYPE(WebKitNetworkRequest, webkit_network_request, G_TYPE_OBJECT);
45
46 struct _WebKitNetworkRequestPrivate {
47 gchar* uri;
48 SoupMessage* message;
49 };
50
51 enum {
52 PROP_0,
53
54 PROP_URI,
55 PROP_MESSAGE,
56 };
57
webkit_network_request_dispose(GObject * object)58 static void webkit_network_request_dispose(GObject* object)
59 {
60 WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
61 WebKitNetworkRequestPrivate* priv = request->priv;
62
63 if (priv->message) {
64 g_object_unref(priv->message);
65 priv->message = NULL;
66 }
67
68 G_OBJECT_CLASS(webkit_network_request_parent_class)->dispose(object);
69 }
70
webkit_network_request_finalize(GObject * object)71 static void webkit_network_request_finalize(GObject* object)
72 {
73 WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
74 WebKitNetworkRequestPrivate* priv = request->priv;
75
76 g_free(priv->uri);
77
78 G_OBJECT_CLASS(webkit_network_request_parent_class)->finalize(object);
79 }
80
webkit_network_request_get_property(GObject * object,guint propertyID,GValue * value,GParamSpec * pspec)81 static void webkit_network_request_get_property(GObject* object, guint propertyID, GValue* value, GParamSpec* pspec)
82 {
83 WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
84
85 switch(propertyID) {
86 case PROP_URI:
87 g_value_set_string(value, webkit_network_request_get_uri(request));
88 break;
89 case PROP_MESSAGE:
90 g_value_set_object(value, webkit_network_request_get_message(request));
91 break;
92 default:
93 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
94 }
95 }
96
webkit_network_request_set_property(GObject * object,guint propertyID,const GValue * value,GParamSpec * pspec)97 static void webkit_network_request_set_property(GObject* object, guint propertyID, const GValue* value, GParamSpec* pspec)
98 {
99 WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
100 WebKitNetworkRequestPrivate* priv = request->priv;
101
102 switch(propertyID) {
103 case PROP_URI:
104 webkit_network_request_set_uri(request, g_value_get_string(value));
105 break;
106 case PROP_MESSAGE:
107 priv->message = SOUP_MESSAGE(g_value_dup_object(value));
108 break;
109 default:
110 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
111 }
112 }
113
webkit_network_request_class_init(WebKitNetworkRequestClass * requestClass)114 static void webkit_network_request_class_init(WebKitNetworkRequestClass* requestClass)
115 {
116 GObjectClass* objectClass = G_OBJECT_CLASS(requestClass);
117
118 objectClass->dispose = webkit_network_request_dispose;
119 objectClass->finalize = webkit_network_request_finalize;
120 objectClass->get_property = webkit_network_request_get_property;
121 objectClass->set_property = webkit_network_request_set_property;
122
123 webkitInit();
124
125 /**
126 * WebKitNetworkRequest:uri:
127 *
128 * The URI to which the request will be made.
129 *
130 * Since: 1.1.10
131 */
132 g_object_class_install_property(objectClass, PROP_URI,
133 g_param_spec_string("uri",
134 _("URI"),
135 _("The URI to which the request will be made."),
136 NULL,
137 (GParamFlags)(WEBKIT_PARAM_READWRITE)));
138
139 /**
140 * WebKitNetworkRequest:message:
141 *
142 * The #SoupMessage that backs the request.
143 *
144 * Since: 1.1.10
145 */
146 g_object_class_install_property(objectClass, PROP_MESSAGE,
147 g_param_spec_object("message",
148 _("Message"),
149 _("The SoupMessage that backs the request."),
150 SOUP_TYPE_MESSAGE,
151 (GParamFlags)(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)));
152
153 g_type_class_add_private(requestClass, sizeof(WebKitNetworkRequestPrivate));
154 }
155
webkit_network_request_init(WebKitNetworkRequest * request)156 static void webkit_network_request_init(WebKitNetworkRequest* request)
157 {
158 WebKitNetworkRequestPrivate* priv = G_TYPE_INSTANCE_GET_PRIVATE(request, WEBKIT_TYPE_NETWORK_REQUEST, WebKitNetworkRequestPrivate);
159 request->priv = priv;
160 }
161
162 /**
163 * webkit_network_request_new:
164 * @uri: an URI
165 *
166 * Creates a new #WebKitNetworkRequest initialized with an URI.
167 *
168 * Returns: a new #WebKitNetworkRequest, or %NULL if the URI is
169 * invalid.
170 */
webkit_network_request_new(const gchar * uri)171 WebKitNetworkRequest* webkit_network_request_new(const gchar* uri)
172 {
173 g_return_val_if_fail(uri, NULL);
174
175 return WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "uri", uri, NULL));
176 }
177
178 /**
179 * webkit_network_request_set_uri:
180 * @request: a #WebKitNetworkRequest
181 * @uri: an URI
182 *
183 * Sets the URI held and used by the given request. When the request
184 * has an associated #SoupMessage, its URI will also be set by this
185 * call.
186 *
187 */
webkit_network_request_set_uri(WebKitNetworkRequest * request,const gchar * uri)188 void webkit_network_request_set_uri(WebKitNetworkRequest* request, const gchar* uri)
189 {
190 g_return_if_fail(WEBKIT_IS_NETWORK_REQUEST(request));
191 g_return_if_fail(uri);
192
193 WebKitNetworkRequestPrivate* priv = request->priv;
194
195 if (priv->uri)
196 g_free(priv->uri);
197 priv->uri = g_strdup(uri);
198
199 if (!priv->message)
200 return;
201
202 SoupURI* soupURI = soup_uri_new(uri);
203 g_return_if_fail(soupURI);
204
205 soup_message_set_uri(priv->message, soupURI);
206 soup_uri_free(soupURI);
207 }
208
209 /**
210 * webkit_network_request_get_uri:
211 * @request: a #WebKitNetworkRequest
212 *
213 * Returns: the uri of the #WebKitNetworkRequest
214 *
215 * Since: 1.0.0
216 */
webkit_network_request_get_uri(WebKitNetworkRequest * request)217 G_CONST_RETURN gchar* webkit_network_request_get_uri(WebKitNetworkRequest* request)
218 {
219 g_return_val_if_fail(WEBKIT_IS_NETWORK_REQUEST(request), NULL);
220
221 WebKitNetworkRequestPrivate* priv = request->priv;
222
223 if (priv->uri)
224 return priv->uri;
225
226 SoupURI* soupURI = soup_message_get_uri(priv->message);
227 priv->uri = soup_uri_to_string(soupURI, FALSE);
228 return priv->uri;
229 }
230
231 /**
232 * webkit_network_request_get_message:
233 * @request: a #WebKitNetworkRequest
234 *
235 * Obtains the #SoupMessage held and used by the given request. Notice
236 * that modification of the SoupMessage of a request by signal
237 * handlers is only supported (as in, will only affect what is
238 * actually sent to the server) where explicitly documented.
239 *
240 * Returns: (transfer none): the #SoupMessage
241 * Since: 1.1.9
242 */
webkit_network_request_get_message(WebKitNetworkRequest * request)243 SoupMessage* webkit_network_request_get_message(WebKitNetworkRequest* request)
244 {
245 g_return_val_if_fail(WEBKIT_IS_NETWORK_REQUEST(request), NULL);
246
247 WebKitNetworkRequestPrivate* priv = request->priv;
248
249 return priv->message;
250 }
251
252 namespace WebKit {
253
kitNew(const WebCore::ResourceRequest & resourceRequest)254 WebKitNetworkRequest* kitNew(const WebCore::ResourceRequest& resourceRequest)
255 {
256 GRefPtr<SoupMessage> soupMessage(adoptGRef(resourceRequest.toSoupMessage()));
257 if (soupMessage)
258 return WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "message", soupMessage.get(), NULL));
259
260 return WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "uri", resourceRequest.url().string().utf8().data(), NULL));
261 }
262
core(WebKitNetworkRequest * request)263 WebCore::ResourceRequest core(WebKitNetworkRequest* request)
264 {
265 SoupMessage* soupMessage = webkit_network_request_get_message(request);
266 if (soupMessage)
267 return WebCore::ResourceRequest(soupMessage);
268
269 WebCore::KURL url = WebCore::KURL(WebCore::KURL(), String::fromUTF8(webkit_network_request_get_uri(request)));
270 return WebCore::ResourceRequest(url);
271 }
272
273 }
274