1 module dast.http.common;
2 
3 enum Status {
4 	OK = "200 OK",
5 	NoContent = "204 No Content",
6 	Unauthorized = "401 Unauthorized",
7 	Forbidden = "403 Forbidden",
8 	NotFound = "404 Not Found",
9 	MethodNotAllowed = "405 Method Not Allowed",
10 	Error = "500 Internal Server Error"
11 }
12 
13 struct Headers {
14 	const(char)[][const(char)[]] data;
15 	alias data this;
16 
17 	auto opIndex(in char[] key) const {
18 		if (auto p = key in data)
19 			return *p;
20 		return null;
21 	}
22 
23 	void opIndexAssign(in char[] value, in char[] key) pure @trusted nothrow {
24 		import tame.text.ascii;
25 
26 		data[cast(string)toLower(key.dup)] = value;
27 	}
28 
29 	auto opDispatch(string key)() const => this[key];
30 
31 	auto opDispatch(string key)(in char[] value) => data[key] = value;
32 }
33 
34 struct Request {
35 	const(char)[] method, path, httpVersion;
36 	Headers headers;
37 	const(char)[] message;
38 
39 @safe pure nothrow:
40 	void onMethod(const char[] m) @nogc {
41 		method = m;
42 	}
43 
44 	void onUri(const char[] uri) @nogc {
45 		path = uri;
46 	}
47 
48 	void onHeader(const char[] name, const char[] value) {
49 		headers[name] = value;
50 	}
51 
52 	bool tryParse(const ubyte[] data) nothrow {
53 		import dast.http.parser;
54 
55 		Parser!Request parser;
56 		int res = parser.parseRequest(data);
57 		this = parser.msg;
58 		return res <= data.length;
59 	}
60 }