-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathHttp_Ext.cpp
More file actions
59 lines (50 loc) · 1.19 KB
/
Http_Ext.cpp
File metadata and controls
59 lines (50 loc) · 1.19 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
#include <stdio.h>
#include <map>
#include <string_view>
#include <functional>
#include "HttpServer.h"
#include "EventLoop.h"
#include "HttpConnect.h"
#include "HttpParser.h"
#include <iostream>
class HttpExt
{
public:
void printField(const char* p, int len)
{
std::string_view v(p, len);
std::cout << v <<": ";
}
void printValue(const char* p, int len)
{
std::string_view v(p, len);
std::cout << v << std::endl;
}
};
HttpExt httpext;
int main()
{
EventLoop::Instance()->init();
HttpServer server(EventLoop::Instance());
server.listen("127.0.0.1", 80);
server.addGet("/", [](HttpConnect* conn, std::string_view& data) {
conn->autoMsg("hello world");
});
HttpSettings* setting = getHttpSettings();
setting->on_message_begin = [](llhttp_t* http)->int {
http->data = &httpext;
return 0;
};
setting->on_header_field = [](llhttp_t* http, const char* at, size_t length)->int {
HttpExt* ext = (HttpExt*)(http->data);
ext->printField(at, length);
return 0;
};
setting->on_header_value = [](llhttp_t* http, const char* at, size_t length)->int {
HttpExt* ext = (HttpExt*)(http->data);
ext->printValue(at, length);
return 0;
};
EventLoop::Instance()->run();
return 0;
}