Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Record final redirected URL instead of initial URL #936

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions docker/sswlinkauditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,39 @@ func check(link Link, linkch chan LinkStatus, number int) {
linkch <- LinkStatus{link.url, link.srcUrl, "Unknown error", -1, link.anchor}
}
} else {
location, noLocError := resp.Location()
url := link.url

if noLocError == nil {
url = location.String()
fmt.Println("redirect:", link.url, "redirected to", url)
}

defer resp.Body.Close()
linkch <- LinkStatus{link.url, link.srcUrl, resp.Status, resp.StatusCode, link.anchor}

linkch <- LinkStatus{url, link.srcUrl, resp.Status, resp.StatusCode, link.anchor}
}
}

func crawl(link Link, ch chan Link, linkch chan LinkStatus, number int) {
fmt.Println("CRAW", number, link.url)
thisUrl := link.url

client := &http.Client{
Timeout: 1 * time.Minute,
}
resp, err := client.Get(link.url)
dnsErr := new(net.DNSError)

if err == nil {
location, noLocError := resp.Location()

if noLocError == nil {
thisUrl = location.String()
fmt.Println("redirect:", link.url, "redirected to", thisUrl)
}
}

defer func() {
if err != nil {
fmt.Println("error: ", err)
Expand All @@ -124,7 +143,7 @@ func crawl(link Link, ch chan Link, linkch chan LinkStatus, number int) {
linkch <- LinkStatus{link.url, link.srcUrl, "Unknown error", -1, link.anchor}
}
} else {
linkch <- LinkStatus{link.url, link.srcUrl, resp.Status, resp.StatusCode, link.anchor}
linkch <- LinkStatus{thisUrl, link.srcUrl, resp.Status, resp.StatusCode, link.anchor}
}
}()

Expand Down Expand Up @@ -153,7 +172,7 @@ func crawl(link Link, ch chan Link, linkch chan LinkStatus, number int) {
case html.TextToken:
if depth > 0 {
text := strings.TrimSpace(string(z.Text()))
ch <- Link{linkUrl, link.url, "a", text}
ch <- Link{linkUrl, thisUrl, "a", text}
}
case html.StartTagToken, html.SelfClosingTagToken, html.EndTagToken:
t := z.Token()
Expand All @@ -168,7 +187,7 @@ func crawl(link Link, ch chan Link, linkch chan LinkStatus, number int) {
depth--
}
} else {
ch <- Link{newUrl, link.url, t.Data, ""}
ch <- Link{newUrl, thisUrl, t.Data, ""}
}
}

Expand Down
Loading