From 283096d2999ded96c1576226f640ace2560f2828 Mon Sep 17 00:00:00 2001 From: partisan Date: Thu, 8 Aug 2024 16:23:36 +0200 Subject: [PATCH] stupid proofing missing "http" in config file --- open-search.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/open-search.go b/open-search.go index 138020f..f6214a5 100644 --- a/open-search.go +++ b/open-search.go @@ -21,25 +21,38 @@ type URL struct { Template string `xml:"template,attr"` } -// isLocalAddress checks if the domain is a local address +// Checks if the URL already includes a protocol +func hasProtocol(url string) bool { + return strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") +} + +// Checks if the domain is a local address func isLocalAddress(domain string) bool { return domain == "localhost" || strings.HasPrefix(domain, "127.") || strings.HasPrefix(domain, "192.168.") || strings.HasPrefix(domain, "10.") } -func generateOpenSearchXML(config Config) { - protocol := "https://" - if isLocalAddress(config.Domain) { - protocol = "http://" +// Ensures that HTTP or HTTPS is befor the adress if needed +func addProtocol(domain string) string { + if hasProtocol(domain) { + return domain } + if isLocalAddress(domain) { + return "http://" + domain + } + return "https://" + domain +} + +func generateOpenSearchXML(config Config) { + baseURL := addProtocol(config.Domain) opensearch := OpenSearchDescription{ Xmlns: "http://a9.com/-/spec/opensearch/1.1/", - ShortName: "Ocásek", + ShortName: "Search Engine", Description: "Search engine", Tags: "search, engine", URL: URL{ Type: "text/html", - Template: fmt.Sprintf("%s%s/search?q={searchTerms}", protocol, config.Domain), + Template: fmt.Sprintf("%s/search?q={searchTerms}", baseURL), }, }