a7460a
From 780c4ea1b4c9d2fb41f6962aa6ed73ae57f74b2b Mon Sep 17 00:00:00 2001
a7460a
From: Joshua Rogers <MegaManSec@users.noreply.github.com>
a7460a
Date: Mon, 18 Apr 2022 13:42:36 +0000
a7460a
Subject: [PATCH] Improve handling of Gopher responses (#1022)
a7460a
a7460a
---
a7460a
 src/gopher.cc | 45 ++++++++++++++++++++-------------------------
a7460a
 1 file changed, 20 insertions(+), 25 deletions(-)
a7460a
a7460a
diff --git a/src/gopher.cc b/src/gopher.cc
a7460a
index 169b0e18299..6187da18bcd 100644
a7460a
--- a/src/gopher.cc
a7460a
+++ b/src/gopher.cc
a7460a
@@ -371,7 +371,6 @@ gopherToHTML(GopherStateData * gopherState, char *inbuf, int len)
a7460a
     char *lpos = NULL;
a7460a
     char *tline = NULL;
a7460a
     LOCAL_ARRAY(char, line, TEMP_BUF_SIZE);
a7460a
-    LOCAL_ARRAY(char, tmpbuf, TEMP_BUF_SIZE);
a7460a
     char *name = NULL;
a7460a
     char *selector = NULL;
a7460a
     char *host = NULL;
a7460a
@@ -381,7 +380,6 @@ gopherToHTML(GopherStateData * gopherState, char *inbuf, int len)
a7460a
     char gtype;
a7460a
     StoreEntry *entry = NULL;
a7460a
 
a7460a
-    memset(tmpbuf, '\0', TEMP_BUF_SIZE);
a7460a
     memset(line, '\0', TEMP_BUF_SIZE);
a7460a
 
a7460a
     entry = gopherState->entry;
a7460a
@@ -416,7 +414,7 @@ gopherToHTML(GopherStateData * gopherState, char *inbuf, int len)
a7460a
         return;
a7460a
     }
a7460a
 
a7460a
-    String outbuf;
a7460a
+    SBuf outbuf;
a7460a
 
a7460a
     if (!gopherState->HTML_header_added) {
a7460a
         if (gopherState->conversion == GopherStateData::HTML_CSO_RESULT)
a7460a
@@ -583,34 +581,34 @@ gopherToHTML(GopherStateData * gopherState, char *inbuf, int len)
a7460a
                         break;
a7460a
                     }
a7460a
 
a7460a
-                    memset(tmpbuf, '\0', TEMP_BUF_SIZE);
a7460a
-
a7460a
                     if ((gtype == GOPHER_TELNET) || (gtype == GOPHER_3270)) {
a7460a
                         if (strlen(escaped_selector) != 0)
a7460a
-                            snprintf(tmpbuf, TEMP_BUF_SIZE, " %s\n",
a7460a
-                                     icon_url, escaped_selector, rfc1738_escape_part(host),
a7460a
-                                     *port ? ":" : "", port, html_quote(name));
a7460a
+                            outbuf.appendf(" %s\n",
a7460a
+                                           icon_url, escaped_selector, rfc1738_escape_part(host),
a7460a
+                                           *port ? ":" : "", port, html_quote(name));
a7460a
                         else
a7460a
-                            snprintf(tmpbuf, TEMP_BUF_SIZE, " %s\n",
a7460a
-                                     icon_url, rfc1738_escape_part(host), *port ? ":" : "",
a7460a
-                                     port, html_quote(name));
a7460a
+                            outbuf.appendf(" %s\n",
a7460a
+                                           icon_url, rfc1738_escape_part(host), *port ? ":" : "",
a7460a
+                                           port, html_quote(name));
a7460a
 
a7460a
                     } else if (gtype == GOPHER_INFO) {
a7460a
-                        snprintf(tmpbuf, TEMP_BUF_SIZE, "\t%s\n", html_quote(name));
a7460a
+                        outbuf.appendf("\t%s\n", html_quote(name));
a7460a
                     } else {
a7460a
                         if (strncmp(selector, "GET /", 5) == 0) {
a7460a
                             /* WWW link */
a7460a
-                            snprintf(tmpbuf, TEMP_BUF_SIZE, " %s\n",
a7460a
-                                     icon_url, host, rfc1738_escape_unescaped(selector + 5), html_quote(name));
a7460a
+                            outbuf.appendf(" %s\n",
a7460a
+                                           icon_url, host, rfc1738_escape_unescaped(selector + 5), html_quote(name));
a7460a
+                        } else if (gtype == GOPHER_WWW) {
a7460a
+                            outbuf.appendf(" %s\n",
a7460a
+                                           icon_url, rfc1738_escape_unescaped(selector), html_quote(name));
a7460a
                         } else {
a7460a
                             /* Standard link */
a7460a
-                            snprintf(tmpbuf, TEMP_BUF_SIZE, " %s\n",
a7460a
-                                     icon_url, host, gtype, escaped_selector, html_quote(name));
a7460a
+                            outbuf.appendf(" %s\n",
a7460a
+                                           icon_url, host, gtype, escaped_selector, html_quote(name));
a7460a
                         }
a7460a
                     }
a7460a
 
a7460a
                     safe_free(escaped_selector);
a7460a
-                    outbuf.append(tmpbuf);
a7460a
                 } else {
a7460a
                     memset(line, '\0', TEMP_BUF_SIZE);
a7460a
                     continue;
a7460a
@@ -643,13 +641,12 @@ gopherToHTML(GopherStateData * gopherState, char *inbuf, int len)
a7460a
                     break;
a7460a
 
a7460a
                 if (gopherState->cso_recno != recno) {
a7460a
-                    snprintf(tmpbuf, TEMP_BUF_SIZE, "

Record# %d
%s

\n
", recno, html_quote(result));
a7460a
+                    outbuf.appendf("

Record# %d
%s

\n
", recno, html_quote(result));
a7460a
                     gopherState->cso_recno = recno;
a7460a
                 } else {
a7460a
-                    snprintf(tmpbuf, TEMP_BUF_SIZE, "%s\n", html_quote(result));
a7460a
+                    outbuf.appendf("%s\n", html_quote(result));
a7460a
                 }
a7460a
 
a7460a
-                outbuf.append(tmpbuf);
a7460a
                 break;
a7460a
             } else {
a7460a
                 int code;
a7460a
@@ -677,8 +674,7 @@ gopherToHTML(GopherStateData * gopherState, char *inbuf, int len)
a7460a
 
a7460a
                 case 502: { /* Too Many Matches */
a7460a
                     /* Print the message the server returns */
a7460a
-                    snprintf(tmpbuf, TEMP_BUF_SIZE, "

%s

\n
", html_quote(result));
a7460a
-                    outbuf.append(tmpbuf);
a7460a
+                    outbuf.appendf("

%s

\n
", html_quote(result));
a7460a
                     break;
a7460a
                 }
a7460a
 
a7460a
@@ -694,13 +690,12 @@ gopherToHTML(GopherStateData * gopherState, char *inbuf, int len)
a7460a
 
a7460a
     }               /* while loop */
a7460a
 
a7460a
-    if (outbuf.size() > 0) {
a7460a
-        entry->append(outbuf.rawBuf(), outbuf.size());
a7460a
+    if (outbuf.length() > 0) {
a7460a
+        entry->append(outbuf.rawContent(), outbuf.length());
a7460a
         /* now let start sending stuff to client */
a7460a
         entry->flush();
a7460a
     }
a7460a
 
a7460a
-    outbuf.clean();
a7460a
     return;
a7460a
 }
a7460a