diff options
Diffstat (limited to 'src/send_file.cc')
| -rw-r--r-- | src/send_file.cc | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/send_file.cc b/src/send_file.cc new file mode 100644 index 0000000..8611b6f --- /dev/null +++ b/src/send_file.cc @@ -0,0 +1,45 @@ +#include "common.hh" + +#include "config.hh" +#include "send_file.hh" + +namespace { + +class SendFileImpl : public SendFile { +public: + bool setup(Logger*, Config* config, + std::string_view sendfile_header_name, + std::string_view sendfile_path_name) override { + header_ = config->get(std::string(sendfile_header_name), ""); + path_ = config->get(std::string(sendfile_path_name), ""); + return true; + } + + std::unique_ptr<Transport::Response> create_ok_file( + Transport* transport, + std::filesystem::path const& full_path, std::string_view relative_path, + std::string_view etag, std::optional<uint64_t> size) override { + if (header_.empty()) { + auto resp = transport->create_ok_file(full_path); + if (!etag.empty()) + resp->add_header("ETag", std::string(etag)); + if (size.has_value()) + resp->add_header("Content-Length", std::to_string(size.value())); + return resp; + } + + auto resp = transport->create_ok_data(""); + resp->add_header(header_, path_ + std::string(relative_path)); + return resp; + } + +private: + std::string header_; + std::string path_; +}; + +} // namespace + +std::unique_ptr<SendFile> SendFile::create() { + return std::make_unique<SendFileImpl>(); +} |
