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