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 "WebKitURIRequestPrivate.h" |
24 | #include <WebCore/GUniquePtrSoup.h> |
25 | #include <glib/gi18n-lib.h> |
26 | #include <wtf/glib/WTFGType.h> |
27 | #include <wtf/text/CString.h> |
28 | |
29 | enum { |
30 | PROP_0, |
31 | |
32 | PROP_URI |
33 | }; |
34 | |
35 | using 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 | |
48 | struct _WebKitURIRequestPrivate { |
49 | WebCore::ResourceRequest resourceRequest; |
50 | CString uri; |
51 | const char* httpMethod; |
52 | GUniquePtr<SoupMessageHeaders> ; |
53 | }; |
54 | |
55 | WEBKIT_DEFINE_TYPE(WebKitURIRequest, webkit_uri_request, G_TYPE_OBJECT) |
56 | |
57 | static void webkitURIRequestGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec) |
58 | { |
59 | WebKitURIRequest* request = WEBKIT_URI_REQUEST(object); |
60 | |
61 | switch (propId) { |
62 | case PROP_URI: |
63 | g_value_set_string(value, webkit_uri_request_get_uri(request)); |
64 | break; |
65 | default: |
66 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec); |
67 | } |
68 | } |
69 | |
70 | static void webkitURIRequestSetProperty(GObject* object, guint propId, const GValue* value, GParamSpec* paramSpec) |
71 | { |
72 | WebKitURIRequest* request = WEBKIT_URI_REQUEST(object); |
73 | |
74 | switch (propId) { |
75 | case PROP_URI: |
76 | webkit_uri_request_set_uri(request, g_value_get_string(value)); |
77 | break; |
78 | default: |
79 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec); |
80 | } |
81 | } |
82 | |
83 | static void webkit_uri_request_class_init(WebKitURIRequestClass* requestClass) |
84 | { |
85 | GObjectClass* objectClass = G_OBJECT_CLASS(requestClass); |
86 | objectClass->get_property = webkitURIRequestGetProperty; |
87 | objectClass->set_property = webkitURIRequestSetProperty; |
88 | |
89 | /** |
90 | * WebKitURIRequest:uri: |
91 | * |
92 | * The URI to which the request will be made. |
93 | */ |
94 | g_object_class_install_property(objectClass, PROP_URI, |
95 | g_param_spec_string("uri" , |
96 | _("URI" ), |
97 | _("The URI to which the request will be made." ), |
98 | "about:blank" , |
99 | static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT))); |
100 | } |
101 | |
102 | /** |
103 | * webkit_uri_request_new: |
104 | * @uri: an URI |
105 | * |
106 | * Creates a new #WebKitURIRequest for the given URI. |
107 | * |
108 | * Returns: a new #WebKitURIRequest |
109 | */ |
110 | WebKitURIRequest* webkit_uri_request_new(const gchar* uri) |
111 | { |
112 | g_return_val_if_fail(uri, 0); |
113 | |
114 | return WEBKIT_URI_REQUEST(g_object_new(WEBKIT_TYPE_URI_REQUEST, "uri" , uri, NULL)); |
115 | } |
116 | |
117 | /** |
118 | * webkit_uri_request_get_uri: |
119 | * @request: a #WebKitURIRequest |
120 | * |
121 | * Returns: the uri of the #WebKitURIRequest |
122 | */ |
123 | const gchar* webkit_uri_request_get_uri(WebKitURIRequest* request) |
124 | { |
125 | g_return_val_if_fail(WEBKIT_IS_URI_REQUEST(request), 0); |
126 | |
127 | request->priv->uri = request->priv->resourceRequest.url().string().utf8(); |
128 | return request->priv->uri.data(); |
129 | } |
130 | |
131 | /** |
132 | * webkit_uri_request_set_uri: |
133 | * @request: a #WebKitURIRequest |
134 | * @uri: an URI |
135 | * |
136 | * Set the URI of @request |
137 | */ |
138 | void webkit_uri_request_set_uri(WebKitURIRequest* request, const char* uri) |
139 | { |
140 | g_return_if_fail(WEBKIT_IS_URI_REQUEST(request)); |
141 | g_return_if_fail(uri); |
142 | |
143 | URL url(URL(), uri); |
144 | if (url == request->priv->resourceRequest.url()) |
145 | return; |
146 | |
147 | request->priv->resourceRequest.setURL(url); |
148 | g_object_notify(G_OBJECT(request), "uri" ); |
149 | } |
150 | |
151 | /** |
152 | * webkit_uri_request_get_http_headers: |
153 | * @request: a #WebKitURIRequest |
154 | * |
155 | * Get the HTTP headers of a #WebKitURIRequest as a #SoupMessageHeaders. |
156 | * |
157 | * Returns: (transfer none): a #SoupMessageHeaders with the HTTP headers of @request |
158 | * or %NULL if @request is not an HTTP request. |
159 | */ |
160 | SoupMessageHeaders* (WebKitURIRequest* request) |
161 | { |
162 | g_return_val_if_fail(WEBKIT_IS_URI_REQUEST(request), 0); |
163 | |
164 | if (request->priv->httpHeaders) |
165 | return request->priv->httpHeaders.get(); |
166 | |
167 | if (!request->priv->resourceRequest.url().protocolIsInHTTPFamily()) |
168 | return 0; |
169 | |
170 | request->priv->httpHeaders.reset(soup_message_headers_new(SOUP_MESSAGE_HEADERS_REQUEST)); |
171 | request->priv->resourceRequest.updateSoupMessageHeaders(request->priv->httpHeaders.get()); |
172 | return request->priv->httpHeaders.get(); |
173 | } |
174 | |
175 | /** |
176 | * webkit_uri_request_get_http_method: |
177 | * @request: a #WebKitURIRequest |
178 | * |
179 | * Get the HTTP method of the #WebKitURIRequest. |
180 | * |
181 | * Returns: the HTTP method of the #WebKitURIRequest or %NULL if @request is not |
182 | * an HTTP request. |
183 | * |
184 | * Since: 2.12 |
185 | */ |
186 | const gchar* webkit_uri_request_get_http_method(WebKitURIRequest* request) |
187 | { |
188 | g_return_val_if_fail(WEBKIT_IS_URI_REQUEST(request), nullptr); |
189 | |
190 | if (!request->priv->resourceRequest.url().protocolIsInHTTPFamily()) |
191 | return nullptr; |
192 | |
193 | if (request->priv->resourceRequest.httpMethod().isEmpty()) |
194 | return nullptr; |
195 | |
196 | if (!request->priv->httpMethod) |
197 | request->priv->httpMethod = g_intern_string(request->priv->resourceRequest.httpMethod().utf8().data()); |
198 | return request->priv->httpMethod; |
199 | } |
200 | |
201 | WebKitURIRequest* webkitURIRequestCreateForResourceRequest(const ResourceRequest& resourceRequest) |
202 | { |
203 | WebKitURIRequest* uriRequest = WEBKIT_URI_REQUEST(g_object_new(WEBKIT_TYPE_URI_REQUEST, NULL)); |
204 | uriRequest->priv->resourceRequest = resourceRequest; |
205 | return uriRequest; |
206 | } |
207 | |
208 | void webkitURIRequestGetResourceRequest(WebKitURIRequest* request, ResourceRequest& resourceRequest) |
209 | { |
210 | resourceRequest = request->priv->resourceRequest; |
211 | if (request->priv->httpHeaders) |
212 | resourceRequest.updateFromSoupMessageHeaders(request->priv->httpHeaders.get()); |
213 | } |
214 | |