123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682 |
- /*
- * Copyright (c) 2017-2023 zhllxt
- *
- * author : zhllxt
- * email : 37792738@qq.com
- *
- * 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)
- */
- #if defined(ASIO2_ENABLE_SSL) || defined(ASIO2_USE_SSL)
- #ifndef __ASIO2_HTTPS_EXECUTE_HPP__
- #define __ASIO2_HTTPS_EXECUTE_HPP__
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- #pragma once
- #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
- #include <asio2/base/detail/push_options.hpp>
- #include <asio2/base/detail/function_traits.hpp>
- #include <asio2/util/string.hpp>
- #include <asio2/http/detail/http_util.hpp>
- #include <asio2/http/detail/http_make.hpp>
- #include <asio2/http/detail/http_traits.hpp>
- #include <asio2/component/socks/socks5_client_cp.hpp>
- namespace asio2::detail
- {
- template<class derived_t, class args_t, bool Enable = is_https_execute_download_enabled<args_t>::value()>
- struct https_execute_impl_bridge;
- template<class derived_t, class args_t>
- struct https_execute_impl_bridge<derived_t, args_t, false>
- {
- };
- template<class derived_t, class args_t>
- struct https_execute_impl_bridge<derived_t, args_t, true>
- {
- protected:
- template<typename String, typename StrOrInt, class Proxy, class Body, class Fields, class Buffer>
- static void _execute_with_socks5(
- asio::io_context& ioc, asio::ip::tcp::resolver& resolver, asio::ip::tcp::socket& socket,
- asio::ssl::stream<asio::ip::tcp::socket&>& stream,
- http::parser<false, Body, typename Fields::allocator_type>& parser,
- Buffer& buffer,
- String&& host, StrOrInt&& port,
- http::request<Body, Fields>& req, Proxy&& proxy)
- {
- auto sk5 = detail::to_shared_ptr(std::forward<Proxy>(proxy));
- std::string_view h{ sk5->host() };
- std::string_view p{ sk5->port() };
-
- if (static_cast<int>(sk5->command()) == 0)
- {
- sk5->command(socks5::command::connect);
- }
- // Look up the domain name
- resolver.async_resolve(h, p, [&, s5 = std::move(sk5)]
- (const error_code& ec1, const asio::ip::tcp::resolver::results_type& endpoints) mutable
- {
- if (ec1) { set_last_error(ec1); return; }
- // Make the connection on the IP address we get from a lookup
- asio::async_connect(socket, endpoints,
- [&, s5 = std::move(s5)](const error_code& ec2, const asio::ip::tcp::endpoint&) mutable
- {
- if (ec2) { set_last_error(ec2); return; }
- socks5_async_handshake
- (
- detail::to_string(std::forward<String >(host)),
- detail::to_string(std::forward<StrOrInt>(port)),
- socket,
- std::move(s5),
- [&](error_code ecs5, std::string, std::string) mutable
- {
- if (ecs5) { set_last_error(ecs5); return; }
- // https://github.com/djarek/certify
- if (auto it = req.find(http::field::host); it != req.end())
- {
- std::string hostname(it->value());
- SSL_set_tlsext_host_name(stream.native_handle(), hostname.data());
- }
- stream.async_handshake(asio::ssl::stream_base::client,
- [&](const error_code& ec3) mutable
- {
- if (ec3) { set_last_error(ec3); return; }
- http::async_write(stream, req, [&](const error_code& ec4, std::size_t) mutable
- {
- // can't use stream.shutdown(),in some case the shutdowm will blocking forever.
- if (ec4) { set_last_error(ec4); stream.async_shutdown([](const error_code&) {}); return; }
- // Then start asynchronous reading
- http::async_read(stream, buffer, parser,
- [&](const error_code& ec5, std::size_t) mutable
- {
- // Reading completed, assign the read the result to last error
- // If the code does not execute into here, the last error
- // is the default value timed_out.
- set_last_error(ec5);
- stream.async_shutdown([](const error_code&) mutable {});
- });
- });
- });
- }
- );
- });
- });
- }
- template<typename String, typename StrOrInt, class Body, class Fields, class Buffer>
- static void _execute_trivially(
- asio::io_context& ioc, asio::ip::tcp::resolver& resolver, asio::ip::tcp::socket& socket,
- asio::ssl::stream<asio::ip::tcp::socket&>& stream,
- http::parser<false, Body, typename Fields::allocator_type>& parser,
- Buffer& buffer,
- String&& host, StrOrInt&& port,
- http::request<Body, Fields>& req)
- {
- detail::ignore_unused(ioc);
- // Look up the domain name
- resolver.async_resolve(std::forward<String>(host), detail::to_string(std::forward<StrOrInt>(port)),
- [&](const error_code& ec1, const asio::ip::tcp::resolver::results_type& endpoints) mutable
- {
- if (ec1) { set_last_error(ec1); return; }
- // Make the connection on the IP address we get from a lookup
- asio::async_connect(socket, endpoints,
- [&](const error_code& ec2, const asio::ip::tcp::endpoint&) mutable
- {
- if (ec2) { set_last_error(ec2); return; }
- // https://github.com/djarek/certify
- if (auto it = req.find(http::field::host); it != req.end())
- {
- std::string hostname(it->value());
- SSL_set_tlsext_host_name(stream.native_handle(), hostname.data());
- }
- stream.async_handshake(asio::ssl::stream_base::client,
- [&](const error_code& ec3) mutable
- {
- if (ec3) { set_last_error(ec3); return; }
- http::async_write(stream, req, [&](const error_code& ec4, std::size_t) mutable
- {
- // can't use stream.shutdown(),in some case the shutdowm will blocking forever.
- if (ec4) { set_last_error(ec4); stream.async_shutdown([](const error_code&) {}); return; }
- // Then start asynchronous reading
- http::async_read(stream, buffer, parser,
- [&](const error_code& ec5, std::size_t) mutable
- {
- // Reading completed, assign the read the result to last error
- // If the code does not execute into here, the last error
- // is the default value timed_out.
- set_last_error(ec5);
- stream.async_shutdown([](const error_code&) mutable {});
- });
- });
- });
- });
- });
- }
- template<typename String, typename StrOrInt, class Proxy, class Body, class Fields, class Buffer>
- static void _execute_impl(
- asio::io_context& ioc, asio::ip::tcp::resolver& resolver, asio::ip::tcp::socket& socket,
- asio::ssl::stream<asio::ip::tcp::socket&>& stream,
- http::parser<false, Body, typename Fields::allocator_type>& parser,
- Buffer& buffer,
- String&& host, StrOrInt&& port,
- http::request<Body, Fields>& req, Proxy&& proxy)
- {
- // if has socks5 proxy
- if constexpr (std::is_base_of_v<asio2::socks5::option_base,
- typename detail::element_type_adapter<detail::remove_cvref_t<Proxy>>::type>)
- {
- derived_t::_execute_with_socks5(ioc, resolver, socket, stream, parser, buffer
- , std::forward<String>(host), std::forward<StrOrInt>(port)
- , req
- , std::forward<Proxy>(proxy)
- );
- }
- else
- {
- detail::ignore_unused(proxy);
- derived_t::_execute_trivially(ioc, resolver, socket, stream, parser, buffer
- , std::forward<String>(host), std::forward<StrOrInt>(port)
- , req
- );
- }
- }
- public:
- template<typename String, typename StrOrInt, class Rep, class Period, class Proxy,
- class Body = http::string_body, class Fields = http::fields, class Buffer = beast::flat_buffer>
- typename std::enable_if_t<detail::is_character_string_v<detail::remove_cvref_t<String>>
- && detail::http_proxy_checker_v<Proxy>, http::response<Body, Fields>>
- static inline execute(const asio::ssl::context& ctx, String&& host, StrOrInt&& port,
- http::request<Body, Fields>& req, std::chrono::duration<Rep, Period> timeout, Proxy&& proxy)
- {
- http::parser<false, Body, typename Fields::allocator_type> parser;
- // First assign default value timed_out to last error
- set_last_error(asio::error::timed_out);
- // set default result to unknown
- parser.get().result(http::status::unknown);
- parser.eager(true);
- // The io_context is required for all I/O
- asio::io_context ioc;
- // These objects perform our I/O
- asio::ip::tcp::resolver resolver{ ioc };
- asio::ip::tcp::socket socket{ ioc };
- asio::ssl::stream<asio::ip::tcp::socket&> stream(socket, const_cast<asio::ssl::context&>(ctx));
- // This buffer is used for reading and must be persisted
- Buffer buffer;
- // Some sites must set the http::field::host
- if (req.find(http::field::host) == req.end())
- {
- std::string strhost = asio2::to_string(host);
- std::string strport = asio2::to_string(port);
- if (strport != "443")
- {
- strhost += ":";
- strhost += strport;
- }
- req.set(http::field::host, strhost);
- }
- // Some sites must set the http::field::user_agent
- if (req.find(http::field::user_agent) == req.end())
- {
- req.set(http::field::user_agent,
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36");
- }
- // do work
- derived_t::_execute_impl(ioc, resolver, socket, stream, parser, buffer
- , std::forward<String>(host), std::forward<StrOrInt>(port)
- , req
- , std::forward<Proxy>(proxy)
- );
- // timedout run
- ioc.run_for(timeout);
- error_code ec_ignore{};
- // Gracefully close the socket
- socket.shutdown(asio::ip::tcp::socket::shutdown_both, ec_ignore);
- socket.cancel(ec_ignore);
- socket.close(ec_ignore);
- return parser.release();
- }
- // ----------------------------------------------------------------------------------------
- template<typename String, typename StrOrInt, class Rep, class Period,
- class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::is_character_string_v<detail::remove_cvref_t<String>>
- , http::response<Body, Fields>>
- static inline execute(
- const asio::ssl::context& ctx, String&& host, StrOrInt&& port,
- http::request<Body, Fields>& req, std::chrono::duration<Rep, Period> timeout)
- {
- return derived_t::execute(ctx, std::forward<String>(host), std::forward<StrOrInt>(port),
- req, timeout, std::in_place);
- }
- template<typename String, typename StrOrInt,
- class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::is_character_string_v<detail::remove_cvref_t<String>>
- , http::response<Body, Fields>>
- static inline execute(
- const asio::ssl::context& ctx, String&& host, StrOrInt&& port,
- http::request<Body, Fields>& req)
- {
- return derived_t::execute(ctx, std::forward<String>(host), std::forward<StrOrInt>(port),
- req, std::chrono::milliseconds(http_execute_timeout), std::in_place);
- }
- // ----------------------------------------------------------------------------------------
- template<typename String, typename StrOrInt, class Rep, class Period,
- class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::is_character_string_v<detail::remove_cvref_t<String>>
- , http::response<Body, Fields>>
- static inline execute(String&& host, StrOrInt&& port,
- http::request<Body, Fields>& req, std::chrono::duration<Rep, Period> timeout)
- {
- return derived_t::execute(asio::ssl::context{ ASIO2_DEFAULT_SSL_METHOD },
- std::forward<String>(host), std::forward<StrOrInt>(port), req, timeout, std::in_place);
- }
- template<typename String, typename StrOrInt, class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::is_character_string_v<detail::remove_cvref_t<String>>
- , http::response<Body, Fields>>
- static inline execute(String&& host, StrOrInt&& port, http::request<Body, Fields>& req)
- {
- return derived_t::execute(
- std::forward<String>(host), std::forward<StrOrInt>(port),
- req, std::chrono::milliseconds(http_execute_timeout));
- }
- // ----------------------------------------------------------------------------------------
- template<class Rep, class Period, class Body = http::string_body, class Fields = http::fields>
- static inline http::response<Body, Fields> execute(
- const asio::ssl::context& ctx, http::web_request& req, std::chrono::duration<Rep, Period> timeout)
- {
- return derived_t::execute(ctx, req.url().host(), req.url().port(), req.base(), timeout, std::in_place);
- }
- template<class Body = http::string_body, class Fields = http::fields>
- static inline http::response<Body, Fields> execute(
- const asio::ssl::context& ctx, http::web_request& req)
- {
- return derived_t::execute(ctx, req, std::chrono::milliseconds(http_execute_timeout));
- }
- template<class Rep, class Period, class Body = http::string_body, class Fields = http::fields>
- static inline http::response<Body, Fields> execute(
- http::web_request& req, std::chrono::duration<Rep, Period> timeout)
- {
- return derived_t::execute(asio::ssl::context{ ASIO2_DEFAULT_SSL_METHOD }, req, timeout);
- }
- template<class Body = http::string_body, class Fields = http::fields>
- static inline http::response<Body, Fields> execute(http::web_request& req)
- {
- return derived_t::execute(req, std::chrono::milliseconds(http_execute_timeout));
- }
- // ----------------------------------------------------------------------------------------
- /**
- * @brief blocking execute the http request until it is returned on success or failure
- */
- template<class Rep, class Period, class Body = http::string_body, class Fields = http::fields>
- static inline http::response<Body, Fields> execute(const asio::ssl::context& ctx, std::string_view url,
- std::chrono::duration<Rep, Period> timeout)
- {
- http::web_request req = http::make_request(url);
- if (get_last_error())
- {
- return http::response<Body, Fields>{ http::status::unknown, 11};
- }
- return derived_t::execute(ctx, req.host(), req.port(), req.base(), timeout, std::in_place);
- }
- /**
- * @brief blocking execute the http request until it is returned on success or failure
- */
- template<class Body = http::string_body, class Fields = http::fields>
- static inline http::response<Body, Fields> execute(const asio::ssl::context& ctx, std::string_view url)
- {
- return derived_t::execute(ctx, url, std::chrono::milliseconds(http_execute_timeout));
- }
- // ----------------------------------------------------------------------------------------
- /**
- * @brief blocking execute the http request until it is returned on success or failure
- */
- template<class Rep, class Period, class Body = http::string_body, class Fields = http::fields>
- static inline http::response<Body, Fields> execute(std::string_view url,
- std::chrono::duration<Rep, Period> timeout)
- {
- return derived_t::execute(asio::ssl::context{ ASIO2_DEFAULT_SSL_METHOD }, url, timeout);
- }
- /**
- * @brief blocking execute the http request until it is returned on success or failure
- */
- template<class Body = http::string_body, class Fields = http::fields>
- static inline http::response<Body, Fields> execute(std::string_view url)
- {
- return derived_t::execute(url, std::chrono::milliseconds(http_execute_timeout));
- }
- // ----------------------------------------------------------------------------------------
- /**
- * @brief blocking execute the http request until it is returned on success or failure
- */
- template<typename String, typename StrOrInt, class Rep, class Period,
- class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::is_character_string_v<detail::remove_cvref_t<String>>
- , http::response<Body, Fields>>
- static inline execute(
- const asio::ssl::context& ctx, String&& host, StrOrInt&& port,
- std::string_view target, std::chrono::duration<Rep, Period> timeout)
- {
- http::web_request req = http::make_request(host, port, target);
- if (get_last_error())
- {
- return http::response<Body, Fields>{ http::status::unknown, 11};
- }
- return derived_t::execute(ctx,
- std::forward<String>(host), std::forward<StrOrInt>(port),
- req.base(), timeout, std::in_place);
- }
- /**
- * @brief blocking execute the http request until it is returned on success or failure
- */
- template<typename String, typename StrOrInt, class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::is_character_string_v<detail::remove_cvref_t<String>>
- , http::response<Body, Fields>>
- static inline execute(
- const asio::ssl::context& ctx, String&& host, StrOrInt&& port,
- std::string_view target)
- {
- return derived_t::execute(ctx,
- std::forward<String>(host), std::forward<StrOrInt>(port),
- target, std::chrono::milliseconds(http_execute_timeout));
- }
- // ----------------------------------------------------------------------------------------
- /**
- * @brief blocking execute the http request until it is returned on success or failure
- */
- template<typename String, typename StrOrInt, class Rep, class Period,
- class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::can_convert_to_string_v<detail::remove_cvref_t<String>>,
- http::response<Body, Fields>>
- static inline execute(String&& host, StrOrInt&& port, std::string_view target,
- std::chrono::duration<Rep, Period> timeout)
- {
- return derived_t::execute(asio::ssl::context{ ASIO2_DEFAULT_SSL_METHOD },
- std::forward<String>(host), std::forward<StrOrInt>(port),
- target, timeout);
- }
- /**
- * @brief blocking execute the http request until it is returned on success or failure
- */
- template<typename String, typename StrOrInt, class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::can_convert_to_string_v<detail::remove_cvref_t<String>>,
- http::response<Body, Fields>>
- static inline execute(String&& host, StrOrInt&& port, std::string_view target)
- {
- return derived_t::execute(
- std::forward<String>(host), std::forward<StrOrInt>(port),
- target, std::chrono::milliseconds(http_execute_timeout));
- }
- // ----------------------------------------------------------------------------------------
- template<typename String, typename StrOrInt, class Proxy,
- class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::is_character_string_v<detail::remove_cvref_t<String>>
- && detail::http_proxy_checker_v<Proxy>, http::response<Body, Fields>>
- static inline execute(
- const asio::ssl::context& ctx, String&& host, StrOrInt&& port,
- http::request<Body, Fields>& req, Proxy&& proxy)
- {
- return derived_t::execute(ctx, std::forward<String>(host), std::forward<StrOrInt>(port),
- req, std::chrono::milliseconds(http_execute_timeout), std::forward<Proxy>(proxy));
- }
- // ----------------------------------------------------------------------------------------
- template<typename String, typename StrOrInt, class Proxy, class Rep, class Period,
- class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::is_character_string_v<detail::remove_cvref_t<String>>
- && detail::http_proxy_checker_v<Proxy>, http::response<Body, Fields>>
- static inline execute(String&& host, StrOrInt&& port,
- http::request<Body, Fields>& req, std::chrono::duration<Rep, Period> timeout, Proxy&& proxy)
- {
- return derived_t::execute(asio::ssl::context{ ASIO2_DEFAULT_SSL_METHOD },
- std::forward<String>(host), std::forward<StrOrInt>(port),
- req, timeout, std::forward<Proxy>(proxy));
- }
- template<typename String, typename StrOrInt, class Proxy,
- class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::is_character_string_v<detail::remove_cvref_t<String>>
- && detail::http_proxy_checker_v<Proxy>, http::response<Body, Fields>>
- static inline execute(String&& host, StrOrInt&& port, http::request<Body, Fields>& req, Proxy&& proxy)
- {
- return derived_t::execute(
- std::forward<String>(host), std::forward<StrOrInt>(port),
- req, std::chrono::milliseconds(http_execute_timeout), std::forward<Proxy>(proxy));
- }
- // ----------------------------------------------------------------------------------------
- template<class Proxy, class Rep, class Period,
- class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::http_proxy_checker_v<Proxy>, http::response<Body, Fields>>
- static inline execute(
- const asio::ssl::context& ctx, http::web_request& req,
- std::chrono::duration<Rep, Period> timeout, Proxy&& proxy)
- {
- return derived_t::execute(ctx, req.url().host(), req.url().port(), req.base(), timeout,
- std::forward<Proxy>(proxy));
- }
- template<class Proxy, class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::http_proxy_checker_v<Proxy>, http::response<Body, Fields>>
- static inline execute(const asio::ssl::context& ctx, http::web_request& req, Proxy&& proxy)
- {
- return derived_t::execute(ctx, req, std::chrono::milliseconds(http_execute_timeout),
- std::forward<Proxy>(proxy));
- }
- template<class Proxy, class Rep, class Period,
- class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::http_proxy_checker_v<Proxy>, http::response<Body, Fields>>
- static inline execute(
- http::web_request& req, std::chrono::duration<Rep, Period> timeout, Proxy&& proxy)
- {
- return derived_t::execute(asio::ssl::context{ ASIO2_DEFAULT_SSL_METHOD }, req, timeout,
- std::forward<Proxy>(proxy));
- }
- template<class Proxy, class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::http_proxy_checker_v<Proxy>, http::response<Body, Fields>>
- static inline execute(http::web_request& req, Proxy&& proxy)
- {
- return derived_t::execute(req, std::chrono::milliseconds(http_execute_timeout),
- std::forward<Proxy>(proxy));
- }
- // ----------------------------------------------------------------------------------------
- /**
- * @brief blocking execute the http request until it is returned on success or failure
- */
- template<class Proxy, class Rep, class Period,
- class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::http_proxy_checker_v<Proxy>, http::response<Body, Fields>>
- static inline execute(
- const asio::ssl::context& ctx, std::string_view url,
- std::chrono::duration<Rep, Period> timeout, Proxy&& proxy)
- {
- http::web_request req = http::make_request(url);
- if (get_last_error())
- {
- return http::response<Body, Fields>{ http::status::unknown, 11};
- }
- return derived_t::execute(ctx, req.host(), req.port(), req.base(), timeout, std::forward<Proxy>(proxy));
- }
- /**
- * @brief blocking execute the http request until it is returned on success or failure
- */
- template<class Proxy, class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::http_proxy_checker_v<Proxy>, http::response<Body, Fields>>
- static inline execute(const asio::ssl::context& ctx, std::string_view url, Proxy&& proxy)
- {
- return derived_t::execute(ctx, url, std::chrono::milliseconds(http_execute_timeout),
- std::forward<Proxy>(proxy));
- }
- // ----------------------------------------------------------------------------------------
- /**
- * @brief blocking execute the http request until it is returned on success or failure
- */
- template<class Proxy, class Rep, class Period,
- class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::http_proxy_checker_v<Proxy>, http::response<Body, Fields>>
- static inline execute(std::string_view url, std::chrono::duration<Rep, Period> timeout, Proxy&& proxy)
- {
- return derived_t::execute(asio::ssl::context{ ASIO2_DEFAULT_SSL_METHOD }, url, timeout,
- std::forward<Proxy>(proxy));
- }
- /**
- * @brief blocking execute the http request until it is returned on success or failure
- */
- template<class Proxy, class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::http_proxy_checker_v<Proxy>, http::response<Body, Fields>>
- static inline execute(std::string_view url, Proxy&& proxy)
- {
- return derived_t::execute(url, std::chrono::milliseconds(http_execute_timeout),
- std::forward<Proxy>(proxy));
- }
- // ----------------------------------------------------------------------------------------
- /**
- * @brief blocking execute the http request until it is returned on success or failure
- */
- template<typename String, typename StrOrInt, class Proxy, class Rep, class Period,
- class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::is_character_string_v<detail::remove_cvref_t<String>>
- && detail::http_proxy_checker_v<Proxy>, http::response<Body, Fields>>
- static inline execute(
- const asio::ssl::context& ctx, String&& host, StrOrInt&& port,
- std::string_view target, std::chrono::duration<Rep, Period> timeout, Proxy&& proxy)
- {
- http::web_request req = http::make_request(host, port, target);
- if (get_last_error())
- {
- return http::response<Body, Fields>{ http::status::unknown, 11};
- }
- return derived_t::execute(ctx,
- std::forward<String>(host), std::forward<StrOrInt>(port),
- req.base(), timeout, std::forward<Proxy>(proxy));
- }
- /**
- * @brief blocking execute the http request until it is returned on success or failure
- */
- template<typename String, typename StrOrInt, class Proxy,
- class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::is_character_string_v<detail::remove_cvref_t<String>>
- && detail::http_proxy_checker_v<Proxy>, http::response<Body, Fields>>
- static inline execute(
- const asio::ssl::context& ctx, String&& host, StrOrInt&& port,
- std::string_view target, Proxy&& proxy)
- {
- return derived_t::execute(ctx,
- std::forward<String>(host), std::forward<StrOrInt>(port),
- target, std::chrono::milliseconds(http_execute_timeout), std::forward<Proxy>(proxy));
- }
- // ----------------------------------------------------------------------------------------
- /**
- * @brief blocking execute the http request until it is returned on success or failure
- */
- template<typename String, typename StrOrInt, class Proxy, class Rep, class Period,
- class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::is_character_string_v<detail::remove_cvref_t<String>>
- && detail::http_proxy_checker_v<Proxy>, http::response<Body, Fields>>
- static inline execute(String&& host, StrOrInt&& port, std::string_view target,
- std::chrono::duration<Rep, Period> timeout, Proxy&& proxy)
- {
- return derived_t::execute(asio::ssl::context{ ASIO2_DEFAULT_SSL_METHOD },
- std::forward<String>(host), std::forward<StrOrInt>(port),
- target, timeout, std::forward<Proxy>(proxy));
- }
- /**
- * @brief blocking execute the http request until it is returned on success or failure
- */
- template<typename String, typename StrOrInt, class Proxy,
- class Body = http::string_body, class Fields = http::fields>
- typename std::enable_if_t<detail::is_character_string_v<detail::remove_cvref_t<String>>
- && detail::http_proxy_checker_v<Proxy>, http::response<Body, Fields>>
- static inline execute(String&& host, StrOrInt&& port, std::string_view target, Proxy&& proxy)
- {
- return derived_t::execute(
- std::forward<String>(host), std::forward<StrOrInt>(port),
- target, std::chrono::milliseconds(http_execute_timeout), std::forward<Proxy>(proxy));
- }
- };
- template<class derived_t, class args_t = void>
- struct https_execute_impl : public https_execute_impl_bridge<derived_t, args_t> {};
- }
- #include <asio2/base/detail/pop_options.hpp>
- #endif // !__ASIO2_HTTPS_EXECUTE_HPP__
- #endif
|