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

Copy the headers from the last request on redirect #24

Merged
merged 4 commits into from
Oct 13, 2017
Merged
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: 27 additions & 0 deletions colly.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package colly
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -130,6 +131,7 @@ func (c *Collector) Init() {
c.MaxBodySize = 10 * 1024 * 1024
c.backend = &httpBackend{}
c.backend.Init()
c.backend.Client.CheckRedirect = c.checkRedirectFunc()
c.wg = &sync.WaitGroup{}
c.lock = &sync.Mutex{}
}
Expand Down Expand Up @@ -356,6 +358,31 @@ func (c *Collector) Cookies(URL string) []*http.Cookie {
return c.backend.Client.Jar.Cookies(u)
}

func (c *Collector) checkRedirectFunc() func(req *http.Request, via []*http.Request) error {
return func(req *http.Request, via []*http.Request) error {
if !c.isDomainAllowed(req.URL.Host) {
return fmt.Errorf("Not following redirect to %s because its not in AllowedDomains", req.URL.Host)
}

// Honor golangs default of maximum of 10 redirects
if len(via) >= 10 {
return http.ErrUseLastResponse
}

lastRequest := via[len(via)-1]

// Copy the headers from last request
req.Header = lastRequest.Header

// If domain has changed, remove the Authorization-header if it exists
if req.URL.Host != lastRequest.URL.Host {
req.Header.Del("Authorization")
}

return nil
}
}

// Attr returns the selected attribute of a HTMLElement or empty string
// if no attribute found
func (h *HTMLElement) Attr(k string) string {
Expand Down