blob: 965fe72f1aa2b805630e6b420d21c7433cd87edd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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;
}
}
}
}
|