-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpd.cpp
95 lines (86 loc) · 2.66 KB
/
httpd.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
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
#include "httpd.h"
#include "httplib.h"
#include <QtConcurrent>
#include <QDebug>
using namespace httplib;
#if SSL_SERVER
HttpD::HttpD(QString szRootPath, quint16 u16Port, quint16 u16sslPort, QObject *parent)
#else
HttpD::HttpD(QString szRootPath, quint16 u16Port, QObject *parent)
#endif
: QObject(parent)
, m_szRootPath(szRootPath)
#if SSL_SERVER
, m_httpsServer(new SSLServer(g_szClientCert.toStdString().c_str(), g_szClientCertKey.toStdString().c_str()))
, m_u16HttpsServerBindPort(u16sslPort)
#endif
, m_httpServer(new Server())
, m_u16HttpServerBindPort(u16Port)
{
#if SSL_SERVER
m_httpsServer->set_idle_interval(0, 500 * 1000);
m_httpsServer->set_read_timeout(1);
m_httpsServer->set_write_timeout(1);
#endif
m_httpServer->set_idle_interval(0, 500 * 1000);
m_httpServer->set_read_timeout(1);
m_httpServer->set_write_timeout(1);
#if SSL_SERVER
auto ret = m_httpsServer->set_mount_point("/", m_szRootPath.toStdString().c_str());
if (!ret)
{
qInfo("HTTP Server Mount root directory Fail!");
}
m_httpsServer->set_error_handler([](const Request &, Response & res)
{
auto fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), fmt, res.status);
res.set_content(buf, "text/html");
});
#endif
auto ret = m_httpServer->set_mount_point("/", m_szRootPath.toStdString().c_str());
if (!ret)
{
qInfo("HTTP Server Mount root directory Fail!");
}
m_httpServer->set_error_handler([](const Request &, Response & res)
{
auto fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), fmt, res.status);
res.set_content(buf, "text/html");
});
}
void HttpD::start()
{
#if SSL_SERVER
QtConcurrent::run([&]
{
if (m_u16HttpsServerBindPort > 0)
{
CommonTools::outputLog(QString("HTTPS Server Startup with [%1] listen to %2 ...")
.arg(m_szRootPath)
.arg(m_u16HttpServerBindPort);
m_httpsServer->listen("0.0.0.0", m_u16HttpsServerBindPort);
}
});
#endif
QtConcurrent::run([&]
{
if (m_u16HttpServerBindPort > 0)
{
qInfo() << QString("HTTP Server Startup with [%1] listen to %2 ...")
.arg(m_szRootPath)
.arg(m_u16HttpServerBindPort);
m_httpServer->listen("0.0.0.0", m_u16HttpServerBindPort);
}
});
}
void HttpD::stop()
{
#if SSL_SERVER
m_httpsServer->stop();
#endif
m_httpServer->stop();
}