decompress handles gzip and deflate Content-Encoding. Also auto-detects gzip/zlib by magic bytes if header is missing.
(data []byte, encoding string)
| 302 | // decompress handles gzip and deflate Content-Encoding. |
| 303 | // Also auto-detects gzip/zlib by magic bytes if header is missing. |
| 304 | func decompress(data []byte, encoding string) []byte { |
| 305 | if len(data) == 0 { |
| 306 | return data |
| 307 | } |
| 308 | |
| 309 | // Try by Content-Encoding header first. |
| 310 | switch strings.ToLower(encoding) { |
| 311 | case "gzip": |
| 312 | if d, err := decompressGzip(data); err == nil { |
| 313 | return d |
| 314 | } |
| 315 | case "deflate": |
| 316 | if d, err := decompressZlib(data); err == nil { |
| 317 | return d |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // Auto-detect by magic bytes (Sentry SDKs sometimes omit the header). |
| 322 | if len(data) >= 2 { |
| 323 | // Gzip magic: 0x1f 0x8b |
| 324 | if data[0] == 0x1f && data[1] == 0x8b { |
| 325 | if d, err := decompressGzip(data); err == nil { |
| 326 | return d |
| 327 | } |
| 328 | } |
| 329 | // Zlib magic: 0x78 (0x01, 0x5e, 0x9c, 0xda) |
| 330 | if data[0] == 0x78 { |
| 331 | if d, err := decompressZlib(data); err == nil { |
| 332 | return d |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | return data |
| 338 | } |
| 339 | |
| 340 | func decompressGzip(data []byte) ([]byte, error) { |
| 341 | r, err := gzip.NewReader(bytes.NewReader(data)) |