]> Sergey Matveev's repositories - tofuproxy.git/blobdiff - rounds/gemini.go
More Fprintf usage
[tofuproxy.git] / rounds / gemini.go
index 4876ff7dadccbc741f5c272f610c8ed6a0f39e67..d81039fd01872a9aabd2627658de1b8fb68a2848 100644 (file)
@@ -1,7 +1,7 @@
 /*
-tofuproxy -- flexible HTTP proxy, TLS terminator, X.509 certificates
-             manager, WARC/Gemini browser
-Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
+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
@@ -94,6 +94,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
@@ -103,11 +109,11 @@ func RoundGemini(
                log.Printf("%s: can not dial: %+v\n", req.URL, err)
                return false, err
        }
-       _, err = fmt.Fprintf(
-               conn, "%s%s/%s\r\n",
-               SchemeGemini, host, strings.Join(paths, "/"),
-       )
-       if err != nil {
+       query := fmt.Sprintf("%s%s/%s", SchemeGemini, host, strings.Join(paths, "/"))
+       if req.URL.RawQuery != "" {
+               query += "?" + req.URL.RawQuery
+       }
+       if _, err = conn.Write([]byte(query + "\r\n")); err != nil {
                log.Printf("%s: can not send request: %+v\n", req.URL, err)
                return false, err
        }
@@ -145,39 +151,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]
@@ -253,6 +265,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 +274,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
 }