Skip to content

Commit

Permalink
all: make URL scheme handling case-insensitive (#3521)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gofastasf authored Feb 7, 2025
1 parent 4d1f585 commit af15ad4
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions internal/openurl/openurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

// SchemeMap maps URL schemes to values. The zero value is an empty map, ready for use.
// All schemes are stored and compared case-insensitively.
type SchemeMap struct {
api string
m map[string]any
Expand All @@ -46,6 +47,7 @@ func (m *SchemeMap) Register(api, typ, scheme string, value any) {
} else if m.api != api {
panic(fmt.Errorf("previously registered using api %q (now %q)", m.api, api))
}
scheme = strings.ToLower(scheme)
if _, exists := m.m[scheme]; exists {
panic(fmt.Errorf("scheme %q already registered for %s.%s", scheme, api, typ))
}
Expand All @@ -67,7 +69,7 @@ func (m *SchemeMap) FromString(typ, urlstr string) (any, *url.URL, error) {

// FromURL looks up the value for u's scheme.
func (m *SchemeMap) FromURL(typ string, u *url.URL) (any, error) {
scheme := u.Scheme
scheme := strings.ToLower(u.Scheme)
if scheme == "" {
return nil, fmt.Errorf("open %s.%s: no scheme in URL %q", m.api, typ, u)
}
Expand Down Expand Up @@ -96,10 +98,6 @@ func (m *SchemeMap) Schemes() []string {

// ValidScheme returns true iff scheme has been registered.
func (m *SchemeMap) ValidScheme(scheme string) bool {
for s := range m.m {
if scheme == s {
return true
}
}
return false
_, exists := m.m[strings.ToLower(scheme)]
return exists
}

0 comments on commit af15ad4

Please sign in to comment.