Skip to content
Open
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
15 changes: 12 additions & 3 deletions httprewrite/rawbody.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@ func getRawBodyReader(res *http.Response) (body io.ReadCloser, mimeType string,
contentType := res.Header.Get("Content-Type")
mimeType, params, err := mime.ParseMediaType(contentType)
if err != nil {
return nil, "", fmt.Errorf("parse content type %q: %v", contentType, err)
mimeType = "text/plain" // Fallback
}
if encoding == "" && strings.ToLower(params["charset"]) == "utf-8" {
// The body is already UTF-8 encoded and not compressed.

// [FIX 1] Treat missing charset as UTF-8 (Modern Web Default)
// If encoding is empty and no charset is specified, pass the body through raw.
charsetParam := strings.ToLower(params["charset"])
if encoding == "" && (charsetParam == "utf-8" || charsetParam == "") {
return res.Body, mimeType, nil
}

Expand All @@ -92,6 +95,12 @@ func getRawBodyReader(res *http.Response) (body io.ReadCloser, mimeType string,
return nil, "", fmt.Errorf("create decompressed reader for encoding %q: %v", encoding, err)
}

// [FIX 2] If we reach here (because of compression), ensure we don't
// let charset.NewReader default to Windows-1252 if charset is missing.
if charsetParam == "" {
contentType = mimeType + "; charset=utf-8"
Copy link
Member

Choose a reason for hiding this comment

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

}

decodedReader, err := charset.NewReader(decompressedReader, contentType)
if err != nil {
decompressedReader.Close()
Expand Down