Skip to content

Instantly share code, notes, and snippets.

@aperezdc
Last active March 5, 2023 23:31
Show Gist options
  • Save aperezdc/74809a6cd617faf445c22097a47bcb50 to your computer and use it in GitHub Desktop.
Save aperezdc/74809a6cd617faf445c22097a47bcb50 to your computer and use it in GitHub Desktop.
Minimal WPE launcher
/*
* SPDX-License-Identifier: MIT
*
* cc -o minicog minicog.c $(pkg-config wpe-webkit-1.1 cogcore --cflags --libs)
*/
#include <cog/cog.h>
static const char *s_starturl = NULL;
static void
handle_echo_request(WebKitURISchemeRequest *request, void *userdata)
{
const char *request_uri = webkit_uri_scheme_request_get_uri(request);
g_print("Request URI: %s (%s)\n", request_uri, webkit_uri_scheme_request_get_http_method(request));
g_autoptr(GBytes) data = g_bytes_new(request_uri, strlen(request_uri));
g_autoptr(GInputStream) stream = g_memory_input_stream_new_from_bytes(data);
g_autoptr(SoupMessageHeaders) headers = soup_message_headers_new(SOUP_MESSAGE_HEADERS_RESPONSE);
soup_message_headers_replace(headers, "Access-Control-Allow-Origin", "*");
soup_message_headers_replace(headers, "Access-Control-Allow-Methods", "GET");
g_autoptr(WebKitURISchemeResponse) response = webkit_uri_scheme_response_new(stream, g_bytes_get_size(data));
webkit_uri_scheme_response_set_http_headers(response, g_steal_pointer(&headers));
webkit_uri_scheme_response_set_content_type(response, "text/plain");
webkit_uri_scheme_response_set_status(response, 200, NULL);
webkit_uri_scheme_request_finish_with_response(request, response);
}
static void
configure_web_context(WebKitWebContext *context)
{
webkit_web_context_register_uri_scheme(context, "echo", handle_echo_request, NULL, NULL);
WebKitSecurityManager *manager = webkit_web_context_get_security_manager(context);
webkit_security_manager_register_uri_scheme_as_cors_enabled(manager, "echo");
}
static WebKitWebView*
on_create_view(CogShell *shell, CogPlatform *platform)
{
g_autoptr(GError) error = NULL;
WebKitWebViewBackend *view_backend = cog_platform_get_view_backend(platform, NULL, &error);
if (!view_backend)
g_error("Cannot obtain view backend: %s", error->message);
configure_web_context(cog_shell_get_web_context(shell));
WebKitSettings *settings = cog_shell_get_web_settings(shell);
webkit_settings_set_enable_write_console_messages_to_stdout(settings, TRUE);
g_autoptr(WebKitWebView) web_view =
g_object_new(WEBKIT_TYPE_WEB_VIEW,
"settings", settings,
"web-context", cog_shell_get_web_context(shell),
"backend", view_backend,
NULL);
cog_platform_init_web_view(platform, web_view);
webkit_web_view_load_uri(web_view, s_starturl);
return g_steal_pointer(&web_view);
}
int
main(int argc, char *argv[])
{
g_set_application_name("minicog");
if (argc != 2 && argc != 3) {
g_printerr("Usage: %s [URL [platform]]\n", argv[0]);
return EXIT_FAILURE;
}
g_autoptr(GError) error = NULL;
if (!(s_starturl = cog_uri_guess_from_user_input(argv[1], TRUE, &error)))
g_error("Invalid URL '%s': %s", argv[1], error->message);
cog_modules_add_directory(COG_MODULEDIR);
g_autoptr(GApplication) app = g_application_new(NULL, G_APPLICATION_DEFAULT_FLAGS);
g_autoptr(CogShell) shell = cog_shell_new("minicog", FALSE);
g_autoptr(CogPlatform) platform =
cog_platform_new((argc == 3) ? argv[2] : g_getenv("COG_PLATFORM"), &error);
if (!platform)
g_error("Cannot create platform: %s", error->message);
if (!cog_platform_setup(platform, shell, "", &error))
g_error("Cannot setup platform: %s\n", error->message);
g_signal_connect(shell, "create-view", G_CALLBACK(on_create_view), platform);
g_signal_connect_swapped(app, "shutdown", G_CALLBACK(cog_shell_shutdown), shell);
g_signal_connect_swapped(app, "startup", G_CALLBACK(cog_shell_startup), shell);
g_signal_connect(app, "activate", G_CALLBACK(g_application_hold), NULL);
return g_application_run(app, 1, argv);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>URI Scheme CORS Test</title>
</head>
<body>
<p id="output"></p>
<button id="doit">Do it!</button>
<script type="text/javascript">
document.getElementById("doit").addEventListener("click", async function (event) {
let res = await fetch("echo:/the/answer/is/42");
let el = document.getElementById("output");
el.innerHTML = await res.text();
return false;
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment