Fix URL re-encoding for square brackets

This commit is contained in:
comp500 2022-02-14 18:47:03 +00:00
parent 55ce2fb581
commit f1716faa72

View File

@ -3,9 +3,15 @@ package core
import ( import (
"fmt" "fmt"
"net/url" "net/url"
"strings"
) )
// ReencodeURL re-encodes URLs for RFC3986 compliance; as CurseForge URLs aren't properly encoded
func ReencodeURL(u string) (string, error) { func ReencodeURL(u string) (string, error) {
// Go's URL library isn't entirely RFC3986 compliant :(
// Manually replace [ and ] with %5B and %5D
u = strings.ReplaceAll(u, "[", "%5B")
u = strings.ReplaceAll(u, "]", "%5D")
parsed, err := url.Parse(u) parsed, err := url.Parse(u)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to parse url: %s, %v", u, err) return "", fmt.Errorf("failed to parse url: %s, %v", u, err)