| 1 | /* |
| 2 | * Copyright (C) 2012 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 "WebKitURISchemeRequest.h" |
| 22 | |
| 23 | #include "APIData.h" |
| 24 | #include "WebKitPrivate.h" |
| 25 | #include "WebKitURISchemeRequestPrivate.h" |
| 26 | #include "WebKitWebContextPrivate.h" |
| 27 | #include "WebKitWebView.h" |
| 28 | #include "WebPageProxy.h" |
| 29 | #include <WebCore/GUniquePtrSoup.h> |
| 30 | #include <WebCore/ResourceError.h> |
| 31 | #include <WebCore/URLSoup.h> |
| 32 | #include <libsoup/soup.h> |
| 33 | #include <wtf/glib/GRefPtr.h> |
| 34 | #include <wtf/glib/RunLoopSourcePriority.h> |
| 35 | #include <wtf/glib/WTFGType.h> |
| 36 | #include <wtf/text/CString.h> |
| 37 | |
| 38 | using namespace WebKit; |
| 39 | using namespace WebCore; |
| 40 | |
| 41 | /** |
| 42 | * SECTION: WebKitURISchemeRequest |
| 43 | * @Short_description: Represents a URI scheme request |
| 44 | * @Title: WebKitURISchemeRequest |
| 45 | * |
| 46 | * If you register a particular URI scheme in a #WebKitWebContext, |
| 47 | * using webkit_web_context_register_uri_scheme(), you have to provide |
| 48 | * a #WebKitURISchemeRequestCallback. After that, when a URI request |
| 49 | * is made with that particular scheme, your callback will be |
| 50 | * called. There you will be able to access properties such as the |
| 51 | * scheme, the URI and path, and the #WebKitWebView that initiated the |
| 52 | * request, and also finish the request with |
| 53 | * webkit_uri_scheme_request_finish(). |
| 54 | * |
| 55 | */ |
| 56 | |
| 57 | static const unsigned int gReadBufferSize = 8192; |
| 58 | |
| 59 | struct _WebKitURISchemeRequestPrivate { |
| 60 | WebKitWebContext* webContext; |
| 61 | LegacyCustomProtocolManagerProxy* manager; |
| 62 | RefPtr<WebPageProxy> initiatingPage; |
| 63 | uint64_t requestID; |
| 64 | CString uri; |
| 65 | GUniquePtr<SoupURI> soupURI; |
| 66 | |
| 67 | GRefPtr<GInputStream> stream; |
| 68 | uint64_t streamLength; |
| 69 | GRefPtr<GCancellable> cancellable; |
| 70 | char readBuffer[gReadBufferSize]; |
| 71 | uint64_t bytesRead; |
| 72 | CString mimeType; |
| 73 | }; |
| 74 | |
| 75 | WEBKIT_DEFINE_TYPE(WebKitURISchemeRequest, webkit_uri_scheme_request, G_TYPE_OBJECT) |
| 76 | |
| 77 | static void webkit_uri_scheme_request_class_init(WebKitURISchemeRequestClass*) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | WebKitURISchemeRequest* webkitURISchemeRequestCreate(uint64_t requestID, WebKitWebContext* webContext, const ResourceRequest& resourceRequest, LegacyCustomProtocolManagerProxy& manager) |
| 82 | { |
| 83 | WebKitURISchemeRequest* request = WEBKIT_URI_SCHEME_REQUEST(g_object_new(WEBKIT_TYPE_URI_SCHEME_REQUEST, nullptr)); |
| 84 | request->priv->webContext = webContext; |
| 85 | request->priv->manager = &manager; |
| 86 | request->priv->uri = resourceRequest.url().string().utf8(); |
| 87 | request->priv->initiatingPage = WebProcessProxy::webPage(resourceRequest.initiatingPageID()); |
| 88 | request->priv->requestID = requestID; |
| 89 | return request; |
| 90 | } |
| 91 | |
| 92 | void webkitURISchemeRequestCancel(WebKitURISchemeRequest* request) |
| 93 | { |
| 94 | g_cancellable_cancel(request->priv->cancellable.get()); |
| 95 | } |
| 96 | |
| 97 | LegacyCustomProtocolManagerProxy* webkitURISchemeRequestGetManager(WebKitURISchemeRequest* request) |
| 98 | { |
| 99 | return request->priv->manager; |
| 100 | } |
| 101 | |
| 102 | void webkitURISchemeRequestInvalidate(WebKitURISchemeRequest* request) |
| 103 | { |
| 104 | request->priv->manager = nullptr; |
| 105 | webkitURISchemeRequestCancel(request); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * webkit_uri_scheme_request_get_scheme: |
| 110 | * @request: a #WebKitURISchemeRequest |
| 111 | * |
| 112 | * Get the URI scheme of @request |
| 113 | * |
| 114 | * Returns: the URI scheme of @request |
| 115 | */ |
| 116 | const char* webkit_uri_scheme_request_get_scheme(WebKitURISchemeRequest* request) |
| 117 | { |
| 118 | g_return_val_if_fail(WEBKIT_IS_URI_SCHEME_REQUEST(request), 0); |
| 119 | |
| 120 | if (!request->priv->soupURI) |
| 121 | request->priv->soupURI.reset(soup_uri_new(request->priv->uri.data())); |
| 122 | return request->priv->soupURI->scheme; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * webkit_uri_scheme_request_get_uri: |
| 127 | * @request: a #WebKitURISchemeRequest |
| 128 | * |
| 129 | * Get the URI of @request |
| 130 | * |
| 131 | * Returns: the full URI of @request |
| 132 | */ |
| 133 | const char* webkit_uri_scheme_request_get_uri(WebKitURISchemeRequest* request) |
| 134 | { |
| 135 | g_return_val_if_fail(WEBKIT_IS_URI_SCHEME_REQUEST(request), 0); |
| 136 | |
| 137 | return request->priv->uri.data(); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * webkit_uri_scheme_request_get_path: |
| 142 | * @request: a #WebKitURISchemeRequest |
| 143 | * |
| 144 | * Get the URI path of @request |
| 145 | * |
| 146 | * Returns: the URI path of @request |
| 147 | */ |
| 148 | const char* webkit_uri_scheme_request_get_path(WebKitURISchemeRequest* request) |
| 149 | { |
| 150 | g_return_val_if_fail(WEBKIT_IS_URI_SCHEME_REQUEST(request), 0); |
| 151 | |
| 152 | if (!request->priv->soupURI) |
| 153 | request->priv->soupURI.reset(soup_uri_new(request->priv->uri.data())); |
| 154 | return request->priv->soupURI->path; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * webkit_uri_scheme_request_get_web_view: |
| 159 | * @request: a #WebKitURISchemeRequest |
| 160 | * |
| 161 | * Get the #WebKitWebView that initiated the request. |
| 162 | * |
| 163 | * Returns: (transfer none): the #WebKitWebView that initiated @request. |
| 164 | */ |
| 165 | WebKitWebView* webkit_uri_scheme_request_get_web_view(WebKitURISchemeRequest* request) |
| 166 | { |
| 167 | g_return_val_if_fail(WEBKIT_IS_URI_SCHEME_REQUEST(request), 0); |
| 168 | |
| 169 | // FIXME: initiatingPage is now always null, we need to re-implement this somehow. |
| 170 | return request->priv->initiatingPage ? webkitWebContextGetWebViewForPage(request->priv->webContext, request->priv->initiatingPage.get()) : nullptr; |
| 171 | } |
| 172 | |
| 173 | static void webkitURISchemeRequestReadCallback(GInputStream* inputStream, GAsyncResult* result, WebKitURISchemeRequest* schemeRequest) |
| 174 | { |
| 175 | GRefPtr<WebKitURISchemeRequest> request = adoptGRef(schemeRequest); |
| 176 | WebKitURISchemeRequestPrivate* priv = request->priv; |
| 177 | GUniqueOutPtr<GError> error; |
| 178 | gssize bytesRead = g_input_stream_read_finish(inputStream, result, &error.outPtr()); |
| 179 | if (!priv->manager) { |
| 180 | webkitWebContextDidFinishLoadingCustomProtocol(priv->webContext, priv->requestID); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | if (bytesRead == -1) { |
| 185 | webkit_uri_scheme_request_finish_error(request.get(), error.get()); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | // Need to check the stream before proceeding as it can be cancelled if finish_error |
| 190 | // was previously call, which won't be detected by g_input_stream_read_finish(). |
| 191 | if (!request->priv->stream) |
| 192 | return; |
| 193 | |
| 194 | auto webData = IPC::DataReference(reinterpret_cast<const uint8_t*>(priv->readBuffer), bytesRead); |
| 195 | if (!priv->bytesRead) { |
| 196 | // First chunk read. In case of empty reply an empty API::Data is sent to the networking process. |
| 197 | ResourceResponse response(URL(URL(), String::fromUTF8(priv->uri)), String::fromUTF8(priv->mimeType.data()), |
| 198 | priv->streamLength, emptyString()); |
| 199 | priv->manager->didReceiveResponse(priv->requestID, response, 0); |
| 200 | priv->manager->didLoadData(priv->requestID, webData); |
| 201 | } else if (bytesRead || (!bytesRead && !priv->streamLength)) { |
| 202 | // Subsequent chunk read. We only send an empty API::Data to the networking process when stream length is unknown. |
| 203 | priv->manager->didLoadData(priv->requestID, webData); |
| 204 | } |
| 205 | |
| 206 | if (!bytesRead) { |
| 207 | priv->manager->didFinishLoading(priv->requestID); |
| 208 | webkitWebContextDidFinishLoadingCustomProtocol(priv->webContext, priv->requestID); |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | priv->bytesRead += bytesRead; |
| 213 | g_input_stream_read_async(inputStream, priv->readBuffer, gReadBufferSize, RunLoopSourcePriority::AsyncIONetwork, priv->cancellable.get(), |
| 214 | reinterpret_cast<GAsyncReadyCallback>(webkitURISchemeRequestReadCallback), g_object_ref(request.get())); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * webkit_uri_scheme_request_finish: |
| 219 | * @request: a #WebKitURISchemeRequest |
| 220 | * @stream: a #GInputStream to read the contents of the request |
| 221 | * @stream_length: the length of the stream or -1 if not known |
| 222 | * @mime_type: (allow-none): the content type of the stream or %NULL if not known |
| 223 | * |
| 224 | * Finish a #WebKitURISchemeRequest by setting the contents of the request and its mime type. |
| 225 | */ |
| 226 | void webkit_uri_scheme_request_finish(WebKitURISchemeRequest* request, GInputStream* inputStream, gint64 streamLength, const gchar* mimeType) |
| 227 | { |
| 228 | g_return_if_fail(WEBKIT_IS_URI_SCHEME_REQUEST(request)); |
| 229 | g_return_if_fail(G_IS_INPUT_STREAM(inputStream)); |
| 230 | g_return_if_fail(streamLength == -1 || streamLength >= 0); |
| 231 | |
| 232 | request->priv->stream = inputStream; |
| 233 | // We use -1 in the API for consistency with soup when the content length is not known, but 0 internally. |
| 234 | request->priv->streamLength = streamLength == -1 ? 0 : streamLength; |
| 235 | request->priv->cancellable = adoptGRef(g_cancellable_new()); |
| 236 | request->priv->bytesRead = 0; |
| 237 | request->priv->mimeType = mimeType; |
| 238 | g_input_stream_read_async(inputStream, request->priv->readBuffer, gReadBufferSize, RunLoopSourcePriority::AsyncIONetwork, request->priv->cancellable.get(), |
| 239 | reinterpret_cast<GAsyncReadyCallback>(webkitURISchemeRequestReadCallback), g_object_ref(request)); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * webkit_uri_scheme_request_finish_error: |
| 244 | * @request: a #WebKitURISchemeRequest |
| 245 | * @error: a #GError that will be passed to the #WebKitWebView |
| 246 | * |
| 247 | * Finish a #WebKitURISchemeRequest with a #GError. |
| 248 | * |
| 249 | * Since: 2.2 |
| 250 | */ |
| 251 | void webkit_uri_scheme_request_finish_error(WebKitURISchemeRequest* request, GError* error) |
| 252 | { |
| 253 | g_return_if_fail(WEBKIT_IS_URI_SCHEME_REQUEST(request)); |
| 254 | g_return_if_fail(error); |
| 255 | |
| 256 | WebKitURISchemeRequestPrivate* priv = request->priv; |
| 257 | if (!webkitWebContextIsLoadingCustomProtocol(priv->webContext, priv->requestID)) |
| 258 | return; |
| 259 | |
| 260 | priv->stream = nullptr; |
| 261 | ResourceError resourceError(g_quark_to_string(error->domain), toWebCoreError(error->code), soupURIToURL(priv->soupURI.get()), String::fromUTF8(error->message)); |
| 262 | priv->manager->didFailWithError(priv->requestID, resourceError); |
| 263 | webkitWebContextDidFinishLoadingCustomProtocol(priv->webContext, priv->requestID); |
| 264 | } |
| 265 | |