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

Attempting to fetch nonexisting /sitemap.xml.gz results in "gzip: invalid header" #745

Open
WGH- opened this issue Jan 5, 2023 · 1 comment · May be fixed by #746
Open

Attempting to fetch nonexisting /sitemap.xml.gz results in "gzip: invalid header" #745

WGH- opened this issue Jan 5, 2023 · 1 comment · May be fixed by #746
Assignees
Labels

Comments

@WGH-
Copy link
Collaborator

WGH- commented Jan 5, 2023

package main

import (
	"log"
	"net/http"

	"github.com/gocolly/colly/v2"
)

func main() {
	// we have to disable Accept-Encoding: gzip,
	// because the remote server might send compressed
	// 404 page, avoiding the bug
	tr := &http.Transport{
		DisableCompression: true,
	}

	c := colly.NewCollector()
	c.WithTransport(tr)

	c.OnError(func(resp *colly.Response, err error) {
		log.Printf("err=%v", err)
	})

	c.Visit("https://example.org/sitemap.xml.gz")
}

Results in:

2023/01/05 22:30:27 err=gzip: invalid header
@WGH- WGH- added the bug label Jan 5, 2023
@WGH- WGH- self-assigned this Jan 5, 2023
@WGH-
Copy link
Collaborator Author

WGH- commented Jan 5, 2023

This is the root cause of this bug:

colly/http_backend.go

Lines 203 to 209 in 947eeea

if !res.Uncompressed && (strings.Contains(contentEncoding, "gzip") || (contentEncoding == "" && strings.Contains(strings.ToLower(res.Header.Get("Content-Type")), "gzip")) || strings.HasSuffix(strings.ToLower(request.URL.Path), ".xml.gz")) {
bodyReader, err = gzip.NewReader(bodyReader)
if err != nil {
return nil, err
}
defer bodyReader.(*gzip.Reader).Close()
}

It incorrectly assumes that if request URL ends with .xml.gz, the response must be compressed.

I assume this was done to support /sitemap.xml.gz which are usually compressed, but not sent with Content-Encoding: gzip.

@WGH- WGH- linked a pull request Jan 5, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant