//#!/usr/bin/rdmd import std.stdio, std.path, std.process, std.file, std.array, std.string, std.algorithm, std.datetime, std.ascii, std.regex; import config; string dweb_root; string indent = ""; void html(string s) { writeln(indent ~ s); } void html_pop(string s) { indent = indent[0..max(0, $-4)]; html(s); } void html_push(string s) { html(s); indent ~= " "; } void write_link(string to, bool expand) { bool isdir = dirExists(dweb_root ~ "/srv/" ~ to[url_root.length..$]); html_push(""); html("" ~ baseName(to) ~ (isdir ? "/" : "") ~ ""); html_pop(""); } void nav_tree_r(string url, string cur_loc, string[] subdirs) { string[] dirs = array(map!"a.name"(dirEntries(dweb_root ~ "/srv/" ~ cur_loc, SpanMode.shallow))); sort(dirs); if (dirs.length == 0) return; bool inserted_ul = false; bool next = false; string next_loc; foreach(string s; dirs) { s= s[(dweb_root ~ "/srv/").length..$]; string name = stripExtension(baseName(s)); if (name.length == 0) continue; // e.g. ".md", should we do something else with these files? if (name == "index") continue; // "index" will never appear in the nav_tree. if (name[0] == '@') continue; // hidden file if (!inserted_ul) { html_push(""); if (next) nav_tree_r(url, next_loc, subdirs[1..$]); } void do_nav_tree(string url) { html_push("
"); nav_tree_r(url, "", cast(string[])array(pathSplitter(url))); html_pop("
\n"); } void not_found(string path) { html("The page " ~ path ~ " does not exist. (404)"); } void do_header() { html_push("
"); html_push("
"); html_push("
"); html_pop("
"); html_push("
"); html("changelog"); html_pop("
"); html_pop("
"); html_push("
"); html_push("

"); html("" ~ site_title ~ " " ~ site_subtitle ~ ""); html_pop("

"); html_pop("
"); html_push("
"); html("
"); html_pop("
"); html_pop("
\n"); } bool dirExists(string path) { try { if (isDir(path)) return true; else return false; } catch (Exception e) { return false; } } void do_content(string url) { html_push("
"); // first, see if we have something that wants to handle url outright foreach (StaticRegex!char reg, string h; handlers) { if (match(url, reg)) { html(shell(dweb_root ~ "/bin/" ~ h ~ " " ~ url)); html_pop("
"); return; } } // if that failed, see if we can handle the file if (url == "" ? true : url[$-1] == '/') url ~= "index"; foreach (f; array(map!"a.name"(dirEntries(dirName(dweb_root ~ "/srv/" ~ url), SpanMode.shallow)))) { if (isDir(f)) continue; string name = baseName(f); name = name[0] == '@' ? name[1..$] : name; if (stripExtension(name) == baseName(url)) { foreach (StaticRegex!char reg, string h; handlers) { if (match(name, reg)) { html(shell(dweb_root ~ "/bin/" ~ h ~ " " ~ f)); html_pop(""); return; } } } } if (baseName(url) != "index") not_found(url); html_pop(""); } void do_footer() { html_push("
"); html_push("
"); html("Powered by dweb"); html_pop("
"); html_push("
"); html(" "); html_pop("
"); html_pop("
\n"); } bool evil(string s) { foreach(char c; s) if (!isAlphaNum(c) && c != '/' && c != '-' && c != '_') return true; return false; } void main(string[] args) { init_handlers(); dweb_root = getcwd()[0..$-4]; // take out bin/ html("Content-type: text/html\n"); html(""); html_push("\n"); string url = getenv("REQUEST_URI")[url_root.length..$]; if (evil(url)) { html ("bad url."); return; } string pagename = baseName(url); if (pagename.length != 0) pagename = " - " ~ pagename; pagename = site_title ~ pagename; html_push(""); html("" ~ pagename ~ ""); html(""); html(""); html(""); html_pop("\n"); html_push(""); html_push("
"); do_header(); do_nav_tree(url); do_content(url); do_footer(); html_pop("
"); html_pop("\n"); html_pop(""); }