Skip to content
Permalink
Browse files
[GTK][WPE] Support setting status code and getting HTTP method in cus…
…tom URI scheme handlers

https://bugs.webkit.org/show_bug.cgi?id=231880

Patch by Zixing Liu <liushuyu011@gmail.com> on 2021-11-02
Reviewed by Carlos Garcia Campos.

Source/WebKit:

* SourcesGTK.txt: Added WebKitURISchemeResponse.cpp
* SourcesWPE.txt: Added WebKitURISchemeResponse.cpp
* UIProcess/API/glib/WebKitURISchemeRequest.cpp:
(webkit_uri_scheme_request_get_http_method): Added new API function
(webkit_uri_request_finish_with_response): Added new API function
* UIProcess/API/glib/WebKitURISchemeRequestPrivate.h:  Added.
* UIProcess/API/glib/WebKitURISchemeResponse.cpp: Added.
(webkit_uri_scheme_response_class_init): Added init function for the new type
(webkit_uri_scheme_response_new): Added new API function
(webkit_uri_scheme_response_set_status): Added new API function
(webkit_uri_scheme_response_set_content_type): Added new API function
* UIProcess/API/glib/WebKitURISchemeResponsePrivate.h: Copied from Source/WebKit/UIProcess/API/glib/WebKitURISchemeRequestPrivate.h.
* UIProcess/API/gtk/WebKitAutocleanups.h: Register clean-up function for WebkitURISchemeResponse
* UIProcess/API/gtk/WebKitURISchemeRequest.h: Added function prototype for
webkit_uri_scheme_request_get_http_method
* UIProcess/API/gtk/WebKitURISchemeResponse.h: Added.
* UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt: Added new symbols and new types
* UIProcess/API/gtk/webkit2.h: Include a new header: WebKitURISchemeResponse.h
* UIProcess/API/wpe/WebKitURISchemeRequest.h: Added function prototype for
webkit_uri_scheme_request_get_http_method
* UIProcess/API/gtk/WebKitURISchemeResponse.h: Added.
* UIProcess/API/wpe/webkit.h: Include a new header: WebKitURISchemeResponse.h
* UIProcess/API/wpe/docs/wpe-1.0-sections.txt: Added new symbols and new types

Tools:

* TestWebKitAPI/Tests/WebKitGLib/TestWebKitWebContext.cpp:
(testWebContextURIScheme): Added tests to test new public APIs for WebKitGtk


Canonical link: https://commits.webkit.org/243791@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@285155 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
liushuyu authored and webkit-commit-queue committed Nov 2, 2021
1 parent 306fd25 commit 882ca4b40ae8a0a5d64b50a2115491b08bb28c1c
Showing 23 changed files with 617 additions and 21 deletions.
@@ -1,3 +1,34 @@
2021-11-02 Zixing Liu <liushuyu011@gmail.com>

[GTK][WPE] Support setting status code and getting HTTP method in custom URI scheme handlers
https://bugs.webkit.org/show_bug.cgi?id=231880

Reviewed by Carlos Garcia Campos.

* SourcesGTK.txt: Added WebKitURISchemeResponse.cpp
* SourcesWPE.txt: Added WebKitURISchemeResponse.cpp
* UIProcess/API/glib/WebKitURISchemeRequest.cpp:
(webkit_uri_scheme_request_get_http_method): Added new API function
(webkit_uri_request_finish_with_response): Added new API function
* UIProcess/API/glib/WebKitURISchemeRequestPrivate.h: Added.
* UIProcess/API/glib/WebKitURISchemeResponse.cpp: Added.
(webkit_uri_scheme_response_class_init): Added init function for the new type
(webkit_uri_scheme_response_new): Added new API function
(webkit_uri_scheme_response_set_status): Added new API function
(webkit_uri_scheme_response_set_content_type): Added new API function
* UIProcess/API/glib/WebKitURISchemeResponsePrivate.h: Copied from Source/WebKit/UIProcess/API/glib/WebKitURISchemeRequestPrivate.h.
* UIProcess/API/gtk/WebKitAutocleanups.h: Register clean-up function for WebkitURISchemeResponse
* UIProcess/API/gtk/WebKitURISchemeRequest.h: Added function prototype for
webkit_uri_scheme_request_get_http_method
* UIProcess/API/gtk/WebKitURISchemeResponse.h: Added.
* UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt: Added new symbols and new types
* UIProcess/API/gtk/webkit2.h: Include a new header: WebKitURISchemeResponse.h
* UIProcess/API/wpe/WebKitURISchemeRequest.h: Added function prototype for
webkit_uri_scheme_request_get_http_method
* UIProcess/API/gtk/WebKitURISchemeResponse.h: Added.
* UIProcess/API/wpe/webkit.h: Include a new header: WebKitURISchemeResponse.h
* UIProcess/API/wpe/docs/wpe-1.0-sections.txt: Added new symbols and new types

2021-11-01 Michael[tm] Smith <mike@w3.org>

[WebInspector][CORS] Show HTTP status code in CORS messages.
@@ -146,6 +146,7 @@ set(WebKit2GTK_INSTALLED_HEADERS
${WEBKIT_DIR}/UIProcess/API/gtk/WebKitURIRequest.h
${WEBKIT_DIR}/UIProcess/API/gtk/WebKitURIResponse.h
${WEBKIT_DIR}/UIProcess/API/gtk/WebKitURISchemeRequest.h
${WEBKIT_DIR}/UIProcess/API/gtk/WebKitURISchemeResponse.h
${WEBKIT_DIR}/UIProcess/API/gtk/WebKitURIUtilities.h
${WEBKIT_DIR}/UIProcess/API/gtk/WebKitUserContent.h
${WEBKIT_DIR}/UIProcess/API/gtk/WebKitUserContentFilterStore.h
@@ -176,6 +176,7 @@ set(WPE_API_INSTALLED_HEADERS
${WEBKIT_DIR}/UIProcess/API/wpe/WebKitURIRequest.h
${WEBKIT_DIR}/UIProcess/API/wpe/WebKitURIResponse.h
${WEBKIT_DIR}/UIProcess/API/wpe/WebKitURISchemeRequest.h
${WEBKIT_DIR}/UIProcess/API/wpe/WebKitURISchemeResponse.h
${WEBKIT_DIR}/UIProcess/API/wpe/WebKitURIUtilities.h
${WEBKIT_DIR}/UIProcess/API/wpe/WebKitUserContent.h
${WEBKIT_DIR}/UIProcess/API/wpe/WebKitUserContentFilterStore.h
@@ -173,6 +173,7 @@ UIProcess/API/glib/WebKitSecurityOrigin.cpp @no-unify
UIProcess/API/glib/WebKitSettings.cpp @no-unify
UIProcess/API/glib/WebKitUIClient.cpp @no-unify
UIProcess/API/glib/WebKitURISchemeRequest.cpp @no-unify
UIProcess/API/glib/WebKitURISchemeResponse.cpp @no-unify
UIProcess/API/glib/WebKitURIUtilities.cpp @no-unify
UIProcess/API/glib/WebKitUserContent.cpp @no-unify
UIProcess/API/glib/WebKitUserContentFilterStore.cpp @no-unify
@@ -164,6 +164,7 @@ UIProcess/API/glib/WebKitSecurityOrigin.cpp @no-unify
UIProcess/API/glib/WebKitSettings.cpp @no-unify
UIProcess/API/glib/WebKitUIClient.cpp @no-unify
UIProcess/API/glib/WebKitURISchemeRequest.cpp @no-unify
UIProcess/API/glib/WebKitURISchemeResponse.cpp @no-unify
UIProcess/API/glib/WebKitURIUtilities.cpp @no-unify
UIProcess/API/glib/WebKitUserContent.cpp @no-unify
UIProcess/API/glib/WebKitUserContentFilterStore.cpp @no-unify
@@ -23,6 +23,7 @@
#include "APIData.h"
#include "WebKitPrivate.h"
#include "WebKitURISchemeRequestPrivate.h"
#include "WebKitURISchemeResponsePrivate.h"
#include "WebKitWebContextPrivate.h"
#include "WebKitWebView.h"
#include "WebPageProxy.h"
@@ -64,12 +65,11 @@ struct _WebKitURISchemeRequestPrivate {
CString uriScheme;
CString uriPath;

GRefPtr<GInputStream> stream;
uint64_t streamLength;
GRefPtr<WebKitURISchemeResponse> response;
GRefPtr<GCancellable> cancellable;
char readBuffer[gReadBufferSize];
uint64_t bytesRead;
CString contentType;
const char* httpMethod;
};

WEBKIT_DEFINE_TYPE(WebKitURISchemeRequest, webkit_uri_scheme_request, G_TYPE_OBJECT)
@@ -161,6 +161,26 @@ WebKitWebView* webkit_uri_scheme_request_get_web_view(WebKitURISchemeRequest* re
return webkitWebContextGetWebViewForPage(request->priv->webContext, request->priv->initiatingPage.get());
}

/**
* webkit_uri_scheme_request_get_http_method:
* @request: a #WebKitURISchemeRequest
*
* Get the HTTP method of the @request
*
* Returns: the HTTP method of the @request
*
* Since: 2.36
*/
const gchar* webkit_uri_scheme_request_get_http_method(WebKitURISchemeRequest* request)
{
g_return_val_if_fail(WEBKIT_IS_URI_SCHEME_REQUEST(request), nullptr);

if (!request->priv->httpMethod)
request->priv->httpMethod = g_intern_string(request->priv->task->request().httpMethod().utf8().data());

return request->priv->httpMethod;
}

static void webkitURISchemeRequestReadCallback(GInputStream* inputStream, GAsyncResult* result, WebKitURISchemeRequest* schemeRequest)
{
GRefPtr<WebKitURISchemeRequest> request = adoptGRef(schemeRequest);
@@ -174,20 +194,29 @@ static void webkitURISchemeRequestReadCallback(GInputStream* inputStream, GAsync
WebKitURISchemeRequestPrivate* priv = request->priv;
// Need to check the stream before proceeding as it can be cancelled if finish_error
// was previously call, which won't be detected by g_input_stream_read_finish().
if (!priv->stream)
if (!priv->response)
return;

WebKitURISchemeResponse* resp = priv->response.get();
if (!priv->bytesRead) {
ResourceResponse response(priv->task->request().url(), extractMIMETypeFromMediaType(priv->contentType.data()), priv->streamLength, emptyString());
response.setTextEncodingName(extractCharsetFromMediaType(priv->contentType.data()));
response.setHTTPStatusCode(200);
response.setHTTPStatusText("OK"_s);
CString contentType = WebKitURISchemeResponseGetContentType(resp);
ResourceResponse response(priv->task->request().url(), extractMIMETypeFromMediaType(contentType.data()), WebKitURISchemeResponseGetStreamLength(resp), emptyString());
response.setTextEncodingName(extractCharsetFromMediaType(contentType.data()));
const CString& statusMessage = WebKitURISchemeResponseGetStatusMessage(resp);
if (statusMessage.isNull()) {
response.setHTTPStatusCode(200);
response.setHTTPStatusText("OK"_s);
} else {
response.setHTTPStatusCode(WebKitURISchemeResponseGetStatusCode(resp));
response.setHTTPStatusText(statusMessage.data());
}
if (response.mimeType().isEmpty())
response.setMimeType(MIMETypeRegistry::mimeTypeForPath(response.url().path().toString()));
priv->task->didReceiveResponse(response);
}

if (!bytesRead) {
priv->response = nullptr;
priv->task->didComplete({ });
return;
}
@@ -213,13 +242,31 @@ void webkit_uri_scheme_request_finish(WebKitURISchemeRequest* request, GInputStr
g_return_if_fail(G_IS_INPUT_STREAM(inputStream));
g_return_if_fail(streamLength == -1 || streamLength >= 0);

request->priv->stream = inputStream;
// We use -1 in the API for consistency with soup when the content length is not known, but 0 internally.
request->priv->streamLength = streamLength == -1 ? 0 : streamLength;
GRefPtr<WebKitURISchemeResponse> response = adoptGRef(webkit_uri_scheme_response_new(inputStream, streamLength));
if (contentType)
webkit_uri_scheme_response_set_content_type(response.get(), contentType);

webkit_uri_scheme_request_finish_with_response(request, response.get());
}

/**
* webkit_uri_scheme_request_finish_with_response:
* @request: a #WebKitURISchemeRequest
* @response: a #WebKitURISchemeResponse
*
* Finish a #WebKitURISchemeRequest by returning a #WebKitURISchemeResponse
*
* Since: 2.36
*/
void webkit_uri_scheme_request_finish_with_response(WebKitURISchemeRequest* request, WebKitURISchemeResponse* response)
{
g_return_if_fail(WEBKIT_IS_URI_SCHEME_REQUEST(request));
g_return_if_fail(WEBKIT_IS_URI_SCHEME_RESPONSE(response));

request->priv->cancellable = adoptGRef(g_cancellable_new());
request->priv->bytesRead = 0;
request->priv->contentType = contentType;
g_input_stream_read_async(inputStream, request->priv->readBuffer, gReadBufferSize, RunLoopSourcePriority::AsyncIONetwork, request->priv->cancellable.get(),
request->priv->response = response;

g_input_stream_read_async(WebKitURISchemeResponseGetStream(response), request->priv->readBuffer, gReadBufferSize, RunLoopSourcePriority::AsyncIONetwork, request->priv->cancellable.get(),
reinterpret_cast<GAsyncReadyCallback>(webkitURISchemeRequestReadCallback), g_object_ref(request));
}

@@ -238,7 +285,7 @@ void webkit_uri_scheme_request_finish_error(WebKitURISchemeRequest* request, GEr
g_return_if_fail(error);

WebKitURISchemeRequestPrivate* priv = request->priv;
priv->stream = nullptr;
priv->response = nullptr;
ResourceError resourceError(g_quark_to_string(error->domain), toWebCoreError(error->code), priv->task->request().url(), String::fromUTF8(error->message));
priv->task->didComplete(resourceError);
}

0 comments on commit 882ca4b

Please sign in to comment.