Compare commits

...

8 Commits

Author SHA1 Message Date
bugdea1er 3e9eb1172a Define GET_IO_SERVICE once 2024-04-19 17:06:13 +02:00
bugdea1er e3ff9238a4 Add option to use Boost.Asio 2024-04-19 17:06:13 +02:00
ssams 96e049666e add HTTP status 406 2024-04-19 17:05:48 +02:00
ItsAlbertZhang ad337a8a86 fix: compilation error with libc++ 2024-03-19 16:59:39 +01:00
Tristan CADET d0f1991e38 fix: parse body when unsupported HTTP/2 upgrade is requested
Crow does not support HTTP/2 at the moment.
According to the RFC https://datatracker.ietf.org/doc/html/rfc7540#section-3.2
"A server that does not support HTTP/2 can respond to the request as though the Upgrade header field were absent"
2024-03-17 15:02:25 +01:00
Gulliver da570e1cbd added pip install for mike 2024-03-12 21:52:15 +01:00
sina-rostami 106aeb0f0d Add indentation to json dump method
Closes #747
2024-03-10 21:41:43 +01:00
Corentin Schreiber 049490c2c9
Add configurable exception handler (#637)
* Added exception_handler()

* Fixed worker crash if exception thrown in catch-all handler
2024-03-10 13:52:13 +01:00
15 changed files with 445 additions and 77 deletions

View File

@ -18,7 +18,7 @@ jobs:
- name: Prepare dependencies
run: sudo apt-get update && sudo apt-get -yq install libasio-dev doxygen mkdocs graphviz zlib1g-dev gcc clang make cmake python3 python3-pip git openssl libssl-dev
- name: prepate pip dependencies
run: pip3 install mkdocs-material mkdocs-redirects pyyaml mkdocs-meta-descriptions-plugin --no-input
run: pip3 install mkdocs-material mkdocs-redirects pyyaml mkdocs-meta-descriptions-plugin mike --no-input
- name: configure
run: cmake -B build -DCROW_AMALGAMATE=ON
- name: clean generated docs dir

View File

@ -46,6 +46,7 @@ option(CROW_BUILD_EXAMPLES "Build the examples in the project" ${CROW_I
option(CROW_BUILD_TESTS "Build the tests in the project" ${CROW_IS_MAIN_PROJECT})
option(CROW_AMALGAMATE "Combine all headers into one" OFF)
option(CROW_INSTALL "Add install step for Crow" ON )
option(CROW_USE_BOOST "Use Boost.Asio for Crow" OFF)
# Possible values: ssl, compression
option(CROW_FEATURES "Enable features extending Crow's abilities" "")
@ -62,12 +63,20 @@ target_include_directories(Crow
$<INSTALL_INTERFACE:include>
)
find_package(asio REQUIRED)
target_link_libraries(Crow
INTERFACE
asio::asio
)
if(CROW_USE_BOOST)
find_package(Boost 1.64 COMPONENTS system date_time REQUIRED)
target_link_libraries(Crow
INTERFACE
Boost::boost Boost::system Boost::date_time
)
target_compile_definitions(Crow INTERFACE CROW_USE_BOOST)
else()
find_package(asio REQUIRED)
target_link_libraries(Crow
INTERFACE
asio::asio
)
endif()
target_compile_definitions(Crow INTERFACE "")

View File

@ -125,6 +125,7 @@ Crow defines the following status codes:
403 Forbidden
404 Not Found
405 Method Not Allowed
406 Not Acceptable
407 Proxy Authentication Required
409 Conflict
410 Gone

View File

@ -252,6 +252,24 @@ namespace crow
return *this;
}
/// Set the function to call to handle uncaught exceptions generated in routes (Default generates error 500).
///
/// The function must have the following signature: void(crow::response&).
/// It must set the response passed in argument to the function, which will be sent back to the client.
/// See Router::default_exception_handler() for the default implementation.
template<typename Func>
self_t& exception_handler(Func&& f)
{
router_.exception_handler() = std::forward<Func>(f);
return *this;
}
std::function<void(crow::response&)>& exception_handler()
{
return router_.exception_handler();
}
/// Set a custom duration and function to run on every tick
template<typename Duration, typename Func>
self_t& tick(Duration d, Func f)

View File

@ -190,6 +190,7 @@ namespace crow
FORBIDDEN = 403,
NOT_FOUND = 404,
METHOD_NOT_ALLOWED = 405,
NOT_ACCEPTABLE = 406,
PROXY_AUTHENTICATION_REQUIRED = 407,
CONFLICT = 409,
GONE = 410,

View File

@ -1,32 +1,43 @@
#pragma once
#ifdef CROW_USE_BOOST
#include <boost/asio.hpp>
#else
#ifndef ASIO_STANDALONE
#define ASIO_STANDALONE
#endif
#include <asio.hpp>
#endif
#include <algorithm>
#include <atomic>
#include <chrono>
#include <vector>
#include <memory>
#include <vector>
#include "crow/http_parser_merged.h"
#include "crow/common.h"
#include "crow/parser.h"
#include "crow/compression.h"
#include "crow/http_response.h"
#include "crow/logging.h"
#include "crow/settings.h"
#include "crow/task_timer.h"
#include "crow/middleware_context.h"
#include "crow/middleware.h"
#include "crow/middleware_context.h"
#include "crow/parser.h"
#include "crow/settings.h"
#include "crow/socket_adaptors.h"
#include "crow/compression.h"
#include "crow/task_timer.h"
#include "crow/utility.h"
namespace crow
{
#ifdef CROW_USE_BOOST
namespace asio = boost::asio;
using error_code = boost::system::error_code;
#else
using error_code = asio::error_code;
#endif
using tcp = asio::ip::tcp;
#ifdef CROW_ENABLE_DEBUG
static std::atomic<int> connectionCount;
#endif
@ -81,7 +92,7 @@ namespace crow
void start()
{
auto self = this->shared_from_this();
adaptor_.start([self](const asio::error_code& ec) {
adaptor_.start([self](const error_code& ec) {
if (!ec)
{
self->start_deadline();
@ -312,6 +323,7 @@ namespace crow
{status::FORBIDDEN, "HTTP/1.1 403 Forbidden\r\n"},
{status::NOT_FOUND, "HTTP/1.1 404 Not Found\r\n"},
{status::METHOD_NOT_ALLOWED, "HTTP/1.1 405 Method Not Allowed\r\n"},
{status::NOT_ACCEPTABLE, "HTTP/1.1 406 Not Acceptable\r\n"},
{status::PROXY_AUTHENTICATION_REQUIRED, "HTTP/1.1 407 Proxy Authentication Required\r\n"},
{status::CONFLICT, "HTTP/1.1 409 Conflict\r\n"},
{status::GONE, "HTTP/1.1 410 Gone\r\n"},
@ -473,7 +485,7 @@ namespace crow
auto self = this->shared_from_this();
adaptor_.socket().async_read_some(
asio::buffer(buffer_),
[self](const asio::error_code& ec, std::size_t bytes_transferred) {
[self](const error_code& ec, std::size_t bytes_transferred) {
bool error_while_reading = true;
if (!ec)
{
@ -516,7 +528,7 @@ namespace crow
auto self = this->shared_from_this();
asio::async_write(
adaptor_.socket(), buffers_,
[self](const asio::error_code& ec, std::size_t /*bytes_transferred*/) {
[self](const error_code& ec, std::size_t /*bytes_transferred*/) {
self->res.clear();
self->res_body_copy_.clear();
if (!self->continue_requested)
@ -547,7 +559,7 @@ namespace crow
inline void do_write_sync(std::vector<asio::const_buffer>& buffers)
{
asio::write(adaptor_.socket(), buffers, [&](asio::error_code ec, std::size_t) {
asio::write(adaptor_.socket(), buffers, [&](error_code ec, std::size_t) {
if (!ec)
{
return false;

View File

@ -1273,7 +1273,14 @@ reexecute:
switch (parser->header_state) {
case h_upgrade:
parser->flags |= F_UPGRADE;
// Crow does not support HTTP/2 at the moment.
// According to the RFC https://datatracker.ietf.org/doc/html/rfc7540#section-3.2
// "A server that does not support HTTP/2 can respond to the request as though the Upgrade header field were absent"
// => `F_UPGRADE` is not set if the header starts by "h2".
// This prevents the parser from skipping the request body.
if (ch != 'h' || p+1 == (data + len) || *(p+1) != '2') {
parser->flags |= F_UPGRADE;
}
parser->header_state = h_general;
break;

View File

@ -1,9 +1,13 @@
#pragma once
#ifdef CROW_USE_BOOST
#include <boost/asio.hpp>
#else
#ifndef ASIO_STANDALONE
#define ASIO_STANDALONE
#endif
#include <asio.hpp>
#endif
#include "crow/common.h"
#include "crow/ci_map.h"
@ -11,6 +15,10 @@
namespace crow
{
#ifdef CROW_USE_BOOST
namespace asio = boost::asio;
#endif
/// Find and return the value associated with the key. (returns an empty string if nothing is found)
template<typename T>
inline const std::string& get_header_value(const T& headers, const std::string& key)

View File

@ -1,6 +1,11 @@
#pragma once
#include <chrono>
#ifdef CROW_USE_BOOST
#include <boost/asio.hpp>
#ifdef CROW_ENABLE_SSL
#include <boost/asio/ssl.hpp>
#endif
#else
#ifndef ASIO_STANDALONE
#define ASIO_STANDALONE
#endif
@ -8,11 +13,14 @@
#ifdef CROW_ENABLE_SSL
#include <asio/ssl.hpp>
#endif
#include <cstdint>
#endif
#include <atomic>
#include <chrono>
#include <cstdint>
#include <future>
#include <vector>
#include <memory>
#include <vector>
#include "crow/version.h"
#include "crow/http_connection.h"
@ -21,6 +29,12 @@
namespace crow
{
#ifdef CROW_USE_BOOST
namespace asio = boost::asio;
using error_code = boost::system::error_code;
#else
using error_code = asio::error_code;
#endif
using tcp = asio::ip::tcp;
template<typename Handler, typename Adaptor = SocketAdaptor, typename... Middlewares>
@ -52,7 +66,7 @@ namespace crow
{
tick_function_();
tick_timer_.expires_after(std::chrono::milliseconds(tick_interval_.count()));
tick_timer_.async_wait([this](const asio::error_code& ec) {
tick_timer_.async_wait([this](const error_code& ec) {
if (ec)
return;
on_tick();
@ -128,7 +142,7 @@ namespace crow
{
tick_timer_.expires_after(std::chrono::milliseconds(tick_interval_.count()));
tick_timer_.async_wait(
[this](const asio::error_code& ec) {
[this](const error_code& ec) {
if (ec)
return;
on_tick();
@ -143,7 +157,7 @@ namespace crow
CROW_LOG_INFO << "Call `app.loglevel(crow::LogLevel::Warning)` to hide Info level logs.";
signals_.async_wait(
[&](const asio::error_code& /*error*/, int /*signal_number*/) {
[&](const error_code& /*error*/, int /*signal_number*/) {
stop();
});
@ -227,7 +241,7 @@ namespace crow
acceptor_.async_accept(
p->socket(),
[this, p, &is, service_idx](asio::error_code ec) {
[this, p, &is, service_idx](error_code ec) {
if (!ec)
{
is.post(

View File

@ -1670,7 +1670,7 @@ namespace crow
}
else
{
#if defined(__APPLE__) || defined(__MACH__) || defined(__FreeBSD__) || defined(__ANDROID__)
#if defined(__APPLE__) || defined(__MACH__) || defined(__FreeBSD__) || defined(__ANDROID__) || defined(_LIBCPP_VERSION)
o = std::unique_ptr<object>(new object(initializer_list));
#else
(*o) = initializer_list;
@ -1689,7 +1689,7 @@ namespace crow
}
else
{
#if defined(__APPLE__) || defined(__MACH__) || defined(__FreeBSD__) || defined(__ANDROID__)
#if defined(__APPLE__) || defined(__MACH__) || defined(__FreeBSD__) || defined(__ANDROID__) || defined(_LIBCPP_VERSION)
o = std::unique_ptr<object>(new object(value));
#else
(*o) = value;
@ -1840,7 +1840,14 @@ namespace crow
out.push_back('"');
}
inline void dump_internal(const wvalue& v, std::string& out) const
inline void dump_indentation_part(std::string& out, const int indent, const char separator, const int indent_level) const
{
out.push_back('\n');
out.append(indent_level * indent, separator);
}
inline void dump_internal(const wvalue& v, std::string& out, const int indent, const char separator, const int indent_level = 0) const
{
switch (v.t_)
{
@ -1934,6 +1941,12 @@ namespace crow
case type::List:
{
out.push_back('[');
if (indent >= 0)
{
dump_indentation_part(out, indent, separator, indent_level + 1);
}
if (v.l)
{
bool first = true;
@ -1942,17 +1955,34 @@ namespace crow
if (!first)
{
out.push_back(',');
if (indent >= 0)
{
dump_indentation_part(out, indent, separator, indent_level + 1);
}
}
first = false;
dump_internal(x, out);
dump_internal(x, out, indent, separator, indent_level + 1);
}
}
if (indent >= 0)
{
dump_indentation_part(out, indent, separator, indent_level);
}
out.push_back(']');
}
break;
case type::Object:
{
out.push_back('{');
if (indent >= 0)
{
dump_indentation_part(out, indent, separator, indent_level + 1);
}
if (v.o)
{
bool first = true;
@ -1961,13 +1991,29 @@ namespace crow
if (!first)
{
out.push_back(',');
if (indent >= 0)
{
dump_indentation_part(out, indent, separator, indent_level + 1);
}
}
first = false;
dump_string(kv.first, out);
out.push_back(':');
dump_internal(kv.second, out);
if (indent >= 0)
{
out.push_back(' ');
}
dump_internal(kv.second, out, indent, separator, indent_level + 1);
}
}
if (indent >= 0)
{
dump_indentation_part(out, indent, separator, indent_level);
}
out.push_back('}');
}
break;
@ -1979,13 +2025,20 @@ namespace crow
}
public:
std::string dump() const
std::string dump(const int indent, const char separator = ' ') const
{
std::string ret;
ret.reserve(estimate_length());
dump_internal(*this, ret);
dump_internal(*this, ret, indent, separator);
return ret;
}
std::string dump() const
{
static constexpr int DontIndent = -1;
return dump(DontIndent);
}
};
// Used for accessing the internals of a wvalue

View File

@ -1461,22 +1461,13 @@ namespace crow
CROW_LOG_DEBUG << "Matched rule (upgrade) '" << rules[rule_index]->rule_ << "' " << static_cast<uint32_t>(req.method) << " / " << rules[rule_index]->get_methods();
// any uncaught exceptions become 500s
try
{
rules[rule_index]->handle_upgrade(req, res, std::move(adaptor));
}
catch (std::exception& e)
{
CROW_LOG_ERROR << "An uncaught exception occurred: " << e.what();
res = response(500);
res.end();
return;
}
catch (...)
{
CROW_LOG_ERROR << "An uncaught exception occurred. The type was unknown so no information was available.";
res = response(500);
exception_handler_(res);
res.end();
return;
}
@ -1534,7 +1525,14 @@ namespace crow
std::vector<uint16_t> bpi = found.blueprint_indices;
if (bps_found[i]->catchall_rule().has_handler())
{
bps_found[i]->catchall_rule().handler_(req, res);
try
{
bps_found[i]->catchall_rule().handler_(req, res);
}
catch (...)
{
exception_handler_(res);
}
#ifdef CROW_ENABLE_DEBUG
return std::string("Redirected to Blueprint \"" + bps_found[i]->prefix() + "\" Catchall rule");
#else
@ -1544,7 +1542,14 @@ namespace crow
}
if (catchall_rule_.has_handler())
{
catchall_rule_.handler_(req, res);
try
{
catchall_rule_.handler_(req, res);
}
catch (...)
{
exception_handler_(res);
}
#ifdef CROW_ENABLE_DEBUG
return std::string("Redirected to global Catchall rule");
#else
@ -1704,23 +1709,14 @@ namespace crow
CROW_LOG_DEBUG << "Matched rule '" << rules[rule_index]->rule_ << "' " << static_cast<uint32_t>(req.method) << " / " << rules[rule_index]->get_methods();
// any uncaught exceptions become 500s
try
{
auto& rule = rules[rule_index];
handle_rule<App>(rule, req, res, found.r_params);
}
catch (std::exception& e)
{
CROW_LOG_ERROR << "An uncaught exception occurred: " << e.what();
res = response(500);
res.end();
return;
}
catch (...)
{
CROW_LOG_ERROR << "An uncaught exception occurred. The type was unknown so no information was available.";
res = response(500);
exception_handler_(res);
res.end();
return;
}
@ -1787,6 +1783,30 @@ namespace crow
return blueprints_;
}
std::function<void(crow::response&)>& exception_handler()
{
return exception_handler_;
}
static void default_exception_handler(response& res)
{
// any uncaught exceptions become 500s
res = response(500);
try
{
throw;
}
catch (const std::exception& e)
{
CROW_LOG_ERROR << "An uncaught exception occurred: " << e.what();
}
catch (...)
{
CROW_LOG_ERROR << "An uncaught exception occurred. The type was unknown so no information was available.";
}
}
private:
CatchallRule catchall_rule_;
@ -1802,5 +1822,6 @@ namespace crow
std::array<PerMethod, static_cast<int>(HTTPMethod::InternalMethodCount)> per_methods_;
std::vector<std::unique_ptr<BaseRule>> all_rules_;
std::vector<Blueprint*> blueprints_;
std::function<void(crow::response&)> exception_handler_ = &default_exception_handler;
};
} // namespace crow

View File

@ -1,20 +1,37 @@
#pragma once
#ifdef CROW_USE_BOOST
#include <boost/asio.hpp>
#include <boost/asio/version.hpp>
#ifdef CROW_ENABLE_SSL
#include <boost/asio/ssl.hpp>
#endif
#else
#ifndef ASIO_STANDALONE
#define ASIO_STANDALONE
#endif
#include <asio.hpp>
#include <asio/version.hpp>
#ifdef CROW_ENABLE_SSL
#include <asio/ssl.hpp>
#endif
#endif
#include "crow/settings.h"
#include <asio/version.hpp>
#if ASIO_VERSION >= 101300 // 1.13.0
#if (CROW_USE_BOOST && BOOST_VERSION >= 107000) || (ASIO_VERSION >= 101300)
#define GET_IO_SERVICE(s) ((asio::io_context&)(s).get_executor().context())
#else
#define GET_IO_SERVICE(s) ((s).get_io_service())
#endif
namespace crow
{
#ifdef CROW_USE_BOOST
namespace asio = boost::asio;
using error_code = boost::system::error_code;
#else
using error_code = asio::error_code;
#endif
using tcp = asio::ip::tcp;
/// A wrapper for the asio::ip::tcp::socket and asio::ssl::stream
@ -54,32 +71,32 @@ namespace crow
void close()
{
asio::error_code ec;
error_code ec;
socket_.close(ec);
}
void shutdown_readwrite()
{
asio::error_code ec;
error_code ec;
socket_.shutdown(asio::socket_base::shutdown_type::shutdown_both, ec);
}
void shutdown_write()
{
asio::error_code ec;
error_code ec;
socket_.shutdown(asio::socket_base::shutdown_type::shutdown_send, ec);
}
void shutdown_read()
{
asio::error_code ec;
error_code ec;
socket_.shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec);
}
template<typename F>
void start(F f)
{
f(asio::error_code());
f(error_code());
}
tcp::socket socket_;
@ -119,7 +136,7 @@ namespace crow
{
if (is_open())
{
asio::error_code ec;
error_code ec;
raw_socket().close(ec);
}
}
@ -128,7 +145,7 @@ namespace crow
{
if (is_open())
{
asio::error_code ec;
error_code ec;
raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_both, ec);
}
}
@ -137,7 +154,7 @@ namespace crow
{
if (is_open())
{
asio::error_code ec;
error_code ec;
raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_send, ec);
}
}
@ -146,7 +163,7 @@ namespace crow
{
if (is_open())
{
asio::error_code ec;
error_code ec;
raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec);
}
}
@ -160,7 +177,7 @@ namespace crow
void start(F f)
{
ssl_socket_->async_handshake(asio::ssl::stream_base::server,
[f](const asio::error_code& ec) {
[f](const error_code& ec) {
f(ec);
});
}

View File

@ -1,10 +1,15 @@
#pragma once
#ifdef CROW_USE_BOOST
#include <boost/asio.hpp>
#include <boost/asio/basic_waitable_timer.hpp>
#else
#ifndef ASIO_STANDALONE
#define ASIO_STANDALONE
#endif
#include <asio.hpp>
#include <asio/basic_waitable_timer.hpp>
#endif
#include <chrono>
#include <functional>
@ -15,6 +20,12 @@
namespace crow
{
#ifdef CROW_USE_BOOST
namespace asio = boost::asio;
using error_code = boost::system::error_code;
#else
using error_code = asio::error_code;
#endif
namespace detail
{
@ -112,7 +123,7 @@ namespace crow
if (tasks_.empty()) highest_id_ = 0;
}
void tick_handler(const asio::error_code& ec)
void tick_handler(const error_code& ec)
{
if (ec) return;

View File

@ -8,6 +8,12 @@
namespace crow
{
#ifdef CROW_USE_BOOST
namespace asio = boost::asio;
using error_code = boost::system::error_code;
#else
using error_code = asio::error_code;
#endif
namespace websocket
{
enum class WebSocketReadState
@ -299,7 +305,7 @@ namespace crow
//asio::async_read(adaptor_.socket(), asio::buffer(&mini_header_, 1),
adaptor_.socket().async_read_some(
asio::buffer(&mini_header_, 2),
[this](const asio::error_code& ec, std::size_t
[this](const error_code& ec, std::size_t
#ifdef CROW_ENABLE_DEBUG
bytes_transferred
#endif
@ -367,7 +373,7 @@ namespace crow
remaining_length16_ = 0;
asio::async_read(
adaptor_.socket(), asio::buffer(&remaining_length16_, 2),
[this](const asio::error_code& ec, std::size_t
[this](const error_code& ec, std::size_t
#ifdef CROW_ENABLE_DEBUG
bytes_transferred
#endif
@ -403,7 +409,7 @@ namespace crow
{
asio::async_read(
adaptor_.socket(), asio::buffer(&remaining_length_, 8),
[this](const asio::error_code& ec, std::size_t
[this](const error_code& ec, std::size_t
#ifdef CROW_ENABLE_DEBUG
bytes_transferred
#endif
@ -447,7 +453,7 @@ namespace crow
{
asio::async_read(
adaptor_.socket(), asio::buffer((char*)&mask_, 4),
[this](const asio::error_code& ec, std::size_t
[this](const error_code& ec, std::size_t
#ifdef CROW_ENABLE_DEBUG
bytes_transferred
#endif
@ -489,7 +495,7 @@ namespace crow
to_read = remaining_length_;
adaptor_.socket().async_read_some(
asio::buffer(buffer_, static_cast<std::size_t>(to_read)),
[this](const asio::error_code& ec, std::size_t bytes_transferred) {
[this](const error_code& ec, std::size_t bytes_transferred) {
is_reading = false;
if (!ec)
@ -641,7 +647,7 @@ namespace crow
auto watch = std::weak_ptr<void>{anchor_};
asio::async_write(
adaptor_.socket(), buffers,
[&, watch](const asio::error_code& ec, std::size_t /*bytes_transferred*/) {
[&, watch](const error_code& ec, std::size_t /*bytes_transferred*/) {
if (!ec && !close_connection_)
{
sending_buffers_.clear();

View File

@ -19,6 +19,13 @@
using namespace std;
using namespace crow;
#ifdef CROW_USE_BOOST
namespace asio = boost::asio;
using asio_error_code = boost::system::error_code;
#else
using asio_error_code = asio::error_code;
#endif
#define LOCALHOST_ADDRESS "127.0.0.1"
TEST_CASE("Rule")
@ -943,6 +950,72 @@ TEST_CASE("json_write")
CHECK(R"({"scores":[1,2,3]})" == y.dump());
} // json_write
TEST_CASE("json_write_with_indent")
{
static constexpr int IndentationLevelOne = 1;
static constexpr int IndentationLevelTwo = 2;
static constexpr int IndentationLevelFour = 4;
json::wvalue y;
y["scores"][0] = 1;
y["scores"][1] = "king";
y["scores"][2][0] = "real";
y["scores"][2][1] = false;
y["scores"][2][2] = true;
CHECK(R"({
"scores": [
1,
"king",
[
"real",
false,
true
]
]
})" == y.dump(IndentationLevelOne));
CHECK(R"({
"scores": [
1,
"king",
[
"real",
false,
true
]
]
})" == y.dump(IndentationLevelTwo));
CHECK(R"({
"scores": [
1,
"king",
[
"real",
false,
true
]
]
})" == y.dump(IndentationLevelFour));
static constexpr char TabSeparator = '\t';
CHECK(R"({
"scores": [
1,
"king",
[
"real",
false,
true
]
]
})" == y.dump(IndentationLevelOne, TabSeparator));
} // json_write_with_indent
TEST_CASE("json_copy_r_to_w_to_w_to_r")
{
json::rvalue r = json::load(
@ -2859,7 +2932,7 @@ TEST_CASE("websocket_max_payload")
}
}
asio::error_code ec;
asio_error_code ec;
c.lowest_layer().shutdown(asio::socket_base::shutdown_type::shutdown_both, ec);
app.stop();
@ -3261,6 +3334,79 @@ TEST_CASE("blueprint")
}
} // blueprint
TEST_CASE("exception_handler")
{
SimpleApp app;
CROW_ROUTE(app, "/get_error")
([&]() -> std::string {
throw std::runtime_error("some error occurred");
});
CROW_ROUTE(app, "/get_no_error")
([&]() {
return "Hello world";
});
app.validate();
{
request req;
response res;
req.url = "/get_error";
app.handle_full(req, res);
CHECK(500 == res.code);
CHECK(res.body.empty());
}
{
request req;
response res;
req.url = "/get_no_error";
app.handle_full(req, res);
CHECK(200 == res.code);
CHECK(res.body.find("Hello world") != std::string::npos);
}
app.exception_handler([](crow::response& res) {
try
{
throw;
}
catch (const std::exception& e)
{
res = response(501, e.what());
}
});
{
request req;
response res;
req.url = "/get_error";
app.handle_full(req, res);
CHECK(501 == res.code);
CHECK(res.body.find("some error occurred") != std::string::npos);
}
{
request req;
response res;
req.url = "/get_no_error";
app.handle_full(req, res);
CHECK(200 == res.code);
CHECK(res.body.find("some error occurred") == std::string::npos);
CHECK(res.body.find("Hello world") != std::string::npos);
}
} // exception_handler
TEST_CASE("base64")
{
unsigned char sample_bin[] = {0x14, 0xfb, 0x9c, 0x03, 0xd9, 0x7e};
@ -3371,7 +3517,7 @@ TEST_CASE("timeout")
asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451));
auto receive_future = async(launch::async, [&]() {
asio::error_code ec;
asio_error_code ec;
c.receive(asio::buffer(buf, 2048), 0, ec);
return ec;
});
@ -3391,7 +3537,7 @@ TEST_CASE("timeout")
size_t received;
auto receive_future = async(launch::async, [&]() {
asio::error_code ec;
asio_error_code ec;
received = c.receive(asio::buffer(buf, 2048), 0, ec);
return ec;
});
@ -3490,3 +3636,47 @@ TEST_CASE("lexical_cast")
CHECK(utility::lexical_cast<string>(4) == "4");
CHECK(utility::lexical_cast<float>("10", 2) == 10.0f);
}
TEST_CASE("http2_upgrade_is_ignored")
{
// Crow does not support HTTP/2 so upgrade headers must be ignored
// relevant RFC: https://datatracker.ietf.org/doc/html/rfc7540#section-3.2
static char buf[5012];
SimpleApp app;
CROW_ROUTE(app, "/echo").methods("POST"_method)
([](crow::request const& req) {
return req.body;
});
auto _ = app.bindaddr(LOCALHOST_ADDRESS).port(45451).run_async();
app.wait_for_server_start();
asio::io_service is;
auto make_request = [&](const std::string& rq) {
asio::ip::tcp::socket c(is);
c.connect(asio::ip::tcp::endpoint(
asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451));
c.send(asio::buffer(rq));
c.receive(asio::buffer(buf, 2048));
c.close();
return std::string(buf);
};
std::string request =
"POST /echo HTTP/1.1\r\n"
"user-agent: unittest.cpp\r\n"
"host: " LOCALHOST_ADDRESS ":45451\r\n"
"content-length: 48\r\n"
"connection: upgrade\r\n"
"upgrade: h2c\r\n"
"\r\n"
"http2 upgrade is not supported so body is parsed\r\n"
"\r\n";
auto res = make_request(request);
CHECK(res.find("http2 upgrade is not supported so body is parsed") != std::string::npos);
app.stop();
}