-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseConfig.cpp
More file actions
112 lines (95 loc) · 3.63 KB
/
ResponseConfig.cpp
File metadata and controls
112 lines (95 loc) · 3.63 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include"ResponseConfig.hpp"
std::map<std::string, std::string> ResponseConfig::statusCodeRepo;
std::map<std::string, std::string> ResponseConfig::cgiAddressRepo;
std::set<std::string> ResponseConfig::supportedTypesRepo;
ResponseConfig::ResponseConfig()
{
setHttpStatusCode(); // StatusCode 초기화 설정
setHttpContentType();
}
ResponseConfig::~ResponseConfig()
{}
std::string ResponseConfig::getHttpStatusMsg(std::string key)
{
return (statusCodeRepo[key]);
}
void ResponseConfig::putHttpStatusCode(std::string key, std::string value)
{
statusCodeRepo[key] = value;
}
void ResponseConfig::setHttpStatusCode(void)
{
int i;
std::string key[35] = {
"405", "200", "201", "202", "206", "301", "302", "303", "307", "400", "401", "404", "406", "408", "411", "413", "414", "416", "431", "400", "401", "404", "406", "408", "411", "413", "414", "416", "431", "500", "501", "417", ""
};
std::string value[35] = {
"Method Not Allowed", "OK","Created","Accepted","Partial Content","Moved Permanently","Found","See Other","Temporary Redirect","Bad Request","Unauthorized","Not Found","Not Acceptable","Request Timeout","Length Required","Payload Too Large","URI Too Long","Requested Range Not Satisfiable","Request Header Fields Too Large","Bad Request","Unauthorized","Not Found","Not Acceptable","Request Timeout","Length Required","Payload Too Large","URI Too Long","Requested Range Not Satisfiable","Request Header Fields Too Large","Internal Server Error","Not Implemented", "Expectation Failed", ""
};
i = 0;
while (key[i].length() != 0)
{
statusCodeRepo.insert(std::make_pair(key[i], value[i]));
i++;
}
}
std::string ResponseConfig::getCurrentDate()
{
std::time_t currentTime_t;
std::time(¤tTime_t);
std::tm* localTime = std::localtime(¤tTime_t);
char buffer[80];
std::strftime(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S GMT", localTime);
return buffer;
}
void ResponseConfig::setHttpContentType(void)
{
// text 타입
supportedTypesRepo.insert("text/html");
supportedTypesRepo.insert("text/plain");
supportedTypesRepo.insert("text/css");
supportedTypesRepo.insert("text/javascript"); // 또는 application/javascript
// 애플리케이션 타입
supportedTypesRepo.insert("application/json");
supportedTypesRepo.insert("application/xml");
supportedTypesRepo.insert("application/x-www-form-urlencoded");
supportedTypesRepo.insert("application/pdf");
supportedTypesRepo.insert("application/octet-stream");
// 이미지 타입
supportedTypesRepo.insert("image/jpeg");
supportedTypesRepo.insert("image/png");
supportedTypesRepo.insert("image/gif");
supportedTypesRepo.insert("image/webp");
supportedTypesRepo.insert("image/x-icon");
// 오디오/비디오 타입
supportedTypesRepo.insert("audio/mpeg");
supportedTypesRepo.insert("audio/ogg");
supportedTypesRepo.insert("video/mp4");
supportedTypesRepo.insert("video/webm");
}
std::string ResponseConfig::getContentType(std::string filename)
{
std::set<std::string>::iterator iter;
std::string extension;
std::string contentType;
int i;
contentType = "text/plain";
for (i = filename.length() - 1; i >= 0; i--)
{
if (filename[i] == '.')
break ;
}
extension = filename.substr(i + 1);
for (iter = supportedTypesRepo.begin(); iter != supportedTypesRepo.end(); iter++)
{
if ((*iter).find(extension) != std::string::npos)
{
contentType = *iter;
break ;
}
}
return (contentType);
}
//================================================================================================//
// 필수적인 설정 부분 //
//================================================================================================//