Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/librevox/protocol/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,24 @@ def error?

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
# (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)
Expand Down
13 changes: 11 additions & 2 deletions test/functional/librevox/listener/outbound_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ 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_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
Expand All @@ -43,6 +46,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
Expand Down
20 changes: 17 additions & 3 deletions test/unit/librevox/response_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,23 @@ 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

# 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
Expand Down