2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/net/HttpChunkedByteFlow.h"
|
|
|
|
#include "td/net/HttpContentLengthByteFlow.h"
|
|
|
|
#include "td/net/HttpQuery.h"
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/ByteFlow.h"
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/GzipByteFlow.h"
|
|
|
|
#include "td/utils/port/FileFd.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class HttpReader {
|
|
|
|
public:
|
|
|
|
void init(ChainBufferReader *input, size_t max_post_size = std::numeric_limits<size_t>::max(),
|
|
|
|
size_t max_files = 100);
|
2020-07-23 18:47:12 +02:00
|
|
|
Result<size_t> read_next(HttpQuery *query, bool can_be_slow = true) TD_WARN_UNUSED_RESULT; // TODO move query to init
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
HttpReader() = default;
|
|
|
|
HttpReader(const HttpReader &other) = delete;
|
|
|
|
HttpReader &operator=(const HttpReader &other) = delete;
|
|
|
|
HttpReader(HttpReader &&other) = delete;
|
|
|
|
HttpReader &operator=(HttpReader &&other) = delete;
|
|
|
|
~HttpReader() {
|
|
|
|
if (!temp_file_.empty()) {
|
2021-12-06 19:59:24 +01:00
|
|
|
clean_temporary_file();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void delete_temp_file(CSlice file_name);
|
|
|
|
|
|
|
|
private:
|
2018-10-26 16:11:20 +02:00
|
|
|
size_t max_post_size_ = 0;
|
|
|
|
size_t max_files_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-01-19 01:02:56 +01:00
|
|
|
enum class State { ReadHeaders, ReadContent, ReadContentToFile, ReadArgs, ReadMultipartFormData };
|
|
|
|
State state_ = State::ReadHeaders;
|
2018-10-26 16:11:20 +02:00
|
|
|
size_t headers_read_length_ = 0;
|
|
|
|
size_t content_length_ = 0;
|
|
|
|
ChainBufferReader *input_ = nullptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
ByteFlowSource flow_source_;
|
|
|
|
HttpChunkedByteFlow chunked_flow_;
|
|
|
|
GzipByteFlow gzip_flow_;
|
|
|
|
HttpContentLengthByteFlow content_length_flow_;
|
|
|
|
ByteFlowSink flow_sink_;
|
2018-10-26 16:11:20 +02:00
|
|
|
ChainBufferReader *content_ = nullptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-10-26 16:11:20 +02:00
|
|
|
HttpQuery *query_ = nullptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
Slice transfer_encoding_;
|
|
|
|
Slice content_encoding_;
|
|
|
|
Slice content_type_;
|
|
|
|
string content_type_lowercased_;
|
2018-10-26 16:11:20 +02:00
|
|
|
size_t total_parameters_length_ = 0;
|
|
|
|
size_t total_headers_length_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
string boundary_;
|
2018-10-26 16:11:20 +02:00
|
|
|
size_t form_data_read_length_ = 0;
|
|
|
|
size_t form_data_skipped_length_ = 0;
|
2020-01-19 01:02:56 +01:00
|
|
|
enum class FormDataParseState : int32 {
|
2018-12-31 20:04:05 +01:00
|
|
|
SkipPrologue,
|
|
|
|
ReadPartHeaders,
|
|
|
|
ReadPartValue,
|
|
|
|
ReadFile,
|
|
|
|
CheckForLastBoundary,
|
|
|
|
SkipEpilogue
|
2020-01-19 01:02:56 +01:00
|
|
|
};
|
|
|
|
FormDataParseState form_data_parse_state_ = FormDataParseState::SkipPrologue;
|
2018-12-31 20:04:05 +01:00
|
|
|
MutableSlice field_name_;
|
|
|
|
string file_field_name_;
|
|
|
|
string field_content_type_;
|
|
|
|
string file_name_;
|
2018-10-14 18:08:15 +02:00
|
|
|
bool has_file_name_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
FileFd temp_file_;
|
|
|
|
string temp_file_name_;
|
2018-10-26 16:11:20 +02:00
|
|
|
int64 file_size_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Result<size_t> split_header() TD_WARN_UNUSED_RESULT;
|
|
|
|
void process_header(MutableSlice header_name, MutableSlice header_value);
|
2020-07-24 14:32:33 +02:00
|
|
|
Result<bool> parse_multipart_form_data(bool can_be_slow) TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
Status parse_url(MutableSlice url) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status parse_parameters(MutableSlice parameters) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status parse_json_parameters(MutableSlice parameters) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status parse_head(MutableSlice head) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
Status open_temp_file(CSlice desired_file_name) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status try_open_temp_file(Slice directory_name, CSlice desired_file_name) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status save_file_part(BufferSlice &&file_part) TD_WARN_UNUSED_RESULT;
|
|
|
|
void close_temp_file();
|
2018-08-27 14:52:47 +02:00
|
|
|
void clean_temporary_file();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-07-19 12:17:05 +02:00
|
|
|
static constexpr size_t MAX_CONTENT_SIZE = std::numeric_limits<int32>::max(); // Some reasonable limit
|
2020-07-20 19:04:59 +02:00
|
|
|
static constexpr size_t MAX_TOTAL_PARAMETERS_LENGTH = 1 << 20; // Some reasonable limit
|
2020-07-19 12:17:05 +02:00
|
|
|
static constexpr size_t MAX_TOTAL_HEADERS_LENGTH = 1 << 18; // Some reasonable limit
|
|
|
|
static constexpr size_t MAX_BOUNDARY_LENGTH = 70; // As defined by RFC1341
|
|
|
|
static constexpr int64 MAX_FILE_SIZE = 2000 << 20; // Telegram server file size limit
|
2018-12-31 20:04:05 +01:00
|
|
|
static constexpr const char TEMP_DIRECTORY_PREFIX[] = "tdlib-server-tmp";
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|