-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresponse.cpp
53 lines (40 loc) · 1.24 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
#include "request_handler.h"
#include <string>
#include <vector>
namespace http {
namespace server {
void Response::SetStatus(const ResponseCode response_code) {
responseStatus = response_code;
}
void Response::AddHeader(const std::string& header_name, const std::string& header_value) {
mHeaders.push_back(std::make_pair(header_name, header_value));
}
void Response::SetBody(const std::string& body) {
content = body;
}
Response::ResponseCode Response::GetStatus(){
return responseStatus;
}
std::string Response::ToString() {
std::string responseString = "";
switch(responseStatus) {
case OK:
responseString += "HTTP/1.1 200 OK\r\n";
break;
case BAD_REQUEST:
responseString += "HTTP/1.1 400 Bad Request\r\n";
break;
case NOT_FOUND:
responseString += "HTTP/1.1 404 Not Found\r\n";
break;
}
for(std::vector<std::pair<std::string, std::string>>::const_iterator it = mHeaders.begin(); it != mHeaders.end(); it++){
responseString += it->first + ": " + it->second + "\r\n";
}
//need to end headers with carriage return
responseString += "\r\n";
responseString += content;
return responseString;
}
} // namespace server
} // namespace http