-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_utils.cpp
50 lines (43 loc) · 1.43 KB
/
server_utils.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
#include "Server.hpp"
std::string trim(const std::string &str) {
size_t first = str.find_first_not_of(WHITESPACES);
if (first == std::string::npos)
return str;
size_t last = str.find_last_not_of(WHITESPACES);
return str.substr(first, (last - first + 1));
}
std::pair<std::string, std::string> find_page(std::string &path) {
size_t pos = 0;
std::vector<std::string> allowed_requests;
allowed_requests.push_back("GET");
allowed_requests.push_back("POST");
allowed_requests.push_back("HEAD");
allowed_requests.push_back("PUT");
allowed_requests.push_back("DELETE");
for (size_t i = 0; i < allowed_requests.size(); i++) {
if (path.find(allowed_requests[i]) != std::string::npos) {
pos = i;
break;
}
}
std::string page;
std::string::size_type end = path.find("HTTP") - 1;
std::string::size_type start = path.find("/");
page = path.substr(start, end - start);
return (
std::make_pair<std::string, std::string>(allowed_requests[pos], page));
}
std::string content_typer(std::vector<std::string> &content_type, int index) {
std::string content_type_str;
std::string temp;
if (index > 2) {
content_type_str = "img/";
temp = content_type[index].substr(1, content_type[index].size());
content_type_str += temp;
} else {
content_type_str = "text/";
temp = content_type[index].substr(1, content_type[index].size());
content_type_str += temp;
}
return content_type_str;
}