Skip to content

Instantly share code, notes, and snippets.

@mxs42
Created May 15, 2018 23:03
Show Gist options
  • Save mxs42/84112fdf2d24dcfd28cfe503cb278a67 to your computer and use it in GitHub Desktop.
Save mxs42/84112fdf2d24dcfd28cfe503cb278a67 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