-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathHttpServer.cpp
67 lines (57 loc) · 1.32 KB
/
HttpServer.cpp
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
#include <map>
#include <functional>
#include <string_view>
#include "HttpServer.h"
#include "HttpConnect.h"
#include "CommonPool.h"
HttpServer::HttpServer(EventLoop * loop):
TcpServer(loop)
{
}
TcpSocket * HttpServer::createSocket()
{
HttpConnect * conn = CommPool::create<HttpConnect>();
conn->setEvent(this);
return conn;
}
void HttpServer::onSocket(TcpSocket * connect)
{
}
void HttpServer::onClose(HttpConnect *conn)
{
CommPool::reclaim(conn);
}
void HttpServer::onGet(HttpConnect* conn, std::string_view& path, std::string_view& data)
{
std::string spath(path);
auto it = m_get.find(spath);
if (it != m_get.end())
{
(it->second)(conn, data);
}
}
void HttpServer::onPost(HttpConnect* conn, std::string_view& path, std::string_view& data)
{
std::string spath(path);
auto it = m_post.find(spath);
if (it != m_post.end())
{
(it->second)(conn, data);
}
}
void HttpServer::onOther(HttpConnect* conn, HttpParser* parser)
{
if (m_other) m_other(conn, parser);
}
void HttpServer::addGet(const char * name, std::function<void(HttpConnect *, std::string_view &)> back)
{
m_get[name] = back;
}
void HttpServer::addPost(const char * name, std::function<void(HttpConnect *, std::string_view &)> back)
{
m_post[name] = back;
}
void HttpServer::setOther(std::function<void(HttpConnect*, HttpParser*)> back)
{
m_other = back;
}