phroxy

Gopher to HTTP proxy
git clone git://git.z3bra.org/phroxy.git
Log | Files | Refs | LICENSE

commit f1491a5ecb91945b0ef4dc69aff9d8f05ce00fea
parent 0938e922f3ff3a0e7e50a3a49ebbbeee9f26588d
Author: Willy Goiffon <dev@z3bra.org>
Date:   Thu, 22 Oct 2020 09:50:33 +0200

Escape XML characters for types 0 and 1

Diffstat:
Mphroxy.c | 28+++++++++++++++++++---------
1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/phroxy.c b/phroxy.c @@ -285,20 +285,30 @@ int printhtml(int fd, const char *data, size_t len) { size_t r, n; - const char *s, *e; + const char *s, *e, *x; - n = 0; write(fd, "<pre>", 5); - while(n < len) { + + for (n = 0; n < len; n++) { + s = data + n; - e = strstr(s, "\n\n"); - r = e ? (size_t)(e - s + 2) : strlen(s); - n += r; - if (!r) + /* escape XML characters */ + x = NULL; + switch (*s) { + case '&': x = x ? x : "&amp;"; /* FALLTHROUGH */ + case '<': x = x ? x : "&lt;"; /* FALLTHROUGH */ + case '>': x = x ? x : "&gt;"; /* FALLTHROUGH */ + write(fd, x, strlen(x)); break; - - write(fd, s, r); + default: + e = strpbrk(s, "&<>"); + r = e ? (size_t)(e - s) : len - n; + if (r) { + write(fd, s, r); + n += r - 1; + } + } } write(fd, "</pre>\n", 7); return 0;