libcogcore-sys 0.1.0

FFI bindings to libcogcore (Cog WPE browser engine)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
 * cog-directory-files-handler.c
 * Copyright (C) 2017-2018 Adrian Perez <aperez@igalia.com>
 *
 * SPDX-License-Identifier: MIT
 */

#include "cog-directory-files-handler.h"
#include <gio/gio.h>

/**
 * CogDirectoryFilesHandler:
 *
 * Request handler implementation that loads content from files inside
 * a local directory.
 *
 * By default only the path component of requested URIs is taken into
 * account, and setting [property@Cog.DirectoryFilesHandler:use-host].
 * also uses the URI host component. If a resolved path points to a
 * local directory and it contains a file named `index.html`, it will
 * be used as the response.
 */

struct _CogDirectoryFilesHandler {
    GObject  parent;
    GFile   *base_path;
    gboolean use_host;
    unsigned strip_components;
};

enum {
    PROP_0,
    PROP_BASE_PATH,
    PROP_USE_HOST,
    PROP_STRIP_COMPONENTS,
    N_PROPERTIES,
};

static GParamSpec *s_properties[N_PROPERTIES] = { NULL, };

static const char s_file_query_attributes[] =
    G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
    G_FILE_ATTRIBUTE_STANDARD_SIZE ","
    G_FILE_ATTRIBUTE_STANDARD_TYPE;

static void* s_request_resolving_index = (void*) 0xCAFECAFE;

G_DEFINE_QUARK (cog-directory-files-handler-data, cog_directory_files_handler);


static void
on_file_read_async_completed (GObject      *source_object,
                              GAsyncResult *result,
                              void         *user_data)
{
    g_autoptr(GFile) file = G_FILE (g_object_ref (source_object));
    g_autoptr(WebKitURISchemeRequest) request = WEBKIT_URI_SCHEME_REQUEST (user_data);

    g_autoptr(GError) error = NULL;
    g_autoptr(GFileInputStream) file_stream =
        g_file_read_finish (file, result, &error);

    if (file_stream) {
        g_autoptr(GInputStream) stream =
            g_buffered_input_stream_new (G_INPUT_STREAM (file_stream));
        g_autoptr(GFileInfo) info =
            G_FILE_INFO (g_object_steal_qdata (source_object, cog_directory_files_handler_quark ()));
        guint64 size =
            g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE);
        const char *mime_type =
            g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
        webkit_uri_scheme_request_finish (request, stream, size, mime_type);
    } else {
        /*
         * TODO: Generate a nicer error page.
         */
        g_assert (error);
        webkit_uri_scheme_request_finish_error (request, error);
    }
}


static void
on_file_query_info_async_completed (GObject      *source_object,
                                    GAsyncResult *result,
                                    void         *user_data)
{
    g_autoptr(GFile) file = G_FILE (g_object_ref (source_object));
    g_autoptr(WebKitURISchemeRequest) request = WEBKIT_URI_SCHEME_REQUEST (user_data);

    g_autoptr(GError) error = NULL;
    g_autoptr(GFileInfo) info = g_file_query_info_finish (file, result, &error);

    if (!info) {
        g_assert (error);
        webkit_uri_scheme_request_finish_error (request, error);
        return;
    }

    GFileType type =
        g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_STANDARD_TYPE);

    if (type == G_FILE_TYPE_REGULAR) {
        /*
         * The current file information will be retrieved by 
         * on_file_read_async_completed() to avoid querying again.
         */
        g_object_set_qdata_full (source_object,
                                 cog_directory_files_handler_quark (),
                                 g_steal_pointer (&info),
                                 g_object_unref);
        g_file_read_async (file,
                           G_PRIORITY_DEFAULT,
                           NULL,
                           on_file_read_async_completed,
                           g_steal_pointer (&request));
    } else if (type == G_FILE_TYPE_DIRECTORY) {
        /*
         * If the request has been marked, it means this function is being
         * called after having previously found a directory. In that case,
         * do not try to resolve "index.html" a second time and produce
         * an error instead.
         */
        if (s_request_resolving_index == g_object_get_qdata (G_OBJECT (request),
                                                             cog_directory_files_handler_quark ()))
        {
            g_autofree char *path = g_file_get_path (file);
            error = g_error_new (cog_directory_files_handler_error_quark (),
                                 COG_DIRECTORY_FILES_HANDLER_ERROR_CANNOT_RESOLVE,
                                 "Path '%s' does not represent a regular file",
                                 path);
            webkit_uri_scheme_request_finish_error (request, error);
        } else {
            /* Mark request as being resolved for its index. */
            g_object_set_qdata (G_OBJECT (request),
                                cog_directory_files_handler_quark (),
                                s_request_resolving_index);
            g_autoptr(GFile) index = g_file_get_child (file, "index.html");
            g_file_query_info_async (index,
                                     s_file_query_attributes,
                                     G_FILE_QUERY_INFO_NONE,
                                     G_PRIORITY_DEFAULT,
                                     NULL,
                                     on_file_query_info_async_completed,
                                     g_steal_pointer (&request));
        }
    } else {
        g_autofree char *path = g_file_get_path (file);
        error = g_error_new (cog_directory_files_handler_error_quark (),
                             COG_DIRECTORY_FILES_HANDLER_ERROR_CANNOT_RESOLVE,
                             "Path '%s' does not represent a regular file or directory",
                             path);
        webkit_uri_scheme_request_finish_error (request, error);
    }
}


static void
cog_directory_files_handler_run (CogRequestHandler      *request_handler,
                                 WebKitURISchemeRequest *request)
{
    CogDirectoryFilesHandler *handler = COG_DIRECTORY_FILES_HANDLER(request_handler);

#if COG_USE_SOUP2
    g_autoptr(SoupURI) uri = soup_uri_new(webkit_uri_scheme_request_get_uri(request));
#else
    g_autoptr(GUri) uri = g_uri_parse(webkit_uri_scheme_request_get_uri(request), SOUP_HTTP_URI_FLAGS, NULL);
#endif

    /*
     * If we get an empty path, redirect to the root resource "/", otherwise
     * subresources cannot load properly as there would be no base URI.
     */
#if COG_USE_SOUP2
    const char *path = soup_uri_get_path(uri);
#else
    const char *path = g_uri_get_path(uri);
#endif
    if (path[0] != '/') {
#if COG_USE_SOUP2
        soup_uri_set_path(uri, "/");
        g_autofree char *uri_string = soup_uri_to_string(uri, FALSE);
#else
        g_autoptr(GUri) new_uri = soup_uri_copy(uri, SOUP_URI_PATH, "/", SOUP_URI_NONE);
        g_autofree char *uri_string = g_uri_to_string(new_uri);
#endif
        webkit_web_view_load_uri(webkit_uri_scheme_request_get_web_view(request), uri_string);
        return;
    }

    g_autoptr(GFile) base_path = NULL;
    if (handler->use_host) {
#if COG_USE_SOUP2
        const char *host = soup_uri_get_host(uri);
#else
        const char *host = g_uri_get_host(uri);
#endif
        if (!host || host[0] == '\0') {
#if COG_USE_SOUP2
            g_autofree char *uri_string = soup_uri_to_string(uri, FALSE);
#else
            g_autofree char *uri_string = g_uri_to_string(uri);
#endif
            g_autoptr(GError) error = g_error_new(G_FILE_ERROR, G_FILE_ERROR_INVAL, "No host in URI: %s", uri_string);
            webkit_uri_scheme_request_finish_error (request, error);
            return;
        }
        base_path = g_file_get_child (handler->base_path, host);
    } else {
        base_path = g_object_ref (handler->base_path);
    }

    /*
     * Skip trailing forward slashes: If an absolute child path would
     * be passed to g_file_get_child() it will ignore the base path.
     */
    while (path[0] == '/')
        ++path;

    /*
     * Discard non-empty leading path components, ignoring empty path
     * components (empty component == two consecutive slashes). For
     * example with "strip-components" being 2, paths would get munged
     * as follows:
     *
     *   - /foo/bar/baz  -> /baz
     *   - /foo//bar/baz -> /baz
     *   - /foo/bar/     -> /
     *   - /foo/bar      -> (empty)
     *   - /foo          -> (empty)
     */
    for (unsigned i = 0; i < handler->strip_components && path[0] != '\0'; i++) {
        /* Skip up to the next slash. */
        while (path[0] != '/' && path[0] != '\0')
            ++path;
        /* Skip consecutive slashes. */
        while (path[0] == '/')
            ++path;
    }

    g_autoptr(GFile) file = g_file_get_child (base_path, path);

    /*
     * Check whether the resolved path is contained inside base_path,
     * to prevent URIs with ".." components from accessing resources
     * outside of base_path. The g_file_get_relative_path() method
     * returns NULL when the file path is NOT descendant of base_path,
     * or when both paths are the same.
     */
    if (!g_file_equal (base_path, file)) {
        g_autofree char *relative_path = g_file_get_relative_path (base_path, file);
        if (!relative_path) {
            g_autoptr(GError) error = g_error_new (G_FILE_ERROR,
                                                   G_FILE_ERROR_PERM,
                                                   "Resolved path '%s' not "
                                                   "contained in base path '%s'",
                                                   g_file_peek_path (file),
                                                   g_file_peek_path (base_path));
            return webkit_uri_scheme_request_finish_error (request, error);
        }
    }

    g_file_query_info_async (file,
                             s_file_query_attributes,
                             G_FILE_QUERY_INFO_NONE,
                             G_PRIORITY_DEFAULT,
                             NULL,
                             on_file_query_info_async_completed,
                             g_object_ref (request));
}

static void
cog_directory_files_handler_iface_init (CogRequestHandlerInterface *iface)
{
    iface->run = cog_directory_files_handler_run;
}


G_DEFINE_TYPE_WITH_CODE (CogDirectoryFilesHandler,
                         cog_directory_files_handler,
                         G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (COG_TYPE_REQUEST_HANDLER,
                                                cog_directory_files_handler_iface_init))


static void
cog_directory_files_handler_get_property (GObject    *object,
                                          unsigned    prop_id,
                                          GValue     *value,
                                          GParamSpec *pspec)
{
    CogDirectoryFilesHandler *handler = COG_DIRECTORY_FILES_HANDLER (object);
    switch (prop_id) {
        case PROP_BASE_PATH:
            g_value_set_object (value, handler->base_path);
            break;
        case PROP_USE_HOST:
            g_value_set_boolean (value, cog_directory_files_handler_get_use_host (handler));
            break;
        case PROP_STRIP_COMPONENTS:
            g_value_set_uint (value, cog_directory_files_handler_get_strip_components (handler));
            break;
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}


static void
cog_directory_files_handler_set_property (GObject      *object,
                                          unsigned      prop_id,
                                          const GValue *value,
                                          GParamSpec   *pspec)
{
    CogDirectoryFilesHandler *handler = COG_DIRECTORY_FILES_HANDLER (object);
    switch (prop_id) {
        case PROP_BASE_PATH:
            g_clear_object (&handler->base_path);
            handler->base_path = g_value_dup_object (value);
            break;
        case PROP_USE_HOST:
            cog_directory_files_handler_set_use_host (handler,
                                                      g_value_get_boolean (value));
            break;
        case PROP_STRIP_COMPONENTS:
            cog_directory_files_handler_set_strip_components (handler,
                                                              g_value_get_uint (value));
            break;
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}


static void
cog_directory_files_handler_constructed (GObject *object)
{
    G_OBJECT_CLASS (cog_directory_files_handler_parent_class)->constructed (object);

    CogDirectoryFilesHandler *handler = COG_DIRECTORY_FILES_HANDLER (object);

    g_return_if_fail (cog_directory_files_handler_is_suitable_path (handler->base_path, NULL));
}


static void
cog_directory_files_handler_dispose (GObject *object)
{
    CogDirectoryFilesHandler *handler = COG_DIRECTORY_FILES_HANDLER (object);

    g_clear_object (&handler->base_path);

    G_OBJECT_CLASS (cog_directory_files_handler_parent_class)->dispose (object);
}


static void
cog_directory_files_handler_class_init (CogDirectoryFilesHandlerClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    object_class->get_property = cog_directory_files_handler_get_property;
    object_class->set_property = cog_directory_files_handler_set_property;
    object_class->constructed = cog_directory_files_handler_constructed;
    object_class->dispose = cog_directory_files_handler_dispose;

    /**
     * CogDirectoryFilesHandler:base-path:
     *
     * Base directory where to search for files to “serve” as resources.
     */
    s_properties[PROP_BASE_PATH] =
        g_param_spec_object ("base-path",
                             "Base path",
                             "Path where to load files from",
                             G_TYPE_FILE,
                             G_PARAM_READWRITE |
                             G_PARAM_CONSTRUCT_ONLY |
                             G_PARAM_STATIC_STRINGS);

    /**
     * CogDirectoryFilesHandler:use-host: (attributes org.gtk.Property.get=cog_directory_files_handler_get_use_host org.gtk.Property.set=cog_directory_files_handler_set_use_host):
     *
     * Whether to use the URI host component as the first subdirectory name.
     *
     * For example, given a request for the URI `scheme://host/some/path`:
     *
     * - When enabled, a subdirectory named `host/some/path` will
     *   be searched for inside the `base-path`.
     * - When disabled, a subdirectory named `some/path` will be
     *   searched for inside the `base-path`.
     */
    s_properties[PROP_USE_HOST] =
        g_param_spec_boolean ("use-host",
                              "Use URI host",
                              "Use the host from the URI authority part as top-level subdirectory",
                              FALSE,
                              G_PARAM_READWRITE |
                              G_PARAM_STATIC_STRINGS);

    /**
     * CogDirectoryFilesHandler:strip-components: (attributes org.gtk.Property.get=cog_directory_files_handler_get_strip_components org.gtk.Property.set=cog_directory_files_handler_set_strip_components):
     *
     * Number of leading path components to strip (ignore) at the beginning
     * of request URIs.
     *
     * For example, when set to `2`, a  request for a URI with path
     * `/a/b/c/d.html` will ignore the `/a/b` prefix and search for `c/d.html`.
     */
    s_properties[PROP_STRIP_COMPONENTS] =
        g_param_spec_uint ("strip-components",
                           "Strip path components",
                           "Number of leading URI path components to ignore",
                           0, G_MAXUINT, 0,
                           G_PARAM_READWRITE |
                           G_PARAM_CONSTRUCT |
                           G_PARAM_STATIC_STRINGS);

    g_object_class_install_properties (object_class, N_PROPERTIES, s_properties);
}


static void
cog_directory_files_handler_init (CogDirectoryFilesHandler *handler)
{
}


G_DEFINE_QUARK (CogDirectoryFilesHandlerError, cog_directory_files_handler_error)


gboolean
cog_directory_files_handler_is_suitable_path (GFile   *file,
                                              GError **error)
{
    g_return_val_if_fail (G_IS_FILE (file), FALSE);

    if (!g_file_is_native (file)) {
        g_autofree char *path = error ? g_file_get_path (file) : NULL;
        g_set_error (error,
                     cog_directory_files_handler_error_quark (),
                     COG_DIRECTORY_FILES_HANDLER_ERROR_PATH_NOT_NATIVE,
                     "Path '%s' is not native",
                     path);
        return FALSE;
    }

    if (g_file_query_file_type (file, G_FILE_QUERY_INFO_NONE, NULL) != G_FILE_TYPE_DIRECTORY) {
        g_autofree char *path = error ? g_file_get_path (file) : NULL;
        g_set_error (error,
                     cog_directory_files_handler_error_quark (),
                     COG_DIRECTORY_FILES_HANDLER_ERROR_PATH_NOT_DIRECTORY,
                     "Path '%s' is not a directory",
                     path);
        return FALSE;
    }

    return TRUE;
}

/**
 * cog_directory_files_handler_new: (constructor)
 * @base_path: Base directory where to search for files.
 *
 * Create a new handler.
 *
 * Returns: (transfer full): A directory files handler.
 */
CogRequestHandler*
cog_directory_files_handler_new (GFile *base_path)
{
    g_return_val_if_fail (cog_directory_files_handler_is_suitable_path (base_path, NULL), NULL);
    return g_object_new (COG_TYPE_DIRECTORY_FILES_HANDLER,
                         "base-path", base_path,
                         NULL);
}

/**
 * cog_directory_files_handler_get_use_host:
 *
 * Gets the value of the [property@Cog.DirectoryFilesHandler:use-host]
 * property.
 *
 * Returns: Whether to use the URI host part as a subdirectory name.
 */
gboolean
cog_directory_files_handler_get_use_host (CogDirectoryFilesHandler *self)
{
    g_return_val_if_fail (COG_IS_DIRECTORY_FILES_HANDLER (self), FALSE);
    return self->use_host;
}

/**
 * cog_directory_files_handler_set_use_host:
 * @use_host: Whether to use the URI host part as a subdirectory name.
 *
 * Sets the value of the [property@Cog.DirectoryFilesHandler:use-host]
 * property.
 */
void
cog_directory_files_handler_set_use_host (CogDirectoryFilesHandler *self,
                                          gboolean                  use_host)
{
    g_return_if_fail (COG_IS_DIRECTORY_FILES_HANDLER (self));

    use_host = use_host ? TRUE : FALSE;
    if (self->use_host == use_host)
        return;

    self->use_host = use_host;
    g_object_notify_by_pspec (G_OBJECT (self), s_properties[PROP_USE_HOST]);
}

/**
 * cog_directory_files_handler_get_strip_components:
 * @self: a #CogDirectoryFilesHandler
 *
 * Gets the value of the [property@Cog.DirectoryFilesHandler:strip-components]
 * property.
 *
 * Returns: Number of leading URI path components to ignore.
 */
unsigned
cog_directory_files_handler_get_strip_components (CogDirectoryFilesHandler *self)
{
    g_return_val_if_fail (COG_IS_DIRECTORY_FILES_HANDLER (self), 0);
    return self->strip_components;
}

/**
 * cog_directory_files_handler_set_strip_components:
 * @self: a #CogDirectoryFilesHandler
 * @count: Number of leading URI path components to ignore.
 *
 * Sets the value of the [property@Cog.DirectoryFilesHandler:strip-components]
 * property.
 */
void
cog_directory_files_handler_set_strip_components (CogDirectoryFilesHandler *self,
                                                  unsigned                  count)
{
    g_return_if_fail (COG_IS_DIRECTORY_FILES_HANDLER (self));

    if (self->strip_components == count)
        return;

    self->strip_components = count;
    g_object_notify_by_pspec (G_OBJECT (self), s_properties[PROP_STRIP_COMPONENTS]);
}