-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathHTTPResponse.cpp
68 lines (55 loc) · 1.37 KB
/
HTTPResponse.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
#include <sstream>
#include "HTTPResponse.h"
using namespace std;
HTTPResponse::HTTPResponse() {
this->streaming = false;
this->contentType = "text/html; charset=ISO-8859-1";
this->headers["Server"] = "Gunrock Web";
this->status = 200;
}
void HTTPResponse::withStreaming() {
this->streaming = true;
}
void HTTPResponse::setHeader(string name, string value) {
this->headers[name] = value;
}
void HTTPResponse::setBody(string data) {
body = data;
}
int HTTPResponse::getStatus() {
return status;
}
void HTTPResponse::setContentType(string contentType) {
this->contentType = contentType;
}
void HTTPResponse::setStatus(int status) {
this->status = status;
}
string HTTPResponse::statusToString() {
if (status == 200) {
return "OK";
} else {
return "Unknown";
}
}
string HTTPResponse::response() {
stringstream out;
setHeader("Content-Type", contentType);
if (streaming) {
setHeader("Transfer-Encoding", "chunked");
} else {
stringstream len;
len << body.size();
setHeader("Content-Length", len.str());
}
out << "HTTP/1.1 " << status << " " << statusToString() << "\r\n";
map<string, string>::iterator iter;
for(iter = headers.begin(); iter != headers.end(); iter++) {
out << iter->first << ": " << iter->second << "\r\n";
}
out << "\r\n";
if (body.size() > 0 && !streaming) {
out << body;
}
return out.str();
}