-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.cpp
More file actions
58 lines (53 loc) · 1.63 KB
/
response.cpp
File metadata and controls
58 lines (53 loc) · 1.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
#include "response.h"
SLOWSRes::SLOWSRes() {
Headers.insert({"Version", "HTTP/1.1"});
}
void SLOWSRes::Send(int clientSocket, std::string body) {
std::stringstream response;
response << "HTTP/1.1 " << Status << " " << StatusDescription << "\r\n";
for (auto header : Headers)
response << header.first << ": " << header.second << "\r\n";
response << "Content-Length: " << body.length();
response << "\r\n\r\n";
response << body;
int result =
send(clientSocket, response.str().c_str(), response.str().length(), 0);
if (result == -1) {
perror("Socket send Error");
close(clientSocket);
exit(EXIT_FAILURE);
}
}
void SLOWSRes::Send(int clientSocket) {
std::stringstream response;
response << "HTTP/1.1 " << Status << " " << StatusDescription << "\r\n";
for (auto header : Headers)
response << header.first << ": " << header.second << "\r\n";
int result =
send(clientSocket, response.str().c_str(), response.str().length(), 0);
if (result == -1) {
perror("Socket send Error");
close(clientSocket);
exit(EXIT_FAILURE);
}
}
void SLOWSRes::PushHeader(std::string name, std::string value) {
auto search = Headers.find(name);
if (search != Headers.end())
Headers.erase(search);
Headers.insert({name, value});
}
void SLOWSRes::SetStatus(short status) {
switch (status) {
case 200:
StatusDescription = "OK";
break;
case 201:
StatusDescription = "Created";
break;
case 202:
StatusDescription = "Accepted";
break;
}
}
SLOWSRes::~SLOWSRes() {}