-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathDataReaderStream.cpp
242 lines (199 loc) · 6.66 KB
/
DataReaderStream.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "restc-cpp/DataReaderStream.h"
#include "restc-cpp/error.h"
#include "restc-cpp/url_encode.h"
#include "restc-cpp/logging.h"
#include <restc-cpp/typename.h>
using namespace std;
namespace restc_cpp {
DataReaderStream::DataReaderStream(std::unique_ptr<DataReader>&& source)
: source_{move(source)} {
RESTC_CPP_LOG_TRACE_("DataReaderStream: Chained to "
<< RESTC_CPP_TYPENAME(decltype(*source_)));
}
void DataReaderStream::Fetch() {
if (++curr_ >= end_) {
auto buf = source_->ReadSome();
RESTC_CPP_LOG_TRACE_("DataReaderStream::Fetch: Fetched buffer with "
<< boost::asio::buffer_size(buf) << " bytes.");
const auto bytes = boost::asio::buffer_size(buf);
if (bytes == 0) {
RESTC_CPP_LOG_TRACE_("DataReaderStream::Fetch: EOF");
throw ProtocolException("Fetch(): EOF");
}
curr_ = boost::asio::buffer_cast<const char *>(buf);
end_ = curr_ + boost::asio::buffer_size(buf);
}
}
boost::asio::const_buffers_1
DataReaderStream::ReadSome() {
Fetch();
boost::asio::const_buffers_1 rval = {curr_,
static_cast<size_t>(end_ - curr_)};
curr_ = end_;
RESTC_CPP_LOG_TRACE_("DataReaderStream::ReadSome: Returning buffer with "
<< boost::asio::buffer_size(rval) << " bytes.");
if (source_->IsEof()) {
SetEof();
}
return rval;
}
boost::asio::const_buffers_1
DataReaderStream::GetData(size_t maxBytes) {
Fetch();
const auto diff = end_ - curr_;
assert(diff >= 0);
const auto seg_len = std::min<size_t>(maxBytes, diff);
boost::asio::const_buffers_1 rval = {curr_, seg_len};
if (seg_len > 0) {
curr_ += seg_len - 1;
}
RESTC_CPP_LOG_TRACE_("DataReaderStream::GetData(" << maxBytes << "): Returning buffer with "
<< boost::asio::buffer_size(rval) << " bytes.");
return rval;
}
void DataReaderStream::ReadServerResponse(Reply::HttpResponse& response)
{
static const string http_1_0{"HTTP/1.0"}; //some proxies use HTTP/1.0
static const string http_1_1{"HTTP/1.1"};
constexpr size_t max_version_len = 16;
constexpr size_t max_phrase_len = 256;
char ch = {};
getc_bytes_ = 0;
// Get HTTP version
std::string value;
for(ch = Getc(); ch != ' '; ch = Getc()) {
value += ch;
if (value.size() > max_version_len) {
throw ProtocolException("ReadHeaders(): Too much HTTP version!");
}
}
if (ch != ' ') {
throw ProtocolException("ReadHeaders(): No space after HTTP version");
}
if (value.empty()) {
throw ProtocolException("ReadHeaders(): No HTTP version");
}
if (ciEqLibC()(value, http_1_1) ||
ciEqLibC()(value, http_1_0)) {
; // Do nothing HTTP 1.1 is the default value
} else {
throw ProtocolException(
string("ReadHeaders(): unsupported HTTP version: ")
+ url_encode(value));
}
// Get response code
value.clear();
for(ch = Getc(); ch != ' '; ch = Getc()) {
value += ch;
if (value.size() > 3) {
throw ProtocolException("ReadHeaders(): Too much HTTP response code!");
}
}
if (value.size() != 3) {
throw ProtocolException(
string("ReadHeaders(): Incorrect length of HTTP response code!: ")
+ value);
}
response.status_code = stoi(value);
if (ch != ' ') {
throw ProtocolException("ReadHeaders(): No space after HTTP response code");
}
// Get response text
value.clear();
for(ch = Getc(); ch != '\r'; ch = Getc()) {
value += ch;
if (value.size() > max_phrase_len) {
throw ConstraintException("ReadHeaders(): Too long HTTP response phrase!");
}
}
// Skip CRLF
assert(ch == '\r');
ch = Getc();
if (ch != '\n') {
throw ProtocolException("ReadHeaders(): No CR/LF after HTTP response phrase!");
}
response.reason_phrase = move(value);
RESTC_CPP_LOG_TRACE_("ReadServerResponse: getc_bytes is " << getc_bytes_);
RESTC_CPP_LOG_TRACE_("HTTP Response: "
<< (response.http_version == Reply::HttpResponse::HttpVersion::HTTP_1_1
? "HTTP/1.1" : "???")
<< ' ' << response.status_code
<< ' ' << response.reason_phrase);
}
void DataReaderStream::ReadHeaderLines(const add_header_fn_t& addHeader) {
constexpr size_t max_name_len = 256;
constexpr size_t max_headers = 256;
while(true) {
char ch;
string name;
string value;
for(ch = Getc(); ch != '\r'; ch = Getc()) {
if (ch == ' ' || ch == '\t') {
continue;
}
if (ch == ':') {
value = GetHeaderValue();
ch = '\n';
break;
}
name += ch;
if (name.size() > max_name_len) {
throw ConstraintException("Chunk Trailer: Header name too long!");
}
}
if (ch == '\r') {
ch = Getc();
}
if (ch != '\n') {
throw ProtocolException("Chunk Trailer: Missing LF after parse!");
}
if (name.empty()) {
if (!value.empty()) {
throw ProtocolException("Chunk Trailer: Header value without name!");
}
RESTC_CPP_LOG_TRACE_("ReadHeaderLines: getc_bytes is " << getc_bytes_);
getc_bytes_ = 0;
return; // An empty line marks the end of the trailer
}
if (++num_headers_ > max_headers) {
throw ConstraintException("Chunk Trailer: Too many lines in header!");
}
RESTC_CPP_LOG_TRACE_(name << ": " << value);
addHeader(move(name), move(value));
name.clear();
value.clear();
}
}
std::string DataReaderStream::GetHeaderValue() {
constexpr size_t max_header_value_len = 1024 * 4;
std::string value;
char ch;
while(true) {
for (ch = Getc(); ch == ' ' || ch == '\t'; ch = Getc())
; // skip space
for (; ch != '\r'; ch = Getc()) {
value += ch;
if (value.size() > max_header_value_len) {
throw ConstraintException("Chunk Trailer: Header value too long!");
}
}
if (ch != '\r') {
throw ProtocolException("Chunk Trailer: Missing CR!");
}
if ((ch = Getc()) != '\n') {
throw ProtocolException("Chunk Trailer: Missing LF!");
}
// Peek
ch = Getc();
if ((ch != ' ') && (ch != '\t')) {
Ungetc();
return value;
}
value += ' ';
}
}
void DataReaderStream::SetEof() {
RESTC_CPP_LOG_TRACE_("Reached EOF");
eof_ = true;
}
} // namespace