-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathPlainReaderImpl.cpp
54 lines (40 loc) · 1.24 KB
/
PlainReaderImpl.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
#include "restc-cpp/restc-cpp.h"
#include "restc-cpp/Connection.h"
#include "restc-cpp/Socket.h"
#include "restc-cpp/DataReader.h"
#include "restc-cpp/error.h"
using namespace std;
namespace restc_cpp {
class PlainReaderImpl : public DataReader {
public:
PlainReaderImpl(size_t contentLength, ptr_t&& source)
: remaining_{contentLength},
source_{std::move(source)} {}
[[nodiscard]] bool IsEof() const override { return remaining_ == 0; }
void Finish() override {
if (source_) {
source_->Finish();
}
}
::restc_cpp::boost_const_buffer ReadSome() override {
if (IsEof()) {
return {nullptr, 0};
}
auto buffer = source_->ReadSome();
const auto bytes = boost::asio::buffer_size(buffer);
if ((static_cast<int64_t>(remaining_)
- static_cast<int64_t>(bytes)) < 0) {
throw ProtocolException("Body-size exceeds content-size");
}
remaining_ -= bytes;
return buffer;
}
private:
size_t remaining_;
ptr_t source_;
};
DataReader::ptr_t
DataReader::CreatePlainReader(size_t contentLength, ptr_t&& source) {
return make_unique<PlainReaderImpl>(contentLength, std::move(source));
}
} // namespace