diff options
| author | demo <demo@antix1> | 2026-05-28 16:58:21 -0400 |
|---|---|---|
| committer | demo <demo@antix1> | 2026-05-28 16:58:21 -0400 |
| commit | 0d7056446009f38ea44249f2f74ab70b05410cfb (patch) | |
| tree | b45cacc9257ac092a8d25e17c71a0bdc181f6467 | |
| parent | 828562584138f27f4b75209dcf665a5033bd0881 (diff) | |
refactor: move initial URL parsing into function 'convertToURL'
| -rw-r--r-- | main.go | 29 |
1 files changed, 20 insertions, 9 deletions
@@ -46,14 +46,7 @@ func main() { log.Fatalf("Invalid -depth argument: %d", *maxDepth) } - // Add "https://" scheme prefix if missing. It looks like we - // have to do this before the parsing step, not after. - if !strings.HasPrefix(*startRawURL, "http://") && !strings.HasPrefix(*startRawURL, "https://") { - *startRawURL = "https://" + *startRawURL - fmt.Printf("start url: %s\n", *startRawURL) - } - - startURL, err := url.Parse(*startRawURL) + startURL, err := convertToURL(*startRawURL) if err != nil { log.Fatal(err) } @@ -61,10 +54,28 @@ func main() { // Our web crawlers use concurrency: check if any goroutines // have leaked. getLeakProfile(func() { - classic(*startURL, *maxConcurrency, *maxURLs, *maxDepth) + classic(startURL, *maxConcurrency, *maxURLs, *maxDepth) }) } +// convertToURL parses the given rawURL into a [url.URL]. If the +// rawURL is missing a scheme, "https://" is prepended before parsing. +// +// Return the parsed URL, along with any error. +func convertToURL(rawURL string) (url.URL, error) { + if !strings.HasPrefix(rawURL, "http://") && !strings.HasPrefix(rawURL, "https://") { + rawURL = "https://" + rawURL + fmt.Printf("start url: %s\n", rawURL) + } + + u, err := url.Parse(rawURL) + if err != nil { + return url.URL{}, fmt.Errorf("can't parse %s: %w", rawURL, err) + } + + return *u, nil +} + // getLeakProfile runs a leaky program snippet, extracts the goroutine leak profile, // and writes it to stdout. func getLeakProfile(leakySnippet func()) { |
