-
Notifications
You must be signed in to change notification settings - Fork 43
/
HTTPresponse.cpp
177 lines (157 loc) · 4.72 KB
/
HTTPresponse.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
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
// encode UTF-8
// @Author : Aged_cat
// @Date : 2021-05-21
#include "HTTPresponse.h"
const std::unordered_map<std::string, std::string> HTTPresponse::SUFFIX_TYPE = {
{ ".html", "text/html" },
{ ".xml", "text/xml" },
{ ".xhtml", "application/xhtml+xml" },
{ ".txt", "text/plain" },
{ ".rtf", "application/rtf" },
{ ".pdf", "application/pdf" },
{ ".word", "application/nsword" },
{ ".png", "image/png" },
{ ".gif", "image/gif" },
{ ".jpg", "image/jpeg" },
{ ".jpeg", "image/jpeg" },
{ ".au", "audio/basic" },
{ ".mpeg", "video/mpeg" },
{ ".mpg", "video/mpeg" },
{ ".avi", "video/x-msvideo" },
{ ".gz", "application/x-gzip" },
{ ".tar", "application/x-tar" },
{ ".css", "text/css "},
{ ".js", "text/javascript "},
};
const std::unordered_map<int, std::string> HTTPresponse::CODE_STATUS = {
{ 200, "OK" },
{ 400, "Bad Request" },
{ 403, "Forbidden" },
{ 404, "Not Found" },
};
const std::unordered_map<int, std::string> HTTPresponse::CODE_PATH = {
{ 400, "/400.html" },
{ 403, "/403.html" },
{ 404, "/404.html" },
};
HTTPresponse::HTTPresponse() {
code_ = -1;
path_ = srcDir_ = "";
isKeepAlive_ = false;
mmFile_ = nullptr;
mmFileStat_ = { 0 };
};
HTTPresponse::~HTTPresponse() {
unmapFile_();
}
void HTTPresponse::init(const std::string& srcDir, std::string& path, bool isKeepAlive, int code){
assert(srcDir != "");
if(mmFile_) { unmapFile_(); }
code_ = code;
isKeepAlive_ = isKeepAlive;
path_ = path;
srcDir_ = srcDir;
mmFile_ = nullptr;
mmFileStat_ = { 0 };
}
void HTTPresponse::makeResponse(Buffer& buff) {
/* 判断请求的资源文件 */
if(stat((srcDir_ + path_).data(), &mmFileStat_) < 0 || S_ISDIR(mmFileStat_.st_mode)) {
code_ = 404;
}
else if(!(mmFileStat_.st_mode & S_IROTH)) {
code_ = 403;
}
else if(code_ == -1) {
code_ = 200;
}
errorHTML_();
addStateLine_(buff);
addResponseHeader_(buff);
addResponseContent_(buff);
}
char* HTTPresponse::file() {
return mmFile_;
}
size_t HTTPresponse::fileLen() const {
return mmFileStat_.st_size;
}
void HTTPresponse::errorHTML_() {
if(CODE_PATH.count(code_) == 1) {
path_ = CODE_PATH.find(code_)->second;
stat((srcDir_ + path_).data(), &mmFileStat_);
}
}
void HTTPresponse::addStateLine_(Buffer& buff) {
std::string status;
if(CODE_STATUS.count(code_) == 1) {
status = CODE_STATUS.find(code_)->second;
}
else {
code_ = 400;
status = CODE_STATUS.find(400)->second;
}
buff.append("HTTP/1.1 " + std::to_string(code_) + " " + status + "\r\n");
}
void HTTPresponse::addResponseHeader_(Buffer& buff) {
buff.append("Connection: ");
if(isKeepAlive_) {
buff.append("keep-alive\r\n");
buff.append("keep-alive: max=6, timeout=120\r\n");
} else{
buff.append("close\r\n");
}
buff.append("Content-type: " + getFileType_() + "\r\n");
}
void HTTPresponse::addResponseContent_(Buffer& buff) {
int srcFd = open((srcDir_ + path_).data(), O_RDONLY);
if(srcFd < 0) {
errorContent(buff, "File NotFound!");
return;
}
// 将文件映射到内存提高文件的访问速度
// MAP_PRIVATE 建立一个写入时拷贝的私有映射
int* mmRet = (int*)mmap(0, mmFileStat_.st_size, PROT_READ, MAP_PRIVATE, srcFd, 0);
if(*mmRet == -1) {
errorContent(buff, "File NotFound!");
return;
}
mmFile_ = (char*)mmRet;
close(srcFd);
buff.append("Content-length: " + std::to_string(mmFileStat_.st_size) + "\r\n\r\n");
}
void HTTPresponse::unmapFile_() {
if(mmFile_) {
munmap(mmFile_, mmFileStat_.st_size);
mmFile_ = nullptr;
}
}
std::string HTTPresponse::getFileType_() {
/* 判断文件类型 */
std::string::size_type idx = path_.find_last_of('.');
if(idx == std::string::npos) {
return "text/plain";
}
std::string suffix = path_.substr(idx);
if(SUFFIX_TYPE.count(suffix) == 1) {
return SUFFIX_TYPE.find(suffix)->second;
}
return "text/plain";
}
void HTTPresponse::errorContent(Buffer& buff, std::string message)
{
std::string body;
std::string status;
body += "<html><title>Error</title>";
body += "<body bgcolor=\"ffffff\">";
if(CODE_STATUS.count(code_) == 1) {
status = CODE_STATUS.find(code_)->second;
} else {
status = "Bad Request";
}
body += std::to_string(code_) + " : " + status + "\n";
body += "<p>" + message + "</p>";
body += "<hr><em>TinyWebServer</em></body></html>";
buff.append("Content-length: " + std::to_string(body.size()) + "\r\n\r\n");
buff.append(body);
}