Skip to content

Commit

Permalink
Untested code that compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
jgaa committed Jan 18, 2025
1 parent 5470ee3 commit e0570c4
Show file tree
Hide file tree
Showing 30 changed files with 264 additions and 133 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ set(ACTUAL_SOURCES
src/RequestBodyStringImpl.cpp
src/RequestBodyFileImpl.cpp
src/url_encode.cpp
src/boost_compitability.cpp
${LOGGING_SRC}
)

Expand Down
3 changes: 3 additions & 0 deletions examples/logip/logip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include <boost/log/expressions.hpp>
#endif

#include <boost/exception/all.hpp>
#include <boost/exception/diagnostic_information.hpp>

#include "restc-cpp/restc-cpp.h"
#include "restc-cpp/RequestBuilder.h"
#include "restc-cpp/SerializeJson.h"
Expand Down
2 changes: 1 addition & 1 deletion include/restc-cpp/DataReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class DataReader {
virtual ~DataReader() = default;

virtual bool IsEof() const = 0;
virtual boost::asio::const_buffers_1 ReadSome() = 0;
virtual boost_const_buffer ReadSome() = 0;
virtual void Finish() = 0; // Make sure there are no pending data for the current request

static ptr_t CreateIoReader(const Connection::ptr_t& conn,
Expand Down
4 changes: 2 additions & 2 deletions include/restc-cpp/DataReaderStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class DataReaderStream : public DataReader {
}

/*! Read whatever we have buffered or can get downstream */
boost::asio::const_buffers_1 ReadSome() override;
boost_const_buffer ReadSome() override;

/*! Read up to maxBytes from whatever we have buffered or can get downstream.*/
boost::asio::const_buffers_1 GetData(size_t maxBytes);
boost_const_buffer GetData(size_t maxBytes);

/*! Get one char
*
Expand Down
4 changes: 2 additions & 2 deletions include/restc-cpp/DataWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class DataWriter {
virtual ~DataWriter() = default;

/*! Write some data */
virtual void Write(boost::asio::const_buffers_1 buffers) = 0;
virtual void Write(boost_const_buffer buffers) = 0;

/*! Write without altering the data (headers) */
virtual void WriteDirect(boost::asio::const_buffers_1 buffers) = 0;
virtual void WriteDirect(boost_const_buffer buffers) = 0;

/*! Write some data */
virtual void Write(const write_buffers_t& buffers) = 0;
Expand Down
3 changes: 2 additions & 1 deletion include/restc-cpp/RapidJsonReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <assert.h>

#include "restc-cpp/boost_compatibility.h"
#include "rapidjson/reader.h"
#include "restc-cpp/restc-cpp.h"

Expand Down Expand Up @@ -96,7 +97,7 @@ class RapidJsonReader {
const auto len = boost::asio::buffer_size(buffer);

if (len) {
ch_ = boost::asio::buffer_cast<const char*>(buffer);
ch_ = boost_buffer_cast(buffer);
end_ = ch_ + len;
} else {
ch_ = end_ = nullptr;
Expand Down
9 changes: 5 additions & 4 deletions include/restc-cpp/Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <boost/system/error_code.hpp>

#include "restc-cpp/boost_compatibility.h"
#include "restc-cpp/typename.h"
#include "restc-cpp/logging.h"

Expand All @@ -34,21 +35,21 @@ class Socket

virtual const boost::asio::ip::tcp::socket& GetSocket() const = 0;

virtual std::size_t AsyncReadSome(boost::asio::mutable_buffers_1 buffers,
virtual std::size_t AsyncReadSome(boost_mutable_buffer buffers,
boost::asio::yield_context& yield) = 0;

virtual std::size_t AsyncRead(boost::asio::mutable_buffers_1 buffers,
virtual std::size_t AsyncRead(boost_mutable_buffer buffers,
boost::asio::yield_context& yield) = 0;

virtual void AsyncWrite(const boost::asio::const_buffers_1& buffers,
virtual void AsyncWrite(const boost_const_buffer& buffers,
boost::asio::yield_context& yield) = 0;

virtual void AsyncWrite(const write_buffers_t& buffers,
boost::asio::yield_context& yield) = 0;

template <typename T>
void AsyncWriteT(const T& buffer, boost::asio::yield_context& yield) {
boost::asio::const_buffers_1 b{buffer.data(), buffer.size()};
boost_const_buffer b{buffer.data(), buffer.size()};
AsyncWrite(b, yield);
}

Expand Down
114 changes: 114 additions & 0 deletions include/restc-cpp/boost_compatibility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#pragma once

#include <boost/asio.hpp>
#include <boost/version.hpp>
#include <boost/asio/ip/address_v4.hpp>

/* Boost keeps introducing breaking changes in the asio library. It seems like
* their window of backwards compatibility is about 5 years.
*
* This is a nightmare for library developers, if we want to maintain support
* for older versions of boost. I don't want the users of restc-cpp to be forced to
* refactor their code just because the boost library has been updated. Refactoring
* for this reason alone is just a waste of time and a huge cost for no gain.
*
* Here I try to handle the differences between the different versions of boost::asio
* in order to make it easier to maintain support for older versions of boost.
*
* So if restc-spp is the only library that you are using that requires broken parts
* of boost, then you should be fine.
*
* I take full credits for whatever works well here. All blame goes to ChatGPT! ;)
*/

#if BOOST_VERSION >= 107000
#include <boost/coroutine/exceptions.hpp>
#define RESTC_CPP_IN_COROUTINE_CATCH_ALL \
catch (boost::coroutines::detail::forced_unwind const&) { \
throw; /* required for Boost Coroutine! */ \
} catch (...)
#elif BOOST_VERSION >= 106000
#include <boost/coroutine/detail/forced_unwind.hpp>
#define RESTC_CPP_IN_COROUTINE_CATCH_ALL \
catch (boost::coroutines::detail::forced_unwind const&) { \
throw; /* required for Boost Coroutine! */ \
} catch (...)
#else
#define RESTC_CPP_IN_COROUTINE_CATCH_ALL \
catch (...)
#endif

#if BOOST_VERSION >= 108100
// They changed the function signature. In boost 1.86 it broke the build.
#define RESTC_CPP_SPAWN_TRAILER \
, boost::asio::detached
#else
#define RESTC_CPP_SPAWN_TRAILER
#endif


namespace restc_cpp {

#if BOOST_VERSION >= 107000
using boost_const_buffer = boost::asio::const_buffer;
using boost_mutable_buffer = boost::asio::mutable_buffer;
#else
using boost_const_buffer = boost::asio::const_buffers_1;
sing boost_mutable_buffer = boost::asio::mutable_buffers_1;
#endif


#if BOOST_VERSION >= 106600
using boost_io_service = boost::asio::io_context;
using boost_work = boost::asio::executor_work_guard<boost::asio::io_context::executor_type>;

#else
using boost_io_service = boost::asio::io_service;
using boost_work = boost::asio::io_service::work;
#endif

template <typename Buffer>
const char* boost_buffer_cast(const Buffer& buffer) {
#if BOOST_VERSION >= 107000
return static_cast<const char*>(buffer.data());
#else
return boost::asio::buffer_cast<const char*>(buffer);
#endif
}

template <typename Buffer>
std::size_t boost_buffer_size(const Buffer& buffer) {
#if BOOST_VERSION >= 107000
return buffer.size();
#else
return boost::asio::buffer_size(buffer);
#endif
}

template <typename IOService, typename Handler>
void boost_dispatch(IOService& io_service, Handler&& handler) {
#if BOOST_VERSION >= 106600
// Determine if IOService is a pointer
if constexpr (std::is_pointer_v<IOService>) {
io_service->get_executor().dispatch(
std::forward<Handler>(handler),
std::allocator<void>() // Default allocator
);
} else {
io_service.get_executor().dispatch(
std::forward<Handler>(handler),
std::allocator<void>() // Default allocator
);
}
#else
io_service.dispatch(std::forward<Handler>(handler));
#endif
}

boost::asio::ip::tcp::endpoint boost_create_endpoint(const std::string& ip_address, unsigned short port);
uint32_t boost_convert_ipv4_to_uint(const std::string& ip_address);
std::unique_ptr<boost_work> boost_make_work(boost_io_service& ioservice);

} // ns


10 changes: 9 additions & 1 deletion include/restc-cpp/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,17 @@ class ToBuffer
{
}

operator boost::asio::const_buffers_1 () const {
#if BOOST_VERSION >= 107000
// For Boost 1.70 and newer
operator boost::asio::const_buffer() const {
return {buf_.data(), buf_.size()};
}
#else
// For Boost versions older than 1.70
operator boost::asio::const_buffers_1() const {
return {buf_.c_str(), buf_.size()};
}
#endif

operator const boost::string_ref() const {
return buf_;
Expand Down
46 changes: 17 additions & 29 deletions include/restc-cpp/restc-cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>

#include "restc-cpp/boost_compatibility.h"
#include "restc-cpp/helper.h"
#include "restc-cpp/Connection.h"

Expand Down Expand Up @@ -52,19 +53,6 @@
# define RESTC_CPP_IO_BUFFER_SIZE (1024 * 16)
#endif

#define RESTC_CPP_IN_COROUTINE_CATCH_ALL \
catch (boost::coroutines::detail::forced_unwind const&) { \
throw; /* required for Boost Coroutine! */ \
} catch (...)

#if BOOST_VERSION >= 108100
// They changed the function signature. In boost 1.86 it broke the build.
#define RESTC_CPP_SPAWN_TRAILER \
, boost::asio::detached
#else
#define RESTC_CPP_SPAWN_TRAILER
#endif

namespace restc_cpp {

class RestClient;
Expand Down Expand Up @@ -285,7 +273,7 @@ class Reply {
* be fetched from the server. The data is safe to use until
* the method is called again.
*/
virtual boost::asio::const_buffers_1 GetSomeData() = 0;
virtual boost_const_buffer GetSomeData() = 0;

/*! Returns true as long as you have not yet pulled all
* the data from the response.
Expand Down Expand Up @@ -350,15 +338,15 @@ class Context {
const auto microseconds =
std::chrono::duration_cast<std::chrono::microseconds>(
duration).count();
boost::posix_time::microseconds ms(microseconds);
::boost::posix_time::microseconds ms(microseconds);
Sleep(ms);
}

/*! Asynchronously sleep for a period */
virtual void Sleep(const boost::posix_time::microseconds& ms) = 0;
virtual void Sleep(const ::boost::posix_time::microseconds& ms) = 0;

static std::unique_ptr<Context>
Create(boost::asio::yield_context& yield,
Create(::boost::asio::yield_context& yield,
RestClient& rc);
};

Expand Down Expand Up @@ -442,7 +430,7 @@ class RestClient {
}

virtual std::shared_ptr<ConnectionPool> GetConnectionPool() = 0;
virtual boost::asio::io_service& GetIoService() = 0;
virtual boost_io_service& GetIoService() = 0;

#ifdef RESTC_CPP_WITH_TLS
virtual std::shared_ptr<boost::asio::ssl::context> GetTLSContext() = 0;
Expand Down Expand Up @@ -476,30 +464,30 @@ class RestClient {

#ifdef RESTC_CPP_WITH_TLS
static std::unique_ptr<RestClient> Create(
std::shared_ptr<boost::asio::ssl::context> ctx);
std::shared_ptr<::boost::asio::ssl::context> ctx);
static std::unique_ptr<RestClient> Create(
std::shared_ptr<boost::asio::ssl::context> ctx,
const boost::optional<Request::Properties>& properties);
std::shared_ptr<::boost::asio::ssl::context> ctx,
const ::boost::optional<Request::Properties>& properties);
static std::unique_ptr<RestClient> Create(
std::shared_ptr<boost::asio::ssl::context> ctx,
const boost::optional<Request::Properties>& properties,
boost::asio::io_service& ioservice);
std::shared_ptr<::boost::asio::ssl::context> ctx,
const ::boost::optional<Request::Properties>& properties,
boost_io_service& ioservice);
#endif

static std::unique_ptr<RestClient>
Create(const boost::optional<Request::Properties>& properties);
Create(const ::boost::optional<Request::Properties>& properties);

static std::unique_ptr<RestClient> CreateUseOwnThread();

static std::unique_ptr<RestClient>
CreateUseOwnThread(const boost::optional<Request::Properties>& properties);
CreateUseOwnThread(const ::boost::optional<Request::Properties>& properties);

static std::unique_ptr<RestClient>
Create(const boost::optional<Request::Properties>& properties,
boost::asio::io_service& ioservice);
Create(const ::boost::optional<Request::Properties>& properties,
boost_io_service& ioservice);

static std::unique_ptr<RestClient>
Create(boost::asio::io_service& ioservice);
Create(boost_io_service& ioservice);


protected:
Expand Down
8 changes: 4 additions & 4 deletions src/ChunkedReaderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ class ChunkedReaderImpl : public DataReader {
return out.str();
}

static void Log(const boost::asio::const_buffers_1 buffers, const char * /*tag*/)
static void Log(const ::restc_cpp::boost_const_buffer buffers, const char * /*tag*/)
{
const auto buf_len = boost::asio::buffer_size(*buffers.begin());
const auto buf_len = boost_buffer_size(buffers);

// At the time of the implementation, there are never multiple buffers.
RESTC_CPP_LOG_TRACE_(tag << ' ' << "# " << buf_len
Expand All @@ -67,7 +67,7 @@ class ChunkedReaderImpl : public DataReader {
buf_len}));
}

boost::asio::const_buffers_1 ReadSome() override {
::restc_cpp::boost_const_buffer ReadSome() override {

EatPadding();

Expand Down Expand Up @@ -113,7 +113,7 @@ class ChunkedReaderImpl : public DataReader {
}
}

boost::asio::const_buffers_1 GetData() {
::restc_cpp::boost_const_buffer GetData() {

auto rval = stream_->GetData(chunk_len_);
const auto seg_len = boost::asio::buffer_size(rval);
Expand Down
4 changes: 2 additions & 2 deletions src/ChunkedWriterImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ class ChunkedWriterImpl : public DataWriter {
{
}

void WriteDirect(boost::asio::const_buffers_1 buffers) override {
void WriteDirect(::restc_cpp::boost_const_buffer buffers) override {
next_->WriteDirect(buffers);
}

void Write(boost::asio::const_buffers_1 buffers) override {
void Write(::restc_cpp::boost_const_buffer buffers) override {
const auto len = boost::asio::buffer_size(buffers);
buffers_.resize(2);
buffers_[1] = buffers;
Expand Down
Loading

0 comments on commit e0570c4

Please sign in to comment.