tEscape XML characters for types 0 and 1 - phroxy - Gopher to HTTP proxy
 (HTM) git clone git://git.z3bra.org/phroxy.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
 (DIR) commit f1491a5ecb91945b0ef4dc69aff9d8f05ce00fea
 (DIR) parent 0938e922f3ff3a0e7e50a3a49ebbbeee9f26588d
 (HTM) Author: Willy Goiffon <dev@z3bra.org>
       Date:   Thu, 22 Oct 2020 09:50:33 +0200
       
       Escape XML characters for types 0 and 1
       
       Diffstat:
         M phroxy.c                            |      28 +++++++++++++++++++---------
       
       1 file changed, 19 insertions(+), 9 deletions(-)
       ---
 (DIR) diff --git a/phroxy.c b/phroxy.c
       t@@ -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;