forked from ares-emulator/ares
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.hpp
104 lines (82 loc) · 3.01 KB
/
message.hpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#pragma once
//httpMessage: base class for httpRequest and httpResponse
//provides shared functionality
namespace nall::HTTP {
struct Variable {
string name;
string value;
};
struct SharedVariable {
SharedVariable(const nall::string& name = "", const nall::string& value = "") : shared(new Variable{name, value}) {}
explicit operator bool() const { return (bool)shared->name; }
auto operator()() const { return shared->value; }
auto& operator=(const nall::string& value) { shared->value = value; return *this; }
auto name() const { return shared->name; }
auto value() const { return shared->value; }
auto string() const { return nall::string{shared->value}.strip().replace("\r", ""); }
auto boolean() const { return string() == "true"; }
auto integer() const { return string().integer(); }
auto natural() const { return string().natural(); }
auto real() const { return string().real(); }
auto& setName(const nall::string& name) { shared->name = name; return *this; }
auto& setValue(const nall::string& value = "") { shared->value = value; return *this; }
shared_pointer<Variable> shared;
};
struct Variables {
auto operator[](const string& name) const -> SharedVariable {
for(auto& variable : variables) {
if(variable.shared->name.iequals(name)) return variable;
}
return {};
}
auto operator()(const string& name) -> SharedVariable {
for(auto& variable : variables) {
if(variable.shared->name.iequals(name)) return variable;
}
return append(name);
}
auto find(const string& name) const -> vector<SharedVariable> {
vector<SharedVariable> result;
for(auto& variable : variables) {
if(variable.shared->name.iequals(name)) result.append(variable);
}
return result;
}
auto assign(const string& name, const string& value = "") -> SharedVariable {
for(auto& variable : variables) {
if(variable.shared->name.iequals(name)) {
variable.shared->value = value;
return variable;
}
}
return append(name, value);
}
auto append(const string& name, const string& value = "") -> SharedVariable {
SharedVariable variable{name, value};
variables.append(variable);
return variable;
}
auto remove(const string& name) -> void {
for(auto n : reverse(range(variables.size()))) {
if(variables[n].shared->name.iequals(name)) variables.remove(n);
}
}
auto size() const { return variables.size(); }
auto begin() const { return variables.begin(); }
auto end() const { return variables.end(); }
auto begin() { return variables.begin(); }
auto end() { return variables.end(); }
vector<SharedVariable> variables;
};
struct Message {
using type = Message;
virtual auto head(const function<bool (const u8* data, u32 size)>& callback) const -> bool = 0;
virtual auto setHead() -> bool = 0;
virtual auto body(const function<bool (const u8* data, u32 size)>& callback) const -> bool = 0;
virtual auto setBody() -> bool = 0;
Variables header;
//private:
string _head;
string _body;
};
}