Commit 9af5eddd authored by Dominik Charousset's avatar Dominik Charousset

Rename http::{ => request_}header

parent d9255378
...@@ -61,7 +61,7 @@ int caf_main(caf::actor_system& sys, const config& cfg) { ...@@ -61,7 +61,7 @@ int caf_main(caf::actor_system& sys, const config& cfg) {
// Limit how many clients may be connected at any given time. // Limit how many clients may be connected at any given time.
.max_connections(max_connections) .max_connections(max_connections)
// Accept only requests for path "/". // Accept only requests for path "/".
.on_request([](ws::acceptor<>& acc, const http::header& hdr) { .on_request([](ws::acceptor<>& acc, const http::request_header& hdr) {
// The hdr parameter is a dictionary with fields from the WebSocket // The hdr parameter is a dictionary with fields from the WebSocket
// handshake such as the path. // handshake such as the path.
auto path = hdr.path(); auto path = hdr.path();
......
...@@ -140,8 +140,8 @@ int caf_main(caf::actor_system& sys, const config& cfg) { ...@@ -140,8 +140,8 @@ int caf_main(caf::actor_system& sys, const config& cfg) {
// Limit how many clients may be connected at any given time. // Limit how many clients may be connected at any given time.
.max_connections(max_connections) .max_connections(max_connections)
// Forward the path from the WebSocket request to the worker. // Forward the path from the WebSocket request to the worker.
.on_request( .on_request([](ws::acceptor<caf::cow_string>& acc,
[](ws::acceptor<caf::cow_string>& acc, const http::header& hdr) { const http::request_header& hdr) {
// The hdr parameter is a dictionary with fields from the WebSocket // The hdr parameter is a dictionary with fields from the WebSocket
// handshake such as the path. This is only field we care about // handshake such as the path. This is only field we care about
// here. By passing the (copy-on-write) string to accept() here, we // here. By passing the (copy-on-write) string to accept() here, we
......
...@@ -185,7 +185,7 @@ int caf_main(caf::actor_system& sys, const config& cfg) { ...@@ -185,7 +185,7 @@ int caf_main(caf::actor_system& sys, const config& cfg) {
// Limit how many clients may be connected at any given time. // Limit how many clients may be connected at any given time.
.max_connections(max_connections) .max_connections(max_connections)
// Add handler for incoming connections. // Add handler for incoming connections.
.on_request([](ws::acceptor<>& acc, const http::header&) { .on_request([](ws::acceptor<>& acc, const http::request_header&) {
// Ignore all header fields and accept the connection. // Ignore all header fields and accept the connection.
acc.accept(); acc.accept();
}) })
......
...@@ -37,10 +37,10 @@ caf_add_component( ...@@ -37,10 +37,10 @@ caf_add_component(
src/net/dsl/config_base.cpp src/net/dsl/config_base.cpp
src/net/generic_lower_layer.cpp src/net/generic_lower_layer.cpp
src/net/generic_upper_layer.cpp src/net/generic_upper_layer.cpp
src/net/http/header.cpp
src/net/http/lower_layer.cpp src/net/http/lower_layer.cpp
src/net/http/method.cpp src/net/http/method.cpp
src/net/http/request.cpp src/net/http/request.cpp
src/net/http/request_header.cpp
src/net/http/response.cpp src/net/http/response.cpp
src/net/http/server.cpp src/net/http/server.cpp
src/net/http/server_factory.cpp src/net/http/server_factory.cpp
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include "caf/async/spsc_buffer.hpp" #include "caf/async/spsc_buffer.hpp"
#include "caf/fwd.hpp" #include "caf/fwd.hpp"
#include "caf/logger.hpp" #include "caf/logger.hpp"
#include "caf/net/http/header.hpp" #include "caf/net/http/request_header.hpp"
#include "caf/sec.hpp" #include "caf/sec.hpp"
#include <utility> #include <utility>
......
...@@ -125,8 +125,9 @@ enum class status : uint16_t; ...@@ -125,8 +125,9 @@ enum class status : uint16_t;
namespace caf::net::http { namespace caf::net::http {
class header;
class lower_layer; class lower_layer;
class request;
class request_header;
class server; class server;
class upper_layer; class upper_layer;
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include "caf/byte_span.hpp" #include "caf/byte_span.hpp"
#include "caf/detail/net_export.hpp" #include "caf/detail/net_export.hpp"
#include "caf/net/fwd.hpp" #include "caf/net/fwd.hpp"
#include "caf/net/http/header.hpp" #include "caf/net/http/request_header.hpp"
#include "caf/net/http/response.hpp" #include "caf/net/http/response.hpp"
#include <cstdint> #include <cstdint>
...@@ -20,11 +20,11 @@ ...@@ -20,11 +20,11 @@
namespace caf::net::http { namespace caf::net::http {
/// Handle type (implicitly shared) that represents an HTTP client request. /// Implicitly shared Handle type that represents an HTTP client request.
class CAF_NET_EXPORT request { class CAF_NET_EXPORT request {
public: public:
struct impl { struct impl {
header hdr; request_header hdr;
std::vector<std::byte> body; std::vector<std::byte> body;
async::promise<response> prom; async::promise<response> prom;
}; };
...@@ -40,18 +40,23 @@ public: ...@@ -40,18 +40,23 @@ public:
// nop // nop
} }
/// Returns the HTTP header. /// Returns the HTTP header for the request.
/// @pre `valid()` /// @pre `valid()`
const header& hdr() const { const request_header& header() const {
return pimpl_->hdr; return pimpl_->hdr;
} }
/// Returns the HTTP body (payload). /// Returns the HTTP body (payload) for the request.
/// @pre `valid()` /// @pre `valid()`
const_byte_span body() const { const_byte_span body() const {
return make_span(pimpl_->body); return make_span(pimpl_->body);
} }
/// @copydoc body
const_byte_span payload() const {
return make_span(pimpl_->body);
}
/// Sends an HTTP response message to the client. Automatically sets the /// Sends an HTTP response message to the client. Automatically sets the
/// `Content-Type` and `Content-Length` header fields. /// `Content-Type` and `Content-Length` header fields.
/// @pre `valid()` /// @pre `valid()`
......
...@@ -16,19 +16,20 @@ ...@@ -16,19 +16,20 @@
namespace caf::net::http { namespace caf::net::http {
class CAF_NET_EXPORT header { /// Encapsulates meta data for HTTP requests.
class CAF_NET_EXPORT request_header {
public: public:
header() = default; request_header() = default;
header(header&&) = default; request_header(request_header&&) = default;
header& operator=(header&&) = default; request_header& operator=(request_header&&) = default;
header(const header&); request_header(const request_header&);
header& operator=(const header&); request_header& operator=(const request_header&);
void assign(const header&); void assign(const request_header&);
http::method method() const noexcept { http::method method() const noexcept {
return method_; return method_;
......
...@@ -11,9 +11,9 @@ ...@@ -11,9 +11,9 @@
#include "caf/error.hpp" #include "caf/error.hpp"
#include "caf/logger.hpp" #include "caf/logger.hpp"
#include "caf/net/fwd.hpp" #include "caf/net/fwd.hpp"
#include "caf/net/http/header.hpp"
#include "caf/net/http/header_fields_map.hpp" #include "caf/net/http/header_fields_map.hpp"
#include "caf/net/http/lower_layer.hpp" #include "caf/net/http/lower_layer.hpp"
#include "caf/net/http/request_header.hpp"
#include "caf/net/http/status.hpp" #include "caf/net/http/status.hpp"
#include "caf/net/http/upper_layer.hpp" #include "caf/net/http/upper_layer.hpp"
#include "caf/net/http/v1.hpp" #include "caf/net/http/v1.hpp"
...@@ -35,12 +35,6 @@ class CAF_NET_EXPORT server : public octet_stream::upper_layer, ...@@ -35,12 +35,6 @@ class CAF_NET_EXPORT server : public octet_stream::upper_layer,
public: public:
// -- member types ----------------------------------------------------------- // -- member types -----------------------------------------------------------
using header_fields_type = header_fields_map;
using status_code_type = status;
using header_type = header;
enum class mode { enum class mode {
read_header, read_header,
read_payload, read_payload,
...@@ -134,7 +128,7 @@ private: ...@@ -134,7 +128,7 @@ private:
upper_layer_ptr up_; upper_layer_ptr up_;
/// Buffer for re-using memory. /// Buffer for re-using memory.
header hdr_; request_header hdr_;
/// Stores whether we are currently waiting for the payload. /// Stores whether we are currently waiting for the payload.
mode mode_ = mode::read_header; mode mode_ = mode::read_header;
......
...@@ -75,7 +75,7 @@ public: ...@@ -75,7 +75,7 @@ public:
error start(net::http::lower_layer* down) override; error start(net::http::lower_layer* down) override;
ptrdiff_t consume(const net::http::header& hdr, ptrdiff_t consume(const net::http::request_header& hdr,
const_byte_span payload) override; const_byte_span payload) override;
static auto make(async::execution_context_ptr loop, static auto make(async::execution_context_ptr loop,
......
...@@ -27,7 +27,8 @@ public: ...@@ -27,7 +27,8 @@ public:
/// @returns The number of consumed bytes or a negative value to signal an /// @returns The number of consumed bytes or a negative value to signal an
/// error. /// error.
/// @note Discarded data is lost permanently. /// @note Discarded data is lost permanently.
virtual ptrdiff_t consume(const header& hdr, const_byte_span payload) = 0; virtual ptrdiff_t consume(const request_header& hdr, const_byte_span payload)
= 0;
}; };
} // namespace caf::net::http } // namespace caf::net::http
...@@ -63,7 +63,8 @@ public: ...@@ -63,7 +63,8 @@ public:
error start(http::lower_layer* down) override; error start(http::lower_layer* down) override;
ptrdiff_t consume(const http::header& hdr, const_byte_span payload) override; ptrdiff_t consume(const http::request_header& hdr,
const_byte_span payload) override;
private: private:
explicit server(scrape_state_ptr state) : state_(std::move(state)) { explicit server(scrape_state_ptr state) : state_(std::move(state)) {
......
...@@ -41,8 +41,9 @@ public: ...@@ -41,8 +41,9 @@ public:
"on_request must take an acceptor as 1st argument"); "on_request must take an acceptor as 1st argument");
static_assert(std::is_same_v<arg1_t, acceptor_t&>, static_assert(std::is_same_v<arg1_t, acceptor_t&>,
"on_request must take the acceptor as mutable reference"); "on_request must take the acceptor as mutable reference");
static_assert(std::is_same_v<arg2_t, const http::header&>, static_assert(
"on_request must take 'const http::header&' as 2nd argument"); std::is_same_v<arg2_t, const http::request_header&>,
"on_request must take 'const http::request_header&' as 2nd argument");
// Wrap the callback and return the factory object. // Wrap the callback and return the factory object.
using factory_t = typename acceptor_t::template server_factory_type<Trait>; using factory_t = typename acceptor_t::template server_factory_type<Trait>;
auto callback = make_shared_type_erased_callback(std::move(on_request)); auto callback = make_shared_type_erased_callback(std::move(on_request));
......
...@@ -10,8 +10,8 @@ ...@@ -10,8 +10,8 @@
#include "caf/error.hpp" #include "caf/error.hpp"
#include "caf/logger.hpp" #include "caf/logger.hpp"
#include "caf/net/fwd.hpp" #include "caf/net/fwd.hpp"
#include "caf/net/http/header.hpp"
#include "caf/net/http/method.hpp" #include "caf/net/http/method.hpp"
#include "caf/net/http/request_header.hpp"
#include "caf/net/http/status.hpp" #include "caf/net/http/status.hpp"
#include "caf/net/http/v1.hpp" #include "caf/net/http/v1.hpp"
#include "caf/net/multiplexer.hpp" #include "caf/net/multiplexer.hpp"
...@@ -47,7 +47,7 @@ public: ...@@ -47,7 +47,7 @@ public:
/// @param down A pointer to the lower layer that remains valid for the /// @param down A pointer to the lower layer that remains valid for the
/// lifetime of the upper layer. /// lifetime of the upper layer.
/// @param hdr The HTTP request header from the client handshake. /// @param hdr The HTTP request header from the client handshake.
virtual error start(lower_layer* down, const http::header& hdr) = 0; virtual error start(lower_layer* down, const http::request_header& hdr) = 0;
}; };
using upper_layer_ptr = std::unique_ptr<upper_layer>; using upper_layer_ptr = std::unique_ptr<upper_layer>;
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#include "caf/detail/ws_flow_bridge.hpp" #include "caf/detail/ws_flow_bridge.hpp"
#include "caf/fwd.hpp" #include "caf/fwd.hpp"
#include "caf/net/dsl/server_factory_base.hpp" #include "caf/net/dsl/server_factory_base.hpp"
#include "caf/net/http/header.hpp" #include "caf/net/http/request_header.hpp"
#include "caf/net/http/server.hpp" #include "caf/net/http/server.hpp"
#include "caf/net/multiplexer.hpp" #include "caf/net/multiplexer.hpp"
#include "caf/net/octet_stream/transport.hpp" #include "caf/net/octet_stream/transport.hpp"
...@@ -36,7 +36,8 @@ public: ...@@ -36,7 +36,8 @@ public:
using ws_acceptor_t = net::web_socket::acceptor<Ts...>; using ws_acceptor_t = net::web_socket::acceptor<Ts...>;
using on_request_cb_type using on_request_cb_type
= shared_callback_ptr<void(ws_acceptor_t&, const net::http::header&)>; = shared_callback_ptr<void(ws_acceptor_t&, //
const net::http::request_header&)>;
using accept_event = typename Trait::template accept_event<Ts...>; using accept_event = typename Trait::template accept_event<Ts...>;
...@@ -63,7 +64,7 @@ public: ...@@ -63,7 +64,7 @@ public:
} }
error start(net::web_socket::lower_layer* down_ptr, error start(net::web_socket::lower_layer* down_ptr,
const net::http::header& hdr) override { const net::http::request_header& hdr) override {
CAF_ASSERT(down_ptr != nullptr); CAF_ASSERT(down_ptr != nullptr);
super::down_ = down_ptr; super::down_ = down_ptr;
net::web_socket::acceptor_impl<Trait, Ts...> acc; net::web_socket::acceptor_impl<Trait, Ts...> acc;
...@@ -95,7 +96,8 @@ public: ...@@ -95,7 +96,8 @@ public:
using ws_acceptor_t = net::web_socket::acceptor<Ts...>; using ws_acceptor_t = net::web_socket::acceptor<Ts...>;
using on_request_cb_type using on_request_cb_type
= shared_callback_ptr<void(ws_acceptor_t&, const net::http::header&)>; = shared_callback_ptr<void(ws_acceptor_t&, //
const net::http::request_header&)>;
using accept_event = typename Trait::template accept_event<Ts...>; using accept_event = typename Trait::template accept_event<Ts...>;
...@@ -155,7 +157,7 @@ public: ...@@ -155,7 +157,7 @@ public:
using config_type = typename super::config_type; using config_type = typename super::config_type;
using on_request_cb_type using on_request_cb_type
= shared_callback_ptr<void(acceptor<Ts...>&, const http::header&)>; = shared_callback_ptr<void(acceptor<Ts...>&, const http::request_header&)>;
server_factory(intrusive_ptr<config_type> cfg, on_request_cb_type on_request) server_factory(intrusive_ptr<config_type> cfg, on_request_cb_type on_request)
: super(std::move(cfg)), on_request_(std::move(on_request)) { : super(std::move(cfg)), on_request_(std::move(on_request)) {
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
#include "caf/detail/net_export.hpp" #include "caf/detail/net_export.hpp"
#include "caf/net/fwd.hpp" #include "caf/net/fwd.hpp"
#include "caf/net/generic_upper_layer.hpp" #include "caf/net/generic_upper_layer.hpp"
#include "caf/net/http/header.hpp" #include "caf/net/http/request_header.hpp"
namespace caf::net::web_socket { namespace caf::net::web_socket {
......
#include "caf/net/http/header.hpp" #include "caf/net/http/request_header.hpp"
#include "caf/expected.hpp" #include "caf/expected.hpp"
#include "caf/logger.hpp" #include "caf/logger.hpp"
...@@ -30,16 +30,16 @@ bool for_each_line(std::string_view input, F&& f) { ...@@ -30,16 +30,16 @@ bool for_each_line(std::string_view input, F&& f) {
} // namespace } // namespace
header::header(const header& other) { request_header::request_header(const request_header& other) {
assign(other); assign(other);
} }
header& header::operator=(const header& other) { request_header& request_header::operator=(const request_header& other) {
assign(other); assign(other);
return *this; return *this;
} }
void header::assign(const header& other) { void request_header::assign(const request_header& other) {
auto remap = [](const char* base, std::string_view src, auto remap = [](const char* base, std::string_view src,
const char* new_base) { const char* new_base) {
auto offset = std::distance(base, src.data()); auto offset = std::distance(base, src.data());
...@@ -66,7 +66,8 @@ void header::assign(const header& other) { ...@@ -66,7 +66,8 @@ void header::assign(const header& other) {
} }
} }
std::pair<status, std::string_view> header::parse(std::string_view raw) { std::pair<status, std::string_view>
request_header::parse(std::string_view raw) {
CAF_LOG_TRACE(CAF_ARG(raw)); CAF_LOG_TRACE(CAF_ARG(raw));
// Sanity checking and copying of the raw input. // Sanity checking and copying of the raw input.
using namespace literals; using namespace literals;
...@@ -146,11 +147,11 @@ std::pair<status, std::string_view> header::parse(std::string_view raw) { ...@@ -146,11 +147,11 @@ std::pair<status, std::string_view> header::parse(std::string_view raw) {
} }
} }
bool header::chunked_transfer_encoding() const { bool request_header::chunked_transfer_encoding() const {
return field("Transfer-Encoding").find("chunked") != std::string_view::npos; return field("Transfer-Encoding").find("chunked") != std::string_view::npos;
} }
std::optional<size_t> header::content_length() const { std::optional<size_t> request_header::content_length() const {
return field_as<size_t>("Content-Length"); return field_as<size_t>("Content-Length");
} }
......
...@@ -51,7 +51,7 @@ error http_flow_adapter::start(net::http::lower_layer* down) { ...@@ -51,7 +51,7 @@ error http_flow_adapter::start(net::http::lower_layer* down) {
return none; return none;
} }
ptrdiff_t http_flow_adapter::consume(const net::http::header& hdr, ptrdiff_t http_flow_adapter::consume(const net::http::request_header& hdr,
const_byte_span payload) { const_byte_span payload) {
using namespace net::http; using namespace net::http;
if (!pending_.empty()) { if (!pending_.empty()) {
......
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
#include "caf/net/prometheus/server.hpp" #include "caf/net/prometheus/server.hpp"
#include "caf/net/http/header.hpp"
#include "caf/net/http/lower_layer.hpp" #include "caf/net/http/lower_layer.hpp"
#include "caf/net/http/request_header.hpp"
using namespace std::literals; using namespace std::literals;
...@@ -42,7 +42,8 @@ error server::start(http::lower_layer* down) { ...@@ -42,7 +42,8 @@ error server::start(http::lower_layer* down) {
return caf::none; return caf::none;
} }
ptrdiff_t server::consume(const http::header& hdr, const_byte_span payload) { ptrdiff_t server::consume(const http::request_header& hdr,
const_byte_span payload) {
if (hdr.path() != "/metrics") { if (hdr.path() != "/metrics") {
down_->send_response(http::status::not_found, "text/plain", "Not found."); down_->send_response(http::status::not_found, "text/plain", "Not found.");
down_->shutdown(); down_->shutdown();
......
...@@ -89,7 +89,7 @@ void server::write_response(http::status code, std::string_view msg) { ...@@ -89,7 +89,7 @@ void server::write_response(http::status code, std::string_view msg) {
bool server::handle_header(std::string_view http) { bool server::handle_header(std::string_view http) {
using namespace std::literals; using namespace std::literals;
// Parse the header and reject invalid inputs. // Parse the header and reject invalid inputs.
http::header hdr; http::request_header hdr;
auto [code, msg] = hdr.parse(http); auto [code, msg] = hdr.parse(http);
if (code != http::status::ok) { if (code != http::status::ok) {
write_response(code, msg); write_response(code, msg);
......
...@@ -17,7 +17,7 @@ class app_t : public net::http::upper_layer { ...@@ -17,7 +17,7 @@ class app_t : public net::http::upper_layer {
public: public:
// -- member variables ------------------------------------------------------- // -- member variables -------------------------------------------------------
net::http::header hdr; net::http::request_header hdr;
caf::byte_buffer payload; caf::byte_buffer payload;
...@@ -66,7 +66,7 @@ public: ...@@ -66,7 +66,7 @@ public:
return true; return true;
} }
ptrdiff_t consume(const net::http::header& request_hdr, ptrdiff_t consume(const net::http::request_header& request_hdr,
const_byte_span body) override { const_byte_span body) override {
hdr = request_hdr; hdr = request_hdr;
auto content = "Hello world!"sv; auto content = "Hello world!"sv;
......
...@@ -33,7 +33,7 @@ public: ...@@ -33,7 +33,7 @@ public:
} }
error start(net::web_socket::lower_layer* down, error start(net::web_socket::lower_layer* down,
const net::http::header& hdr) override { const net::http::request_header& hdr) override {
down->request_messages(); down->request_messages();
// Store the request information in cfg to evaluate them later. // Store the request information in cfg to evaluate them later.
auto& ws = cfg["web-socket"].as_dictionary(); auto& ws = cfg["web-socket"].as_dictionary();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment