FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
expresscpp/src/fetch.cpp at master · expresscpp/expresscpp · GitHub
expresscpp
/
expresscpp
Public
Notifications
You must be signed in to change notification settings
Fork
15
Star
96
Code
Issues
5
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
expresscpp
/
src
/
fetch.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
89 lines (69 loc) · 2.52 KB
Breadcrumbs
expresscpp
/
src
/
fetch.cpp
Copy path
File metadata and controls
89 lines (69 loc) · 2.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#
include
"
expresscpp/fetch.hpp
"
#
include
<
cstdlib
>
#
include
<
string
>
#
include
"
boost/asio/connect.hpp
"
#
include
"
boost/asio/ip/tcp.hpp
"
#
include
"
boost/beast/core.hpp
"
#
include
"
boost/beast/http.hpp
"
#
include
"
boost/beast/version.hpp
"
#
include
"
expresscpp/console.hpp
"
#
include
"
expresscpp/url.hpp
"
namespace
expresscpp
{
namespace
beast
=
boost::beast;
namespace
http
=
beast::http;
namespace
net
=
boost::asio;
using
tcp = net::ip::tcp;
std::string
fetch
(
const
std::string& url,
const
FetchOptions options) {
std::string string_response;
try
{
auto
parsed_url =
parseURI
(url);
auto
const
host = parsed_url.
domain
;
auto
const
port = parsed_url.
port
;
auto
required_path = parsed_url.
resource
;
if
(!parsed_url.
query
.
empty
()) {
required_path +=
"
?
"
+ parsed_url.
query
;
}
constexpr
int
version =
11
;
//
The io_context is required for all I/O
net::io_context ioc;
//
These objects perform our I/O
tcp::resolver
resolver
(ioc);
tcp::socket socket{ioc};
//
Look up the domain name
auto
const
results = resolver.
resolve
(host, port);
//
Make the connection on the IP address we get from a lookup
boost::asio::connect
(socket, results.
begin
(), results.
end
());
//
Set up an HTTP GET request message
http::request<http::string_body> req{
getBeastVerbFromExpressVerb
(options.
method
), required_path, version};
req.
set
(http::field::host, host);
req.
set
(http::field::user_agent,
BOOST_BEAST_VERSION_STRING
);
for
(
const
auto
& h : options.
headers
) {
req.
set
(h.
first
, h.
second
);
}
if
(!options.
body
.
empty
()) {
req.
body
() = options.
body
;
req.
prepare_payload
();
}
//
Send the HTTP request to the remote host
http::write
(socket, req);
//
This buffer is used for reading and must be persisted
beast::flat_buffer buffer;
//
Declare a container to hold the response
http::response<http::dynamic_body> res;
//
Receive the HTTP response
http::read
(socket, buffer, res);
string_response =
boost::beast::buffers_to_string
(res.
body
().
data
());
//
Gracefully close the socket
beast::error_code ec;
socket.
shutdown
(tcp::socket::shutdown_both, ec);
//
not_connected happens sometimes so don't bother reporting it.
if
(ec && ec != beast::errc::not_connected) {
throw
beast::system_error{ec};
}
//
If we get here then the connection is closed gracefully
}
catch
(std::exception
const
& e) {
Console::Error
(
fmt::format
(
"
Error: {}
"
, e.
what
()));
}
return
string_response;
}
}
//
namespace expresscpp
Back
|
FazBrowse Home
|
New Git URL