-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttpServer.cpp
More file actions
231 lines (214 loc) · 6.23 KB
/
HttpServer.cpp
File metadata and controls
231 lines (214 loc) · 6.23 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "HttpServer.h"
#include "spdlog/spdlog.h"
#include "spdlog/async.h" //support for async logging
#include "spdlog/sinks/daily_file_sink.h"
#include "LogWriter.h"
#ifdef _DEBUG
#pragma comment(lib,"Lib\\libeay32MDd.lib")
#pragma comment(lib,"Lib\\ssleay32MDd.lib")
#else
#pragma comment(lib,"Lib\\libeay32MD.lib")
#pragma comment(lib,"Lib\\ssleay32MD.lib")
#endif
namespace spd = spdlog;
//静态变量需要在使用前定义 否则lnk2001
std::unordered_map<std::string, ReqHandler> HttpServer::s_handler_map;
auto m_logger = spd::daily_logger_mt("HttpServer", "logs/Http.log", 0, 0);
mg_mgr HttpServer::mgr;
void HttpServer::init(const std::string port)
{
m_port = port;
m_logger->flush_on(spd::level::info);
}
void HttpServer::startHttpService(std::string certName,std::string keyName)
{
struct mg_connection *nc;
struct mg_bind_opts bind_opts;
const char *err;
mg_mgr_init(&mgr, NULL);
memset(&bind_opts, 0, sizeof(bind_opts));
bind_opts.ssl_cert = certName.c_str();
bind_opts.ssl_key = keyName.c_str();
bind_opts.error_string = &err;
m_logger->info("Starting Https server on port:{}, cert:{}, key:{}", m_port.c_str(), bind_opts.ssl_cert, bind_opts.ssl_key);
nc = mg_bind_opt(&mgr, m_port.c_str(), ev_handler, bind_opts);
if (nc == NULL)
{
m_logger->info("Failed to create listener:{}", err);
return ;
}
mg_set_protocol_http_websocket(nc);
//通过调用循环创建一个事件mg_mgr_poll()循环
for (;;)
{
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
}
void HttpServer::startHttpService()
{
struct mg_mgr mgr;
struct mg_connection *nc;
struct mg_bind_opts bind_opts;
const char *err;
mg_mgr_init(&mgr, NULL);
memset(&bind_opts, 0, sizeof(bind_opts));
bind_opts.error_string = &err;
m_logger->info("Starting Http server on port:{}", m_port.c_str());
nc = mg_bind_opt(&mgr, m_port.c_str(), ev_handler, bind_opts);
if (nc == NULL)
{
m_logger->info("Failed to create listener:{}", err);
return ;
}
mg_set_protocol_http_websocket(nc);
//通过调用循环创建一个事件mg_mgr_poll()循环
for (;;)
{
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
}
bool HttpServer::route_check(http_message *http_msg,const char *route_prefix)
{
if (mg_vcmp(&http_msg->uri, route_prefix) == 0)
return true;
else
return false;
}
//web server
void HttpServer::HandleHttpEvent(mg_connection *connection, http_message *http_req)
{
//std::string req_str = std::string(http_req->message.p, http_req->message.len);
//m_logger->info("got request: %s\n", req_str.c_str());
//// 先过滤是否已注册的函数回调
//std::string url = std::string(http_req->uri.p, http_req->uri.len);
//std::string body = std::string(http_req->body.p, http_req->body.len);
//auto it = s_handler_map.find(url);
//if (it != s_handler_map.end())
//{
// ReqHandler handle_func = it->second;
// handle_func(url, body, connection, SendData);
//}
//// 其他请求
//if (route_check(http_req, "/"))
//{
// static mg_serve_http_opts s_server_option;
// mg_serve_http(connection, http_req, s_server_option);
//}
//else if (route_check(http_req, "/api/hello"))
//{
// // 直接回传
// SendData(connection, "welcome to httpserver");
//}
//else if (route_check(http_req, "/api/sum"))
//{
// // 简单post请求,加法运算测试
// char n1[100], n2[100];
// double result;
// /* Get form variables */
// mg_get_http_var(&http_req->body, "n1", n1, sizeof(n1));
// mg_get_http_var(&http_req->body, "n2", n2, sizeof(n2));
// /* Compute the result and send it back as a JSON object */
// result = strtod(n1, NULL) + strtod(n2, NULL);
// SendData(connection, std::to_string(result));
//}
//else
//{
// mg_printf(
// connection,
// "%s",
// "HTTP/1.1 501 Not Implemented\r\n"
// "Content-Length: 0\r\n\r\n");
//}
}
void HttpServer::ev_handler(struct mg_connection *nc, int ev, void *p)
{
m_logger->info("ev_handler event:{}", ev);
switch (ev)
{
case MG_EV_ACCEPT:
break;
case MG_EV_CLOSE:
break;
case MG_EV_CONNECT:
break;
case MG_EV_HTTP_CHUNK:
break;
case MG_EV_HTTP_REPLY:
break;
case MG_EV_HTTP_REQUEST:
{
//mg_serve_http(nc, (struct http_message *) p, s_http_server_opts);
struct http_message *hm = (struct http_message *)p;
//请求方式 get/post
std::string strMethod(hm->method.p, (hm->method.p + hm->method.len));
m_logger->info("Method:{}", strMethod.c_str());
//请求体
std::string strbody(hm->body.p, (hm->body.p + hm->body.len));
m_logger->info("Body:{}", strbody.c_str());
//请求uri
std::string struri(hm->uri.p, (hm->uri.p + hm->uri.len));
m_logger->info("Uri:{}", struri.c_str());
if (struri == "close" || struri=="/close")
{
HttpServer::Close(mgr);
break;
}
int i=0;
while(hm->header_names[i].p != NULL && hm->header_values[i].p != NULL)
{
std::string strKey(hm->header_names[i].p, (hm->header_names[i].p + hm->header_names[i].len));
std::string strValue(hm->header_values[i].p, (hm->header_values[i].p + hm->header_values[i].len));
m_logger->info("Header:{} {}" , strKey.c_str(), strValue.c_str());
i++;
}
//std::string struri(hm->header_names, (hm->uri.p + hm->uri.len));
SendData(nc, "Hello world");
}
break;
default:
break;
}
}
void HttpServer::AddHandler(const std::string &url, ReqHandler req_handler)
{
if (s_handler_map.find(url) != s_handler_map.end())
return;
s_handler_map.insert(std::make_pair(url, req_handler));
}
void HttpServer::RemoveHandler(const std::string &url)
{
auto it = s_handler_map.find(url);
if (it != s_handler_map.end())
s_handler_map.erase(it);
}
//支持跨域
void HttpServer::SendData(mg_connection * nc, const char* sdata)
{
if (!nc || strlen(sdata)<= 0)
{
mg_http_send_error(nc, 400, "Error : bad request");
return;
}
mg_send_head(nc, 200,strlen(sdata), "Access-Control-Allow-Origin: *");
mg_send(nc, sdata, strlen(sdata));
m_logger->info("SendData: {}", sdata);
}
//支持跨域
void HttpServer::SendResponse(mg_connection *connection,const char* rsp)
{
//必须先发送header, 也可以用HTTP/2.0
mg_printf(connection, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
//以json形式返回
mg_printf_http_chunk(connection, "{ \"result\":\"%s\" }", rsp);
//发送空白字符块,结束当前响应
mg_send_http_chunk(connection, "", 0);
connection->flags |= MG_F_SEND_AND_CLOSE;
m_logger->info("SendResponse {}", rsp);
}
bool HttpServer::Close(mg_mgr mgr)
{
mg_mgr_free(&mgr);
return true;
}