Add `.headers` support
The .headers file idea comes from the File Handler in wptserve (a Python-based server for running the Web Platform Tests system at the W3C).
They look like this:
Content-Type: text/html
In wptserve they can be associated with single files (index.html.headers or index.headers) as well as directories (for /about/ style requests) via a __dir__.headers file (because Python-esque).
The advantage is that you can then provide any number of HTTP headers for any file or "directory" or perhaps even non-resource requests such as handling HEAD-only requests or 303 See Also specific entities.
This could be used to address a number of issues related mentioned here related to caching and content-types as well as things like advanced CORS and CSP handling.
Thoughts welcome!
I'd love to see this merged into http-server. It's harder than I expected to find a simple Node HTTP server that lets me set custom headers easily.
I am sorry, I cannot understand how to use it. I added a .headers file under the dir, but it doesn't work.
@LvChengbin it's not implemented yet, but there seems to be enough interest in it to do the work now.
@nbarbettini thanks for the +1. I'd been a bit concerned that this changed the nature of http-server a bit, but given that these are optional files that augment requests, I think it's fine--though I'll still probably put it behind an optional command line flag to avoid the double-disk checking for each request if it's not in use.
Netlify uses a top-level _headers file with path mapping:
https://www.netlify.com/docs/headers-and-basic-auth/
Having the header files closer to the actual requested file seems better, but a single top-level file does avoid the double-file disk reads per each request (so that's a good).
Out of desperation I've forked all this and merged hedefalk's 7 year old version with the latest stuff here.
It's a temporary hack solution and I'm likely not going to merge future changes into that, so it's officially unmaintained, but it's a workaround for the desperate like me until the authors merge in hedefalk's changes.