Malloy
Toggle main menu visibility
Loading...
Searching...
No Matches
lib
malloy
core
http
filters
file.hpp
1
#pragma once
2
3
#include "../response.hpp"
4
#include "../request.hpp"
5
#include "../../error.hpp"
6
7
#include <filesystem>
8
#include <variant>
9
16
namespace
malloy::http::filters
17
{
18
23
template
<
bool
isRequest>
24
struct
basic_file
25
{
26
using
response_type =
malloy::http::response<boost::beast::http::file_body>
;
27
using
request_type =
malloy::http::request<boost::beast::http::file_body>
;
28
using
value_type = boost::beast::http::file_body::value_type;
29
using
header_type = boost::beast::http::header<isRequest>;
30
using
setup_handler_t = std::function<void(
const
header_type&, value_type&)>;
31
35
setup_handler_t
setup
;
36
42
basic_file
() =
default
;
43
47
explicit
48
basic_file
(setup_handler_t setup_) :
49
setup
{ std::move(setup_) }
50
{
51
}
52
56
basic_file
(
basic_file
&&) noexcept = default;
57
63
basic_file
&
64
operator=(
basic_file
&&) noexcept = default;
65
72
[[nodiscard]]
73
static
74
basic_file
75
open
(
76
const std::filesystem::path& location,
77
std::function<
void
(
malloy
::
error_code
)> on_error,
78
boost::beast::file_mode mode
79
)
80
{
81
return
basic_file
{ [location, mode, on_error = std::move(on_error)](
auto
&&,
auto
& body) {
82
boost::beast::error_code ec;
83
body.open(location.string().c_str(), mode, ec);
84
if
(ec && on_error)
85
on_error(ec);
86
} };
87
}
88
89
[[nodiscard]]
90
std::variant<boost::beast::http::file_body>
91
body_for(
const
header_type&)
const
92
{
93
return
{ };
94
}
95
96
void
97
setup_body(
const
header_type& h, value_type& body)
const
98
{
99
if
(
setup
)
100
setup
(h, body);
101
}
102
};
103
104
using
file_request =
basic_file<true>
;
105
using
file_response =
basic_file<false>
;
106
107
}
malloy::http::request
Definition
request.hpp:19
malloy::http::response
Definition
response.hpp:22
malloy::http::filters
Contains the filter types bundled with malloy.
malloy
Definition
controller.hpp:32
malloy::error_code
boost::beast::error_code error_code
Error code used to signify errors without throwing. Truthy means it holds an error.
Definition
error.hpp:9
malloy::http::filters::basic_file
Writes the contents of a message to disk.
Definition
file.hpp:25
malloy::http::filters::basic_file::basic_file
basic_file(setup_handler_t setup_)
Construct with a setup handler.
Definition
file.hpp:48
malloy::http::filters::basic_file< true >::open
static basic_file open(const std::filesystem::path &location, std::function< void(malloy::error_code)> on_error, boost::beast::file_mode mode)
Definition
file.hpp:75
malloy::http::filters::basic_file::basic_file
basic_file(basic_file &&) noexcept=default
malloy::http::filters::basic_file::basic_file
basic_file()=default
Default ctor.
malloy::http::filters::basic_file< true >::setup
setup_handler_t setup
Definition
file.hpp:35
Generated by
1.18.0