-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsupport.h
139 lines (115 loc) · 3.5 KB
/
support.h
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
#ifndef SUPPORT_H
#define SUPPORT_H
#include <sys/socket.h>
#include <arpa/inet.h>
#include <cstring>
#include <string>
#include <vector>
#include <thread>
#include <atomic>
#include <map>
namespace server_impl {
using Headers = std::vector<std::string>;
using Params = std::map<std::string, std::string>;
void parse_q(std::string str, Params& params)
{
size_t pos = 0;
if (pos = str.find("?"); pos == std::string::npos)
return;
str.erase(0, pos + 1);
if (pos = str.find("HTTP/"); pos == std::string::npos)
return;
str.erase(pos - 1);
while (true) {
if ((pos = str.find("=")); pos == std::string::npos)
return;
std::string s2 = str.substr(0, pos);
str.erase(0, pos + 1);
if (pos = str.find("&"); str == "" && pos == std::string::npos)
return;
params[s2] = str.substr(0, pos);
str.erase(0, pos + 1);
}
}
void parse(std::string str, Headers& headers, Params& params)
{
size_t pos = 0;
pos = str.find("\r\n");
parse_q(str.substr(0, pos), params);
str.erase(0, pos + 2);
while ((pos = str.find("\r\n")) != std::string::npos && str != "\r\n") {
headers.push_back(str.substr(0, pos));
str.erase(0, pos + 2);
}
}
size_t get_first_free(std::vector<std::thread*>& tasks,
std::vector<std::atomic_bool*>& ended_t)
{
for (size_t i = 0; i < tasks.size(); ++i)
if (*ended_t[i]) {
if (tasks[i]->joinable())
tasks[i]->join();
*ended_t[i] = false;
return i;
}
tasks.push_back(nullptr);
ended_t.push_back(new std::atomic_bool(false));
return tasks.size() - 1;
}
int accept(int sock_fd)
{
sockaddr_in c_addr;
socklen_t c_length = sizeof(c_addr);
return accept(sock_fd, reinterpret_cast<sockaddr*>(&c_addr), &c_length);
}
std::string receive(int fd)
{
char buffer[1024 * 8] {0};
std::string str = "";
while (recv(fd, buffer, sizeof (buffer), MSG_DONTWAIT) > 0)
str += std::string(buffer);
return str;
}
bool send_h(int fd, const std::vector<std::string>& headers)
{
std::string res = "";
for (const auto& h : headers)
res += h + "\r\n";
res += "\r\n";
return send(fd, res.c_str(), res.size(), MSG_NOSIGNAL) >= 0;
}
bool send_m(int fd, const std::string& msg)
{
std::string res = msg;
return send(fd, res.c_str(), res.size(), MSG_NOSIGNAL) >= 0;
}
std::string get_request_path(const std::string& req)
{
if (req.find("?") != std::string::npos)
return (req.size() > 0) ?
req.substr(req.find("GET") + 4, req.find("?") - req.find("GET") - 4)
: "";
return (req.size() > 0) ?
req.substr(req.find("GET") + 4, req.find("HTTP/") - req.find("GET") - 5)
: "";
}
void set_channel(int sock_fd, sockaddr_in& s_addr)
{
if (bind(sock_fd, reinterpret_cast<sockaddr*>(&s_addr), sizeof(s_addr)) < 0)
throw std::runtime_error("binding_failed");
listen(sock_fd, 32);
}
void init_server(int& sock_fd, sockaddr_in& s_addr, uint16_t port)
{
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
const int optVal = 1;
const socklen_t optLen = sizeof(optVal);
setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const void*>(&optVal), optLen);
memset(&s_addr, 0, sizeof(s_addr));
s_addr.sin_family = AF_INET;
s_addr.sin_addr.s_addr = inet_addr("0.0.0.0");
s_addr.sin_port = htons(port);
set_channel(sock_fd, s_addr);
}
};
#endif // SUPPORT_H