-
Notifications
You must be signed in to change notification settings - Fork 125
/
avhttp.cpp
60 lines (47 loc) · 1.36 KB
/
avhttp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <string>
#include <memory>
#include <iostream>
#include <stdio.h>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/write.hpp>
#include <boost/boost/throw_exception.hpp>
using boost::asio::ip::tcp;
#define ENABLE_LOGGER
#define LOGGING_COMPRESS_LOGS
#include "logging.hpp"
template<class ... T> inline constexpr bool always_false = false;
#include "simple_http.hpp"
namespace avhttp {
http_response fetch(const std::string& url, const std::string& proxy, boost::system::error_code& ec)
{
http_response res;
{
boost::asio::io_context io_context;
boost::asio::spawn(io_context,
[&](boost::asio::yield_context yield) mutable
{
simple_http http{ io_context.get_executor() };
http_request req;
req.set(boost::beast::http::field::user_agent, AVHTTP_VERSION_STRING);
boost::system::error_code ec;
if (proxy.empty())
res = http.async_perform(url, req, yield[ec]);
else
res = http.async_perform(url, proxy, req, yield[ec]);
});
io_context.run();
}
return res;
}
http_response fetch(const std::string& url, const std::string& proxy)
{
boost::system::error_code ec;
auto res = fetch(url, proxy, ec);
if (ec)
boost::throw_exception(boost::system::system_error(ec));
return res;
}
}