adjustURL validates if a profile source is a URL and returns an cleaned up URL and the timeout to use for retrieval over HTTP. If the source cannot be recognized as a URL it returns an empty string.
(source string, duration, timeout time.Duration)
| 593 | // cleaned up URL and the timeout to use for retrieval over HTTP. |
| 594 | // If the source cannot be recognized as a URL it returns an empty string. |
| 595 | func adjustURL(source string, duration, timeout time.Duration) (string, time.Duration) { |
| 596 | u, err := url.Parse(source) |
| 597 | if err != nil || (u.Host == "" && u.Scheme != "" && u.Scheme != "file") { |
| 598 | // Try adding http:// to catch sources of the form hostname:port/path. |
| 599 | // url.Parse treats "hostname" as the scheme. |
| 600 | u, err = url.Parse("http://" + source) |
| 601 | } |
| 602 | if err != nil || u.Host == "" { |
| 603 | return "", 0 |
| 604 | } |
| 605 | |
| 606 | // Apply duration/timeout overrides to URL. |
| 607 | values := u.Query() |
| 608 | if duration > 0 { |
| 609 | values.Set("seconds", fmt.Sprint(int(duration.Seconds()))) |
| 610 | } else { |
| 611 | if urlSeconds := values.Get("seconds"); urlSeconds != "" { |
| 612 | if us, err := strconv.ParseInt(urlSeconds, 10, 32); err == nil { |
| 613 | duration = time.Duration(us) * time.Second |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | if timeout <= 0 { |
| 618 | if duration > 0 { |
| 619 | timeout = duration + duration/2 |
| 620 | } else { |
| 621 | timeout = 60 * time.Second |
| 622 | } |
| 623 | } |
| 624 | u.RawQuery = values.Encode() |
| 625 | return u.String(), timeout |
| 626 | } |