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 "CString.h"
25 #include "GOwnPtr.h"
26 #include "ResourceRequest.h"
27 #include "webkitprivate.h"
28
29 #include <glib/gi18n-lib.h>
30
31 /**
32 * SECTION:webkitnetworkrequest
33 * @short_description: The target of a navigation request
34 * @see_also: #WebKitWebView::navigation-policy-decision-requested
35 *
36 * This class represents the network related aspects of a navigation
37 * request. It is used whenever WebKit wants to provide information
38 * about a request that will be sent, or has been sent. Inside it you
39 * can find the URI of the request, and, for valid URIs, a
40 * #SoupMessage object, which provides access to further information
41 * such as headers.
42 *
43 */
44
45 G_DEFINE_TYPE(WebKitNetworkRequest, webkit_network_request, G_TYPE_OBJECT);
46
47 struct _WebKitNetworkRequestPrivate {
48 gchar* uri;
49 SoupMessage* message;
50 };
51
52 #define WEBKIT_NETWORK_REQUEST_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_NETWORK_REQUEST, WebKitNetworkRequestPrivate))
53
54 enum {
55 PROP_0,
56
57 PROP_URI,
58 PROP_MESSAGE,
59 };
60
webkit_network_request_dispose(GObject * object)61 static void webkit_network_request_dispose(GObject* object)
62 {
63 WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
64 WebKitNetworkRequestPrivate* priv = request->priv;
65
66 if (priv->message) {
67 g_object_unref(priv->message);
68 priv->message = NULL;
69 }
70
71 G_OBJECT_CLASS(webkit_network_request_parent_class)->dispose(object);
72 }
73
webkit_network_request_finalize(GObject * object)74 static void webkit_network_request_finalize(GObject* object)
75 {
76 WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
77 WebKitNetworkRequestPrivate* priv = request->priv;
78
79 g_free(priv->uri);
80
81 G_OBJECT_CLASS(webkit_network_request_parent_class)->finalize(object);
82 }
83
webkit_network_request_get_property(GObject * object,guint propertyID,GValue * value,GParamSpec * pspec)84 static void webkit_network_request_get_property(GObject* object, guint propertyID, GValue* value, GParamSpec* pspec)
85 {
86 WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
87
88 switch(propertyID) {
89 case PROP_URI:
90 g_value_set_string(value, webkit_network_request_get_uri(request));
91 break;
92 case PROP_MESSAGE:
93 g_value_set_object(value, webkit_network_request_get_message(request));
94 break;
95 default:
96 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
97 }
98 }
99
webkit_network_request_set_property(GObject * object,guint propertyID,const GValue * value,GParamSpec * pspec)100 static void webkit_network_request_set_property(GObject* object, guint propertyID, const GValue* value, GParamSpec* pspec)
101 {
102 WebKitNetworkRequest* request = WEBKIT_NETWORK_REQUEST(object);
103 WebKitNetworkRequestPrivate* priv = request->priv;
104
105 switch(propertyID) {
106 case PROP_URI:
107 webkit_network_request_set_uri(request, g_value_get_string(value));
108 break;
109 case PROP_MESSAGE:
110 priv->message = SOUP_MESSAGE(g_value_dup_object(value));
111 break;
112 default:
113 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
114 }
115 }
116
webkit_network_request_class_init(WebKitNetworkRequestClass * requestClass)117 static void webkit_network_request_class_init(WebKitNetworkRequestClass* requestClass)
118 {
119 GObjectClass* objectClass = G_OBJECT_CLASS(requestClass);
120
121 objectClass->dispose = webkit_network_request_dispose;
122 objectClass->finalize = webkit_network_request_finalize;
123 objectClass->get_property = webkit_network_request_get_property;
124 objectClass->set_property = webkit_network_request_set_property;
125
126 webkit_init();
127
128 /**
129 * WebKitNetworkRequest:uri:
130 *
131 * The URI to which the request will be made.
132 *
133 * Since: 1.1.10
134 */
135 g_object_class_install_property(objectClass, PROP_URI,
136 g_param_spec_string("uri",
137 _("URI"),
138 _("The URI to which the request will be made."),
139 NULL,
140 (GParamFlags)(WEBKIT_PARAM_READWRITE)));
141
142 /**
143 * WebKitNetworkRequest:message:
144 *
145 * The #SoupMessage that backs the request.
146 *
147 * Since: 1.1.10
148 */
149 g_object_class_install_property(objectClass, PROP_MESSAGE,
150 g_param_spec_object("message",
151 _("Message"),
152 _("The SoupMessage that backs the request."),
153 SOUP_TYPE_MESSAGE,
154 (GParamFlags)(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)));
155
156 g_type_class_add_private(requestClass, sizeof(WebKitNetworkRequestPrivate));
157 }
158
webkit_network_request_init(WebKitNetworkRequest * request)159 static void webkit_network_request_init(WebKitNetworkRequest* request)
160 {
161 WebKitNetworkRequestPrivate* priv = WEBKIT_NETWORK_REQUEST_GET_PRIVATE(request);
162 request->priv = priv;
163 }
164
165 // for internal use only
webkit_network_request_new_with_core_request(const WebCore::ResourceRequest & resourceRequest)166 WebKitNetworkRequest* webkit_network_request_new_with_core_request(const WebCore::ResourceRequest& resourceRequest)
167 {
168 GOwnPtr<SoupMessage> soupMessage(resourceRequest.toSoupMessage());
169 if (soupMessage)
170 return WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "message", soupMessage.get(), NULL));
171
172 return WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "uri", resourceRequest.url().string().utf8().data(), NULL));
173 }
174
175 /**
176 * webkit_network_request_new:
177 * @uri: an URI
178 *
179 * Creates a new #WebKitNetworkRequest initialized with an URI.
180 *
181 * Returns: a new #WebKitNetworkRequest, or %NULL if the URI is
182 * invalid.
183 */
webkit_network_request_new(const gchar * uri)184 WebKitNetworkRequest* webkit_network_request_new(const gchar* uri)
185 {
186 g_return_val_if_fail(uri, NULL);
187
188 return WEBKIT_NETWORK_REQUEST(g_object_new(WEBKIT_TYPE_NETWORK_REQUEST, "uri", uri, NULL));
189 }
190
191 /**
192 * webkit_network_request_set_uri:
193 * @request: a #WebKitNetworkRequest
194 * @uri: an URI
195 *
196 * Sets the URI held and used by the given request. When the request
197 * has an associated #SoupMessage, its URI will also be set by this
198 * call.
199 *
200 */
webkit_network_request_set_uri(WebKitNetworkRequest * request,const gchar * uri)201 void webkit_network_request_set_uri(WebKitNetworkRequest* request, const gchar* uri)
202 {
203 g_return_if_fail(WEBKIT_IS_NETWORK_REQUEST(request));
204 g_return_if_fail(uri);
205
206 WebKitNetworkRequestPrivate* priv = request->priv;
207
208 if (priv->uri)
209 g_free(priv->uri);
210 priv->uri = g_strdup(uri);
211
212 if (!priv->message)
213 return;
214
215 SoupURI* soupURI = soup_uri_new(uri);
216 g_return_if_fail(soupURI);
217
218 soup_message_set_uri(priv->message, soupURI);
219 soup_uri_free(soupURI);
220 }
221
222 /**
223 * webkit_network_request_get_uri:
224 * @request: a #WebKitNetworkRequest
225 *
226 * Returns: the uri of the #WebKitNetworkRequest
227 *
228 * Since: 1.0.0
229 */
webkit_network_request_get_uri(WebKitNetworkRequest * request)230 G_CONST_RETURN gchar* webkit_network_request_get_uri(WebKitNetworkRequest* request)
231 {
232 g_return_val_if_fail(WEBKIT_IS_NETWORK_REQUEST(request), NULL);
233
234 WebKitNetworkRequestPrivate* priv = request->priv;
235
236 if (priv->uri)
237 return priv->uri;
238
239 SoupURI* soupURI = soup_message_get_uri(priv->message);
240 priv->uri = soup_uri_to_string(soupURI, FALSE);
241 return priv->uri;
242 }
243
244 /**
245 * webkit_network_request_get_soup_message:
246 * @request: a #WebKitNetworkRequest
247 *
248 * Obtains the #SoupMessage held and used by the given request. Notice
249 * that modification of the SoupMessage of a request by signal
250 * handlers is only supported (as in, will only affect what is
251 * actually sent to the server) where explicitly documented.
252 *
253 * Returns: the #SoupMessage
254 * Since: 1.1.9
255 */
webkit_network_request_get_message(WebKitNetworkRequest * request)256 SoupMessage* webkit_network_request_get_message(WebKitNetworkRequest* request)
257 {
258 g_return_val_if_fail(WEBKIT_IS_NETWORK_REQUEST(request), NULL);
259
260 WebKitNetworkRequestPrivate* priv = request->priv;
261
262 return priv->message;
263 }
264