Fix WebSocket handshake failure for chargers missing Connection: Upgrade header#1984
Fix WebSocket handshake failure for chargers missing Connection: Upgrade header#1984vavallee wants to merge 5 commits into
Conversation
…ade header Some charger firmware (confirmed: Autel MaxiCharger US AC LW10-N14, firmware v1.38.00 / v1.22.00) sends a WebSocket opening handshake without the required `Connection: Upgrade` header. websockets 14+ strictly enforces this header in `ServerProtocol.accept()` and raises `InvalidUpgrade: missing Connection header`, causing the handshake to fail and the charger to be permanently unable to connect. Fix: add a `process_request` hook to the `websockets.serve()` call that injects `Connection: upgrade` when the header is absent, before the library's validation runs. A warning is logged so operators are aware and can chase a firmware update from the charger vendor. (cherry picked from commit 0a7e855)
… unit tests Makes the function directly importable for testing without needing to instantiate CentralSystem or mock websockets.serve. Adds two async unit tests: one verifying header injection + warning log when Connection is absent, one verifying the header is left intact when already present. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> (cherry picked from commit 4729f1b)
test_fix_missing_connection_header_leaves_existing_intact claimed "no warning is logged" in its docstring but never verified it. Added caplog fixture, wrapped the call in caplog.at_level(WARNING), and asserted no WARNING-level records were emitted. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> (cherry picked from commit 4247ac4)
Add missing blank lines after function definition (E302) and restructure docstring to have a one-line summary (D205). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> (cherry picked from commit bfdb4ee)
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughAdds ChangesWebSocket Connection header workaround
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Problem
Some charger firmware (e.g. Autel MaxiCharger US AC LW10-N14, fw v1.38.00/v1.22.00) omits the required
Connection: Upgradeheader from the WebSocket opening handshake.websockets14+ strictly validates this header and raisesInvalidUpgrade, so the affected chargers can never connect to the OCPP central system.Fix
Add a
process_requesthook (_fix_missing_connection_header) that injectsConnection: upgradebefore the library validates the handshake, only when the header is absent. Logs a warning recommending a firmware update. No effect on compliant chargers (covered by a test asserting no warning is logged when the header is present).Notes
api.pyis otherwise unchanged in that range so this applies cleanly.tests/test_api_paths.py.Summary by CodeRabbit