Skip to content
Open
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 History.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 🐞 Bug fixes

- Fixed HTTP header conflict between Content-Length and Transfer-Encoding in res.send - by [@YuryShkoda](https://github.com/YuryShkoda) in [#4893](https://github.com/expressjs/express/pull/4893)
- Fixed `res.links()` producing a trailing comma in the `Link` header when a rel value is an empty array - by [@JSap0914](https://github.com/JSap0914)


Fixed the behavior of `res.send()` to prevent conflicts between `Content-Length` and `Transfer-Encoding` HTTP headers in responses. The `Content-Length` header in `res.send()` is now only added when a `Transfer-Encoding` header is not present, complying with the HTTP specification that states both headers should not coexist in the same response
Expand Down
8 changes: 5 additions & 3 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ res.status = function status(code) {

res.links = function(links) {
var link = this.get('Link') || '';
if (link) link += ', ';
return this.set('Link', link + Object.keys(links).map(function(rel) {
var newLinks = Object.keys(links).map(function(rel) {
// Allow multiple links if links[rel] is an array
if (Array.isArray(links[rel])) {
return links[rel].map(function (singleLink) {
Expand All @@ -107,7 +106,10 @@ res.links = function(links) {
} else {
return `<${links[rel]}>; rel="${rel}"`;
}
}).join(', '));
}).filter(Boolean).join(', ');

if (link && newLinks) link += ', ';
return this.set('Link', link + newLinks);
};

/**
Expand Down
33 changes: 33 additions & 0 deletions test/res.links.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,38 @@ describe('res', function(){
.expect('Link', '<http://api.example.com/users?page=2>; rel="next", <http://api.example.com/users?page=5>; rel="last", <http://api.example.com/users?page=1>; rel="last"')
.expect(200, done);
})

it('should skip empty-array rel values without producing trailing commas', function (done) {
var app = express();

app.use(function (req, res) {
res.links({
next: 'http://api.example.com/users?page=2',
prev: []
});

res.end();
});

request(app)
.get('/')
.expect('Link', '<http://api.example.com/users?page=2>; rel="next"')
.expect(200, done);
})

it('should not set a trailing comma when appending and new rel values are all empty arrays', function (done) {
var app = express();

app.use(function (req, res) {
res.links({ next: 'http://api.example.com/users?page=2' });
res.links({ prev: [] });
res.end();
});

request(app)
.get('/')
.expect('Link', '<http://api.example.com/users?page=2>; rel="next"')
.expect(200, done);
})
})
})