diff --git a/src/common/download.cpp b/src/common/download.cpp index 01fb44c63..859c80219 100644 --- a/src/common/download.cpp +++ b/src/common/download.cpp @@ -125,10 +125,9 @@ namespace tools { // we requested a range, so check if we're getting it, otherwise truncate bool got_range = false; - const std::string prefix = "bytes=" + std::to_string(offset) + "-"; for (const auto &kv: headers.m_header_info.m_etc_fields) { - if (kv.first == "Content-Range" && strncmp(kv.second.c_str(), prefix.c_str(), prefix.size())) + if (kv.first == "Content-Range" && tools::content_range_starts_at(kv.second, offset)) { got_range = true; break; diff --git a/src/common/download.h b/src/common/download.h index d09195c22..8efd29a33 100644 --- a/src/common/download.h +++ b/src/common/download.h @@ -28,6 +28,7 @@ #pragma once +#include #include #include #include @@ -37,6 +38,12 @@ namespace tools struct download_thread_control; typedef std::shared_ptr download_async_handle; + inline bool content_range_starts_at(const std::string &content_range, uint64_t offset) + { + const std::string prefix = "bytes " + std::to_string(offset) + "-"; + return content_range.compare(0, prefix.size(), prefix) == 0; + } + bool download(const std::string &path, const std::string &url, std::function progress = NULL); download_async_handle download_async(const std::string &path, const std::string &url, std::function result, std::function progress = NULL); bool download_error(const download_async_handle &h); diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index a86676fad..761a6593b 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -47,6 +47,7 @@ set(unit_tests_sources device.cpp difficulty.cpp dns_resolver.cpp + download.cpp epee_boosted_tcp_server.cpp epee_levin_protocol_handler_async.cpp epee_http_server.cpp diff --git a/tests/unit_tests/download.cpp b/tests/unit_tests/download.cpp new file mode 100644 index 000000000..c76f10dc1 --- /dev/null +++ b/tests/unit_tests/download.cpp @@ -0,0 +1,14 @@ +#include "common/download.h" + +#include "gtest/gtest.h" + +TEST(download, content_range_starts_at) +{ + EXPECT_TRUE(tools::content_range_starts_at("bytes 1024-2047/4096", 1024)); + EXPECT_TRUE(tools::content_range_starts_at("bytes 1024-", 1024)); + + EXPECT_FALSE(tools::content_range_starts_at("bytes 0-2047/4096", 1024)); + EXPECT_FALSE(tools::content_range_starts_at("bytes 10240-20479/40960", 1024)); + EXPECT_FALSE(tools::content_range_starts_at("bytes=1024-2047/4096", 1024)); + EXPECT_FALSE(tools::content_range_starts_at("", 1024)); +}