Open GoogleCodeExporter opened 9 years ago
You can replace the following code instead of the existing
Message.prototype._parse:
function readBlob(value, m) {
var start = 0;
var stop = value.size - 1;
var reader = new FileReader();
// If we use onloadend, we need to check the readyState.
reader.onloadend = function(evt) {
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
handleMessage(evt.target.result, m);
}
};
var blob = value.slice(start, stop + 1);
reader.readAsBinaryString(blob);
}
Message.prototype._parse = function(value) {
readBlob(value, this);
}
function handleMessage(value, m) {
var indexCRLFCRLF = value.indexOf("\r\n\r\n");
var indexLFLF = value.indexOf("\n\n");
var firstheaders, body;
if (indexCRLFCRLF >=0 && indexLFLF >= 0) {
// use lower value
if (indexCRLFCRLF < indexLFLF)
indexLFLF = -1;
else
indexCRLFCRLF = -1;
}
else if (indexCRLFCRLF < 0 && indexLFLF < 0) {
log("Message.parse() did not find LFLF or CRLFCRLF");
}
if (indexCRLFCRLF >= 0) {
firstheaders = value.substr(0, indexCRLFCRLF);
body = value.substr(indexCRLFCRLF+4);
}
else if (indexLFLF >= 0) {
firstheaders = value.substr(0, indexLFLF);
body = value.substr(indexLFLF+2);
}
else {
firstheaders = value;
body = ''; // no body
}
var firstline, headers;
var indexLF = firstheaders.indexOf("\n");
if (indexLF > 0 && firstheaders.charAt(indexLF-1) == "\r") {
firstline = firstheaders.substr(0, indexLF-1);
headers = firstheaders.substr(indexLF+1);
}
else if (indexLF > 0) {
firstline = firstheaders.substr(0, indexLF);
headers = firstheaders.substr(indexLF+1);
}
var parts = firstline.split(" ");
if (parts.length < 3) {
throw new String("not enough parts in first line");
}
if (parts[1].match(/^\d+$/)) {
this.protocol = parts.shift();
this.response = parseInt(parts.shift());
this.responsetext = parts.join(" ");
}
else if (parts.length > 3) {
throw new String("invalid number of parts in request line");
}
else {
this.method = parts[0];
this.uri = new sip.URI(parts[1]);
this.protocol = parts[2];
}
parts = headers.split("\n");
for (var i=0; i<parts.length; ++i) {
var h = parts[i];
if (h && h.charAt(h.length-1) == "\r") {
h = h.substr(0, h.length-1);
}
if (h.charAt(0) == " " || h.charAt(0) == "\t") {
// need to handle the line folding
}
try {
var hdrs = sip.Header.createHeaders(h);
var name = hdrs[0];
var values = hdrs[1];
if (!m.hasItem(name)) {
m.setItem(name, values.length > 1 ? values : values[0])
}
else if (Message._single.indexOf(name) < 0) {
var existing = m.getItem(name);
if (!sip.is_array(existing)) {
m.setItem(name, [existing]);
}
existing = m.getItem(name);
for (var j=0; j<values.length; ++j) {
existing.push(values[j]);
}
}
}
catch (e) {
log("error parsing " + h);
if (e['stack'] !== undefined)
log(e.stack);
else
log(e);
continue;
}
}
var bodyLen = (m.hasItem('Content-Length') ? parseInt(m.getItem('Content-Length').value) : 0);
if (body) {
m.setBody(body);
if (bodyLen != body.length) {
throw new String("invalid content length " + bodyLen + " != " + body.length);
}
}
var mandatory = ["To", "From", "CSeq", "Call-ID"];
for (var k=0; k<mandatory.length; ++k) {
if (!m.hasItem(mandatory[k])) {
throw new String("mandatory header " + mandatory[k] + " is missing");
}
}
};
Original comment by shahar.b...@gmail.com
on 16 Jan 2013 at 9:32
Original issue reported on code.google.com by
mel...@gmail.com
on 6 Dec 2012 at 12:53