forked from boostorg/beast
Move bench/ to test/
This commit is contained in:
@@ -12,5 +12,6 @@ if (OPENSSL_FOUND)
|
||||
endif()
|
||||
|
||||
add_subdirectory (beast)
|
||||
add_subdirectory (bench)
|
||||
add_subdirectory (doc)
|
||||
add_subdirectory (example)
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
alias run-tests :
|
||||
beast//run-tests
|
||||
bench//run-tests
|
||||
doc//run-tests
|
||||
example//run-tests
|
||||
;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#
|
||||
# Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
#
|
||||
# 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/boostorg/beast
|
||||
#
|
||||
|
||||
add_subdirectory (buffers)
|
||||
add_subdirectory (parser)
|
||||
add_subdirectory (utf8_checker)
|
||||
add_subdirectory (wsload)
|
||||
@@ -0,0 +1,15 @@
|
||||
#
|
||||
# Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
#
|
||||
# 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/boostorg/beast
|
||||
#
|
||||
|
||||
alias run-tests :
|
||||
buffers//run-tests
|
||||
parser//run-tests
|
||||
wsload//run-tests
|
||||
utf8_checker//run-tests
|
||||
;
|
||||
@@ -0,0 +1,20 @@
|
||||
#
|
||||
# Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
#
|
||||
# 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/boostorg/beast
|
||||
#
|
||||
|
||||
GroupSources(extras/boost/beast extras)
|
||||
GroupSources(include/boost/beast beast)
|
||||
GroupSources(test/bench/buffers "/")
|
||||
|
||||
add_executable (bench-buffers
|
||||
${BOOST_BEAST_INCLUDES}
|
||||
${EXTRAS_INCLUDES}
|
||||
${TEST_MAIN}
|
||||
Jamfile
|
||||
bench_buffers.cpp
|
||||
)
|
||||
@@ -0,0 +1,19 @@
|
||||
#
|
||||
# Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
#
|
||||
# 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/boostorg/beast
|
||||
#
|
||||
|
||||
exe bench-buffers :
|
||||
$(TEST_MAIN)
|
||||
bench_buffers.cpp
|
||||
;
|
||||
|
||||
explicit bench-buffers ;
|
||||
|
||||
alias run-tests :
|
||||
[ compile bench_buffers.cpp ]
|
||||
;
|
||||
@@ -0,0 +1,242 @@
|
||||
//
|
||||
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
//
|
||||
// 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/boostorg/beast
|
||||
//
|
||||
|
||||
#include <boost/beast/core/flat_buffer.hpp>
|
||||
#include <boost/beast/core/multi_buffer.hpp>
|
||||
#include <boost/beast/core/read_size.hpp>
|
||||
#include <boost/beast/core/string.hpp>
|
||||
#include <boost/beast/unit_test/suite.hpp>
|
||||
#include <boost/asio/streambuf.hpp>
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <iomanip>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
class buffers_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
using size_type = std::uint64_t;
|
||||
|
||||
class timer
|
||||
{
|
||||
using clock_type =
|
||||
std::chrono::system_clock;
|
||||
|
||||
clock_type::time_point when_;
|
||||
|
||||
public:
|
||||
using duration =
|
||||
clock_type::duration;
|
||||
|
||||
timer()
|
||||
: when_(clock_type::now())
|
||||
{
|
||||
}
|
||||
|
||||
duration
|
||||
elapsed() const
|
||||
{
|
||||
return clock_type::now() - when_;
|
||||
}
|
||||
};
|
||||
|
||||
inline
|
||||
size_type
|
||||
throughput(std::chrono::duration<
|
||||
double> const& elapsed, size_type items)
|
||||
{
|
||||
using namespace std::chrono;
|
||||
return static_cast<size_type>(
|
||||
1 / (elapsed/items).count());
|
||||
}
|
||||
|
||||
template<class MutableBufferSequence>
|
||||
static
|
||||
std::size_t
|
||||
fill(MutableBufferSequence const& buffers)
|
||||
{
|
||||
std::size_t n = 0;
|
||||
using boost::asio::buffer_cast;
|
||||
using boost::asio::buffer_size;
|
||||
for(boost::asio::mutable_buffer buffer : buffers)
|
||||
{
|
||||
std::fill(
|
||||
buffer_cast<char*>(buffer),
|
||||
buffer_cast<char*>(buffer) +
|
||||
buffer_size(buffer), '\0');
|
||||
n += buffer_size(buffer);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
template<class DynamicBuffer>
|
||||
size_type
|
||||
do_prepares(std::size_t repeat,
|
||||
std::size_t count, std::size_t size)
|
||||
{
|
||||
timer t;
|
||||
size_type total = 0;
|
||||
for(auto i = repeat; i--;)
|
||||
{
|
||||
DynamicBuffer b;
|
||||
for(auto j = count; j--;)
|
||||
{
|
||||
auto const n = fill(b.prepare(size));
|
||||
b.commit(n);
|
||||
total += n;
|
||||
}
|
||||
}
|
||||
return throughput(t.elapsed(), total);
|
||||
}
|
||||
|
||||
template<class DynamicBuffer>
|
||||
size_type
|
||||
do_hints(std::size_t repeat,
|
||||
std::size_t count, std::size_t size)
|
||||
{
|
||||
timer t;
|
||||
size_type total = 0;
|
||||
for(auto i = repeat; i--;)
|
||||
{
|
||||
DynamicBuffer b;
|
||||
for(auto j = count; j--;)
|
||||
{
|
||||
for(auto remain = size; remain;)
|
||||
{
|
||||
auto const n = fill(b.prepare(
|
||||
read_size(b, remain)));
|
||||
b.commit(n);
|
||||
remain -= n;
|
||||
total += n;
|
||||
}
|
||||
}
|
||||
}
|
||||
return throughput(t.elapsed(), total);
|
||||
}
|
||||
|
||||
template<class DynamicBuffer>
|
||||
size_type
|
||||
do_random(std::size_t repeat,
|
||||
std::size_t count, std::size_t size)
|
||||
{
|
||||
timer t;
|
||||
size_type total = 0;
|
||||
for(auto i = repeat; i--;)
|
||||
{
|
||||
DynamicBuffer b;
|
||||
for(auto j = count; j--;)
|
||||
{
|
||||
auto const n = fill(b.prepare(
|
||||
1 + (rand()%(2*size))));
|
||||
b.commit(n);
|
||||
total += n;
|
||||
}
|
||||
}
|
||||
return throughput(t.elapsed(), total);
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
void
|
||||
do_trials_1(bool)
|
||||
{
|
||||
}
|
||||
|
||||
template<class F0, class... FN>
|
||||
void
|
||||
do_trials_1(bool print, F0&& f, FN... fn)
|
||||
{
|
||||
timer t;
|
||||
using namespace std::chrono;
|
||||
static size_type constexpr den = 1024 * 1024;
|
||||
if(print)
|
||||
{
|
||||
log << std::right << std::setw(10) <<
|
||||
((f() + (den / 2)) / den) << " MB/s";
|
||||
log.flush();
|
||||
}
|
||||
else
|
||||
{
|
||||
f();
|
||||
}
|
||||
do_trials_1(print, fn...);
|
||||
}
|
||||
|
||||
template<class F0, class... FN>
|
||||
void
|
||||
do_trials(string_view name,
|
||||
std::size_t trials, F0&& f0, FN... fn)
|
||||
{
|
||||
using namespace std::chrono;
|
||||
// warm-up
|
||||
do_trials_1(false, f0, fn...);
|
||||
do_trials_1(false, f0, fn...);
|
||||
while(trials--)
|
||||
{
|
||||
timer t;
|
||||
log << std::left << std::setw(24) << name << ":";
|
||||
log.flush();
|
||||
do_trials_1(true, f0, fn...);
|
||||
log << " " <<
|
||||
duration_cast<milliseconds>(t.elapsed()).count() << "ms";
|
||||
log << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
static std::size_t constexpr trials = 1;
|
||||
static std::size_t constexpr repeat = 250;
|
||||
std::vector<std::pair<std::size_t, std::size_t>> params;
|
||||
params.emplace_back(1024, 1024);
|
||||
params.emplace_back(512, 4096);
|
||||
params.emplace_back(256, 32768);
|
||||
log << std::endl;
|
||||
for(auto const& param : params)
|
||||
{
|
||||
auto const count = param.first;
|
||||
auto const size = param.second;
|
||||
auto const s = std::string("count=") + std::to_string(count) +
|
||||
", size=" + std::to_string(size);
|
||||
log << std::left << std::setw(24) << s << " " <<
|
||||
std::right << std::setw(15) << "prepare" <<
|
||||
std::right << std::setw(15) << "with hint" <<
|
||||
std::right << std::setw(15) << "random" <<
|
||||
std::endl;
|
||||
do_trials("multi_buffer", trials,
|
||||
[&](){ return do_prepares<multi_buffer>(repeat, count, size); }
|
||||
,[&](){ return do_hints <multi_buffer>(repeat, count, size); }
|
||||
,[&](){ return do_random <multi_buffer>(repeat, count, size); }
|
||||
);
|
||||
do_trials("flat_buffer", trials,
|
||||
[&](){ return do_prepares<flat_buffer>(repeat, count, size); }
|
||||
,[&](){ return do_hints <flat_buffer>(repeat, count, size); }
|
||||
,[&](){ return do_random <flat_buffer>(repeat, count, size); }
|
||||
);
|
||||
do_trials("boost::asio::streambuf", trials,
|
||||
[&](){ return do_prepares<boost::asio::streambuf>(repeat, count, size); }
|
||||
,[&](){ return do_hints <boost::asio::streambuf>(repeat, count, size); }
|
||||
,[&](){ return do_random <boost::asio::streambuf>(repeat, count, size); }
|
||||
);
|
||||
log << std::endl;
|
||||
}
|
||||
pass();
|
||||
}
|
||||
};
|
||||
|
||||
BOOST_BEAST_DEFINE_TESTSUITE(buffers,benchmarks,beast);
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
@@ -0,0 +1,24 @@
|
||||
#
|
||||
# Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
#
|
||||
# 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/boostorg/beast
|
||||
#
|
||||
|
||||
GroupSources(extras/boost/beast extras)
|
||||
GroupSources(include/boost/beast beast)
|
||||
GroupSources(test/beast/http "/")
|
||||
GroupSources(test/bench/parser "/")
|
||||
|
||||
add_executable (bench-parser
|
||||
${BOOST_BEAST_INCLUDES}
|
||||
${EXTRAS_INCLUDES}
|
||||
${TEST_MAIN}
|
||||
Jamfile
|
||||
${PROJECT_SOURCE_DIR}/test/beast/http/message_fuzz.hpp
|
||||
nodejs_parser.hpp
|
||||
nodejs_parser.cpp
|
||||
bench_parser.cpp
|
||||
)
|
||||
@@ -0,0 +1,21 @@
|
||||
#
|
||||
# Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
#
|
||||
# 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/boostorg/beast
|
||||
#
|
||||
|
||||
exe bench-parser :
|
||||
$(TEST_MAIN)
|
||||
nodejs_parser.cpp
|
||||
bench_parser.cpp
|
||||
;
|
||||
|
||||
explicit bench-parser ;
|
||||
|
||||
alias run-tests :
|
||||
[ compile nodejs_parser.cpp ]
|
||||
[ compile bench_parser.cpp ]
|
||||
;
|
||||
@@ -0,0 +1,308 @@
|
||||
//
|
||||
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
//
|
||||
// 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/boostorg/beast
|
||||
//
|
||||
|
||||
#include "nodejs_parser.hpp"
|
||||
|
||||
#include "test/beast/http/message_fuzz.hpp"
|
||||
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/core/consuming_buffers.hpp>
|
||||
#include <boost/beast/core/ostream.hpp>
|
||||
#include <boost/beast/core/flat_buffer.hpp>
|
||||
#include <boost/beast/core/multi_buffer.hpp>
|
||||
#include <boost/beast/unit_test/suite.hpp>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class parser_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
static std::size_t constexpr N = 2000;
|
||||
|
||||
//using corpus = std::vector<multi_buffer>;
|
||||
using corpus = std::vector<flat_buffer>;
|
||||
|
||||
corpus creq_;
|
||||
corpus cres_;
|
||||
std::size_t size_ = 0;
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
static
|
||||
std::string
|
||||
to_string(ConstBufferSequence const& bs)
|
||||
{
|
||||
using boost::asio::buffer_cast;
|
||||
using boost::asio::buffer_size;
|
||||
std::string s;
|
||||
s.reserve(buffer_size(bs));
|
||||
for(boost::asio::const_buffer b : bs)
|
||||
s.append(buffer_cast<char const*>(b),
|
||||
buffer_size(b));
|
||||
return s;
|
||||
}
|
||||
|
||||
corpus
|
||||
build_corpus(std::size_t n, std::true_type)
|
||||
{
|
||||
corpus v;
|
||||
v.resize(n);
|
||||
message_fuzz mg;
|
||||
for(std::size_t i = 0; i < n; ++i)
|
||||
{
|
||||
mg.request(v[i]);
|
||||
size_ += v[i].size();
|
||||
BEAST_EXPECT(v[i].size() > 0);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
corpus
|
||||
build_corpus(std::size_t n, std::false_type)
|
||||
{
|
||||
corpus v;
|
||||
v.resize(n);
|
||||
message_fuzz mg;
|
||||
for(std::size_t i = 0; i < n; ++i)
|
||||
{
|
||||
mg.response(v[i]);
|
||||
size_ += v[i].size();
|
||||
BEAST_EXPECT(v[i].size() > 0);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template<class ConstBufferSequence,
|
||||
bool isRequest, class Derived>
|
||||
static
|
||||
std::size_t
|
||||
feed(ConstBufferSequence const& buffers,
|
||||
basic_parser<isRequest, Derived>& parser,
|
||||
error_code& ec)
|
||||
{
|
||||
using boost::asio::buffer_size;
|
||||
beast::consuming_buffers<
|
||||
ConstBufferSequence> cb{buffers};
|
||||
std::size_t used = 0;
|
||||
for(;;)
|
||||
{
|
||||
auto const n =
|
||||
parser.put(cb, ec);
|
||||
if(ec)
|
||||
return 0;
|
||||
if(n == 0)
|
||||
break;
|
||||
cb.consume(n);
|
||||
used += n;
|
||||
if(parser.is_done())
|
||||
break;
|
||||
if(buffer_size(cb) == 0)
|
||||
break;
|
||||
}
|
||||
return used;
|
||||
}
|
||||
|
||||
template<class Parser>
|
||||
void
|
||||
testParser1(std::size_t repeat, corpus const& v)
|
||||
{
|
||||
while(repeat--)
|
||||
for(auto const& b : v)
|
||||
{
|
||||
Parser p;
|
||||
error_code ec;
|
||||
p.write(b.data(), ec);
|
||||
if(! BEAST_EXPECTS(! ec, ec.message()))
|
||||
log << to_string(b.data()) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
template<class Parser>
|
||||
void
|
||||
testParser2(std::size_t repeat, corpus const& v)
|
||||
{
|
||||
while(repeat--)
|
||||
for(auto const& b : v)
|
||||
{
|
||||
Parser p;
|
||||
p.header_limit((std::numeric_limits<std::uint32_t>::max)());
|
||||
error_code ec;
|
||||
feed(b.data(), p, ec);
|
||||
if(! BEAST_EXPECTS(! ec, ec.message()))
|
||||
log << to_string(b.data()) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
template<class Function>
|
||||
void
|
||||
timedTest(std::size_t repeat, std::string const& name, Function&& f)
|
||||
{
|
||||
using namespace std::chrono;
|
||||
using clock_type = std::chrono::high_resolution_clock;
|
||||
log << name << std::endl;
|
||||
for(std::size_t trial = 1; trial <= repeat; ++trial)
|
||||
{
|
||||
auto const t0 = clock_type::now();
|
||||
f();
|
||||
auto const elapsed = clock_type::now() - t0;
|
||||
log <<
|
||||
"Trial " << trial << ": " <<
|
||||
duration_cast<milliseconds>(elapsed).count() << " ms" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
template<bool isRequest>
|
||||
struct null_parser :
|
||||
basic_parser<isRequest, null_parser<isRequest>>
|
||||
{
|
||||
};
|
||||
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
struct bench_parser : basic_parser<
|
||||
isRequest, bench_parser<isRequest, Body, Fields>>
|
||||
{
|
||||
using mutable_buffers_type =
|
||||
boost::asio::mutable_buffers_1;
|
||||
|
||||
void
|
||||
on_request_impl(verb, string_view,
|
||||
string_view, int, error_code& ec)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
void
|
||||
on_response_impl(int,
|
||||
string_view,
|
||||
int, error_code& ec)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
void
|
||||
on_field_impl(field,
|
||||
string_view, string_view, error_code& ec)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
void
|
||||
on_header_impl(error_code& ec)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
void
|
||||
on_body_init_impl(
|
||||
boost::optional<std::uint64_t> const&,
|
||||
error_code& ec)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
std::size_t
|
||||
on_body_impl(string_view s, error_code& ec)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
return s.size();
|
||||
}
|
||||
|
||||
void
|
||||
on_chunk_header_impl(std::uint64_t,
|
||||
string_view, error_code& ec)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
std::size_t
|
||||
on_chunk_body_impl(std::uint64_t,
|
||||
string_view s, error_code& ec)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
return s.size();
|
||||
}
|
||||
|
||||
void
|
||||
on_finish_impl(error_code& ec)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
};
|
||||
|
||||
void
|
||||
testSpeed()
|
||||
{
|
||||
static std::size_t constexpr Trials = 5;
|
||||
static std::size_t constexpr Repeat = 500;
|
||||
|
||||
creq_ = build_corpus(N/2, std::true_type{});
|
||||
cres_ = build_corpus(N/2, std::false_type{});
|
||||
|
||||
log << "sizeof(request parser) == " <<
|
||||
sizeof(null_parser<true>) << '\n';
|
||||
|
||||
log << "sizeof(response parser) == " <<
|
||||
sizeof(null_parser<false>)<< '\n';
|
||||
|
||||
testcase << "Parser speed test, " <<
|
||||
((Repeat * size_ + 512) / 1024) << "KB in " <<
|
||||
(Repeat * (creq_.size() + cres_.size())) << " messages";
|
||||
|
||||
#if 0
|
||||
timedTest(Trials, "http::parser",
|
||||
[&]
|
||||
{
|
||||
testParser2<request_parser<dynamic_body>>(Repeat, creq_);
|
||||
testParser2<response_parser<dynamic_body>>(Repeat, cres_);
|
||||
});
|
||||
#endif
|
||||
#if 1
|
||||
timedTest(Trials, "http::basic_parser",
|
||||
[&]
|
||||
{
|
||||
testParser2<bench_parser<
|
||||
true, dynamic_body, fields> >(
|
||||
Repeat, creq_);
|
||||
testParser2<bench_parser<
|
||||
false, dynamic_body, fields>>(
|
||||
Repeat, cres_);
|
||||
});
|
||||
#if 1
|
||||
timedTest(Trials, "nodejs_parser",
|
||||
[&]
|
||||
{
|
||||
testParser1<nodejs_parser<
|
||||
true, dynamic_body, fields>>(
|
||||
Repeat, creq_);
|
||||
testParser1<nodejs_parser<
|
||||
false, dynamic_body, fields>>(
|
||||
Repeat, cres_);
|
||||
});
|
||||
#endif
|
||||
#endif
|
||||
pass();
|
||||
}
|
||||
|
||||
void run() override
|
||||
{
|
||||
pass();
|
||||
testSpeed();
|
||||
}
|
||||
};
|
||||
|
||||
BOOST_BEAST_DEFINE_TESTSUITE(parser,benchmarks,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
# Authors ordered by first contribution.
|
||||
Ryan Dahl <ry@tinyclouds.org>
|
||||
Jeremy Hinegardner <jeremy@hinegardner.org>
|
||||
Sergey Shepelev <temotor@gmail.com>
|
||||
Joe Damato <ice799@gmail.com>
|
||||
tomika <tomika_nospam@freemail.hu>
|
||||
Phoenix Sol <phoenix@burninglabs.com>
|
||||
Cliff Frey <cliff@meraki.com>
|
||||
Ewen Cheslack-Postava <ewencp@cs.stanford.edu>
|
||||
Santiago Gala <sgala@apache.org>
|
||||
Tim Becker <tim.becker@syngenio.de>
|
||||
Jeff Terrace <jterrace@gmail.com>
|
||||
Ben Noordhuis <info@bnoordhuis.nl>
|
||||
Nathan Rajlich <nathan@tootallnate.net>
|
||||
Mark Nottingham <mnot@mnot.net>
|
||||
Aman Gupta <aman@tmm1.net>
|
||||
Tim Becker <tim.becker@kuriositaet.de>
|
||||
Sean Cunningham <sean.cunningham@mandiant.com>
|
||||
Peter Griess <pg@std.in>
|
||||
Salman Haq <salman.haq@asti-usa.com>
|
||||
Cliff Frey <clifffrey@gmail.com>
|
||||
Jon Kolb <jon@b0g.us>
|
||||
Fouad Mardini <f.mardini@gmail.com>
|
||||
Paul Querna <pquerna@apache.org>
|
||||
Felix Geisendörfer <felix@debuggable.com>
|
||||
koichik <koichik@improvement.jp>
|
||||
Andre Caron <andre.l.caron@gmail.com>
|
||||
Ivo Raisr <ivosh@ivosh.net>
|
||||
James McLaughlin <jamie@lacewing-project.org>
|
||||
David Gwynne <loki@animata.net>
|
||||
Thomas LE ROUX <thomas@november-eleven.fr>
|
||||
Randy Rizun <rrizun@ortivawireless.com>
|
||||
Andre Louis Caron <andre.louis.caron@usherbrooke.ca>
|
||||
Simon Zimmermann <simonz05@gmail.com>
|
||||
Erik Dubbelboer <erik@dubbelboer.com>
|
||||
Martell Malone <martellmalone@gmail.com>
|
||||
Bertrand Paquet <bpaquet@octo.com>
|
||||
BogDan Vatra <bogdan@kde.org>
|
||||
Peter Faiman <peter@thepicard.org>
|
||||
Corey Richardson <corey@octayn.net>
|
||||
Tóth Tamás <tomika_nospam@freemail.hu>
|
||||
Cam Swords <cam.swords@gmail.com>
|
||||
Chris Dickinson <christopher.s.dickinson@gmail.com>
|
||||
Uli Köhler <ukoehler@btronik.de>
|
||||
Charlie Somerville <charlie@charliesomerville.com>
|
||||
Patrik Stutz <patrik.stutz@gmail.com>
|
||||
Fedor Indutny <fedor.indutny@gmail.com>
|
||||
runner <runner.mei@gmail.com>
|
||||
Alexis Campailla <alexis@janeasystems.com>
|
||||
David Wragg <david@wragg.org>
|
||||
Vinnie Falco <vinnie.falco@gmail.com>
|
||||
Alex Butum <alexbutum@linux.com>
|
||||
Rex Feng <rexfeng@gmail.com>
|
||||
Alex Kocharin <alex@kocharin.ru>
|
||||
Mark Koopman <markmontymark@yahoo.com>
|
||||
Helge Heß <me@helgehess.eu>
|
||||
Alexis La Goutte <alexis.lagoutte@gmail.com>
|
||||
George Miroshnykov <george.miroshnykov@gmail.com>
|
||||
Maciej Małecki <me@mmalecki.com>
|
||||
Marc O'Morain <github.com@marcomorain.com>
|
||||
Jeff Pinner <jpinner@twitter.com>
|
||||
Timothy J Fontaine <tjfontaine@gmail.com>
|
||||
Akagi201 <akagi201@gmail.com>
|
||||
Romain Giraud <giraud.romain@gmail.com>
|
||||
Jay Satiro <raysatiro@yahoo.com>
|
||||
Arne Steen <Arne.Steen@gmx.de>
|
||||
Kjell Schubert <kjell.schubert@gmail.com>
|
||||
Olivier Mengué <dolmen@cpan.org>
|
||||
@@ -0,0 +1,23 @@
|
||||
http_parser.c is based on src/http/ngx_http_parse.c from NGINX copyright
|
||||
Igor Sysoev.
|
||||
|
||||
Additional changes are licensed under the same terms as NGINX and
|
||||
copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to
|
||||
deal in the Software without restriction, including without limitation the
|
||||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
sell copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
IN THE SOFTWARE.
|
||||
@@ -0,0 +1,246 @@
|
||||
HTTP Parser
|
||||
===========
|
||||
|
||||
[](https://travis-ci.org/nodejs/http-parser)
|
||||
|
||||
This is a parser for HTTP messages written in C. It parses both requests and
|
||||
responses. The parser is designed to be used in performance HTTP
|
||||
applications. It does not make any syscalls nor allocations, it does not
|
||||
buffer data, it can be interrupted at anytime. Depending on your
|
||||
architecture, it only requires about 40 bytes of data per message
|
||||
stream (in a web server that is per connection).
|
||||
|
||||
Features:
|
||||
|
||||
* No dependencies
|
||||
* Handles persistent streams (keep-alive).
|
||||
* Decodes chunked encoding.
|
||||
* Upgrade support
|
||||
* Defends against buffer overflow attacks.
|
||||
|
||||
The parser extracts the following information from HTTP messages:
|
||||
|
||||
* Header fields and values
|
||||
* Content-Length
|
||||
* Request method
|
||||
* Response status code
|
||||
* Transfer-Encoding
|
||||
* HTTP version
|
||||
* Request URL
|
||||
* Message body
|
||||
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
One `http_parser` object is used per TCP connection. Initialize the struct
|
||||
using `http_parser_init()` and set the callbacks. That might look something
|
||||
like this for a request parser:
|
||||
```c
|
||||
http_parser_settings settings;
|
||||
settings.on_url = my_url_callback;
|
||||
settings.on_header_field = my_header_field_callback;
|
||||
/* ... */
|
||||
|
||||
http_parser *parser = malloc(sizeof(http_parser));
|
||||
http_parser_init(parser, HTTP_REQUEST);
|
||||
parser->data = my_socket;
|
||||
```
|
||||
|
||||
When data is received on the socket execute the parser and check for errors.
|
||||
|
||||
```c
|
||||
size_t len = 80*1024, nparsed;
|
||||
char buf[len];
|
||||
ssize_t recved;
|
||||
|
||||
recved = recv(fd, buf, len, 0);
|
||||
|
||||
if (recved < 0) {
|
||||
/* Handle error. */
|
||||
}
|
||||
|
||||
/* Start up / continue the parser.
|
||||
* Note we pass recved==0 to signal that EOF has been received.
|
||||
*/
|
||||
nparsed = http_parser_execute(parser, &settings, buf, recved);
|
||||
|
||||
if (parser->upgrade) {
|
||||
/* handle new protocol */
|
||||
} else if (nparsed != recved) {
|
||||
/* Handle error. Usually just close the connection. */
|
||||
}
|
||||
```
|
||||
|
||||
HTTP needs to know where the end of the stream is. For example, sometimes
|
||||
servers send responses without Content-Length and expect the client to
|
||||
consume input (for the body) until EOF. To tell http_parser about EOF, give
|
||||
`0` as the fourth parameter to `http_parser_execute()`. Callbacks and errors
|
||||
can still be encountered during an EOF, so one must still be prepared
|
||||
to receive them.
|
||||
|
||||
Scalar valued message information such as `status_code`, `method`, and the
|
||||
HTTP version are stored in the parser structure. This data is only
|
||||
temporally stored in `http_parser` and gets reset on each new message. If
|
||||
this information is needed later, copy it out of the structure during the
|
||||
`headers_complete` callback.
|
||||
|
||||
The parser decodes the transfer-encoding for both requests and responses
|
||||
transparently. That is, a chunked encoding is decoded before being sent to
|
||||
the on_body callback.
|
||||
|
||||
|
||||
The Special Problem of Upgrade
|
||||
------------------------------
|
||||
|
||||
HTTP supports upgrading the connection to a different protocol. An
|
||||
increasingly common example of this is the WebSocket protocol which sends
|
||||
a request like
|
||||
|
||||
GET /demo HTTP/1.1
|
||||
Upgrade: WebSocket
|
||||
Connection: Upgrade
|
||||
Host: example.com
|
||||
Origin: http://example.com
|
||||
WebSocket-Protocol: sample
|
||||
|
||||
followed by non-HTTP data.
|
||||
|
||||
(See [RFC6455](https://tools.ietf.org/html/rfc6455) for more information the
|
||||
WebSocket protocol.)
|
||||
|
||||
To support this, the parser will treat this as a normal HTTP message without a
|
||||
body, issuing both on_headers_complete and on_message_complete callbacks. However
|
||||
http_parser_execute() will stop parsing at the end of the headers and return.
|
||||
|
||||
The user is expected to check if `parser->upgrade` has been set to 1 after
|
||||
`http_parser_execute()` returns. Non-HTTP data begins at the buffer supplied
|
||||
offset by the return value of `http_parser_execute()`.
|
||||
|
||||
|
||||
Callbacks
|
||||
---------
|
||||
|
||||
During the `http_parser_execute()` call, the callbacks set in
|
||||
`http_parser_settings` will be executed. The parser maintains state and
|
||||
never looks behind, so buffering the data is not necessary. If you need to
|
||||
save certain data for later usage, you can do that from the callbacks.
|
||||
|
||||
There are two types of callbacks:
|
||||
|
||||
* notification `typedef int (*http_cb) (http_parser*);`
|
||||
Callbacks: on_message_begin, on_headers_complete, on_message_complete.
|
||||
* data `typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);`
|
||||
Callbacks: (requests only) on_url,
|
||||
(common) on_header_field, on_header_value, on_body;
|
||||
|
||||
Callbacks must return 0 on success. Returning a non-zero value indicates
|
||||
error to the parser, making it exit immediately.
|
||||
|
||||
For cases where it is necessary to pass local information to/from a callback,
|
||||
the `http_parser` object's `data` field can be used.
|
||||
An example of such a case is when using threads to handle a socket connection,
|
||||
parse a request, and then give a response over that socket. By instantiation
|
||||
of a thread-local struct containing relevant data (e.g. accepted socket,
|
||||
allocated memory for callbacks to write into, etc), a parser's callbacks are
|
||||
able to communicate data between the scope of the thread and the scope of the
|
||||
callback in a threadsafe manner. This allows http-parser to be used in
|
||||
multi-threaded contexts.
|
||||
|
||||
Example:
|
||||
```
|
||||
typedef struct {
|
||||
socket_t sock;
|
||||
void* buffer;
|
||||
int buf_len;
|
||||
} custom_data_t;
|
||||
|
||||
|
||||
int my_url_callback(http_parser* parser, const char *at, size_t length) {
|
||||
/* access to thread local custom_data_t struct.
|
||||
Use this access save parsed data for later use into thread local
|
||||
buffer, or communicate over socket
|
||||
*/
|
||||
parser->data;
|
||||
...
|
||||
return 0;
|
||||
}
|
||||
|
||||
...
|
||||
|
||||
void http_parser_thread(socket_t sock) {
|
||||
int nparsed = 0;
|
||||
/* allocate memory for user data */
|
||||
custom_data_t *my_data = malloc(sizeof(custom_data_t));
|
||||
|
||||
/* some information for use by callbacks.
|
||||
* achieves thread -> callback information flow */
|
||||
my_data->sock = sock;
|
||||
|
||||
/* instantiate a thread-local parser */
|
||||
http_parser *parser = malloc(sizeof(http_parser));
|
||||
http_parser_init(parser, HTTP_REQUEST); /* initialise parser */
|
||||
/* this custom data reference is accessible through the reference to the
|
||||
parser supplied to callback functions */
|
||||
parser->data = my_data;
|
||||
|
||||
http_parser_settings settings; / * set up callbacks */
|
||||
settings.on_url = my_url_callback;
|
||||
|
||||
/* execute parser */
|
||||
nparsed = http_parser_execute(parser, &settings, buf, recved);
|
||||
|
||||
...
|
||||
/* parsed information copied from callback.
|
||||
can now perform action on data copied into thread-local memory from callbacks.
|
||||
achieves callback -> thread information flow */
|
||||
my_data->buffer;
|
||||
...
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
In case you parse HTTP message in chunks (i.e. `read()` request line
|
||||
from socket, parse, read half headers, parse, etc) your data callbacks
|
||||
may be called more than once. Http-parser guarantees that data pointer is only
|
||||
valid for the lifetime of callback. You can also `read()` into a heap allocated
|
||||
buffer to avoid copying memory around if this fits your application.
|
||||
|
||||
Reading headers may be a tricky task if you read/parse headers partially.
|
||||
Basically, you need to remember whether last header callback was field or value
|
||||
and apply the following logic:
|
||||
|
||||
(on_header_field and on_header_value shortened to on_h_*)
|
||||
------------------------ ------------ --------------------------------------------
|
||||
| State (prev. callback) | Callback | Description/action |
|
||||
------------------------ ------------ --------------------------------------------
|
||||
| nothing (first call) | on_h_field | Allocate new buffer and copy callback data |
|
||||
| | | into it |
|
||||
------------------------ ------------ --------------------------------------------
|
||||
| value | on_h_field | New header started. |
|
||||
| | | Copy current name,value buffers to headers |
|
||||
| | | list and allocate new buffer for new name |
|
||||
------------------------ ------------ --------------------------------------------
|
||||
| field | on_h_field | Previous name continues. Reallocate name |
|
||||
| | | buffer and append callback data to it |
|
||||
------------------------ ------------ --------------------------------------------
|
||||
| field | on_h_value | Value for current header started. Allocate |
|
||||
| | | new buffer and copy callback data to it |
|
||||
------------------------ ------------ --------------------------------------------
|
||||
| value | on_h_value | Value continues. Reallocate value buffer |
|
||||
| | | and append callback data to it |
|
||||
------------------------ ------------ --------------------------------------------
|
||||
|
||||
|
||||
Parsing URLs
|
||||
------------
|
||||
|
||||
A simplistic zero-copy URL parser is provided as `http_parser_parse_url()`.
|
||||
Users of this library may wish to use it to parse URLs constructed from
|
||||
consecutive `on_url` callbacks.
|
||||
|
||||
See examples of reading in headers:
|
||||
|
||||
* [partial example](http://gist.github.com/155877) in C
|
||||
* [from http-parser tests](http://github.com/joyent/http-parser/blob/37a0ff8/test.c#L403) in C
|
||||
* [from Node library](http://github.com/joyent/node/blob/842eaf4/src/http.js#L284) in Javascript
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,362 @@
|
||||
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
#ifndef http_parser_h
|
||||
#define http_parser_h
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Also update SONAME in the Makefile whenever you change these. */
|
||||
#define HTTP_PARSER_VERSION_MAJOR 2
|
||||
#define HTTP_PARSER_VERSION_MINOR 7
|
||||
#define HTTP_PARSER_VERSION_PATCH 0
|
||||
|
||||
#include <sys/types.h>
|
||||
#if defined(_WIN32) && !defined(__MINGW32__) && \
|
||||
(!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__)
|
||||
#include <BaseTsd.h>
|
||||
#include <stddef.h>
|
||||
typedef __int8 int8_t;
|
||||
typedef unsigned __int8 uint8_t;
|
||||
typedef __int16 int16_t;
|
||||
typedef unsigned __int16 uint16_t;
|
||||
typedef __int32 int32_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
typedef __int64 int64_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
/* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
|
||||
* faster
|
||||
*/
|
||||
#ifndef HTTP_PARSER_STRICT
|
||||
# define HTTP_PARSER_STRICT 1
|
||||
#endif
|
||||
|
||||
/* Maximium header size allowed. If the macro is not defined
|
||||
* before including this header then the default is used. To
|
||||
* change the maximum header size, define the macro in the build
|
||||
* environment (e.g. -DHTTP_MAX_HEADER_SIZE=<value>). To remove
|
||||
* the effective limit on the size of the header, define the macro
|
||||
* to a very large number (e.g. -DHTTP_MAX_HEADER_SIZE=0x7fffffff)
|
||||
*/
|
||||
#ifndef HTTP_MAX_HEADER_SIZE
|
||||
# define HTTP_MAX_HEADER_SIZE (80*1024)
|
||||
#endif
|
||||
|
||||
typedef struct http_parser http_parser;
|
||||
typedef struct http_parser_settings http_parser_settings;
|
||||
|
||||
|
||||
/* Callbacks should return non-zero to indicate an error. The parser will
|
||||
* then halt execution.
|
||||
*
|
||||
* The one exception is on_headers_complete. In a HTTP_RESPONSE parser
|
||||
* returning '1' from on_headers_complete will tell the parser that it
|
||||
* should not expect a body. This is used when receiving a response to a
|
||||
* HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
|
||||
* chunked' headers that indicate the presence of a body.
|
||||
*
|
||||
* Returning `2` from on_headers_complete will tell parser that it should not
|
||||
* expect neither a body nor any futher responses on this connection. This is
|
||||
* useful for handling responses to a CONNECT request which may not contain
|
||||
* `Upgrade` or `Connection: upgrade` headers.
|
||||
*
|
||||
* http_data_cb does not return data chunks. It will be called arbitrarily
|
||||
* many times for each string. E.G. you might get 10 callbacks for "on_url"
|
||||
* each providing just a few characters more data.
|
||||
*/
|
||||
typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
|
||||
typedef int (*http_cb) (http_parser*);
|
||||
|
||||
|
||||
/* Request Methods */
|
||||
#define HTTP_METHOD_MAP(XX) \
|
||||
XX(0, DELETE, DELETE) \
|
||||
XX(1, GET, GET) \
|
||||
XX(2, HEAD, HEAD) \
|
||||
XX(3, POST, POST) \
|
||||
XX(4, PUT, PUT) \
|
||||
/* pathological */ \
|
||||
XX(5, CONNECT, CONNECT) \
|
||||
XX(6, OPTIONS, OPTIONS) \
|
||||
XX(7, TRACE, TRACE) \
|
||||
/* WebDAV */ \
|
||||
XX(8, COPY, COPY) \
|
||||
XX(9, LOCK, LOCK) \
|
||||
XX(10, MKCOL, MKCOL) \
|
||||
XX(11, MOVE, MOVE) \
|
||||
XX(12, PROPFIND, PROPFIND) \
|
||||
XX(13, PROPPATCH, PROPPATCH) \
|
||||
XX(14, SEARCH, SEARCH) \
|
||||
XX(15, UNLOCK, UNLOCK) \
|
||||
XX(16, BIND, BIND) \
|
||||
XX(17, REBIND, REBIND) \
|
||||
XX(18, UNBIND, UNBIND) \
|
||||
XX(19, ACL, ACL) \
|
||||
/* subversion */ \
|
||||
XX(20, REPORT, REPORT) \
|
||||
XX(21, MKACTIVITY, MKACTIVITY) \
|
||||
XX(22, CHECKOUT, CHECKOUT) \
|
||||
XX(23, MERGE, MERGE) \
|
||||
/* upnp */ \
|
||||
XX(24, MSEARCH, M-SEARCH) \
|
||||
XX(25, NOTIFY, NOTIFY) \
|
||||
XX(26, SUBSCRIBE, SUBSCRIBE) \
|
||||
XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \
|
||||
/* RFC-5789 */ \
|
||||
XX(28, PATCH, PATCH) \
|
||||
XX(29, PURGE, PURGE) \
|
||||
/* CalDAV */ \
|
||||
XX(30, MKCALENDAR, MKCALENDAR) \
|
||||
/* RFC-2068, section 19.6.1.2 */ \
|
||||
XX(31, LINK, LINK) \
|
||||
XX(32, UNLINK, UNLINK) \
|
||||
|
||||
enum http_method
|
||||
{
|
||||
#define XX(num, name, string) HTTP_##name = num,
|
||||
HTTP_METHOD_MAP(XX)
|
||||
#undef XX
|
||||
};
|
||||
|
||||
|
||||
enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
|
||||
|
||||
|
||||
/* Flag values for http_parser.flags field */
|
||||
enum flags
|
||||
{ F_CHUNKED = 1 << 0
|
||||
, F_CONNECTION_KEEP_ALIVE = 1 << 1
|
||||
, F_CONNECTION_CLOSE = 1 << 2
|
||||
, F_CONNECTION_UPGRADE = 1 << 3
|
||||
, F_TRAILING = 1 << 4
|
||||
, F_UPGRADE = 1 << 5
|
||||
, F_SKIPBODY = 1 << 6
|
||||
, F_CONTENTLENGTH = 1 << 7
|
||||
};
|
||||
|
||||
|
||||
/* Map for errno-related constants
|
||||
*
|
||||
* The provided argument should be a macro that takes 2 arguments.
|
||||
*/
|
||||
#define HTTP_ERRNO_MAP(XX) \
|
||||
/* No error */ \
|
||||
XX(OK, "success") \
|
||||
\
|
||||
/* Callback-related errors */ \
|
||||
XX(CB_message_begin, "the on_message_begin callback failed") \
|
||||
XX(CB_url, "the on_url callback failed") \
|
||||
XX(CB_header_field, "the on_header_field callback failed") \
|
||||
XX(CB_header_value, "the on_header_value callback failed") \
|
||||
XX(CB_headers_complete, "the on_headers_complete callback failed") \
|
||||
XX(CB_body, "the on_body callback failed") \
|
||||
XX(CB_message_complete, "the on_message_complete callback failed") \
|
||||
XX(CB_status, "the on_status callback failed") \
|
||||
XX(CB_chunk_header, "the on_chunk_header callback failed") \
|
||||
XX(CB_chunk_complete, "the on_chunk_complete callback failed") \
|
||||
\
|
||||
/* Parsing-related errors */ \
|
||||
XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
|
||||
XX(HEADER_OVERFLOW, \
|
||||
"too many header bytes seen; overflow detected") \
|
||||
XX(CLOSED_CONNECTION, \
|
||||
"data received after completed connection: close message") \
|
||||
XX(INVALID_VERSION, "invalid HTTP version") \
|
||||
XX(INVALID_STATUS, "invalid HTTP status code") \
|
||||
XX(INVALID_METHOD, "invalid HTTP method") \
|
||||
XX(INVALID_URL, "invalid URL") \
|
||||
XX(INVALID_HOST, "invalid host") \
|
||||
XX(INVALID_PORT, "invalid port") \
|
||||
XX(INVALID_PATH, "invalid path") \
|
||||
XX(INVALID_QUERY_STRING, "invalid query string") \
|
||||
XX(INVALID_FRAGMENT, "invalid fragment") \
|
||||
XX(LF_EXPECTED, "LF character expected") \
|
||||
XX(INVALID_HEADER_TOKEN, "invalid character in header") \
|
||||
XX(INVALID_CONTENT_LENGTH, \
|
||||
"invalid character in content-length header") \
|
||||
XX(UNEXPECTED_CONTENT_LENGTH, \
|
||||
"unexpected content-length header") \
|
||||
XX(INVALID_CHUNK_SIZE, \
|
||||
"invalid character in chunk size header") \
|
||||
XX(INVALID_CONSTANT, "invalid constant string") \
|
||||
XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
|
||||
XX(STRICT, "strict mode assertion failed") \
|
||||
XX(PAUSED, "parser is paused") \
|
||||
XX(UNKNOWN, "an unknown error occurred")
|
||||
|
||||
|
||||
/* Define HPE_* values for each errno value above */
|
||||
#define HTTP_ERRNO_GEN(n, s) HPE_##n,
|
||||
enum http_errno {
|
||||
HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
|
||||
};
|
||||
#undef HTTP_ERRNO_GEN
|
||||
|
||||
|
||||
/* Get an http_errno value from an http_parser */
|
||||
#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
|
||||
|
||||
|
||||
struct http_parser {
|
||||
/** PRIVATE **/
|
||||
unsigned int type : 2; /* enum http_parser_type */
|
||||
unsigned int flags : 8; /* F_* values from 'flags' enum; semi-public */
|
||||
unsigned int state : 7; /* enum state from http_parser.c */
|
||||
unsigned int header_state : 7; /* enum header_state from http_parser.c */
|
||||
unsigned int index : 7; /* index into current matcher */
|
||||
unsigned int lenient_http_headers : 1;
|
||||
|
||||
uint32_t nread; /* # bytes read in various scenarios */
|
||||
uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
|
||||
|
||||
/** READ-ONLY **/
|
||||
unsigned short http_major;
|
||||
unsigned short http_minor;
|
||||
unsigned int status_code : 16; /* responses only */
|
||||
unsigned int method : 8; /* requests only */
|
||||
unsigned int http_errno : 7;
|
||||
|
||||
/* 1 = Upgrade header was present and the parser has exited because of that.
|
||||
* 0 = No upgrade header present.
|
||||
* Should be checked when http_parser_execute() returns in addition to
|
||||
* error checking.
|
||||
*/
|
||||
unsigned int upgrade : 1;
|
||||
|
||||
/** PUBLIC **/
|
||||
void *data; /* A pointer to get hook to the "connection" or "socket" object */
|
||||
};
|
||||
|
||||
|
||||
struct http_parser_settings {
|
||||
http_cb on_message_begin;
|
||||
http_data_cb on_url;
|
||||
http_data_cb on_status;
|
||||
http_data_cb on_header_field;
|
||||
http_data_cb on_header_value;
|
||||
http_cb on_headers_complete;
|
||||
http_data_cb on_body;
|
||||
http_cb on_message_complete;
|
||||
/* When on_chunk_header is called, the current chunk length is stored
|
||||
* in parser->content_length.
|
||||
*/
|
||||
http_cb on_chunk_header;
|
||||
http_cb on_chunk_complete;
|
||||
};
|
||||
|
||||
|
||||
enum http_parser_url_fields
|
||||
{ UF_SCHEMA = 0
|
||||
, UF_HOST = 1
|
||||
, UF_PORT = 2
|
||||
, UF_PATH = 3
|
||||
, UF_QUERY = 4
|
||||
, UF_FRAGMENT = 5
|
||||
, UF_USERINFO = 6
|
||||
, UF_MAX = 7
|
||||
};
|
||||
|
||||
|
||||
/* Result structure for http_parser_parse_url().
|
||||
*
|
||||
* Callers should index into field_data[] with UF_* values iff field_set
|
||||
* has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
|
||||
* because we probably have padding left over), we convert any port to
|
||||
* a uint16_t.
|
||||
*/
|
||||
struct http_parser_url {
|
||||
uint16_t field_set; /* Bitmask of (1 << UF_*) values */
|
||||
uint16_t port; /* Converted UF_PORT string */
|
||||
|
||||
struct {
|
||||
uint16_t off; /* Offset into buffer in which field starts */
|
||||
uint16_t len; /* Length of run in buffer */
|
||||
} field_data[UF_MAX];
|
||||
};
|
||||
|
||||
|
||||
/* Returns the library version. Bits 16-23 contain the major version number,
|
||||
* bits 8-15 the minor version number and bits 0-7 the patch level.
|
||||
* Usage example:
|
||||
*
|
||||
* unsigned long version = http_parser_version();
|
||||
* unsigned major = (version >> 16) & 255;
|
||||
* unsigned minor = (version >> 8) & 255;
|
||||
* unsigned patch = version & 255;
|
||||
* printf("http_parser v%u.%u.%u\n", major, minor, patch);
|
||||
*/
|
||||
unsigned long http_parser_version(void);
|
||||
|
||||
void http_parser_init(http_parser *parser, enum http_parser_type type);
|
||||
|
||||
|
||||
/* Initialize http_parser_settings members to 0
|
||||
*/
|
||||
void http_parser_settings_init(http_parser_settings *settings);
|
||||
|
||||
|
||||
/* Executes the parser. Returns number of parsed bytes. Sets
|
||||
* `parser->http_errno` on error. */
|
||||
size_t http_parser_execute(http_parser *parser,
|
||||
const http_parser_settings *settings,
|
||||
const char *data,
|
||||
size_t len);
|
||||
|
||||
|
||||
/* If http_should_keep_alive() in the on_headers_complete or
|
||||
* on_message_complete callback returns 0, then this should be
|
||||
* the last message on the connection.
|
||||
* If you are the server, respond with the "Connection: close" header.
|
||||
* If you are the client, close the connection.
|
||||
*/
|
||||
int http_should_keep_alive(const http_parser *parser);
|
||||
|
||||
/* Returns a string version of the HTTP method. */
|
||||
const char *http_method_str(enum http_method m);
|
||||
|
||||
/* Return a string name of the given error */
|
||||
const char *http_errno_name(enum http_errno err);
|
||||
|
||||
/* Return a string description of the given error */
|
||||
const char *http_errno_description(enum http_errno err);
|
||||
|
||||
/* Initialize all http_parser_url members to 0 */
|
||||
void http_parser_url_init(struct http_parser_url *u);
|
||||
|
||||
/* Parse a URL; return nonzero on failure */
|
||||
int http_parser_parse_url(const char *buf, size_t buflen,
|
||||
int is_connect,
|
||||
struct http_parser_url *u);
|
||||
|
||||
/* Pause or un-pause the parser; a nonzero value pauses */
|
||||
void http_parser_pause(http_parser *parser, int paused);
|
||||
|
||||
/* Checks if this is the final chunk of the body. */
|
||||
int http_body_is_final(const http_parser *parser);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
//
|
||||
// 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/boostorg/beast
|
||||
//
|
||||
|
||||
#if defined(__GNUC__) && (__GNUC__ >= 7)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
|
||||
#endif
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#ifdef BOOST_MSVC
|
||||
# pragma warning (push)
|
||||
# pragma warning (disable: 4127) // conditional expression is constant
|
||||
# pragma warning (disable: 4244) // integer conversion, possible loss of data
|
||||
#endif
|
||||
|
||||
#include "nodejs-parser/http_parser.c"
|
||||
|
||||
#ifdef BOOST_MSVC
|
||||
# pragma warning (pop)
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__) && (__GNUC__ >= 7)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
@@ -0,0 +1,642 @@
|
||||
//
|
||||
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
//
|
||||
// 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/boostorg/beast
|
||||
//
|
||||
|
||||
#ifndef BOOST_BEAST_HTTP_NODEJS_PARSER_HPP
|
||||
#define BOOST_BEAST_HTTP_NODEJS_PARSER_HPP
|
||||
|
||||
#include "nodejs-parser/http_parser.h"
|
||||
|
||||
#include <boost/beast/http/message.hpp>
|
||||
#include <boost/beast/http/rfc7230.hpp>
|
||||
#include <boost/beast/core/error.hpp>
|
||||
#include <boost/beast/core/type_traits.hpp>
|
||||
#include <boost/asio/buffer.hpp>
|
||||
#include <boost/system/error_code.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
namespace detail {
|
||||
|
||||
class nodejs_message_category
|
||||
: public boost::system::error_category
|
||||
{
|
||||
public:
|
||||
const char*
|
||||
name() const noexcept override
|
||||
{
|
||||
return "nodejs-http-error";
|
||||
}
|
||||
|
||||
std::string
|
||||
message(int ev) const override
|
||||
{
|
||||
return http_errno_description(
|
||||
static_cast<http_errno>(ev));
|
||||
}
|
||||
|
||||
boost::system::error_condition
|
||||
default_error_condition(int ev) const noexcept override
|
||||
{
|
||||
return boost::system::error_condition{ev, *this};
|
||||
}
|
||||
|
||||
bool
|
||||
equivalent(int ev,
|
||||
boost::system::error_condition const& condition
|
||||
) const noexcept override
|
||||
{
|
||||
return condition.value() == ev &&
|
||||
&condition.category() == this;
|
||||
}
|
||||
|
||||
bool
|
||||
equivalent(boost::system::error_code const& error,
|
||||
int ev) const noexcept override
|
||||
{
|
||||
return error.value() == ev &&
|
||||
&error.category() == this;
|
||||
}
|
||||
};
|
||||
|
||||
template<class = void>
|
||||
boost::system::error_code
|
||||
make_nodejs_error(int http_errno)
|
||||
{
|
||||
static nodejs_message_category const mc{};
|
||||
return boost::system::error_code{http_errno, mc};
|
||||
}
|
||||
|
||||
inline
|
||||
char const*
|
||||
method_to_string(unsigned method)
|
||||
{
|
||||
using namespace beast;
|
||||
switch(static_cast<http_method>(method))
|
||||
{
|
||||
case HTTP_DELETE: return "DELETE";
|
||||
case HTTP_GET: return "GET";
|
||||
case HTTP_HEAD: return "HEAD";
|
||||
case HTTP_POST: return "POST";
|
||||
case HTTP_PUT: return "PUT";
|
||||
|
||||
// pathological
|
||||
case HTTP_CONNECT: return "CONNECT";
|
||||
case HTTP_OPTIONS: return "OPTIONS";
|
||||
case HTTP_TRACE: return "TRACE";
|
||||
|
||||
// webdav
|
||||
case HTTP_COPY: return "COPY";
|
||||
case HTTP_LOCK: return "LOCK";
|
||||
case HTTP_MKCOL: return "MKCOL";
|
||||
case HTTP_MOVE: return "MOVE";
|
||||
case HTTP_PROPFIND: return "PROPFIND";
|
||||
case HTTP_PROPPATCH: return "PROPPATCH";
|
||||
case HTTP_SEARCH: return "SEARCH";
|
||||
case HTTP_UNLOCK: return "UNLOCK";
|
||||
case HTTP_BIND: return "BIND";
|
||||
case HTTP_REBIND: return "REBIND";
|
||||
case HTTP_UNBIND: return "UNBIND";
|
||||
case HTTP_ACL: return "ACL";
|
||||
|
||||
// subversion
|
||||
case HTTP_REPORT: return "REPORT";
|
||||
case HTTP_MKACTIVITY: return "MKACTIVITY";
|
||||
case HTTP_CHECKOUT: return "CHECKOUT";
|
||||
case HTTP_MERGE: return "MERGE";
|
||||
|
||||
// upnp
|
||||
case HTTP_MSEARCH: return "MSEARCH";
|
||||
case HTTP_NOTIFY: return "NOTIFY";
|
||||
case HTTP_SUBSCRIBE: return "SUBSCRIBE";
|
||||
case HTTP_UNSUBSCRIBE: return "UNSUBSCRIBE";
|
||||
|
||||
// RFC-5789
|
||||
case HTTP_PATCH: return "PATCH";
|
||||
case HTTP_PURGE: return "PURGE";
|
||||
|
||||
// CalDav
|
||||
case HTTP_MKCALENDAR: return "MKCALENDAR";
|
||||
|
||||
// RFC-2068, section 19.6.1.2
|
||||
case HTTP_LINK: return "LINK";
|
||||
case HTTP_UNLINK: return "UNLINK";
|
||||
};
|
||||
|
||||
return "<unknown>";
|
||||
}
|
||||
|
||||
} // detail
|
||||
|
||||
template<class Derived>
|
||||
class nodejs_basic_parser
|
||||
{
|
||||
http_parser state_;
|
||||
boost::system::error_code* ec_;
|
||||
bool complete_ = false;
|
||||
std::string url_;
|
||||
std::string status_;
|
||||
std::string field_;
|
||||
std::string value_;
|
||||
|
||||
public:
|
||||
using error_code = boost::system::error_code;
|
||||
|
||||
nodejs_basic_parser(nodejs_basic_parser&& other);
|
||||
|
||||
nodejs_basic_parser&
|
||||
operator=(nodejs_basic_parser&& other);
|
||||
|
||||
nodejs_basic_parser(nodejs_basic_parser const& other);
|
||||
|
||||
nodejs_basic_parser& operator=(nodejs_basic_parser const& other);
|
||||
|
||||
explicit
|
||||
nodejs_basic_parser(bool request) noexcept;
|
||||
|
||||
bool
|
||||
complete() const noexcept
|
||||
{
|
||||
return complete_;
|
||||
}
|
||||
|
||||
std::size_t
|
||||
write(void const* data, std::size_t size)
|
||||
{
|
||||
error_code ec;
|
||||
auto const used = write(data, size, ec);
|
||||
if(ec)
|
||||
BOOST_THROW_EXCEPTION(system_error{ec});
|
||||
return used;
|
||||
}
|
||||
|
||||
std::size_t
|
||||
write(void const* data, std::size_t size,
|
||||
error_code& ec);
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
std::size_t
|
||||
write(ConstBufferSequence const& buffers)
|
||||
{
|
||||
error_code ec;
|
||||
auto const used = write(buffers, ec);
|
||||
if(ec)
|
||||
BOOST_THROW_EXCEPTION(system_error{ec});
|
||||
return used;
|
||||
}
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
std::size_t
|
||||
write(ConstBufferSequence const& buffers,
|
||||
error_code& ec);
|
||||
|
||||
void
|
||||
write_eof()
|
||||
{
|
||||
error_code ec;
|
||||
write_eof(ec);
|
||||
if(ec)
|
||||
BOOST_THROW_EXCEPTION(system_error{ec});
|
||||
}
|
||||
|
||||
void
|
||||
write_eof(error_code& ec);
|
||||
|
||||
void
|
||||
check_header();
|
||||
|
||||
private:
|
||||
Derived&
|
||||
impl()
|
||||
{
|
||||
return *static_cast<Derived*>(this);
|
||||
}
|
||||
|
||||
static int cb_message_start(http_parser*);
|
||||
static int cb_url(http_parser*, char const*, std::size_t);
|
||||
static int cb_status(http_parser*, char const*, std::size_t);
|
||||
static int cb_header_field(http_parser*, char const*, std::size_t);
|
||||
static int cb_header_value(http_parser*, char const*, std::size_t);
|
||||
static int cb_headers_complete(http_parser*);
|
||||
static int cb_body(http_parser*, char const*, std::size_t);
|
||||
static int cb_message_complete(http_parser*);
|
||||
static int cb_chunk_header(http_parser*);
|
||||
static int cb_chunk_complete(http_parser*);
|
||||
|
||||
struct hooks_t : http_parser_settings
|
||||
{
|
||||
hooks_t()
|
||||
{
|
||||
http_parser_settings_init(this);
|
||||
on_message_begin = &nodejs_basic_parser::cb_message_start;
|
||||
on_url = &nodejs_basic_parser::cb_url;
|
||||
on_status = &nodejs_basic_parser::cb_status;
|
||||
on_header_field = &nodejs_basic_parser::cb_header_field;
|
||||
on_header_value = &nodejs_basic_parser::cb_header_value;
|
||||
on_headers_complete = &nodejs_basic_parser::cb_headers_complete;
|
||||
on_body = &nodejs_basic_parser::cb_body;
|
||||
on_message_complete = &nodejs_basic_parser::cb_message_complete;
|
||||
on_chunk_header = &nodejs_basic_parser::cb_chunk_header;
|
||||
on_chunk_complete = &nodejs_basic_parser::cb_chunk_complete;
|
||||
}
|
||||
};
|
||||
|
||||
static
|
||||
http_parser_settings const*
|
||||
hooks();
|
||||
};
|
||||
|
||||
template<class Derived>
|
||||
template<class ConstBufferSequence>
|
||||
std::size_t
|
||||
nodejs_basic_parser<Derived>::write(
|
||||
ConstBufferSequence const& buffers, error_code& ec)
|
||||
{
|
||||
static_assert(beast::is_const_buffer_sequence<
|
||||
ConstBufferSequence>::value,
|
||||
"ConstBufferSequence requirements not met");
|
||||
using boost::asio::buffer_cast;
|
||||
using boost::asio::buffer_size;
|
||||
std::size_t bytes_used = 0;
|
||||
for(boost::asio::const_buffer buffer : buffers)
|
||||
{
|
||||
auto const n = write(
|
||||
buffer_cast<void const*>(buffer),
|
||||
buffer_size(buffer), ec);
|
||||
if(ec)
|
||||
return 0;
|
||||
bytes_used += n;
|
||||
if(complete())
|
||||
break;
|
||||
}
|
||||
return bytes_used;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
http_parser_settings const*
|
||||
nodejs_basic_parser<Derived>::hooks()
|
||||
{
|
||||
static hooks_t const h;
|
||||
return &h;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
nodejs_basic_parser<Derived>::
|
||||
nodejs_basic_parser(nodejs_basic_parser&& other)
|
||||
{
|
||||
state_ = other.state_;
|
||||
state_.data = this;
|
||||
complete_ = other.complete_;
|
||||
url_ = std::move(other.url_);
|
||||
status_ = std::move(other.status_);
|
||||
field_ = std::move(other.field_);
|
||||
value_ = std::move(other.value_);
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
auto
|
||||
nodejs_basic_parser<Derived>::
|
||||
operator=(nodejs_basic_parser&& other) ->
|
||||
nodejs_basic_parser&
|
||||
{
|
||||
state_ = other.state_;
|
||||
state_.data = this;
|
||||
complete_ = other.complete_;
|
||||
url_ = std::move(other.url_);
|
||||
status_ = std::move(other.status_);
|
||||
field_ = std::move(other.field_);
|
||||
value_ = std::move(other.value_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
nodejs_basic_parser<Derived>::
|
||||
nodejs_basic_parser(nodejs_basic_parser const& other)
|
||||
{
|
||||
state_ = other.state_;
|
||||
state_.data = this;
|
||||
complete_ = other.complete_;
|
||||
url_ = other.url_;
|
||||
status_ = other.status_;
|
||||
field_ = other.field_;
|
||||
value_ = other.value_;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
auto
|
||||
nodejs_basic_parser<Derived>::
|
||||
operator=(nodejs_basic_parser const& other) ->
|
||||
nodejs_basic_parser&
|
||||
{
|
||||
state_ = other.state_;
|
||||
state_.data = this;
|
||||
complete_ = other.complete_;
|
||||
url_ = other.url_;
|
||||
status_ = other.status_;
|
||||
field_ = other.field_;
|
||||
value_ = other.value_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
nodejs_basic_parser<Derived>::
|
||||
nodejs_basic_parser(bool request) noexcept
|
||||
{
|
||||
state_.data = this;
|
||||
http_parser_init(&state_, request
|
||||
? http_parser_type::HTTP_REQUEST
|
||||
: http_parser_type::HTTP_RESPONSE);
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
std::size_t
|
||||
nodejs_basic_parser<Derived>::
|
||||
write(void const* data,
|
||||
std::size_t size, error_code& ec)
|
||||
{
|
||||
ec_ = &ec;
|
||||
auto const n = http_parser_execute(
|
||||
&state_, hooks(),
|
||||
static_cast<const char*>(data), size);
|
||||
if(! ec)
|
||||
ec = detail::make_nodejs_error(
|
||||
static_cast<int>(state_.http_errno));
|
||||
if(ec)
|
||||
return 0;
|
||||
return n;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
void
|
||||
nodejs_basic_parser<Derived>::
|
||||
write_eof(error_code& ec)
|
||||
{
|
||||
ec_ = &ec;
|
||||
http_parser_execute(&state_, hooks(), nullptr, 0);
|
||||
if(! ec)
|
||||
ec = detail::make_nodejs_error(
|
||||
static_cast<int>(state_.http_errno));
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
void
|
||||
nodejs_basic_parser<Derived>::
|
||||
check_header()
|
||||
{
|
||||
if(! value_.empty())
|
||||
{
|
||||
impl().on_field(field_, value_);
|
||||
field_.clear();
|
||||
value_.clear();
|
||||
}
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::
|
||||
cb_message_start(http_parser* p)
|
||||
{
|
||||
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
||||
t.complete_ = false;
|
||||
t.url_.clear();
|
||||
t.status_.clear();
|
||||
t.field_.clear();
|
||||
t.value_.clear();
|
||||
t.impl().on_start();
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::
|
||||
cb_url(http_parser* p,
|
||||
char const* in, std::size_t bytes)
|
||||
{
|
||||
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
||||
t.url_.append(in, bytes);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::
|
||||
cb_status(http_parser* p,
|
||||
char const* in, std::size_t bytes)
|
||||
{
|
||||
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
||||
t.status_.append(in, bytes);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::
|
||||
cb_header_field(http_parser* p,
|
||||
char const* in, std::size_t bytes)
|
||||
{
|
||||
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
||||
t.check_header();
|
||||
t.field_.append(in, bytes);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::
|
||||
cb_header_value(http_parser* p,
|
||||
char const* in, std::size_t bytes)
|
||||
{
|
||||
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
||||
t.value_.append(in, bytes);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::
|
||||
cb_headers_complete(http_parser* p)
|
||||
{
|
||||
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
||||
t.check_header();
|
||||
t.impl().on_headers_complete(*t.ec_);
|
||||
if(*t.ec_)
|
||||
return 1;
|
||||
bool const keep_alive =
|
||||
http_should_keep_alive(p) != 0;
|
||||
if(p->type == http_parser_type::HTTP_REQUEST)
|
||||
{
|
||||
t.impl().on_request(p->method, t.url_,
|
||||
p->http_major, p->http_minor, keep_alive,
|
||||
p->upgrade);
|
||||
return 0;
|
||||
}
|
||||
return t.impl().on_response(p->status_code, t.status_,
|
||||
p->http_major, p->http_minor, keep_alive,
|
||||
p->upgrade) ? 0 : 1;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::
|
||||
cb_body(http_parser* p,
|
||||
char const* in, std::size_t bytes)
|
||||
{
|
||||
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
||||
t.impl().on_body(in, bytes, *t.ec_);
|
||||
return *t.ec_ ? 1 : 0;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::
|
||||
cb_message_complete(http_parser* p)
|
||||
{
|
||||
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
||||
t.complete_ = true;
|
||||
t.impl().on_complete();
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::
|
||||
cb_chunk_header(http_parser*)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::
|
||||
cb_chunk_complete(http_parser*)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/** A HTTP parser.
|
||||
|
||||
The parser may only be used once.
|
||||
*/
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
class nodejs_parser
|
||||
: public nodejs_basic_parser<nodejs_parser<isRequest, Body, Fields>>
|
||||
{
|
||||
bool started_ = false;
|
||||
|
||||
public:
|
||||
nodejs_parser(nodejs_parser&&) = default;
|
||||
|
||||
nodejs_parser()
|
||||
: http::nodejs_basic_parser<nodejs_parser>(isRequest)
|
||||
{
|
||||
}
|
||||
|
||||
/// Returns `true` if at least one byte has been processed
|
||||
bool
|
||||
started()
|
||||
{
|
||||
return started_;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class http::nodejs_basic_parser<nodejs_parser>;
|
||||
|
||||
void
|
||||
on_start()
|
||||
{
|
||||
started_ = true;
|
||||
}
|
||||
|
||||
void
|
||||
on_field(std::string const&, std::string const&)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
on_headers_complete(error_code& ec)
|
||||
{
|
||||
// vFALCO TODO Decode the Content-Length and
|
||||
// Transfer-Encoding, see if we can reserve the buffer.
|
||||
//
|
||||
// r_.reserve(content_length)
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
bool
|
||||
on_request(unsigned, std::string const&,
|
||||
int, int, bool, bool, std::true_type)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
on_request(unsigned, std::string const&,
|
||||
int, int, bool, bool, std::false_type)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
on_request(unsigned method, std::string const& url,
|
||||
int major, int minor, bool keep_alive, bool upgrade)
|
||||
{
|
||||
return on_request(method, url,
|
||||
major, minor, keep_alive, upgrade,
|
||||
std::integral_constant<
|
||||
bool, isRequest>{});
|
||||
}
|
||||
|
||||
bool
|
||||
on_response(int, std::string const&,
|
||||
int, int, bool, bool, std::true_type)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
on_response(int, std::string const&, int, int, bool, bool,
|
||||
std::false_type)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
on_response(int status, std::string const& reason,
|
||||
int major, int minor, bool keep_alive, bool upgrade)
|
||||
{
|
||||
return on_response(
|
||||
status, reason, major, minor, keep_alive, upgrade,
|
||||
std::integral_constant<bool, ! isRequest>{});
|
||||
}
|
||||
|
||||
void
|
||||
on_body(void const*, std::size_t, error_code& ec)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
void
|
||||
on_complete()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
#
|
||||
# Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
#
|
||||
# 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/boostorg/beast
|
||||
#
|
||||
|
||||
GroupSources(extras/boost/beast extras)
|
||||
GroupSources(include/boost/beast beast)
|
||||
GroupSources(test/bench/utf8_checker "/")
|
||||
|
||||
add_executable (bench-utf8-checker
|
||||
${BOOST_BEAST_INCLUDES}
|
||||
${EXTRAS_INCLUDES}
|
||||
${TEST_MAIN}
|
||||
Jamfile
|
||||
bench_utf8_checker.cpp
|
||||
)
|
||||
@@ -0,0 +1,19 @@
|
||||
#
|
||||
# Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
#
|
||||
# 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/boostorg/beast
|
||||
#
|
||||
|
||||
exe bench-utf8-checker :
|
||||
$(TEST_MAIN)
|
||||
bench_utf8_checker.cpp
|
||||
;
|
||||
|
||||
explicit bench-utf8-checker ;
|
||||
|
||||
alias run-tests :
|
||||
[ compile bench_utf8_checker.cpp ]
|
||||
;
|
||||
@@ -0,0 +1,157 @@
|
||||
//
|
||||
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
//
|
||||
// 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/boostorg/beast
|
||||
//
|
||||
|
||||
#include <boost/beast/websocket/detail/utf8_checker.hpp>
|
||||
#include <boost/beast/unit_test/suite.hpp>
|
||||
#include <chrono>
|
||||
#include <random>
|
||||
|
||||
#ifndef BEAST_USE_BOOST_LOCALE_BENCHMARK
|
||||
#define BEAST_USE_BOOST_LOCALE_BENCHMARK 0
|
||||
#endif
|
||||
|
||||
#if BEAST_USE_BOOST_LOCALE_BENCHMARK
|
||||
#include <boost/locale.hpp>
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
|
||||
class utf8_checker_test : public beast::unit_test::suite
|
||||
{
|
||||
std::mt19937 rng_;
|
||||
|
||||
public:
|
||||
using size_type = std::uint64_t;
|
||||
|
||||
class timer
|
||||
{
|
||||
public:
|
||||
using clock_type =
|
||||
std::chrono::system_clock;
|
||||
|
||||
private:
|
||||
clock_type::time_point when_;
|
||||
|
||||
public:
|
||||
using duration =
|
||||
clock_type::duration;
|
||||
|
||||
timer()
|
||||
: when_(clock_type::now())
|
||||
{
|
||||
}
|
||||
|
||||
duration
|
||||
elapsed() const
|
||||
{
|
||||
return clock_type::now() - when_;
|
||||
}
|
||||
};
|
||||
|
||||
template<class UInt = std::size_t>
|
||||
UInt
|
||||
rand(std::size_t n)
|
||||
{
|
||||
return static_cast<UInt>(
|
||||
std::uniform_int_distribution<
|
||||
std::size_t>{0, n-1}(rng_));
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
size_type
|
||||
throughput(std::chrono::duration<
|
||||
double> const& elapsed, size_type items)
|
||||
{
|
||||
using namespace std::chrono;
|
||||
return static_cast<size_type>(
|
||||
1 / (elapsed/items).count());
|
||||
}
|
||||
|
||||
std::string
|
||||
corpus(std::size_t n)
|
||||
{
|
||||
std::string s;
|
||||
s.reserve(n);
|
||||
while(n--)
|
||||
s.push_back(static_cast<char>(
|
||||
' ' + rand(95)));
|
||||
return s;
|
||||
}
|
||||
|
||||
void
|
||||
checkBeast(std::string const& s)
|
||||
{
|
||||
beast::websocket::detail::check_utf8(
|
||||
s.data(), s.size());
|
||||
}
|
||||
|
||||
#if BEAST_USE_BOOST_LOCALE_BENCHMARK
|
||||
void
|
||||
checkLocale(std::string const& s)
|
||||
{
|
||||
using namespace boost::locale;
|
||||
auto p = s.begin();
|
||||
auto const e = s.end();
|
||||
while(p != e)
|
||||
{
|
||||
auto cp = utf::utf_traits<char>::decode(p, e);
|
||||
if(cp == utf::illegal)
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
template<class F>
|
||||
typename timer::clock_type::duration
|
||||
test(F const& f)
|
||||
{
|
||||
timer t;
|
||||
f();
|
||||
return t.elapsed();
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
auto const s = corpus(32 * 1024 * 1024);
|
||||
for(int i = 0; i < 5; ++ i)
|
||||
{
|
||||
auto const elapsed = test([&]{
|
||||
checkBeast(s);
|
||||
checkBeast(s);
|
||||
checkBeast(s);
|
||||
checkBeast(s);
|
||||
checkBeast(s);
|
||||
});
|
||||
log << "beast: " << throughput(elapsed, s.size()) << " char/s" << std::endl;
|
||||
}
|
||||
#if BEAST_USE_BOOST_LOCALE_BENCHMARK
|
||||
for(int i = 0; i < 5; ++ i)
|
||||
{
|
||||
auto const elapsed = test([&]{
|
||||
checkLocale(s);
|
||||
checkLocale(s);
|
||||
checkLocale(s);
|
||||
checkLocale(s);
|
||||
checkLocale(s);
|
||||
});
|
||||
log << "locale: " << throughput(elapsed, s.size()) << " char/s" << std::endl;
|
||||
}
|
||||
#endif
|
||||
pass();
|
||||
}
|
||||
};
|
||||
|
||||
BOOST_BEAST_DEFINE_TESTSUITE(utf8_checker,benchmarks,beast);
|
||||
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
#
|
||||
# Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
#
|
||||
# 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/boostorg/beast
|
||||
#
|
||||
|
||||
GroupSources(include/boost/beast beast)
|
||||
GroupSources(example/common common)
|
||||
GroupSources(extras/boost/beast extras)
|
||||
GroupSources(test/bench/wsload "/")
|
||||
|
||||
add_executable (bench-wsload
|
||||
${BOOST_BEAST_INCLUDES}
|
||||
${COMMON_INCLUDES}
|
||||
${EXTRAS_INCLUDES}
|
||||
Jamfile
|
||||
wsload_main.cpp
|
||||
)
|
||||
@@ -0,0 +1,19 @@
|
||||
#
|
||||
# Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
#
|
||||
# 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/boostorg/beast
|
||||
#
|
||||
|
||||
exe wsload :
|
||||
$(TEST_MAIN)
|
||||
wsload_main.cpp
|
||||
;
|
||||
|
||||
explicit wsload ;
|
||||
|
||||
alias run-tests :
|
||||
[ compile wsload_main.cpp ]
|
||||
;
|
||||
@@ -0,0 +1,344 @@
|
||||
//
|
||||
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
//
|
||||
// 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/boostorg/beast
|
||||
//
|
||||
|
||||
#include <example/common/helpers.hpp>
|
||||
#include <example/common/session_alloc.hpp>
|
||||
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/websocket.hpp>
|
||||
#include <boost/beast/unit_test/dstream.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <random>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace asio = boost::asio;
|
||||
namespace ip = boost::asio::ip;
|
||||
using tcp = boost::asio::ip::tcp;
|
||||
namespace ws = boost::beast::websocket;
|
||||
namespace ph = std::placeholders;
|
||||
using error_code = boost::beast::error_code;
|
||||
|
||||
class test_buffer : public asio::const_buffers_1
|
||||
{
|
||||
char data_[4096];
|
||||
|
||||
public:
|
||||
test_buffer()
|
||||
: asio::const_buffers_1(data_, sizeof(data_))
|
||||
{
|
||||
std::mt19937_64 rng;
|
||||
std::uniform_int_distribution<unsigned short> dist;
|
||||
for(auto& c : data_)
|
||||
c = static_cast<unsigned char>(dist(rng));
|
||||
}
|
||||
};
|
||||
|
||||
class report
|
||||
{
|
||||
std::mutex m_;
|
||||
std::size_t bytes_ = 0;
|
||||
std::size_t messages_ = 0;
|
||||
|
||||
public:
|
||||
void
|
||||
insert(std::size_t messages, std::size_t bytes)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_);
|
||||
bytes_ += bytes;
|
||||
messages_ += messages;
|
||||
}
|
||||
|
||||
std::size_t
|
||||
bytes() const
|
||||
{
|
||||
return bytes_;
|
||||
}
|
||||
|
||||
std::size_t
|
||||
messages() const
|
||||
{
|
||||
return messages_;
|
||||
}
|
||||
};
|
||||
|
||||
class connection
|
||||
: public std::enable_shared_from_this<connection>
|
||||
{
|
||||
std::ostream& log_;
|
||||
ws::stream<tcp::socket> ws_;
|
||||
tcp::endpoint ep_;
|
||||
std::size_t messages_;
|
||||
report& rep_;
|
||||
test_buffer const& tb_;
|
||||
asio::io_service::strand strand_;
|
||||
boost::beast::multi_buffer buffer_;
|
||||
std::mt19937_64 rng_;
|
||||
std::size_t count_ = 0;
|
||||
std::size_t bytes_ = 0;
|
||||
session_alloc<char> alloc_;
|
||||
|
||||
public:
|
||||
connection(
|
||||
std::ostream& log,
|
||||
asio::io_service& ios,
|
||||
tcp::endpoint const& ep,
|
||||
std::size_t messages,
|
||||
bool deflate,
|
||||
report& rep,
|
||||
test_buffer const& tb)
|
||||
: log_(log)
|
||||
, ws_(ios)
|
||||
, ep_(ep)
|
||||
, messages_(messages)
|
||||
, rep_(rep)
|
||||
, tb_(tb)
|
||||
, strand_(ios)
|
||||
{
|
||||
ws::permessage_deflate pmd;
|
||||
pmd.client_enable = deflate;
|
||||
ws_.set_option(pmd);
|
||||
ws_.binary(true);
|
||||
ws_.auto_fragment(false);
|
||||
ws_.write_buffer_size(64 * 1024);
|
||||
}
|
||||
|
||||
~connection()
|
||||
{
|
||||
rep_.insert(count_, bytes_);
|
||||
}
|
||||
|
||||
void
|
||||
run()
|
||||
{
|
||||
ws_.next_layer().async_connect(ep_,
|
||||
alloc_.wrap(std::bind(
|
||||
&connection::on_connect,
|
||||
shared_from_this(),
|
||||
ph::_1)));
|
||||
}
|
||||
|
||||
private:
|
||||
void
|
||||
fail(boost::beast::string_view what, error_code ec)
|
||||
{
|
||||
if( ec == asio::error::operation_aborted ||
|
||||
ec == ws::error::closed)
|
||||
return;
|
||||
print(log_, "[", ep_, "] ", what, ": ", ec.message());
|
||||
}
|
||||
|
||||
void
|
||||
on_connect(error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
return fail("on_connect", ec);
|
||||
ws_.async_handshake(
|
||||
ep_.address().to_string() + ":" + std::to_string(ep_.port()),
|
||||
"/",
|
||||
alloc_.wrap(std::bind(
|
||||
&connection::on_handshake,
|
||||
shared_from_this(),
|
||||
ph::_1)));
|
||||
}
|
||||
|
||||
void
|
||||
on_handshake(error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
return fail("on_connect", ec);
|
||||
do_write();
|
||||
}
|
||||
|
||||
void
|
||||
do_write()
|
||||
{
|
||||
std::geometric_distribution<std::size_t> dist{
|
||||
double(4) / boost::asio::buffer_size(tb_)};
|
||||
ws_.async_write_some(true,
|
||||
boost::beast::buffer_prefix(dist(rng_), tb_),
|
||||
alloc_.wrap(std::bind(
|
||||
&connection::on_write,
|
||||
shared_from_this(),
|
||||
ph::_1)));
|
||||
}
|
||||
|
||||
void
|
||||
on_write(error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
return fail("on_read", ec);
|
||||
if(messages_--)
|
||||
return do_read();
|
||||
ws_.async_close({},
|
||||
alloc_.wrap(std::bind(
|
||||
&connection::on_close,
|
||||
shared_from_this(),
|
||||
ph::_1)));
|
||||
}
|
||||
|
||||
void
|
||||
do_read()
|
||||
{
|
||||
ws_.async_read(buffer_,
|
||||
alloc_.wrap(std::bind(
|
||||
&connection::on_read,
|
||||
shared_from_this(),
|
||||
ph::_1)));
|
||||
}
|
||||
|
||||
void
|
||||
on_read(error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
return fail("on_read", ec);
|
||||
++count_;
|
||||
bytes_ += buffer_.size();
|
||||
buffer_.consume(buffer_.size());
|
||||
do_write();
|
||||
}
|
||||
|
||||
void
|
||||
on_close(error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
return fail("on_close", ec);
|
||||
do_drain();
|
||||
}
|
||||
|
||||
void
|
||||
do_drain()
|
||||
{
|
||||
ws_.async_read(buffer_,
|
||||
alloc_.wrap(std::bind(
|
||||
&connection::on_drain,
|
||||
shared_from_this(),
|
||||
ph::_1)));
|
||||
}
|
||||
|
||||
void
|
||||
on_drain(error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
return fail("on_drain", ec);
|
||||
do_drain();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class timer
|
||||
{
|
||||
using clock_type =
|
||||
std::chrono::system_clock;
|
||||
|
||||
clock_type::time_point when_;
|
||||
|
||||
public:
|
||||
using duration =
|
||||
clock_type::duration;
|
||||
|
||||
timer()
|
||||
: when_(clock_type::now())
|
||||
{
|
||||
}
|
||||
|
||||
duration
|
||||
elapsed() const
|
||||
{
|
||||
return clock_type::now() - when_;
|
||||
}
|
||||
};
|
||||
|
||||
inline
|
||||
std::uint64_t
|
||||
throughput(
|
||||
std::chrono::duration<double> const& elapsed,
|
||||
std::uint64_t items)
|
||||
{
|
||||
using namespace std::chrono;
|
||||
return static_cast<std::uint64_t>(
|
||||
1 / (elapsed/items).count());
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char** argv)
|
||||
{
|
||||
boost::beast::unit_test::dstream dout(std::cerr);
|
||||
|
||||
try
|
||||
{
|
||||
// Check command line arguments.
|
||||
if(argc != 8)
|
||||
{
|
||||
std::cerr <<
|
||||
"Usage: " << argv[0] <<
|
||||
" <address> <port> <trials> <messages> <workers> <threads> <compression:0|1>";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
auto const address = ip::address::from_string(argv[1]);
|
||||
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
|
||||
auto const trials = static_cast<std::size_t>(std::atoi(argv[3]));
|
||||
auto const messages= static_cast<std::size_t>(std::atoi(argv[4]));
|
||||
auto const workers = static_cast<std::size_t>(std::atoi(argv[5]));
|
||||
auto const threads = static_cast<std::size_t>(std::atoi(argv[6]));
|
||||
auto const deflate = static_cast<bool>(std::atoi(argv[7]));
|
||||
auto const work = (messages + workers - 1) / workers;
|
||||
test_buffer tb;
|
||||
for(auto i = trials; i; --i)
|
||||
{
|
||||
report rep;
|
||||
boost::asio::io_service ios{1};
|
||||
for(auto j = workers; j; --j)
|
||||
{
|
||||
auto sp =
|
||||
std::make_shared<connection>(
|
||||
dout,
|
||||
ios,
|
||||
tcp::endpoint{address, port},
|
||||
work,
|
||||
deflate,
|
||||
rep,
|
||||
tb);
|
||||
sp->run();
|
||||
}
|
||||
timer clock;
|
||||
std::vector<std::thread> tv;
|
||||
if(threads > 1)
|
||||
{
|
||||
tv.reserve(threads);
|
||||
tv.emplace_back([&ios]{ ios.run(); });
|
||||
}
|
||||
ios.run();
|
||||
for(auto& t : tv)
|
||||
t.join();
|
||||
auto const elapsed = clock.elapsed();
|
||||
dout <<
|
||||
throughput(elapsed, rep.bytes()) << " bytes/s in " <<
|
||||
(std::chrono::duration_cast<
|
||||
std::chrono::milliseconds>(
|
||||
elapsed).count() / 1000.) << "ms and " <<
|
||||
rep.bytes() << " bytes" << std::endl;
|
||||
}
|
||||
}
|
||||
catch(std::exception const& e)
|
||||
{
|
||||
std::cerr << "Exception: " << e.what() << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user