From ab8691aef13fe0be2831f3b5e1af23489230cdff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Hauge=20Bj=C3=B8rnskov?= <19725+henrikbjorn@users.noreply.github.com> Date: Thu, 1 Oct 2026 09:14:37 +0200 Subject: [PATCH 1/3] Decode the channel data in an outbound socket's connect reply FreeSWITCH URL-encodes the channel data it sends in reply to connect, as it does in event bodies. Response parses reply headers without decoding, so the first snapshot of every outbound session held raw values: route_plan as %7B%22type%22..., and +45... numbers as %2B45.... Later CHANNEL_DATA and execute-complete updates go through the event body and were decoded, so the bug showed only in values read before a verb ran. Response#decoded_headers decodes the values, and Outbound#run_session uses it for the connect reply only. Other replies' headers are not encoded. --- lib/librevox/listener/outbound.rb | 2 +- lib/librevox/protocol/response.rb | 7 +++++++ test/functional/librevox/listener/outbound_test.rb | 10 +++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/librevox/listener/outbound.rb b/lib/librevox/listener/outbound.rb index 26ff77e..dbb1cfc 100644 --- a/lib/librevox/listener/outbound.rb +++ b/lib/librevox/listener/outbound.rb @@ -19,7 +19,7 @@ def initialize(connection, **) end def run_session - @session = send_message("connect").headers + @session = send_message("connect").decoded_headers send_message "myevents" send_message "linger" diff --git a/lib/librevox/protocol/response.rb b/lib/librevox/protocol/response.rb index 6e2b484..e0c7f2a 100644 --- a/lib/librevox/protocol/response.rb +++ b/lib/librevox/protocol/response.rb @@ -40,6 +40,13 @@ def error? reply? && headers[:reply_text]&.start_with?("-ERR") end + # The headers with their values URL-decoded. FreeSWITCH URL-encodes the + # channel data in its reply to an outbound socket's `connect`, as it + # does in event bodies. Other replies' headers are not encoded. + def decoded_headers + @headers.transform_values { |value| URI::RFC2396_PARSER.unescape(value) } + end + private def parse_headers(headers) diff --git a/test/functional/librevox/listener/outbound_test.rb b/test/functional/librevox/listener/outbound_test.rb index 0523f26..1a41aec 100644 --- a/test/functional/librevox/listener/outbound_test.rb +++ b/test/functional/librevox/listener/outbound_test.rb @@ -21,7 +21,9 @@ def setup command_reply "Caller-Caller-Id-Number" => "8675309", "Unique-ID" => "1234", - "variable_some_var" => "some value" + "variable_some_var" => "some value", + "variable_route_plan" => "%7B%22a%22%3A1%7D", + "variable_from_number" => "%2B4512345678" command_reply "Reply-Text" => "+OK Events Enabled" command_reply "Reply-Text" => "+OK will linger" end @@ -43,6 +45,12 @@ def test_establishes_session_from_connect_reply assert_equal "8675309", @listener.session[:caller_caller_id_number] end + # FreeSWITCH URL-encodes the channel data in its connect reply. + def test_decodes_the_channel_data + assert_equal '{"a":1}', @listener.session[:variable_route_plan] + assert_equal "+4512345678", @listener.session[:variable_from_number] + end + def test_calls_session_initiated_after_handshake assert_includes @listener.hook_log, "session was initiated" end From 36cd63f5ca8459e53213b138e823b7730e9db30b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Hauge=20Bj=C3=B8rnskov?= <19725+henrikbjorn@users.noreply.github.com> Date: Thu, 1 Oct 2026 09:36:11 +0200 Subject: [PATCH 2/3] URL-decode only the events FreeSWITCH serializes FreeSWITCH URL-encodes exactly two things on the event socket, both with switch_event_serialize(..., SWITCH_TRUE): an event's header values (text/event-plain) and the channel data in the connect reply. Everything else (command/reply, api/response, log/data, disconnect notices) is written raw. Response decoded the header lines of any body, so a literal %2B in a log line would have come out as +. Bodies are now decoded only for text/event-plain. --- lib/librevox/protocol/response.rb | 5 ++++- test/unit/librevox/response_test.rb | 13 ++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/librevox/protocol/response.rb b/lib/librevox/protocol/response.rb index e0c7f2a..544035b 100644 --- a/lib/librevox/protocol/response.rb +++ b/lib/librevox/protocol/response.rb @@ -53,11 +53,14 @@ def parse_headers(headers) parse_kv(headers) end + # FreeSWITCH URL-encodes the header values of an event it serializes + # (text/event-plain), and nothing else it sends in a body: log/data and + # disconnect notices are raw. An event's own body is raw too. def parse_content(content) return content unless content.include?(":") headers, body = content.split("\n\n", 2) - parse_kv(headers, decode: true).merge(body: body || "") + parse_kv(headers, decode: event?).merge(body: body || "") end def parse_kv(string, decode: false) diff --git a/test/unit/librevox/response_test.rb b/test/unit/librevox/response_test.rb index 38ea27c..57f18d4 100644 --- a/test/unit/librevox/response_test.rb +++ b/test/unit/librevox/response_test.rb @@ -81,9 +81,16 @@ def test_url_decode_preserves_literal_plus assert_equal "hello+world", response.content[:some_header] end - def test_url_decode_non_event_content - response = Librevox::Protocol::Response.new("Content-Type: command/reply", "Reply-Text: %2BOK") - assert_equal "+OK", response.content[:reply_text] + # FreeSWITCH encodes only the events it serializes; log lines are raw. + def test_does_not_url_decode_non_event_content + response = Librevox::Protocol::Response.new("Content-Type: log/data", "Log-Level: 7\n\nrate is 100%2B") + assert_equal "7", response.content[:log_level] + assert_equal "rate is 100%2B", response.content[:body] + end + + def test_does_not_url_decode_a_reply_body + response = Librevox::Protocol::Response.new("Content-Type: command/reply", "Reply-Text: 100%2B") + assert_equal "100%2B", response.content[:reply_text] end def test_does_not_url_decode_headers From b224c0326ab77db5ab6d2e8965a1e1f75cdb5d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Hauge=20Bj=C3=B8rnskov?= <19725+henrikbjorn@users.noreply.github.com> Date: Thu, 1 Oct 2026 10:28:54 +0200 Subject: [PATCH 3/3] Recognize the connect reply by its content, in Response A reply that FreeSWITCH builds from a whole event carries Event-Name; its reply to connect is the channel data (Event-Name: CHANNEL_DATA). Response now URL-decodes the headers of such a reply, so the outbound listener no longer has to know which reply to decode, and decoded_headers goes. --- lib/librevox/listener/outbound.rb | 2 +- lib/librevox/protocol/response.rb | 15 +++++++-------- .../functional/librevox/listener/outbound_test.rb | 3 ++- test/unit/librevox/response_test.rb | 7 +++++++ 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/librevox/listener/outbound.rb b/lib/librevox/listener/outbound.rb index dbb1cfc..26ff77e 100644 --- a/lib/librevox/listener/outbound.rb +++ b/lib/librevox/listener/outbound.rb @@ -19,7 +19,7 @@ def initialize(connection, **) end def run_session - @session = send_message("connect").decoded_headers + @session = send_message("connect").headers send_message "myevents" send_message "linger" diff --git a/lib/librevox/protocol/response.rb b/lib/librevox/protocol/response.rb index 544035b..a6f704c 100644 --- a/lib/librevox/protocol/response.rb +++ b/lib/librevox/protocol/response.rb @@ -40,17 +40,16 @@ def error? reply? && headers[:reply_text]&.start_with?("-ERR") end - # The headers with their values URL-decoded. FreeSWITCH URL-encodes the - # channel data in its reply to an outbound socket's `connect`, as it - # does in event bodies. Other replies' headers are not encoded. - def decoded_headers - @headers.transform_values { |value| URI::RFC2396_PARSER.unescape(value) } - end - private + # Headers are raw, except in a reply FreeSWITCH builds from a whole + # event: its reply to an outbound socket's `connect` is the channel data + # (Event-Name: CHANNEL_DATA), URL-encoded like any event it serializes. def parse_headers(headers) - parse_kv(headers) + parsed = parse_kv(headers) + return parsed unless parsed.key?(:event_name) + + parse_kv(headers, decode: true) end # FreeSWITCH URL-encodes the header values of an event it serializes diff --git a/test/functional/librevox/listener/outbound_test.rb b/test/functional/librevox/listener/outbound_test.rb index 1a41aec..bade7b9 100644 --- a/test/functional/librevox/listener/outbound_test.rb +++ b/test/functional/librevox/listener/outbound_test.rb @@ -19,7 +19,8 @@ def setup @listener = OutboundTestListener.new(MockConnection.new) @session_task = Async { @listener.run_session } - command_reply "Caller-Caller-Id-Number" => "8675309", + command_reply "Event-Name" => "CHANNEL_DATA", + "Caller-Caller-Id-Number" => "8675309", "Unique-ID" => "1234", "variable_some_var" => "some value", "variable_route_plan" => "%7B%22a%22%3A1%7D", diff --git a/test/unit/librevox/response_test.rb b/test/unit/librevox/response_test.rb index 57f18d4..8f5891b 100644 --- a/test/unit/librevox/response_test.rb +++ b/test/unit/librevox/response_test.rb @@ -93,6 +93,13 @@ def test_does_not_url_decode_a_reply_body assert_equal "100%2B", response.content[:reply_text] end + # FreeSWITCH's reply to connect is the channel data, a serialized event. + def test_url_decodes_the_headers_of_a_reply_that_is_an_event + response = Librevox::Protocol::Response.new("Content-Type: command/reply\nEvent-Name: CHANNEL_DATA\nCaller-Caller-ID-Number: %2B4512345678", "") + assert_equal "+4512345678", response.headers[:caller_caller_id_number] + assert_equal "command/reply", response.headers[:content_type] + end + def test_does_not_url_decode_headers response = Librevox::Protocol::Response.new("Content-Type: text%2Fevent-plain", "") assert_equal "text%2Fevent-plain", response.headers[:content_type]