2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/net/HttpChunkedByteFlow.h"
|
|
|
|
|
|
|
|
#include "td/utils/find_boundary.h"
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
namespace td {
|
2018-02-12 11:37:54 +01:00
|
|
|
|
2020-07-22 20:52:00 +02:00
|
|
|
bool HttpChunkedByteFlow::loop() {
|
|
|
|
bool result = false;
|
|
|
|
do {
|
2020-01-19 01:02:56 +01:00
|
|
|
if (state_ == State::ReadChunkLength) {
|
2018-12-31 20:04:05 +01:00
|
|
|
bool ok = find_boundary(input_->clone(), "\r\n", len_);
|
|
|
|
if (len_ > 10) {
|
2020-07-22 20:52:00 +02:00
|
|
|
finish(Status::Error(PSLICE() << "Too long length in chunked "
|
|
|
|
<< input_->cut_head(len_).move_as_buffer_slice().as_slice()));
|
|
|
|
return false;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (!ok) {
|
2020-07-22 20:52:00 +02:00
|
|
|
set_need_size(input_->size() + 1);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto s_len = input_->cut_head(len_).move_as_buffer_slice();
|
|
|
|
input_->advance(2);
|
|
|
|
len_ = hex_to_integer<size_t>(s_len.as_slice());
|
|
|
|
if (len_ > MAX_CHUNK_SIZE) {
|
2020-07-22 20:52:00 +02:00
|
|
|
finish(Status::Error(PSLICE() << "Invalid chunk size " << tag("size", len_)));
|
|
|
|
return false;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
save_len_ = len_;
|
2020-01-19 01:02:56 +01:00
|
|
|
state_ = State::ReadChunkContent;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto size = input_->size();
|
2018-02-12 11:37:54 +01:00
|
|
|
auto ready = min(len_, size);
|
2020-07-22 20:52:00 +02:00
|
|
|
auto need_size = min(MIN_UPDATE_SIZE, len_ + 2);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (size < need_size) {
|
2020-07-22 20:52:00 +02:00
|
|
|
set_need_size(need_size);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
total_size_ += ready;
|
|
|
|
uncommited_size_ += ready;
|
|
|
|
if (total_size_ > MAX_SIZE) {
|
2020-07-22 20:52:00 +02:00
|
|
|
finish(Status::Error(PSLICE() << "Too big query " << tag("size", input_->size())));
|
|
|
|
return false;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
output_.append(input_->cut_head(ready));
|
2020-07-22 20:52:00 +02:00
|
|
|
result = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
len_ -= ready;
|
|
|
|
if (uncommited_size_ >= MIN_UPDATE_SIZE) {
|
|
|
|
uncommited_size_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len_ == 0) {
|
|
|
|
if (input_->size() < 2) {
|
|
|
|
need_size = 2;
|
|
|
|
break;
|
|
|
|
}
|
2018-10-26 16:11:20 +02:00
|
|
|
input_->advance(2);
|
2018-12-31 20:04:05 +01:00
|
|
|
total_size_ += 2;
|
|
|
|
if (save_len_ == 0) {
|
2020-07-22 20:52:00 +02:00
|
|
|
finish(Status::OK());
|
|
|
|
return false;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-01-19 01:02:56 +01:00
|
|
|
state_ = State::ReadChunkLength;
|
2018-12-31 20:04:05 +01:00
|
|
|
len_ = 0;
|
|
|
|
}
|
2020-08-13 15:57:28 +02:00
|
|
|
} while (false);
|
2020-07-22 20:52:00 +02:00
|
|
|
if (!is_input_active_ && !result) {
|
|
|
|
finish(Status::Error("Unexpected end of stream"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-07-22 20:52:00 +02:00
|
|
|
return result;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-02-12 11:37:54 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|