-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyweb.h
98 lines (95 loc) · 2.04 KB
/
myweb.h
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
#pragma once
#pragma execution_character_set("utf-8")
#include<iostream>
#include<vector>
#include<format>
#include<map>
#include<sstream>
class ztWeb {
private:
struct Header
{
std::string first;
std::string second;
};
std::vector<Header> headers;
std::vector<std::string> mess;
std::map<std::string, std::string> query;
public:
std::string getQuery(std::string s) {
return query[s].size() ? query[s] : "无";
}
public:
void addHeader(std::string fir, std::string sec) {
headers.push_back({
fir,
sec
});
}
void send() {
for (Header i : headers) {
std::cout << std::format("{}: {}\r", i.first, i.second);
}
std::cout << std::format("\n\r\n");
for (std::string i : mess) {
std::cout <<std::format("{}\r",i);
}
}
void addBody(std::string mes) {
mess.push_back(mes);
}
ztWeb() {
std::string Query{getenv("QUERY_STRING")};
decodeUrl(Query);
std::map<std::string, std::string> queryMap;
m_to_query(Query);
}
private:
void decodeUrl(std::string &encodedUrl) {
std::stringstream decoded;
for (size_t i{ 0 }; i < encodedUrl.size(); i++) {
if (encodedUrl[i] == '%') {
std::string encodedChar = encodedUrl.substr(i + 1, 2);
int charCode{ 0 };
std::stringstream ss;
ss << std::hex << encodedChar;
ss >> charCode;
decoded << (char)charCode;
i += 2;
}
else if (encodedUrl[i] == '+')
{
decoded << " ";
}
else {
decoded << encodedUrl[i];
}
}
encodedUrl = decoded.str();
}
void m_to_query(std::string& s) {
std::vector<std::string> v;
m_split(s, "&", v);
std::vector<std::string> querys;
for (std::string i : v) {
m_split(i, "=", querys);
if (querys.size()>=2)
{
query[querys[0]] = querys[1];
}
}
}
void m_split(std::string& s,std::string to_split ,std::vector<std::string>& v) {
std::string myStr = s;
v.clear();
int sf{ 0 };
while (sf = myStr.find(to_split),sf != std::string::npos) {
v.push_back(myStr.substr(0, sf));
myStr = myStr.substr(sf + 1, myStr.size());
}
if (myStr.size()>0)
{
v.push_back(myStr);
}
}
};