Skip to content

Commit

Permalink
zlib::service refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtum committed Sep 20, 2024
1 parent 582dabc commit a44dfe9
Show file tree
Hide file tree
Showing 9 changed files with 1,018 additions and 934 deletions.
1,270 changes: 635 additions & 635 deletions .github/workflows/ci.yml

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions build/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#

import ac ;
import path ;
import ../../config/checks/config : requires ;

using zlib ;
Expand Down Expand Up @@ -38,7 +37,7 @@ project boost/http_proto
: source-location $(HTTP_PROTO_ROOT)
;

alias http_proto_sources : [ path.glob-tree $(HTTP_PROTO_ROOT)/src : *.cpp ] ;
alias http_proto_sources : [ glob-tree-ex ./src : *.cpp ] ;

explicit http_proto_sources ;

Expand All @@ -52,7 +51,7 @@ lib boost_http_proto
<library>/boost//url
;

alias http_proto_zlib_sources : [ path.glob-tree $(HTTP_PROTO_ROOT)/src_zlib : *.cpp ] ;
alias http_proto_zlib_sources : [ glob-tree-ex ./src_zlib : *.cpp ] ;

explicit http_proto_zlib_sources ;

Expand Down
72 changes: 72 additions & 0 deletions include/boost/http_proto/service/impl/zlib_service.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// Copyright (c) 2021 Vinnie Falco ([email protected])
// Copyright (c) 2024 Mohammad Nejati
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/cppalliance/http_proto
//

#ifndef BOOST_HTTP_PROTO_SERVICE_IMPL_ZLIB_SERVICE_HPP
#define BOOST_HTTP_PROTO_SERVICE_IMPL_ZLIB_SERVICE_HPP

#include <boost/system/is_error_code_enum.hpp>

namespace boost {

namespace system {
template<>
struct is_error_code_enum<
::boost::http_proto::zlib::error>
{
static bool const value = true;
};
} // system

namespace http_proto {
namespace zlib {

namespace detail {

struct BOOST_HTTP_PROTO_DECL
error_cat_type
: system::error_category
{
BOOST_HTTP_PROTO_DECL const char* name(
) const noexcept override;
BOOST_HTTP_PROTO_DECL bool failed(
int) const noexcept override;
BOOST_HTTP_PROTO_DECL std::string message(
int) const override;
BOOST_HTTP_PROTO_DECL char const* message(
int, char*, std::size_t
) const noexcept override;
BOOST_SYSTEM_CONSTEXPR error_cat_type()
: error_category(0xe6c6d0215d1d6e22)
{
}
};

BOOST_HTTP_PROTO_DECL extern
error_cat_type error_cat;

} // detail

inline
BOOST_SYSTEM_CONSTEXPR
system::error_code
make_error_code(
error ev) noexcept
{
return system::error_code{
static_cast<std::underlying_type<
error>::type>(ev),
detail::error_cat};
}

} // zip
} // http_proto
} // boost

#endif
148 changes: 113 additions & 35 deletions include/boost/http_proto/service/zlib_service.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//
// Copyright (c) 2021 Vinnie Falco ([email protected])
// Copyright (c) 2024 Christian Mazakas
// Copyright (c) 2024 Mohammad Nejati
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Expand All @@ -16,9 +17,6 @@
#include <boost/http_proto/detail/workspace.hpp>
#include <boost/http_proto/service/service.hpp>

#include <boost/buffers/const_buffer.hpp>
#include <boost/buffers/mutable_buffer.hpp>

namespace boost {
namespace http_proto {
namespace zlib {
Expand All @@ -29,8 +27,6 @@ struct decoder_config
unsigned mem_level = 8;
};

//------------------------------------------------

constexpr
inline
std::size_t
Expand All @@ -54,60 +50,142 @@ encoding_size_hint(decoder_config cfg = {}) noexcept
(6 * 1024);
}

struct BOOST_HTTP_PROTO_ZLIB_DECL
/** Error codes returned from compression/decompression functions.
Negative values are errors, positive values are used
for special but normal events.
*/
enum class error
{
ok = 0,
stream_end = 1,
need_dict = 2,
errno_ = -1,
stream_err = -2,
data_err = -3,
mem_err = -4,
buf_err = -5,
version_err = -6
};

/// Flush methods.
enum class flush
{
none,
partial,
sync,
full,
finish,
block,
trees
};

/** Input and output buffers.
The application must update `next_in` and `avail_in` when `avail_in`
has dropped to zero. It must update `next_out` and `avail_out` when
`avail_out` has dropped to zero.
*/
struct params
{
/// Next input byte
void const* next_in;

/// Number of bytes available at `next_in`
std::size_t avail_in;

/// Next output byte
void* next_out;

/// Number of bytes remaining free at `next_out`
std::size_t avail_out;
};

/// Abstract interface for deflator/inflator streams.
struct stream
{
/** Call the underling compression/decompression algorithm.
@param p The input and output buffers.
@param f The flush method.
@return The result of operation that contains a value
of @ref error.
*/
virtual system::error_code
write(params& p, flush f) noexcept = 0;
};

/** Provides in-memory compression and decompression functions
using zlib underneath.
*/
struct BOOST_HTTP_PROTO_DECL
service
: http_proto::service
{
struct stream
{
enum class flush
{
none,
partial,
sync,
full,
finish,
block,
trees
};

struct results
{
std::size_t out_bytes;
std::size_t in_bytes;
system::error_code ec;
bool finished;
};

virtual results
write(
buffers::mutable_buffer out,
buffers::const_buffer in,
flush) noexcept = 0;
};

virtual
std::size_t
space_needed() const noexcept = 0;

/** Create a deflator stream by calling zlib `deflateInit2()`.
@param ws A reference to the workspace used for constructing the
deflator stream object and for storage used by zlib.
@param level The compression level.
@param window_bits The window size.
@param mem_level Specifies how much memory should be allocated
for the internal compression state.
@return A reference to the created deflator stream.
@throws std::length_error If there is insufficient free space in
@ref `http_proto::detail::workspace`.
*/
virtual stream&
make_deflator(
http_proto::detail::workspace& ws,
int level,
int window_bits,
int mem_level) const = 0;

/** Create an inflator stream by calling zlib `inflateInit2()`.
@param ws A reference to the workspace used for constructing the
inflator stream object and for storage used by zlib.
@param window_bits The window size.
@return A reference to the created inflator stream.
@throws std::length_error If there is insufficient free space in
@ref `http_proto::detail::workspace`.
*/
virtual stream&
make_inflator(
http_proto::detail::workspace& ws,
int window_bits) const = 0;
};

void BOOST_HTTP_PROTO_ZLIB_DECL
/** Installs a zlib service on the provided context.
@param ctx A reference to the @ref context where the service
will be installed.
@throw std::invalid_argument If the zlib service already
exist on the context.
*/
BOOST_HTTP_PROTO_ZLIB_DECL
void
install_service(context& ctx);

} // zlib
} // http_proto
} // boost

#include <boost/http_proto/service/impl/zlib_service.hpp>

#endif
26 changes: 0 additions & 26 deletions include/boost/http_proto/src_brotli.hpp

This file was deleted.

26 changes: 0 additions & 26 deletions include/boost/http_proto/src_zlib.hpp

This file was deleted.

Loading

0 comments on commit a44dfe9

Please sign in to comment.