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

Patch: update crawling to not follow redirects when -disable-redirects is set #630

Merged
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions pkg/engine/common/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ func (s *Shared) Do(crawlSession *CrawlSession, doRequest DoRequestFunc) error {
if resp.Resp == nil || resp.Reader == nil {
return
}
if s.Options.Options.DisableRedirects && resp.IsRedirect() {
return
}

navigationRequests := parser.ParseResponse(resp)
s.Enqueue(crawlSession.Queue, navigationRequests...)
Expand Down
5 changes: 5 additions & 0 deletions pkg/engine/hybrid/crawl.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ func (c *Crawler) navigateRequest(s *common.CrawlSession, request *navigation.Re
// process the raw response
navigationRequests := parser.ParseResponse(resp)
c.Enqueue(s.Queue, navigationRequests...)

// do not continue following the request if it's a redirect and redirects are disabled
if c.Options.Options.DisableRedirects && resp.IsRedirect() {
return nil
}
return FetchContinueRequest(page, e)
})() //nolint
defer func() {
Expand Down
4 changes: 3 additions & 1 deletion pkg/engine/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ var responseParsers = []responseParser{
// Header based parsers
{headerParser, headerContentLocationParser},
{headerParser, headerLinkParser},
{headerParser, headerLocationParser},
{headerParser, headerRefreshParser},

// Body based parsers
Expand Down Expand Up @@ -84,6 +83,9 @@ func InitWithOptions(options *types.Options) {
responseParsers = append(responseParsers, responseParser{contentParser, scriptJSFileRegexParser})
responseParsers = append(responseParsers, responseParser{contentParser, bodyScrapeEndpointsParser})
}
if !options.DisableRedirects {
responseParsers = append(responseParsers, responseParser{headerParser, headerLocationParser})
}
}

// parseResponse runs the response parsers on the navigation response
Expand Down
4 changes: 4 additions & 0 deletions pkg/navigation/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ func (n Response) AbsoluteURL(path string) string {
final := absURL.String()
return final
}

func (n Response) IsRedirect() bool {
return n.StatusCode >= 300 && n.StatusCode <= 399
}
Loading