-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathHttp_Server.cpp
More file actions
97 lines (82 loc) · 1.78 KB
/
Http_Server.cpp
File metadata and controls
97 lines (82 loc) · 1.78 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
#include <stdio.h>
#include <stdlib.h>
#include <string_view>
#include <functional>
#include <map>
#include "HttpServer.h"
#include "EventLoop.h"
#include <iostream>
#include "HttpParam.h"
#include "HttpConnect.h"
const char * html = R"(<html>
<body>
<h1>login</h1>
<p>hello world!</p>
<form action="login" method="post">
<input type="text" name="user"/>
<input type="password" name="pass"/>
<input type="submit" value="login"/>
</form>
</body>
</html>)";
const char * html2 = R"(<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.get("/test",function(data,status){
alert("data:" + data + "\nstate:" + status);
});
});
});
</script>
</head>
<body>
<button>send</button>
</body>
</html>)";
const char * succeed = ""
"<html>"
"<body>"
"<h1>login succeed</h1>"
"</body>"
"</html>";
const char * failing = ""
"<html>"
"<body>"
"<h1>login failing</h1>"
"</body>"
"</html>";
#include "XFile.h"
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(html);
/*
std::string s;
XFile::readFile("test.html", s);
conn->sendMsg(s);
*/
});
server.addGet("/test", [](HttpConnect *conn, std::string_view & data) {
conn->autoMsg("hello world");
});
server.addPost("/login", [](HttpConnect *conn, std::string_view & data) {
HttpParam hp(data);
if (hp.getStr("user") == "jw" && hp.getStr("pass") == "1111")
{
conn->autoMsg(succeed);
}
else
{
conn->autoMsg(failing);
}
});
EventLoop::Instance()->run();
return 0;
}