fish2018/pansou · error

创建gzip读取器失败

Error message

创建gzip读取器失败: %w

What it means

getFormhash wraps a failure from gzip.NewReader when the homepage response declares Content-Encoding: gzip. The library attempts manual gzip decoding; if the body is not valid gzip data, this error is returned.

Solutions

  1. If using a custom Transport, ensure DisableCompression is false so net/http handles gzip transparently (then the manual path won't trigger)
  2. Inspect raw response bytes to confirm whether they're actually gzip
  3. Retry the request; the body may be corrupted transiently
  4. Handle both gzip and deflate/br encodings defensively

Example fix

// before
tr := &http.Transport{DisableCompression: true}
// after
tr := &http.Transport{} // let net/http auto-decompress gzip
Defensive patterns

Strategy: try-catch

Try / catch

if err != nil && strings.Contains(err.Error(), "创建gzip读取器失败") {
    // compression mismatch; retry — transport may decompress next time
    return retryOnce(err)
}

Prevention

When it happens

Trigger: Response header Content-Encoding == gzip but resp.Body is not a valid gzip stream — e.g., double decompression by transport, corrupted body, or a mislabeled Content-Encoding header.

Common situations: Custom Transport that already decompresses while the header still says gzip, middleboxes/proxies mangling the response, or CDN sending inconsistent headers.

Understand the failure class

Background: "Invalid JSON response" and "Failed to parse response" errors: when an API answers 200 but the body isn't the JSON your library expected — this error's family across 28 libraries.

Related errors


AI-assisted analysis of fish2018/pansou@beaa561337 (2026-09-07). Data as JSON: /api/errors/7e27e1e02ce5989c. Report an issue: GitHub.

Appendix: source

Thrown at plugin/qupanshe/qupanshe.go:182

		if u, _ := url.Parse(BaseURL); u != nil {
			cookies := client.Jar.Cookies(u)
			fmt.Printf("[qupanshe] 从首页获取到 %d 个cookies:\n", len(cookies))
			for i, cookie := range cookies {
				fmt.Printf("  Cookie[%d]: %s=%s\n", i, cookie.Name, cookie.Value)
			}
		}
	}

	if resp.StatusCode != http.StatusOK {
		return "", fmt.Errorf("首页请求返回状态码: %d", resp.StatusCode)
	}

	// 处理可能的gzip压缩
	var reader io.Reader = resp.Body
	if resp.Header.Get("Content-Encoding") == "gzip" {
		gzipReader, err := gzip.NewReader(resp.Body)
		if err != nil {
			return "", fmt.Errorf("创建gzip读取器失败: %w", err)
		}
		defer gzipReader.Close()
		reader = gzipReader
	}

	// 解析HTML
	doc, err := goquery.NewDocumentFromReader(reader)
	if err != nil {
		return "", fmt.Errorf("解析HTML失败: %w", err)
	}

	// 查找formhash
	formhash := ""
	inputCount := doc.Find("input[name='formhash']").Length()
	if DebugLog {
		fmt.Printf("[qupanshe] 找到input[name='formhash']元素数量: %d\n", inputCount)
	}

View on GitHub (pinned to beaa561337)