# HG changeset patch # User Sebastien Jodogne # Date 1779185461 -7200 # Node ID 6df5cb04dd7f43ad189306be291d298a5a8ab6f3 # Parent 68d9bad294115d1bad22770c16d55d1301e4c4a3 renamed StorageAccessor::Range as StorageRange diff -r 68d9bad29411 -r 6df5cb04dd7f OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake --- a/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake Tue May 19 10:52:00 2026 +0200 +++ b/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake Tue May 19 12:11:01 2026 +0200 @@ -173,6 +173,7 @@ ${CMAKE_CURRENT_LIST_DIR}/../../Sources/FileStorage/FileInfo.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/FileStorage/MemoryStorageArea.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/FileStorage/PluginStorageAreaAdapter.cpp + ${CMAKE_CURRENT_LIST_DIR}/../../Sources/FileStorage/StorageRange.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/HttpServer/CStringMatcher.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/HttpServer/HttpContentNegociation.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/HttpServer/HttpToolbox.cpp diff -r 68d9bad29411 -r 6df5cb04dd7f OrthancFramework/Sources/FileStorage/StorageAccessor.cpp --- a/OrthancFramework/Sources/FileStorage/StorageAccessor.cpp Tue May 19 10:52:00 2026 +0200 +++ b/OrthancFramework/Sources/FileStorage/StorageAccessor.cpp Tue May 19 12:11:01 2026 +0200 @@ -53,212 +53,6 @@ namespace Orthanc { - void StorageAccessor::Range::SanityCheck() const - { - if (hasStart_ && hasEnd_ && start_ > end_) - { - throw OrthancException(ErrorCode_BadSequenceOfCalls); - } - } - - StorageAccessor::Range::Range(): - hasStart_(false), - start_(0), - hasEnd_(false), - end_(0) - { - } - - void StorageAccessor::Range::SetStartInclusive(uint64_t start) - { - hasStart_ = true; - start_ = start; - } - - void StorageAccessor::Range::SetEndInclusive(uint64_t end) - { - hasEnd_ = true; - end_ = end; - } - - uint64_t StorageAccessor::Range::GetStartInclusive() const - { - if (!hasStart_) - { - throw OrthancException(ErrorCode_BadSequenceOfCalls); - } - else if (hasEnd_ && start_ > end_) - { - throw OrthancException(ErrorCode_BadSequenceOfCalls); - } - else - { - return start_; - } - } - - uint64_t StorageAccessor::Range::GetEndInclusive() const - { - if (!hasEnd_) - { - throw OrthancException(ErrorCode_BadSequenceOfCalls); - } - else if (hasStart_ && start_ > end_) - { - throw OrthancException(ErrorCode_BadSequenceOfCalls); - } - else - { - return end_; - } - } - - std::string StorageAccessor::Range::FormatHttpContentRange(uint64_t fullSize) const - { - SanityCheck(); - - if (fullSize == 0 || - (hasStart_ && start_ >= fullSize) || - (hasEnd_ && end_ >= fullSize)) - { - throw OrthancException(ErrorCode_BadRange); - } - - std::string s = "bytes "; - - if (hasStart_) - { - s += boost::lexical_cast(start_); - } - else - { - s += "0"; - } - - s += "-"; - - if (hasEnd_) - { - s += boost::lexical_cast(end_); - } - else - { - s += boost::lexical_cast(fullSize - 1); - } - - return s + "/" + boost::lexical_cast(fullSize); - } - - void StorageAccessor::Range::Extract(std::string &target, - const std::string &source) const - { - SanityCheck(); - - if (hasStart_ && start_ >= source.size()) - { - throw OrthancException(ErrorCode_BadRange); - } - - if (hasEnd_ && end_ >= source.size()) - { - throw OrthancException(ErrorCode_BadRange); - } - - if (hasStart_ && hasEnd_) - { - target = source.substr(start_, end_ - start_ + 1); - } - else if (hasStart_) - { - target = source.substr(start_, source.size() - start_); - } - else if (hasEnd_) - { - target = source.substr(0, end_ + 1); - } - else - { - target = source; - } - } - - uint64_t StorageAccessor::Range::GetContentLength(uint64_t fullSize) const - { - SanityCheck(); - - if (fullSize == 0) - { - throw OrthancException(ErrorCode_BadRange); - } - - if (hasStart_ && start_ >= fullSize) - { - throw OrthancException(ErrorCode_BadRange); - } - - if (hasEnd_ && end_ >= fullSize) - { - throw OrthancException(ErrorCode_BadRange); - } - - if (hasStart_ && hasEnd_) - { - return end_ - start_ + 1; - } - else if (hasStart_) - { - return fullSize - start_; - } - else if (hasEnd_) - { - return end_ + 1; - } - else - { - return fullSize; - } - } - - StorageAccessor::Range StorageAccessor::Range::ParseHttpRange(const std::string& s) - { - static const std::string BYTES = "bytes="; - - if (!boost::starts_with(s, BYTES)) - { - throw OrthancException(ErrorCode_BadRange); // Range not satisfiable - } - - std::vector tokens; - Orthanc::Toolbox::TokenizeString(tokens, s.substr(BYTES.length()), '-'); - - if (tokens.size() != 2) - { - throw OrthancException(ErrorCode_BadRange); - } - - Range range; - - uint64_t tmp; - if (!tokens[0].empty()) - { - if (SerializationToolbox::ParseUnsignedInteger64(tmp, tokens[0])) - { - range.SetStartInclusive(tmp); - } - } - - if (!tokens[1].empty()) - { - if (SerializationToolbox::ParseUnsignedInteger64(tmp, tokens[1])) - { - range.SetEndInclusive(tmp); - } - } - - range.SanityCheck(); - return range; - } - class StorageAccessor::MetricsTimer : public boost::noncopyable { private: @@ -644,7 +438,7 @@ void StorageAccessor::ReadRange(std::string &target, const FileInfo &info, - const Range &range, + const StorageRange &range, bool uncompressIfNeeded) { if (uncompressIfNeeded && diff -r 68d9bad29411 -r 6df5cb04dd7f OrthancFramework/Sources/FileStorage/StorageAccessor.h --- a/OrthancFramework/Sources/FileStorage/StorageAccessor.h Tue May 19 10:52:00 2026 +0200 +++ b/OrthancFramework/Sources/FileStorage/StorageAccessor.h Tue May 19 12:11:01 2026 +0200 @@ -40,8 +40,9 @@ # error Macro ORTHANC_ENABLE_MONGOOSE must be defined to use this file #endif +#include "FileInfo.h" #include "IStorageArea.h" -#include "FileInfo.h" +#include "StorageRange.h" #if ORTHANC_ENABLE_CIVETWEB == 1 || ORTHANC_ENABLE_MONGOOSE == 1 # include "../HttpServer/BufferHttpSender.h" @@ -49,9 +50,7 @@ #endif #include -#include #include -#include namespace Orthanc { @@ -65,48 +64,6 @@ **/ class ORTHANC_PUBLIC StorageAccessor : boost::noncopyable { - public: - class ORTHANC_PUBLIC Range - { - private: - bool hasStart_; - uint64_t start_; - bool hasEnd_; - uint64_t end_; - - void SanityCheck() const; - - public: - Range(); - - void SetStartInclusive(uint64_t start); - - void SetEndInclusive(uint64_t end); - - bool HasStart() const - { - return hasStart_; - } - - bool HasEnd() const - { - return hasEnd_; - } - - uint64_t GetStartInclusive() const; - - uint64_t GetEndInclusive() const; - - std::string FormatHttpContentRange(uint64_t fullSize) const; - - void Extract(std::string& target, - const std::string& source) const; - - uint64_t GetContentLength(uint64_t fullSize) const; - - static Range ParseHttpRange(const std::string& s); - }; - private: class MetricsTimer; @@ -168,7 +125,7 @@ void ReadRange(std::string& target, const FileInfo& info, - const Range& range, + const StorageRange& range, bool uncompressIfNeeded); #if ORTHANC_ENABLE_CIVETWEB == 1 || ORTHANC_ENABLE_MONGOOSE == 1 diff -r 68d9bad29411 -r 6df5cb04dd7f OrthancFramework/Sources/FileStorage/StorageRange.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/FileStorage/StorageRange.cpp Tue May 19 12:11:01 2026 +0200 @@ -0,0 +1,243 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + **/ + + +#include "../PrecompiledHeaders.h" +#include "StorageRange.h" + +#include "../OrthancException.h" +#include "../Toolbox.h" +#include "../SerializationToolbox.h" + +#include +#include + + +namespace Orthanc +{ + void StorageRange::SanityCheck() const + { + if (hasStart_ && hasEnd_ && start_ > end_) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + } + + StorageRange::StorageRange(): + hasStart_(false), + start_(0), + hasEnd_(false), + end_(0) + { + } + + void StorageRange::SetStartInclusive(uint64_t start) + { + hasStart_ = true; + start_ = start; + } + + void StorageRange::SetEndInclusive(uint64_t end) + { + hasEnd_ = true; + end_ = end; + } + + uint64_t StorageRange::GetStartInclusive() const + { + if (!hasStart_) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else if (hasEnd_ && start_ > end_) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + return start_; + } + } + + uint64_t StorageRange::GetEndInclusive() const + { + if (!hasEnd_) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else if (hasStart_ && start_ > end_) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + return end_; + } + } + + std::string StorageRange::FormatHttpContentRange(uint64_t fullSize) const + { + SanityCheck(); + + if (fullSize == 0 || + (hasStart_ && start_ >= fullSize) || + (hasEnd_ && end_ >= fullSize)) + { + throw OrthancException(ErrorCode_BadRange); + } + + std::string s = "bytes "; + + if (hasStart_) + { + s += boost::lexical_cast(start_); + } + else + { + s += "0"; + } + + s += "-"; + + if (hasEnd_) + { + s += boost::lexical_cast(end_); + } + else + { + s += boost::lexical_cast(fullSize - 1); + } + + return s + "/" + boost::lexical_cast(fullSize); + } + + void StorageRange::Extract(std::string &target, + const std::string &source) const + { + SanityCheck(); + + if (hasStart_ && start_ >= source.size()) + { + throw OrthancException(ErrorCode_BadRange); + } + + if (hasEnd_ && end_ >= source.size()) + { + throw OrthancException(ErrorCode_BadRange); + } + + if (hasStart_ && hasEnd_) + { + target = source.substr(start_, end_ - start_ + 1); + } + else if (hasStart_) + { + target = source.substr(start_, source.size() - start_); + } + else if (hasEnd_) + { + target = source.substr(0, end_ + 1); + } + else + { + target = source; + } + } + + uint64_t StorageRange::GetContentLength(uint64_t fullSize) const + { + SanityCheck(); + + if (fullSize == 0) + { + throw OrthancException(ErrorCode_BadRange); + } + + if (hasStart_ && start_ >= fullSize) + { + throw OrthancException(ErrorCode_BadRange); + } + + if (hasEnd_ && end_ >= fullSize) + { + throw OrthancException(ErrorCode_BadRange); + } + + if (hasStart_ && hasEnd_) + { + return end_ - start_ + 1; + } + else if (hasStart_) + { + return fullSize - start_; + } + else if (hasEnd_) + { + return end_ + 1; + } + else + { + return fullSize; + } + } + + StorageRange StorageRange::ParseHttpRange(const std::string& s) + { + static const std::string BYTES = "bytes="; + + if (!boost::starts_with(s, BYTES)) + { + throw OrthancException(ErrorCode_BadRange); // Range not satisfiable + } + + std::vector tokens; + Orthanc::Toolbox::TokenizeString(tokens, s.substr(BYTES.length()), '-'); + + if (tokens.size() != 2) + { + throw OrthancException(ErrorCode_BadRange); + } + + StorageRange range; + + uint64_t tmp; + if (!tokens[0].empty()) + { + if (SerializationToolbox::ParseUnsignedInteger64(tmp, tokens[0])) + { + range.SetStartInclusive(tmp); + } + } + + if (!tokens[1].empty()) + { + if (SerializationToolbox::ParseUnsignedInteger64(tmp, tokens[1])) + { + range.SetEndInclusive(tmp); + } + } + + range.SanityCheck(); + return range; + } +} diff -r 68d9bad29411 -r 6df5cb04dd7f OrthancFramework/Sources/FileStorage/StorageRange.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/FileStorage/StorageRange.h Tue May 19 12:11:01 2026 +0200 @@ -0,0 +1,76 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + **/ + + +#pragma once + +#include "../OrthancFramework.h" + +#include +#include + + +namespace Orthanc +{ + // In Orthanc <= 1.12.11, this class was known as "StorageAccessor::Range" + class ORTHANC_PUBLIC StorageRange + { + private: + bool hasStart_; + uint64_t start_; + bool hasEnd_; + uint64_t end_; + + void SanityCheck() const; + + public: + StorageRange(); + + void SetStartInclusive(uint64_t start); + + void SetEndInclusive(uint64_t end); + + bool HasStart() const + { + return hasStart_; + } + + bool HasEnd() const + { + return hasEnd_; + } + + uint64_t GetStartInclusive() const; + + uint64_t GetEndInclusive() const; + + std::string FormatHttpContentRange(uint64_t fullSize) const; + + void Extract(std::string& target, + const std::string& source) const; + + uint64_t GetContentLength(uint64_t fullSize) const; + + static StorageRange ParseHttpRange(const std::string& s); + }; +} diff -r 68d9bad29411 -r 6df5cb04dd7f OrthancFramework/UnitTestsSources/FileStorageTests.cpp --- a/OrthancFramework/UnitTestsSources/FileStorageTests.cpp Tue May 19 10:52:00 2026 +0200 +++ b/OrthancFramework/UnitTestsSources/FileStorageTests.cpp Tue May 19 12:11:01 2026 +0200 @@ -271,7 +271,7 @@ TEST(StorageAccessor, Range) { { - StorageAccessor::Range range; + StorageRange range; ASSERT_FALSE(range.HasStart()); ASSERT_FALSE(range.HasEnd()); ASSERT_THROW(range.GetStartInclusive(), OrthancException); @@ -309,7 +309,7 @@ } { - StorageAccessor::Range range; + StorageRange range; range.SetEndInclusive(20); ASSERT_FALSE(range.HasStart()); ASSERT_TRUE(range.HasEnd()); @@ -324,7 +324,7 @@ } { - StorageAccessor::Range range = StorageAccessor::Range::ParseHttpRange("bytes=1-30"); + StorageRange range = StorageRange::ParseHttpRange("bytes=1-30"); ASSERT_TRUE(range.HasStart()); ASSERT_TRUE(range.HasEnd()); ASSERT_EQ(1u, range.GetStartInclusive()); @@ -332,27 +332,27 @@ ASSERT_EQ("bytes 1-30/100", range.FormatHttpContentRange(100)); } - ASSERT_THROW(StorageAccessor::Range::ParseHttpRange("bytes="), OrthancException); - ASSERT_THROW(StorageAccessor::Range::ParseHttpRange("bytes=-1-30"), OrthancException); - ASSERT_THROW(StorageAccessor::Range::ParseHttpRange("bytes=100-30"), OrthancException); + ASSERT_THROW(StorageRange::ParseHttpRange("bytes="), OrthancException); + ASSERT_THROW(StorageRange::ParseHttpRange("bytes=-1-30"), OrthancException); + ASSERT_THROW(StorageRange::ParseHttpRange("bytes=100-30"), OrthancException); - ASSERT_EQ("bytes 0-99/100", StorageAccessor::Range::ParseHttpRange("bytes=-").FormatHttpContentRange(100)); - ASSERT_EQ("bytes 0-10/100", StorageAccessor::Range::ParseHttpRange("bytes=-10").FormatHttpContentRange(100)); - ASSERT_EQ("bytes 10-99/100", StorageAccessor::Range::ParseHttpRange("bytes=10-").FormatHttpContentRange(100)); + ASSERT_EQ("bytes 0-99/100", StorageRange::ParseHttpRange("bytes=-").FormatHttpContentRange(100)); + ASSERT_EQ("bytes 0-10/100", StorageRange::ParseHttpRange("bytes=-10").FormatHttpContentRange(100)); + ASSERT_EQ("bytes 10-99/100", StorageRange::ParseHttpRange("bytes=10-").FormatHttpContentRange(100)); { std::string s; - StorageAccessor::Range::ParseHttpRange("bytes=1-2").Extract(s, "Hello"); + StorageRange::ParseHttpRange("bytes=1-2").Extract(s, "Hello"); ASSERT_EQ("el", s); - StorageAccessor::Range::ParseHttpRange("bytes=-2").Extract(s, "Hello"); + StorageRange::ParseHttpRange("bytes=-2").Extract(s, "Hello"); ASSERT_EQ("Hel", s); - StorageAccessor::Range::ParseHttpRange("bytes=3-").Extract(s, "Hello"); + StorageRange::ParseHttpRange("bytes=3-").Extract(s, "Hello"); ASSERT_EQ("lo", s); - StorageAccessor::Range::ParseHttpRange("bytes=-").Extract(s, "Hello"); + StorageRange::ParseHttpRange("bytes=-").Extract(s, "Hello"); ASSERT_EQ("Hello", s); - StorageAccessor::Range::ParseHttpRange("bytes=4-").Extract(s, "Hello"); + StorageRange::ParseHttpRange("bytes=4-").Extract(s, "Hello"); ASSERT_EQ("o", s); - ASSERT_THROW(StorageAccessor::Range::ParseHttpRange("bytes=5-").Extract(s, "Hello"), OrthancException); + ASSERT_THROW(StorageRange::ParseHttpRange("bytes=5-").Extract(s, "Hello"), OrthancException); } } diff -r 68d9bad29411 -r 6df5cb04dd7f OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp --- a/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp Tue May 19 10:52:00 2026 +0200 +++ b/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp Tue May 19 12:11:01 2026 +0200 @@ -2350,13 +2350,13 @@ ServerContext& context = OrthancRestApi::GetContext(call); bool hasRangeHeader = false; - StorageAccessor::Range range; + StorageRange range; HttpToolbox::Arguments::const_iterator rangeHeader = call.GetHttpHeaders().find("range"); if (rangeHeader != call.GetHttpHeaders().end()) { hasRangeHeader = true; - range = StorageAccessor::Range::ParseHttpRange(rangeHeader->second); + range = StorageRange::ParseHttpRange(rangeHeader->second); } FileInfo info; diff -r 68d9bad29411 -r 6df5cb04dd7f OrthancServer/Sources/ServerContext.cpp --- a/OrthancServer/Sources/ServerContext.cpp Tue May 19 10:52:00 2026 +0200 +++ b/OrthancServer/Sources/ServerContext.cpp Tue May 19 12:11:01 2026 +0200 @@ -1545,7 +1545,7 @@ void ServerContext::ReadAttachmentRange(std::string &result, const FileInfo &attachment, - const StorageAccessor::Range &range, + const StorageRange &range, bool uncompressIfNeeded) { StorageAccessor accessor(area_, storageCache_, GetMetricsRegistry()); diff -r 68d9bad29411 -r 6df5cb04dd7f OrthancServer/Sources/ServerContext.h --- a/OrthancServer/Sources/ServerContext.h Tue May 19 10:52:00 2026 +0200 +++ b/OrthancServer/Sources/ServerContext.h Tue May 19 12:11:01 2026 +0200 @@ -432,7 +432,7 @@ void ReadAttachmentRange(std::string& result, const FileInfo& attachment, - const StorageAccessor::Range& range, + const StorageRange& range, bool uncompressIfNeeded); void SetStoreMD5ForAttachments(bool storeMD5);