forked from ares-emulator/ares
-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.hpp
158 lines (128 loc) · 4.32 KB
/
role.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#pragma once
//Role: base class for Client and Server
//provides shared functionality
#include <nall/http/request.hpp>
#include <nall/http/response.hpp>
namespace nall::HTTP {
struct Role {
struct Settings {
s32 connectionLimit = 1 * 1024; //server
s32 headSizeLimit = 16 * 1024; //client, server
s32 bodySizeLimit = 65536 * 1024; //client, server
s32 chunkSize = 32 * 1024; //client, server
s32 threadStackSize = 128 * 1024; //server
s32 timeoutReceive = 15 * 1000; //server
s32 timeoutSend = 15 * 1000; //server
} settings;
auto configure(const string& parameters) -> bool;
auto download(s32 fd, Message& message) -> bool;
auto upload(s32 fd, const Message& message) -> bool;
};
inline auto Role::configure(const string& parameters) -> bool {
auto document = BML::unserialize(parameters);
for(auto parameter : document) {
auto name = parameter.name();
auto value = parameter.integer();
if(0);
else if(name == "connectionLimit") settings.connectionLimit = value;
else if(name == "headSizeLimit") settings.headSizeLimit = value;
else if(name == "bodySizeLimit") settings.bodySizeLimit = value;
else if(name == "chunkSize") settings.chunkSize = value;
else if(name == "threadStackSize") settings.threadStackSize = value;
else if(name == "timeoutReceive") settings.timeoutReceive = value;
else if(name == "timeoutSend") settings.timeoutSend = value;
}
return true;
}
inline auto Role::download(s32 fd, Message& message) -> bool {
auto& head = message._head;
auto& body = message._body;
string chunk;
u8 packet[settings.chunkSize], *p = nullptr;
head.reset(), head.reserve(4095);
body.reset(), body.reserve(4095);
bool headReceived = false;
bool chunked = false;
bool chunkReceived = false;
bool chunkFooterReceived = true;
s32 length = 0;
s32 chunkLength = 0;
s32 contentLength = 0;
while(true) {
if(auto limit = settings.headSizeLimit) if(head.size() >= limit) return false;
if(auto limit = settings.bodySizeLimit) if(body.size() >= limit) return false;
if(headReceived && !chunked && body.size() >= contentLength) {
body.resize(contentLength);
break;
}
if(length == 0) {
length = recv(fd, packet, settings.chunkSize, MSG_NOSIGNAL);
if(length <= 0) return false;
p = packet;
}
if(!headReceived) {
head.append((char)*p++);
--length;
if(head.endsWith("\r\n\r\n") || head.endsWith("\n\n")) {
headReceived = true;
if(!message.setHead()) return false;
chunked = message.header["Transfer-Encoding"].value().iequals("chunked");
contentLength = message.header["Content-Length"].value().natural();
}
continue;
}
if(chunked && !chunkReceived) {
char n = *p++;
--length;
if(!chunkFooterReceived) {
if(n == '\n') chunkFooterReceived = true;
continue;
}
chunk.append(n);
if(chunk.endsWith("\r\n") || chunk.endsWith("\n")) {
chunkReceived = true;
chunkLength = chunk.hex();
if(chunkLength == 0) break;
chunk.reset();
}
continue;
}
if(!chunked) {
body.resize(body.size() + length);
memory::copy(body.get() + body.size() - length, p, length);
p += length;
length = 0;
} else {
s32 transferLength = min(length, chunkLength);
body.resize(body.size() + transferLength);
memory::copy(body.get() + body.size() - transferLength, p, transferLength);
p += transferLength;
length -= transferLength;
chunkLength -= transferLength;
if(chunkLength == 0) {
chunkReceived = false;
chunkFooterReceived = false;
}
}
}
if(!message.setBody()) return false;
return true;
}
inline auto Role::upload(s32 fd, const Message& message) -> bool {
auto transfer = [&](const u8* data, u32 size) -> bool {
while(size) {
s32 length = send(fd, data, min(size, settings.chunkSize), MSG_NOSIGNAL);
if(length < 0) return false;
data += length;
size -= length;
}
return true;
};
if(message.head([&](const u8* data, u32 size) -> bool { return transfer(data, size); })) {
if(message.body([&](const u8* data, u32 size) -> bool { return transfer(data, size); })) {
return true;
}
}
return false;
}
}