From 9d441470c8c640553088a37cce81ee3c8f9091f9 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 19 Nov 2019 15:11:19 +0300 Subject: [PATCH] Support POST requests in Wget. GitOrigin-RevId: d3f50200c22dcc6c5142581d74de3a41fca00305 --- tdnet/td/net/Wget.cpp | 19 +++++++++++++++---- tdnet/td/net/Wget.h | 5 ++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/tdnet/td/net/Wget.cpp b/tdnet/td/net/Wget.cpp index 2965c3b4..bd76d04f 100644 --- a/tdnet/td/net/Wget.cpp +++ b/tdnet/td/net/Wget.cpp @@ -23,14 +23,17 @@ namespace td { Wget::Wget(Promise> promise, string url, std::vector> headers, - int32 timeout_in, int32 ttl, bool prefer_ipv6, SslStream::VerifyPeer verify_peer) + int32 timeout_in, int32 ttl, bool prefer_ipv6, SslStream::VerifyPeer verify_peer, string content, + string content_type) : promise_(std::move(promise)) , input_url_(std::move(url)) , headers_(std::move(headers)) , timeout_in_(timeout_in) , ttl_(ttl) , prefer_ipv6_(prefer_ipv6) - , verify_peer_(verify_peer) { + , verify_peer_(verify_peer) + , content_(std::move(content)) + , content_type_(std::move(content_type)) { } Status Wget::try_init() { @@ -39,7 +42,15 @@ Status Wget::try_init() { url.host_ = std::move(ascii_host); HttpHeaderCreator hc; - hc.init_get(url.query_); + if (content_.empty()) { + hc.init_get(url.query_); + } else { + hc.init_post(url.query_); + hc.set_content_size(content_.size()); + if (!content_type_.empty()) { + hc.set_content_type(content_type_); + } + } bool was_host = false; bool was_accept_encoding = false; for (auto &header : headers_) { @@ -58,7 +69,7 @@ Status Wget::try_init() { if (!was_accept_encoding) { hc.add_header("Accept-Encoding", "gzip, deflate"); } - TRY_RESULT(header, hc.finish()); + TRY_RESULT(header, hc.finish(content_)); IPAddress addr; TRY_STATUS(addr.init_host_port(url.host_, url.port_, prefer_ipv6_)); diff --git a/tdnet/td/net/Wget.h b/tdnet/td/net/Wget.h index edf46779..aa6880c3 100644 --- a/tdnet/td/net/Wget.h +++ b/tdnet/td/net/Wget.h @@ -24,7 +24,8 @@ class Wget : public HttpOutboundConnection::Callback { public: explicit Wget(Promise> promise, string url, std::vector> headers = {}, int32 timeout_in = 10, int32 ttl = 3, bool prefer_ipv6 = false, - SslStream::VerifyPeer verify_peer = SslStream::VerifyPeer::On); + SslStream::VerifyPeer verify_peer = SslStream::VerifyPeer::On, string content = {}, + string content_type = {}); private: Status try_init(); @@ -46,6 +47,8 @@ class Wget : public HttpOutboundConnection::Callback { int32 ttl_; bool prefer_ipv6_ = false; SslStream::VerifyPeer verify_peer_; + string content_; + string content_type_; }; } // namespace td