]> Sergey Matveev's repositories - tofuproxy.git/blobdiff - rounds/gemini.go
Download link for 0.6.0 release
[tofuproxy.git] / rounds / gemini.go
index 43cf422f5db1600c832c678b503db0dfead8d6e6..71da97f361bfe33526a696be30e00ac0b2177b62 100644 (file)
@@ -1,20 +1,18 @@
-/*
-tofuproxy -- flexible HTTP proxy, TLS terminator, X.509 certificates
-             manager, WARC/Gemini browser
-Copyright (C) 2021 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
 
@@ -94,6 +92,12 @@ func RoundGemini(
        }
        paths := strings.Split(strings.TrimPrefix(req.URL.Path, "/"), "/")
        host, paths = paths[0], paths[1:]
+       if host == "gemini:" {
+               http.Redirect(w, req, strings.Join(
+                       append([]string{GeminiEntrypoint}, paths[1:]...), "/",
+               ), http.StatusTemporaryRedirect)
+               return false, nil
+       }
        hostWithPort := host
        if !strings.Contains(hostWithPort, ":") {
                hostWithPort += GeminiPort
@@ -120,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)
@@ -145,39 +149,45 @@ func RoundGemini(
                w.Header().Add("Content-Type", "text/html")
                w.WriteHeader(http.StatusOK)
                u := geminifyURL(host, cols[1], paths...)
-               w.Write([]byte(
-                       fmt.Sprintf(
-                               `<!DOCTYPE html>
+               fmt.Fprintf(w, `<!DOCTYPE html>
 <html><head><title>%d (%s) redirection</title></head>
-<body>Redirection to <a href="%s">%s</a></body></html>`,
-                               code, codeName, u, u,
-                       )))
+<body><a href="%s">%s</a></body></html>`, code, codeName, u, u)
                fifos.LogRedir <- fmt.Sprintf(
                        "%s %s\t%d\t%s", req.Method, req.URL, code, cols[1],
                )
                return false, nil
        }
+       msg := fmt.Sprintf(
+               "%s %s\t%d (%s)\t%s",
+               req.Method, req.URL,
+               code, codeName,
+               cols[1],
+       )
        if 40 <= code && code <= 49 {
                w.Header().Add("Content-Type", "text/plain")
                w.WriteHeader(http.StatusBadGateway)
                fmt.Fprintf(w, "%s\n%d (%s)\n", cols[1], code, codeName)
+               fifos.LogNonOK <- msg
                return false, nil
        }
        if 50 <= code && code <= 59 {
                w.Header().Add("Content-Type", "text/plain")
                w.WriteHeader(http.StatusBadGateway)
                fmt.Fprintf(w, "%s\n%d (%s)\n", cols[1], code, codeName)
+               fifos.LogNonOK <- msg
                return false, nil
        }
        if 60 <= code && code <= 69 {
                w.Header().Add("Content-Type", "text/plain")
                w.WriteHeader(http.StatusUnauthorized)
                fmt.Fprintf(w, "%s\n%d (%s)\n", cols[1], code, codeName)
+               fifos.LogNonOK <- msg
                return false, nil
        }
        if !(20 <= code && code <= 29) {
                err = fmt.Errorf("unknown response code: %d", code)
                log.Printf("%s: %s\n", req.URL, err)
+               fifos.LogNonOK <- msg
                return false, err
        }
        contentType := strings.Split(strings.TrimRight(cols[1], "\r\n"), ";")[0]
@@ -209,20 +219,30 @@ func RoundGemini(
                                fmt.Fprintf(&buf, "%s\n", line)
                                continue
                        }
-                       if strings.HasPrefix(line, "=> ") {
+                       if strings.HasPrefix(line, "=>") {
+                               line = strings.TrimLeft(line[2:], " ")
                                cols = strings.Fields(line)
-                               u := geminifyURL(host, cols[1], paths...)
+                               u1 := geminifyURL(host, cols[0], paths...)
+                               u2 := geminifyURL(host, cols[0])
                                switch len(cols) {
-                               case 2:
+                               case 1:
                                        fmt.Fprintf(
-                                               &buf, "<a href=\"%s\">%s</a><br/>\n",
-                                               u, html.EscapeString(cols[1]),
+                                               &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[2:], " ")), cols[1],
+                                               &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
                        }
@@ -238,10 +258,6 @@ func RoundGemini(
                                fmt.Fprintf(&buf, "<h3>%s</h3>\n", html.EscapeString(line[4:]))
                                continue
                        }
-                       if strings.HasPrefix(line, "* ") {
-                               fmt.Fprintf(&buf, "&bullet; %s\n", html.EscapeString(line[2:]))
-                               continue
-                       }
                        if strings.HasPrefix(line, "> ") {
                                fmt.Fprintf(
                                        &buf, "<blockquote><tt>%s</tt></blockquote>\n",
@@ -253,6 +269,7 @@ func RoundGemini(
                }
                buf.WriteString("</body></html>\n")
                _, err = w.Write(buf.Bytes())
+               fifos.LogOK <- msg
                return false, err
        }
        w.Header().Add("Content-Type", contentType)
@@ -261,5 +278,6 @@ func RoundGemini(
        if err != nil {
                log.Printf("%s: can not read response body: %+v\n", req.URL, err)
        }
+       fifos.LogOK <- msg
        return false, err
 }