aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Lasseter <nathan@4574.co.uk>2013-10-24 21:08:50 +0100
committerNathan Lasseter <nathan@4574.co.uk>2013-10-24 21:08:50 +0100
commitf3ee7526e1f04ccee1f1a4d3a775e2ce75169926 (patch)
treec49717071752e759ec7e63f002bbbdccc7d3338d
parent8480e8287fdf3aa933ad7b82457b3e67ab0bfa0b (diff)
Initial implementation of client context, headers not working.
-rw-r--r--.gitignore3
-rw-r--r--Makefile4
-rw-r--r--context.d65
-rw-r--r--server.d18
-rw-r--r--serverutils.d2
5 files changed, 74 insertions, 18 deletions
diff --git a/.gitignore b/.gitignore
index 6c46439..da09320 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
*.o
-server
+*.swp
+servd
diff --git a/Makefile b/Makefile
index b741505..7bca880 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
-TARGET=server
-PREQS=server.o serverutils.o
+TARGET=servd
+PREQS=server.o serverutils.o context.o
all: ${TARGET}
diff --git a/context.d b/context.d
new file mode 100644
index 0000000..965fe72
--- /dev/null
+++ b/context.d
@@ -0,0 +1,65 @@
+import
+ std.socket,
+ std.file,
+ std.string,
+ std.format,
+ std.stdio;
+import
+ serverutils;
+
+enum ClientState {
+ START,
+ REQUEST_LINE,
+ HEADER_LINES,
+ DONE,
+ ERROR
+};
+
+class Request {
+ string method;
+ string path;
+ string vers;
+ char[][] header_lines;
+ auto this(char[] line) {
+ line = strip(line);
+ formattedRead(line, "%s %s %s", &method, &path, &vers);
+ }
+}
+
+class ClientContext {
+ ClientState state;
+ Socket pair;
+ Request request;
+ auto this(Socket p) {
+ pair = p;
+ state = ClientState.START;
+ }
+ void handle() {
+ while(true) {
+ switch(state) {
+ case ClientState.START:
+ char[255] line;
+ auto len = pair.receive(line);
+ auto request_line = line[0 .. len];
+ request = new Request(request_line);
+ state = ClientState.REQUEST_LINE;
+ break;
+ case ClientState.REQUEST_LINE:
+ state
+ break;
+ case ClientState.HEADER_LINES:
+ if (inRoot(request.path))
+ pair.send(readText(request.path));
+ else
+ pair.send("ERROR: Client left root!\n");
+ state = ClientState.DONE;
+ break;
+ case ClientState.DONE:
+ return;
+ break;
+ default:
+ break;
+ }
+ }
+ }
+}
diff --git a/server.d b/server.d
index 640271c..ed54be9 100644
--- a/server.d
+++ b/server.d
@@ -1,7 +1,5 @@
-import std.socket, std.stdio,
- std.conv, std.file,
- std.string;
-import serverutils;
+import std.socket, std.conv;
+import context;
void main(string[] args) {
if (args.length != 2) return;
@@ -9,17 +7,9 @@ void main(string[] args) {
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");
- }
+ auto context = new ClientContext(pair);
+ context.handle();
pair.shutdown(SocketShutdown.BOTH);
pair.close();
}
diff --git a/serverutils.d b/serverutils.d
index d5b9836..43063ef 100644
--- a/serverutils.d
+++ b/serverutils.d
@@ -1,4 +1,4 @@
-bool inRoot(char[] path) {
+bool inRoot(string path) {
auto level = 0;
if (path[0] == '/') return false;
while (path.length > 0) {