summaryrefslogtreecommitdiffstats
path: root/WebKit/gtk/webkit/webkitnetworkresponse.cpp
blob: 0aca6d894fd6306bb0c31ee763bb84c10a277ea7 (plain)
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
/*
 * Copyright (C) 2007, 2008 Holger Hans Peter Freyther
 * Copyright (C) 2009 Gustavo Noronha Silva
 * Copyright (C) 2009 Collabora Ltd.
 *
 * 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 "webkitnetworkresponse.h"

#include "GRefPtr.h"
#include "ResourceResponse.h"
#include "webkitprivate.h"

#include <glib/gi18n-lib.h>

/**
 * SECTION:webkitnetworkresponse
 * @short_description: the response given to a network request
 * @see_also: #WebKitNetworkRequest
 *
 * This class represents the network related aspects of a navigation
 * response.
 *
 * Since: 1.1.14
 */

G_DEFINE_TYPE(WebKitNetworkResponse, webkit_network_response, G_TYPE_OBJECT);

struct _WebKitNetworkResponsePrivate {
    gchar* uri;
    SoupMessage* message;
};

#define WEBKIT_NETWORK_RESPONSE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_NETWORK_RESPONSE, WebKitNetworkResponsePrivate))

enum {
    PROP_0,

    PROP_URI,
    PROP_MESSAGE,
};

static void webkit_network_response_dispose(GObject* object)
{
    WebKitNetworkResponse* response = WEBKIT_NETWORK_RESPONSE(object);
    WebKitNetworkResponsePrivate* priv = response->priv;

    if (priv->message) {
        g_object_unref(priv->message);
        priv->message = NULL;
    }

    G_OBJECT_CLASS(webkit_network_response_parent_class)->dispose(object);
}

static void webkit_network_response_finalize(GObject* object)
{
    WebKitNetworkResponse* response = WEBKIT_NETWORK_RESPONSE(object);
    WebKitNetworkResponsePrivate* priv = response->priv;

    g_free(priv->uri);

    G_OBJECT_CLASS(webkit_network_response_parent_class)->finalize(object);
}

static void webkit_network_response_get_property(GObject* object, guint propertyID, GValue* value, GParamSpec* pspec)
{
    WebKitNetworkResponse* response = WEBKIT_NETWORK_RESPONSE(object);

    switch(propertyID) {
    case PROP_URI:
        g_value_set_string(value, webkit_network_response_get_uri(response));
        break;
    case PROP_MESSAGE:
        g_value_set_object(value, webkit_network_response_get_message(response));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec);
    }
}

static void webkit_network_response_set_property(GObject* object, guint propertyID, const GValue* value, GParamSpec* pspec)
{
    WebKitNetworkResponse* response = WEBKIT_NETWORK_RESPONSE(object);
    WebKitNetworkResponsePrivate* priv = response->priv;

    switch(propertyID) {
    case PROP_URI:
        webkit_network_response_set_uri(response, 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_response_class_init(WebKitNetworkResponseClass* responseClass)
{
    GObjectClass* objectClass = G_OBJECT_CLASS(responseClass);

    objectClass->dispose = webkit_network_response_dispose;
    objectClass->finalize = webkit_network_response_finalize;
    objectClass->get_property = webkit_network_response_get_property;
    objectClass->set_property = webkit_network_response_set_property;

    webkit_init();

    /**
     * WebKitNetworkResponse:uri:
     *
     * The URI to which the response will be made.
     *
     * Since: 1.1.14
     */
    g_object_class_install_property(objectClass, PROP_URI,
                                    g_param_spec_string("uri",
                                                        _("URI"),
                                                        _("The URI to which the response will be made."),
                                                        NULL,
                                                        (GParamFlags)(WEBKIT_PARAM_READWRITE)));

    /**
     * WebKitNetworkResponse:message:
     *
     * The #SoupMessage that backs the response.
     *
     * Since: 1.1.14
     */
    g_object_class_install_property(objectClass, PROP_MESSAGE,
                                    g_param_spec_object("message",
                                                        _("Message"),
                                                        _("The SoupMessage that backs the response."),
                                                        SOUP_TYPE_MESSAGE,
                                                        (GParamFlags)(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY)));

    g_type_class_add_private(responseClass, sizeof(WebKitNetworkResponsePrivate));
}

static void webkit_network_response_init(WebKitNetworkResponse* response)
{
    response->priv = WEBKIT_NETWORK_RESPONSE_GET_PRIVATE(response);
}

// for internal use only
WebKitNetworkResponse* webkit_network_response_new_with_core_response(const WebCore::ResourceResponse& resourceResponse)
{
    PlatformRefPtr<SoupMessage> soupMessage(adoptPlatformRef(resourceResponse.toSoupMessage()));
    if (soupMessage)
        return WEBKIT_NETWORK_RESPONSE(g_object_new(WEBKIT_TYPE_NETWORK_RESPONSE, "message", soupMessage.get(), NULL));

    return WEBKIT_NETWORK_RESPONSE(g_object_new(WEBKIT_TYPE_NETWORK_RESPONSE, "uri", resourceResponse.url().string().utf8().data(), NULL));
}

/**
 * webkit_network_response_new:
 * @uri: an URI
 *
 * Creates a new #WebKitNetworkResponse initialized with an URI.
 *
 * Returns: a new #WebKitNetworkResponse, or %NULL if the URI is
 * invalid.
 *
 * Since: 1.1.14
 */
WebKitNetworkResponse* webkit_network_response_new(const gchar* uri)
{
    g_return_val_if_fail(uri, NULL);

    return WEBKIT_NETWORK_RESPONSE(g_object_new(WEBKIT_TYPE_NETWORK_RESPONSE, "uri", uri, NULL));
}

/**
 * webkit_network_response_set_uri:
 * @response: a #WebKitNetworkResponse
 * @uri: an URI
 *
 * Sets the URI held and used by the given response. When the response
 * has an associated #SoupMessage, its URI will also be set by this
 * call.
 *
 * Since: 1.1.14
 */
void webkit_network_response_set_uri(WebKitNetworkResponse* response, const gchar* uri)
{
    g_return_if_fail(WEBKIT_IS_NETWORK_RESPONSE(response));
    g_return_if_fail(uri);

    WebKitNetworkResponsePrivate* priv = response->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_response_get_uri:
 * @response: a #WebKitNetworkResponse
 *
 * Returns: the uri of the #WebKitNetworkResponse
 *
 * Since: 1.1.14
 */
G_CONST_RETURN gchar* webkit_network_response_get_uri(WebKitNetworkResponse* response)
{
    g_return_val_if_fail(WEBKIT_IS_NETWORK_RESPONSE(response), NULL);

    WebKitNetworkResponsePrivate* priv = response->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_response_get_message:
 * @response: a #WebKitNetworkResponse
 *
 * Obtains the #SoupMessage that represents the given response. Notice
 * that only the response side of the HTTP conversation is
 * represented.
 *
 * Returns: (transfer none): the #SoupMessage
 * Since: 1.1.14
 */
SoupMessage* webkit_network_response_get_message(WebKitNetworkResponse* response)
{
    g_return_val_if_fail(WEBKIT_IS_NETWORK_RESPONSE(response), NULL);

    WebKitNetworkResponsePrivate* priv = response->priv;

    return priv->message;
}