From f3ee7526e1f04ccee1f1a4d3a775e2ce75169926 Mon Sep 17 00:00:00 2001 From: Nathan Lasseter Date: Thu, 24 Oct 2013 21:08:50 +0100 Subject: Initial implementation of client context, headers not working. --- .gitignore | 3 ++- Makefile | 4 ++-- context.d | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ server.d | 18 ++++------------- serverutils.d | 2 +- 5 files changed, 74 insertions(+), 18 deletions(-) create mode 100644 context.d 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) { -- cgit v1.2.1