From a4948f0c2f837bb9191fee7524457ba2becb2be2 Mon Sep 17 00:00:00 2001 From: Jacob Parker Date: Wed, 22 Feb 2012 22:43:07 -0500 Subject: [PATCH 1/4] ported more generalized handler stuff from csc fork --- .htaccess | 2 +- src/config.d | 6 +++--- src/web.d | 22 +++++++++++++++++++++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/.htaccess b/.htaccess index 0039b2c..7b23492 100644 --- a/.htaccess +++ b/.htaccess @@ -1,4 +1,4 @@ RewriteEngine on RewriteBase /~j3parker/ RewriteRule ^pub/(.*) - [L] -RewriteRule ^(.*) /users/j3parker/www/bin/web [L] +RewriteRule ^(.*) bin/web [L] diff --git a/src/config.d b/src/config.d index a133c26..b7f1171 100644 --- a/src/config.d +++ b/src/config.d @@ -3,9 +3,9 @@ const string url_root = "/~j3parker/"; const string site_title = "This is a Title"; const string site_subtitle = "but this is a subtitle"; -const bool nav_tree_vert = true; -const bool nav_tree_chev = true; -const bool page_container = false; +const bool nav_tree_vert = false; +const bool nav_tree_chev = false; +const bool page_container = true; string[string] handlers; diff --git a/src/web.d b/src/web.d index 5013d7d..c5ad886 100755 --- a/src/web.d +++ b/src/web.d @@ -1,5 +1,5 @@ //#!/usr/bin/rdmd -import std.stdio, std.path, std.process, std.file, std.array, std.string, std.algorithm, std.datetime; +import std.stdio, std.path, std.process, std.file, std.array, std.string, std.algorithm, std.datetime, std.ascii; import config; string dweb_root; @@ -65,9 +65,14 @@ void do_header() { html_push("
"); html_push("
"); + + html_push("
"); + html_pop("
"); + html_push("
"); html("changelog"); html_pop("
"); + html_pop("
"); html_push("
"); @@ -87,6 +92,15 @@ bool dirExists(string path) { try { if (isDir(path)) return true; else return void do_content(string url) { html_push("
"); + // first, see if we have something that wants to handle url outright + foreach (string glob, string h; handlers) { + if (globMatch(url, glob)) { + html(shell(dweb_root ~ "/bin/" ~ h ~ " " ~ url)); + html_pop("
"); + return; + } + } + // if that failed, see if we can handle the file if (url == "" ? false : url[$-1] == '/') url ~= "index"; foreach (f; array(map!"a.name"(dirEntries(dirName(dweb_root ~ "/srv/" ~ url), SpanMode.shallow)))) { if (isDir(f)) continue; @@ -119,6 +133,11 @@ void do_footer() { 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/ @@ -128,6 +147,7 @@ void main(string[] args) { html_push("\n"); string url = getenv("SCRIPT_URL")[url_root.length..$]; + if (evil(url)) { html ("bad url."); return; } string pagename = baseName(url); if (pagename.length != 0) pagename = " - " ~ pagename; From c71bd3a81dbae6f31f4ff225d461dad4a8e496c6 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Thu, 23 Feb 2012 01:46:22 -0500 Subject: [PATCH 2/4] Added readme. .htaccess and config.d are also now ignored in git instead samples are provided to be copied over and modified. --- .gitignore | 2 ++ .htaccess | 4 --- README.md | 53 +++++++++++++++++++++++++++++++ htaccess-sample | 4 +++ src/{config.d => config.d.sample} | 2 +- 5 files changed, 60 insertions(+), 5 deletions(-) delete mode 100644 .htaccess create mode 100644 README.md create mode 100644 htaccess-sample rename src/{config.d => config.d.sample} (89%) diff --git a/.gitignore b/.gitignore index cebac2a..7087134 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ pub/* srv/* bin/web +.htaccess +src/config.d diff --git a/.htaccess b/.htaccess deleted file mode 100644 index 0039b2c..0000000 --- a/.htaccess +++ /dev/null @@ -1,4 +0,0 @@ -RewriteEngine on -RewriteBase /~j3parker/ -RewriteRule ^pub/(.*) - [L] -RewriteRule ^(.*) /users/j3parker/www/bin/web [L] diff --git a/README.md b/README.md new file mode 100644 index 0000000..bd0dab3 --- /dev/null +++ b/README.md @@ -0,0 +1,53 @@ +dweb - A simple website written in D +===== +dweb is a simple website framework based off the [werc][werc] software. + +Its principles are: + +- Database free, uses files and directories instead. +- Written using the D programming language. +- Minimize tedious work: eg., no need to ever write HTML, use markdown instead. +- Very minimalist yet extensible codebase. Handlers for special things should be easy to add. + +It was created because werc was annoying to deploy on UW Computer Science Club's Apache setup and because [Not Invented Here](http://en.wikipedia.org/wiki/Not_Invented_Here). + +[werc]:http://werc.cat-v.org/ +[md]:http://daringfireball.net/projects/markdown + +Install Guide +------ +You will need: +- An HTTP server with CGI support. +- The D compiler. + +Extract the contents into the location you want to serve webpages from. Create $DWEB\_ROOT/.htaccess and $DWEB\_ROOT/src/config.d by copying the provided sample files. Then edit the following files: + +- $DWEB_ROOT/.htaccess: make the paths work for your setup. +- $DWEB_ROOT/src/config.d: change these strings as necessary. +- $DWEB_ROOT/src/web.d: this is the main web code. +- $DWEB_ROOT/pub/: static content goes here. +- $DWEB_ROOT/srv/: directories, markdown webpages etc. go in here. +- $DWEB_ROOT/bin/: custom handlers go here. + +Run build in $DWEB_ROOT/src to recompile the website software. + +Source +-------- + +You can get the source code on [github](https://github.com/j3parker/dweb) or by running + + git clone git://github.com/j3parker/dweb.git + +Contact +-------- +For questions, suggestions, bug reports and contributing patches email [j3parker](mailto:j3parker@csclub.uwaterloo.ca) + +License +------- +Public domain. + +Credits +------- +The idea and css stolen from [werc][werc]. This page itself also plagarised. + +Thanks to John Gruber for the [Markdown.pl][md] script. diff --git a/htaccess-sample b/htaccess-sample new file mode 100644 index 0000000..23c9137 --- /dev/null +++ b/htaccess-sample @@ -0,0 +1,4 @@ +RewriteEngine on +RewriteBase / +RewriteRule ^pub/(.*) - [L] +RewriteRule ^(.*) bin/web [L] diff --git a/src/config.d b/src/config.d.sample similarity index 89% rename from src/config.d rename to src/config.d.sample index b7f1171..2e12188 100644 --- a/src/config.d +++ b/src/config.d.sample @@ -1,4 +1,4 @@ -const string url_root = "/~j3parker/"; +const string url_root = "/"; const string site_title = "This is a Title"; const string site_subtitle = "but this is a subtitle"; From 8fe3797d0725f14cf9c805152f2bc8c6996294cd Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Thu, 23 Feb 2012 02:08:21 -0500 Subject: [PATCH 3/4] Fixed 404 error on index pages. Also changed to REQUEST_URI instead of SCRIPT_URL. --- src/web.d | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/web.d b/src/web.d index c5ad886..814cb3f 100755 --- a/src/web.d +++ b/src/web.d @@ -101,7 +101,7 @@ void do_content(string url) { } } // if that failed, see if we can handle the file - if (url == "" ? false : url[$-1] == '/') url ~= "index"; + 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; @@ -146,7 +146,7 @@ void main(string[] args) { html(""); html_push("\n"); - string url = getenv("SCRIPT_URL")[url_root.length..$]; + string url = getenv("REQUEST_URI")[url_root.length..$]; if (evil(url)) { html ("bad url."); return; } string pagename = baseName(url); From 0d4efc8b77fa6ff242b8f1b5e5d1599439d09b98 Mon Sep 17 00:00:00 2001 From: Jeremy Roman Date: Thu, 23 Feb 2012 20:20:48 -0500 Subject: [PATCH 4/4] allow building from outside src/ --- src/build | 1 + 1 file changed, 1 insertion(+) diff --git a/src/build b/src/build index 0f837d1..ea85a52 100755 --- a/src/build +++ b/src/build @@ -1 +1,2 @@ +cd "$(dirname $0)" dmd config.d web.d -ofweb && rm web.o && mv web ../bin/