Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make header read case insensitive, and keep case when proxying headers #165

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ module.exports = Request ;
delegate(Request.prototype, 'msg')
.method('get')
.method('has')
.method('getHeaderName')
.method('getParsedHeader')
.method('set')
.access('method')
Expand Down
1 change: 1 addition & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ module.exports = Response ;
delegate(Response.prototype, 'msg')
.method('get')
.method('has')
.method('getHeaderName')
.method('getParsedHeader')
.method('set')
.access('headers')
Expand Down
16 changes: 11 additions & 5 deletions lib/sip-parser/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class SipMessage {
return ('INVITE' === this.method || 'SUBSCRIBE' === this.method) && !this.get('to').tag ;
}

getHeaderName(hdr) {
const hdrLowerCase = hdr.toLowerCase();
return Object.keys(this.headers).find((h) => h.toLowerCase() === hdrLowerCase);
}

set(hdr, value) {
const hdrs = {} ;
if (typeof hdr === 'string') hdrs[hdr] = value ;
Expand All @@ -76,17 +81,18 @@ class SipMessage {
}

get(hdr) {
if (this.has(hdr)) { return this.headers[parser.getHeaderName(hdr)] ; }
const headerName = this.getHeaderName(parser.getHeaderName(hdr));
if (headerName) {
return this.headers[headerName];
}
}

has(hdr) {
const name = parser.getHeaderName(hdr) ;
return name in this.headers ;
return !!this.getHeaderName(hdr);
}

getParsedHeader(hdr) {
const name = parser.getHeaderName(hdr) ;
const v = this.headers[name];
const v = this.get(hdr);

if (!v) {
throw new Error('header not available');
Expand Down
29 changes: 22 additions & 7 deletions lib/srf.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,8 +814,14 @@ class Srf extends Emitter {
const reqHeaders = req.headers;
possiblyRemoveHeaders(proxyRequestHeaders.slice(1), reqHeaders);
copyAllHeaders(reqHeaders, opts.headers);
} else {
proxyRequestHeaders.forEach((hdr) => {
const headerName = req.getHeaderName(hdr);
if (headerName) {
opts.headers[headerName] = req.get(hdr);
}
});
}
else proxyRequestHeaders.forEach((hdr) => { if (req.has(hdr)) opts.headers[hdr] = req.get(hdr);}) ;

if (!(opts.headers.from || opts.headers.From) && !opts.callingNumber) { opts.callingNumber = req.callingNumber; }
if (!(opts.headers.from || opts.headers.From) && !opts.callingName) { opts.callingName = req.callingName; }
Expand Down Expand Up @@ -912,9 +918,10 @@ class Srf extends Emitter {
else {
proxyResponseHeaders.forEach((hdr) => {
debug(`copyUACHeadersToUAS: hdr ${hdr}`);
if (uacRes.has(hdr)) {
const headerName = uacRes.getHeaderName(hdr);
if (headerName) {
debug(`copyUACHeadersToUAS: adding ${hdr}: uacRes.get(hdr)`);
headers[hdr] = uacRes.get(hdr) ;
headers[headerName] = uacRes.get(hdr);
}
});
}
Expand Down Expand Up @@ -1221,17 +1228,25 @@ class Srf extends Emitter {
_b2bRequestWithinDialog(dlg, req, res, proxyRequestHeaders, proxyResponseHeaders, callback) {
callback = callback || noop ;
let headers = {} ;
proxyRequestHeaders.forEach((h) => {
if (req.has(h)) { headers[h] = req.get(h); }
proxyRequestHeaders.forEach((hdr) => {
const headerName = req.getHeaderName(hdr);
if (headerName) {
headers[headerName] = req.get(hdr);
}
}) ;
dlg.request({
method: req.method,
headers: headers,
body: req.body
}, (err, response) => {
headers = {} ;
proxyResponseHeaders.forEach((h) => {
if (!!response && response.has(h)) { headers[h] = response.get(h); }
proxyResponseHeaders.forEach((hdr) => {
if (!!response && response.has(hdr)) {
const headerName = response.getHeaderName(hdr);
if (headerName) {
headers[headerName] = response.get(hdr);
}
}
}) ;

if (err) {
Expand Down
10 changes: 10 additions & 0 deletions test/unit-tests/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ describe('Parser', function () {
msg.set('From', '<sip:daveh@localhost>;tag=1234');
msg.get('from').should.eql('<sip:daveh@localhost>;tag=1234');
});
it('getting a private header should be case insensitive', function () {
var msg = new SipMessage();
msg.set('P-Called-Party-ID', '"Dave" <sip:daveh@localhost>');
msg.get('p-called-party-id').should.eql('"Dave" <sip:daveh@localhost>');
});
it('getting a custom header should be case insensitive', function () {
var msg = new SipMessage();
msg.set('X-Foo', 'bar');
msg.get('x-foo').should.eql('bar');
});
it('should not parse a header when not available', function () {
var msg = new SipMessage();
should.throws(msg.getParsedHeader.bind(msg, 'contact'));
Expand Down