6 Link::Response::Response() {
7 this->SetHeadersRaw("HTTP/1.1 200 OK\r\n")->SetBody("");
8 this->SetInstanceType("Server");
12 Link::Response::Response(std::string header
, std::string body
) {
13 this->SetHeadersRaw(header
);
15 this->SetInstanceType("Client");
19 Link::Response
* Link::Response::SetInstanceType(std::string type
) {
20 this->instanceType
= type
;
24 bool Link::Response::InstanceOf(std::string type
) {
25 return this->instanceType
== type
;
28 Link::Response
* Link::Response::Close() {
33 bool Link::Response::isClosed() {
37 Link::Response
* Link::Response::SetHeader(std::string key
, std::string value
) {
38 std::transform(key
.begin(), key
.end(), key
.begin(), ::tolower
);
39 this->headers
[key
] = value
;
43 std::string
decompress(std::string data
) {
48 strm
.avail_in
= data
.size();
49 strm
.next_in
= (Bytef
*)data
.data();
50 inflateInit2(&strm
, 16 + MAX_WBITS
);
51 char outbuffer
[32768];
52 std::string outstring
;
54 strm
.avail_out
= 32768;
55 strm
.next_out
= (Bytef
*)outbuffer
;
56 inflate(&strm
, Z_NO_FLUSH
);
57 if (outstring
.size() < strm
.total_out
) {
58 outstring
.append(outbuffer
, strm
.total_out
- outstring
.size());
60 } while (strm
.avail_out
== 0);
65 std::string
deflate(std::string data
) {
70 strm
.avail_in
= data
.size();
71 strm
.next_in
= (Bytef
*)data
.data();
72 deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
, Z_DEFLATED
, 16 + MAX_WBITS
, 8, Z_DEFAULT_STRATEGY
);
73 char outbuffer
[32768];
74 std::string outstring
;
76 strm
.avail_out
= 32768;
77 strm
.next_out
= (Bytef
*)outbuffer
;
78 deflate(&strm
, Z_FINISH
);
79 if (outstring
.size() < strm
.total_out
) {
80 outstring
.append(outbuffer
, strm
.total_out
- outstring
.size());
82 } while (strm
.avail_out
== 0);
87 Link::Response
* Link::Response::SetBody(std::string body
) {
88 this->SetHeader("Content-Length", std::to_string(body
.size()));
89 this->SetHeader("Transfer-Encoding", "");
91 if (this->InstanceOf("Server")) {
92 this->SetHeader("Content-Encoding", "");
95 if (this->GetHeader("Content-Encoding") == "gzip") {
96 this->body
= decompress(body
);
99 if (this->GetHeader("Content-Encoding") == "deflate") {
100 this->body
= deflate(body
);
106 Link::Response
* Link::Response::SetRawHeader(std::string key
, std::string value
) {
107 this->headers
[key
] = value
;
111 Link::Response
* Link::Response::SetHeadersRaw(std::string headersRaw
) {
112 this->headersRaw
= headersRaw
;
113 std::string line
= headersRaw
.substr(0, headersRaw
.find("\r\n"));
114 this->version
= line
.substr(0, line
.find(" "));
115 line
= line
.substr(line
.find(" ") + 1);
116 this->status
= std::stoi(line
);
118 std::istringstream
iss(headersRaw
.substr(headersRaw
.find("\r\n") + 2));
120 while (std::getline(iss
, l
)) {
122 std::string key
= l
.substr(0, l
.find(":"));
123 std::string value
= l
.substr(l
.find(":") + 2);
124 value
= value
.substr(0, value
.find("\r"));
125 this->SetHeader(key
, value
);
130 Link::Response
* Link::Response::SetStatus(int status
) {
131 this->status
= status
;
135 Link::Response
* Link::Response::SetVersion(std::string version
) {
136 this->version
= version
;
140 std::string
Link::Response::GetHeader(std::string key
) {
141 std::string lower
= key
;
142 std::transform(lower
.begin(), lower
.end(), lower
.begin(), ::tolower
);
143 return this->headers
[lower
];
146 std::string
Link::Response::GetBody() {
150 std::string
Link::Response::GetHeadersRaw() {
151 std::string headers
= this->version
+ " " + std::to_string(this->status
) + " " + Link::Status(this->status
) + "\r\n";
152 for (auto const& x
: this->headers
) {
153 if (x
.second
!= "") headers
+= x
.first
+ ": " + x
.second
+ "\r\n";
158 std::string
Link::Response::GetVersion() {
159 return this->version
;
162 int Link::Response::GetStatus() {