From 421ec5fa8f96e88fbe0df555b8d5f48654b59277 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 24 Jul 2020 15:32:33 +0300 Subject: [PATCH] Migrate to other thread when receiving a file in multipart/form-data. GitOrigin-RevId: dbd6c1092f297c5b534508f070f49c311a756fe4 --- tdnet/td/net/HttpReader.cpp | 7 +++++-- tdnet/td/net/HttpReader.h | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tdnet/td/net/HttpReader.cpp b/tdnet/td/net/HttpReader.cpp index b22d54a70..54cf93357 100644 --- a/tdnet/td/net/HttpReader.cpp +++ b/tdnet/td/net/HttpReader.cpp @@ -247,7 +247,7 @@ Result HttpReader::read_next(HttpQuery *query, bool can_be_slow) { } case State::ReadMultipartFormData: { if (!content_->empty()) { - TRY_RESULT(result, parse_multipart_form_data()); + TRY_RESULT(result, parse_multipart_form_data(can_be_slow)); if (result) { break; } @@ -267,7 +267,7 @@ Result HttpReader::read_next(HttpQuery *query, bool can_be_slow) { // returns Status on wrong request // returns true if parsing has finished // returns false if need more data -Result HttpReader::parse_multipart_form_data() { +Result HttpReader::parse_multipart_form_data(bool can_be_slow) { while (true) { LOG(DEBUG) << "Parsing multipart form data in state " << static_cast(form_data_parse_state_) << " with already read length " << form_data_read_length_; @@ -452,6 +452,9 @@ Result HttpReader::parse_multipart_form_data() { } return false; case FormDataParseState::ReadFile: { + if (!can_be_slow) { + return Status::Error("SLOW"); + } if (find_boundary(content_->clone(), boundary_, form_data_read_length_)) { auto file_part = content_->cut_head(form_data_read_length_).move_as_buffer_slice(); content_->advance(boundary_.size()); diff --git a/tdnet/td/net/HttpReader.h b/tdnet/td/net/HttpReader.h index c3ebf2b2a..4e6e8ddb3 100644 --- a/tdnet/td/net/HttpReader.h +++ b/tdnet/td/net/HttpReader.h @@ -89,7 +89,7 @@ class HttpReader { Result split_header() TD_WARN_UNUSED_RESULT; void process_header(MutableSlice header_name, MutableSlice header_value); - Result parse_multipart_form_data() TD_WARN_UNUSED_RESULT; + Result parse_multipart_form_data(bool can_be_slow) TD_WARN_UNUSED_RESULT; 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;