#58 URI construction function / transformation

closed
core (12)
5
2010-10-15
2010-08-16
No

I run a couple of times into situations where I need to change the URI a little bit. It ends up as a mess of if/else-s because the urls can have different protocols, may or may not contain a user and port, may have params, etc.

I propose a function `construct_uri(protocol, user, domain, port, params)` which returns a URI based on those arguments. The user, port, params parts should be optional, so it should be legal to call:

construct_uri("sip", null, "1.2.3.4", null, null) -> "sip:1.2.3.4"
construct_uri("sip", $rU, $rd, null, "key=val") -> "sip:$rU@$rd;key=val" (with expanded vars of course)
etc.

Discussion

  • Hi Stan,

    But what will be the benefit of
    construct_uri("sip", null, "1.2.3.4", null, null) -> "sip:1.2.3.4"
    over
    $ru = "sip:1.2.3.4";

    or of
    construct_uri("sip", $rU, $rd, null, "key=val") -> "sip:$rU@$rd;key=val"
    over
    $ru = :sip:"+$rU+"@" +$rd+";key=val"

    ?

    Regards,
    Bogdan

     
    • assigned_to: nobody --> bogdan_iancu
     
  • Optional fields are the problem. Let's say I want to change a domain and prepend a parameter - after splitting the original uri I have the relevant parts in different $var-s. To construct a new uri, I need many cases like:

    if ($var(user)) {
    if ($var(port))
    $var(result) = "sip:" + $var(user) + "@" + $var(domain) + ":" + $var(port) + $var(newparam) + $(params);
    else
    $var(result) = "sip:" + $var(user) + "@" + $var(domain) + $var(newparam) + $(params);
    } else {
    if ($var(port))
    $var(result) = "sip:" + $var(domain) + ":" + $var(port) + $var(newparam) + $(params);
    else
    $var(result) = "sip:" + $var(domain) + $var(newparam) + $(params);
    }

    I'd prefer to do something like this instead:

    $var(result) = construct_uri("sip",$var(user),$var(domain),$var(port) ,$var(newparam) + $(params);

     
    • assigned_to: bogdan_iancu --> vladut-paiu
     
    • status: open --> closed
     
  • Hello Stan,

    I have just commited a patch to the trunk that implements the functionality that you asked for. A small documentation as well as a usage example can be found at http://www.opensips.org/Resources/DocsCoreFcn#toc99

    Let me know if you encounter any problems.