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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
#include "test_server.hpp"
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <poll.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cerrno>
#include <cstring>
#include <stdexcept>
namespace cepis::testing {
namespace {
std::string reason_phrase(int status) {
switch (status) {
case 200:
return "OK";
case 400:
return "Bad Request";
case 404:
return "Not Found";
case 429:
return "Too Many Requests";
case 500:
return "Internal Server Error";
case 502:
return "Bad Gateway";
case 503:
return "Service Unavailable";
case 504:
return "Gateway Timeout";
default:
return "Status";
}
}
bool write_all(int fd, const std::string& data) {
std::size_t sent = 0;
while (sent < data.size()) {
const ssize_t written = ::send(fd, data.data() + sent, data.size() - sent, MSG_NOSIGNAL);
if (written <= 0) {
return false;
}
sent += static_cast<std::size_t>(written);
}
return true;
}
} // namespace
Server::Server() {
listen_fd_ = ::socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd_ < 0) {
throw std::runtime_error("socket: " + std::string(std::strerror(errno)));
}
const int enable = 1;
::setsockopt(listen_fd_, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable));
sockaddr_in address{};
address.sin_family = AF_INET;
address.sin_addr.s_addr = ::htonl(INADDR_LOOPBACK);
address.sin_port = 0;
if (::bind(listen_fd_, reinterpret_cast<sockaddr*>(&address), sizeof(address)) != 0) {
::close(listen_fd_);
throw std::runtime_error("bind: " + std::string(std::strerror(errno)));
}
if (::listen(listen_fd_, 32) != 0) {
::close(listen_fd_);
throw std::runtime_error("listen: " + std::string(std::strerror(errno)));
}
sockaddr_in bound{};
socklen_t length = sizeof(bound);
if (::getsockname(listen_fd_, reinterpret_cast<sockaddr*>(&bound), &length) != 0) {
::close(listen_fd_);
throw std::runtime_error("getsockname: " + std::string(std::strerror(errno)));
}
port_ = ::ntohs(bound.sin_port);
thread_ = std::thread([this] { serve(); });
}
Server::~Server() {
stopping_.store(true);
if (thread_.joinable()) {
thread_.join();
}
if (listen_fd_ >= 0) {
::close(listen_fd_);
}
}
std::string Server::url() const { return "http://127.0.0.1:" + std::to_string(port_); }
void Server::respond(std::string path, Reply reply) {
respond(std::move(path), std::vector<Reply>{std::move(reply)});
}
void Server::respond(std::string path, std::vector<Reply> replies) {
const std::scoped_lock guard(mutex_);
routes_[std::move(path)] = std::move(replies);
}
void Server::handle(std::function<Reply(const std::string&)> handler) {
const std::scoped_lock guard(mutex_);
handler_ = std::move(handler);
}
int Server::count(std::string_view prefix) const {
const std::scoped_lock guard(mutex_);
int total = 0;
for (const std::string& call : calls_) {
if (call.rfind(prefix, 0) == 0) {
++total;
}
}
return total;
}
std::vector<std::string> Server::calls() const {
const std::scoped_lock guard(mutex_);
return calls_;
}
Reply Server::next(const std::string& path) {
std::function<Reply(const std::string&)> handler;
{
const std::scoped_lock guard(mutex_);
calls_.push_back(path);
handler = handler_;
if (!handler) {
const auto found = routes_.find(path);
if (found == routes_.end()) {
return Reply{404, R"({"erro":"cep_nao_encontrado"})", {}};
}
std::vector<Reply>& queue = found->second;
if (queue.size() > 1) {
Reply reply = queue.front();
queue.erase(queue.begin());
return reply;
}
return queue.front();
}
}
return handler(path);
}
void Server::serve() {
while (!stopping_.load()) {
pollfd waiting{listen_fd_, POLLIN, 0};
const int ready = ::poll(&waiting, 1, 10);
if (ready <= 0) {
continue;
}
const int client = ::accept(listen_fd_, nullptr, nullptr);
if (client < 0) {
continue;
}
std::string request;
char buffer[4096];
while (request.find("\r\n\r\n") == std::string::npos) {
const ssize_t read_bytes = ::recv(client, buffer, sizeof(buffer), 0);
if (read_bytes <= 0) {
break;
}
request.append(buffer, static_cast<std::size_t>(read_bytes));
}
const std::size_t first_space = request.find(' ');
const std::size_t second_space =
first_space == std::string::npos ? std::string::npos : request.find(' ', first_space + 1);
if (second_space == std::string::npos) {
::close(client);
continue;
}
const std::string path = request.substr(first_space + 1, second_space - first_space - 1);
const Reply reply = next(path);
std::string out = "HTTP/1.1 ";
out += std::to_string(reply.status);
out += ' ';
out += reason_phrase(reply.status);
out += "\r\n";
for (const auto& [name, value] : reply.headers) {
out += name;
out += ": ";
out += value;
out += "\r\n";
}
out += "Content-Length: ";
out += std::to_string(reply.body.size());
out += "\r\n";
out += "Connection: close\r\n\r\n";
out += reply.body;
write_all(client, out);
::shutdown(client, SHUT_WR);
::close(client);
}
}
} // namespace cepis::testing
|