http-server icon indicating copy to clipboard operation
http-server copied to clipboard

Add `.headers` support

Open BigBlueHat opened this issue 8 years ago • 6 comments

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!

BigBlueHat avatar Apr 25 '17 19:04 BigBlueHat

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.

nbarbettini avatar Oct 23 '17 04:10 nbarbettini

I am sorry, I cannot understand how to use it. I added a .headers file under the dir, but it doesn't work.

LvChengbin avatar Jan 09 '18 02:01 LvChengbin

@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.

BigBlueHat avatar Jan 09 '18 15:01 BigBlueHat

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).

BigBlueHat avatar May 10 '18 13:05 BigBlueHat

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.

aggregate1166877 avatar May 22 '23 10:05 aggregate1166877