Skip to content

http: Add OutgoingMessage#addHeader

Rodrigo Muino Tomonari requested to merge github/fork/awwright/addHeader into master

On occasion I've needed to append an HTTP header instead of set one. For example, Link and Set-Cookie often specify message headers in multiple places, all of which need to appear in the message.

Doing this with setHeader is awkward, requiring three different branches, depending on the current value of the header. Here is a simple polyfill:

if(!OutboundMessage.prototype.addHeader)
OutboundMessage.prototype.addHeader = function addHeader(n, v){
	const u = res.getHeader(n);
	if(u === undefined) res.setHeader(n, v);
	else if(typeof w === 'string') res.setHeader(n, [u, v] );
	else res.setHeader(n, u.concat([v]) );
}

It seems to me that userland code shouldn't have to concern itself with the current state of the headers if it doesn't need to, therefore, there should be a function that adds a new header (in addition to any existing).

I don't have documentation started yet, but this is a fairly straightforward feature; and this needs an http2 implementation. I can get started on those, if this feature looks reasonable.

Note this will re-use the capitalization of the existing header, if it exists.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

Merge request reports

Loading