mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-05-01 21:10:13 +00:00
Normalize outbound client and update URLs
This commit is contained in:
parent
dcc4747215
commit
33efdc3fb5
9 changed files with 166 additions and 47 deletions
46
pkg/netutil/http_urls.go
Normal file
46
pkg/netutil/http_urls.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package netutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NormalizeHTTPBaseURL parses and canonicalizes an HTTP(S) base URL. If the
|
||||
// input has no scheme, defaultScheme is applied.
|
||||
func NormalizeHTTPBaseURL(rawURL, defaultScheme string) (*url.URL, error) {
|
||||
s := strings.TrimSpace(rawURL)
|
||||
if s == "" {
|
||||
return nil, fmt.Errorf("URL cannot be empty")
|
||||
}
|
||||
if !strings.Contains(s, "://") {
|
||||
if defaultScheme == "" {
|
||||
return nil, fmt.Errorf("URL must include http:// or https://")
|
||||
}
|
||||
s = strings.TrimSuffix(defaultScheme, "://") + "://" + s
|
||||
}
|
||||
|
||||
parsed, err := url.ParseRequestURI(s)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid URL format")
|
||||
}
|
||||
if parsed.User != nil {
|
||||
return nil, fmt.Errorf("embedded credentials are not allowed")
|
||||
}
|
||||
if parsed.Fragment != "" {
|
||||
return nil, fmt.Errorf("URL fragments are not allowed")
|
||||
}
|
||||
if parsed.Hostname() == "" {
|
||||
return nil, fmt.Errorf("URL must have a hostname")
|
||||
}
|
||||
if parsed.Scheme != "http" && parsed.Scheme != "https" {
|
||||
return nil, fmt.Errorf("URL scheme must be http or https")
|
||||
}
|
||||
parsed.RawQuery = ""
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
// ValidateAbsoluteHTTPURL validates an already absolute HTTP(S) URL.
|
||||
func ValidateAbsoluteHTTPURL(rawURL string) (*url.URL, error) {
|
||||
return NormalizeHTTPBaseURL(rawURL, "")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue