-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
http: introduce getAllHeaders()
#772
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -337,6 +337,14 @@ OutgoingMessage.prototype.getHeader = function(name) { | |
}; | ||
|
||
|
||
OutgoingMessage.prototype.getAllHeaders = function() { | ||
if (!this._headers) | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. I think it was just copied from I had taken it because I thought it was done so that the api was closer to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds reasonable. I'll keep it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean that I always should check a type of the returning value? Are there cases when you need to check that at least one header was set? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now the method returns one of two types: Object with headers or undefined. And second one does not solve the problem of caller. I want all the headers, but it said "no headers yet", but it can just return an empty set. The method have dual meaning, now it is hard to override this with saving exisiting semantics. Also this behaviour here are forcing a caller to be more complex, because it needed to handle unexpected behaviour Example res.getAllHeaderNames = function () {
var headers = this.getAllHeaders();
if (!headers) {
// 1. Why should I do this check?
// 2. Can I return [] or i should inherit the behaviour?
return undefined;
}
return Object.keys(headers);
};
res.getContentHeaderNames = function () {
var names = this.getAllHeaderNames();
if (!names) {
// again! It haunting me!
return undefined;
}
return names.filter(...);
}; Also inspected https://github.com/iojs/io.js/blob/v1.x/lib/_http_outgoing.js : there are tons of |
||
else | ||
return util._extend({}, this._headers); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think it might be worth using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, why not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh. We should use whatever There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The performance hit is unfortunate. Anyone know if v8 is working on making that better? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @chrisdickinson Of course. The discussion is weather to extend There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, V8 really wants you to use |
||
}; | ||
|
||
|
||
OutgoingMessage.prototype.removeHeader = function(name) { | ||
if (arguments.length < 1) { | ||
throw new Error('`name` is required for removeHeader(name).'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var common = require('../common'); | ||
var assert = require('assert'); | ||
var http = require('http'); | ||
|
||
var s = http.createServer(function(req, res) { | ||
var contentType = 'content-type'; | ||
var plain = 'text/plain'; | ||
res.setHeader(contentType, plain); | ||
assert.ok(!res.headersSent); | ||
res.writeHead(200); | ||
assert.ok(res.headersSent); | ||
res.end('hello world\n'); | ||
// This checks that after the headers have been sent, getHeader works | ||
// and does not throw an exception (joyent/node Issue 752) | ||
assert.doesNotThrow( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the |
||
function() { | ||
assert.deepStrictEqual({ 'content-type': 'text/plain' }, res.getAllHeaders()); | ||
} | ||
); | ||
}); | ||
|
||
s.listen(common.PORT, runTest); | ||
|
||
function runTest() { | ||
http.get({ port: common.PORT }, function(response) { | ||
response.on('end', function() { | ||
s.close(); | ||
}); | ||
response.resume(); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that are
should bethat have