]> Sergey Matveev's repositories - tofuproxy.git/blobdiff - rounds/gemini.go
Download link for 0.6.0 release
[tofuproxy.git] / rounds / gemini.go
index 96b77ee5931328c3bd8bda6078efbd2385dcd702..71da97f361bfe33526a696be30e00ac0b2177b62 100644 (file)
@@ -1,20 +1,18 @@
-/*
-tofuproxy -- flexible HTTP/HTTPS proxy, TLS terminator, X.509 TOFU
-             manager, WARC/geminispace browser
-Copyright (C) 2021-2023 Sergey Matveev <stargrave@stargrave.org>
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, version 3 of the License.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
+// tofuproxy -- flexible HTTP/HTTPS proxy, TLS terminator, X.509 TOFU
+//              manager, WARC/geminispace browser
+// Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, version 3 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 package rounds
 
@@ -126,7 +124,7 @@ func RoundGemini(
                log.Printf("%s: can not read response: %+v\n", req.URL, err)
                return false, err
        }
-       cols := strings.SplitN(rawResp, " ", 2)
+       cols := strings.SplitN(strings.TrimRight(rawResp, "\r\n"), " ", 2)
        if len(cols) < 2 {
                err = fmt.Errorf("invalid response format: %s", rawResp)
                log.Printf("%s: %s\n", req.URL, err)
@@ -224,18 +222,27 @@ func RoundGemini(
                        if strings.HasPrefix(line, "=>") {
                                line = strings.TrimLeft(line[2:], " ")
                                cols = strings.Fields(line)
-                               u := geminifyURL(host, cols[0], paths...)
+                               u1 := geminifyURL(host, cols[0], paths...)
+                               u2 := geminifyURL(host, cols[0])
                                switch len(cols) {
                                case 1:
                                        fmt.Fprintf(
-                                               &buf, "<a href=\"%s\">%s</a><br/>\n",
-                                               u, html.EscapeString(cols[0]),
+                                               &buf, "<a href=\"%s\">%s</a>",
+                                               u1, html.EscapeString(cols[0]),
                                        )
+                                       if u1 != u2 {
+                                               fmt.Fprintf(&buf, "&nbsp;<a href=\"%s\">[2]</a>", u2)
+                                       }
+                                       fmt.Fprintf(&buf, "<br/>\n")
                                default:
                                        fmt.Fprintf(
-                                               &buf, "<a href=\"%s\">%s</a> (<tt>%s</tt>)<br/>\n",
-                                               u, html.EscapeString(strings.Join(cols[1:], " ")), cols[0],
+                                               &buf, "<a href=\"%s\">%s</a>",
+                                               u1, html.EscapeString(strings.Join(cols[1:], " ")),
                                        )
+                                       if u1 != u2 {
+                                               fmt.Fprintf(&buf, "&nbsp;<a href=\"%s\">[2]</a>", u2)
+                                       }
+                                       fmt.Fprintf(&buf, "(<tt>%s</tt>)<br/>\n", cols[0])
                                }
                                continue
                        }