Encode characters for 'q' parameter to get passed and parsed correctly - gopherproxy-c - Gopher HTTP proxy in C (CGI)
 (HTM) git clone git://git.codemadness.org/gopherproxy-c
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit e96ccc93928250ede30995cff7bf0e11758b2602
 (DIR) parent 21855e485f7a47b90546f9039fec6a4b4f382567
 (HTM) Author: Julian Schweinsberg <pazz0@0xfa.de>
       Date:   Wed, 13 Nov 2024 18:41:56 +0000
       
       Encode characters for 'q' parameter to get passed and parsed correctly
       
       Without encoding the characters '&' and '=' they would be interpreted by
       getparam(), this is not correct because these characters are part of the
       selector of the gopher URI and shouldn't be interpreted by the
       gopherproxy but the gopher server.
       
       Additionally '#' gets escaped or it wouldn't be passed to the
       gopherproxy and by this not to the gopher server.
       
       The additional characters get URL escaped, so that no xmlenode is
       needed.
       
       For the character '&' gopher://gopherddit.com/1 is a good test case.
       
       Diffstat:
         M gopherproxy.c                       |      26 +++++++++++++++++++++++++-
       
       1 file changed, 25 insertions(+), 1 deletion(-)
       ---
 (DIR) diff --git a/gopherproxy.c b/gopherproxy.c
       @@ -91,6 +91,30 @@ xmlencode(const char *s)
                }
        }
        
       +/* Percent-encode characters so that the string can be used as a value for a
       +   query string.
       +   Additional characters get encoded so that no xmlencode() is needed */
       +void
       +encodeparam(const char *s)
       +{
       +        for (; *s; s++) {
       +                switch (*s) {
       +                case '<':
       +                case '>':
       +                case '\'':
       +                case '&':
       +                case '"':
       +                case '#':
       +                case '=':
       +                        printf("%%%02X", (unsigned char)*s);
       +                        break;
       +                default:
       +                        putchar(*s);
       +                        break;
       +                }
       +        }
       +}
       +
        int
        edial(const char *host, const char *port)
        {
       @@ -371,7 +395,7 @@ servedir(const char *server, const char *port, const char *path, const char *que
                                        xmlencode(v.path + sizeof("URL:") - 1);
                                } else {
                                        fputs("?q=", stdout);
       -                                xmlencode(uri);
       +                                encodeparam(uri);
                                }
                                fputs("\">", stdout);
                                xmlencode(v.username);