-
Notifications
You must be signed in to change notification settings - Fork 10
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
6 changed files
with
139 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// | ||
// Copyright (c) 2021 Vinnie Falco ([email protected]) | ||
// Copyright (c) 2024 Christian Mazakas | ||
// | ||
// 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) | ||
|
@@ -11,9 +12,12 @@ | |
#define BOOST_HTTP_PROTO_MESSAGE_BASE_HPP | ||
|
||
#include <boost/http_proto/detail/config.hpp> | ||
#include <boost/http_proto/detail/header.hpp> | ||
#include <boost/http_proto/fields_base.hpp> | ||
#include <boost/http_proto/message_view_base.hpp> | ||
#include <boost/http_proto/detail/except.hpp> | ||
#include <boost/core/detail/string_view.hpp> | ||
#include <boost/core/span.hpp> | ||
|
||
namespace boost { | ||
namespace http_proto { | ||
|
@@ -93,9 +97,6 @@ class BOOST_SYMBOL_VISIBLE | |
BOOST_HTTP_PROTO_DECL | ||
void | ||
set_keep_alive(bool value); | ||
|
||
private: | ||
char* set_prefix_impl(std::size_t); | ||
}; | ||
|
||
} // http_proto | ||
|
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,111 @@ | ||
// | ||
// Copyright (c) 2024 Christian Mazakas | ||
// | ||
// 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_SRC_DETAIL_HEADER_HPP | ||
#define BOOST_HTTP_PROTO_SRC_DETAIL_HEADER_HPP | ||
|
||
#include <boost/http_proto/message_base.hpp> | ||
#include <boost/http_proto/detail/header.hpp> | ||
#include <boost/core/span.hpp> | ||
#include <cstddef> | ||
|
||
namespace boost { | ||
namespace http_proto { | ||
namespace detail { | ||
|
||
struct prefix_op { | ||
message_base& mb_; | ||
span<char> prefix_; | ||
char* buf_ = nullptr; | ||
std::size_t n_ = 0; | ||
|
||
prefix_op( | ||
message_base& mb, | ||
std::size_t n) | ||
: mb_{mb} | ||
, n_{n} | ||
{ | ||
auto& h = mb_.h_; | ||
if( h.buf && n <= h.prefix ) | ||
{ | ||
prefix_ = {h.buf, n}; | ||
return; | ||
} | ||
|
||
// allocate or grow | ||
if( n > h.prefix && | ||
static_cast<std::size_t>( | ||
n - h.prefix) > | ||
static_cast<std::size_t>( | ||
max_offset - h.size)) | ||
detail::throw_length_error(); | ||
|
||
auto n0 = detail::header::bytes_needed( | ||
n + h.size - h.prefix, | ||
h.count); | ||
|
||
auto p = new char[n0]; | ||
if( h.buf != nullptr ) | ||
{ | ||
std::memcpy( | ||
p + n, | ||
h.buf + h.prefix, | ||
h.size - h.prefix); | ||
|
||
h.copy_table(p + n0); | ||
} | ||
else | ||
{ | ||
std::memcpy( | ||
p + n, | ||
h.cbuf + h.prefix, | ||
h.size - h.prefix); | ||
} | ||
|
||
prefix_ = {p, n}; | ||
buf_ = h.buf; | ||
|
||
h.buf = p; | ||
h.cbuf = p; | ||
h.size = static_cast< | ||
offset_type>(h.size + | ||
n - h.prefix); | ||
h.prefix = static_cast< | ||
offset_type>(n); | ||
h.cap = n0; | ||
} | ||
|
||
prefix_op(prefix_op&&) = delete; | ||
prefix_op(prefix_op const&) = delete; | ||
|
||
~prefix_op() | ||
{ | ||
auto& h = mb_.h_; | ||
if( n_ < h.prefix ) | ||
{ | ||
std::memmove( | ||
h.buf + n_, | ||
h.buf + h.prefix, | ||
h.size - h.prefix); | ||
|
||
h.size = static_cast< | ||
offset_type>(h.size - | ||
h.prefix + n_); | ||
h.prefix = static_cast< | ||
offset_type>(n_); | ||
} | ||
delete[] buf_; | ||
} | ||
}; | ||
|
||
} // detail | ||
} // http_proto | ||
} // boost | ||
|
||
#endif // BOOST_HTTP_PROTO_SRC_DETAIL_HEADER_HPP |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// | ||
// Copyright (c) 2021 Vinnie Falco ([email protected]) | ||
// Copyright (c) 2024 Christian Mazakas | ||
// | ||
// 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) | ||
|
@@ -185,69 +186,5 @@ set_keep_alive(bool value) | |
} | ||
} | ||
|
||
//------------------------------------------------ | ||
|
||
char* | ||
message_base:: | ||
set_prefix_impl( | ||
std::size_t n) | ||
{ | ||
if( n > h_.prefix || | ||
h_.buf == nullptr) | ||
{ | ||
// allocate or grow | ||
if( n > h_.prefix && | ||
static_cast<std::size_t>( | ||
n - h_.prefix) > | ||
static_cast<std::size_t>( | ||
max_offset - h_.size)) | ||
detail::throw_length_error(); | ||
|
||
auto n0 = detail::header::bytes_needed( | ||
n + h_.size - h_.prefix, | ||
h_.count); | ||
auto buf = new char[n0]; | ||
if(h_.buf != nullptr) | ||
{ | ||
std::memcpy( | ||
buf + n, | ||
h_.buf + h_.prefix, | ||
h_.size - h_.prefix); | ||
detail::header::table ft( | ||
h_.buf + h_.cap); | ||
h_.copy_table(buf + n0); | ||
delete[] h_.buf; | ||
} | ||
else | ||
{ | ||
std::memcpy( | ||
buf + n, | ||
h_.cbuf + h_.prefix, | ||
h_.size - h_.prefix); | ||
} | ||
h_.buf = buf; | ||
h_.cbuf = buf; | ||
h_.size = static_cast< | ||
offset_type>(h_.size + | ||
n - h_.prefix); | ||
h_.prefix = static_cast< | ||
offset_type>(n); | ||
h_.cap = n0; | ||
return h_.buf; | ||
} | ||
|
||
// shrink | ||
std::memmove( | ||
h_.buf + n, | ||
h_.buf + h_.prefix, | ||
h_.size - h_.prefix); | ||
h_.size = static_cast< | ||
offset_type>(h_.size - | ||
h_.prefix + n); | ||
h_.prefix = static_cast< | ||
offset_type>(n); | ||
return h_.buf; | ||
} | ||
|
||
} // http_proto | ||
} // boost |
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