-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include "utilhttp.h" | ||
#include "util.h" | ||
|
||
#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 <cstdlib> | ||
#include <iostream> | ||
#include <sstream> | ||
#include <stdexcept> | ||
|
||
using tcp = boost::asio::ip::tcp; | ||
namespace http = boost::beast::http; | ||
|
||
std::stringstream http_get(const std::string &host, | ||
const std::string &service, /* aka port number */ | ||
const std::string &target) | ||
{ | ||
boost::asio::io_context ioc; | ||
tcp::resolver resolver{ioc}; | ||
tcp::socket socket{ioc}; | ||
|
||
// resolve host | ||
auto const results = resolver.resolve(host, service); | ||
|
||
// send request | ||
boost::asio::connect(socket, results.begin(), results.end()); | ||
int version = 11; // HTTP 1.1 | ||
http::request<http::string_body> req{http::verb::get, target, version}; | ||
req.set(http::field::host, host); | ||
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING); | ||
http::write(socket, req); | ||
|
||
// read response | ||
boost::beast::flat_buffer buffer; | ||
http::response<http::dynamic_body> res; | ||
http::read(socket, buffer, res); | ||
std::stringstream ss; | ||
ss << res; | ||
|
||
// close connection | ||
boost::system::error_code ec; | ||
socket.shutdown(tcp::socket::shutdown_both, ec); | ||
|
||
// not_connected is supposedly ok to ignore | ||
if (ec && ec != boost::system::errc::not_connected) | ||
throw std::runtime_error(ec.message()); | ||
|
||
return ss; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef UTILHTTP_H | ||
#define UTILHTTP_H | ||
|
||
#include <sstream> | ||
#include <string> | ||
|
||
std::stringstream http_get(const std::string &host, | ||
const std::string &service /* aka port */, | ||
const std::string &target /* ex: /index.html */); | ||
|
||
#endif |