feat(net): complete P2 M-NET-01..05 codec, pool, safe static open, redirects, and RFC 6455 WebSocket
This commit is contained in:
1 parent
700024a24d
commit
86d777eae6
14 files changed
+4346
-19
No files matched your search
+271
-1
@@ -1,9 +1,20 @@
|
||||
#include <Void/Net/Server.hpp>
|
||||
|
||||
#if !defined(_WIN32)
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cctype>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <format>
|
||||
#include <set>
|
||||
#include <system_error>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Net
|
||||
{
|
||||
@@ -440,6 +451,106 @@ namespace Void::Net
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
OpenedStaticFile::OpenedStaticFile(int fd,
|
||||
std::filesystem::path relative_path,
|
||||
std::string_view content_type,
|
||||
std::uint64_t size) noexcept
|
||||
: fd_(fd), relative_path_(std::move(relative_path)),
|
||||
content_type_(content_type), size_(size)
|
||||
{
|
||||
}
|
||||
|
||||
OpenedStaticFile::~OpenedStaticFile() noexcept { close(); }
|
||||
|
||||
OpenedStaticFile::OpenedStaticFile(OpenedStaticFile&& other) noexcept
|
||||
: fd_(std::exchange(other.fd_, -1)),
|
||||
relative_path_(std::move(other.relative_path_)),
|
||||
content_type_(std::exchange(other.content_type_, {})),
|
||||
size_(std::exchange(other.size_, 0))
|
||||
{
|
||||
}
|
||||
|
||||
OpenedStaticFile&
|
||||
OpenedStaticFile::operator=(OpenedStaticFile&& other) noexcept
|
||||
{
|
||||
if (this != &other) {
|
||||
close();
|
||||
fd_ = std::exchange(other.fd_, -1);
|
||||
relative_path_ = std::move(other.relative_path_);
|
||||
content_type_ = std::exchange(other.content_type_, {});
|
||||
size_ = std::exchange(other.size_, 0);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void OpenedStaticFile::close() noexcept
|
||||
{
|
||||
#if !defined(_WIN32)
|
||||
if (fd_ >= 0) {
|
||||
(void)::close(fd_);
|
||||
fd_ = -1;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Void::Result::Result<std::string>
|
||||
OpenedStaticFile::read_all(std::uint64_t max_bytes) const
|
||||
{
|
||||
#if !defined(_WIN32)
|
||||
if (fd_ < 0) {
|
||||
return Void::Result::fail(
|
||||
"Net::OpenedStaticFile: file descriptor is closed");
|
||||
}
|
||||
if (size_ > max_bytes) {
|
||||
return std::unexpected{make_net_error(
|
||||
NetErrorCode::payload_too_large,
|
||||
"Net::OpenedStaticFile: file size exceeds max_bytes")};
|
||||
}
|
||||
std::string out;
|
||||
out.reserve(static_cast<std::size_t>(size_));
|
||||
std::array<char, 32 * 1024> buf{};
|
||||
std::uint64_t offset = 0;
|
||||
for (;;) {
|
||||
const ssize_t n = ::pread(fd_, buf.data(), buf.size(),
|
||||
static_cast<off_t>(offset));
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
return std::unexpected{make_net_error(
|
||||
NetErrorCode::io_error,
|
||||
std::format("Net::OpenedStaticFile: pread failed: {}",
|
||||
std::strerror(errno)),
|
||||
std::error_code{errno, std::generic_category()})};
|
||||
}
|
||||
if (n == 0) {
|
||||
break;
|
||||
}
|
||||
const auto un = static_cast<std::uint64_t>(n);
|
||||
if (un > max_bytes || offset > max_bytes - un) {
|
||||
return std::unexpected{make_net_error(
|
||||
NetErrorCode::payload_too_large,
|
||||
"Net::OpenedStaticFile: read exceeds max_bytes")};
|
||||
}
|
||||
out.append(buf.data(), static_cast<std::size_t>(n));
|
||||
offset += un;
|
||||
}
|
||||
return out;
|
||||
#else
|
||||
(void)max_bytes;
|
||||
return Void::Result::fail(
|
||||
"Net::OpenedStaticFile: unsupported platform");
|
||||
#endif
|
||||
}
|
||||
|
||||
StaticFileResolver::DirHandle::~DirHandle() noexcept
|
||||
{
|
||||
#if !defined(_WIN32)
|
||||
if (fd >= 0) {
|
||||
(void)::close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Void::Result::Result<StaticFileResolver>
|
||||
StaticFileResolver::create(std::filesystem::path root,
|
||||
std::string index_file)
|
||||
@@ -458,8 +569,167 @@ namespace Void::Net
|
||||
return Void::Result::fail(
|
||||
"Net::StaticFileResolver: document root is not a directory");
|
||||
}
|
||||
std::shared_ptr<DirHandle> root_handle;
|
||||
#if !defined(_WIN32)
|
||||
const int dir_fd =
|
||||
::open(canonical_root.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
||||
if (dir_fd < 0) {
|
||||
return std::unexpected{make_net_error(
|
||||
NetErrorCode::io_error,
|
||||
std::format(
|
||||
"Net::StaticFileResolver: cannot open root directory: {}",
|
||||
std::strerror(errno)),
|
||||
std::error_code{errno, std::generic_category()})};
|
||||
}
|
||||
root_handle = std::make_shared<DirHandle>(dir_fd);
|
||||
#endif
|
||||
return StaticFileResolver{std::move(canonical_root),
|
||||
std::move(index_file)};
|
||||
std::move(index_file),
|
||||
std::move(root_handle)};
|
||||
}
|
||||
|
||||
Void::Result::Result<std::optional<OpenedStaticFile>>
|
||||
StaticFileResolver::open_safe(std::string_view request_path) const
|
||||
{
|
||||
#if !defined(_WIN32)
|
||||
if (!root_dir_ || root_dir_->fd < 0) {
|
||||
return Void::Result::fail(
|
||||
"Net::StaticFileResolver: root directory handle unavailable");
|
||||
}
|
||||
auto decoded = decode_url_path(request_path);
|
||||
if (!decoded) {
|
||||
return Void::Result::fail(std::move(decoded).error());
|
||||
}
|
||||
|
||||
const auto segments = split_path(*decoded);
|
||||
for (const auto& seg : segments) {
|
||||
if (seg == "." || seg == ".." ||
|
||||
seg.find('/') != std::string::npos) {
|
||||
return Void::Result::fail(
|
||||
"Net::StaticFileResolver: path traversal rejected");
|
||||
}
|
||||
}
|
||||
|
||||
struct ScopedFd {
|
||||
int fd{-1};
|
||||
bool owned{false};
|
||||
~ScopedFd() noexcept
|
||||
{
|
||||
if (owned && fd >= 0) {
|
||||
(void)::close(fd);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ScopedFd cur{root_dir_->fd, false};
|
||||
std::filesystem::path rel_path;
|
||||
|
||||
// Walk intermediate directories strictly via openat + O_NOFOLLOW.
|
||||
for (std::size_t i = 0; i + 1U < segments.size(); ++i) {
|
||||
const int next =
|
||||
::openat(cur.fd, segments[i].c_str(),
|
||||
O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
||||
if (next < 0) {
|
||||
const int err = errno;
|
||||
if (err == ENOENT || err == ENOTDIR) {
|
||||
return std::optional<OpenedStaticFile>{};
|
||||
}
|
||||
if (err == ELOOP) {
|
||||
return Void::Result::fail(
|
||||
"Net::StaticFileResolver: symlink directory rejected");
|
||||
}
|
||||
return std::unexpected{make_net_error(
|
||||
NetErrorCode::io_error,
|
||||
std::format("Net::StaticFileResolver: openat dir failed: "
|
||||
"{}",
|
||||
std::strerror(err)),
|
||||
std::error_code{err, std::generic_category()})};
|
||||
}
|
||||
if (cur.owned && cur.fd >= 0) {
|
||||
(void)::close(cur.fd);
|
||||
}
|
||||
cur.fd = next;
|
||||
cur.owned = true;
|
||||
rel_path /= segments[i];
|
||||
}
|
||||
|
||||
// Open leaf (or index_file_ if root or directory).
|
||||
int file_fd = -1;
|
||||
if (segments.empty()) {
|
||||
file_fd = ::openat(cur.fd, index_file_.c_str(),
|
||||
O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
|
||||
rel_path /= index_file_;
|
||||
} else {
|
||||
const std::string& leaf = segments.back();
|
||||
file_fd = ::openat(cur.fd, leaf.c_str(),
|
||||
O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
|
||||
if (file_fd >= 0) {
|
||||
struct stat st{};
|
||||
if (::fstat(file_fd, &st) == 0 && S_ISDIR(st.st_mode)) {
|
||||
const int idx_fd = ::openat(
|
||||
file_fd, index_file_.c_str(),
|
||||
O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
|
||||
(void)::close(file_fd);
|
||||
file_fd = idx_fd;
|
||||
rel_path /= leaf;
|
||||
rel_path /= index_file_;
|
||||
} else {
|
||||
rel_path /= leaf;
|
||||
}
|
||||
} else if (errno == EISDIR) {
|
||||
const int dir_fd =
|
||||
::openat(cur.fd, leaf.c_str(),
|
||||
O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
||||
if (dir_fd >= 0) {
|
||||
file_fd = ::openat(dir_fd, index_file_.c_str(),
|
||||
O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
|
||||
(void)::close(dir_fd);
|
||||
rel_path /= leaf;
|
||||
rel_path /= index_file_;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (file_fd < 0) {
|
||||
const int err = errno;
|
||||
if (err == ENOENT || err == ENOTDIR) {
|
||||
return std::optional<OpenedStaticFile>{};
|
||||
}
|
||||
if (err == ELOOP) {
|
||||
return Void::Result::fail(
|
||||
"Net::StaticFileResolver: symlink file rejected");
|
||||
}
|
||||
return std::unexpected{make_net_error(
|
||||
NetErrorCode::io_error,
|
||||
std::format("Net::StaticFileResolver: openat file failed: {}",
|
||||
std::strerror(err)),
|
||||
std::error_code{err, std::generic_category()})};
|
||||
}
|
||||
|
||||
struct stat st{};
|
||||
if (::fstat(file_fd, &st) != 0) {
|
||||
const int err = errno;
|
||||
(void)::close(file_fd);
|
||||
return std::unexpected{make_net_error(
|
||||
NetErrorCode::io_error,
|
||||
std::format("Net::StaticFileResolver: fstat failed: {}",
|
||||
std::strerror(err)),
|
||||
std::error_code{err, std::generic_category()})};
|
||||
}
|
||||
if (!S_ISREG(st.st_mode)) {
|
||||
(void)::close(file_fd);
|
||||
return std::optional<OpenedStaticFile>{};
|
||||
}
|
||||
|
||||
const auto ct = content_type_for(rel_path);
|
||||
return std::optional<OpenedStaticFile>{OpenedStaticFile{
|
||||
file_fd, std::move(rel_path), ct,
|
||||
static_cast<std::uint64_t>(st.st_size)}};
|
||||
#else
|
||||
(void)request_path;
|
||||
return Void::Result::fail(
|
||||
"Net::StaticFileResolver::open_safe: unsupported platform");
|
||||
#endif
|
||||
}
|
||||
|
||||
Void::Result::Result<std::optional<StaticFile>>
|
||||
|
||||
Reference in new issue
Block a user