emikulic / darkhttpd

When you need a web server in a hurry.
https://unix4lyfe.org/darkhttpd/
ISC License
1.03k stars 83 forks source link

Add an option to serve a single file for all requests #23

Closed iliazeus closed 6 months ago

iliazeus commented 2 years ago

Often, I want to just share a single file over HTTP. Right now, we can only pass a directory to darkhttpd, and it will serve any file from that directory. I would love to be able to do darkhttpd ./my/single/file.html.

kugland commented 1 year ago

Why not create a directory with the single file you want to serve?

iliazeus commented 1 year ago

@kugland the same reason I don't just spin up nginx instead: it's way less convenient.

guest271314 commented 1 year ago

I have this use case as well. I just need to serve a single HTML page on localhost, which I request using window.open() on Chromium, then I register a ServiceWorker from a script in the HTML which is used to stream data to the Tab which opened the window. I don't even need darkhttpd to remain active once the single request is served. I can use Native Messaging to start the local server, ideally the server would terminate itself once the request is served - the script and ServiceWorker in the served HTML will remain active.

emikulic commented 1 year ago

I can kinda appreciate this from a simplicity and security standpoint - locking serving down to a single path. Who wants to implement it?

guest271314 commented 1 year ago

I am not an expert in C. Otherwise I would file a PR as this is exactly what I am looking for for my own projects in the browser and potentially as a base to import into QuickJS.

g-rden commented 6 months ago

I couldn't keep myself from trying to implement this. Here is my naive approach: https://github.com/g-rden/darkhttpd/commit/134f8258c888fbf3c4a3b8850aae122aca65683a

I am not sure what --chroot should do. Should it error, change to the directory the file is in or something else? Probably the second, but it could get quite annoying to get the directory name and the file name without the path.

kugland commented 6 months ago

I couldn't keep myself from trying to implement this. Here is my naive approach: g-rden@134f825

I am not sure what --chroot should do. Should it error, change to the directory the file is in or something else? Probably the second, but it could get quite annoying to get the directory name and the file name without the path.

You could open the file and keep the file handle, and then chroot to an empty directory.

g-rden commented 6 months ago

https://github.com/g-rden/darkhttpd/commit/b83124b449a30ec2a5cbd78b749d64bd71da8165 this implements the second option for --chroot. I think this makes the most sense.

emikulic commented 6 months ago

Nice work @g-rden, please open a PR!