Added podcast support, only from subsonic API for now, ref #726

This commit is contained in:
emeric
2025-09-13 15:04:13 +02:00
parent 1d584ebcc6
commit 932e7715f5
111 changed files with 4717 additions and 188 deletions
+1
View File
@@ -46,6 +46,7 @@ target_include_directories(lmscore PRIVATE
target_link_libraries(lmscore PRIVATE
PkgConfig::Config++
PkgConfig::Archive
OpenSSL::Crypto
)
target_link_libraries(lmscore PUBLIC
+2
View File
@@ -59,6 +59,8 @@ namespace lms::core::logging
return "MAIN";
case Module::METADATA:
return "METADATA";
case Module::PODCAST:
return "PODCAST";
case Module::REMOTE:
return "REMOTE";
case Module::SCROBBLING:
+1
View File
@@ -67,6 +67,7 @@ namespace lms::core
{ ".jpg", "image/jpeg" },
{ ".jpeg", "image/jpeg" },
{ ".png", "image/png" },
{ ".svg", "image/svg+xml" },
{ ".webp", "image/webp" },
};
+2 -9
View File
@@ -19,22 +19,15 @@
#include "core/Path.hpp"
#include <algorithm>
#include <array>
#include <cassert>
#include <unistd.h>
#include "core/ILogger.hpp"
#include "core/String.hpp"
namespace lms::core::pathUtils
{
bool ensureDirectory(const std::filesystem::path& dir)
{
if (std::filesystem::exists(dir))
return std::filesystem::is_directory(dir);
else
return std::filesystem::create_directory(dir);
}
bool hasFileAnyExtension(const std::filesystem::path& file, std::span<const std::filesystem::path> supportedExtensions)
{
const std::filesystem::path extension{ stringUtils::stringToLower(file.extension().c_str()) };
+121 -5
View File
@@ -20,6 +20,8 @@
#include "core/String.hpp"
#include <algorithm>
#include <charconv>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <utility>
@@ -133,6 +135,64 @@ namespace lms::core::stringUtils
res.push_back(str.substr(currentPos));
return res;
}
static std::optional<std::chrono::minutes> getRFC822ZoneOffset(std::string_view zoneStr)
{
if (zoneStr == "UT" || zoneStr == "GMT" || zoneStr == "Z") return std::chrono::hours{ 0 };
if (zoneStr == "EST") return -std::chrono::hours{ 5 };
if (zoneStr == "EDT") return -std::chrono::hours{ 4 };
if (zoneStr == "CST") return -std::chrono::hours{ 6 };
if (zoneStr == "CDT") return -std::chrono::hours{ 5 };
if (zoneStr == "MST") return -std::chrono::hours{ 7 };
if (zoneStr == "MDT") return -std::chrono::hours{ 6 };
if (zoneStr == "PST") return -std::chrono::hours{ 8 };
if (zoneStr == "PDT") return -std::chrono::hours{ 7 };
if (zoneStr.size() == 1)
{
const char c{ zoneStr[0] };
if (c == 'A') return -std::chrono::hours{ 1 };
if (c == 'M') return -std::chrono::hours{ 12 };
if (c == 'N') return std::chrono::hours{ 1 };
if (c == 'Y') return std::chrono::hours{ 12 };
return std::nullopt;
}
// (+/-)HHMM
if (zoneStr[0] != '+' && zoneStr[0] != '-')
return std::nullopt;
if (zoneStr.size() != 5)
return std::nullopt;
if (!std::all_of(std::cbegin(zoneStr) + 1, std::cend(zoneStr), [](char c) { return std::isdigit(c); }))
return std::nullopt;
int hours{};
const auto [p, ec]{ std::from_chars(zoneStr.data() + 1, zoneStr.data() + 3, hours) };
if (ec != std::errc())
return std::nullopt;
int minutes{};
const auto [p2, ec2] = std::from_chars(zoneStr.data() + 3, zoneStr.data() + 5, minutes);
if (ec2 != std::errc())
return std::nullopt;
std::chrono::minutes res{ std::chrono::hours{ hours } + std::chrono::minutes{ minutes } };
if (zoneStr[0] == '-')
res = -res;
return res;
}
std::optional<unsigned> getRFC822Month(std::string_view monthStr)
{
static const std::array<std::string_view, 12> months{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
const std::string_view* str{ std::find(std::cbegin(months), std::cend(months), monthStr) };
if (str == std::cend(months))
return {};
return std::distance(std::cbegin(months), str) + 1;
}
} // namespace details
template<>
@@ -435,9 +495,9 @@ namespace lms::core::stringUtils
return str.substr(str.length() - ending.length()) == ending;
}
std::optional<std::string> stringFromHex(const std::string& str)
std::optional<std::string> stringFromHex(std::string_view str)
{
static const char lut[]{ "0123456789ABCDEF" };
constexpr char lut[]{ "0123456789ABCDEF" };
if (str.length() % 2 != 0)
return std::nullopt;
@@ -445,13 +505,13 @@ namespace lms::core::stringUtils
std::string res;
res.reserve(str.length() / 2);
auto it{ std::cbegin(str) };
const char* it{ std::cbegin(str) };
while (it != std::cend(str))
{
unsigned val{};
auto itHigh{ std::lower_bound(std::cbegin(lut), std::cend(lut), std::toupper(*(it++))) };
auto itLow{ std::lower_bound(std::cbegin(lut), std::cend(lut), std::toupper(*(it++))) };
const char* itHigh{ std::lower_bound(std::cbegin(lut), std::cend(lut), std::toupper(*(it++))) };
const char* itLow{ std::lower_bound(std::cbegin(lut), std::cend(lut), std::toupper(*(it++))) };
if (itHigh == std::cend(lut) || itLow == std::cend(lut))
return {};
@@ -465,6 +525,21 @@ namespace lms::core::stringUtils
return res;
}
std::string toHexString(std::string_view str)
{
constexpr char lut[]{ "0123456789ABCDEF" };
std::string res;
for (char c : str)
{
res.push_back(lut[(c >> 4) & 0xF]);
res.push_back(lut[c & 0xF]);
}
return res;
}
std::string toISO8601String(const Wt::WDateTime& dateTime)
{
if (dateTime.isValid())
@@ -496,6 +571,47 @@ namespace lms::core::stringUtils
return Wt::WDateTime::fromString(Wt::WString{ std::string{ dateTime } }, "yyyy-MM-ddThh:mm:ss.zzz");
}
Wt::WDateTime fromRFC822String(std::string_view dateTime)
{
// Expect something like "[Sun,] 6 Nov 1994 08:49[:37] GMT"
if (dateTime.size() > 4 && dateTime[3] == ',')
dateTime.remove_prefix(5);
// Extract parts
const std::vector<std::string_view> subParts{ splitString(dateTime, ' ') };
if (subParts.size() != 5)
return {};
const std::string_view dayStr{ subParts[0] };
const std::string_view monthStr{ subParts[1] };
const std::string_view yearStr{ subParts[2] };
std::string timeStr{ subParts[3] };
const std::string_view zoneStr{ subParts[4] };
if (std::count(std::cbegin(timeStr), std::cend(timeStr), ':') == 1)
timeStr += ":00";
// Normalize zone
const std::optional<std::chrono::minutes> offset{ details::getRFC822ZoneOffset(zoneStr) };
if (!offset)
return {};
std::optional<unsigned> month{ details::getRFC822Month(monthStr) };
if (!month)
return {};
std::string datetimeStr;
datetimeStr = dayStr;
datetimeStr += " ";
datetimeStr += std::to_string(month.value());
datetimeStr += " ";
datetimeStr += yearStr;
datetimeStr += " ";
datetimeStr += timeStr;
const Wt::WDateTime res{ Wt::WDateTime::fromString(Wt::WString{ datetimeStr }, "d M yyyy HH:mm:ss") };
return res.addSecs(static_cast<int>(std::chrono::duration_cast<std::chrono::seconds>(offset.value()).count()));
}
std::string formatTimestamp(std::chrono::milliseconds timestamp)
{
using namespace std::chrono;
+5
View File
@@ -36,4 +36,9 @@ namespace lms::core::http
{
_sendQueue.sendRequest(std::make_unique<ClientRequest>(std::move(POSTParams)));
}
void Client::abortAllRequests()
{
_sendQueue.abortAllRequests();
}
} // namespace lms::core::http
+1
View File
@@ -40,6 +40,7 @@ namespace lms::core::http
private:
void sendGETRequest(ClientGETRequestParameters&& request) override;
void sendPOSTRequest(ClientPOSTRequestParameters&& request) override;
void abortAllRequests() override;
SendQueue _sendQueue;
};
+132 -30
View File
@@ -19,16 +19,19 @@
#include "SendQueue.hpp"
#include <latch>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/ssl/error.hpp>
#include "core/Exception.hpp"
#include "core/ILogger.hpp"
#include "core/ITraceLogger.hpp"
#include "core/String.hpp"
#define LOG(sev, message) LMS_LOG(SCROBBLING, sev, "[Http SendQueue] - " << message)
#define LOG(sev, message) LMS_LOG(HTTP, sev, "[Http SendQueue] - " << message)
namespace lms::core::stringUtils
{
@@ -63,7 +66,21 @@ namespace lms::core::http
SendQueue::SendQueue(boost::asio::io_context& ioContext, std::string_view baseUrl)
: _ioContext{ ioContext }
, _baseUrl{ baseUrl }
, _abortAllRequests{ false }
, _state{ State::Idle }
, _client{ _ioContext }
{
_client.setFollowRedirect(true);
_client.setTimeout(std::chrono::seconds{ 5 });
// not very efficient (response bodies are copied for each callback), but Wt's code already makes copies anyway
_client.bodyDataReceived().connect([this](const std::string& data) {
boost::asio::post(boost::asio::bind_executor(_strand, [this, data] {
onClientBodyDataReceived(data);
}));
});
_client.done().connect([this](Wt::AsioWrapper::error_code ec, const Wt::Http::Message& msg) {
boost::asio::post(boost::asio::bind_executor(_strand, [this, ec, msg = std::move(msg)] {
onClientDone(ec, msg);
@@ -73,12 +90,60 @@ namespace lms::core::http
SendQueue::~SendQueue()
{
_client.abort();
abortAllRequests();
}
void SendQueue::abortAllRequests()
{
LOG(DEBUG, "Aborting all requests...");
assert(!_abortAllRequests);
_abortAllRequests = true;
std::latch abortLatch{ 1 };
boost::asio::post(boost::asio::bind_executor(_strand, [this, &abortLatch] {
for (auto& [prio, requests] : _sendQueue)
{
while (!requests.empty())
{
std::unique_ptr<ClientRequest> request{ std::move(requests.front()) };
requests.pop_front();
if (request->getParameters().onAbortFunc)
request->getParameters().onAbortFunc();
}
}
if (_state == State::Throttled)
_throttleTimer.cancel();
else if (_state == State::Sending)
_client.abort();
abortLatch.count_down();
}));
abortLatch.wait();
while (_state != State::Idle)
std::this_thread::yield();
_abortAllRequests = false;
LOG(DEBUG, "All requests aborted!");
}
void SendQueue::sendRequest(std::unique_ptr<ClientRequest> request)
{
boost::asio::dispatch(_strand, [this, request = std::move(request)]() mutable {
boost::asio::post(_strand, [this, request = std::move(request)]() mutable {
if (_abortAllRequests)
{
LOG(DEBUG, "Not posting request because abortAllRequests() in progress");
if (request->getParameters().onAbortFunc)
request->getParameters().onAbortFunc();
return;
}
_sendQueue[request->getParameters().priority].emplace_back(std::move(request));
if (_state == State::Idle)
@@ -88,7 +153,7 @@ namespace lms::core::http
void SendQueue::sendNextQueuedRequest()
{
assert(_state == State::Idle);
assert(_strand.running_in_this_thread());
assert(!_currentRequest);
for (auto& [prio, requests] : _sendQueue)
@@ -100,21 +165,31 @@ namespace lms::core::http
requests.pop_front();
if (!sendRequest(*request))
{
if (request->getParameters().onFailureFunc)
request->getParameters().onFailureFunc();
continue;
}
_state = State::Sending;
setState(State::Sending);
_currentRequest = std::move(request);
return;
}
}
setState(State::Idle);
}
bool SendQueue::sendRequest(const ClientRequest& request)
{
assert(_strand.running_in_this_thread());
LMS_SCOPED_TRACE_DETAILED("SendQueue", "SendRequest");
std::string url{ _baseUrl + request.getParameters().relativeUrl };
LOG(DEBUG, "Sending request to url '" << url << "'");
const std::string url{ _baseUrl + request.getParameters().relativeUrl };
LOG(DEBUG, "Sending " << (request.getType() == ClientRequest::Type::GET ? "GET" : "POST") << " request to url '" << url << "'");
_client.setMaximumResponseSize(request.getParameters().onChunkReceived ? 0 : request.getParameters().responseBufferSize);
bool res{};
switch (request.getType())
@@ -134,29 +209,50 @@ namespace lms::core::http
return res;
}
void SendQueue::onClientBodyDataReceived(const std::string& data)
{
assert(_strand.running_in_this_thread());
assert(_currentRequest);
if (_currentRequest->getParameters().onChunkReceived)
{
const auto byteSpan{ std::as_bytes(std::span{ data.data(), data.size() }) };
if (_currentRequest->getParameters().onChunkReceived(byteSpan) == ClientRequestParameters::ChunckReceivedResult::Abort)
_client.abort();
}
}
void SendQueue::onClientDone(Wt::AsioWrapper::error_code ec, const Wt::Http::Message& msg)
{
LMS_SCOPED_TRACE_DETAILED("SendQueue", "OnClientDone");
if (ec == boost::asio::error::operation_aborted)
{
LOG(DEBUG, "Client aborted");
return;
}
assert(_currentRequest);
_state = State::Idle;
LOG(DEBUG, "Client done. status = " << msg.status());
if (ec)
LOG(DEBUG, "Client done. ec = " << ec.category().name() << " - " << ec.message() << " (" << ec.value() << "), status = " << msg.status());
if (_abortAllRequests || ec == boost::asio::error::operation_aborted)
onClientAborted(std::move(_currentRequest));
else if (ec && (ec != boost::asio::ssl::error::stream_truncated))
onClientDoneError(std::move(_currentRequest), ec);
else
onClientDoneSuccess(std::move(_currentRequest), msg);
}
void SendQueue::onClientAborted(std::unique_ptr<ClientRequest> request)
{
assert(_strand.running_in_this_thread());
if (request->getParameters().onAbortFunc)
request->getParameters().onAbortFunc();
sendNextQueuedRequest();
}
void SendQueue::onClientDoneError(std::unique_ptr<ClientRequest> request, Wt::AsioWrapper::error_code ec)
{
LOG(ERROR, "Retry " << request->retryCount << ", client error: '" << ec.message() << "'");
assert(_strand.running_in_this_thread());
LOG(WARNING, "Retry " << request->retryCount << ", client error: '" << ec.message() << "'");
// may be a network error, try again later
throttle(_defaultRetryWaitDuration);
@@ -196,42 +292,48 @@ namespace lms::core::http
if (msg.status() == 200)
{
if (requestParameters.onSuccessFunc)
requestParameters.onSuccessFunc(msg.body());
requestParameters.onSuccessFunc(msg);
}
else
{
LOG(ERROR, "Send error: '" << msg.body() << "'");
LOG(ERROR, "Send error, status = " << msg.status() << ", body = '" << msg.body() << "'");
if (requestParameters.onFailureFunc)
requestParameters.onFailureFunc();
}
}
if (_state == State::Idle)
if (_state != State::Throttled)
sendNextQueuedRequest();
}
void SendQueue::throttle(std::chrono::seconds requestedDuration)
{
assert(_state == State::Idle);
const std::chrono::seconds duration{ clamp(requestedDuration, _minRetryWaitDuration, _maxRetryWaitDuration) };
LOG(DEBUG, "Throttling for " << duration.count() << " seconds");
_throttleTimer.expires_after(duration);
_throttleTimer.async_wait([this](const boost::system::error_code& ec) {
if (ec == boost::asio::error::operation_aborted)
{
LOG(DEBUG, "Throttle aborted");
return;
}
else if (ec)
{
throw LmsException{ "Throttle timer failure: " + std::string{ ec.message() } };
}
_state = State::Idle;
sendNextQueuedRequest();
setState(State::Idle);
if (!ec)
sendNextQueuedRequest();
});
_state = State::Throttled;
setState(State::Throttled);
}
void SendQueue::setState(State state)
{
assert(_strand.running_in_this_thread());
if (_state != state)
{
LOG(DEBUG, "Changing state to " << (state == State::Idle ? "Idle" : state == State::Sending ? "Sending" :
"Throttled"));
_state = state;
}
}
} // namespace lms::core::http
+10 -6
View File
@@ -19,9 +19,9 @@
#pragma once
#include <atomic>
#include <deque>
#include <string_view>
#include <vector>
#include <Wt/Http/Client.h>
#include <boost/asio/io_context.hpp>
@@ -44,10 +44,13 @@ namespace lms::core::http
SendQueue& operator=(const SendQueue&&) = delete;
void sendRequest(std::unique_ptr<ClientRequest> request);
void abortAllRequests();
private:
void sendNextQueuedRequest();
bool sendRequest(const ClientRequest& request);
void onClientBodyDataReceived(const std::string& data);
void onClientAborted(std::unique_ptr<ClientRequest> request);
void onClientDone(Wt::AsioWrapper::error_code ec, const Wt::Http::Message& msg);
void onClientDoneError(std::unique_ptr<ClientRequest> request, Wt::AsioWrapper::error_code ec);
void onClientDoneSuccess(std::unique_ptr<ClientRequest> request, const Wt::Http::Message& msg);
@@ -59,9 +62,9 @@ namespace lms::core::http
const std::chrono::seconds _maxRetryWaitDuration{ 300 };
boost::asio::io_context& _ioContext;
boost::asio::io_context::strand _strand{ _ioContext };
boost::asio::io_context::strand _strand{ _ioContext }; // protect _state, _sendQueue and _currentRequest
boost::asio::steady_timer _throttleTimer{ _ioContext };
std::string _baseUrl;
const std::string _baseUrl;
enum class State
{
@@ -69,10 +72,11 @@ namespace lms::core::http
Throttled,
Sending,
};
State _state{ State::Idle };
Wt::Http::Client _client{ _ioContext };
void setState(State state);
std::atomic<bool> _abortAllRequests;
State _state;
Wt::Http::Client _client;
std::map<ClientRequestParameters::Priority, std::deque<std::unique_ptr<ClientRequest>>> _sendQueue;
std::unique_ptr<ClientRequest> _currentRequest;
};
} // namespace lms::core::http
+1
View File
@@ -51,6 +51,7 @@ namespace lms::core::logging
HTTP,
MAIN,
METADATA,
PODCAST,
REMOTE,
SCROBBLING,
SERVICE,
-6
View File
@@ -24,14 +24,8 @@
#include <string>
#include <string_view>
#include <Wt/WDateTime.h>
namespace lms::core::pathUtils
{
// Make sure the given path is a directory
// Create it if needed
bool ensureDirectory(const std::filesystem::path& dir);
// Check if file's extension is one of provided extensions
bool hasFileAnyExtension(const std::filesystem::path& file, std::span<const std::filesystem::path> extensions);
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstddef>
namespace lms::core::literals
{
constexpr std::size_t operator""_KiB(unsigned long long int x)
{
return 1024ULL * x;
}
constexpr std::size_t operator""_MiB(unsigned long long int x)
{
return 1024_KiB * x;
}
constexpr std::size_t operator""_GiB(unsigned long long int x)
{
return 1024_MiB * x;
}
constexpr std::size_t operator""_TiB(unsigned long long int x)
{
return 1024_GiB * x;
}
constexpr std::size_t operator""_PiB(unsigned long long int x)
{
return 1024_TiB * x;
}
} // namespace lms::core::literals
+3 -1
View File
@@ -110,12 +110,14 @@ namespace lms::core::stringUtils
[[nodiscard]] bool stringEndsWith(std::string_view str, std::string_view ending);
[[nodiscard]] std::optional<std::string> stringFromHex(const std::string& str);
[[nodiscard]] std::optional<std::string> stringFromHex(std::string_view str);
[[nodiscard]] std::string toHexString(std::string_view str);
[[nodiscard]] std::string toISO8601String(const Wt::WDateTime& dateTime);
[[nodiscard]] std::string toISO8601String(const Wt::WDate& date);
[[nodiscard]] Wt::WDateTime fromISO8601String(std::string_view dateTime);
[[nodiscard]] Wt::WDateTime fromRFC822String(std::string_view dateTime);
// to "[minutes:seconds.milliseconds]"
std::string formatTimestamp(std::chrono::milliseconds timestamp);
@@ -19,8 +19,9 @@
#pragma once
#include <cstddef>
#include <functional>
#include <string_view>
#include <span>
#include <vector>
#include <Wt/Http/Message.h>
@@ -37,13 +38,28 @@ namespace lms::core::http
};
Priority priority{ Priority::Normal };
std::string relativeUrl; // relative to baseUrl used by the client
std::string relativeUrl; // relative to baseUrl used by the client
std::size_t responseBufferSize{ 10 * 1024 * 1024 }; // only used if onChunkReceived is not set
using OnSuccessFunc = std::function<void(std::string_view msgBody)>;
// If `onChunkReceived` is set, the response will be streamed in chunks.
// In that case, `onSuccessFunc` is still called at the end (with an empty msgBody).
// If `onChunkReceived` is not set, the response will be fully buffered and passed to `onSuccessFunc`.
enum class ChunckReceivedResult
{
Continue,
Abort,
};
using OnChunkReceived = std::function<ChunckReceivedResult(std::span<const std::byte> chunk)>; // return false to stop (onFailureFunc callback will be called)
OnChunkReceived onChunkReceived;
using OnSuccessFunc = std::function<void(const Wt::Http::Message& msg)>;
OnSuccessFunc onSuccessFunc;
using OnFailureFunc = std::function<void()>;
OnFailureFunc onFailureFunc;
using OnAbortFunc = std::function<void()>;
OnAbortFunc onAbortFunc;
};
struct ClientGETRequestParameters final : public ClientRequestParameters
@@ -27,6 +27,8 @@
namespace lms::core::http
{
// Very simple http client, will handle all requests sequentially.
// User callbacks are dispatched within a strand.
class IClient
{
public:
@@ -34,6 +36,8 @@ namespace lms::core::http
virtual void sendGETRequest(ClientGETRequestParameters&& request) = 0;
virtual void sendPOSTRequest(ClientPOSTRequestParameters&& request) = 0;
virtual void abortAllRequests() = 0;
};
std::unique_ptr<IClient> createClient(boost::asio::io_context& ioContext, std::string_view baseUrl);
+30
View File
@@ -338,6 +338,20 @@ namespace lms::core::stringUtils::tests
EXPECT_EQ(fromISO8601String(""), Wt::WDateTime{});
}
TEST(Stringutils, DateTimeFromRFC822String)
{
EXPECT_EQ(fromRFC822String(""), Wt::WDateTime{});
EXPECT_EQ(fromRFC822String("Mon, 3 Jan 2020 09:08:11 UT"), (Wt::WDateTime{ Wt::WDate{ 2020, 01, 03 }, Wt::WTime{ 9, 8, 11, 0 } }));
EXPECT_EQ(fromRFC822String("3 Jan 2020 09:08:11 UT"), (Wt::WDateTime{ Wt::WDate{ 2020, 01, 03 }, Wt::WTime{ 9, 8, 11, 0 } }));
EXPECT_EQ(fromRFC822String("3 Jan 2020 09:08:11 +0200"), (Wt::WDateTime{ Wt::WDate{ 2020, 01, 03 }, Wt::WTime{ 11, 8, 11, 0 } }));
EXPECT_EQ(fromRFC822String("3 Jan 2020 09:08 +0200"), (Wt::WDateTime{ Wt::WDate{ 2020, 01, 03 }, Wt::WTime{ 11, 8, 00, 0 } }));
EXPECT_EQ(fromRFC822String("3 Jan 2020 09:08:11 -0200"), (Wt::WDateTime{ Wt::WDate{ 2020, 01, 03 }, Wt::WTime{ 7, 8, 11, 0 } }));
EXPECT_EQ(fromRFC822String("3 Jan 2020 01:08:11 -0200"), (Wt::WDateTime{ Wt::WDate{ 2020, 01, 02 }, Wt::WTime{ 23, 8, 11, 0 } }));
EXPECT_EQ(fromRFC822String("3 Jan 2020 01:08:11 -0230"), (Wt::WDateTime{ Wt::WDate{ 2020, 01, 02 }, Wt::WTime{ 22, 38, 11, 0 } }));
EXPECT_EQ(fromRFC822String("3 Jan 2020 10:08:11 CST"), (Wt::WDateTime{ Wt::WDate{ 2020, 01, 03 }, Wt::WTime{ 04, 8, 11, 0 } }));
EXPECT_EQ(fromRFC822String("Sat, 09 Aug 2025 21:34:32 +0200"), (Wt::WDateTime{ Wt::WDate{ 2025, 8, 9 }, Wt::WTime{ 23, 34, 32 } }));
}
TEST(StringUtils, stringEndsWith)
{
EXPECT_TRUE(stringEndsWith("FooBar", "Bar"));
@@ -361,4 +375,20 @@ namespace lms::core::stringUtils::tests
EXPECT_TRUE(stringCaseInsensitiveContains("", ""));
EXPECT_FALSE(stringCaseInsensitiveContains("", "Foo"));
}
TEST(StringUtils, toHexString)
{
EXPECT_EQ(toHexString(""), "");
EXPECT_EQ(toHexString("123"), "313233");
EXPECT_EQ(toHexString("1234"), "31323334");
EXPECT_EQ(toHexString("12345"), "3132333435");
EXPECT_EQ(toHexString("Test"), "54657374");
// test back stringFromHex
EXPECT_EQ(stringFromHex(""), "");
EXPECT_EQ(stringFromHex("313233"), "123");
EXPECT_EQ(stringFromHex("31323334"), "1234");
EXPECT_EQ(stringFromHex("3132333435"), "12345");
EXPECT_EQ(stringFromHex("54657374"), "Test");
}
} // namespace lms::core::stringUtils::tests