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

fixed fixCharset ignore not enough bug #755

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions colly.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ type Collector struct {
backend *httpBackend
wg *sync.WaitGroup
lock *sync.RWMutex

fixCharset bool
}

// RequestCallback is a type alias for OnRequest callback functions
Expand Down Expand Up @@ -462,6 +464,12 @@ func CheckHead() CollectorOption {
}
}

func FixCharset(f bool) CollectorOption {
return func(c *Collector) {
c.fixCharset = f
}
}

Comment on lines +467 to +472
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this different from DetectCharset option?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if fixCharset set false, user can fix charset onResponse

// Init initializes the Collector's private variables and sets default
// configuration for the Collector
func (c *Collector) Init() {
Expand All @@ -483,6 +491,9 @@ func (c *Collector) Init() {
c.ID = atomic.AddUint32(&collectorCounter, 1)
c.TraceHTTP = false
c.Context = context.Background()

// default true same as colly
c.fixCharset = true
}

// Appengine will replace the Collector's backend http.Client
Expand Down Expand Up @@ -711,9 +722,11 @@ func (c *Collector) fetch(u, method string, depth int, requestData io.Reader, ct
response.Request = request
response.Trace = hTrace

err = response.fixCharset(c.DetectCharset, request.ResponseCharacterEncoding)
if err != nil {
return err
if c.fixCharset {
err = response.fixCharset(c.DetectCharset, request.ResponseCharacterEncoding)
if err != nil {
return err
}
}

c.handleOnResponse(response)
Expand Down
7 changes: 1 addition & 6 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ func (r *Response) fixCharset(detectCharset bool, defaultEncoding string) error
}
contentType := strings.ToLower(r.Headers.Get("Content-Type"))

if strings.Contains(contentType, "image/") ||
strings.Contains(contentType, "video/") ||
strings.Contains(contentType, "audio/") ||
strings.Contains(contentType, "font/") {
// These MIME types should not have textual data.

if !strings.Contains(contentType, "text/") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the original check is too simplistic, but they're a lot of textual MIME types without text/ that might have encoding problems as well. There are application/xhtml+xml, application/xml, application/atom+xml, to name a few.

return nil
}

Expand Down