Skip to content

Instantly share code, notes, and snippets.

@bvisonl
Forked from mxs42/parse-sip-uri.js
Created May 28, 2020 15:56
Show Gist options
  • Save bvisonl/add693105077e1de406cab47da5cd0f5 to your computer and use it in GitHub Desktop.
Save bvisonl/add693105077e1de406cab47da5cd0f5 to your computer and use it in GitHub Desktop.
function parseUri(s) {
if(typeof s === 'object')
return s;
var re = /^\<?(sips?):(?:([^\s>:@]+)(?::([^\s@>]+))?@)?([\w\-\.]+)(?::(\d+))?((?:;[^\s=\?>;]+(?:=[^\s?\;]+)?)*)(?:\?(([^\s&=>]+=[^\s&=>]+)(&[^\s&=>]+=[^\s&=>]+)*))?$/;
var r = re.exec(s);
if(r) {
return {
schema: r[1],
user: r[2],
password: r[3],
host: r[4],
port: +r[5],
params: (r[6].match(/([^;=]+)(=([^;=]+))?/g) || [])
.map(function(s) { return s.split('='); })
.reduce(function(params, x) { params[x[0]]=x[1] || null; return params;}, {}),
headers: ((r[7] || '').match(/[^&=]+=[^&=]+/g) || [])
.map(function(s){ return s.split('=') })
.reduce(function(params, x) { params[x[0]]=x[1]; return params; }, {})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment