]> Sergey Matveev's repositories - tofuproxy.git/blobdiff - rounds/redirectHTML.go
More Fprintf usage
[tofuproxy.git] / rounds / redirectHTML.go
index a5d4bd779f6e430b2008d12e1d6389c0d885b743..d139eb28ae41157c5e5bfc50ad4a784d81b10a73 100644 (file)
@@ -1,7 +1,7 @@
 /*
 tofuproxy -- flexible HTTP/HTTPS proxy, TLS terminator, X.509 TOFU
              manager, WARC/geminispace browser
-Copyright (C) 2021-2022 Sergey Matveev <stargrave@stargrave.org>
+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
@@ -44,8 +44,14 @@ var imageExts = map[string]struct{}{
        ".webp": {},
 }
 
-func isNewsboat(req *http.Request) bool {
-       return strings.Contains(req.Header.Get("User-Agent"), "newsboat/")
+func isFeeder(req *http.Request) bool {
+       if strings.Contains(req.Header.Get("User-Agent"), "newsboat/") {
+               return true
+       }
+       if strings.Contains(req.Header.Get("User-Agent"), "stargrave.org-feeder/") {
+               return true
+       }
+       return false
 }
 
 func RoundRedirectHTML(
@@ -62,7 +68,7 @@ func RoundRedirectHTML(
        case http.StatusMovedPermanently, http.StatusPermanentRedirect:
                redirType = "permanent"
        case http.StatusFound, http.StatusSeeOther, http.StatusTemporaryRedirect:
-               if isNewsboat(req) {
+               if isFeeder(req) {
                        return true, nil
                }
                if _, ok := imageExts[filepath.Ext(req.URL.Path)]; ok {
@@ -76,14 +82,13 @@ func RoundRedirectHTML(
        w.Header().Add("Content-Type", "text/html")
        w.WriteHeader(http.StatusOK)
        location := resp.Header.Get("Location")
-       w.Write([]byte(
-               fmt.Sprintf(
-                       `<!DOCTYPE html>
+       fmt.Fprintf(
+               w, `<!DOCTYPE html>
 <html><head><title>%d %s: %s redirection</title></head>
 <body><a href="%s">%s</a></body></html>`,
-                       resp.StatusCode, http.StatusText(resp.StatusCode),
-                       redirType, location, location,
-               )))
+               resp.StatusCode, http.StatusText(resp.StatusCode),
+               redirType, location, location,
+       )
        fifos.LogRedir <- fmt.Sprintf(
                "%s %s\t%s\t%s", req.Method, req.URL, resp.Status, location,
        )