From 7af182481ec323788d8b6d2c248eb3d5103769b1 Mon Sep 17 00:00:00 2001 From: Thomas Schleicher Date: Tue, 22 Oct 2024 22:40:48 +0200 Subject: [PATCH] simple redirect test --- src/main.cpp | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2dc9dbb..6587548 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,39 @@ +#include +#include +#include +#include +#include #include -#include -using namespace std; +namespace beast = boost::beast; +namespace http = beast::http; +namespace net = boost::asio; +using tcp = net::ip::tcp; int main(int argc, char *argv[]) { - cout << "Boost version: " << BOOST_LIB_VERSION << endl; + try { + net::io_context ioc; + tcp::acceptor acceptor{ioc, {tcp::v4(), 8080}}; + + std::cout << "Server is running on port 8080\n"; + + while (true) { + tcp::socket socket(ioc); + acceptor.accept(socket); + beast::flat_buffer buffer; + + http::request req; + http::read(socket, buffer, req); + + http::response res{http::status::found, 11}; + + res.set(http::field::location, "https://www.google.com"); + res.prepare_payload(); + + http::write(socket, res); + socket.shutdown(tcp::socket::shutdown_send); + } + } catch (std::exception e) { + std::cerr << "Error: " << e.what() << std::endl; + } } \ No newline at end of file