commit - 51254cccedb5bd0fe1e68c8d11c0af9941de9b73
commit + d6e3180e246d82e01f17fe84983d657de7a23a25
blob - /dev/null
blob + 3adb3f1a33725888bb9995182902bfa952a29d97 (mode 644)
--- /dev/null
+++ capsule/coding_style.gmi
+# vostok C++ coding style
+
+=> https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines C++ Core Guidelines
+
+## Files
+
+* <snake_case>.cc
+* <snake_case>.h
+
+## Names
+
+Styles:
+* CamelCase for class and struct;
+* snake_case for namespace, variable, function and method;
+* SCREAMING_CASE for constants.
+
+Prefixes:
+* Use "m_" prefix for private members of class ;
+* Use "s_" prefix for static members of class;
+* Use "g_" prefix for global variables.
+
+## class / struct
+
+struct:
+* data members can vary independently;
+* all members are public;
+* does not contain methods.
+
+class:
+* has an invariant
+* all members are private;
+
blob - cea622f498bd0f077d52e69efb29a893e6b0bdb7
blob + 3f4fbd8aecafd4938a205056f29188facaeb1e3c
--- tests/test_open_file.cc
+++ tests/test_open_file.cc
ss << "/tmp/test_open_file." << getpid() << "." << time(nullptr);
IS_TRUE_ERRNO(mkdir(ss.str().c_str(), S_IRWXU) != -1, "Create directory " << ss.str().c_str());
- const unique_fd dir{open(ss.str().c_str(), O_RDONLY | O_DIRECTORY)};
+ const UniqueFd dir{open(ss.str().c_str(), O_RDONLY | O_DIRECTORY)};
IS_TRUE_ERRNO(dir, "Open directory " << ss.str().c_str());
{
- unique_fd opened_file{};
+ UniqueFd opened_file;
IS_TRUE(open_file(dir.get(), "", opened_file) == file_not_found and !opened_file);
IS_TRUE(open_file(dir.get(), "non-existent-file", opened_file) == file_not_found and !opened_file);
IS_TRUE(open_file(dir.get(), "non-existent-dir/file", opened_file) == file_not_found and !opened_file);
}
- const unique_fd index_gmi{openat(dir.get(), "index.gmi", O_RDONLY | O_CREAT | O_EXCL, S_IRWXU)};
+ const UniqueFd index_gmi{openat(dir.get(), "index.gmi", O_RDONLY | O_CREAT | O_EXCL, S_IRWXU)};
IS_TRUE_ERRNO(index_gmi, "Create file " << ss.str().c_str() << "/index.gmi");
struct stat sb1{};
IS_TRUE_ERRNO(fstat(index_gmi.get(), &sb1) != -1, "Stat file " << ss.str().c_str() << "/index.gmi");
{
- unique_fd opened_file{};
+ UniqueFd opened_file;
IS_TRUE(open_file(dir.get(), "", opened_file) == file_opened and opened_file);
struct stat sb2{};
IS_TRUE_ERRNO(fstat(opened_file.get(), &sb2) != -1, "Stat file " << ss.str().c_str() << "/index.gmi");
}
{
- unique_fd opened_file{};
+ UniqueFd opened_file;
IS_TRUE(open_file(dir.get(), "index.gmi", opened_file) == file_opened and opened_file);
struct stat sb2{};
IS_TRUE_ERRNO(fstat(opened_file.get(), &sb2) != -1, "Stat file " << ss.str().c_str() << "/index.gmi");
IS_TRUE_ERRNO(mkdirat(dir.get(), "subdir", S_IRWXU) != -1, "Create directory " << ss.str().c_str() << "/subdir");
{
- unique_fd opened_file{};
+ UniqueFd opened_file;
IS_TRUE(open_file(dir.get(), "subdir", opened_file) == file_not_found and !opened_file);
IS_TRUE(open_file(dir.get(), "subdir/", opened_file) == file_not_found and !opened_file);
}
- const unique_fd subdir{openat(dir.get(), "subdir", O_RDONLY | O_DIRECTORY)};
+ const UniqueFd subdir{openat(dir.get(), "subdir", O_RDONLY | O_DIRECTORY)};
IS_TRUE_ERRNO(subdir, "Open directory " << ss.str().c_str() << "/subdir");
- const unique_fd sub_index_gmi{openat(subdir.get(), "index.gmi", O_RDONLY | O_CREAT | O_EXCL, S_IRWXU)};
+ const UniqueFd sub_index_gmi{openat(subdir.get(), "index.gmi", O_RDONLY | O_CREAT | O_EXCL, S_IRWXU)};
IS_TRUE_ERRNO(sub_index_gmi, "Create file " << ss.str().c_str() << "/subdir/index.gmi");
IS_TRUE_ERRNO(fstat(sub_index_gmi.get(), &sb1) != -1, "Stat file " << ss.str().c_str() << "/subdir/index.gmi");
{
- unique_fd opened_file{};
+ UniqueFd opened_file;
IS_TRUE(open_file(dir.get(), "subdir", opened_file) == file_opened and opened_file);
struct stat sb2{};
IS_TRUE_ERRNO(fstat(opened_file.get(), &sb2) != -1, "Stat file " << ss.str().c_str() << "/subdir/index.gmi");
}
{
- unique_fd opened_file{};
+ UniqueFd opened_file;
IS_TRUE(open_file(dir.get(), "subdir/", opened_file) == file_opened and opened_file);
struct stat sb2{};
IS_TRUE_ERRNO(fstat(opened_file.get(), &sb2) != -1, "Stat file " << ss.str().c_str() << "/subdir/index.gmi");
}
{
- unique_fd opened_file{};
+ UniqueFd opened_file;
IS_TRUE(open_file(dir.get(), "subdir/index.gmi", opened_file) == file_opened and opened_file);
struct stat sb2{};
IS_TRUE_ERRNO(fstat(opened_file.get(), &sb2) != -1, "Stat file " << ss.str().c_str() << "/subdir/index.gmi");
IS_TRUE((sb1.st_dev == sb2.st_dev) && (sb1.st_ino == sb2.st_ino));
}
- const unique_fd eperm{openat(subdir.get(), "EPERM.gmi", O_RDONLY | O_CREAT | O_EXCL, 0)};
+ const UniqueFd eperm{openat(subdir.get(), "EPERM.gmi", O_RDONLY | O_CREAT | O_EXCL, 0)};
IS_TRUE_ERRNO(eperm, "Create file " << ss.str().c_str() << "/subdir/index.gmi");
{
- unique_fd opened_file{};
+ UniqueFd opened_file;
IS_TRUE(open_file(dir.get(), "subdir/EPERM.gmi", opened_file) == file_opening_error and !opened_file);
}
blob - 94b5019fb6f0bd231cc26ba512811ea27082155b
blob + 19144e4a70be55173bbd017c1697f77ebf2669bc
--- vostok/args.cc
+++ vostok/args.cc
} // namespace <unnamed>
-bool command_line_arguments::parse(int argc, char *argv[])
+bool
+parse_command_line_arguments(
+ /* in */ int argc,
+ /* in */ char *argv[],
+ /* out */ CommandLineArguments &args
+)
{
int ch;
char *p = nullptr;
while ((ch = getopt(argc, argv, "a:p:c:k:f:")) != -1) {
switch (ch) {
case 'a':
- m_addr = optarg;
+ args.addr = optarg;
break;
case 'p':
p = nullptr;
- m_port = std::strtoul(optarg, &p, 10);
- if (!m_port)
+ args.port = std::strtoul(optarg, &p, 10);
+ if (!args.port)
{
error::g_log << "Invalid PORT value: " << optarg << std::endl;
return usage(argv[0]);
}
break;
case 'c':
- m_cert_file = optarg;
+ args.cert_file = optarg;
break;
case 'k':
- m_key_file = optarg;
+ args.key_file = optarg;
break;
case 'f':
- m_directory.reset(open(optarg, O_RDONLY));
- if (!m_directory)
+ args.directory.reset(open(optarg, O_RDONLY));
+ if (!args.directory)
{
error::occurred(
[]
{
error::g_log << "Open directory \"" << optarg << "\"";
},
- error::print{}
+ error::Print{}
);
return false;
}
return usage(argv[0]);
}
}
- if (!m_cert_file)
+ if (!args.cert_file)
{
error::g_log << "Invalid command line: -c option required" << std::endl;
return usage(argv[0]);
}
- if (!m_key_file)
+ if (!args.key_file)
{
error::g_log << "Invalid command line: -k option required" << std::endl;
return usage(argv[0]);
}
- if (!m_directory)
+ if (!args.directory)
{
error::g_log << "Invalid command line: -d option required" << std::endl;
return usage(argv[0]);
blob - 7f7b8682627a3b5ff65838d3724972184ead7e07
blob + 0d5d970247a1b2947a8fb3f7fb034b373f784656
--- vostok/args.h
+++ vostok/args.h
/** Parse command line arguments */
+#include "utils.h"
#pragma once
-#include "utils.h"
-
namespace vostok
{
-struct command_line_arguments
+struct CommandLineArguments
{
- not_null<czstring> m_addr{"127.0.0.1"};
- int m_port{1965};
- czstring m_cert_file{nullptr};
- czstring m_key_file{nullptr};
- unique_fd m_directory;
-
- bool parse(int argc, char *argv[]);
+ NotNull<czstring> addr{"127.0.0.1"};
+ int port{1965};
+ czstring cert_file{nullptr};
+ czstring key_file{nullptr};
+ UniqueFd directory;
};
+bool
+parse_command_line_arguments(
+ /* in */ int argc,
+ /* in */ char *argv[],
+ /* out */ CommandLineArguments &args
+);
+
+
} // namespace vostok
blob - a1791a32da6f24b2d3a3060d95fb971e744eaecd
blob + 84d7f97f0e3b15bfa61f8da4c5e2419468355860
--- vostok/error.h
+++ vostok/error.h
/** Default error code (errno) printer */
-struct print
+class Print
{
- const int m_error;
- explicit print(int error=errno) : m_error{error} {}
+public:
+ explicit Print(int error=errno) : m_error{error} {}
void operator() () const
{
g_log << "Error code: " << std::dec << m_error << ". " << strerror(m_error) << std::endl;
}
+
+private:
+ const int m_error;
};
/** Empty error code printer */
-struct none
+class None
{
+public:
void operator() () const {}
};
/** Error handler: print action, print error */
template<
- typename TPrintAction,
- typename TPrintError
+ typename PrintAction,
+ typename PrintError
>
void
occurred(
- TPrintAction print_action,
- TPrintError print_error
+ PrintAction print_action,
+ PrintError print_error
)
{
print_action();
blob - 224e22e17c796b4e3b805df0d6582f1ad60cc932
blob + b9f759c3f7cb8c276e7736ddb6cf270de97787d4
--- vostok/open_file.cc
+++ vostok/open_file.cc
{
namespace
{
-not_null<czstring> index_gmi = "index.gmi";
+NotNull<czstring> index_gmi = "index.gmi";
} // namespace <unnamed>
-open_file_result open_file(int directory_fd, not_null<czstring> file_name, unique_fd &opened_file)
+open_file_result open_file(int directory_fd, NotNull<czstring> file_name, UniqueFd &opened_file)
{
if (!*file_name)
file_name = index_gmi;
{
error::g_log << "Open file \"" << file_name << "\"";
},
- error::print{error_code}
+ error::Print{error_code}
);
return (error_code == ENOENT) ? file_not_found : file_opening_error;
}
{
error::g_log << "Stat file \"" << file_name << "\"";
},
- error::print{}
+ error::Print{}
);
return file_opening_error;
}
if (S_ISDIR(sb.st_mode))
{
- const unique_fd new_parent{opened_file.release()};
+ const UniqueFd new_parent{opened_file.release()};
opened_file.reset(openat(new_parent.get(), index_gmi, O_RDONLY));
if (!opened_file)
{
{
error::g_log << "Open file \"" << file_name << "\" /" << index_gmi;
},
- error::print{error_code}
+ error::Print{error_code}
);
return (error_code == ENOENT) ? file_not_found : file_opening_error;
}
blob - b370afb5847fa49a237389954a908a3b9b1ab47f
blob + fbbac29f2d23f9f486cc070d8bee803ae886cfc0
--- vostok/open_file.h
+++ vostok/open_file.h
file_opening_error,
};
-open_file_result open_file(int directory_fd, not_null<czstring> file_name, unique_fd &opened_file);
+open_file_result open_file(int directory_fd, NotNull<czstring> file_name, UniqueFd &opened_file);
} // namespace vostok
blob - 1aaa90926bb3db42ac911c8ea9084866c41cc1a5
blob + d81f5fd62b340269f56acd085a42c8a107fe12e0
--- vostok/parse_url.cc
+++ vostok/parse_url.cc
);
}
-class path_normalization
+class PathNormalization
{
public:
- using path_components_t = std::list< span<const char> >;
+ using Components = std::list< Span<const char> >;
url_normalization_result
operator() (
void fill(std::vector<char> &url) const;
private:
- path_components_t::value_type m_inprogress;
- path_components_t m_result;
+ Components::value_type m_inprogress;
+ Components m_result;
};
url_normalization_result
-path_normalization::operator() (
+PathNormalization::operator() (
std::vector<char>::const_iterator p,
std::vector<char> &url
)
{
- m_inprogress = path_components_t::value_type{nullptr, 0};
+ m_inprogress = Components::value_type{nullptr, 0};
m_result.clear();
for (; p != url.cend(); ++p)
}
-url_normalization_result path_normalization::on_component()
+url_normalization_result PathNormalization::on_component()
{
if ((m_inprogress.size() == 0) || (m_inprogress.size() == 1 && m_inprogress[0] == '.'))
{
return on_component_ok();
}
-void path_normalization::fill(std::vector<char> &url) const
+void PathNormalization::fill(std::vector<char> &url) const
{
auto current = url.begin();
for (auto it = m_result.cbegin(); it != m_result.cend(); ++it)
}
// normalize '.' and '..'
- path_normalization normalizer;
- return normalizer(current, url);
+ return PathNormalization{}(current, url);
}
blob - eee8490d9b37cbbf3be882e1591c5b26f75531d0
blob + 4fece0d30541a02b52be7cfc58bc84d700aaaab7
--- vostok/parse_url.h
+++ vostok/parse_url.h
url_root_traverse,
};
-/** Zero-terminated path from gemini URL */
-using zs_url_path_t = std::array<char, gemini::MAX_URL_LENGTH + 1>;
-
/** Extract normalized path from URL as null-terminated string (inplace) */
url_normalization_result
extract_url_path(
blob - 51ee360ea9d26a76b2e8f366dbb3d17a9967b0ec
blob + 2aaa254c5a3bfa60d5b3f39508e1eac1210709e8
--- vostok/transport.cc
+++ vostok/transport.cc
namespace
{
-using config_t = std::unique_ptr<struct tls_config, decltype(&tls_config_free)>;
constexpr auto protocols = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
-bool read(not_null<struct tls *> ctx, std::vector<char> &buff)
+using ConfigPtrBase = std::unique_ptr<struct tls_config, decltype(&tls_config_free)>;
+class ConfigPtr : public ConfigPtrBase
{
+public:
+ explicit ConfigPtr(struct tls_config *p=nullptr) : ConfigPtrBase{p, tls_config_free} {}
+};
+
+
+bool read(NotNull<struct tls *> ctx, std::vector<char> &buff)
+{
ssize_t ret{};
for (; ; )
{
{
if (tls_init() == -1)
{
- error::occurred("TLS initialization", error::none{});
+ error::occurred("TLS initialization", error::None{});
return false;
}
}
-void create_server(not_null<czstring> cert_file, not_null<czstring> key_file, context_t &ret_ctx)
+void create_server(NotNull<czstring> cert_file, NotNull<czstring> key_file, ContextPtr &ret_ctx)
{
- config_t cfg{tls_config_new(), tls_config_free};
+ ConfigPtr cfg{tls_config_new()};
if (!cfg)
{
- error::occurred("Create TLS configuration", error::none{});
+ error::occurred("Create TLS configuration", error::None{});
return;
}
if (tls_config_set_protocols(cfg.get(), protocols) == -1)
{
- error::occurred("Allow TLS 1.2 and TLS 1.3 protocols", error::none{});
+ error::occurred("Allow TLS 1.2 and TLS 1.3 protocols", error::None{});
return;
}
if (tls_config_set_cert_file(cfg.get(), cert_file) == -1)
{
error::g_log << "Load certificate file " << cert_file;
},
- error::none{}
+ error::None{}
);
return;
}
{
error::g_log << "Load key file " << key_file;
},
- error::none{}
+ error::None{}
);
return;
}
- context_t ctx{tls_server(), tls_free};
+ ContextPtr ctx{tls_server()};
if (!ctx)
{
- error::occurred("Create TLS server context", error::none{});
+ error::occurred("Create TLS server context", error::None{});
return;
}
}
-accepted_client::accepted_client(int server_socket, struct tls *server_ctx)
+AcceptedClient::AcceptedClient(int server_socket, struct tls *server_ctx)
{
struct sockaddr_in addr{};
socklen_t addrlen = sizeof(addr);
m_fd.reset(accept(server_socket, (struct sockaddr *)&addr, &addrlen));
if (!m_fd)
{
- error::occurred("Accept socket", error::print{});
+ error::occurred("Accept socket", error::Print{});
return;
}
struct tls *ctx = nullptr;
if (tls_accept_socket(server_ctx, &ctx, m_fd.get()) == -1)
{
- error::occurred("TLS accept", error::none{});
+ error::occurred("TLS accept", error::None{});
return;
}
}
-bool read_request(not_null<struct tls *> ctx, std::vector<char> &url)
+bool read_request(NotNull<struct tls *> ctx, std::vector<char> &url)
{
// <URL><CR><LF>
url.resize(gemini::MAX_REQUEST_LENGTH);
}
}
- error::occurred("Parse request", error::none{});
+ error::occurred("Parse request", error::None{});
return false;
}
-bool send_response(not_null<struct tls *> ctx, gemini::status_t status, span<const char> meta)
+bool send_response(NotNull<struct tls *> ctx, gemini::status_t status, Span<const char> meta)
{
// > <STATUS><SPACE><META><CR><LF>
std::array<char, status.size() + gemini::SPACE.size() + gemini::MAX_META_LENGTH + gemini::CRLF.size()> buff;
current = std::copy(meta.begin(), meta.end(), current);
current = std::copy(gemini::CRLF.cbegin(), gemini::CRLF.cend(), current);
- return send(ctx, span<char const>{&buff[0], static_cast<size_t>(current - buff.begin())});
+ return send(ctx, Span<char const>{&buff[0], static_cast<size_t>(current - buff.begin())});
}
-bool send(not_null<struct tls *> ctx, span<const char> buff)
+bool send(NotNull<struct tls *> ctx, Span<const char> buff)
{
ssize_t ret{0};
while (buff.size() > 0)
blob - 22a608f8129218141bf49b9cc068aad1526d29c9
blob + 4f66289ff2a7033b98164f09f8534ffe59199dba
--- vostok/transport.h
+++ vostok/transport.h
/** `struct tls *` smart pointer */
-typedef std::unique_ptr<struct tls, decltype(&tls_free)> context_t;
+using ContextPtrBase = std::unique_ptr<struct tls, decltype(&tls_free)>;
+class ContextPtr : public ContextPtrBase
+{
+public:
+ explicit ContextPtr(struct tls *p=nullptr) : ContextPtrBase{p, tls_free} {}
+};
/** Per-process initialization */
/* Create new TLS context for gemini server */
-void create_server(not_null<czstring> cert_file, not_null<czstring> key_file, context_t &ctx);
+void create_server(NotNull<czstring> cert_file, NotNull<czstring> key_file, ContextPtr &server_ctx);
/** Accept new client */
-class accepted_client
+class AcceptedClient
{
public:
- accepted_client(int server_socket, struct tls *server_ctx);
+ AcceptedClient(int server_socket, struct tls *server_ctx);
bool is_accepted() const { return m_is_accepted; }
struct tls *get_ctx() const { return m_ctx.get(); }
int get_fd() const { return m_fd.get(); }
private:
- unique_fd m_fd;
- context_t m_ctx{nullptr, tls_free};
+ UniqueFd m_fd;
+ ContextPtr m_ctx;
bool m_is_accepted{false};
};
/** Read genimi request and return url */
-bool read_request(not_null<struct tls *> ctx, std::vector<char> &url);
+bool read_request(NotNull<struct tls *> ctx, std::vector<char> &url);
/** Write gemini response */
-bool send_response(not_null<struct tls *> ctx, gemini::status_t status, span<const char> meta);
+bool send_response(NotNull<struct tls *> ctx, gemini::status_t status, Span<const char> meta);
/** Send raw bytes */
-bool send(not_null<struct tls *> ctx, span<const char> buff);
+bool send(NotNull<struct tls *> ctx, Span<const char> buff);
} // namespace transport
blob - 987e0eb0db0d3606adc002559eeaca9834890165
blob + fece1ef2319fa1e4f2a37f7d1bf7542634df0c27
--- vostok/utils.h
+++ vostok/utils.h
/** Non-copiable objects */
-struct non_copiable
+struct NonCopiable
{
- non_copiable() = default;
- non_copiable(const non_copiable &) = delete;
- non_copiable &operator =(const non_copiable &) = delete;
+ NonCopiable() = default;
+ NonCopiable(const NonCopiable &) = delete;
+ NonCopiable &operator =(const NonCopiable &) = delete;
};
/** Restricts a pointer to only hold non-null values. */
template <class T>
-class not_null
+class NotNull
{
public:
- not_null(T p) :m_p{p} { assert(m_p != nullptr); }
+ NotNull(T p) :m_p{p} { assert(m_p != nullptr); }
T get() const {return m_p;}
operator T() const {return m_p;}
// prevents compilation when someone attempts to assign a null pointer constant
- not_null(std::nullptr_t) = delete;
- not_null& operator=(std::nullptr_t) = delete;
+ NotNull(std::nullptr_t) = delete;
+ NotNull& operator=(std::nullptr_t) = delete;
// unwanted operators...pointers only point to single objects!
- not_null& operator++() = delete;
- not_null& operator--() = delete;
- not_null operator++(int) = delete;
- not_null operator--(int) = delete;
- not_null& operator+=(std::ptrdiff_t) = delete;
- not_null& operator-=(std::ptrdiff_t) = delete;
+ NotNull& operator++() = delete;
+ NotNull& operator--() = delete;
+ NotNull operator++(int) = delete;
+ NotNull operator--(int) = delete;
+ NotNull& operator+=(std::ptrdiff_t) = delete;
+ NotNull& operator-=(std::ptrdiff_t) = delete;
void operator[](std::ptrdiff_t) const = delete;
private:
};
-/** Simply std::span implementation for C++11 */
+/** Simply std::Span implementation for C++11 */
template<typename ElemT>
-class span
+class Span
{
public:
using element_type = ElemT;
using iterator = ElemT *;
- constexpr span() : m_p{nullptr}, m_count{0} {}
- constexpr span(element_type *p, std::size_t count) : m_p{count ? p : nullptr}, m_count{count} {}
+ constexpr Span() : m_p{nullptr}, m_count{0} {}
+ constexpr Span(element_type *p, std::size_t count) : m_p{count ? p : nullptr}, m_count{count} {}
template <std::size_t N>
- constexpr span(element_type (&arr)[N]) : m_p{arr}, m_count{N} {}
+ constexpr Span(element_type (&arr)[N]) : m_p{arr}, m_count{N} {}
template <std::size_t N>
- constexpr span(std::array<element_type, N> &arr) : m_p{arr.data()}, m_count{N} {}
+ constexpr Span(std::array<element_type, N> &arr) : m_p{arr.data()}, m_count{N} {}
constexpr std::size_t size() const {return m_count;}
constexpr iterator begin() const noexcept { return m_p; }
constexpr iterator end() const noexcept { return m_p + size(); }
- span<element_type> first(std::size_t count) const
+ Span<element_type> first(std::size_t count) const
{
assert(count <= m_count);
- return span<element_type>{m_p, count};
+ return Span<element_type>{m_p, count};
}
- span<element_type> subspan(std::size_t offset) const
+ Span<element_type> subspan(std::size_t offset) const
{
assert(offset <= m_count);
- return (offset < m_count) ? span<element_type>{m_p + offset, m_count - offset} : span<element_type>{};
+ return (offset < m_count) ? Span<element_type>{m_p + offset, m_count - offset} : Span<element_type>{};
}
element_type &operator[](std::size_t idx) const
};
-/** Remove null terminating character and return as span */
+/** Remove null terminating character and return as Span */
template <std::size_t N>
-constexpr span<const char> cut_null(const char (&arr)[N])
+constexpr Span<const char> cut_null(const char (&arr)[N])
{
static_assert(N > 0, "!(N > 0)");
- return span<const char>{arr, N - 1};
+ return Span<const char>{arr, N - 1};
}
/** Smart pointer for file descriptor */
-class unique_fd : non_copiable
+class UniqueFd : public NonCopiable
{
public:
- explicit unique_fd(int fd = -1) : m_fd(fd) {}
- ~unique_fd(){ reset();}
+ explicit UniqueFd(int fd = -1) : m_fd(fd) {}
+ ~UniqueFd(){ reset();}
operator bool() const {return m_fd != -1;}
blob - a5bb489e81d2f77428c326766a2fdcc78353f61a
blob + 1522a58db3d9e2729758ad19dda6d5cf73e2ba8f
--- vostok/vostok.cc
+++ vostok/vostok.cc
} // namespace meta
-void client_thread(const transport::accepted_client *accepted_client, int directory_fd)
+void client_thread(const transport::AcceptedClient *accepted_client, int directory_fd)
{
assert(accepted_client);
- std::unique_ptr<const transport::accepted_client> accepted_client_deleter{accepted_client};
+ std::unique_ptr<const transport::AcceptedClient> accepted_client_deleter{accepted_client};
std::vector<char> url;
if (!transport::read_request(accepted_client->get_ctx(), url))
break;
}
- unique_fd opened_file{};
+ UniqueFd opened_file{};
const auto open_file_result = open_file(directory_fd, url.data(), opened_file);
switch (open_file_result)
{
{
error::g_log << "Read file \"" << url.data() << "\"";
},
- error::print{}
+ error::Print{}
);
return;
}
const auto readed = static_cast<size_t>(ret);
if (readed)
{
- if (!transport::send(accepted_client->get_ctx(), span<const char>{buffer.data(), readed}))
+ if (!transport::send(accepted_client->get_ctx(), Span<const char>{buffer.data(), readed}))
return;
}
if (readed < buffer.size())
}
-bool server_loop(int server_socket, not_null<struct tls *>ctx, int directory_fd)
+bool server_loop(int server_socket, NotNull<struct tls *>server_ctx, int directory_fd)
{
error::g_log << "🚀 Vostok server listening..." << std::endl;
for (; ; )
{
if (listen(server_socket, 1024) == -1)
{
- error::occurred("Listen socket", error::print{});
+ error::occurred("Listen socket", error::Print{});
return false;
}
- std::unique_ptr<const transport::accepted_client> accepted_client{
- new transport::accepted_client(server_socket, ctx)
+ std::unique_ptr<const transport::AcceptedClient> accepted_client{
+ new transport::AcceptedClient(server_socket, server_ctx)
};
if (accepted_client->is_accepted())
{
try
{
std::thread{client_thread, accepted_client.get(), directory_fd}.detach();
- accepted_client.release(); // move ownership to thread
}
catch (const std::system_error &e)
{
}
);
}
+ accepted_client.release(); // move ownership to thread
}
}
return true;
/** C++ entry point */
-bool main(const command_line_arguments &args)
+bool main(const CommandLineArguments &args)
{
if (!transport::init())
return false;
- transport::context_t ctx{nullptr, tls_free};
- transport::create_server(args.m_cert_file, args.m_key_file, ctx);
- if (!ctx)
+ transport::ContextPtr server_ctx;
+ transport::create_server(args.cert_file, args.key_file, server_ctx);
+ if (!server_ctx)
return false;
- const unique_fd server_socket{socket(AF_INET, SOCK_STREAM, 0)};
+ const UniqueFd server_socket{socket(AF_INET, SOCK_STREAM, 0)};
if (!server_socket)
{
- error::occurred("Create socket", error::print{});
+ error::occurred("Create socket", error::Print{});
return false;
}
struct sockaddr_in sockaddr{};
sockaddr.sin_family = AF_INET;
- sockaddr.sin_addr.s_addr = inet_addr(args.m_addr);
- sockaddr.sin_port = htons(args.m_port);
+ sockaddr.sin_addr.s_addr = inet_addr(args.addr);
+ sockaddr.sin_port = htons(args.port);
if (bind(server_socket.get(), (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1)
{
error::occurred(
[&args]
{
- error::g_log << "Bind to " << args.m_addr << ":" << std::dec << args.m_port;
+ error::g_log << "Bind to " << args.addr << ":" << std::dec << args.port;
},
- error::print{}
+ error::Print{}
);
return false;
}
- return server_loop(server_socket.get(), ctx.get(), args.m_directory.get());
+ return server_loop(server_socket.get(), server_ctx.get(), args.directory.get());
}
int
main(int argc, char *argv[])
{
- vostok::command_line_arguments args;
- if (!args.parse(argc, argv))
+ vostok::CommandLineArguments args;
+ if (!vostok::parse_command_line_arguments(argc, argv, args))
return 1;
return vostok::main(args) ? 0 : 1;