Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing patterns for SSDP responses in Sonos #1108

Merged
merged 3 commits into from
Dec 12, 2023

Conversation

dljsjr
Copy link
Contributor

@dljsjr dljsjr commented Dec 8, 2023

The current way we parse the SSDP response headers can cause failures in discovering Sonos speakers with Unicode characters that require more than one byte for their representation, such as CJK characters.

The current parsing patterns use the '%g' character class, which does not match characters that don't fit in a single byte. While Lua itself is utf-8 compatible, and it is safe to use multi-code-point graphemes or clusters in Lua strings, the actual string library is byte-oriented for many operations. The pattern used currently tries to match each byte against the provided class, which causes it to fail to match and capture many headers.

We now use a parsing pass for header key/value pairs derived from the Luncheon library with bug fixes that is aware of the proper structure for HTTP Header key/values, which is the format that an SSDP response uses.

Copy link

github-actions bot commented Dec 8, 2023

Test Results

     55 files     347 suites   0s ⏱️
1 633 tests 1 633 ✔️ 0 💤 0
2 861 runs  2 861 ✔️ 0 💤 0

Results for commit 7e4e948.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented Dec 8, 2023

Minimum allowed coverage is 90%

Generated by 🐒 cobertura-action against 7e4e948

Copy link

github-actions bot commented Dec 8, 2023

Channel deleted.

@dljsjr
Copy link
Contributor Author

dljsjr commented Dec 11, 2023

Waiting for #1107 to fix tests.

@dljsjr dljsjr force-pushed the fix/sonos-discovery-non-ascii-name-failure branch from 051dfdb to 33e863f Compare December 11, 2023 20:45
The current way we parse the SSDP response headers can cause failures
in discovering Sonos speakers with unicode characters that require
more than one byte for their representation, such as CJK characters.

The current parsing patterns use the '`%g`' character class, which does
not parse characters that don't fit in a single byte; while Lua itself
is utf-8 compatible and it is safe to use multi-code-point graphemes or
clusters in Lua strings, the actual string library is byte-oriented for
many operations and the pattern used currently tries to match each byte
against the provided class.

'`.-`' works here instead, as it matches all bytes in a non-greedy way
(akin to '`.*?`' in PCRE).
@dljsjr dljsjr force-pushed the fix/sonos-discovery-non-ascii-name-failure branch from bab287d to 570b8af Compare December 11, 2023 20:49
@dljsjr dljsjr force-pushed the fix/sonos-discovery-non-ascii-name-failure branch from 570b8af to 4e19c74 Compare December 11, 2023 22:02
@dljsjr dljsjr requested a review from varzac December 12, 2023 15:39
Previous approach could have potentially accepted invalid headers. This
tweaks the parsing pattern to fail to find headers that don't coform to
the actual accepted structure of a header key.
@dljsjr dljsjr force-pushed the fix/sonos-discovery-non-ascii-name-failure branch from 4e19c74 to 7e4e948 Compare December 12, 2023 19:21
@dljsjr dljsjr merged commit c8038a4 into main Dec 12, 2023
10 checks passed
@dljsjr dljsjr deleted the fix/sonos-discovery-non-ascii-name-failure branch December 12, 2023 19:26
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.

4 participants