forked from s4mu313/webcam-http-streamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.h
58 lines (41 loc) · 1020 Bytes
/
server.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
#ifndef SERVER_H
#define SERVER_H
#include "req_res.h"
#include "support.h"
#include "server_base.h"
void _callback(int client, Server_base::Functions& paths_f) {
std::string req = server_impl::receive(client);
std::string path = server_impl::get_request_path(req);
if (paths_f.find(path) != paths_f.end()) {
Request r(path);
server_impl::parse(req, r.headers, r.query);
Response res(client);
paths_f[path](r, res);
}
close(client);
}
namespace http {
class Server : public Server_base {
public:
using Callback = std::function<void(Request&, Response&)>;
Server(uint16_t port)
: Server_base(port)
{}
~Server() = default;
void
listen()
{
int client;
while((client = server_impl::accept(sock_fd)) > 0)
tp.exec(_callback, client, paths_f);
}
Server&
get(std::string path,
Callback f)
{
paths_f[path] = f;
return *this;
}
};
};
#endif // SERVER_H