@@ -5,7 +5,7 @@ use super::{
55 DialogId ,
66} ;
77use crate :: {
8- rsip_ext:: { destination_from_request , extract_uri_from_contact} ,
8+ rsip_ext:: extract_uri_from_contact,
99 transaction:: {
1010 endpoint:: EndpointInnerRef ,
1111 key:: { TransactionKey , TransactionRole } ,
@@ -239,6 +239,7 @@ impl DialogInner {
239239 route_set. push ( Route :: from ( rr. value ( ) ) ) ;
240240 }
241241 }
242+ route_set. reverse ( ) ;
242243
243244 Ok ( Self {
244245 role,
@@ -472,8 +473,40 @@ impl DialogInner {
472473
473474 pub ( super ) async fn do_request ( & self , request : Request ) -> Result < Option < rsip:: Response > > {
474475 let method = request. method ( ) . to_owned ( ) ;
475- let destination =
476- destination_from_request ( & request) . or_else ( || self . initial_destination . clone ( ) ) ;
476+ // request destination determination
477+ // Route header first, then remote contact, then initial request destination
478+ let mut destination = None ;
479+ for h in request. headers . iter ( ) {
480+ if let Header :: Route ( route) = h {
481+ let dest = route
482+ . typed ( )
483+ . ok ( )
484+ . map ( |r| {
485+ r. uris ( )
486+ . first ( )
487+ . map ( |u| SipAddr :: try_from ( & u. uri ) . ok ( ) )
488+ . flatten ( )
489+ } )
490+ . flatten ( ) ;
491+ if dest. is_some ( ) {
492+ destination = dest;
493+ break ;
494+ }
495+ }
496+ }
497+
498+ if destination. is_none ( ) {
499+ destination = self
500+ . remote_contact
501+ . lock ( )
502+ . unwrap ( )
503+ . as_ref ( )
504+ . map ( |c| extract_uri_from_contact ( c. value ( ) ) . ok ( ) )
505+ . flatten ( )
506+ . map ( |u| SipAddr :: try_from ( & u) . ok ( ) )
507+ . flatten ( )
508+ . or_else ( || self . initial_destination . clone ( ) ) ;
509+ }
477510
478511 let key = TransactionKey :: from_request ( & request, TransactionRole :: Client ) ?;
479512 let mut tx = Transaction :: new_client ( key, request, self . endpoint_inner . clone ( ) , None ) ;
@@ -682,6 +715,7 @@ impl Dialog {
682715 Dialog :: ClientInvite ( d) => d. inner . to . lock ( ) . unwrap ( ) . clone ( ) ,
683716 }
684717 }
718+
685719 pub fn remote_contact ( & self ) -> Option < rsip:: Uri > {
686720 match self {
687721 Dialog :: ServerInvite ( d) => d
@@ -690,15 +724,15 @@ impl Dialog {
690724 . lock ( )
691725 . unwrap ( )
692726 . as_ref ( )
693- . map ( |c| c . uri ( ) . ok ( ) )
727+ . map ( |c| extract_uri_from_contact ( c . value ( ) ) . ok ( ) )
694728 . flatten ( ) ,
695729 Dialog :: ClientInvite ( d) => d
696730 . inner
697731 . remote_contact
698732 . lock ( )
699733 . unwrap ( )
700734 . as_ref ( )
701- . map ( |c| c . uri ( ) . ok ( ) )
735+ . map ( |c| extract_uri_from_contact ( c . value ( ) ) . ok ( ) )
702736 . flatten ( ) ,
703737 }
704738 }
0 commit comments