X-Git-Url: http://www.git.stargrave.org/?p=tofuproxy.git;a=blobdiff_plain;f=rounds%2Fgemini.go;h=987b62eb7115c8e9879a910ccd44163135a43ff6;hp=b87c18a19f69a78785e73f8a5bfa05cc9f251589;hb=HEAD;hpb=9088a7eaa3d2a6885adf47bee4c1cccead68630e diff --git a/rounds/gemini.go b/rounds/gemini.go index b87c18a..4213876 100644 --- a/rounds/gemini.go +++ b/rounds/gemini.go @@ -1,6 +1,6 @@ // tofuproxy -- flexible HTTP/HTTPS proxy, TLS terminator, X.509 TOFU // manager, WARC/geminispace browser -// Copyright (C) 2021-2024 Sergey Matveev +// Copyright (C) 2021-2025 Sergey Matveev // // 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 @@ -124,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) @@ -195,7 +195,8 @@ func RoundGemini( !strings.Contains(req.Header.Get("Accept"), ContentTypeGemini) { w.Header().Add("Content-Type", "text/html") w.WriteHeader(http.StatusOK) - raw, err := io.ReadAll(br) + var raw []byte + raw, err = io.ReadAll(br) if err != nil { log.Printf("%s: can not read response body: %+v\n", req.URL, err) return false, err @@ -205,7 +206,7 @@ func RoundGemini( %d (%s) `, code, codeName) pre := false - for _, line := range strings.Split(string(raw), "\n") { + for line := range strings.SplitSeq(string(raw), "\n") { if strings.HasPrefix(line, "```") { if pre { buf.WriteString("\n") @@ -222,18 +223,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, "%s
\n", - u, html.EscapeString(cols[0]), + &buf, "%s", + u1, html.EscapeString(cols[0]), ) + if u1 != u2 { + fmt.Fprintf(&buf, " [2]", u2) + } + fmt.Fprintf(&buf, "
\n") default: fmt.Fprintf( - &buf, "%s (%s)
\n", - u, html.EscapeString(strings.Join(cols[1:], " ")), cols[0], + &buf, "%s", + u1, html.EscapeString(strings.Join(cols[1:], " ")), ) + if u1 != u2 { + fmt.Fprintf(&buf, " [2]", u2) + } + fmt.Fprintf(&buf, "(%s)
\n", cols[0]) } continue }