1 /* 2 * Copyright (C) 2007, 2008 Holger Hans Peter Freyther 3 * Copyright (C) 2009 Gustavo Noronha Silva 4 * Copyright (C) 2009 Collabora Ltd. 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Library General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Library General Public License for more details. 15 * 16 * You should have received a copy of the GNU Library General Public License 17 * along with this library; see the file COPYING.LIB. If not, write to 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA. 20 */ 21 22 #include "config.h" 23 #include "webkitnetworkresponse.h" 24 25 #include "GOwnPtr.h" 26 #include "ResourceResponse.h" 27 #include "webkitprivate.h" 28 29 #include <glib/gi18n-lib.h> 30 31 /** 32 * SECTION:webkitnetworkresponse 33 * @short_description: the response given to a network request 34 * @see_also: #WebKitNetworkRequest 35 * 36 * This class represents the network related aspects of a navigation 37 * response. 38 * 39 * Since: 1.1.14 40 */ 41 42 G_DEFINE_TYPE(WebKitNetworkResponse, webkit_network_response, G_TYPE_OBJECT); 43 44 struct _WebKitNetworkResponsePrivate { 45 gchar* uri; 46 SoupMessage* message; 47 }; 48 49 #define WEBKIT_NETWORK_RESPONSE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_NETWORK_RESPONSE, WebKitNetworkResponsePrivate)) 50 51 enum { 52 PROP_0, 53 54 PROP_URI, 55 PROP_MESSAGE, 56 }; 57 58 static void webkit_network_response_dispose(GObject* object) 59 { 60 WebKitNetworkResponse* response = WEBKIT_NETWORK_RESPONSE(object); 61 WebKitNetworkResponsePrivate* priv = response->priv; 62 63 if (priv->message) { 64 g_object_unref(priv->message); 65 priv->message = NULL; 66 } 67 68 G_OBJECT_CLASS(webkit_network_response_parent_class)->dispose(object); 69 } 70 71 static void webkit_network_response_finalize(GObject* object) 72 { 73 WebKitNetworkResponse* response = WEBKIT_NETWORK_RESPONSE(object); 74 WebKitNetworkResponsePrivate* priv = response->priv; 75 76 g_free(priv->uri); 77 78 G_OBJECT_CLASS(webkit_network_response_parent_class)->finalize(object); 79 } 80 81 static void webkit_network_response_get_property(GObject* object, guint propertyID, GValue* value, GParamSpec* pspec) 82 { 83 WebKitNetworkResponse* response = WEBKIT_NETWORK_RESPONSE(object); 84 85 switch(propertyID) { 86 case PROP_URI: 87 g_value_set_string(value, webkit_network_response_get_uri(response)); 88 break; 89 case PROP_MESSAGE: 90 g_value_set_object(value, webkit_network_response_get_message(response)); 91 break; 92 default: 93 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyID, pspec); 94 } 95 } 96 97 static void webkit_network_response_set_property(GObject* object, guint propertyID, const GValue* value, GParamSpec* pspec) 98 { 99 WebKitNetworkResponse* response = WEBKIT_NETWORK_RESPONSE(object); 100 WebKitNetworkResponsePrivate* priv = response->priv; 101 102 switch(propertyID) { 103 case PROP_URI: 104 webkit_network_response_set_uri(response, 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 114 static void webkit_network_response_class_init(WebKitNetworkResponseClass* responseClass) 115 { 116 GObjectClass* objectClass = G_OBJECT_CLASS(responseClass); 117 118 objectClass->dispose = webkit_network_response_dispose; 119 objectClass->finalize = webkit_network_response_finalize; 120 objectClass->get_property = webkit_network_response_get_property; 121 objectClass->set_property = webkit_network_response_set_property; 122 123 webkit_init(); 124 125 /** 126 * WebKitNetworkResponse:uri: 127 * 128 * The URI to which the response will be made. 129 * 130 * Since: 1.1.14 131 */ 132 g_object_class_install_property(objectClass, PROP_URI, 133 g_param_spec_string("uri", 134 _("URI"), 135 _("The URI to which the response will be made."), 136 NULL, 137 (GParamFlags)(WEBKIT_PARAM_READWRITE))); 138 139 /** 140 * WebKitNetworkResponse:message: 141 * 142 * The #SoupMessage that backs the response. 143 * 144 * Since: 1.1.14 145 */ 146 g_object_class_install_property(objectClass, PROP_MESSAGE, 147 g_param_spec_object("message", 148 _("Message"), 149 _("The SoupMessage that backs the response."), 150 SOUP_TYPE_MESSAGE, 151 (GParamFlags)(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY))); 152 153 g_type_class_add_private(responseClass, sizeof(WebKitNetworkResponsePrivate)); 154 } 155 156 static void webkit_network_response_init(WebKitNetworkResponse* response) 157 { 158 response->priv = WEBKIT_NETWORK_RESPONSE_GET_PRIVATE(response); 159 } 160 161 // for internal use only 162 WebKitNetworkResponse* webkit_network_response_new_with_core_response(const WebCore::ResourceResponse& resourceResponse) 163 { 164 GOwnPtr<SoupMessage> soupMessage(resourceResponse.toSoupMessage()); 165 if (soupMessage) 166 return WEBKIT_NETWORK_RESPONSE(g_object_new(WEBKIT_TYPE_NETWORK_RESPONSE, "message", soupMessage.get(), NULL)); 167 168 return WEBKIT_NETWORK_RESPONSE(g_object_new(WEBKIT_TYPE_NETWORK_RESPONSE, "uri", resourceResponse.url().string().utf8().data(), NULL)); 169 } 170 171 /** 172 * webkit_network_response_new: 173 * @uri: an URI 174 * 175 * Creates a new #WebKitNetworkResponse initialized with an URI. 176 * 177 * Returns: a new #WebKitNetworkResponse, or %NULL if the URI is 178 * invalid. 179 * 180 * Since: 1.1.14 181 */ 182 WebKitNetworkResponse* webkit_network_response_new(const gchar* uri) 183 { 184 g_return_val_if_fail(uri, NULL); 185 186 return WEBKIT_NETWORK_RESPONSE(g_object_new(WEBKIT_TYPE_NETWORK_RESPONSE, "uri", uri, NULL)); 187 } 188 189 /** 190 * webkit_network_response_set_uri: 191 * @response: a #WebKitNetworkResponse 192 * @uri: an URI 193 * 194 * Sets the URI held and used by the given response. When the response 195 * has an associated #SoupMessage, its URI will also be set by this 196 * call. 197 * 198 * Since: 1.1.14 199 */ 200 void webkit_network_response_set_uri(WebKitNetworkResponse* response, const gchar* uri) 201 { 202 g_return_if_fail(WEBKIT_IS_NETWORK_RESPONSE(response)); 203 g_return_if_fail(uri); 204 205 WebKitNetworkResponsePrivate* priv = response->priv; 206 207 if (priv->uri) 208 g_free(priv->uri); 209 priv->uri = g_strdup(uri); 210 211 if (!priv->message) 212 return; 213 214 SoupURI* soupURI = soup_uri_new(uri); 215 g_return_if_fail(soupURI); 216 217 soup_message_set_uri(priv->message, soupURI); 218 soup_uri_free(soupURI); 219 } 220 221 /** 222 * webkit_network_response_get_uri: 223 * @response: a #WebKitNetworkResponse 224 * 225 * Returns: the uri of the #WebKitNetworkResponse 226 * 227 * Since: 1.1.14 228 */ 229 G_CONST_RETURN gchar* webkit_network_response_get_uri(WebKitNetworkResponse* response) 230 { 231 g_return_val_if_fail(WEBKIT_IS_NETWORK_RESPONSE(response), NULL); 232 233 WebKitNetworkResponsePrivate* priv = response->priv; 234 235 if (priv->uri) 236 return priv->uri; 237 238 SoupURI* soupURI = soup_message_get_uri(priv->message); 239 priv->uri = soup_uri_to_string(soupURI, FALSE); 240 return priv->uri; 241 } 242 243 /** 244 * webkit_network_response_get_soup_message: 245 * @response: a #WebKitNetworkResponse 246 * 247 * Obtains the #SoupMessage that represents the given response. Notice 248 * that only the response side of the HTTP conversation is 249 * represented. 250 * 251 * Returns: the #SoupMessage 252 * Since: 1.1.14 253 */ 254 SoupMessage* webkit_network_response_get_message(WebKitNetworkResponse* response) 255 { 256 g_return_val_if_fail(WEBKIT_IS_NETWORK_RESPONSE(response), NULL); 257 258 WebKitNetworkResponsePrivate* priv = response->priv; 259 260 return priv->message; 261 } 262