aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk>2013-10-21 20:14:27 +0100
committerNathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk>2013-10-21 20:14:27 +0100
commit8480e8287fdf3aa933ad7b82457b3e67ab0bfa0b (patch)
tree7d0cbd32d390218a24217b657907a8a5dc949605
First commit
-rw-r--r--.gitignore2
-rw-r--r--Makefile10
-rw-r--r--Readme.textile9
-rw-r--r--server.d26
-rw-r--r--serverutils.d17
5 files changed, 64 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..6c46439
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*.o
+server
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..b741505
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,10 @@
+TARGET=server
+PREQS=server.o serverutils.o
+
+all: ${TARGET}
+
+${TARGET}: ${PREQS}
+ dmd -of$@ $^
+
+%.o: %.d
+ dmd -c $<
diff --git a/Readme.textile b/Readme.textile
new file mode 100644
index 0000000..b47c8ca
--- /dev/null
+++ b/Readme.textile
@@ -0,0 +1,9 @@
+h1. servd
+
+h2. Lightweight low-config no-frills webserver
+
+servd is a very non-featureful webserver. It doesn't yet even do the web part.
+
+It roots itself in the directory it is called from.
+
+Call it with a port. It only listens locally.
diff --git a/server.d b/server.d
new file mode 100644
index 0000000..640271c
--- /dev/null
+++ b/server.d
@@ -0,0 +1,26 @@
+import std.socket, std.stdio,
+ std.conv, std.file,
+ std.string;
+import serverutils;
+
+void main(string[] args) {
+ if (args.length != 2) return;
+ auto sock = new TcpSocket();
+ sock.bind(new InternetAddress("127.0.0.1", to!ushort(args[1])));
+ sock.listen(1000);
+ while (true) {
+ auto level = 0;
+ auto pair = sock.accept();
+ char[255] filename;
+ auto len = pair.receive(filename);
+ auto file = filename[0 .. len];
+ file = strip(file);
+ if (inRoot(file)) {
+ pair.send(readText(file));
+ } else {
+ pair.send("ERROR: Client left root!\n");
+ }
+ pair.shutdown(SocketShutdown.BOTH);
+ pair.close();
+ }
+}
diff --git a/serverutils.d b/serverutils.d
new file mode 100644
index 0000000..d5b9836
--- /dev/null
+++ b/serverutils.d
@@ -0,0 +1,17 @@
+bool inRoot(char[] path) {
+ auto level = 0;
+ if (path[0] == '/') return false;
+ while (path.length > 0) {
+ if (path[0] == '/') {
+ level++;
+ path = path[1..$];
+ } else if (path[0] == '.' && path[1] == '.' && path[2] == '/') {
+ level--;
+ path = path[3..$];
+ } else {
+ path = path[1..$];
+ }
+ if (level < 0) return false;
+ }
+ return true;
+}