1/* 2 * Copyright (C) 2011 Igalia S.L. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 */ 19 20#include "config.h" 21#include "WebKitURIRequest.h" 22 23#include "WebKitPrivate.h" 24#include "WebKitURIRequestPrivate.h" 25#include <WebCore/GUniquePtrSoup.h> 26#include <glib/gi18n-lib.h> 27#include <wtf/text/CString.h> 28 29enum { 30 PROP_0, 31 32 PROP_URI 33}; 34 35using namespace WebCore; 36 37/** 38 * SECTION: WebKitURIRequest 39 * @Short_description: Represents a URI request 40 * @Title: WebKitURIRequest 41 * 42 * A #WebKitURIRequest can be created with a URI using the 43 * webkit_uri_request_new() method, and you can get the URI of an 44 * existing request with the webkit_uri_request_get_uri() one. 45 * 46 */ 47 48struct _WebKitURIRequestPrivate { 49 WebCore::ResourceRequest resourceRequest; 50 CString uri; 51 GUniquePtr<SoupMessageHeaders> httpHeaders; 52}; 53 54WEBKIT_DEFINE_TYPE(WebKitURIRequest, webkit_uri_request, G_TYPE_OBJECT) 55 56static void webkitURIRequestGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec) 57{ 58 WebKitURIRequest* request = WEBKIT_URI_REQUEST(object); 59 60 switch (propId) { 61 case PROP_URI: 62 g_value_set_string(value, webkit_uri_request_get_uri(request)); 63 break; 64 default: 65 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec); 66 } 67} 68 69static void webkitURIRequestSetProperty(GObject* object, guint propId, const GValue* value, GParamSpec* paramSpec) 70{ 71 WebKitURIRequest* request = WEBKIT_URI_REQUEST(object); 72 73 switch (propId) { 74 case PROP_URI: 75 webkit_uri_request_set_uri(request, g_value_get_string(value)); 76 break; 77 default: 78 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec); 79 } 80} 81 82static void webkit_uri_request_class_init(WebKitURIRequestClass* requestClass) 83{ 84 GObjectClass* objectClass = G_OBJECT_CLASS(requestClass); 85 objectClass->get_property = webkitURIRequestGetProperty; 86 objectClass->set_property = webkitURIRequestSetProperty; 87 88 /** 89 * WebKitURIRequest:uri: 90 * 91 * The URI to which the request will be made. 92 */ 93 g_object_class_install_property(objectClass, PROP_URI, 94 g_param_spec_string("uri", 95 _("URI"), 96 _("The URI to which the request will be made."), 97 "about:blank", 98 static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT))); 99} 100 101/** 102 * webkit_uri_request_new: 103 * @uri: an URI 104 * 105 * Creates a new #WebKitURIRequest for the given URI. 106 * 107 * Returns: a new #WebKitURIRequest 108 */ 109WebKitURIRequest* webkit_uri_request_new(const gchar* uri) 110{ 111 g_return_val_if_fail(uri, 0); 112 113 return WEBKIT_URI_REQUEST(g_object_new(WEBKIT_TYPE_URI_REQUEST, "uri", uri, NULL)); 114} 115 116/** 117 * webkit_uri_request_get_uri: 118 * @request: a #WebKitURIRequest 119 * 120 * Returns: the uri of the #WebKitURIRequest 121 */ 122const gchar* webkit_uri_request_get_uri(WebKitURIRequest* request) 123{ 124 g_return_val_if_fail(WEBKIT_IS_URI_REQUEST(request), 0); 125 126 request->priv->uri = request->priv->resourceRequest.url().string().utf8(); 127 return request->priv->uri.data(); 128} 129 130/** 131 * webkit_uri_request_set_uri: 132 * @request: a #WebKitURIRequest 133 * @uri: an URI 134 * 135 * Set the URI of @request 136 */ 137void webkit_uri_request_set_uri(WebKitURIRequest* request, const char* uri) 138{ 139 g_return_if_fail(WEBKIT_IS_URI_REQUEST(request)); 140 g_return_if_fail(uri); 141 142 URL url(URL(), uri); 143 if (url == request->priv->resourceRequest.url()) 144 return; 145 146 request->priv->resourceRequest.setURL(url); 147 g_object_notify(G_OBJECT(request), "uri"); 148} 149 150/** 151 * webkit_uri_request_get_http_headers: 152 * @request: a #WebKitURIRequest 153 * 154 * Get the HTTP headers of a #WebKitURIRequest as a #SoupMessageHeaders. 155 * 156 * Returns: (transfer none): a #SoupMessageHeaders with the HTTP headers of @request 157 * or %NULL if @request is not an HTTP request. 158 */ 159SoupMessageHeaders* webkit_uri_request_get_http_headers(WebKitURIRequest* request) 160{ 161 g_return_val_if_fail(WEBKIT_IS_URI_REQUEST(request), 0); 162 163 if (request->priv->httpHeaders) 164 return request->priv->httpHeaders.get(); 165 166 if (!request->priv->resourceRequest.url().protocolIsInHTTPFamily()) 167 return 0; 168 169 request->priv->httpHeaders.reset(soup_message_headers_new(SOUP_MESSAGE_HEADERS_REQUEST)); 170 request->priv->resourceRequest.updateSoupMessageHeaders(request->priv->httpHeaders.get()); 171 return request->priv->httpHeaders.get(); 172} 173 174WebKitURIRequest* webkitURIRequestCreateForResourceRequest(const ResourceRequest& resourceRequest) 175{ 176 WebKitURIRequest* uriRequest = WEBKIT_URI_REQUEST(g_object_new(WEBKIT_TYPE_URI_REQUEST, NULL)); 177 uriRequest->priv->resourceRequest = resourceRequest; 178 return uriRequest; 179} 180 181void webkitURIRequestGetResourceRequest(WebKitURIRequest* request, ResourceRequest& resourceRequest) 182{ 183 resourceRequest = request->priv->resourceRequest; 184 if (request->priv->httpHeaders) 185 resourceRequest.updateFromSoupMessageHeaders(request->priv->httpHeaders.get()); 186} 187