bwindels / wwwee

wwwee, the wee webserver: a small, robust, low-resource and fast web application server for low workloads (home server, personal cloud)
GNU General Public License v3.0
26 stars 1 forks source link

Support serving static files given as relative path to document root #14

Closed bwindels closed 5 years ago

bwindels commented 6 years ago

Right now the example file stream handler allocates memory to compose a path. This might be avoidable by getting an fd for the root directory (using ) (which could be passed into the handler? or cross-request state?) and then do file lookups with relative paths from the dir fd. Care would need to be taken to make sure that file lookups cant escape the root directory by using .. (& others?) in the relative path.

Would be used like so:

trait Path {
  fn open(&self, flags: libc::c_int) -> OwnedFd
}
// created at server startup, no need to mutate once created, can be put
// in stated shared across handlers
// Directory could also support reading the entries for example
// Internally it has an OwnedFd for it's file descriptor
let base = Directory::new("/var/www/")?;
//returns error if the given path would resolve outside of base
//returns struct RelativePath that implements Path which defines open
let path = base.sub_path("some/file.txt")?;
//does path.open(flags)? internally to return an OwnedFd
let reader = file::Reader::open(path);