-
Notifications
You must be signed in to change notification settings - Fork 0
/
Response.cpp
183 lines (164 loc) · 5.16 KB
/
Response.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
178
179
180
181
182
183
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Response.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eej-jama <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/25 19:34:28 by hlakhal- #+# #+# */
/* Updated: 2024/02/04 19:54:45 by eej-jama ### ########.fr */
/* */
/* ************************************************************************** */
#include"Response.hpp"
Response::Response()
{
this->status_header = false;
this->status = true;
HttpStatusMessages[200] = " OK";
HttpStatusMessages[201] = " Created";
HttpStatusMessages[204] = " No Content";
HttpStatusMessages[301] = " Moved Permanently";
HttpStatusMessages[400] = " Bad Request";
HttpStatusMessages[401] = " Unauthorized";
HttpStatusMessages[402] = " Payment Required";
HttpStatusMessages[403] = " Forbidden";
HttpStatusMessages[404] = " Not Found";
HttpStatusMessages[405] = " Method Not Allowed";
HttpStatusMessages[408] = " Request Timeout";
HttpStatusMessages[413] = " Request Entity Too Large";
HttpStatusMessages[500] = " Internal Server Error";
HttpStatusMessages[501] = " Not Implemented";
HttpStatusMessages[502] = " Bad Gateway";
HttpStatusMessages[503] = " Service Unavailable";
HttpStatusMessages[504] = " Gateway Timeout";
HttpStatusMessages[505] = " HTTP Version Not Supported";
}
Response::Response(bool status,int fd, int status_code, std::string path)
{
this->status_header = false;
this->status = status;
this->status_code = status_code;
this->path = path;
this->fd = fd;
}
bool Response::getStatusClinet() const
{
return this->status_close;
}
void Response::setStatusHeader(bool status_header)
{
this->status_header = status_header;
}
const std::string& Response::getPathFile()
{
return this->path;
}
void Response::openFile(const std::string& path)
{
file.open(path.c_str(), std::ios::in | std::ios::binary);
}
std::ifstream& Response::getFile()
{
return this->file;
}
std::string Response::getBody()
{
return this->body;
}
void Response::closeFile()
{
this->file.close();
}
void Response::setValues(bool status,int fd, int status_code, std::string path, std::string type,std::string content, bool isCgi)
{
this->status = status;
this->status_code = status_code;
this->path = path;
this->fd = fd;
this->type = type;
this->body = content;
this->isCgi = isCgi;
}
bool Response::getStatusResponse() const
{
return this->status;
}
int Response::getStatusCode() const
{
return this->status_code;
}
Response::~Response()
{
}
std::string Response::getHeader()
{
std::string start_line, header, result,cache, name_server, location;
std::stringstream ss;
ss << this->status_code;
result = ss.str();
name_server = "test";
if (result == "301" || result == "201")
location = "\r\nLocation: " + this->path;
if (result == "204")
{
start_line = "HTTP/1.1 " + result + HttpStatusMessages[status_code] +"\r\n";
header = "Server: YourServer\r\n";
header += "Content-Length: 0\r\n";
header += "\r\n";
return start_line + header;
}
start_line = "HTTP/1.1 " + result + HttpStatusMessages[status_code] + "\r\n";
header = "Server: " + name_server + "\r\n" + "Content-Type: " + type + location + "\r\n\r\n";
start_line += header;
return start_line;
}
Response& Response::operator=(const Response& other)
{
if (this != &other)
{
status = other.status;
fd = other.fd;
status_code = other.status_code;
status_close = other.status_close;
header = other.header;
body = other.body;
path = other.path;
type = other.type;
this->isCgi = other.isCgi;
status_header = other.status_header;
if (file.is_open())
file.close();
if (other.file.is_open())
file.open(other.path.c_str(), std::ios::in | std::ios::binary);
}
return *this;
}
bool Response::getStatusHeader() const
{
return this->status_header;
}
bool Response::getStatustCgi() const
{
return this->isCgi;
}
Response::Response(const Response& other)
{
status = other.status;
fd = other.fd;
status_code = other.status_code;
status_close = other.status_close;
header = other.header;
body = other.body;
path = other.path;
type = other.type;
this->isCgi = other.isCgi;
status_header = other.status_header;
if (file.is_open())
file.close();
if (other.file.is_open())
file.open(other.path.c_str(), std::ios::in | std::ios::binary);
}
const std::map<int, std::string>& Response::getHttpStatusMessages() const
{
return HttpStatusMessages;
}