-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponse.h
183 lines (143 loc) · 4.96 KB
/
Response.h
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
#pragma once
#include <map>
#include <string>
#include <vector>
#include <memory>
#include <sstream>
#include <fstream>
#include "HttpStatus.h"
#include "Response.h"
#include "Cookie.h"
#include <algorithm>
#include <FlowUtils/FlowSParser.h>
#include <iterator>
class Response : public std::map<std::string, std::string> {
private:
std::unique_ptr<std::ifstream> stream;
std::string filePath;
std::size_t contentSize;
std::size_t leftToRead;
std::vector<unsigned char> data;
std::vector<Cookie> cookies;
public:
std::string HttpVersion;
HttpStatusCode StatusCode;
std::string ContentType;
Response() {
HttpVersion = "HTTP/1.1";
StatusCode = HttpStatusCode::OK;
ContentType = "text/plain";
contentSize = 0;
}
Response(bool isSSL) {
HttpVersion = isSSL ? "HTTPS/1.1" : "HTTP/1.1";
StatusCode = HttpStatusCode::OK;
ContentType = "text/plain";
contentSize = 0;
}
inline void SetContentSize(const size_t &contentSize) {
this->contentSize = contentSize;
}
inline void AddCookie(const Cookie &cookie) {
cookies.emplace_back(cookie);
}
inline void AddCookie(const std::string &name, const std::string &value) {
cookies.emplace_back(name, value);
}
inline std::string ToString() const {
using namespace std;
stringstream rtn;
rtn << Header();
if (!data.empty()) {
copy(data.begin(), data.end(), ostream_iterator<unsigned char>(rtn));
}
return rtn.str();
}
inline bool Range(const std::string &range) {
using namespace FlowSParser;
if (!(range.rfind("bytes=", 0) == 0))
return false;
size_t np;
size_t startOffset = nextSizeT(range.substr(6), &np);
if (startOffset < contentSize) {
stream->seekg(startOffset);
leftToRead -= startOffset;
} else {
return false;
}
size_t endOffset = contentSize - 1;
if (range.length() > np + 7 && range.at(np + 6) == '-') {
endOffset = nextSizeT(range.substr(np + 7), &np);
if (endOffset < contentSize && endOffset > startOffset) {
leftToRead = endOffset - startOffset + 1;
}
}
this->emplace("Content-Range", "bytes " + std::to_string(startOffset) + "-" + std::to_string(endOffset) + "/" +
std::to_string(contentSize));
if (contentSize != (endOffset - startOffset)) {
if (contentSize < leftToRead) {
return false;
}
contentSize = leftToRead;
this->StatusCode = HttpStatusCode::PartialContent;
}
return true;
}
inline size_t ReadData(unsigned char *data, const size_t bufferSize) {
if (leftToRead > bufferSize) {
stream->read(reinterpret_cast<char *>(&data[0]), bufferSize);
leftToRead -= bufferSize;
return bufferSize;
}
stream->read(reinterpret_cast<char *>(&data[0]), leftToRead);
auto toRtn = static_cast<size_t>(leftToRead);
leftToRead = 0;
return toRtn;
}
inline void Data(const std::vector<unsigned char> &data) {
this->data.insert(this->data.end(), data.begin(), data.end());
}
inline void Data(const std::string &data) {
copy(data.begin(), data.end(), back_inserter(this->data));
}
inline void LastModified(const std::string &lastModified) {
this->emplace("Last-Modified", lastModified);
}
inline std::string Header() const {
using namespace std;
stringstream rtn;
rtn << HttpVersion << " " << static_cast<int>(StatusCode) << " " << HttpStatus::codeToString(StatusCode);
for (auto kv : *this) {
rtn << endl << kv.first << ": " << kv.second;
}
rtn << endl;
if (HttpStatus::isError(StatusCode)) {
rtn << endl;
return rtn.str();
}
auto data_size = std::max(data.size(), contentSize);
rtn << "Content-Length: " << data_size << endl;
rtn << "Content-Type: " << ContentType;
for (auto cookie : cookies)
rtn << endl << "Set-Cookie: " << cookie.ToString();
rtn << endl << endl;
return rtn.str();
}
inline size_t GetContentSize() {
return contentSize;
}
inline void SetFile(const std::string &path) {
filePath = path;
stream = std::make_unique<std::ifstream>(filePath, std::ifstream::ate | std::ios::binary);
contentSize = static_cast<size_t>(stream->tellg());
stream->seekg(0, stream->beg);
leftToRead = contentSize;
}
inline std::vector<unsigned char> ToBytes() {
auto header = Header();
std::vector<unsigned char> rtn(header.begin(), header.end());
if (!data.empty())
rtn.insert(rtn.end(), data.begin(), data.end());
return rtn;
}
};