Skip to content

Commit

Permalink
Improve reply() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmillan committed Nov 27, 2012
1 parent 73e4dcb commit a45293b
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/SIPMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,16 @@ JsSIP.IncomingRequest.prototype.reply = function(code, reason, extraHeaders, bod
r = 0,
v = 0;

code = code || null;
reason = reason || null;

// Validate code and reason values
if (!code || (code < 100 || code > 699)) {
throw new JsSIP.exceptions.InvalidValueError();
} else if (reason && typeof reason !== 'string' && !(reason instanceof String)) {
throw new JsSIP.exceptions.InvalidValueError();
}

reason = reason || JsSIP.c.REASON_PHRASE[code] || ' ';
extraHeaders = extraHeaders || [];

Expand All @@ -367,9 +377,7 @@ JsSIP.IncomingRequest.prototype.reply = function(code, reason, extraHeaders, bod
response += 'Via: ' + this.getHeader('via', v) + '\r\n';
}

response += 'Max-Forwards: ' + JsSIP.c.MAX_FORWARDS + '\r\n';

if(code !== 100 && !this.to_tag) {
if(!this.to_tag) {
to += ';tag=' + JsSIP.utils.newTag();
} else if(this.to_tag && !this.s('to').tag) {
to += ';tag=' + this.to_tag;
Expand All @@ -391,7 +399,7 @@ JsSIP.IncomingRequest.prototype.reply = function(code, reason, extraHeaders, bod
response += 'Content-Length: ' + length + '\r\n\r\n';
response += body;
} else {
response += "\r\n";
response += '\r\n';
}

this.server_transaction.receiveResponse(code, response, onSuccess, onFailure);
Expand All @@ -406,7 +414,17 @@ JsSIP.IncomingRequest.prototype.reply_sl = function(code, reason) {
var to, response,
vias = this.countHeader('via');

reason = reason || JsSIP.c.REASON_PHRASE[code] || ' ';
code = code || null;
reason = reason || null;

// Validate code and reason values
if (!code || (code < 100 || code > 699)) {
throw new JsSIP.exceptions.InvalidValueError();
} else if (reason && typeof reason !== 'string' && !(reason instanceof String)) {
throw new JsSIP.exceptions.InvalidValueError();
}

reason = reason || JsSIP.c.REASON_PHRASE[code] || '';

response = 'SIP/2.0 ' + code + ' ' + reason + '\r\n';

Expand All @@ -418,6 +436,8 @@ JsSIP.IncomingRequest.prototype.reply_sl = function(code, reason) {

if(!this.to_tag) {
to += ';tag=' + JsSIP.utils.newTag();
} else if(this.to_tag && !this.s('to').tag) {
to += ';tag=' + this.to_tag;
}

response += 'To: ' + to + '\r\n';
Expand Down

0 comments on commit a45293b

Please sign in to comment.