From 0d7056446009f38ea44249f2f74ab70b05410cfb Mon Sep 17 00:00:00 2001 From: demo Date: Thu, 28 May 2026 16:58:21 -0400 Subject: refactor: move initial URL parsing into function 'convertToURL' --- main.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 4cb3d0d..3582ed5 100644 --- a/main.go +++ b/main.go @@ -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()) { -- cgit v1.2.3