Skip to content

Commit 9fad09b

Browse files
committed
Add JsSIP.URI and JsSIP.NameAddrHeader classes
2 parents f4f9cf3 + 7b345bf commit 9fad09b

File tree

14 files changed

+608
-263
lines changed

14 files changed

+608
-263
lines changed

grunt.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ module.exports = function(grunt) {
2222
"src/Transport.js",
2323
"src/Parser.js",
2424
"src/SIPMessage.js",
25+
"src/URI.js",
26+
"src/NameAddrHeader.js",
2527
"src/Transactions.js",
2628
"src/Dialogs.js",
2729
"src/RequestSender.js",

src/Message.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ JsSIP.Message.prototype.send = function(target, body, contentType, options) {
4141
}
4242

4343
// Check target validity
44-
target = JsSIP.utils.normalizeUri(target, this.ua.configuration.domain);
44+
target = JsSIP.utils.normalizeURI(target, this.ua.configuration.domain);
4545
if (!target) {
4646
throw new JsSIP.exceptions.InvalidTargetError();
4747
}
4848

4949
// Message parameter initialization
5050
this.direction = 'outgoing';
51-
this.local_identity = this.ua.configuration.user;
51+
this.local_identity = this.ua.configuration.from_uri;
5252
this.remote_identity = target;
5353

5454
this.closed = false;
@@ -164,8 +164,8 @@ JsSIP.Message.prototype.init_incoming = function(request) {
164164

165165
this.direction = 'incoming';
166166
this.request = request;
167-
this.local_identity = request.s('to').uri;
168-
this.remote_identity = request.s('from').uri;
167+
this.local_identity = request.s('to').uri.toAor();
168+
this.remote_identity = request.s('from').uri.toAor();
169169

170170
if (contentType && (contentType.match(/^text\/plain(\s*;\s*.+)*$/i) || contentType.match(/^text\/html(\s*;\s*.+)*$/i))) {
171171
this.ua.emit('newMessage', this.ua, {

src/NameAddrHeader.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* @augments JsSIP
3+
* @class Class creating a Name Address SIP header.
4+
*
5+
* @param {JsSIP.URI} uri
6+
* @param {String} [display_name]
7+
* @param {Object} [parameters]
8+
*
9+
*/
10+
JsSIP.NameAddrHeader = function(uri, display_name, parameters) {
11+
var param;
12+
13+
// Checks
14+
if(!uri || !uri instanceof JsSIP.URI) {
15+
console.warn('Missing or invalid "uri" in NameAddrHeader');
16+
throw new JsSIP.exceptions.InvalidValueError();
17+
}
18+
19+
// Initialize parameters
20+
this.uri = uri;
21+
this.parameters = {};
22+
23+
for (param in parameters) {
24+
this.setParam(param, parameters[param]);
25+
}
26+
27+
Object.defineProperties(this, {
28+
display_name: {
29+
get: function() { return display_name; },
30+
set: function(value) {
31+
display_name = value;
32+
}
33+
}
34+
});
35+
};
36+
JsSIP.NameAddrHeader.prototype = {
37+
setParam: function(key, value) {
38+
if (key) {
39+
this.parameters[key.toLowerCase()] = (typeof value === 'undefined' || value === null)? null : value.toString();
40+
}
41+
},
42+
43+
getParam: function(key) {
44+
if(key) {
45+
return this.parameters[key.toLowerCase()];
46+
}
47+
},
48+
49+
hasParam: function(key) {
50+
if(key) {
51+
return this.parameters.hasOwnProperty(key.toLowerCase()) && true || false;
52+
}
53+
},
54+
55+
deleteParam: function(parameter) {
56+
parameter = parameter.toLowerCase();
57+
if (this.parameters.hasOwnProperty(parameter)) {
58+
delete this.parameters[parameter];
59+
}
60+
},
61+
62+
clearParams: function() {
63+
this.parameters = {};
64+
},
65+
66+
clone: function() {
67+
return new JsSIP.NameAddrHeader(
68+
this.uri.clone(),
69+
this.display_name,
70+
window.JSON.parse(window.JSON.stringify(this.parameters)));
71+
},
72+
73+
toString: function() {
74+
var body, parameter;
75+
76+
body = (this.display_name) ? '"' + this.display_name + '" ' : '';
77+
body += (this.display_name) ? '<' + this.uri.toString() + '>' : this.uri.toString();
78+
79+
80+
for (parameter in this.parameters) {
81+
body += ';' + parameter;
82+
body += (this.parameters[parameter] === null)? '' : '=' + this.parameters[parameter];
83+
}
84+
return body;
85+
}
86+
};

src/Parser.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ JsSIP.Parser = {
7373
parsed = message.parseHeader('from');
7474
if(parsed) {
7575
message.from = parsed;
76-
message.from_tag = parsed.tag;
76+
message.from_tag = parsed.getParam('tag');
7777
}
7878
break;
7979
case 'to':
@@ -82,7 +82,7 @@ JsSIP.Parser = {
8282
parsed = message.parseHeader('to');
8383
if(parsed) {
8484
message.to = parsed;
85-
message.to_tag = parsed.tag;
85+
message.to_tag = parsed.getParam('tag');
8686
}
8787
break;
8888
case 'record-route':
@@ -191,7 +191,7 @@ JsSIP.Parser = {
191191
} else if(!parsed.status_code) {
192192
message = new JsSIP.IncomingRequest();
193193
message.method = parsed.method;
194-
message.ruri = parsed;
194+
message.ruri = parsed.uri;
195195
} else {
196196
message = new JsSIP.IncomingResponse();
197197
message.status_code = parsed.status_code;

src/Registrator.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ JsSIP.Registrator.prototype = {
9494
while(contacts--) {
9595
contact = response.parseHeader('contact', contacts);
9696
if(contact.uri === this.ua.contact.uri) {
97-
expires = contact.params.expires;
97+
expires = contact.getParam('expires');
9898
break;
9999
}
100100
}
@@ -115,11 +115,11 @@ JsSIP.Registrator.prototype = {
115115
}, (expires * 1000) - 3000);
116116

117117
//Save gruu values
118-
if (contact.params['temp-gruu']) {
119-
this.ua.contact.temp_gruu = contact.params['temp-gruu'].replace(/"/g,'');
118+
if (contact.hasParam('temp-gruu')) {
119+
this.ua.contact.temp_gruu = contact.getParam('temp-gruu').replace(/"/g,'');
120120
}
121-
if (contact.params['pub-gruu']) {
122-
this.ua.contact.pub_gruu = contact.params['pub-gruu'].replace(/"/g,'');
121+
if (contact.hasParam('pub-gruu')) {
122+
this.ua.contact.pub_gruu = contact.getParam('pub-gruu').replace(/"/g,'');
123123
}
124124

125125
this.registered = true;

src/SIPMessage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ JsSIP.IncomingRequest.prototype.reply = function(code, reason, extraHeaders, bod
378378

379379
if(!this.to_tag) {
380380
to += ';tag=' + JsSIP.utils.newTag();
381-
} else if(this.to_tag && !this.s('to').tag) {
381+
} else if(this.to_tag && !this.s('to').hasParam('tag')) {
382382
to += ';tag=' + this.to_tag;
383383
}
384384

@@ -435,7 +435,7 @@ JsSIP.IncomingRequest.prototype.reply_sl = function(code, reason) {
435435

436436
if(!this.to_tag) {
437437
to += ';tag=' + JsSIP.utils.newTag();
438-
} else if(this.to_tag && !this.s('to').tag) {
438+
} else if(this.to_tag && !this.s('to').hasParam('tag')) {
439439
to += ';tag=' + this.to_tag;
440440
}
441441

src/SanityCheck.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ JsSIP.sanityCheck = (function() {
3636

3737
// Sanity Check functions for requests
3838
function rfc3261_8_2_2_1() {
39-
if(message.s('to').scheme !== 'sip') {
39+
if(message.s('to').uri.scheme !== 'sip') {
4040
reply(416);
4141
return false;
4242
}

src/Session.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ JsSIP.Session.prototype.connect = function(target, options) {
103103
}
104104

105105
// Check target validity
106-
target = JsSIP.utils.normalizeUri(target, this.ua.configuration.domain);
106+
target = JsSIP.utils.normalizeURI(target, this.ua.configuration.domain);
107107
if (!target) {
108108
throw new JsSIP.exceptions.InvalidTargetError();
109109
}
@@ -746,10 +746,10 @@ JsSIP.Session.prototype.newSession = function(originator, request, target) {
746746
session.direction = (originator === 'local') ? 'outgoing' : 'incoming';
747747

748748
if (originator === 'remote') {
749-
session.local_identity = request.s('to').uri;
750-
session.remote_identity = request.s('from').uri;
749+
session.local_identity = request.s('to').uri.toAor();
750+
session.remote_identity = request.s('from').uri.toAor();
751751
} else if (originator === 'local'){
752-
session.local_identity = session.ua.configuration.user;
752+
session.local_identity = session.ua.configuration.from_uri;
753753
session.remote_identity = target;
754754
}
755755

0 commit comments

Comments
 (0)