Skip to content

Commit 45197b5

Browse files
committed
refactor, add client and visit
1 parent 2951683 commit 45197b5

File tree

11 files changed

+419
-4
lines changed

11 files changed

+419
-4
lines changed

CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,14 @@ endif ()
131131
#-------------------------------------------------
132132
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
133133

134-
file(GLOB_RECURSE BOOST_HTTP_IO_HEADERS CONFIGURE_DEPENDS include/boost/*.hpp include/boost/*.natvis)
134+
file(GLOB_RECURSE BOOST_HTTP_IO_HEADERS CONFIGURE_DEPENDS include/boost/http_io/*.hpp include/boost/*.natvis)
135135
file(GLOB_RECURSE BOOST_HTTP_IO_SOURCES CONFIGURE_DEPENDS src/*.cpp src/*.hpp)
136136

137-
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/include/boost PREFIX "" FILES ${BOOST_HTTP_IO_HEADERS})
138-
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/src PREFIX "http_io" FILES ${BOOST_HTTP_IO_SOURCES})
137+
source_group("" FILES "include/boost/http_io.hpp" "build/Jamfile")
138+
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/include/boost/http_io PREFIX "include" FILES ${BOOST_HTTP_IO_HEADERS})
139+
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/src PREFIX "src" FILES ${BOOST_HTTP_IO_SOURCES})
139140

140-
add_library(boost_http_io ${BOOST_HTTP_IO_HEADERS} ${BOOST_HTTP_IO_SOURCES})
141+
add_library(boost_http_io include/boost/http_io.hpp build/Jamfile ${BOOST_HTTP_IO_HEADERS} ${BOOST_HTTP_IO_SOURCES})
141142
add_library(Boost::http_io ALIAS boost_http_io)
142143
target_compile_features(boost_http_io PUBLIC cxx_constexpr)
143144
target_include_directories(boost_http_io PUBLIC "${PROJECT_SOURCE_DIR}/include")

build/Jamfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,28 @@ constant c11-requires :
1919
]
2020
;
2121

22+
explicit
23+
[ searched-lib ws2_32 : : <target-os>windows ] # NT
24+
[ searched-lib mswsock : : <target-os>windows ] # NT
25+
;
26+
2227
project boost/http_io
2328
: requirements
2429
$(c11-requires)
2530
<link>shared:<define>BOOST_HTTP_IO_DYN_LINK=1
2631
<link>static:<define>BOOST_HTTP_IO_STATIC_LINK=1
2732
<target-os>windows:<define>_WIN32_WINNT=0x0601 # VFALCO?
33+
<target-os>windows,<toolset>gcc:<library>ws2_32
34+
<target-os>windows,<toolset>gcc:<library>mswsock
35+
<target-os>windows,<toolset>gcc-cygwin:<define>__USE_W32_SOCKETS
2836
<define>BOOST_HTTP_IO_SOURCE
2937
: usage-requirements
3038
<link>shared:<define>BOOST_HTTP_IO_DYN_LINK=1
3139
<link>static:<define>BOOST_HTTP_IO_STATIC_LINK=1
40+
<target-os>windows:<define>_WIN32_WINNT=0x0601 # VFALCO?
41+
<target-os>windows,<toolset>gcc:<library>ws2_32
42+
<target-os>windows,<toolset>gcc:<library>mswsock
43+
<target-os>windows,<toolset>gcc-cygwin:<define>__USE_W32_SOCKETS
3244
: source-location ../src
3345
;
3446

cmake/toolchains/msvc.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_compile_options(
99
/permissive- # strict C++
1010
/W4 # enable all warnings
1111
/MP # multi-processor compilation
12+
/bigobj
1213
)
1314
if("${CMAKE_GENERATOR_PLATFORM}" STREQUAL "Win32") # 32-bit
1415
add_compile_options(

example/client/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
#
99

1010
add_subdirectory(burl)
11+
add_subdirectory(visit)

example/client/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
#
99

1010
build-project burl ;
11+
build-project visit ;

example/client/visit/CMakeLists.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#
2+
# Copyright (c) 2024 Mohammad Nejati
3+
#
4+
# Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
#
7+
# Official repository: https://github.com/cppalliance/http_io
8+
#
9+
10+
file(GLOB_RECURSE PFILES CONFIGURE_DEPENDS *.cpp *.hpp
11+
CMakeLists.txt
12+
Jamfile)
13+
14+
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} PREFIX "" FILES ${PFILES})
15+
16+
add_executable(http_io_example_client_visit ${PFILES})
17+
18+
target_compile_definitions(http_io_example_client_visit
19+
PRIVATE BOOST_ASIO_NO_DEPRECATED)
20+
21+
set_property(TARGET http_io_example_client_visit
22+
PROPERTY FOLDER "examples")
23+
24+
find_package(OpenSSL REQUIRED)
25+
find_package(ZLIB)
26+
27+
target_link_libraries(http_io_example_client_visit
28+
boost_http_io
29+
boost_program_options
30+
boost_scope
31+
OpenSSL::SSL
32+
OpenSSL::Crypto)
33+
34+
if (WIN32)
35+
target_link_libraries(http_io_example_client_visit
36+
crypt32)
37+
endif()
38+
39+
if (ZLIB_FOUND)
40+
target_link_libraries(http_io_example_client_visit
41+
boost_http_proto_zlib)
42+
endif()

example/client/visit/Jamfile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#
2+
# Copyright (c) 2024 Mohammad Nejati
3+
# Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
4+
#
5+
# Distributed under the Boost Software License, Version 1.0. (See accompanying
6+
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7+
#
8+
# Official repository: https://github.com/cppalliance/http_io
9+
#
10+
11+
import ../../../../config/checks/config : requires ;
12+
13+
using openssl ;
14+
import ac ;
15+
16+
project
17+
: requirements
18+
$(c11-requires)
19+
<library>/boost/http_proto//boost_http_proto
20+
[ ac.check-library /boost/http_proto//boost_http_proto_zlib : <library>/boost/http_proto//boost_http_proto_zlib : ]
21+
<library>/boost/http_io//boost_http_io
22+
<library>/boost/program_options//boost_program_options
23+
<library>/boost/scope//scope
24+
<library>/openssl//ssl/<link>shared
25+
<library>/openssl//crypto/<link>shared
26+
<target-os>windows:<library>crypt32
27+
<include>.
28+
;
29+
30+
exe visit :
31+
[ glob *.cpp ]
32+
;

example/client/visit/main.cpp

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
//
2+
// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
// Official repository: https://github.com/cppalliance/http_io
8+
//
9+
10+
#include <cstdlib>
11+
#include <boost/asio/basic_stream_socket.hpp>
12+
#include <boost/asio/connect.hpp>
13+
#include <boost/asio/io_context.hpp>
14+
#include <boost/asio/ip/tcp.hpp>
15+
#include <boost/core/detail/string_view.hpp>
16+
#include <boost/http_proto/context.hpp>
17+
#include <boost/http_proto/request.hpp>
18+
#include <boost/http_proto/response_parser.hpp>
19+
#include <boost/url/url_view.hpp>
20+
21+
struct worker
22+
{
23+
using executor_type =
24+
boost::asio::io_context::executor_type;
25+
using resolver_type =
26+
boost::asio::ip::basic_resolver<
27+
boost::asio::ip::tcp, executor_type>;
28+
using socket_type = boost::asio::basic_stream_socket<
29+
boost::asio::ip::tcp, executor_type>;
30+
31+
socket_type sock;
32+
resolver_type resolver;
33+
boost::http_proto::response_parser pr;
34+
boost::urls::url_view url;
35+
36+
explicit
37+
worker(
38+
executor_type ex,
39+
boost::http_proto::context& ctx)
40+
: sock(ex)
41+
, resolver(ex)
42+
, pr(ctx)
43+
{
44+
sock.open(boost::asio::ip::tcp::v4());
45+
}
46+
47+
void
48+
do_next()
49+
{
50+
do_visit("http://www.boost.org");
51+
}
52+
53+
void
54+
do_visit(boost::urls::url_view url_)
55+
{
56+
url = url_;
57+
do_resolve();
58+
}
59+
60+
void
61+
do_resolve()
62+
{
63+
resolver.async_resolve(
64+
url.encoded_host(),
65+
url.scheme(),
66+
[&](
67+
boost::system::error_code ec,
68+
resolver_type::results_type results)
69+
{
70+
if(ec.failed())
71+
{
72+
// log (target, ec.message())
73+
auto s = ec.message();
74+
return do_next();
75+
}
76+
do_connect(results);
77+
});
78+
}
79+
80+
void
81+
do_connect(
82+
resolver_type::results_type results)
83+
{
84+
boost::asio::async_connect(
85+
sock,
86+
results.begin(),
87+
results.end(),
88+
[&](
89+
boost::system::error_code ec,
90+
resolver_type::results_type::const_iterator it)
91+
{
92+
if(ec.failed())
93+
{
94+
// log (target, ec.message())
95+
return do_next();
96+
}
97+
do_request();
98+
});
99+
}
100+
101+
void
102+
do_request()
103+
{
104+
boost::http_proto::request req;
105+
auto path = url.encoded_path();
106+
req.set_start_line(
107+
boost::http_proto::method::get,
108+
path.empty() ? "/" : path,
109+
boost::http_proto::version::http_1_1);
110+
111+
do_shutdown();
112+
}
113+
114+
void
115+
do_shutdown()
116+
{
117+
boost::system::error_code ec;
118+
sock.shutdown(socket_type::shutdown_both, ec);
119+
if(ec.failed())
120+
{
121+
// log(ec.message())
122+
return do_next();
123+
}
124+
do_next();
125+
}
126+
};
127+
128+
int
129+
main(int argc, char* argv[])
130+
{
131+
boost::http_proto::context ctx;
132+
boost::http_proto::parser::config_base cfg;
133+
boost::http_proto::install_parser_service(ctx, cfg);
134+
135+
boost::asio::io_context ioc;
136+
137+
worker w(ioc.get_executor(), ctx);
138+
139+
w.do_next();
140+
141+
ioc.run();
142+
143+
return EXIT_SUCCESS;
144+
}

include/boost/http_io/client.hpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
// Official repository: https://github.com/cppalliance/http_io
8+
//
9+
10+
#ifndef BOOST_HTTP_IO_CLIENT_HPP
11+
#define BOOST_HTTP_IO_CLIENT_HPP
12+
13+
#include <boost/http_io/detail/config.hpp>
14+
#include <type_traits>
15+
#include <utility>
16+
17+
namespace boost {
18+
namespace http_io {
19+
20+
template<
21+
class AsyncStream
22+
/*,class Derived*/ // VFALCO CRTP for things like shutdown()
23+
>
24+
class client
25+
{
26+
public:
27+
using stream_type = typename
28+
std::remove_reference<AsyncStream>::type;
29+
30+
using executor_type = decltype(
31+
std::declval<stream_type&>().get_executor());
32+
33+
template<
34+
class... Args,
35+
class = std::enable_if<
36+
std::is_constructible<
37+
AsyncStream, Args...>::value>
38+
>
39+
explicit
40+
client(
41+
Args&&... args) noexcept(
42+
std::is_nothrow_constructible<
43+
AsyncStream, Args...>::value)
44+
: stream_(std::forward<Args>(args)...)
45+
{
46+
}
47+
48+
stream_type const&
49+
stream() const noexcept
50+
{
51+
return stream_;
52+
}
53+
54+
stream_type&
55+
stream() noexcept
56+
{
57+
return stream_;
58+
}
59+
60+
executor_type
61+
get_executor() const
62+
{
63+
return stream_.get_executor();
64+
}
65+
66+
private:
67+
AsyncStream stream_;
68+
};
69+
70+
} // http_io
71+
} // boost
72+
73+
#endif

0 commit comments

Comments
 (0)