Skip to content

Conversation

@bdraco
Copy link
Member

@bdraco bdraco commented Aug 14, 2025

Problem

When using the aiohttp transport with ONVIF cameras that send gzip-compressed responses (e.g., Pelco cameras), users encounter a zlib decompression error:

zlib.error: Error -3 while decompressing data: incorrect header check

This issue started appearing in Home Assistant 2025.7.x after switching from httpx to aiohttp for the ONVIF integration.

Root Cause

The issue occurs because:

  1. aiohttp automatically decompresses gzip content when it sees the Content-Encoding: gzip header in the response
  2. The header was still being passed to zeep in the converted httpx/requests Response object
  3. zeep attempts to decompress again, trying to decompress already-decompressed content, resulting in the zlib error

Solution

This PR removes the Content-Encoding header when converting aiohttp responses to httpx.Response or requests.Response objects, since aiohttp has already handled the decompression.

Changes Made

  1. Added _filter_headers() helper method to remove the Content-Encoding header while preserving all other headers (including duplicates)
  2. Updated _aiohttp_to_httpx_response() to use the filter
  3. Updated _aiohttp_to_requests_response() to use the filter
  4. Refactored to eliminate code duplication:
    • Created _post_internal() method to share POST logic between post() and post_xml()
    • post_xml() now directly returns a requests.Response, avoiding unnecessary aiohttp→httpx→requests conversion
    • Removed unused _httpx_to_requests_response() method
  5. Added comprehensive tests to verify the fix and ensure duplicate headers are preserved

Testing

Two new tests have been added:

  • test_gzip_content_encoding_header_removed: Verifies that the Content-Encoding: gzip header is removed after aiohttp decompresses the content, preventing double decompression attempts
  • test_multiple_duplicate_headers_preserved: Ensures that other headers, including duplicates (e.g., multiple Set-Cookie headers), are correctly preserved

All existing tests continue to pass, confirming backward compatibility.

Impact

This fix resolves connectivity issues with ONVIF cameras that send gzip-compressed responses, including:

  • Pelco cameras (confirmed via packet capture analysis)
  • 2N IP Verso devices
  • Any other ONVIF device sending gzip-encoded SOAP responses

Related Issues

Verification

The fix has been verified with:

  • Packet capture analysis showing Pelco cameras send responses with Content-Encoding: gzip
  • Test script simulating the exact response pattern from affected cameras
  • Comprehensive unit tests covering both the fix and edge cases

@codecov
Copy link

codecov bot commented Aug 14, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
onvif/zeep_aiohttp.py 96.62% <100.00%> (-0.22%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bdraco bdraco marked this pull request as ready for review August 14, 2025 21:41
@bdraco bdraco merged commit 2761842 into async Aug 14, 2025
6 checks passed
@bdraco bdraco deleted the fix_double_decompress branch August 14, 2025 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants