The "headers_more" module: Set and clear output headers...more than "add"!

Hi, folks!

After two days’ work, I’ve now got the “headers_more” module passing
the its own test suite. This is an enhanced version of the standard
“headers” module because it provides more utilities like resetting or
clearing output headers. Builtin headers, like “Content-Type”,
“Content-Length”, and “Server”, are also manipulatable :wink:

It also allows you to specify an optional HTTP status code criteria
using the -s option and an optional content type criteria using the -t
option.

Here’s some random use cases:

more_set_headers 'Server: my-server';

location /foo {
    more_set_headers 'X-MyHeader: blah' 'X-MyHeader2: foo';
    more_set_headers -t 'text/plain text/css' 'Content-Type: 

text/foo’;
more_set_headers -s ‘400 404 500 503’ ‘Foo: Bar’;
more_clear_headers ‘Transfer-Encoding’ ‘Content-Type’;
}

location /type {
    more_set_headers 'Content-Type: text/plain';
}

One important TODO is to allow variables within the new headers’
values. That’s where “headers_more” is “less” than the standard
“headers” module :stuck_out_tongue:

The Git source repository is here:

http://github.com/agentzh/headers-more-nginx-module

And you’re recommended to use the tarballs for the tagged releases:

http://github.com/agentzh/headers-more-nginx-module/downloads

Don’t hesitate to report any issues that you find :wink:

Enjoy!
-agentzh

On Tue, Nov 17, 2009 at 6:00 PM, agentzh [email protected] wrote:

One important TODO is to allow variables within the new headers’
values. That’s where “headers_more” is “less” than the standard
“headers” module :stuck_out_tongue:

Okay, I’ve just shamelessly stolen the implementation directly from
the standard “headers” module, and made variables-in-header-values
work as expected. For example,

set $my_server 'Super Star Server';
more_set_headers 'Server: $my_server';

This feature is now included in v0.02:

http://github.com/agentzh/headers-more-nginx-module/downloads

But another TODO comes into my mind: adding support for input headers!
Stay tuned :slight_smile:

Have fun!
-agentzh

Hey agentzh,

Very nice module. Good job and thanks!

On Wed, Nov 18, 2009 at 10:18 AM, agentzh [email protected] wrote:

But another TODO comes into my mind: adding support for input headers!
Stay tuned :slight_smile:

Okay, the input header support has finally landed in v0.05 after a few
hours’ big refactoring and hacking. Here’s some funny examples:

 location /foo {
       set $my_host 'my dog';
       more_set_input_headers 'Host: $my_host';
       more_set_input_headers -t 'text/plain' 'X-Foo: bah';

       # now $host and $http_host have their new values...
 }

The “input header filter” runs in the access phase. I’m not sure if
it’s completely right but this phase lets me pass all the related
tests in

http://github.com/agentzh/headers-more-nginx-module/blob/master/test/t/input.t

and

http://github.com/agentzh/headers-more-nginx-module/blob/master/test/t/vars.t

while other phases break things horribly :slight_smile:

Theoretically it should also work with subrequests. But unfortunately
the ngx_http_subrequest function in the core seems to be buggy
regarding r->headers_in handling IIRC. So be careful with that :slight_smile:

I’ll work on the wiki page for this module soon.

Enjoy!
-agentzh

agentzh Wrote:

Headers More | NGINX

Thanks Cliff W. for providing such an excellent
facility that makes
me much more motivated to write documentation :slight_smile:

Great work… this will be invaluable for conditionally adding keywords
like “public” to Cache-Control headers (which is necessary for clients
to persistently cache SSL resources in most browsers). Should
drastically simplify configuration files.

Does anyone know if there any plans to move the “headers_more” module
(or any of the others) into the mainline nginx code base? It would be
very nice to have this in a stable release someday…

Posted at Nginx Forum:

On Wed, Nov 18, 2009 at 3:48 PM, agentzh [email protected] wrote:

I’ll work on the wiki page for this module soon.

As promised, I’ve added detailed documentation to the following wiki
page:

http://wiki.nginx.org/NginxHttpHeadersMoreModule

Thanks Cliff W. for providing such an excellent facility that makes
me much more motivated to write documentation :slight_smile:

Cheers,
-agentzh