From e3e3ffc67c4f8f5f4d1214b13a9dfc13b013612b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 12 Jan 2021 14:45:04 +0100 Subject: [PATCH 1/5] selection: URI decoder: break out decoding of a single URI --- selection.c | 56 +++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/selection.c b/selection.c index e922db53..03ee85da 100644 --- a/selection.c +++ b/selection.c @@ -1479,6 +1479,34 @@ fdm_receive_decoder_plain(struct clipboard_receive *ctx, char *data, size_t size ctx->cb(data, size, ctx->user); } +static bool +decode_one_uri(struct clipboard_receive *ctx, char *uri, size_t len) +{ + LOG_DBG("URI: \"%.*s\"", (int)len, uri); + + char *scheme, *host, *path; + if (!uri_parse(uri, len, &scheme, NULL, NULL, &host, NULL, &path, NULL, NULL)) { + LOG_ERR("drag-and-drop: invalid URI: %.*s", (int)len, uri); + return false; + } + + if (ctx->add_space) + ctx->cb(" ", 1, ctx->user); + ctx->add_space = true; + + if (strcmp(scheme, "file") == 0 && hostname_is_localhost(host)) { + ctx->cb("'", 1, ctx->user); + ctx->cb(path, strlen(path), ctx->user); + ctx->cb("'", 1, ctx->user); + } else + ctx->cb(uri, len, ctx->user); + + free(scheme); + free(host); + free(path); + return true; +} + static void fdm_receive_decoder_uri(struct clipboard_receive *ctx, char *data, size_t size) { @@ -1495,34 +1523,8 @@ fdm_receive_decoder_uri(struct clipboard_receive *ctx, char *data, size_t size) char *end = NULL; while ((end = memchr(start, '\n', ctx->buf.idx - (start - ctx->buf.data))) != NULL) { - const size_t len = end - start; - - LOG_DBG("URI: \"%.*s\"", (int)len, start); - - char *scheme, *host, *path; - if (!uri_parse(start, len, &scheme, NULL, NULL, &host, NULL, &path, NULL, NULL)) { - LOG_ERR("drag-and-drop: invalid URI: %.*s", (int)len, start); - start = end + 1; - continue; - } - - if (ctx->add_space) - ctx->cb(" ", 1, ctx->user); - ctx->add_space = true; - - - if (strcmp(scheme, "file") == 0 && hostname_is_localhost(host)) { - ctx->cb("'", 1, ctx->user); - ctx->cb(path, strlen(path), ctx->user); - ctx->cb("'", 1, ctx->user); - } else - ctx->cb(start, len, ctx->user); - + decode_one_uri(ctx, start, end - start); start = end + 1; - - free(scheme); - free(host); - free(path); } const size_t ofs = start - ctx->buf.data; From c8bcce83d5499ef8e565b59ddcc9803919a3b371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 12 Jan 2021 14:45:41 +0100 Subject: [PATCH 2/5] =?UTF-8?q?selection:=20add=20a=20=E2=80=98finish?= =?UTF-8?q?=E2=80=99=20function,=20called=20at=20the=20end=20of=20receivin?= =?UTF-8?q?g=20clipboard=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is necessary to decode the final URI in a text/uri-list offer if it hasn’t been newline terminated. --- selection.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/selection.c b/selection.c index 03ee85da..b9b086a8 100644 --- a/selection.c +++ b/selection.c @@ -1422,6 +1422,7 @@ struct clipboard_receive { struct itimerspec timeout; void (*decoder)(struct clipboard_receive *ctx, char *data, size_t size); + void (*finish)(struct clipboard_receive *ctx); /* URI state */ bool add_space; @@ -1479,6 +1480,11 @@ fdm_receive_decoder_plain(struct clipboard_receive *ctx, char *data, size_t size ctx->cb(data, size, ctx->user); } +static void +fdm_receive_finish_plain(struct clipboard_receive *ctx) +{ +} + static bool decode_one_uri(struct clipboard_receive *ctx, char *uri, size_t len) { @@ -1534,6 +1540,14 @@ fdm_receive_decoder_uri(struct clipboard_receive *ctx, char *data, size_t size) ctx->buf.idx = left; } +static void +fdm_receive_finish_uri(struct clipboard_receive *ctx) +{ + LOG_DBG("finish: %.*s", (int)ctx->buf.idx, ctx->buf.data); + if (ctx->buf.idx > 0) + decode_one_uri(ctx, ctx->buf.data, ctx->buf.idx); +} + static bool fdm_receive(struct fdm *fdm, int fd, int events, void *data) { @@ -1584,6 +1598,7 @@ fdm_receive(struct fdm *fdm, int fd, int events, void *data) } done: + ctx->finish(ctx); clipboard_receive_done(fdm, ctx); return true; } @@ -1625,6 +1640,9 @@ begin_receive_clipboard(struct terminal *term, int read_fd, .decoder = (mime_type == DATA_OFFER_MIME_URI_LIST ? &fdm_receive_decoder_uri : &fdm_receive_decoder_plain), + .finish = (mime_type == DATA_OFFER_MIME_URI_LIST + ? &fdm_receive_finish_uri + : &fdm_receive_finish_plain), .cb = cb, .done = done, .user = user, @@ -1666,6 +1684,9 @@ text_from_clipboard(struct seat *seat, struct terminal *term, return; } + LOG_DBG("receive from clipboard: mime-type=%s", + mime_type_map[clipboard->mime_type]); + int read_fd = fds[0]; int write_fd = fds[1]; @@ -1810,6 +1831,9 @@ text_from_primary( return; } + LOG_DBG("receive from primary: mime-type=%s", + mime_type_map[primary->mime_type]); + int read_fd = fds[0]; int write_fd = fds[1]; From d968bcd506cb9a185b2569169db8e9dc459c64c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 12 Jan 2021 14:48:20 +0100 Subject: [PATCH 3/5] changelog: pasting non-newline terminated text/uri-list offers --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48655ebe..552d2fe2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,8 @@ (https://codeberg.org/dnkl/foot/issues/270) * Combining characters not being rendered when composed with colored bitmap glyphs (i.e. colored emojis). +* Pasting URIs from the clipboard when the source has not newline + terminated the last URI (https://codeberg.org/dnkl/foot/issues/291). ### Security From 497b8e6c0a21a333b37e5fe6903bd0308fc5cc31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 12 Jan 2021 14:55:21 +0100 Subject: [PATCH 4/5] =?UTF-8?q?changelog:=20add=20hyphen=20between=20?= =?UTF-8?q?=E2=80=98newline=E2=80=99=20and=20=E2=80=98terminated=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 552d2fe2..592ad2ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,8 +64,9 @@ (https://codeberg.org/dnkl/foot/issues/270) * Combining characters not being rendered when composed with colored bitmap glyphs (i.e. colored emojis). -* Pasting URIs from the clipboard when the source has not newline - terminated the last URI (https://codeberg.org/dnkl/foot/issues/291). +* Pasting URIs from the clipboard when the source has not + newline-terminated the last URI + (https://codeberg.org/dnkl/foot/issues/291). ### Security From 3be80622efacd303b68482ef84aa426b79452185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 12 Jan 2021 14:56:47 +0100 Subject: [PATCH 5/5] selection: uri decode: move zero-length check into decode_one_uri() --- selection.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/selection.c b/selection.c index b9b086a8..de8b06e7 100644 --- a/selection.c +++ b/selection.c @@ -1490,6 +1490,9 @@ decode_one_uri(struct clipboard_receive *ctx, char *uri, size_t len) { LOG_DBG("URI: \"%.*s\"", (int)len, uri); + if (len == 0) + return false; + char *scheme, *host, *path; if (!uri_parse(uri, len, &scheme, NULL, NULL, &host, NULL, &path, NULL, NULL)) { LOG_ERR("drag-and-drop: invalid URI: %.*s", (int)len, uri); @@ -1544,8 +1547,7 @@ static void fdm_receive_finish_uri(struct clipboard_receive *ctx) { LOG_DBG("finish: %.*s", (int)ctx->buf.idx, ctx->buf.data); - if (ctx->buf.idx > 0) - decode_one_uri(ctx, ctx->buf.data, ctx->buf.idx); + decode_one_uri(ctx, ctx->buf.data, ctx->buf.idx); } static bool