| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- package types
- import (
- "errors"
- "fmt"
- "net"
- "net/url"
- "sort"
- "strings"
- )
- type URLs []url.URL
- func (us *URLs) Sort() {
- sort.Sort(us)
- }
- func (us URLs) Len() int { return len(us) }
- func (us URLs) Less(i, j int) bool { return us[i].String() < us[j].String() }
- func (us URLs) Swap(i, j int) { us[i], us[j] = us[j], us[i] }
- func (us URLs) StringSlice() []string {
- out := make([]string, len(us))
- for i := range us {
- out[i] = us[i].String()
- }
- return out
- }
- func NewURLs(strs []string) (URLs, error) {
- all := make([]url.URL, len(strs))
- if len(all) == 0 {
- return nil, errors.New("no valid URLs given")
- }
- for i, in := range strs {
- in = strings.TrimSpace(in)
- u, err := url.Parse(in)
- if err != nil {
- return nil, err
- }
- if u.Scheme != "http" && u.Scheme != "https" {
- return nil, fmt.Errorf("URL scheme must be http or https: %s", in)
- }
- if _, _, err := net.SplitHostPort(u.Host); err != nil {
- return nil, fmt.Errorf(`URL address does not have the form "host:port": %s`, in)
- }
- if u.Path != "" {
- return nil, fmt.Errorf("URL must not contain a path: %s", in)
- }
- all[i] = *u
- }
- us := URLs(all)
- us.Sort()
- return us, nil
- }
|