Skip to content

Commit

Permalink
supporting serving an index.html for directories
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinNowak committed Nov 24, 2014
1 parent f29f0ce commit 70f29e4
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions source/vibe/http/fileserver.d
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,18 @@ HTTPServerRequestDelegate serveStaticFiles(Path local_path, HTTPFileServerSettin
logDebug("path '%s' not starting with '%s'", srv_path, settings.serverPathPrefix);
return;
}

auto rel_path = srv_path[settings.serverPathPrefix.length .. $];
auto rpath = Path(rel_path);
logTrace("Processing '%s'", srv_path);

rpath.normalize();
logDebug("Path '%s' -> '%s'", rel_path, rpath.toNativeString());
if (rpath.empty) {
// TODO: support searching for an index file
return;
} else if (rpath.absolute) {
if (rpath.absolute) {
logDebug("Path is absolute, not responding");
return;
} else if (rpath[0] == "..") return; // don't respond to relative paths outside of the root path
} else if (!rpath.empty && rpath[0] == "..")
return; // don't respond to relative paths outside of the root path

sendFile(req, res, local_path ~ rpath, settings);
}
Expand Down Expand Up @@ -139,8 +137,9 @@ class HTTPFileServerSettings {
string serverPathPrefix = "/";
Duration maxAge;// = hours(24);
bool failIfNotFound = false;
bool serveIndex = true;
string[string] encodingFileExtension;

/**
Called just before headers and data are sent.
Allows headers to be customized, or other custom processing to be performed.
Expand All @@ -162,7 +161,7 @@ class HTTPFileServerSettings {
this();
serverPathPrefix = path_prefix;
}
}
}


private void sendFile(HTTPServerRequest req, HTTPServerResponse res, Path path, HTTPFileServerSettings settings)
Expand All @@ -182,6 +181,7 @@ private void sendFile(HTTPServerRequest req, HTTPServerResponse res, Path path,
}

if (dirent.isDirectory) {
if( settings.serveIndex ) return sendFile(req, res, path ~ "index.html", settings);
logDebugV("Hit directory when serving files, ignoring: %s", pathstr);
if( settings.failIfNotFound ) throw new HTTPStatusException(HTTPStatus.NotFound);
else return;
Expand All @@ -190,7 +190,7 @@ private void sendFile(HTTPServerRequest req, HTTPServerResponse res, Path path,
auto lastModified = toRFC822DateTimeString(dirent.timeModified.toUTC());
// simple etag generation
auto etag = "\"" ~ hexDigest!MD5(pathstr ~ ":" ~ lastModified ~ ":" ~ to!string(dirent.size)).idup ~ "\"";

res.headers["Last-Modified"] = lastModified;
res.headers["Etag"] = etag;
if (settings.maxAge > seconds(0)) {
Expand Down Expand Up @@ -221,7 +221,7 @@ private void sendFile(HTTPServerRequest req, HTTPServerResponse res, Path path,
res.headers.remove("Content-Encoding");
res.headers["Content-Type"] = mimetype;
res.headers["Content-Length"] = to!string(dirent.size);

// check for already encoded file if configured
string encodedFilepath;
auto pce = "Content-Encoding" in res.headers;
Expand Down Expand Up @@ -251,18 +251,18 @@ private void sendFile(HTTPServerRequest req, HTTPServerResponse res, Path path,
encodedFilepath = null;
}
}

if(settings.preWriteCallback)
settings.preWriteCallback(req, res, pathstr);

// for HEAD responses, stop here
if( res.isHeadResponse() ){
res.writeVoidBody();
assert(res.headerWritten);
logDebug("sent file header %d, %s!", dirent.size, res.headers["Content-Type"]);
return;
}

// else write out the file contents
//logTrace("Open file '%s' -> '%s'", srv_path, pathstr);
FileStream fil;
Expand Down

0 comments on commit 70f29e4

Please sign in to comment.