diff --git a/lib/oversip/modules/outbound_mangling.rb b/lib/oversip/modules/outbound_mangling.rb index c8a9bef..0294d16 100644 --- a/lib/oversip/modules/outbound_mangling.rb +++ b/lib/oversip/modules/outbound_mangling.rb @@ -6,15 +6,27 @@ module OutboundMangling @log_id = "OutboundMangling module" - def self.add_outbound_to_contact request - if request.contact and request.connection_outbound_flow_token - log_system_debug "performing Contact mangling (adding ;ov-ob Outbound param) for #{request.log_id}" if $oversip_debug + def self.add_outbound_to_contact proxy + unless proxy.is_a? ::OverSIP::SIP::Proxy + raise ::OverSIP::RuntimeError, "proxy must be a OverSIP::SIP::Proxy instance" + end - # Add the ;ov-ob param to the Contact URI. - request.contact.set_param "ov-ob", request.connection_outbound_flow_token - return true - else - return false + proxy.on_target do |target| + request = proxy.request + # Just act in case the request has a single Contact, its connection uses Outbound + # and no ;ov-ob param exists in Contact URI. + if request.contact and request.connection_outbound_flow_token and not request.contact.has_param? "ov-ob" + log_system_debug "performing Contact mangling (adding ;ov-ob Outbound param) for #{request.log_id}" if $oversip_debug + + request.contact.set_param "ov-ob", request.connection_outbound_flow_token + + proxy.on_success_response do |response| + if (contacts = response.headers["Contact"]) + log_system_debug "reverting original Contact value (removing ;ov-ob Outbound param) from response" if $oversip_debug + contacts.each { |contact| contact.gsub! /;ov-ob=[_\-0-9A-Za-z]+/, "" } + end + end + end end end @@ -39,22 +51,6 @@ def self.extract_outbound_from_ruri request end end - def self.remove_outbound_from_contact message - unless message.is_a? ::OverSIP::SIP::Message - raise ::OverSIP::RuntimeError, "message must be a OverSIP::SIP::Request or OverSIP::SIP::Response" - end - - if (contacts = message.headers["Contact"]) - log_system_debug "reverting original Contact value (removing ;ov-ob Outbound param) for response" if $oversip_debug - contacts.each do |contact| - contact.gsub! /;ov-ob=[_\-0-9A-Za-z]+/, "" - end - return true - else - return false - end - end - end # module OutboundMangling end