+
Skip to content

Fix handling of redirected URLs in OnResponseHeaders #561

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

Merged
merged 1 commit into from
Nov 30, 2020
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
13 changes: 6 additions & 7 deletions colly.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,23 +660,22 @@ func (c *Collector) fetch(u, method string, depth int, requestData io.Reader, ct
hTrace = &HTTPTrace{}
req = hTrace.WithTrace(req)
}
checkHeadersFunc := func(statusCode int, headers http.Header) bool {
origURL := req.URL
checkHeadersFunc := func(req *http.Request, statusCode int, headers http.Header) bool {
if req.URL != origURL {
request.URL = req.URL
request.Headers = &req.Header
}
c.handleOnResponseHeaders(&Response{Ctx: ctx, Request: request, StatusCode: statusCode, Headers: &headers})
return !request.abort
}

origURL := req.URL
response, err := c.backend.Cache(req, c.MaxBodySize, checkHeadersFunc, c.CacheDir)
if proxyURL, ok := req.Context().Value(ProxyURLKey).(string); ok {
request.ProxyURL = proxyURL
}
if err := c.handleOnError(response, err, request, ctx); err != nil {
return err
}
if req.URL != origURL {
request.URL = req.URL
request.Headers = &req.Header
}
atomic.AddUint32(&c.responseCount, 1)
response.Ctx = ctx
response.Request = request
Expand Down
9 changes: 8 additions & 1 deletion colly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,9 +778,16 @@ func TestRedirect(t *testing.T) {
t.Error("Invalid URL after redirect: " + u)
}
})

c.OnResponseHeaders(func(r *Response) {
if !strings.HasSuffix(r.Request.URL.String(), "/redirected/") {
t.Error("Invalid URL in Request after redirect (OnResponseHeaders): " + r.Request.URL.String())
}
})

c.OnResponse(func(r *Response) {
if !strings.HasSuffix(r.Request.URL.String(), "/redirected/") {
t.Error("Invalid URL in Request after redirect: " + r.Request.URL.String())
t.Error("Invalid URL in Request after redirect (OnResponse): " + r.Request.URL.String())
}
})
c.Visit(ts.URL + "/redirect")
Expand Down
4 changes: 2 additions & 2 deletions http_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type httpBackend struct {
lock *sync.RWMutex
}

type checkHeadersFunc func(statusCode int, header http.Header) bool
type checkHeadersFunc func(req *http.Request, statusCode int, header http.Header) bool

// LimitRule provides connection restrictions for domains.
// Both DomainRegexp and DomainGlob can be used to specify
Expand Down Expand Up @@ -188,7 +188,7 @@ func (h *httpBackend) Do(request *http.Request, bodySize int, checkHeadersFunc c
if res.Request != nil {
*request = *res.Request
}
if !checkHeadersFunc(res.StatusCode, res.Header) {
if !checkHeadersFunc(request, res.StatusCode, res.Header) {
// closing res.Body (see defer above) without reading it aborts
// the download
return nil, ErrAbortedAfterHeaders
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载