From fa54be052a5f6cb35e2c7f7a12505b42265494a3 Mon Sep 17 00:00:00 2001 From: Timothy Prepscius Date: Wed, 25 Feb 2026 12:39:24 -0500 Subject: [PATCH] flatten 20260225 --- .gitignore | 4 + Core_Misc.xcodeproj/project.pbxproj | 1753 +++ .../contents.xcworkspacedata | 4 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + .../xcschemes/Core_Misc_Tests.xcscheme | 146 + Makefile | 2 + Makefile.def | 7 + Makefile.project | 71 + tests/Core_Misc | 1 + tests/Makefile | 2 + tests/Makefile.project | 45 + tests/main.cpp | 19 + tjp/core/algorithm/Sortable.hpp | 42 + tjp/core/algorithm/_tests/call_with_pairs.cpp | 27 + tjp/core/algorithm/call_with_pairs.hpp | 43 + tjp/core/algorithm/find_if_random.hpp | 57 + tjp/core/algorithm/for_each_argument.hpp | 17 + tjp/core/algorithm/reverse_inplace.hpp | 16 + tjp/core/algorithm/xor_inplace.hpp | 14 + tjp/core/args/Args.cpp | 42 + tjp/core/args/Args.h | 67 + tjp/core/args/ArgsFile.cpp | 60 + tjp/core/args/ArgsFile.h | 16 + tjp/core/bases/Base16.cpp | 96 + tjp/core/bases/Base16.h | 104 + tjp/core/bases/Base32.cpp | 88 + tjp/core/bases/Base32.h | 91 + tjp/core/bases/Base64.cpp | 362 + tjp/core/bases/Base64.h | 46 + tjp/core/bases/Base64URL.cpp | 114 + tjp/core/bases/Base64URL.h | 22 + tjp/core/bases/_remove/ntop.c | 310 + tjp/core/bases/_tests/Base16.cpp | 36 + tjp/core/bases/_tests/Base32.cpp | 58 + tjp/core/bases/_tests/Base64.cpp | 36 + tjp/core/compression/_delete/zlib.cpp | 588 + tjp/core/compression/_delete/zlib.hpp | 88 + tjp/core/compression/_tests/zlib.cpp | 282 + tjp/core/compression/_tests/zstd.cpp | 148 + tjp/core/compression/remote-run | 38 + tjp/core/compression/zlib.cpp | 525 + tjp/core/compression/zlib.h | 11 + tjp/core/compression/zlib.hpp | 158 + tjp/core/compression/zstd.cpp | 356 + tjp/core/compression/zstd.h | 14 + tjp/core/compression/zstd.hpp | 111 + tjp/core/containers/Alias.hpp | 63 + tjp/core/containers/Changed.hpp | 88 + tjp/core/containers/CircularBuffer.hpp | 198 + tjp/core/containers/InvalidatingCache.hpp | 42 + .../containers/InvalidatingTimedCache.hpp | 52 + tjp/core/containers/SizedVector.hpp | 101 + tjp/core/containers/TimedCache.hpp | 39 + tjp/core/containers/_tests/CircularBuffer.cpp | 71 + tjp/core/csv/Csv.cpp | 168 + tjp/core/csv/Csv.h | 208 + tjp/core/csv/Csv.hpp | 82 + tjp/core/csv/_tests/Csv.cpp | 16 + tjp/core/delegate/Delegate+Single.h | 25 + tjp/core/delegate/Delegate+Single.hpp | 54 + tjp/core/delegate/Delegate.h | 25 + tjp/core/delegate/Delegate.hpp | 342 + tjp/core/delegate/_tests/Delegate+Single.cpp | 141 + tjp/core/delegate/_tests/Delegate.cpp | 201 + tjp/core/endian/Endian.cpp | 16 + tjp/core/endian/Endian.h | 72 + tjp/core/events/Signal.cpp | 75 + tjp/core/events/Signal.h | 21 + tjp/core/events/Signal.hpp | 40 + .../extensible/Extensible+CacheViaArray.h | 188 + .../Extensible+CacheViaArrayAndMap.h | 72 + tjp/core/extensible/Extensible+CacheViaMap.h | 51 + tjp/core/extensible/Extensible+Enumerated.h | 27 + tjp/core/extensible/Extensible+TypeInfo.h | 7 + tjp/core/extensible/Extensible.cpp | 67 + tjp/core/extensible/Extensible.h | 269 + tjp/core/extensible/ExtensibleWith.h | 33 + tjp/core/extensible/_tests/Extensible.cpp | 130 + tjp/core/file/Contents.h | 98 + tjp/core/file/Directories.h | 24 + tjp/core/file/File.cpp | 158 + tjp/core/file/File.h | 46 + tjp/core/file/File_Messy.cpp | 328 + tjp/core/file/Path.cpp | 169 + tjp/core/file/Path.h | 44 + tjp/core/file/VirtualFileSystem.cpp | 31 + tjp/core/file/VirtualFileSystem.h | 20 + tjp/core/file/VirtualFileSystem.hpp | 108 + tjp/core/file/_tests/Path.cpp | 32 + tjp/core/file/apple/Directories.cpp | 71 + tjp/core/file/apple/Directories.h | 22 + tjp/core/file/copy.cpp | 28 + tjp/core/file/copy.hpp | 15 + tjp/core/file/directory_exists.cpp | 28 + tjp/core/file/directory_exists.hpp | 15 + tjp/core/file/directory_of.cpp | 20 + tjp/core/file/directory_of.hpp | 15 + tjp/core/file/file_exists.cpp | 34 + tjp/core/file/file_exists.hpp | 17 + tjp/core/file/get_files.cpp | 135 + tjp/core/file/get_files.hpp | 17 + tjp/core/file/is_relative_path.hpp | 15 + tjp/core/file/linux/Directories.cpp | 47 + tjp/core/file/linux/Directories.h | 22 + tjp/core/file/move.cpp | 32 + tjp/core/file/move.hpp | 16 + tjp/core/file/pop_first.hpp | 21 + tjp/core/file/read.cpp | 62 + tjp/core/file/read.hpp | 21 + tjp/core/file/split_first_directory.cpp | 28 + tjp/core/file/split_first_directory.hpp | 17 + tjp/core/file/times.cpp | 74 + tjp/core/file/times.hpp | 22 + tjp/core/file/unlink.cpp | 24 + tjp/core/file/unlink.hpp | 15 + tjp/core/file/validate_safe_relative_path.cpp | 42 + tjp/core/file/validate_safe_relative_path.hpp | 17 + tjp/core/file/write.cpp | 66 + tjp/core/file/write.hpp | 25 + tjp/core/file/writeSegmentsToFile.cpp | 53 + tjp/core/file/writeSegmentsToFile.h | 18 + tjp/core/functions/Dispatch.h | 27 + tjp/core/functions/Dispatch.hpp | 199 + tjp/core/functions/optional_function.h | 18 + tjp/core/functions/optional_function.hpp | 132 + tjp/core/hash/Hash.cpp | 23 + tjp/core/hash/Hash.h | 58 + tjp/core/hash/fasthash.cpp | 84 + tjp/core/hash/fasthash.h | 58 + tjp/core/http/Component.h | 9 + tjp/core/http/Component.hpp | 25 + tjp/core/http/Method.h | 21 + tjp/core/http/Method.hpp | 9 + tjp/core/http/Server.cpp | 9 + tjp/core/http/Server.h | 10 + tjp/core/http/Server.hpp | 96 + tjp/core/http/Types.h | 43 + tjp/core/http/Types.hpp | 385 + tjp/core/http/as_future.hpp | 21 + tjp/core/http/beast/Server.cpp | 985 ++ tjp/core/http/beast/Server.h | 9 + tjp/core/http/beast/Server.hpp | 50 + tjp/core/http/beast/_test/Server.cpp | 505 + tjp/core/http/beast/beast.hpp | 25 + tjp/core/http/collapse_future.hpp | 26 + tjp/core/http/future_response.hpp | 19 + tjp/core/http/hirose/Server.cpp | 404 + tjp/core/http/hirose/Server.h | 9 + tjp/core/http/hirose/Server.hpp | 49 + tjp/core/http/hirose/_test/Server.cpp | 356 + tjp/core/http/hirose/httplib-0_27_0.h | 12067 ++++++++++++++++ tjp/core/http/hirose/httplib.h | 3 + tjp/core/http/hirose/write_result.hpp | 36 + tjp/core/http/imp.hpp | 11 + tjp/core/http/old/httplib.h | 8309 +++++++++++ tjp/core/http/remote-run | 38 + tjp/core/http/require_header.hpp | 18 + .../http/temp/compression/_delete/zlib.cpp | 588 + .../http/temp/compression/_delete/zlib.hpp | 88 + .../http/temp/compression/_tests/zlib.cpp | 282 + .../http/temp/compression/_tests/zstd.cpp | 148 + tjp/core/http/temp/compression/remote-run | 38 + tjp/core/http/temp/compression/zlib.cpp | 525 + tjp/core/http/temp/compression/zlib.h | 11 + tjp/core/http/temp/compression/zlib.hpp | 158 + tjp/core/http/temp/compression/zstd.cpp | 356 + tjp/core/http/temp/compression/zstd.h | 14 + tjp/core/http/temp/compression/zstd.hpp | 111 + tjp/core/http/transform.hpp | 55 + tjp/core/http/transform_base64_text.hpp | 29 + tjp/core/http/transform_binary.hpp | 46 + tjp/core/http/transform_json.hpp | 46 + tjp/core/http/transform_json_text.hpp | 29 + tjp/core/http/transform_ok.hpp | 30 + tjp/core/http/transform_plaintext.hpp | 46 + tjp/core/in_place/InPlace.h | 46 + tjp/core/initializer/Initializer.cpp | 51 + tjp/core/initializer/Initializer.h | 17 + tjp/core/initializer/Initializer.hpp | 238 + tjp/core/internet/Data.hpp | 75 + tjp/core/internet/Headers.cpp | 260 + tjp/core/internet/Headers.hpp | 53 + tjp/core/internet/Internet+Internal.cpp | 147 + tjp/core/internet/Internet+Internal.hpp | 61 + tjp/core/internet/Internet.h | 19 + tjp/core/internet/Internet.hpp | 111 + tjp/core/internet/Queue.h | 15 + tjp/core/internet/Queue.hpp | 89 + tjp/core/internet/URL.cpp | 207 + tjp/core/internet/URL.h | 130 + tjp/core/internet/_tests/Internet+common.cpp | 21 + tjp/core/internet/curl/Internet.cpp | 289 + tjp/core/internet/curl/Internet.hpp | 17 + tjp/core/internet/curl/Queue.cpp | 126 + tjp/core/internet/curl/Queue.hpp | 7 + tjp/core/internet/form_encode.hpp | 49 + tjp/core/internet/future_json_to_result.hpp | 18 + tjp/core/internet/get_embedded_value.hpp | 27 + tjp/core/internet/get_redirect.hpp | 25 + tjp/core/internet/hirose/Internet.cpp | 141 + tjp/core/internet/hirose/Internet.hpp | 16 + tjp/core/internet/html_decode.hpp | 66 + tjp/core/internet/ios/Internet.hpp | 18 + tjp/core/internet/ios/Internet.mm | 614 + tjp/core/internet/ios/Queue.cpp | 106 + tjp/core/internet/ios/Queue.hpp | 7 + tjp/core/internet/join.hpp | 39 + tjp/core/internet/url_param_decode.hpp | 39 + tjp/core/internet/url_param_encode.hpp | 40 + tjp/core/internet/url_path_decode.hpp | 15 + tjp/core/internet/url_path_encode.hpp | 40 + tjp/core/internet/url_with_params.hpp | 16 + tjp/core/mysql/MysqlConnection.h | 11 + tjp/core/mysql/MysqlQueries.h | 19 + tjp/core/pipe/Pipe.h | 85 + tjp/core/pipe/Pipe.inl | 294 + tjp/core/pipe/_tests/Pipe.cpp | 83 + tjp/core/pipe/obsolete/Pipe.cpp | 173 + tjp/core/pipe/obsolete/Pipe.h | 102 + tjp/core/profile/PROFILE_SCOPE.cpp | 150 + tjp/core/profile/PROFILE_SCOPE.h | 147 + tjp/core/profile/PROFILE_SIGNAL.cpp | 106 + tjp/core/profile/PROFILE_SIGNAL.h | 144 + tjp/core/random/Random.cpp | 18 + tjp/core/random/Random.h | 236 + tjp/core/random/bell_curve.hpp | 46 + tjp/core/random/file_name.hpp | 18 + tjp/core/random/temporary_path.hpp | 18 + .../GlobalResourceVersion+IO.cpp | 16 + .../GlobalResourceVersion+IO.inl | 21 + .../GlobalResourceVersion.cpp | 21 + .../resource_version/GlobalResourceVersion.h | 15 + .../GlobalResourceVersion.hpp | 162 + .../LocalResourceVersion+IO.inl | 25 + .../resource_version/LocalResourceVersion.hpp | 114 + .../_tests/GlobalResource.cpp | 71 + tjp/core/runnable/Runnable.h | 9 + tjp/core/runnable/Runnable.hpp | 78 + tjp/core/singleton/Singleton.cpp | 43 + tjp/core/singleton/Singleton.h | 36 + tjp/core/singleton/Singleton.hpp | 125 + tjp/core/singleton/ThreadSafeSingleton.h | 27 + tjp/core/singleton/ThreadSafeSingleton.hpp | 112 + tjp/core/singleton/ThreadSpecificSingleton.h | 41 + .../singleton/ThreadSpecificSingleton.hpp | 132 + tjp/core/string/to_cstring.hpp | 43 + tjp/core/string_lookup/StringLookup.cpp | 166 + tjp/core/string_lookup/StringLookup.h | 98 + tjp/core/system_usage/CpuUsage.h | 28 + tjp/core/system_usage/NetworkUsage.h | 28 + tjp/core/system_usage/ProcessUsage.cpp | 60 + tjp/core/system_usage/ProcessUsage.h | 22 + tjp/core/system_usage/SystemUsage.cpp | 52 + tjp/core/system_usage/SystemUsage.h | 12 + tjp/core/system_usage/SystemUsage.hpp | 35 + tjp/core/system_usage/apple/CpuUsage.cpp | 36 + tjp/core/system_usage/apple/NetworkUsage.cpp | 15 + tjp/core/system_usage/linux/CpuUsage.cpp | 54 + tjp/core/system_usage/linux/NetworkUsage.cpp | 15 + tjp/core/thread_pool/AutoThreadPool.h | 15 + tjp/core/thread_pool/AutoThreadPool.hpp | 151 + tjp/core/thread_pool/Base.h | 287 + tjp/core/thread_pool/FunctionalProcessor.hpp | 86 + tjp/core/thread_pool/ObjectConsumer.hpp | 37 + tjp/core/thread_pool/_delete/AutoThreadPool.h | 15 + .../thread_pool/_delete/AutoThreadPool.hpp | 50 + tjp/core/thread_pool/_delete/ctpl_stl.h | 253 + .../thread_pool/_tests/AutoThreadPool.cpp | 122 + tjp/core/threads/Parallel.cpp | 109 + tjp/core/threads/Parallel.h | 9 + tjp/core/threads/Parallel.hpp | 44 + tjp/core/threads/PinnedFutures.hpp | 140 + tjp/core/time-obsolete/Notes.txt | 2 + tjp/core/time-obsolete/Scheduler+Retry.hpp | 161 + tjp/core/time-obsolete/Scheduler.cpp | 34 + tjp/core/time-obsolete/Scheduler.h | 24 + tjp/core/time-obsolete/Scheduler.hpp | 52 + tjp/core/time-obsolete/Scheduler.inl | 97 + tjp/core/time-obsolete/SchedulerManual.cpp | 229 + tjp/core/time-obsolete/SchedulerManual.h | 21 + tjp/core/time-obsolete/SchedulerManual.hpp | 121 + tjp/core/time-obsolete/SchedulerPool.cpp | 101 + tjp/core/time-obsolete/SchedulerPool.h | 21 + tjp/core/time-obsolete/SchedulerPool.hpp | 47 + tjp/core/time-obsolete/Time+Test.cpp | 50 + tjp/core/time-obsolete/Time.h | 20 + tjp/core/time-obsolete/TimeSource+Test.cpp | 39 + tjp/core/time-obsolete/TimeSource.h | 32 + tjp/core/time-obsolete/TimeSource.hpp | 129 + tjp/core/time-obsolete/Time_v1.cpp | 140 + tjp/core/time-obsolete/Time_v1.h | 45 + tjp/core/time-obsolete/Time_v2.cpp | 51 + tjp/core/time-obsolete/Time_v2.h | 61 + tjp/core/time-obsolete/Time_v2.inl | 114 + tjp/core/url/URL.cpp | 155 + tjp/core/url/URL.h | 53 + tjp/core/url/URL.inl | 32 + tjp/core/url/_tests/URL.cpp | 110 + tjp/core/zip/Compress+zlib.cpp | 425 + tjp/core/zip/Compress+zstd.cpp | 56 + tjp/core/zip/Compress.h | 40 + tjp/core/zip/Decompress.cpp | 276 + tjp/core/zip/Decompress.h | 38 + tjp/core/zip/Exception.h | 13 + tjp/core/zip/MemoryBuffer.cpp | 101 + tjp/core/zip/MemoryBuffer.h | 11 + tjp/core/zip/MemoryBuffer.hpp | 25 + tjp/core/zip/VirtualFileSystem.cpp | 138 + tjp/core/zip/VirtualFileSystem.h | 9 + tjp/core/zip/VirtualFileSystem.hpp | 32 + tjp/core/zip/_delete/Compression.cpp | 232 + tjp/core/zip/_delete/Compression.h | 18 + tjp/core/zip/_delete/Compression.hpp | 267 + tjp/core/zip/_delete/Compression.inl | 82 + tjp/core/zip/_delete/_tests/Compression.cpp | 165 + 315 files changed, 49791 insertions(+) create mode 100644 .gitignore create mode 100644 Core_Misc.xcodeproj/project.pbxproj create mode 100644 Core_Misc.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 Core_Misc.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 Core_Misc.xcodeproj/xcshareddata/xcschemes/Core_Misc_Tests.xcscheme create mode 100644 Makefile create mode 100755 Makefile.def create mode 100755 Makefile.project create mode 120000 tests/Core_Misc create mode 100644 tests/Makefile create mode 100755 tests/Makefile.project create mode 100644 tests/main.cpp create mode 100644 tjp/core/algorithm/Sortable.hpp create mode 100644 tjp/core/algorithm/_tests/call_with_pairs.cpp create mode 100644 tjp/core/algorithm/call_with_pairs.hpp create mode 100644 tjp/core/algorithm/find_if_random.hpp create mode 100644 tjp/core/algorithm/for_each_argument.hpp create mode 100644 tjp/core/algorithm/reverse_inplace.hpp create mode 100644 tjp/core/algorithm/xor_inplace.hpp create mode 100644 tjp/core/args/Args.cpp create mode 100644 tjp/core/args/Args.h create mode 100644 tjp/core/args/ArgsFile.cpp create mode 100644 tjp/core/args/ArgsFile.h create mode 100755 tjp/core/bases/Base16.cpp create mode 100755 tjp/core/bases/Base16.h create mode 100755 tjp/core/bases/Base32.cpp create mode 100755 tjp/core/bases/Base32.h create mode 100755 tjp/core/bases/Base64.cpp create mode 100755 tjp/core/bases/Base64.h create mode 100755 tjp/core/bases/Base64URL.cpp create mode 100755 tjp/core/bases/Base64URL.h create mode 100755 tjp/core/bases/_remove/ntop.c create mode 100644 tjp/core/bases/_tests/Base16.cpp create mode 100644 tjp/core/bases/_tests/Base32.cpp create mode 100644 tjp/core/bases/_tests/Base64.cpp create mode 100644 tjp/core/compression/_delete/zlib.cpp create mode 100644 tjp/core/compression/_delete/zlib.hpp create mode 100644 tjp/core/compression/_tests/zlib.cpp create mode 100644 tjp/core/compression/_tests/zstd.cpp create mode 100755 tjp/core/compression/remote-run create mode 100644 tjp/core/compression/zlib.cpp create mode 100644 tjp/core/compression/zlib.h create mode 100644 tjp/core/compression/zlib.hpp create mode 100644 tjp/core/compression/zstd.cpp create mode 100644 tjp/core/compression/zstd.h create mode 100644 tjp/core/compression/zstd.hpp create mode 100644 tjp/core/containers/Alias.hpp create mode 100644 tjp/core/containers/Changed.hpp create mode 100644 tjp/core/containers/CircularBuffer.hpp create mode 100644 tjp/core/containers/InvalidatingCache.hpp create mode 100644 tjp/core/containers/InvalidatingTimedCache.hpp create mode 100644 tjp/core/containers/SizedVector.hpp create mode 100644 tjp/core/containers/TimedCache.hpp create mode 100644 tjp/core/containers/_tests/CircularBuffer.cpp create mode 100644 tjp/core/csv/Csv.cpp create mode 100644 tjp/core/csv/Csv.h create mode 100644 tjp/core/csv/Csv.hpp create mode 100644 tjp/core/csv/_tests/Csv.cpp create mode 100644 tjp/core/delegate/Delegate+Single.h create mode 100644 tjp/core/delegate/Delegate+Single.hpp create mode 100644 tjp/core/delegate/Delegate.h create mode 100644 tjp/core/delegate/Delegate.hpp create mode 100644 tjp/core/delegate/_tests/Delegate+Single.cpp create mode 100644 tjp/core/delegate/_tests/Delegate.cpp create mode 100755 tjp/core/endian/Endian.cpp create mode 100755 tjp/core/endian/Endian.h create mode 100644 tjp/core/events/Signal.cpp create mode 100644 tjp/core/events/Signal.h create mode 100644 tjp/core/events/Signal.hpp create mode 100644 tjp/core/extensible/Extensible+CacheViaArray.h create mode 100644 tjp/core/extensible/Extensible+CacheViaArrayAndMap.h create mode 100644 tjp/core/extensible/Extensible+CacheViaMap.h create mode 100644 tjp/core/extensible/Extensible+Enumerated.h create mode 100644 tjp/core/extensible/Extensible+TypeInfo.h create mode 100644 tjp/core/extensible/Extensible.cpp create mode 100644 tjp/core/extensible/Extensible.h create mode 100644 tjp/core/extensible/ExtensibleWith.h create mode 100644 tjp/core/extensible/_tests/Extensible.cpp create mode 100644 tjp/core/file/Contents.h create mode 100644 tjp/core/file/Directories.h create mode 100644 tjp/core/file/File.cpp create mode 100644 tjp/core/file/File.h create mode 100644 tjp/core/file/File_Messy.cpp create mode 100644 tjp/core/file/Path.cpp create mode 100644 tjp/core/file/Path.h create mode 100644 tjp/core/file/VirtualFileSystem.cpp create mode 100644 tjp/core/file/VirtualFileSystem.h create mode 100644 tjp/core/file/VirtualFileSystem.hpp create mode 100644 tjp/core/file/_tests/Path.cpp create mode 100644 tjp/core/file/apple/Directories.cpp create mode 100644 tjp/core/file/apple/Directories.h create mode 100644 tjp/core/file/copy.cpp create mode 100644 tjp/core/file/copy.hpp create mode 100644 tjp/core/file/directory_exists.cpp create mode 100644 tjp/core/file/directory_exists.hpp create mode 100644 tjp/core/file/directory_of.cpp create mode 100644 tjp/core/file/directory_of.hpp create mode 100644 tjp/core/file/file_exists.cpp create mode 100644 tjp/core/file/file_exists.hpp create mode 100644 tjp/core/file/get_files.cpp create mode 100644 tjp/core/file/get_files.hpp create mode 100644 tjp/core/file/is_relative_path.hpp create mode 100644 tjp/core/file/linux/Directories.cpp create mode 100644 tjp/core/file/linux/Directories.h create mode 100644 tjp/core/file/move.cpp create mode 100644 tjp/core/file/move.hpp create mode 100644 tjp/core/file/pop_first.hpp create mode 100644 tjp/core/file/read.cpp create mode 100644 tjp/core/file/read.hpp create mode 100644 tjp/core/file/split_first_directory.cpp create mode 100644 tjp/core/file/split_first_directory.hpp create mode 100644 tjp/core/file/times.cpp create mode 100644 tjp/core/file/times.hpp create mode 100644 tjp/core/file/unlink.cpp create mode 100644 tjp/core/file/unlink.hpp create mode 100644 tjp/core/file/validate_safe_relative_path.cpp create mode 100644 tjp/core/file/validate_safe_relative_path.hpp create mode 100644 tjp/core/file/write.cpp create mode 100644 tjp/core/file/write.hpp create mode 100644 tjp/core/file/writeSegmentsToFile.cpp create mode 100644 tjp/core/file/writeSegmentsToFile.h create mode 100644 tjp/core/functions/Dispatch.h create mode 100644 tjp/core/functions/Dispatch.hpp create mode 100644 tjp/core/functions/optional_function.h create mode 100644 tjp/core/functions/optional_function.hpp create mode 100755 tjp/core/hash/Hash.cpp create mode 100755 tjp/core/hash/Hash.h create mode 100644 tjp/core/hash/fasthash.cpp create mode 100644 tjp/core/hash/fasthash.h create mode 100755 tjp/core/http/Component.h create mode 100755 tjp/core/http/Component.hpp create mode 100755 tjp/core/http/Method.h create mode 100755 tjp/core/http/Method.hpp create mode 100755 tjp/core/http/Server.cpp create mode 100755 tjp/core/http/Server.h create mode 100755 tjp/core/http/Server.hpp create mode 100755 tjp/core/http/Types.h create mode 100755 tjp/core/http/Types.hpp create mode 100755 tjp/core/http/as_future.hpp create mode 100644 tjp/core/http/beast/Server.cpp create mode 100644 tjp/core/http/beast/Server.h create mode 100644 tjp/core/http/beast/Server.hpp create mode 100755 tjp/core/http/beast/_test/Server.cpp create mode 100644 tjp/core/http/beast/beast.hpp create mode 100755 tjp/core/http/collapse_future.hpp create mode 100755 tjp/core/http/future_response.hpp create mode 100755 tjp/core/http/hirose/Server.cpp create mode 100755 tjp/core/http/hirose/Server.h create mode 100755 tjp/core/http/hirose/Server.hpp create mode 100755 tjp/core/http/hirose/_test/Server.cpp create mode 100644 tjp/core/http/hirose/httplib-0_27_0.h create mode 100644 tjp/core/http/hirose/httplib.h create mode 100755 tjp/core/http/hirose/write_result.hpp create mode 100755 tjp/core/http/imp.hpp create mode 100644 tjp/core/http/old/httplib.h create mode 100755 tjp/core/http/remote-run create mode 100755 tjp/core/http/require_header.hpp create mode 100644 tjp/core/http/temp/compression/_delete/zlib.cpp create mode 100644 tjp/core/http/temp/compression/_delete/zlib.hpp create mode 100644 tjp/core/http/temp/compression/_tests/zlib.cpp create mode 100644 tjp/core/http/temp/compression/_tests/zstd.cpp create mode 100755 tjp/core/http/temp/compression/remote-run create mode 100644 tjp/core/http/temp/compression/zlib.cpp create mode 100644 tjp/core/http/temp/compression/zlib.h create mode 100644 tjp/core/http/temp/compression/zlib.hpp create mode 100644 tjp/core/http/temp/compression/zstd.cpp create mode 100644 tjp/core/http/temp/compression/zstd.h create mode 100644 tjp/core/http/temp/compression/zstd.hpp create mode 100755 tjp/core/http/transform.hpp create mode 100755 tjp/core/http/transform_base64_text.hpp create mode 100755 tjp/core/http/transform_binary.hpp create mode 100755 tjp/core/http/transform_json.hpp create mode 100755 tjp/core/http/transform_json_text.hpp create mode 100755 tjp/core/http/transform_ok.hpp create mode 100755 tjp/core/http/transform_plaintext.hpp create mode 100755 tjp/core/in_place/InPlace.h create mode 100644 tjp/core/initializer/Initializer.cpp create mode 100644 tjp/core/initializer/Initializer.h create mode 100644 tjp/core/initializer/Initializer.hpp create mode 100755 tjp/core/internet/Data.hpp create mode 100755 tjp/core/internet/Headers.cpp create mode 100755 tjp/core/internet/Headers.hpp create mode 100755 tjp/core/internet/Internet+Internal.cpp create mode 100755 tjp/core/internet/Internet+Internal.hpp create mode 100755 tjp/core/internet/Internet.h create mode 100755 tjp/core/internet/Internet.hpp create mode 100755 tjp/core/internet/Queue.h create mode 100755 tjp/core/internet/Queue.hpp create mode 100755 tjp/core/internet/URL.cpp create mode 100755 tjp/core/internet/URL.h create mode 100755 tjp/core/internet/_tests/Internet+common.cpp create mode 100755 tjp/core/internet/curl/Internet.cpp create mode 100755 tjp/core/internet/curl/Internet.hpp create mode 100755 tjp/core/internet/curl/Queue.cpp create mode 100755 tjp/core/internet/curl/Queue.hpp create mode 100755 tjp/core/internet/form_encode.hpp create mode 100755 tjp/core/internet/future_json_to_result.hpp create mode 100755 tjp/core/internet/get_embedded_value.hpp create mode 100755 tjp/core/internet/get_redirect.hpp create mode 100755 tjp/core/internet/hirose/Internet.cpp create mode 100755 tjp/core/internet/hirose/Internet.hpp create mode 100755 tjp/core/internet/html_decode.hpp create mode 100755 tjp/core/internet/ios/Internet.hpp create mode 100755 tjp/core/internet/ios/Internet.mm create mode 100755 tjp/core/internet/ios/Queue.cpp create mode 100755 tjp/core/internet/ios/Queue.hpp create mode 100755 tjp/core/internet/join.hpp create mode 100755 tjp/core/internet/url_param_decode.hpp create mode 100755 tjp/core/internet/url_param_encode.hpp create mode 100755 tjp/core/internet/url_path_decode.hpp create mode 100755 tjp/core/internet/url_path_encode.hpp create mode 100755 tjp/core/internet/url_with_params.hpp create mode 100755 tjp/core/mysql/MysqlConnection.h create mode 100755 tjp/core/mysql/MysqlQueries.h create mode 100755 tjp/core/pipe/Pipe.h create mode 100755 tjp/core/pipe/Pipe.inl create mode 100755 tjp/core/pipe/_tests/Pipe.cpp create mode 100755 tjp/core/pipe/obsolete/Pipe.cpp create mode 100755 tjp/core/pipe/obsolete/Pipe.h create mode 100644 tjp/core/profile/PROFILE_SCOPE.cpp create mode 100644 tjp/core/profile/PROFILE_SCOPE.h create mode 100644 tjp/core/profile/PROFILE_SIGNAL.cpp create mode 100644 tjp/core/profile/PROFILE_SIGNAL.h create mode 100755 tjp/core/random/Random.cpp create mode 100755 tjp/core/random/Random.h create mode 100755 tjp/core/random/bell_curve.hpp create mode 100755 tjp/core/random/file_name.hpp create mode 100755 tjp/core/random/temporary_path.hpp create mode 100644 tjp/core/resource_version/GlobalResourceVersion+IO.cpp create mode 100644 tjp/core/resource_version/GlobalResourceVersion+IO.inl create mode 100644 tjp/core/resource_version/GlobalResourceVersion.cpp create mode 100644 tjp/core/resource_version/GlobalResourceVersion.h create mode 100644 tjp/core/resource_version/GlobalResourceVersion.hpp create mode 100644 tjp/core/resource_version/LocalResourceVersion+IO.inl create mode 100644 tjp/core/resource_version/LocalResourceVersion.hpp create mode 100755 tjp/core/resource_version/_tests/GlobalResource.cpp create mode 100644 tjp/core/runnable/Runnable.h create mode 100644 tjp/core/runnable/Runnable.hpp create mode 100755 tjp/core/singleton/Singleton.cpp create mode 100755 tjp/core/singleton/Singleton.h create mode 100755 tjp/core/singleton/Singleton.hpp create mode 100755 tjp/core/singleton/ThreadSafeSingleton.h create mode 100755 tjp/core/singleton/ThreadSafeSingleton.hpp create mode 100755 tjp/core/singleton/ThreadSpecificSingleton.h create mode 100755 tjp/core/singleton/ThreadSpecificSingleton.hpp create mode 100644 tjp/core/string/to_cstring.hpp create mode 100755 tjp/core/string_lookup/StringLookup.cpp create mode 100755 tjp/core/string_lookup/StringLookup.h create mode 100644 tjp/core/system_usage/CpuUsage.h create mode 100644 tjp/core/system_usage/NetworkUsage.h create mode 100644 tjp/core/system_usage/ProcessUsage.cpp create mode 100644 tjp/core/system_usage/ProcessUsage.h create mode 100644 tjp/core/system_usage/SystemUsage.cpp create mode 100644 tjp/core/system_usage/SystemUsage.h create mode 100644 tjp/core/system_usage/SystemUsage.hpp create mode 100644 tjp/core/system_usage/apple/CpuUsage.cpp create mode 100644 tjp/core/system_usage/apple/NetworkUsage.cpp create mode 100644 tjp/core/system_usage/linux/CpuUsage.cpp create mode 100644 tjp/core/system_usage/linux/NetworkUsage.cpp create mode 100755 tjp/core/thread_pool/AutoThreadPool.h create mode 100755 tjp/core/thread_pool/AutoThreadPool.hpp create mode 100755 tjp/core/thread_pool/Base.h create mode 100755 tjp/core/thread_pool/FunctionalProcessor.hpp create mode 100755 tjp/core/thread_pool/ObjectConsumer.hpp create mode 100755 tjp/core/thread_pool/_delete/AutoThreadPool.h create mode 100755 tjp/core/thread_pool/_delete/AutoThreadPool.hpp create mode 100755 tjp/core/thread_pool/_delete/ctpl_stl.h create mode 100755 tjp/core/thread_pool/_tests/AutoThreadPool.cpp create mode 100755 tjp/core/threads/Parallel.cpp create mode 100755 tjp/core/threads/Parallel.h create mode 100755 tjp/core/threads/Parallel.hpp create mode 100755 tjp/core/threads/PinnedFutures.hpp create mode 100644 tjp/core/time-obsolete/Notes.txt create mode 100644 tjp/core/time-obsolete/Scheduler+Retry.hpp create mode 100644 tjp/core/time-obsolete/Scheduler.cpp create mode 100644 tjp/core/time-obsolete/Scheduler.h create mode 100644 tjp/core/time-obsolete/Scheduler.hpp create mode 100644 tjp/core/time-obsolete/Scheduler.inl create mode 100644 tjp/core/time-obsolete/SchedulerManual.cpp create mode 100644 tjp/core/time-obsolete/SchedulerManual.h create mode 100644 tjp/core/time-obsolete/SchedulerManual.hpp create mode 100644 tjp/core/time-obsolete/SchedulerPool.cpp create mode 100644 tjp/core/time-obsolete/SchedulerPool.h create mode 100644 tjp/core/time-obsolete/SchedulerPool.hpp create mode 100644 tjp/core/time-obsolete/Time+Test.cpp create mode 100644 tjp/core/time-obsolete/Time.h create mode 100644 tjp/core/time-obsolete/TimeSource+Test.cpp create mode 100644 tjp/core/time-obsolete/TimeSource.h create mode 100644 tjp/core/time-obsolete/TimeSource.hpp create mode 100644 tjp/core/time-obsolete/Time_v1.cpp create mode 100644 tjp/core/time-obsolete/Time_v1.h create mode 100644 tjp/core/time-obsolete/Time_v2.cpp create mode 100644 tjp/core/time-obsolete/Time_v2.h create mode 100644 tjp/core/time-obsolete/Time_v2.inl create mode 100755 tjp/core/url/URL.cpp create mode 100755 tjp/core/url/URL.h create mode 100755 tjp/core/url/URL.inl create mode 100755 tjp/core/url/_tests/URL.cpp create mode 100755 tjp/core/zip/Compress+zlib.cpp create mode 100755 tjp/core/zip/Compress+zstd.cpp create mode 100755 tjp/core/zip/Compress.h create mode 100755 tjp/core/zip/Decompress.cpp create mode 100755 tjp/core/zip/Decompress.h create mode 100755 tjp/core/zip/Exception.h create mode 100755 tjp/core/zip/MemoryBuffer.cpp create mode 100755 tjp/core/zip/MemoryBuffer.h create mode 100755 tjp/core/zip/MemoryBuffer.hpp create mode 100644 tjp/core/zip/VirtualFileSystem.cpp create mode 100644 tjp/core/zip/VirtualFileSystem.h create mode 100644 tjp/core/zip/VirtualFileSystem.hpp create mode 100755 tjp/core/zip/_delete/Compression.cpp create mode 100755 tjp/core/zip/_delete/Compression.h create mode 100755 tjp/core/zip/_delete/Compression.hpp create mode 100755 tjp/core/zip/_delete/Compression.inl create mode 100644 tjp/core/zip/_delete/_tests/Compression.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..54958c9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +*.pyc +xcuserdata +.bin diff --git a/Core_Misc.xcodeproj/project.pbxproj b/Core_Misc.xcodeproj/project.pbxproj new file mode 100644 index 0000000..e74ad29 --- /dev/null +++ b/Core_Misc.xcodeproj/project.pbxproj @@ -0,0 +1,1753 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 55; + objects = { + +/* Begin PBXBuildFile section */ + F608AA9328270416005C276B /* Args.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA3B2826BCC3005C276B /* Args.cpp */; }; + F608AA9B28270442005C276B /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA9A28270442005C276B /* main.cpp */; }; + F608AB5D2827ECDC005C276B /* PROFILE_SCOPE.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AB592827ECD8005C276B /* PROFILE_SCOPE.cpp */; }; + F608AB5E2827ECE0005C276B /* PROFILE_SIGNAL.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AB5A2827ECD8005C276B /* PROFILE_SIGNAL.cpp */; }; + F61DB8F12857AB7B00B74C99 /* Decompress.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6DEACC42831BE190001467D /* Decompress.cpp */; }; + F61DB8F22857AB7B00B74C99 /* Decompress.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6DEACC42831BE190001467D /* Decompress.cpp */; }; + F61F9BE22C6E2E6500F79137 /* CircularBuffer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6F363C329A649DA00D9F857 /* CircularBuffer.cpp */; }; + F62324072ED7AC6900307810 /* Queue.hpp in Sources */ = {isa = PBXBuildFile; fileRef = F62324062ED7AC6900307810 /* Queue.hpp */; }; + F62324092ED7AC9200307810 /* Queue.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F62324082ED7AC9200307810 /* Queue.hpp */; }; + F62919FA2CB6ADD5002060E0 /* Parallel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F62919F62CB6ADD5002060E0 /* Parallel.cpp */; }; + F62919FB2CB6ADD5002060E0 /* Parallel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F62919F62CB6ADD5002060E0 /* Parallel.cpp */; }; + F62919FC2CB6ADD5002060E0 /* Parallel.h in Headers */ = {isa = PBXBuildFile; fileRef = F62919F72CB6ADD5002060E0 /* Parallel.h */; }; + F62919FD2CB6ADD5002060E0 /* Parallel.h in Headers */ = {isa = PBXBuildFile; fileRef = F62919F72CB6ADD5002060E0 /* Parallel.h */; }; + F62919FE2CB6ADD5002060E0 /* Parallel.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F62919F82CB6ADD5002060E0 /* Parallel.hpp */; }; + F62919FF2CB6ADD5002060E0 /* Parallel.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F62919F82CB6ADD5002060E0 /* Parallel.hpp */; }; + F62B77232EBCF06100C7865D /* Headers.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F62B77212EBCEDBA00C7865D /* Headers.hpp */; }; + F62B77242EBCF06100C7865D /* Headers.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F62B77212EBCEDBA00C7865D /* Headers.hpp */; }; + F62B77252EBCF17F00C7865D /* Headers.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F62B77222EBCEED000C7865D /* Headers.cpp */; }; + F62B77262EBCF17F00C7865D /* Headers.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F62B77222EBCEED000C7865D /* Headers.cpp */; }; + F62B77292EBD42D300C7865D /* Internet+Internal.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F62B76F12EBB846800C7865D /* Internet+Internal.cpp */; }; + F62B772A2EBD42D300C7865D /* Internet+Internal.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F62B76F12EBB846800C7865D /* Internet+Internal.cpp */; }; + F62C1BDE28621C660093EFBE /* GlobalResourceVersion.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F62C1BDC28621C660093EFBE /* GlobalResourceVersion.cpp */; }; + F62C1BDF28621C660093EFBE /* GlobalResourceVersion.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F62C1BDC28621C660093EFBE /* GlobalResourceVersion.cpp */; }; + F62F1CF32A7DB33500F838D3 /* ProcessUsage.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F62F1CF22A7DB1D600F838D3 /* ProcessUsage.cpp */; }; + F62F1CF42A7DB33500F838D3 /* ProcessUsage.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F62F1CF22A7DB1D600F838D3 /* ProcessUsage.cpp */; }; + F63D28FC2885E7030042E6BE /* GlobalResourceVersion+IO.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F63D28FB2885E6D00042E6BE /* GlobalResourceVersion+IO.cpp */; }; + F63D28FD2885E7040042E6BE /* GlobalResourceVersion+IO.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F63D28FB2885E6D00042E6BE /* GlobalResourceVersion+IO.cpp */; }; + F63D295C2885F7F40042E6BE /* Directories.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608A9F42826BCC2005C276B /* Directories.cpp */; }; + F63D295D2885F7F50042E6BE /* Directories.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608A9F42826BCC2005C276B /* Directories.cpp */; }; + F63D85172D10CD5D00FF3624 /* VirtualFileSystem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F63D85152D10CB9400FF3624 /* VirtualFileSystem.cpp */; }; + F63D85192D10CD6000FF3624 /* VirtualFileSystem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F63D85152D10CB9400FF3624 /* VirtualFileSystem.cpp */; }; + F63D85202D10E21C00FF3624 /* MemoryBuffer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F63D851D2D10DC9B00FF3624 /* MemoryBuffer.cpp */; }; + F63D85212D10E21D00FF3624 /* MemoryBuffer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F63D851D2D10DC9B00FF3624 /* MemoryBuffer.cpp */; }; + F63D85282D10ECB100FF3624 /* VirtualFileSystem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F63D85262D10ECB100FF3624 /* VirtualFileSystem.cpp */; }; + F63D85292D10ECB100FF3624 /* VirtualFileSystem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F63D85262D10ECB100FF3624 /* VirtualFileSystem.cpp */; }; + F63E1E8C2E97E1AD000D0C5E /* File_Messy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F63E1E8B2E97E1AD000D0C5E /* File_Messy.cpp */; }; + F63E1E8D2E97E1AD000D0C5E /* File_Messy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F63E1E8B2E97E1AD000D0C5E /* File_Messy.cpp */; }; + F643F21928293721000B8B40 /* NetworkUsage.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AB8228281A0D005C276B /* NetworkUsage.cpp */; }; + F643F21A28293721000B8B40 /* CpuUsage.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AB8328281A0D005C276B /* CpuUsage.cpp */; }; + F644FB7628B1699A0022D49F /* StringLookup.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F644FB7528B131CF0022D49F /* StringLookup.cpp */; }; + F644FB7728B1699A0022D49F /* StringLookup.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F644FB7528B131CF0022D49F /* StringLookup.cpp */; }; + F64D83782E4C0F9C009AD431 /* call_with_pairs.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F64D83752E4C0ED6009AD431 /* call_with_pairs.cpp */; }; + F6623D6428788B44007FE5AF /* GlobalResource.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6623D6128788AC2007FE5AF /* GlobalResource.cpp */; }; + F6623D6C28788BC6007FE5AF /* libCore_Misc.a in Frameworks */ = {isa = PBXBuildFile; fileRef = F608A9AC2826B7DE005C276B /* libCore_Misc.a */; }; + F67B0085286738A2002A7A43 /* Signal.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F67B0084286737CD002A7A43 /* Signal.cpp */; }; + F67B0086286738A3002A7A43 /* Signal.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F67B0084286737CD002A7A43 /* Signal.cpp */; }; + F683F5222A67224000191850 /* Delegate.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F683F51F2A670A1500191850 /* Delegate.cpp */; }; + F683F5252A674E0C00191850 /* Delegate+Single.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F683F5242A674E0C00191850 /* Delegate+Single.cpp */; }; + F68BA83D2E93E10C0040FA70 /* Internet.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F68BA83B2E93E10C0040FA70 /* Internet.hpp */; }; + F68BA83E2E93E10C0040FA70 /* Internet.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F68BA83C2E93E10C0040FA70 /* Internet.cpp */; }; + F69534682D84E1F700C9AFA0 /* Internet+common.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F69534662D84E10500C9AFA0 /* Internet+common.cpp */; }; + F695488C28288573005D1B64 /* SystemUsage.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AB8528281A0D005C276B /* SystemUsage.cpp */; }; + F695488E2828857F005D1B64 /* Random.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA2A2826BCC2005C276B /* Random.cpp */; }; + F695488F28288585005D1B64 /* Pipe.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608A9F02826BCC2005C276B /* Pipe.cpp */; }; + F69548902828858B005D1B64 /* URL.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA202826BCC2005C276B /* URL.cpp */; }; + F695489128288590005D1B64 /* Initializer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA372826BCC2005C276B /* Initializer.cpp */; }; + F695489228288596005D1B64 /* writeSegmentsToFile.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608A9F72826BCC2005C276B /* writeSegmentsToFile.cpp */; }; + F69548932828859B005D1B64 /* Path.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608A9FB2826BCC2005C276B /* Path.cpp */; }; + F6954894282885A0005D1B64 /* Extensible.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA092826BCC2005C276B /* Extensible.cpp */; }; + F6954895282885A5005D1B64 /* Endian.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608A9FF2826BCC2005C276B /* Endian.cpp */; }; + F6954897282885AE005D1B64 /* Csv.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA2E2826BCC2005C276B /* Csv.cpp */; }; + F6954898282885B1005D1B64 /* Csv.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA2F2826BCC2005C276B /* Csv.cpp */; }; + F6954899282885B6005D1B64 /* Base64.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA332826BCC2005C276B /* Base64.cpp */; }; + F695489A282885B6005D1B64 /* Base16.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA342826BCC2005C276B /* Base16.cpp */; }; + F695489D28288C1F005D1B64 /* Internet.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA242826BCC2005C276B /* Internet.cpp */; }; + F69548AD28289092005D1B64 /* URL.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F69548A92828908C005D1B64 /* URL.cpp */; }; + F69548AE28289095005D1B64 /* URL.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F69548AA2828908C005D1B64 /* URL.cpp */; }; + F6971F5E282B124D008FBD17 /* URL.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F69548AA2828908C005D1B64 /* URL.cpp */; }; + F6971F60282B124D008FBD17 /* Base64.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA332826BCC2005C276B /* Base64.cpp */; }; + F6971F62282B124D008FBD17 /* Endian.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608A9FF2826BCC2005C276B /* Endian.cpp */; }; + F6971F63282B124D008FBD17 /* URL.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA202826BCC2005C276B /* URL.cpp */; }; + F6971F65282B124D008FBD17 /* CpuUsage.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AB8328281A0D005C276B /* CpuUsage.cpp */; }; + F6971F67282B124D008FBD17 /* PROFILE_SCOPE.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AB592827ECD8005C276B /* PROFILE_SCOPE.cpp */; }; + F6971F6A282B124D008FBD17 /* Random.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA2A2826BCC2005C276B /* Random.cpp */; }; + F6971F6B282B124D008FBD17 /* Path.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608A9FB2826BCC2005C276B /* Path.cpp */; }; + F6971F6C282B124D008FBD17 /* Base16.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA342826BCC2005C276B /* Base16.cpp */; }; + F6971F6D282B124D008FBD17 /* Initializer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA372826BCC2005C276B /* Initializer.cpp */; }; + F6971F6E282B124D008FBD17 /* Csv.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA2E2826BCC2005C276B /* Csv.cpp */; }; + F6971F6F282B124D008FBD17 /* Args.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA3B2826BCC3005C276B /* Args.cpp */; }; + F6971F70282B124D008FBD17 /* Extensible.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA092826BCC2005C276B /* Extensible.cpp */; }; + F6971F71282B124D008FBD17 /* NetworkUsage.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AB8228281A0D005C276B /* NetworkUsage.cpp */; }; + F6971F72282B124D008FBD17 /* SystemUsage.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AB8528281A0D005C276B /* SystemUsage.cpp */; }; + F6971F73282B124D008FBD17 /* writeSegmentsToFile.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608A9F72826BCC2005C276B /* writeSegmentsToFile.cpp */; }; + F6971F74282B124D008FBD17 /* PROFILE_SIGNAL.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AB5A2827ECD8005C276B /* PROFILE_SIGNAL.cpp */; }; + F6971FB5282B1751008FBD17 /* Internet.mm in Sources */ = {isa = PBXBuildFile; fileRef = F608AA222826BCC2005C276B /* Internet.mm */; }; + F6992C662C28B234003EBEA2 /* join.hpp in Sources */ = {isa = PBXBuildFile; fileRef = F6992C652C28B234003EBEA2 /* join.hpp */; }; + F6992C672C28B234003EBEA2 /* join.hpp in Sources */ = {isa = PBXBuildFile; fileRef = F6992C652C28B234003EBEA2 /* join.hpp */; }; + F6A3330D2BD4928E0092BFA7 /* Base64.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6A3330C2BD492160092BFA7 /* Base64.cpp */; }; + F6A8B2A72A63435D00E4FDC7 /* Extensible.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AA0A2826BCC2005C276B /* Extensible.cpp */; }; + F6B9FE3D2D8B5434002831BC /* Server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6B9FE3B2D8B534D002831BC /* Server.cpp */; }; + F6BB293F2DD95AE10061D8D3 /* Compress+zstd.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6BB293E2DD95AE10061D8D3 /* Compress+zstd.cpp */; }; + F6BB29402DD95AE10061D8D3 /* Compress+zstd.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6BB293E2DD95AE10061D8D3 /* Compress+zstd.cpp */; }; + F6BBFE4D2E969AA8009E9C66 /* PinnedFutures.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F6BBFE4C2E969AA8009E9C66 /* PinnedFutures.hpp */; }; + F6BBFE4E2E969AA8009E9C66 /* PinnedFutures.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F6BBFE4C2E969AA8009E9C66 /* PinnedFutures.hpp */; }; + F6DD12B32BAF71940040A088 /* AutoThreadPool.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F608AB632827EE07005C276B /* AutoThreadPool.cpp */; }; + F6DEACCA2831BE230001467D /* Compress+zlib.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6DEACC82831BE190001467D /* Compress+zlib.cpp */; }; + F6DEACCE2831BE250001467D /* Compress+zlib.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6DEACC82831BE190001467D /* Compress+zlib.cpp */; }; + F6DF53662DBAEFBD00FF4522 /* File.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F63D850C2D10970C00FF3624 /* File.cpp */; }; + F6DF53672DBAEFBD00FF4522 /* File.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F63D850C2D10970C00FF3624 /* File.cpp */; }; + F6DF53742DBE624D00FF4522 /* Base32.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6DF53732DBE624D00FF4522 /* Base32.cpp */; }; + F6DF53752DBE624D00FF4522 /* Base32.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6DF53732DBE624D00FF4522 /* Base32.cpp */; }; + F6DF53772DBE6AB700FF4522 /* Base32.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6DF53762DBE6AB700FF4522 /* Base32.cpp */; }; + F6DF53792DBE6AC200FF4522 /* Base16.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6DF53782DBE6AC200FF4522 /* Base16.cpp */; }; + F6E5265D28849865003BB6F1 /* fasthash.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6E5265B28849865003BB6F1 /* fasthash.cpp */; }; + F6E5265E28849865003BB6F1 /* fasthash.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6E5265B28849865003BB6F1 /* fasthash.cpp */; }; + F6E5265F28849865003BB6F1 /* fasthash.h in Headers */ = {isa = PBXBuildFile; fileRef = F6E5265C28849865003BB6F1 /* fasthash.h */; }; + F6E5266028849865003BB6F1 /* fasthash.h in Headers */ = {isa = PBXBuildFile; fileRef = F6E5265C28849865003BB6F1 /* fasthash.h */; }; + F6E5266128849916003BB6F1 /* Hash.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6E5265A288469D7003BB6F1 /* Hash.cpp */; }; + F6E5266228849916003BB6F1 /* Hash.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6E5265A288469D7003BB6F1 /* Hash.cpp */; }; + F6E71934282C185200EEB429 /* Base64URL.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6E71932282C16DA00EEB429 /* Base64URL.cpp */; }; + F6E71935282C185200EEB429 /* Base64URL.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6E71932282C16DA00EEB429 /* Base64URL.cpp */; }; + F6EB9F5D2858ACF8007B3EFB /* Queue.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6EB9F5C2858ACF8007B3EFB /* Queue.cpp */; }; + F6EB9F622858B92E007B3EFB /* Queue.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6EB9F612858B92E007B3EFB /* Queue.cpp */; }; + F6EC55EF2ECBA3C10052CD0A /* get_files.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6EC55EE2ECBA3C10052CD0A /* get_files.cpp */; }; + F6EC55F02ECBA3C10052CD0A /* get_files.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6EC55EE2ECBA3C10052CD0A /* get_files.cpp */; }; + F6F1823A2C06A47100D7005F /* zlib.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6F182392C06A3D000D7005F /* zlib.cpp */; }; + F6F1823B2C06A47100D7005F /* zlib.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6F182392C06A3D000D7005F /* zlib.cpp */; }; + F6F182492C07652700D7005F /* zlib.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6F182422C075F3700D7005F /* zlib.cpp */; }; + F6F1824E2C07A62000D7005F /* zstd.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6F1824C2C07A31300D7005F /* zstd.cpp */; }; + F6F1824F2C07A62100D7005F /* zstd.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6F1824C2C07A31300D7005F /* zstd.cpp */; }; + F6F182502C07A9F500D7005F /* zstd.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6F1824D2C07A31300D7005F /* zstd.cpp */; }; + F6F363C729A649DA00D9F857 /* CircularBuffer.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F6F363C429A649DA00D9F857 /* CircularBuffer.hpp */; }; + F6F363C829A649DA00D9F857 /* CircularBuffer.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F6F363C429A649DA00D9F857 /* CircularBuffer.hpp */; }; + F6F79A602F3F77DE008BF0C6 /* Server.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F6F79A5F2F3F77DE008BF0C6 /* Server.hpp */; }; + F6F79A622F3F7A0B008BF0C6 /* Server.h in Headers */ = {isa = PBXBuildFile; fileRef = F6F79A612F3F7A0B008BF0C6 /* Server.h */; }; + F6F79A662F3F8BF0008BF0C6 /* Server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6F79A5D2F3F7656008BF0C6 /* Server.cpp */; }; + F6F79A6A2F3FEE3F008BF0C6 /* Server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6F79A682F3FED1F008BF0C6 /* Server.cpp */; }; + F6F79A912F40CA36008BF0C6 /* Server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6F79A902F40CA36008BF0C6 /* Server.cpp */; }; + F6F79A922F40E88F008BF0C6 /* Server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6F79A8C2F40C3EA008BF0C6 /* Server.cpp */; }; + F6F7A1612F466052008BF0C6 /* beast.hpp in Sources */ = {isa = PBXBuildFile; fileRef = F6F7A1602F466052008BF0C6 /* beast.hpp */; }; + F6F970BF2D7771E6003B4E63 /* Path.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6F970BE2D777179003B4E63 /* Path.cpp */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + F608AA9628270442005C276B /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + F6028B012A2FDCEC00CAD87D /* _remove */ = {isa = PBXFileReference; lastKnownFileType = folder; path = _remove; sourceTree = ""; }; + F60583302EC37BB8008FE966 /* directory_of.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = directory_of.hpp; sourceTree = ""; }; + F60583312EC37BEC008FE966 /* directory_of.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = directory_of.cpp; sourceTree = ""; }; + F605835D2EC3A6F4008FE966 /* url_path_decode.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = url_path_decode.hpp; sourceTree = ""; }; + F605835E2EC3A6F4008FE966 /* url_path_encode.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = url_path_encode.hpp; sourceTree = ""; }; + F60583FA2EC4E99F008FE966 /* split_first_directory.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = split_first_directory.hpp; sourceTree = ""; }; + F60583FB2EC4E99F008FE966 /* split_first_directory.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = split_first_directory.cpp; sourceTree = ""; }; + F60584A32EC60F81008FE966 /* move.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = move.hpp; sourceTree = ""; }; + F60584A42EC60F9C008FE966 /* move.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = move.cpp; sourceTree = ""; }; + F60584A52EC61172008FE966 /* file_exists.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = file_exists.hpp; sourceTree = ""; }; + F60584A62EC6117A008FE966 /* file_exists.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = file_exists.cpp; sourceTree = ""; }; + F60584A72EC61257008FE966 /* unlink.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = unlink.hpp; sourceTree = ""; }; + F60584A82EC61257008FE966 /* unlink.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = unlink.cpp; sourceTree = ""; }; + F60584B02EC618C2008FE966 /* copy.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = copy.hpp; sourceTree = ""; }; + F60584B12EC618C2008FE966 /* copy.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = copy.cpp; sourceTree = ""; }; + F60584B22EC61928008FE966 /* read.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = read.hpp; sourceTree = ""; }; + F60584B32EC61928008FE966 /* read.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = read.cpp; sourceTree = ""; }; + F60584B42EC619B2008FE966 /* write.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = write.hpp; sourceTree = ""; }; + F60584B52EC619B2008FE966 /* write.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = write.cpp; sourceTree = ""; }; + F60584B62EC61C3B008FE966 /* times.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = times.hpp; sourceTree = ""; }; + F60584B72EC61C3B008FE966 /* times.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = times.cpp; sourceTree = ""; }; + F60892582865EC8E00174811 /* LocalResourceVersion+IO.inl */ = {isa = PBXFileReference; lastKnownFileType = text; path = "LocalResourceVersion+IO.inl"; sourceTree = ""; }; + F608A9AC2826B7DE005C276B /* libCore_Misc.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libCore_Misc.a; sourceTree = BUILT_PRODUCTS_DIR; }; + F608A9EC2826BCC2005C276B /* Pipe.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Pipe.cpp; sourceTree = ""; }; + F608A9ED2826BCC2005C276B /* Pipe.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Pipe.h; sourceTree = ""; }; + F608A9EE2826BCC2005C276B /* Pipe.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Pipe.h; sourceTree = ""; }; + F608A9EF2826BCC2005C276B /* Pipe.inl */ = {isa = PBXFileReference; lastKnownFileType = text; path = Pipe.inl; sourceTree = ""; }; + F608A9F02826BCC2005C276B /* Pipe.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Pipe.cpp; sourceTree = ""; }; + F608A9F32826BCC2005C276B /* Directories.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Directories.h; sourceTree = ""; }; + F608A9F42826BCC2005C276B /* Directories.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Directories.cpp; sourceTree = ""; }; + F608A9F62826BCC2005C276B /* File.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = File.h; sourceTree = ""; }; + F608A9F72826BCC2005C276B /* writeSegmentsToFile.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = writeSegmentsToFile.cpp; sourceTree = ""; }; + F608A9F92826BCC2005C276B /* Directories.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Directories.h; sourceTree = ""; }; + F608A9FA2826BCC2005C276B /* Directories.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Directories.cpp; sourceTree = ""; }; + F608A9FB2826BCC2005C276B /* Path.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Path.cpp; sourceTree = ""; }; + F608A9FC2826BCC2005C276B /* Directories.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Directories.h; sourceTree = ""; }; + F608A9FE2826BCC2005C276B /* Endian.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Endian.h; sourceTree = ""; }; + F608A9FF2826BCC2005C276B /* Endian.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Endian.cpp; sourceTree = ""; }; + F608AA012826BCC2005C276B /* Dispatch.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Dispatch.hpp; sourceTree = ""; }; + F608AA032826BCC2005C276B /* Dispatch.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Dispatch.h; sourceTree = ""; }; + F608AA092826BCC2005C276B /* Extensible.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Extensible.cpp; sourceTree = ""; }; + F608AA0A2826BCC2005C276B /* Extensible.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Extensible.cpp; sourceTree = ""; }; + F608AA0B2826BCC2005C276B /* Extensible.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Extensible.h; sourceTree = ""; }; + F608AA1E2826BCC2005C276B /* Internet.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Internet.hpp; sourceTree = ""; }; + F608AA1F2826BCC2005C276B /* Internet.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Internet.h; sourceTree = ""; }; + F608AA202826BCC2005C276B /* URL.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = URL.cpp; sourceTree = ""; }; + F608AA222826BCC2005C276B /* Internet.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = Internet.mm; sourceTree = ""; }; + F608AA242826BCC2005C276B /* Internet.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Internet.cpp; sourceTree = ""; }; + F608AA252826BCC2005C276B /* URL.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = URL.h; sourceTree = ""; }; + F608AA272826BCC2005C276B /* LocalResourceVersion.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = LocalResourceVersion.hpp; sourceTree = ""; }; + F608AA2A2826BCC2005C276B /* Random.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Random.cpp; sourceTree = ""; }; + F608AA2B2826BCC2005C276B /* Random.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Random.h; sourceTree = ""; }; + F608AA2D2826BCC2005C276B /* Csv.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Csv.h; sourceTree = ""; }; + F608AA2E2826BCC2005C276B /* Csv.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Csv.cpp; sourceTree = ""; }; + F608AA2F2826BCC2005C276B /* Csv.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Csv.cpp; sourceTree = ""; }; + F608AA302826BCC2005C276B /* Csv.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Csv.hpp; sourceTree = ""; }; + F608AA322826BCC2005C276B /* Base64.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Base64.h; sourceTree = ""; }; + F608AA332826BCC2005C276B /* Base64.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Base64.cpp; sourceTree = ""; }; + F608AA342826BCC2005C276B /* Base16.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Base16.cpp; sourceTree = ""; }; + F608AA352826BCC2005C276B /* Base16.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Base16.h; sourceTree = ""; }; + F608AA372826BCC2005C276B /* Initializer.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Initializer.cpp; sourceTree = ""; }; + F608AA382826BCC2005C276B /* Initializer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Initializer.h; sourceTree = ""; }; + F608AA392826BCC2005C276B /* Initializer.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Initializer.hpp; sourceTree = ""; }; + F608AA3B2826BCC3005C276B /* Args.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Args.cpp; sourceTree = ""; }; + F608AA3C2826BCC3005C276B /* Args.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Args.h; sourceTree = ""; }; + F608AA9828270442005C276B /* Core_Misc_Tests */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Core_Misc_Tests; sourceTree = BUILT_PRODUCTS_DIR; }; + F608AA9A28270442005C276B /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; + F608AB592827ECD8005C276B /* PROFILE_SCOPE.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = PROFILE_SCOPE.cpp; sourceTree = ""; }; + F608AB5A2827ECD8005C276B /* PROFILE_SIGNAL.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = PROFILE_SIGNAL.cpp; sourceTree = ""; }; + F608AB5B2827ECD8005C276B /* PROFILE_SIGNAL.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PROFILE_SIGNAL.h; sourceTree = ""; }; + F608AB5C2827ECD8005C276B /* PROFILE_SCOPE.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PROFILE_SCOPE.h; sourceTree = ""; }; + F608AB612827EE07005C276B /* FunctionalProcessor.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = FunctionalProcessor.hpp; sourceTree = ""; }; + F608AB622827EE07005C276B /* AutoThreadPool.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = AutoThreadPool.hpp; sourceTree = ""; }; + F608AB632827EE07005C276B /* AutoThreadPool.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = AutoThreadPool.cpp; sourceTree = ""; }; + F608AB6F2827F3DA005C276B /* Makefile.def */ = {isa = PBXFileReference; lastKnownFileType = text; path = Makefile.def; sourceTree = ""; }; + F608AB702827F3DA005C276B /* Makefile.project */ = {isa = PBXFileReference; lastKnownFileType = text; path = Makefile.project; sourceTree = ""; }; + F608AB7F282814E8005C276B /* Delegate.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Delegate.hpp; sourceTree = ""; }; + F608AB8228281A0D005C276B /* NetworkUsage.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = NetworkUsage.cpp; sourceTree = ""; }; + F608AB8328281A0D005C276B /* CpuUsage.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = CpuUsage.cpp; sourceTree = ""; }; + F608AB8528281A0D005C276B /* SystemUsage.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = SystemUsage.cpp; sourceTree = ""; }; + F608AB8728281A0D005C276B /* CpuUsage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CpuUsage.h; sourceTree = ""; }; + F608AB8828281A0D005C276B /* NetworkUsage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NetworkUsage.h; sourceTree = ""; }; + F608AB8928281A0D005C276B /* SystemUsage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SystemUsage.h; sourceTree = ""; }; + F608AB8B28281A0D005C276B /* NetworkUsage.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = NetworkUsage.cpp; sourceTree = ""; }; + F608AB8C28281A0D005C276B /* CpuUsage.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = CpuUsage.cpp; sourceTree = ""; }; + F608AB8D28281A0D005C276B /* SystemUsage.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = SystemUsage.hpp; sourceTree = ""; }; + F6175A5E2CB2FB73008A9646 /* to_cstring.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = to_cstring.hpp; sourceTree = ""; }; + F6175A692CB3162C008A9646 /* xor_inplace.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = xor_inplace.hpp; sourceTree = ""; }; + F61E82A82D9187FD00EF6FFF /* form_encode.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = form_encode.hpp; sourceTree = ""; }; + F61E82AA2D91951B00EF6FFF /* transform_plaintext.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = transform_plaintext.hpp; sourceTree = ""; }; + F61F9BEB2C6E306B00F79137 /* Makefile.project */ = {isa = PBXFileReference; lastKnownFileType = text; path = Makefile.project; sourceTree = ""; }; + F6226CA22D971F40009F9761 /* url_param_decode.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = url_param_decode.hpp; sourceTree = ""; }; + F62324062ED7AC6900307810 /* Queue.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Queue.hpp; sourceTree = ""; }; + F62324082ED7AC9200307810 /* Queue.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Queue.hpp; sourceTree = ""; }; + F62919F62CB6ADD5002060E0 /* Parallel.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Parallel.cpp; sourceTree = ""; }; + F62919F72CB6ADD5002060E0 /* Parallel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Parallel.h; sourceTree = ""; }; + F62919F82CB6ADD5002060E0 /* Parallel.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = Parallel.hpp; sourceTree = ""; }; + F62AABF52EC0051B00A01727 /* file_name.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = file_name.hpp; sourceTree = ""; }; + F62AABF62EC0065E00A01727 /* temporary_path.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = temporary_path.hpp; sourceTree = ""; }; + F62B76EF2EBACB3F00C7865D /* transform_binary.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = transform_binary.hpp; sourceTree = ""; }; + F62B76F12EBB846800C7865D /* Internet+Internal.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = "Internet+Internal.cpp"; sourceTree = ""; }; + F62B77202EBCED2400C7865D /* Data.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Data.hpp; sourceTree = ""; }; + F62B77212EBCEDBA00C7865D /* Headers.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Headers.hpp; sourceTree = ""; }; + F62B77222EBCEED000C7865D /* Headers.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Headers.cpp; sourceTree = ""; }; + F62B77282EBD0B3E00C7865D /* Internet+Internal.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = "Internet+Internal.hpp"; sourceTree = ""; }; + F62C1B8D28614B400093EFBE /* optional_function.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = optional_function.h; sourceTree = ""; }; + F62C1B8E28614B530093EFBE /* optional_function.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = optional_function.hpp; sourceTree = ""; }; + F62C1BD528620FE80093EFBE /* ExtensibleWith.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ExtensibleWith.h; sourceTree = ""; }; + F62C1BDC28621C660093EFBE /* GlobalResourceVersion.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = GlobalResourceVersion.cpp; sourceTree = ""; }; + F62C1BDD28621C660093EFBE /* GlobalResourceVersion.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = GlobalResourceVersion.hpp; sourceTree = ""; }; + F62F1CF12A7DB18400F838D3 /* ProcessUsage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ProcessUsage.h; sourceTree = ""; }; + F62F1CF22A7DB1D600F838D3 /* ProcessUsage.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ProcessUsage.cpp; sourceTree = ""; }; + F63D28FB2885E6D00042E6BE /* GlobalResourceVersion+IO.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = "GlobalResourceVersion+IO.cpp"; sourceTree = ""; }; + F63D84FA2D1079BC00FF3624 /* httplib.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = httplib.h; sourceTree = ""; }; + F63D850A2D108E4A00FF3624 /* reverse_inplace.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = reverse_inplace.hpp; sourceTree = ""; }; + F63D850B2D10968300FF3624 /* writeSegmentsToFile.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = writeSegmentsToFile.h; sourceTree = ""; }; + F63D850C2D10970C00FF3624 /* File.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = File.cpp; sourceTree = ""; }; + F63D85142D10C7C400FF3624 /* VirtualFileSystem.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VirtualFileSystem.h; sourceTree = ""; }; + F63D85152D10CB9400FF3624 /* VirtualFileSystem.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = VirtualFileSystem.cpp; sourceTree = ""; }; + F63D85162D10CBAA00FF3624 /* VirtualFileSystem.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = VirtualFileSystem.hpp; sourceTree = ""; }; + F63D851B2D10D23300FF3624 /* _delete */ = {isa = PBXFileReference; lastKnownFileType = folder; path = _delete; sourceTree = ""; }; + F63D851C2D10DC4C00FF3624 /* MemoryBuffer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MemoryBuffer.h; sourceTree = ""; }; + F63D851D2D10DC9B00FF3624 /* MemoryBuffer.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = MemoryBuffer.cpp; sourceTree = ""; }; + F63D851E2D10DCF300FF3624 /* MemoryBuffer.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = MemoryBuffer.hpp; sourceTree = ""; }; + F63D85222D10E6DB00FF3624 /* Contents.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Contents.h; sourceTree = ""; }; + F63D85242D10E76E00FF3624 /* Path.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Path.h; sourceTree = ""; }; + F63D85252D10ECB100FF3624 /* VirtualFileSystem.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VirtualFileSystem.h; sourceTree = ""; }; + F63D85262D10ECB100FF3624 /* VirtualFileSystem.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = VirtualFileSystem.cpp; sourceTree = ""; }; + F63D85272D10ECB100FF3624 /* VirtualFileSystem.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = VirtualFileSystem.hpp; sourceTree = ""; }; + F63E1E8B2E97E1AD000D0C5E /* File_Messy.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = File_Messy.cpp; sourceTree = ""; }; + F6422B102D8CA04A00FAE1FC /* url_param_encode.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = url_param_encode.hpp; sourceTree = ""; }; + F6422B112D8CA21500FAE1FC /* future_json_to_result.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = future_json_to_result.hpp; sourceTree = ""; }; + F644FB7428B130DA0022D49F /* MysqlConnection.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MysqlConnection.h; sourceTree = ""; }; + F644FB7528B131CF0022D49F /* StringLookup.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = StringLookup.cpp; sourceTree = ""; }; + F64A5DF92A59573100FE7EEF /* Runnable.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Runnable.hpp; sourceTree = ""; }; + F64A5DFA2A5957B800FE7EEF /* Runnable.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Runnable.h; sourceTree = ""; }; + F64D83732E4C0EAD009AD431 /* call_with_pairs.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = call_with_pairs.hpp; sourceTree = ""; }; + F64D83752E4C0ED6009AD431 /* call_with_pairs.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = call_with_pairs.cpp; sourceTree = ""; }; + F650BA6828906F5F006D2316 /* GlobalResourceVersion.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GlobalResourceVersion.h; sourceTree = ""; }; + F65E7A792A43E00C003B2D0D /* Alias.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Alias.hpp; sourceTree = ""; }; + F6623D6128788AC2007FE5AF /* GlobalResource.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = GlobalResource.cpp; sourceTree = ""; }; + F6623D6628788BB7007FE5AF /* libCore_IO.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libCore_IO.a; sourceTree = BUILT_PRODUCTS_DIR; }; + F6623D6828788BB7007FE5AF /* libCore_Math.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libCore_Math.a; sourceTree = BUILT_PRODUCTS_DIR; }; + F6623D6A28788BB7007FE5AF /* libCore_Zero.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libCore_Zero.a; sourceTree = BUILT_PRODUCTS_DIR; }; + F66B7E982EB636E7009029EF /* transform_base64_text.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = transform_base64_text.hpp; sourceTree = ""; }; + F66B7EAD2EB64717009029EF /* url_with_params.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = url_with_params.hpp; sourceTree = ""; }; + F67B0082286737BF002A7A43 /* Signal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Signal.h; sourceTree = ""; }; + F67B0083286737C7002A7A43 /* Signal.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Signal.hpp; sourceTree = ""; }; + F67B0084286737CD002A7A43 /* Signal.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Signal.cpp; sourceTree = ""; }; + F683F51E2A670A0F00191850 /* Delegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Delegate.h; sourceTree = ""; }; + F683F51F2A670A1500191850 /* Delegate.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Delegate.cpp; sourceTree = ""; }; + F683F5202A6710C600191850 /* Delegate+Single.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = "Delegate+Single.hpp"; sourceTree = ""; }; + F683F5212A6710C600191850 /* Delegate+Single.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Delegate+Single.h"; sourceTree = ""; }; + F683F5242A674E0C00191850 /* Delegate+Single.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = "Delegate+Single.cpp"; sourceTree = ""; }; + F689727E2D8752EF0084C6A9 /* write_result.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = write_result.hpp; sourceTree = ""; }; + F68BA83B2E93E10C0040FA70 /* Internet.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Internet.hpp; sourceTree = ""; }; + F68BA83C2E93E10C0040FA70 /* Internet.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Internet.cpp; sourceTree = ""; }; + F68BD1AF2D99894B008BE7F4 /* get_redirect.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = get_redirect.hpp; sourceTree = ""; }; + F68BD1B02D998D77008BE7F4 /* get_embedded_value.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = get_embedded_value.hpp; sourceTree = ""; }; + F68BD1B12D998F3E008BE7F4 /* html_decode.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = html_decode.hpp; sourceTree = ""; }; + F68CE32C2D37F907004F9466 /* Changed.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Changed.hpp; sourceTree = ""; }; + F68F84BE2D8EF2C500245DB9 /* transform.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = transform.hpp; sourceTree = ""; }; + F68F84C22D8EFCD900245DB9 /* collapse_future.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = collapse_future.hpp; sourceTree = ""; }; + F68F84C42D8EFEC100245DB9 /* future_response.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = future_response.hpp; sourceTree = ""; }; + F69534662D84E10500C9AFA0 /* Internet+common.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = "Internet+common.cpp"; sourceTree = ""; }; + F69548A92828908C005D1B64 /* URL.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = URL.cpp; sourceTree = ""; }; + F69548AA2828908C005D1B64 /* URL.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = URL.cpp; sourceTree = ""; }; + F69548AB2828908C005D1B64 /* URL.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = URL.h; sourceTree = ""; }; + F69548AC2828908C005D1B64 /* URL.inl */ = {isa = PBXFileReference; lastKnownFileType = text; path = URL.inl; sourceTree = ""; }; + F6960CFF2D88F18E00767451 /* require_header.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = require_header.hpp; sourceTree = ""; }; + F6960D152D8AF93B00767451 /* transform_json.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = transform_json.hpp; sourceTree = ""; }; + F6971F79282B124D008FBD17 /* libCore_Misc_iOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libCore_Misc_iOS.a; sourceTree = BUILT_PRODUCTS_DIR; }; + F6984F4F2EF3429200718723 /* pop_first.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = pop_first.hpp; sourceTree = ""; }; + F6992C652C28B234003EBEA2 /* join.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = join.hpp; sourceTree = ""; }; + F69CC2572D2979D400FB29DE /* Sortable.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Sortable.hpp; sourceTree = ""; }; + F69CC2582D297B6D00FB29DE /* bell_curve.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = bell_curve.hpp; sourceTree = ""; }; + F6A3330C2BD492160092BFA7 /* Base64.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Base64.cpp; sourceTree = ""; }; + F6A8B2A12A633B9300E4FDC7 /* Extensible+CacheViaMap.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Extensible+CacheViaMap.h"; sourceTree = ""; }; + F6A8B2A22A633BA000E4FDC7 /* Extensible+CacheViaArrayAndMap.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Extensible+CacheViaArrayAndMap.h"; sourceTree = ""; }; + F6A8B2A32A633BB400E4FDC7 /* Extensible+CacheViaArray.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Extensible+CacheViaArray.h"; sourceTree = ""; }; + F6A8B2A42A633C8C00E4FDC7 /* Extensible+Enumerated.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Extensible+Enumerated.h"; sourceTree = ""; }; + F6A8B2A52A633CB000E4FDC7 /* Extensible+TypeInfo.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Extensible+TypeInfo.h"; sourceTree = ""; }; + F6B9FE3A2D8B5271002831BC /* Server.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Server.hpp; sourceTree = ""; }; + F6B9FE3B2D8B534D002831BC /* Server.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Server.cpp; sourceTree = ""; }; + F6B9FE3C2D8B5380002831BC /* Server.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Server.h; sourceTree = ""; }; + F6BB293E2DD95AE10061D8D3 /* Compress+zstd.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = "Compress+zstd.cpp"; sourceTree = ""; }; + F6BBFE4C2E969AA8009E9C66 /* PinnedFutures.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = PinnedFutures.hpp; sourceTree = ""; }; + F6C8F11E28EB0D1F00FE529A /* StringLookup.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = StringLookup.h; sourceTree = ""; }; + F6C8F11F28EB0DB400FE529A /* MysqlQueries.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MysqlQueries.h; sourceTree = ""; }; + F6CDE5B02F3CBDFD003720FD /* Queue.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Queue.h; sourceTree = ""; }; + F6CDE77B2F3F6418003720FD /* ArgsFile.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ArgsFile.h; sourceTree = ""; }; + F6CDE77C2F3F6432003720FD /* ArgsFile.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ArgsFile.cpp; sourceTree = ""; }; + F6DD12AF2BAF68210040A088 /* ObjectConsumer.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = ObjectConsumer.hpp; sourceTree = ""; }; + F6DD12B02BAF6BD40040A088 /* Base.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Base.h; sourceTree = ""; }; + F6DD12B22BAF6F900040A088 /* _delete */ = {isa = PBXFileReference; lastKnownFileType = folder; path = _delete; sourceTree = ""; }; + F6DD12B42BAF71C40040A088 /* AutoThreadPool.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AutoThreadPool.h; sourceTree = ""; }; + F6DDE7152DAD3577005F94FC /* transform_json_text.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = transform_json_text.hpp; sourceTree = ""; }; + F6DDE74F2DB03CAB005F94FC /* is_relative_path.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = is_relative_path.hpp; sourceTree = ""; }; + F6DEACC42831BE190001467D /* Decompress.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Decompress.cpp; sourceTree = ""; }; + F6DEACC52831BE190001467D /* Decompress.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Decompress.h; sourceTree = ""; }; + F6DEACC62831BE190001467D /* Compress.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Compress.h; sourceTree = ""; }; + F6DEACC72831BE190001467D /* Exception.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Exception.h; sourceTree = ""; }; + F6DEACC82831BE190001467D /* Compress+zlib.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = "Compress+zlib.cpp"; sourceTree = ""; }; + F6DF53722DBE624D00FF4522 /* Base32.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Base32.h; sourceTree = ""; }; + F6DF53732DBE624D00FF4522 /* Base32.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Base32.cpp; sourceTree = ""; }; + F6DF53762DBE6AB700FF4522 /* Base32.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Base32.cpp; sourceTree = ""; }; + F6DF53782DBE6AC200FF4522 /* Base16.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Base16.cpp; sourceTree = ""; }; + F6E4FEA628E1CF5600171E8C /* for_each_argument.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = for_each_argument.hpp; sourceTree = ""; }; + F6E526582884698A003BB6F1 /* Hash.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Hash.h; sourceTree = ""; }; + F6E5265A288469D7003BB6F1 /* Hash.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Hash.cpp; sourceTree = ""; }; + F6E5265B28849865003BB6F1 /* fasthash.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = fasthash.cpp; sourceTree = ""; }; + F6E5265C28849865003BB6F1 /* fasthash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = fasthash.h; sourceTree = ""; }; + F6E71932282C16DA00EEB429 /* Base64URL.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Base64URL.cpp; sourceTree = ""; }; + F6E71933282C16DC00EEB429 /* Base64URL.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Base64URL.h; sourceTree = ""; }; + F6E71957282C582A00EEB429 /* InPlace.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = InPlace.h; sourceTree = ""; }; + F6EA85FE2848D68800DED3C0 /* find_if_random.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = find_if_random.hpp; sourceTree = ""; }; + F6EB9F5B2858AC53007B3EFB /* Queue.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Queue.hpp; sourceTree = ""; }; + F6EB9F5C2858ACF8007B3EFB /* Queue.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Queue.cpp; sourceTree = ""; }; + F6EB9F5E2858AD4F007B3EFB /* Internet.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Internet.hpp; sourceTree = ""; }; + F6EB9F612858B92E007B3EFB /* Queue.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Queue.cpp; sourceTree = ""; }; + F6EB9F642858B983007B3EFB /* Internet.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Internet.hpp; sourceTree = ""; }; + F6EC55EC2ECB9FE30052CD0A /* directory_exists.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = directory_exists.hpp; sourceTree = ""; }; + F6EC55ED2ECB9FE30052CD0A /* directory_exists.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = directory_exists.cpp; sourceTree = ""; }; + F6EC55EE2ECBA3C10052CD0A /* get_files.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = get_files.cpp; sourceTree = ""; }; + F6EC55F42ECBA3D30052CD0A /* get_files.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = get_files.hpp; sourceTree = ""; }; + F6EFB7902E1035520036E227 /* TimedCache.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = TimedCache.hpp; sourceTree = ""; }; + F6EFB7912E1036F10036E227 /* InvalidatingCache.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = InvalidatingCache.hpp; sourceTree = ""; }; + F6EFB7922E1037880036E227 /* InvalidatingTimedCache.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = InvalidatingTimedCache.hpp; sourceTree = ""; }; + F6F04DA52DC3996800A99D3E /* transform_ok.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = transform_ok.hpp; sourceTree = ""; }; + F6F10D802E3809050082E9D9 /* Makefile */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = ""; }; + F6F182382C06A3C900D7005F /* zlib.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = zlib.h; sourceTree = ""; }; + F6F182392C06A3D000D7005F /* zlib.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = zlib.cpp; sourceTree = ""; }; + F6F1823C2C06A4B400D7005F /* zlib.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = zlib.hpp; sourceTree = ""; }; + F6F182422C075F3700D7005F /* zlib.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = zlib.cpp; sourceTree = ""; }; + F6F182482C075F5500D7005F /* _delete */ = {isa = PBXFileReference; lastKnownFileType = folder; path = _delete; sourceTree = ""; }; + F6F1824A2C07A31300D7005F /* zstd.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = zstd.h; sourceTree = ""; }; + F6F1824B2C07A31300D7005F /* zstd.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = zstd.hpp; sourceTree = ""; }; + F6F1824C2C07A31300D7005F /* zstd.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = zstd.cpp; sourceTree = ""; }; + F6F1824D2C07A31300D7005F /* zstd.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = zstd.cpp; sourceTree = ""; }; + F6F363C329A649DA00D9F857 /* CircularBuffer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CircularBuffer.cpp; sourceTree = ""; }; + F6F363C429A649DA00D9F857 /* CircularBuffer.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = CircularBuffer.hpp; sourceTree = ""; }; + F6F363CB29A64E6100D9F857 /* SizedVector.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = SizedVector.hpp; sourceTree = ""; }; + F6F41A78286C75B900016B6F /* GlobalResourceVersion+IO.inl */ = {isa = PBXFileReference; lastKnownFileType = text; path = "GlobalResourceVersion+IO.inl"; sourceTree = ""; }; + F6F79A5D2F3F7656008BF0C6 /* Server.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Server.cpp; sourceTree = ""; }; + F6F79A5F2F3F77DE008BF0C6 /* Server.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Server.hpp; sourceTree = ""; }; + F6F79A612F3F7A0B008BF0C6 /* Server.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Server.h; sourceTree = ""; }; + F6F79A632F3F81BF008BF0C6 /* as_future.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = as_future.hpp; sourceTree = ""; }; + F6F79A642F3F8361008BF0C6 /* Types.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Types.hpp; sourceTree = ""; }; + F6F79A652F3F8B31008BF0C6 /* httplib-0_27_0.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "httplib-0_27_0.h"; sourceTree = ""; }; + F6F79A682F3FED1F008BF0C6 /* Server.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Server.cpp; sourceTree = ""; }; + F6F79A6B2F40BDCE008BF0C6 /* Makefile */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = ""; }; + F6F79A8C2F40C3EA008BF0C6 /* Server.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Server.cpp; sourceTree = ""; }; + F6F79A8E2F40CA06008BF0C6 /* Server.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Server.h; sourceTree = ""; }; + F6F79A8F2F40CA06008BF0C6 /* Server.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Server.hpp; sourceTree = ""; }; + F6F79A902F40CA36008BF0C6 /* Server.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Server.cpp; sourceTree = ""; }; + F6F7A1602F466052008BF0C6 /* beast.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = beast.hpp; sourceTree = ""; }; + F6F7A3042F4A598C008BF0C6 /* Component.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Component.hpp; sourceTree = ""; }; + F6F7A3052F4A5990008BF0C6 /* Component.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Component.h; sourceTree = ""; }; + F6F7A3062F4A5A80008BF0C6 /* imp.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = imp.hpp; sourceTree = ""; }; + F6F7A3072F4A5EA5008BF0C6 /* Method.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Method.hpp; sourceTree = ""; }; + F6F7A4952F4A73F8008BF0C6 /* Types.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Types.h; sourceTree = ""; }; + F6F7A4962F4A741D008BF0C6 /* Method.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Method.h; sourceTree = ""; }; + F6F970B92D776D86003B4E63 /* validate_safe_relative_path.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = validate_safe_relative_path.hpp; sourceTree = ""; }; + F6F970BB2D776DF3003B4E63 /* validate_safe_relative_path.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = validate_safe_relative_path.cpp; sourceTree = ""; }; + F6F970BE2D777179003B4E63 /* Path.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Path.cpp; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + F608A9AA2826B7DE005C276B /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + F608AA9528270442005C276B /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + F6623D6C28788BC6007FE5AF /* libCore_Misc.a in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + F6971F75282B124D008FBD17 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + F608A9A32826B7DE005C276B = { + isa = PBXGroup; + children = ( + F6623D6528788BB7007FE5AF /* Frameworks */, + F6F10D802E3809050082E9D9 /* Makefile */, + F608AB6F2827F3DA005C276B /* Makefile.def */, + F608AB702827F3DA005C276B /* Makefile.project */, + F608A9AD2826B7DE005C276B /* Products */, + F608AA9928270442005C276B /* tests */, + F61D7C3E2E381639002A1AED /* tjp */, + ); + sourceTree = ""; + }; + F608A9AD2826B7DE005C276B /* Products */ = { + isa = PBXGroup; + children = ( + F608A9AC2826B7DE005C276B /* libCore_Misc.a */, + F608AA9828270442005C276B /* Core_Misc_Tests */, + F6971F79282B124D008FBD17 /* libCore_Misc_iOS.a */, + ); + name = Products; + sourceTree = ""; + }; + F608A9B32826BA81005C276B /* core */ = { + isa = PBXGroup; + children = ( + F6EA85FD2848D68000DED3C0 /* algorithm */, + F608AA3A2826BCC3005C276B /* args */, + F608AA312826BCC2005C276B /* bases */, + F6F182372C06A3B800D7005F /* compression */, + F6F363C229A649DA00D9F857 /* containers */, + F608AA2C2826BCC2005C276B /* csv */, + F683F51D2A670A0900191850 /* delegate */, + F608A9FD2826BCC2005C276B /* endian */, + F67B0081286737B1002A7A43 /* events */, + F608AA082826BCC2005C276B /* extensible */, + F608A9F12826BCC2005C276B /* file */, + F608AA002826BCC2005C276B /* functions */, + F6E526572884697A003BB6F1 /* hash */, + F63D84F82D10797700FF3624 /* http */, + F6E71956282C581E00EEB429 /* in_place */, + F608AA362826BCC2005C276B /* initializer */, + F608AA1D2826BCC2005C276B /* internet */, + F6C8F11D28EB0D1500FE529A /* mysql */, + F608A9EA2826BCC2005C276B /* pipe */, + F608AB582827ECD8005C276B /* profile */, + F608AA292826BCC2005C276B /* random */, + F608AA262826BCC2005C276B /* resource_version */, + F64A5DF82A59572900FE7EEF /* runnable */, + F6175A5C2CB2FB58008A9646 /* string */, + F644FB7328B130CC0022D49F /* string_lookup */, + F608AB8028281A0D005C276B /* system_usage */, + F608AB5F2827EDD1005C276B /* thread_pool */, + F62919F92CB6ADD5002060E0 /* threads */, + F69548A82828908C005D1B64 /* url */, + F6DEACC32831BE190001467D /* zip */, + ); + path = core; + sourceTree = ""; + }; + F608A9EA2826BCC2005C276B /* pipe */ = { + isa = PBXGroup; + children = ( + F61F9BE62C6E2FBA00F79137 /* _tests */, + F608A9EB2826BCC2005C276B /* obsolete */, + F608A9EE2826BCC2005C276B /* Pipe.h */, + F608A9EF2826BCC2005C276B /* Pipe.inl */, + ); + path = pipe; + sourceTree = ""; + }; + F608A9EB2826BCC2005C276B /* obsolete */ = { + isa = PBXGroup; + children = ( + F608A9EC2826BCC2005C276B /* Pipe.cpp */, + F608A9ED2826BCC2005C276B /* Pipe.h */, + ); + path = obsolete; + sourceTree = ""; + }; + F608A9F12826BCC2005C276B /* file */ = { + isa = PBXGroup; + children = ( + F6F970BC2D777157003B4E63 /* _tests */, + F608A9F22826BCC2005C276B /* apple */, + F63D85222D10E6DB00FF3624 /* Contents.h */, + F608A9FC2826BCC2005C276B /* Directories.h */, + F63D850C2D10970C00FF3624 /* File.cpp */, + F63E1E8B2E97E1AD000D0C5E /* File_Messy.cpp */, + F608A9F62826BCC2005C276B /* File.h */, + F608A9F82826BCC2005C276B /* linux */, + F608A9FB2826BCC2005C276B /* Path.cpp */, + F63D85242D10E76E00FF3624 /* Path.h */, + F60583302EC37BB8008FE966 /* directory_of.hpp */, + F60583312EC37BEC008FE966 /* directory_of.cpp */, + F60583FA2EC4E99F008FE966 /* split_first_directory.hpp */, + F60583FB2EC4E99F008FE966 /* split_first_directory.cpp */, + F60584A52EC61172008FE966 /* file_exists.hpp */, + F60584A62EC6117A008FE966 /* file_exists.cpp */, + F6EC55EC2ECB9FE30052CD0A /* directory_exists.hpp */, + F6EC55ED2ECB9FE30052CD0A /* directory_exists.cpp */, + F6EC55EE2ECBA3C10052CD0A /* get_files.cpp */, + F6EC55F42ECBA3D30052CD0A /* get_files.hpp */, + F60584A72EC61257008FE966 /* unlink.hpp */, + F60584A82EC61257008FE966 /* unlink.cpp */, + F60584A32EC60F81008FE966 /* move.hpp */, + F60584A42EC60F9C008FE966 /* move.cpp */, + F60584B22EC61928008FE966 /* read.hpp */, + F60584B32EC61928008FE966 /* read.cpp */, + F60584B42EC619B2008FE966 /* write.hpp */, + F60584B52EC619B2008FE966 /* write.cpp */, + F60584B72EC61C3B008FE966 /* times.cpp */, + F60584B62EC61C3B008FE966 /* times.hpp */, + F6984F4F2EF3429200718723 /* pop_first.hpp */, + F60584B02EC618C2008FE966 /* copy.hpp */, + F60584B12EC618C2008FE966 /* copy.cpp */, + F6F970B92D776D86003B4E63 /* validate_safe_relative_path.hpp */, + F6F970BB2D776DF3003B4E63 /* validate_safe_relative_path.cpp */, + F6DDE74F2DB03CAB005F94FC /* is_relative_path.hpp */, + F63D85152D10CB9400FF3624 /* VirtualFileSystem.cpp */, + F63D85142D10C7C400FF3624 /* VirtualFileSystem.h */, + F63D85162D10CBAA00FF3624 /* VirtualFileSystem.hpp */, + F608A9F72826BCC2005C276B /* writeSegmentsToFile.cpp */, + F63D850B2D10968300FF3624 /* writeSegmentsToFile.h */, + ); + path = file; + sourceTree = ""; + }; + F608A9F22826BCC2005C276B /* apple */ = { + isa = PBXGroup; + children = ( + F608A9F32826BCC2005C276B /* Directories.h */, + F608A9F42826BCC2005C276B /* Directories.cpp */, + ); + path = apple; + sourceTree = ""; + }; + F608A9F82826BCC2005C276B /* linux */ = { + isa = PBXGroup; + children = ( + F608A9F92826BCC2005C276B /* Directories.h */, + F608A9FA2826BCC2005C276B /* Directories.cpp */, + ); + path = linux; + sourceTree = ""; + }; + F608A9FD2826BCC2005C276B /* endian */ = { + isa = PBXGroup; + children = ( + F608A9FE2826BCC2005C276B /* Endian.h */, + F608A9FF2826BCC2005C276B /* Endian.cpp */, + ); + path = endian; + sourceTree = ""; + }; + F608AA002826BCC2005C276B /* functions */ = { + isa = PBXGroup; + children = ( + F608AA032826BCC2005C276B /* Dispatch.h */, + F608AA012826BCC2005C276B /* Dispatch.hpp */, + F62C1B8D28614B400093EFBE /* optional_function.h */, + F62C1B8E28614B530093EFBE /* optional_function.hpp */, + ); + path = functions; + sourceTree = ""; + }; + F608AA082826BCC2005C276B /* extensible */ = { + isa = PBXGroup; + children = ( + F61F9BE52C6E2EFF00F79137 /* _tests */, + F608AA092826BCC2005C276B /* Extensible.cpp */, + F62C1BD528620FE80093EFBE /* ExtensibleWith.h */, + F6A8B2A52A633CB000E4FDC7 /* Extensible+TypeInfo.h */, + F6A8B2A42A633C8C00E4FDC7 /* Extensible+Enumerated.h */, + F6A8B2A12A633B9300E4FDC7 /* Extensible+CacheViaMap.h */, + F6A8B2A22A633BA000E4FDC7 /* Extensible+CacheViaArrayAndMap.h */, + F6A8B2A32A633BB400E4FDC7 /* Extensible+CacheViaArray.h */, + F608AA0B2826BCC2005C276B /* Extensible.h */, + ); + path = extensible; + sourceTree = ""; + }; + F608AA1D2826BCC2005C276B /* internet */ = { + isa = PBXGroup; + children = ( + F69534652D84E0FA00C9AFA0 /* _tests */, + F608AA232826BCC2005C276B /* curl */, + F68BA83A2E93E1070040FA70 /* hirose */, + F608AA212826BCC2005C276B /* ios */, + F62B77202EBCED2400C7865D /* Data.hpp */, + F61E82A82D9187FD00EF6FFF /* form_encode.hpp */, + F6422B112D8CA21500FAE1FC /* future_json_to_result.hpp */, + F68BD1B02D998D77008BE7F4 /* get_embedded_value.hpp */, + F68BD1AF2D99894B008BE7F4 /* get_redirect.hpp */, + F62B77212EBCEDBA00C7865D /* Headers.hpp */, + F62B77222EBCEED000C7865D /* Headers.cpp */, + F68BD1B12D998F3E008BE7F4 /* html_decode.hpp */, + F608AA1F2826BCC2005C276B /* Internet.h */, + F608AA1E2826BCC2005C276B /* Internet.hpp */, + F62B77282EBD0B3E00C7865D /* Internet+Internal.hpp */, + F62B76F12EBB846800C7865D /* Internet+Internal.cpp */, + F6992C652C28B234003EBEA2 /* join.hpp */, + F6EB9F5B2858AC53007B3EFB /* Queue.hpp */, + F6CDE5B02F3CBDFD003720FD /* Queue.h */, + F608AA252826BCC2005C276B /* URL.h */, + F608AA202826BCC2005C276B /* URL.cpp */, + F6226CA22D971F40009F9761 /* url_param_decode.hpp */, + F6422B102D8CA04A00FAE1FC /* url_param_encode.hpp */, + F605835D2EC3A6F4008FE966 /* url_path_decode.hpp */, + F605835E2EC3A6F4008FE966 /* url_path_encode.hpp */, + F66B7EAD2EB64717009029EF /* url_with_params.hpp */, + ); + path = internet; + sourceTree = ""; + }; + F608AA212826BCC2005C276B /* ios */ = { + isa = PBXGroup; + children = ( + F62324082ED7AC9200307810 /* Queue.hpp */, + F6EB9F612858B92E007B3EFB /* Queue.cpp */, + F6EB9F642858B983007B3EFB /* Internet.hpp */, + F608AA222826BCC2005C276B /* Internet.mm */, + ); + path = ios; + sourceTree = ""; + }; + F608AA232826BCC2005C276B /* curl */ = { + isa = PBXGroup; + children = ( + F62324062ED7AC6900307810 /* Queue.hpp */, + F6EB9F5C2858ACF8007B3EFB /* Queue.cpp */, + F6EB9F5E2858AD4F007B3EFB /* Internet.hpp */, + F608AA242826BCC2005C276B /* Internet.cpp */, + ); + path = curl; + sourceTree = ""; + }; + F608AA262826BCC2005C276B /* resource_version */ = { + isa = PBXGroup; + children = ( + F61F9BE72C6E2FD000F79137 /* _tests */, + F6F41A78286C75B900016B6F /* GlobalResourceVersion+IO.inl */, + F63D28FB2885E6D00042E6BE /* GlobalResourceVersion+IO.cpp */, + F62C1BDC28621C660093EFBE /* GlobalResourceVersion.cpp */, + F62C1BDD28621C660093EFBE /* GlobalResourceVersion.hpp */, + F650BA6828906F5F006D2316 /* GlobalResourceVersion.h */, + F60892582865EC8E00174811 /* LocalResourceVersion+IO.inl */, + F608AA272826BCC2005C276B /* LocalResourceVersion.hpp */, + ); + path = resource_version; + sourceTree = ""; + }; + F608AA292826BCC2005C276B /* random */ = { + isa = PBXGroup; + children = ( + F69CC2582D297B6D00FB29DE /* bell_curve.hpp */, + F62AABF52EC0051B00A01727 /* file_name.hpp */, + F62AABF62EC0065E00A01727 /* temporary_path.hpp */, + F608AA2B2826BCC2005C276B /* Random.h */, + F608AA2A2826BCC2005C276B /* Random.cpp */, + ); + path = random; + sourceTree = ""; + }; + F608AA2C2826BCC2005C276B /* csv */ = { + isa = PBXGroup; + children = ( + F61F9BE32C6E2E6D00F79137 /* _tests */, + F608AA2D2826BCC2005C276B /* Csv.h */, + F608AA2E2826BCC2005C276B /* Csv.cpp */, + F608AA302826BCC2005C276B /* Csv.hpp */, + ); + path = csv; + sourceTree = ""; + }; + F608AA312826BCC2005C276B /* bases */ = { + isa = PBXGroup; + children = ( + F61F9BDE2C6E2CBA00F79137 /* _tests */, + F6028B012A2FDCEC00CAD87D /* _remove */, + F608AA322826BCC2005C276B /* Base64.h */, + F608AA332826BCC2005C276B /* Base64.cpp */, + F6E71933282C16DC00EEB429 /* Base64URL.h */, + F6E71932282C16DA00EEB429 /* Base64URL.cpp */, + F6DF53722DBE624D00FF4522 /* Base32.h */, + F6DF53732DBE624D00FF4522 /* Base32.cpp */, + F608AA342826BCC2005C276B /* Base16.cpp */, + F608AA352826BCC2005C276B /* Base16.h */, + ); + path = bases; + sourceTree = ""; + }; + F608AA362826BCC2005C276B /* initializer */ = { + isa = PBXGroup; + children = ( + F608AA372826BCC2005C276B /* Initializer.cpp */, + F608AA382826BCC2005C276B /* Initializer.h */, + F608AA392826BCC2005C276B /* Initializer.hpp */, + ); + path = initializer; + sourceTree = ""; + }; + F608AA3A2826BCC3005C276B /* args */ = { + isa = PBXGroup; + children = ( + F6CDE77B2F3F6418003720FD /* ArgsFile.h */, + F6CDE77C2F3F6432003720FD /* ArgsFile.cpp */, + F608AA3B2826BCC3005C276B /* Args.cpp */, + F608AA3C2826BCC3005C276B /* Args.h */, + ); + path = args; + sourceTree = ""; + }; + F608AA9928270442005C276B /* tests */ = { + isa = PBXGroup; + children = ( + F6F79A6B2F40BDCE008BF0C6 /* Makefile */, + F61F9BEB2C6E306B00F79137 /* Makefile.project */, + F608AA9A28270442005C276B /* main.cpp */, + ); + path = tests; + sourceTree = ""; + }; + F608AB582827ECD8005C276B /* profile */ = { + isa = PBXGroup; + children = ( + F608AB592827ECD8005C276B /* PROFILE_SCOPE.cpp */, + F608AB5A2827ECD8005C276B /* PROFILE_SIGNAL.cpp */, + F608AB5B2827ECD8005C276B /* PROFILE_SIGNAL.h */, + F608AB5C2827ECD8005C276B /* PROFILE_SCOPE.h */, + ); + path = profile; + sourceTree = ""; + }; + F608AB5F2827EDD1005C276B /* thread_pool */ = { + isa = PBXGroup; + children = ( + F61F9BE82C6E2FFC00F79137 /* _tests */, + F6DD12B22BAF6F900040A088 /* _delete */, + F608AB622827EE07005C276B /* AutoThreadPool.hpp */, + F6DD12B42BAF71C40040A088 /* AutoThreadPool.h */, + F608AB612827EE07005C276B /* FunctionalProcessor.hpp */, + F6DD12AF2BAF68210040A088 /* ObjectConsumer.hpp */, + F6DD12B02BAF6BD40040A088 /* Base.h */, + ); + path = thread_pool; + sourceTree = ""; + }; + F608AB8028281A0D005C276B /* system_usage */ = { + isa = PBXGroup; + children = ( + F608AB8128281A0D005C276B /* apple */, + F608AB8728281A0D005C276B /* CpuUsage.h */, + F608AB8A28281A0D005C276B /* linux */, + F608AB8828281A0D005C276B /* NetworkUsage.h */, + F62F1CF12A7DB18400F838D3 /* ProcessUsage.h */, + F62F1CF22A7DB1D600F838D3 /* ProcessUsage.cpp */, + F608AB8528281A0D005C276B /* SystemUsage.cpp */, + F608AB8928281A0D005C276B /* SystemUsage.h */, + F608AB8D28281A0D005C276B /* SystemUsage.hpp */, + ); + path = system_usage; + sourceTree = ""; + }; + F608AB8128281A0D005C276B /* apple */ = { + isa = PBXGroup; + children = ( + F608AB8328281A0D005C276B /* CpuUsage.cpp */, + F608AB8228281A0D005C276B /* NetworkUsage.cpp */, + ); + path = apple; + sourceTree = ""; + }; + F608AB8A28281A0D005C276B /* linux */ = { + isa = PBXGroup; + children = ( + F608AB8C28281A0D005C276B /* CpuUsage.cpp */, + F608AB8B28281A0D005C276B /* NetworkUsage.cpp */, + ); + path = linux; + sourceTree = ""; + }; + F6175A5C2CB2FB58008A9646 /* string */ = { + isa = PBXGroup; + children = ( + F6175A5E2CB2FB73008A9646 /* to_cstring.hpp */, + ); + path = string; + sourceTree = ""; + }; + F61D7C3E2E381639002A1AED /* tjp */ = { + isa = PBXGroup; + children = ( + F608A9B32826BA81005C276B /* core */, + ); + path = tjp; + sourceTree = ""; + }; + F61F9BDE2C6E2CBA00F79137 /* _tests */ = { + isa = PBXGroup; + children = ( + F6DF53762DBE6AB700FF4522 /* Base32.cpp */, + F6DF53782DBE6AC200FF4522 /* Base16.cpp */, + F6A3330C2BD492160092BFA7 /* Base64.cpp */, + ); + path = _tests; + sourceTree = ""; + }; + F61F9BE12C6E2E3E00F79137 /* _tests */ = { + isa = PBXGroup; + children = ( + F6F363C329A649DA00D9F857 /* CircularBuffer.cpp */, + ); + path = _tests; + sourceTree = ""; + }; + F61F9BE32C6E2E6D00F79137 /* _tests */ = { + isa = PBXGroup; + children = ( + F608AA2F2826BCC2005C276B /* Csv.cpp */, + ); + path = _tests; + sourceTree = ""; + }; + F61F9BE42C6E2EBC00F79137 /* _tests */ = { + isa = PBXGroup; + children = ( + F683F5242A674E0C00191850 /* Delegate+Single.cpp */, + F683F51F2A670A1500191850 /* Delegate.cpp */, + ); + path = _tests; + sourceTree = ""; + }; + F61F9BE52C6E2EFF00F79137 /* _tests */ = { + isa = PBXGroup; + children = ( + F608AA0A2826BCC2005C276B /* Extensible.cpp */, + ); + path = _tests; + sourceTree = ""; + }; + F61F9BE62C6E2FBA00F79137 /* _tests */ = { + isa = PBXGroup; + children = ( + F608A9F02826BCC2005C276B /* Pipe.cpp */, + ); + path = _tests; + sourceTree = ""; + }; + F61F9BE72C6E2FD000F79137 /* _tests */ = { + isa = PBXGroup; + children = ( + F6623D6128788AC2007FE5AF /* GlobalResource.cpp */, + ); + path = _tests; + sourceTree = ""; + }; + F61F9BE82C6E2FFC00F79137 /* _tests */ = { + isa = PBXGroup; + children = ( + F608AB632827EE07005C276B /* AutoThreadPool.cpp */, + ); + path = _tests; + sourceTree = ""; + }; + F61F9BE92C6E301400F79137 /* _tests */ = { + isa = PBXGroup; + children = ( + F69548A92828908C005D1B64 /* URL.cpp */, + ); + path = _tests; + sourceTree = ""; + }; + F61F9BFC2C6EEBCC00F79137 /* _tests */ = { + isa = PBXGroup; + children = ( + F6F182422C075F3700D7005F /* zlib.cpp */, + F6F1824D2C07A31300D7005F /* zstd.cpp */, + ); + path = _tests; + sourceTree = ""; + }; + F62919F92CB6ADD5002060E0 /* threads */ = { + isa = PBXGroup; + children = ( + F6BBFE4C2E969AA8009E9C66 /* PinnedFutures.hpp */, + F62919F62CB6ADD5002060E0 /* Parallel.cpp */, + F62919F72CB6ADD5002060E0 /* Parallel.h */, + F62919F82CB6ADD5002060E0 /* Parallel.hpp */, + ); + path = threads; + sourceTree = ""; + }; + F63D84F82D10797700FF3624 /* http */ = { + isa = PBXGroup; + children = ( + F6F79A8A2F40C3E1008BF0C6 /* beast */, + F6CDE79B2F3F72D5003720FD /* hirose */, + F6960D152D8AF93B00767451 /* transform_json.hpp */, + F6DDE7152DAD3577005F94FC /* transform_json_text.hpp */, + F66B7E982EB636E7009029EF /* transform_base64_text.hpp */, + F61E82AA2D91951B00EF6FFF /* transform_plaintext.hpp */, + F6F04DA52DC3996800A99D3E /* transform_ok.hpp */, + F62B76EF2EBACB3F00C7865D /* transform_binary.hpp */, + F68F84BE2D8EF2C500245DB9 /* transform.hpp */, + F68F84C42D8EFEC100245DB9 /* future_response.hpp */, + F68F84C22D8EFCD900245DB9 /* collapse_future.hpp */, + F6F79A632F3F81BF008BF0C6 /* as_future.hpp */, + F6960CFF2D88F18E00767451 /* require_header.hpp */, + F6B9FE3A2D8B5271002831BC /* Server.hpp */, + F6B9FE3B2D8B534D002831BC /* Server.cpp */, + F6B9FE3C2D8B5380002831BC /* Server.h */, + F6F7A3062F4A5A80008BF0C6 /* imp.hpp */, + F6F7A3042F4A598C008BF0C6 /* Component.hpp */, + F6F7A3052F4A5990008BF0C6 /* Component.h */, + F6F7A4962F4A741D008BF0C6 /* Method.h */, + F6F7A3072F4A5EA5008BF0C6 /* Method.hpp */, + F6F7A4952F4A73F8008BF0C6 /* Types.h */, + F6F79A642F3F8361008BF0C6 /* Types.hpp */, + ); + path = http; + sourceTree = ""; + }; + F63D851F2D10E20300FF3624 /* _tests */ = { + isa = PBXGroup; + children = ( + ); + path = _tests; + sourceTree = ""; + }; + F644FB7328B130CC0022D49F /* string_lookup */ = { + isa = PBXGroup; + children = ( + F6C8F11E28EB0D1F00FE529A /* StringLookup.h */, + F644FB7528B131CF0022D49F /* StringLookup.cpp */, + ); + path = string_lookup; + sourceTree = ""; + }; + F64A5DF82A59572900FE7EEF /* runnable */ = { + isa = PBXGroup; + children = ( + F64A5DFA2A5957B800FE7EEF /* Runnable.h */, + F64A5DF92A59573100FE7EEF /* Runnable.hpp */, + ); + path = runnable; + sourceTree = ""; + }; + F64D83742E4C0ED2009AD431 /* _tests */ = { + isa = PBXGroup; + children = ( + F64D83752E4C0ED6009AD431 /* call_with_pairs.cpp */, + ); + path = _tests; + sourceTree = ""; + }; + F6623D6528788BB7007FE5AF /* Frameworks */ = { + isa = PBXGroup; + children = ( + F6623D6628788BB7007FE5AF /* libCore_IO.a */, + F6623D6828788BB7007FE5AF /* libCore_Math.a */, + F6623D6A28788BB7007FE5AF /* libCore_Zero.a */, + ); + name = Frameworks; + sourceTree = ""; + }; + F67B0081286737B1002A7A43 /* events */ = { + isa = PBXGroup; + children = ( + F67B0083286737C7002A7A43 /* Signal.hpp */, + F67B0084286737CD002A7A43 /* Signal.cpp */, + F67B0082286737BF002A7A43 /* Signal.h */, + ); + path = events; + sourceTree = ""; + }; + F683F51D2A670A0900191850 /* delegate */ = { + isa = PBXGroup; + children = ( + F61F9BE42C6E2EBC00F79137 /* _tests */, + F683F5202A6710C600191850 /* Delegate+Single.hpp */, + F683F5212A6710C600191850 /* Delegate+Single.h */, + F608AB7F282814E8005C276B /* Delegate.hpp */, + F683F51E2A670A0F00191850 /* Delegate.h */, + ); + path = delegate; + sourceTree = ""; + }; + F68BA83A2E93E1070040FA70 /* hirose */ = { + isa = PBXGroup; + children = ( + F68BA83B2E93E10C0040FA70 /* Internet.hpp */, + F68BA83C2E93E10C0040FA70 /* Internet.cpp */, + ); + path = hirose; + sourceTree = ""; + }; + F69534652D84E0FA00C9AFA0 /* _tests */ = { + isa = PBXGroup; + children = ( + F69534662D84E10500C9AFA0 /* Internet+common.cpp */, + ); + path = _tests; + sourceTree = ""; + }; + F69548A82828908C005D1B64 /* url */ = { + isa = PBXGroup; + children = ( + F61F9BE92C6E301400F79137 /* _tests */, + F69548AA2828908C005D1B64 /* URL.cpp */, + F69548AB2828908C005D1B64 /* URL.h */, + F69548AC2828908C005D1B64 /* URL.inl */, + ); + path = url; + sourceTree = ""; + }; + F6C8F11D28EB0D1500FE529A /* mysql */ = { + isa = PBXGroup; + children = ( + F6C8F11F28EB0DB400FE529A /* MysqlQueries.h */, + F644FB7428B130DA0022D49F /* MysqlConnection.h */, + ); + path = mysql; + sourceTree = ""; + }; + F6CDE79B2F3F72D5003720FD /* hirose */ = { + isa = PBXGroup; + children = ( + F6F79A672F3FED05008BF0C6 /* _test */, + F6F79A652F3F8B31008BF0C6 /* httplib-0_27_0.h */, + F689727E2D8752EF0084C6A9 /* write_result.hpp */, + F6F79A5D2F3F7656008BF0C6 /* Server.cpp */, + F6F79A5F2F3F77DE008BF0C6 /* Server.hpp */, + F6F79A612F3F7A0B008BF0C6 /* Server.h */, + F63D84FA2D1079BC00FF3624 /* httplib.h */, + ); + path = hirose; + sourceTree = ""; + }; + F6DEACC32831BE190001467D /* zip */ = { + isa = PBXGroup; + children = ( + F63D851F2D10E20300FF3624 /* _tests */, + F63D851B2D10D23300FF3624 /* _delete */, + F6DEACC82831BE190001467D /* Compress+zlib.cpp */, + F6BB293E2DD95AE10061D8D3 /* Compress+zstd.cpp */, + F6DEACC62831BE190001467D /* Compress.h */, + F63D851C2D10DC4C00FF3624 /* MemoryBuffer.h */, + F63D851E2D10DCF300FF3624 /* MemoryBuffer.hpp */, + F63D851D2D10DC9B00FF3624 /* MemoryBuffer.cpp */, + F63D85262D10ECB100FF3624 /* VirtualFileSystem.cpp */, + F63D85252D10ECB100FF3624 /* VirtualFileSystem.h */, + F63D85272D10ECB100FF3624 /* VirtualFileSystem.hpp */, + F6DEACC42831BE190001467D /* Decompress.cpp */, + F6DEACC52831BE190001467D /* Decompress.h */, + F6DEACC72831BE190001467D /* Exception.h */, + ); + path = zip; + sourceTree = ""; + }; + F6E526572884697A003BB6F1 /* hash */ = { + isa = PBXGroup; + children = ( + F6E5265B28849865003BB6F1 /* fasthash.cpp */, + F6E5265C28849865003BB6F1 /* fasthash.h */, + F6E5265A288469D7003BB6F1 /* Hash.cpp */, + F6E526582884698A003BB6F1 /* Hash.h */, + ); + path = hash; + sourceTree = ""; + }; + F6E71956282C581E00EEB429 /* in_place */ = { + isa = PBXGroup; + children = ( + F6E71957282C582A00EEB429 /* InPlace.h */, + ); + path = in_place; + sourceTree = ""; + }; + F6EA85FD2848D68000DED3C0 /* algorithm */ = { + isa = PBXGroup; + children = ( + F64D83742E4C0ED2009AD431 /* _tests */, + F6175A692CB3162C008A9646 /* xor_inplace.hpp */, + F63D850A2D108E4A00FF3624 /* reverse_inplace.hpp */, + F64D83732E4C0EAD009AD431 /* call_with_pairs.hpp */, + F69CC2572D2979D400FB29DE /* Sortable.hpp */, + F6E4FEA628E1CF5600171E8C /* for_each_argument.hpp */, + F6EA85FE2848D68800DED3C0 /* find_if_random.hpp */, + ); + path = algorithm; + sourceTree = ""; + }; + F6F182372C06A3B800D7005F /* compression */ = { + isa = PBXGroup; + children = ( + F61F9BFC2C6EEBCC00F79137 /* _tests */, + F6F182482C075F5500D7005F /* _delete */, + F6F182392C06A3D000D7005F /* zlib.cpp */, + F6F182382C06A3C900D7005F /* zlib.h */, + F6F1823C2C06A4B400D7005F /* zlib.hpp */, + F6F1824C2C07A31300D7005F /* zstd.cpp */, + F6F1824A2C07A31300D7005F /* zstd.h */, + F6F1824B2C07A31300D7005F /* zstd.hpp */, + ); + path = compression; + sourceTree = ""; + }; + F6F363C229A649DA00D9F857 /* containers */ = { + isa = PBXGroup; + children = ( + F61F9BE12C6E2E3E00F79137 /* _tests */, + F6F363CB29A64E6100D9F857 /* SizedVector.hpp */, + F6F363C429A649DA00D9F857 /* CircularBuffer.hpp */, + F68CE32C2D37F907004F9466 /* Changed.hpp */, + F6EFB7912E1036F10036E227 /* InvalidatingCache.hpp */, + F6EFB7922E1037880036E227 /* InvalidatingTimedCache.hpp */, + F6EFB7902E1035520036E227 /* TimedCache.hpp */, + F65E7A792A43E00C003B2D0D /* Alias.hpp */, + ); + path = containers; + sourceTree = ""; + }; + F6F79A672F3FED05008BF0C6 /* _test */ = { + isa = PBXGroup; + children = ( + F6F79A682F3FED1F008BF0C6 /* Server.cpp */, + ); + path = _test; + sourceTree = ""; + }; + F6F79A8A2F40C3E1008BF0C6 /* beast */ = { + isa = PBXGroup; + children = ( + F6F7A1602F466052008BF0C6 /* beast.hpp */, + F6F79A902F40CA36008BF0C6 /* Server.cpp */, + F6F79A8E2F40CA06008BF0C6 /* Server.h */, + F6F79A8F2F40CA06008BF0C6 /* Server.hpp */, + F6F79A8B2F40C3E6008BF0C6 /* _test */, + ); + path = beast; + sourceTree = ""; + }; + F6F79A8B2F40C3E6008BF0C6 /* _test */ = { + isa = PBXGroup; + children = ( + F6F79A8C2F40C3EA008BF0C6 /* Server.cpp */, + ); + path = _test; + sourceTree = ""; + }; + F6F970BC2D777157003B4E63 /* _tests */ = { + isa = PBXGroup; + children = ( + F6F970BE2D777179003B4E63 /* Path.cpp */, + ); + path = _tests; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXHeadersBuildPhase section */ + F608A9A82826B7DE005C276B /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + F6F79A622F3F7A0B008BF0C6 /* Server.h in Headers */, + F6E5265F28849865003BB6F1 /* fasthash.h in Headers */, + F68BA83D2E93E10C0040FA70 /* Internet.hpp in Headers */, + F6F363C729A649DA00D9F857 /* CircularBuffer.hpp in Headers */, + F62B77232EBCF06100C7865D /* Headers.hpp in Headers */, + F6BBFE4D2E969AA8009E9C66 /* PinnedFutures.hpp in Headers */, + F62919FC2CB6ADD5002060E0 /* Parallel.h in Headers */, + F6F79A602F3F77DE008BF0C6 /* Server.hpp in Headers */, + F62919FE2CB6ADD5002060E0 /* Parallel.hpp in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + F6971F5C282B124D008FBD17 /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + F6E5266028849865003BB6F1 /* fasthash.h in Headers */, + F6F363C829A649DA00D9F857 /* CircularBuffer.hpp in Headers */, + F6BBFE4E2E969AA8009E9C66 /* PinnedFutures.hpp in Headers */, + F62919FD2CB6ADD5002060E0 /* Parallel.h in Headers */, + F62919FF2CB6ADD5002060E0 /* Parallel.hpp in Headers */, + F62324092ED7AC9200307810 /* Queue.hpp in Headers */, + F62B77242EBCF06100C7865D /* Headers.hpp in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + +/* Begin PBXNativeTarget section */ + F608A9AB2826B7DE005C276B /* Core_Misc */ = { + isa = PBXNativeTarget; + buildConfigurationList = F608A9B02826B7DE005C276B /* Build configuration list for PBXNativeTarget "Core_Misc" */; + buildPhases = ( + F608A9A82826B7DE005C276B /* Headers */, + F608A9A92826B7DE005C276B /* Sources */, + F608A9AA2826B7DE005C276B /* Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Core_Misc; + productName = Core_Misc; + productReference = F608A9AC2826B7DE005C276B /* libCore_Misc.a */; + productType = "com.apple.product-type.library.static"; + }; + F608AA9728270442005C276B /* Core_Misc_Tests */ = { + isa = PBXNativeTarget; + buildConfigurationList = F608AA9C28270442005C276B /* Build configuration list for PBXNativeTarget "Core_Misc_Tests" */; + buildPhases = ( + F608AA9428270442005C276B /* Sources */, + F608AA9528270442005C276B /* Frameworks */, + F608AA9628270442005C276B /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Core_Misc_Tests; + productName = Core_Misc_Tests; + productReference = F608AA9828270442005C276B /* Core_Misc_Tests */; + productType = "com.apple.product-type.tool"; + }; + F6971F5B282B124D008FBD17 /* Core_Misc_iOS */ = { + isa = PBXNativeTarget; + buildConfigurationList = F6971F76282B124D008FBD17 /* Build configuration list for PBXNativeTarget "Core_Misc_iOS" */; + buildPhases = ( + F6971F5C282B124D008FBD17 /* Headers */, + F6971F5D282B124D008FBD17 /* Sources */, + F6971F75282B124D008FBD17 /* Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Core_Misc_iOS; + productName = Core_Misc; + productReference = F6971F79282B124D008FBD17 /* libCore_Misc_iOS.a */; + productType = "com.apple.product-type.library.static"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + F608A9A42826B7DE005C276B /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + DefaultBuildSystemTypeForWorkspace = Original; + LastUpgradeCheck = 1330; + TargetAttributes = { + F608A9AB2826B7DE005C276B = { + CreatedOnToolsVersion = 13.3; + }; + F608AA9728270442005C276B = { + CreatedOnToolsVersion = 13.3; + }; + }; + }; + buildConfigurationList = F608A9A72826B7DE005C276B /* Build configuration list for PBXProject "Core_Misc" */; + compatibilityVersion = "Xcode 13.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = F608A9A32826B7DE005C276B; + productRefGroup = F608A9AD2826B7DE005C276B /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + F608A9AB2826B7DE005C276B /* Core_Misc */, + F6971F5B282B124D008FBD17 /* Core_Misc_iOS */, + F608AA9728270442005C276B /* Core_Misc_Tests */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + F608A9A92826B7DE005C276B /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + F69548AE28289095005D1B64 /* URL.cpp in Sources */, + F6954899282885B6005D1B64 /* Base64.cpp in Sources */, + F6F79A912F40CA36008BF0C6 /* Server.cpp in Sources */, + F6F1823A2C06A47100D7005F /* zlib.cpp in Sources */, + F644FB7628B1699A0022D49F /* StringLookup.cpp in Sources */, + F6954895282885A5005D1B64 /* Endian.cpp in Sources */, + F6DF53752DBE624D00FF4522 /* Base32.cpp in Sources */, + F62C1BDE28621C660093EFBE /* GlobalResourceVersion.cpp in Sources */, + F6DEACCA2831BE230001467D /* Compress+zlib.cpp in Sources */, + F62324072ED7AC6900307810 /* Queue.hpp in Sources */, + F6EB9F5D2858ACF8007B3EFB /* Queue.cpp in Sources */, + F69548902828858B005D1B64 /* URL.cpp in Sources */, + F6E5266128849916003BB6F1 /* Hash.cpp in Sources */, + F63D85172D10CD5D00FF3624 /* VirtualFileSystem.cpp in Sources */, + F643F21A28293721000B8B40 /* CpuUsage.cpp in Sources */, + F6B9FE3D2D8B5434002831BC /* Server.cpp in Sources */, + F695489D28288C1F005D1B64 /* Internet.cpp in Sources */, + F608AB5D2827ECDC005C276B /* PROFILE_SCOPE.cpp in Sources */, + F67B0085286738A2002A7A43 /* Signal.cpp in Sources */, + F63D85282D10ECB100FF3624 /* VirtualFileSystem.cpp in Sources */, + F695488E2828857F005D1B64 /* Random.cpp in Sources */, + F69548932828859B005D1B64 /* Path.cpp in Sources */, + F695489A282885B6005D1B64 /* Base16.cpp in Sources */, + F6F1824E2C07A62000D7005F /* zstd.cpp in Sources */, + F695489128288590005D1B64 /* Initializer.cpp in Sources */, + F6EC55EF2ECBA3C10052CD0A /* get_files.cpp in Sources */, + F63E1E8C2E97E1AD000D0C5E /* File_Messy.cpp in Sources */, + F6954897282885AE005D1B64 /* Csv.cpp in Sources */, + F62919FA2CB6ADD5002060E0 /* Parallel.cpp in Sources */, + F62F1CF32A7DB33500F838D3 /* ProcessUsage.cpp in Sources */, + F608AA9328270416005C276B /* Args.cpp in Sources */, + F6E5265D28849865003BB6F1 /* fasthash.cpp in Sources */, + F6954894282885A0005D1B64 /* Extensible.cpp in Sources */, + F643F21928293721000B8B40 /* NetworkUsage.cpp in Sources */, + F695488C28288573005D1B64 /* SystemUsage.cpp in Sources */, + F6F79A662F3F8BF0008BF0C6 /* Server.cpp in Sources */, + F6BB293F2DD95AE10061D8D3 /* Compress+zstd.cpp in Sources */, + F68BA83E2E93E10C0040FA70 /* Internet.cpp in Sources */, + F63D85202D10E21C00FF3624 /* MemoryBuffer.cpp in Sources */, + F61DB8F22857AB7B00B74C99 /* Decompress.cpp in Sources */, + F63D28FC2885E7030042E6BE /* GlobalResourceVersion+IO.cpp in Sources */, + F6992C662C28B234003EBEA2 /* join.hpp in Sources */, + F695489228288596005D1B64 /* writeSegmentsToFile.cpp in Sources */, + F63D295D2885F7F50042E6BE /* Directories.cpp in Sources */, + F62B77292EBD42D300C7865D /* Internet+Internal.cpp in Sources */, + F62B77252EBCF17F00C7865D /* Headers.cpp in Sources */, + F6F7A1612F466052008BF0C6 /* beast.hpp in Sources */, + F6DF53662DBAEFBD00FF4522 /* File.cpp in Sources */, + F6E71934282C185200EEB429 /* Base64URL.cpp in Sources */, + F608AB5E2827ECE0005C276B /* PROFILE_SIGNAL.cpp in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + F608AA9428270442005C276B /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + F6DF53772DBE6AB700FF4522 /* Base32.cpp in Sources */, + F61F9BE22C6E2E6500F79137 /* CircularBuffer.cpp in Sources */, + F6F79A922F40E88F008BF0C6 /* Server.cpp in Sources */, + F69548AD28289092005D1B64 /* URL.cpp in Sources */, + F6F182502C07A9F500D7005F /* zstd.cpp in Sources */, + F6F182492C07652700D7005F /* zlib.cpp in Sources */, + F683F5222A67224000191850 /* Delegate.cpp in Sources */, + F608AA9B28270442005C276B /* main.cpp in Sources */, + F6A3330D2BD4928E0092BFA7 /* Base64.cpp in Sources */, + F6DD12B32BAF71940040A088 /* AutoThreadPool.cpp in Sources */, + F64D83782E4C0F9C009AD431 /* call_with_pairs.cpp in Sources */, + F683F5252A674E0C00191850 /* Delegate+Single.cpp in Sources */, + F6A8B2A72A63435D00E4FDC7 /* Extensible.cpp in Sources */, + F6F970BF2D7771E6003B4E63 /* Path.cpp in Sources */, + F6954898282885B1005D1B64 /* Csv.cpp in Sources */, + F695488F28288585005D1B64 /* Pipe.cpp in Sources */, + F6DF53792DBE6AC200FF4522 /* Base16.cpp in Sources */, + F6F79A6A2F3FEE3F008BF0C6 /* Server.cpp in Sources */, + F69534682D84E1F700C9AFA0 /* Internet+common.cpp in Sources */, + F6623D6428788B44007FE5AF /* GlobalResource.cpp in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + F6971F5D282B124D008FBD17 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + F6971F5E282B124D008FBD17 /* URL.cpp in Sources */, + F6971F60282B124D008FBD17 /* Base64.cpp in Sources */, + F6F1823B2C06A47100D7005F /* zlib.cpp in Sources */, + F6EB9F622858B92E007B3EFB /* Queue.cpp in Sources */, + F644FB7728B1699A0022D49F /* StringLookup.cpp in Sources */, + F6971F62282B124D008FBD17 /* Endian.cpp in Sources */, + F6BB29402DD95AE10061D8D3 /* Compress+zstd.cpp in Sources */, + F6DEACCE2831BE250001467D /* Compress+zlib.cpp in Sources */, + F62B77262EBCF17F00C7865D /* Headers.cpp in Sources */, + F6971F63282B124D008FBD17 /* URL.cpp in Sources */, + F6971F65282B124D008FBD17 /* CpuUsage.cpp in Sources */, + F63E1E8D2E97E1AD000D0C5E /* File_Messy.cpp in Sources */, + F6E5266228849916003BB6F1 /* Hash.cpp in Sources */, + F63D85192D10CD6000FF3624 /* VirtualFileSystem.cpp in Sources */, + F6DF53672DBAEFBD00FF4522 /* File.cpp in Sources */, + F6EC55F02ECBA3C10052CD0A /* get_files.cpp in Sources */, + F6971F67282B124D008FBD17 /* PROFILE_SCOPE.cpp in Sources */, + F6971FB5282B1751008FBD17 /* Internet.mm in Sources */, + F62C1BDF28621C660093EFBE /* GlobalResourceVersion.cpp in Sources */, + F6971F6A282B124D008FBD17 /* Random.cpp in Sources */, + F63D85292D10ECB100FF3624 /* VirtualFileSystem.cpp in Sources */, + F62B772A2EBD42D300C7865D /* Internet+Internal.cpp in Sources */, + F6971F6B282B124D008FBD17 /* Path.cpp in Sources */, + F6971F6C282B124D008FBD17 /* Base16.cpp in Sources */, + F6971F6D282B124D008FBD17 /* Initializer.cpp in Sources */, + F6F1824F2C07A62100D7005F /* zstd.cpp in Sources */, + F6971F6E282B124D008FBD17 /* Csv.cpp in Sources */, + F6971F6F282B124D008FBD17 /* Args.cpp in Sources */, + F62919FB2CB6ADD5002060E0 /* Parallel.cpp in Sources */, + F62F1CF42A7DB33500F838D3 /* ProcessUsage.cpp in Sources */, + F6971F70282B124D008FBD17 /* Extensible.cpp in Sources */, + F6E5265E28849865003BB6F1 /* fasthash.cpp in Sources */, + F6971F71282B124D008FBD17 /* NetworkUsage.cpp in Sources */, + F6971F72282B124D008FBD17 /* SystemUsage.cpp in Sources */, + F61DB8F12857AB7B00B74C99 /* Decompress.cpp in Sources */, + F63D85212D10E21D00FF3624 /* MemoryBuffer.cpp in Sources */, + F6971F73282B124D008FBD17 /* writeSegmentsToFile.cpp in Sources */, + F6DF53742DBE624D00FF4522 /* Base32.cpp in Sources */, + F63D28FD2885E7040042E6BE /* GlobalResourceVersion+IO.cpp in Sources */, + F6992C672C28B234003EBEA2 /* join.hpp in Sources */, + F67B0086286738A3002A7A43 /* Signal.cpp in Sources */, + F63D295C2885F7F40042E6BE /* Directories.cpp in Sources */, + F6E71935282C185200EEB429 /* Base64URL.cpp in Sources */, + F6971F74282B124D008FBD17 /* PROFILE_SIGNAL.cpp in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + F608A9AE2826B7DE005C276B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + HEADER_SEARCH_PATHS = ( + ../Core_Zero, + ../Core_Future, + ../Core_Libraries/sdk/Darwin/include, + ); + IPHONEOS_DEPLOYMENT_TARGET = 13.6; + LIBRARY_SEARCH_PATHS = ""; + "LIBRARY_SEARCH_PATHS[sdk=iphoneos*][arch=arm64]" = ( + "../Core_Libraries/sdk/Darwin/lib/Debug.iOS-arm64", + "../Core_Libraries/project/Debug.iOS-arm64", + ); + "LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*]" = ( + "../Core_Libraries/sdk/Darwin/lib/Debug.iOS-sim", + "../Core_Libraries/project/Debug.iOS-sim", + ); + "LIBRARY_SEARCH_PATHS[sdk=macosx*][arch=arm64]" = ( + "../Core_Libraries/sdk/Darwin/lib/Debug.Darwin-arm64", + "../Core_Libraries/project/Debug.Darwin-arm64", + ); + MACOSX_DEPLOYMENT_TARGET = 10.15; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + OTHER_LDFLAGS = ( + "-lCore_Future", + "-lCore_Zero", + "-lz_", + "-lzstd", + ); + SDKROOT = macosx; + USE_HEADERMAP = NO; + }; + name = Debug; + }; + F608A9AF2826B7DE005C276B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + HEADER_SEARCH_PATHS = ( + ../Core_Zero, + ../Core_Future, + ../Core_Libraries/sdk/Darwin/include, + ); + IPHONEOS_DEPLOYMENT_TARGET = 13.6; + LIBRARY_SEARCH_PATHS = ""; + "LIBRARY_SEARCH_PATHS[sdk=iphoneos*]" = ( + "../Core_Libraries/sdk/Darwin/lib/Release.iOS-arm64", + "../Core_Libraries/project/Release.iOS-arm64", + ); + "LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*]" = ( + "../Core_Libraries/sdk/Darwin/lib/Release.iOS-sim", + "../Core_Libraries/project/Release.iOS-sim", + ); + "LIBRARY_SEARCH_PATHS[sdk=macosx*][arch=arm64]" = ( + "../Core_Libraries/sdk/Darwin/lib/Release.Darwin-arm64", + "../Core_Libraries/project/Release.Darwin-arm64", + ); + MACOSX_DEPLOYMENT_TARGET = 10.15; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + OTHER_LDFLAGS = ( + "-lCore_Future", + "-lCore_Zero", + "-lz_", + "-lzstd", + ); + SDKROOT = macosx; + USE_HEADERMAP = NO; + }; + name = Release; + }; + F608A9B12826B7DE005C276B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = T2M28D3T75; + EXECUTABLE_PREFIX = lib; + PRODUCT_NAME = "$(TARGET_NAME)"; + SKIP_INSTALL = YES; + }; + name = Debug; + }; + F608A9B22826B7DE005C276B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = T2M28D3T75; + EXECUTABLE_PREFIX = lib; + ONLY_ACTIVE_ARCH = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + SKIP_INSTALL = YES; + }; + name = Release; + }; + F608AA9D28270442005C276B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = T2M28D3T75; + ENABLE_HARDENED_RUNTIME = YES; + HEADER_SEARCH_PATHS = ( + "${inherited}", + ../Core_Misc, + ); + OTHER_LDFLAGS = "${inherited}"; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + F608AA9E28270442005C276B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = T2M28D3T75; + ENABLE_HARDENED_RUNTIME = YES; + HEADER_SEARCH_PATHS = ( + "${inherited}", + ../Core_Misc, + ); + ONLY_ACTIVE_ARCH = YES; + OTHER_LDFLAGS = "${inherited}"; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; + F6971F77282B124D008FBD17 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = T2M28D3T75; + EXECUTABLE_PREFIX = lib; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = iphoneos; + SKIP_INSTALL = YES; + }; + name = Debug; + }; + F6971F78282B124D008FBD17 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = T2M28D3T75; + EXECUTABLE_PREFIX = lib; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = iphoneos; + SKIP_INSTALL = YES; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + F608A9A72826B7DE005C276B /* Build configuration list for PBXProject "Core_Misc" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F608A9AE2826B7DE005C276B /* Debug */, + F608A9AF2826B7DE005C276B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + F608A9B02826B7DE005C276B /* Build configuration list for PBXNativeTarget "Core_Misc" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F608A9B12826B7DE005C276B /* Debug */, + F608A9B22826B7DE005C276B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + F608AA9C28270442005C276B /* Build configuration list for PBXNativeTarget "Core_Misc_Tests" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F608AA9D28270442005C276B /* Debug */, + F608AA9E28270442005C276B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + F6971F76282B124D008FBD17 /* Build configuration list for PBXNativeTarget "Core_Misc_iOS" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F6971F77282B124D008FBD17 /* Debug */, + F6971F78282B124D008FBD17 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = F608A9A42826B7DE005C276B /* Project object */; +} diff --git a/Core_Misc.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Core_Misc.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..94b2795 --- /dev/null +++ b/Core_Misc.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,4 @@ + + + diff --git a/Core_Misc.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Core_Misc.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/Core_Misc.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/Core_Misc.xcodeproj/xcshareddata/xcschemes/Core_Misc_Tests.xcscheme b/Core_Misc.xcodeproj/xcshareddata/xcschemes/Core_Misc_Tests.xcscheme new file mode 100644 index 0000000..4a93785 --- /dev/null +++ b/Core_Misc.xcodeproj/xcshareddata/xcschemes/Core_Misc_Tests.xcscheme @@ -0,0 +1,146 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f61a565 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +ROOTDIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))..) +include $(ROOTDIR)/Core_Make/tjp/Make/Makefile diff --git a/Makefile.def b/Makefile.def new file mode 100755 index 0000000..092252b --- /dev/null +++ b/Makefile.def @@ -0,0 +1,7 @@ +timprepscius.core_misc.include := -I $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) +timprepscius.core_misc.link := -L $(dir $(realpath $(lastword $(MAKEFILE_LIST))))/.bin/$(OBJDIR) + +timprepscius.core.include := $(timprepscius.core.include) $(timprepscius.core_misc.include) +timprepscius.core.link := $(timprepscius.core.link) $(timprepscius.core_misc.link) + + diff --git a/Makefile.project b/Makefile.project new file mode 100755 index 0000000..d5d58ad --- /dev/null +++ b/Makefile.project @@ -0,0 +1,71 @@ +include $(MAKEDIR)/Makefile.base + +# use: ls -d tjp/core/*/ tjp/core/*/*/ | rev | cut -c 2- | rev | sed 's/$/ \\/' + +PROJECTS := \ + tjp/core/args \ + tjp/core/bases \ + tjp/core/csv \ + tjp/core/compression \ + tjp/core/delegate \ + tjp/core/endian \ + tjp/core/events \ + tjp/core/extensible \ + tjp/core/file \ + tjp/core/functions \ + tjp/core/hash \ + tjp/core/http \ + tjp/core/http/hirose \ + tjp/core/http/beast \ + tjp/core/in_place \ + tjp/core/initializer \ + tjp/core/internet \ + tjp/core/pipe \ + tjp/core/profile \ + tjp/core/random \ + tjp/core/resource_version \ + tjp/core/scheduler \ + tjp/core/string_lookup \ + tjp/core/system_usage \ + tjp/core/threads \ + tjp/core/thread_pool \ + tjp/core/time \ + tjp/core/url \ + tjp/core/zip \ + +ifeq (iOS,$(SYS_NAME)) + PROJECTS += \ + tjp/core/internet/ios \ + tjp/core/file/apple \ + tjp/core/system_usage/apple \ + +else + ifeq (Darwin,$(SYS_NAME)) + PROJECTS += \ + tjp/core/internet/curl \ + tjp/core/file/apple \ + tjp/core/system_usage/apple \ + + # tjp/core/internet/curl \ + + else + PROJECTS += \ + tjp/core/internet/curl \ + tjp/core/file/linux \ + tjp/core/system_usage/linux \ + tjp/core/bases/linux \ + + endif +endif + +#SRC_PCH := tjp/core/Precompile.pch + +INCPATH := \ + $(timprepscius.libraries.cpp.include) \ + $(timprepscius.core.include) + +LIBFILE := libCore_Misc.a + +COPYTO := $(LIBRARIES_PROJECT) + +include $(MAKEDIR)/Makefile.lib diff --git a/tests/Core_Misc b/tests/Core_Misc new file mode 120000 index 0000000..a96aa0e --- /dev/null +++ b/tests/Core_Misc @@ -0,0 +1 @@ +.. \ No newline at end of file diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..721a591 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,2 @@ +ROOTDIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))../..) +include $(ROOTDIR)/Core_Make/tjp/Make/Makefile diff --git a/tests/Makefile.project b/tests/Makefile.project new file mode 100755 index 0000000..50170f7 --- /dev/null +++ b/tests/Makefile.project @@ -0,0 +1,45 @@ +include $(MAKEDIR)/Makefile.base + +# use: ls -d core/*/ core/*/*/ | rev | cut -c 2- | rev | sed 's/$/ \\/' + +BASE_PROJECT=Core_Misc + +PROJECTS := \ + . \ + $(BASE_PROJECT)/tjp/core/http/hirose/_test \ + $(BASE_PROJECT)/tjp/core/http/beast/_test \ + $(BASE_PROJECT)/tjp/core/bases/_tests \ + $(BASE_PROJECT)/tjp/core/containers/_tests \ + $(BASE_PROJECT)/tjp/core/compression/_tests \ + $(BASE_PROJECT)/tjp/core/csv/_tests \ + $(BASE_PROJECT)/tjp/core/delegate/_tests \ + $(BASE_PROJECT)/tjp/core/extensible/_tests \ + $(BASE_PROJECT)/tjp/core/pipe/_tests \ + $(BASE_PROJECT)/tjp/core/resource_version/_tests \ + $(BASE_PROJECT)/tjp/core/thread_pool/_tests \ + $(BASE_PROJECT)/tjp/core/url/_tests \ + $(BASE_PROJECT)/tjp/core/zip/_tests \ + +INCPATH := \ + $(timprepscius.libraries.cpp.include) \ + $(timprepscius.core.include) + +LDPATH := $(timprepscius.libraries.cpp.link) + +LIBS := \ + -lCore_Future \ + -lCore_Misc \ + -lCore_Zero \ + -lz_ \ + -lzstd + +EXEFILE := Core_Misc_Tests.exe + +#COPYTO := $(ROOTDIR)/.bin + +ifeq (Darwin,$(SYS_NAME)) + LIBS += -lc++ +endif + + +include $(MAKEDIR)/Makefile.bin diff --git a/tests/main.cpp b/tests/main.cpp new file mode 100644 index 0000000..4c2a4f7 --- /dev/null +++ b/tests/main.cpp @@ -0,0 +1,19 @@ +// TJP COPYRIGHT HEADER + +#define CATCH_CONFIG_RUNNER +#include + +#include + +using namespace tjp; +using namespace core; + +int main( int argc, char* argv[] ) +{ + xLogInitialize("Core_Misc_Tests.txt"); + xLogActivateStory("testing"); + xLogActivateStory("debug"); + + int result = Catch::Session().run( argc, argv ); + return result; +} diff --git a/tjp/core/algorithm/Sortable.hpp b/tjp/core/algorithm/Sortable.hpp new file mode 100644 index 0000000..1090f70 --- /dev/null +++ b/tjp/core/algorithm/Sortable.hpp @@ -0,0 +1,42 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +namespace tjp::core { + +template +struct Sortable +{ + T t; + S s; + + bool operator<(const Sortable &rhs) const + { + return s < rhs.s; + } +} ; + +template +struct SortableFunc +{ + T t; + + bool operator<(const SortableFunc &rhs) const + { + return f(t) < f(rhs.t); + } +} ; + +template +struct SortablePtr +{ + T t; + S *s; + + bool operator<(const SortablePtr &rhs) const + { + return *s < *rhs.s; + } +} ; + +} // namespace diff --git a/tjp/core/algorithm/_tests/call_with_pairs.cpp b/tjp/core/algorithm/_tests/call_with_pairs.cpp new file mode 100644 index 0000000..b98ef31 --- /dev/null +++ b/tjp/core/algorithm/_tests/call_with_pairs.cpp @@ -0,0 +1,27 @@ +// TJP COPYRIGHT HEADER + +#include +#include "../call_with_pairs.hpp" + +namespace tjp::core { +namespace { + +SCENARIO("core::call_with_pairs") +{ + WHEN("a function is called with pairs") + { + int c = 0; + + auto f = [&](auto a, auto b) + { + return c += a * b; + }; + + call_with_pairs(f, 1, 2, 3, 4, 5, 6); + + REQUIRE(c == 44); + } +} + +} +} // namespace diff --git a/tjp/core/algorithm/call_with_pairs.hpp b/tjp/core/algorithm/call_with_pairs.hpp new file mode 100644 index 0000000..94782e2 --- /dev/null +++ b/tjp/core/algorithm/call_with_pairs.hpp @@ -0,0 +1,43 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include +#include // for std::invoke + +namespace tjp::core { + +template +constexpr void call_with_pairs(F&& f, ObjOrFirstArg&& obj_or_first, Args&&... args) { + constexpr bool is_member = std::is_member_function_pointer_v>; + + if constexpr (is_member) { + static_assert(sizeof...(Args) % 2 == 0, + "Member function call_with_pairs: arguments after object must be in pairs"); + } else { + static_assert((sizeof...(Args)+1) % 2 == 0, + "Non-member call_with_pairs: all arguments must be in pairs"); + } + + if constexpr (is_member) { + auto tup = std::forward_as_tuple(std::forward(args)...); + [&](std::index_sequence) { + (std::invoke(f, + std::forward(obj_or_first), + std::get<2*I>(tup), + std::get<2*I+1>(tup)), ...); + }(std::make_index_sequence{}); + } else { + auto tup = std::forward_as_tuple(std::forward(obj_or_first), + std::forward(args)...); + [&](std::index_sequence) { + (std::invoke(f, + std::get<2*I>(tup), + std::get<2*I+1>(tup)), ...); + }(std::make_index_sequence<(sizeof...(Args) + 1) / 2>{}); + } +} + +} // namespace diff --git a/tjp/core/algorithm/find_if_random.hpp b/tjp/core/algorithm/find_if_random.hpp new file mode 100644 index 0000000..b62dfe5 --- /dev/null +++ b/tjp/core/algorithm/find_if_random.hpp @@ -0,0 +1,57 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +#include +#include +#include + +namespace tjp { +namespace core { + +template +auto find_get_if_random_transform (T &c, F &&f, F1 &&transform) +{ + std::vector items; + for (auto i=c.begin(); i!=c.end(); ++i) + { + if (f(*i)) + items.push_back(i); + } + + using value_type = decltype(transform(*items.front())); + + if (items.empty()) + return std::optional(); + + auto index = StandardRandom.nextInt(0,items.size()-1); + return std::optional(transform(*items[index])); +} + +template +auto find_get_if_random (T &c, F &&f) +{ + std::vector items; + for (auto i=c.begin(); i!=c.end(); ++i) + { + if (f(*i)) + items.push_back(i); + } + + using value_type = typename T::value_type; + + if (items.empty()) + return std::optional(); + + auto index = StandardRandom.nextInt(0,items.size()-1); + return std::optional(*items[index]); +} + + + + +} // namespace utilities +} // namespace diff --git a/tjp/core/algorithm/for_each_argument.hpp b/tjp/core/algorithm/for_each_argument.hpp new file mode 100644 index 0000000..a68fff7 --- /dev/null +++ b/tjp/core/algorithm/for_each_argument.hpp @@ -0,0 +1,17 @@ +// TJP COPYRIGHT HEADER + +// https://stackoverflow.com/questions/12030538/calling-a-function-for-each-variadic-template-argument-and-an-array + + +#pragma once + +namespace tjp { +namespace core { + +template +void for_each_argument(F f, Args&&... args) { + [](...){}((f(std::forward(args)), 0)...); +} + +} // namespace core +} // namespace diff --git a/tjp/core/algorithm/reverse_inplace.hpp b/tjp/core/algorithm/reverse_inplace.hpp new file mode 100644 index 0000000..68824fd --- /dev/null +++ b/tjp/core/algorithm/reverse_inplace.hpp @@ -0,0 +1,16 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +namespace tjp::core { + +template +T reverse_return(T &&c) +{ + for (auto i=0; i +void xor_inplace(T &t, U u) +{ + for (auto &v : t) + v ^= u; +} + +} // namespace diff --git a/tjp/core/args/Args.cpp b/tjp/core/args/Args.cpp new file mode 100644 index 0000000..52f0513 --- /dev/null +++ b/tjp/core/args/Args.cpp @@ -0,0 +1,42 @@ +// TJP COPYRIGHT HEADER + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif +#include + +#include "Args.h" +#include + +namespace tjp::core { + +TJP_CORE_HEADER_ONLY_INLINE +std::map mapArgs(int argc, const char *argv[]) +{ + std::map mapped; + static const std::string prefix = "--"; + for (int i=1; i +#include + +#include +#include + +namespace tjp { +namespace core { + +typedef std::map Args; + +std::map mapArgs(int argc, const char *argv[]); + +template +inline T argOrDefault(const std::map &args, const std::string &key, const T &default_) +{ + auto i = args.find(key); + if (i == args.end()) + { + return default_; + } + + return core::from_string(i->second); +} + +template<> +inline bool argOrDefault(const std::map &args, const std::string &key, const bool &default_) +{ + auto i = args.find(key); + if (i == args.end()) + { + return default_; + } + + return equal_case(i->second, "true"); +} + +inline std::string argOrDefault_(const std::map &args, const std::string &key, const char *default_) +{ + auto i = args.find(key); + if (i == args.end()) + { + return default_; + } + + return i->second; +} + +inline Optional argOptional(const std::map &args, const std::string &key) +{ + auto i = args.find(key); + if (i != args.end()) + return i->second; + + return {}; +} + +} // namespace +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "Args.cpp" +#endif diff --git a/tjp/core/args/ArgsFile.cpp b/tjp/core/args/ArgsFile.cpp new file mode 100644 index 0000000..c4efb33 --- /dev/null +++ b/tjp/core/args/ArgsFile.cpp @@ -0,0 +1,60 @@ +// TJP COPYRIGHT HEADER + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#include + +#include "ArgsFile.h" +#include + +#include +#include +#include + +#include +#include +#include + +namespace tjp::core { + +TJP_CORE_HEADER_ONLY_INLINE +Optional readArgsFile(const std::string& filename) +{ + std::ifstream file(filename); + if (!file) + throw Exception("Failed to open file"); + + Args result; + String line_; + + while (std::getline(file, line_)) + { + auto line = trim_copy(StringView(line_)); + // Skip empty lines and comments + if (line.empty() || line[0] == '#') + continue; + + auto pos = line.find('='); + if (pos == line.npos) + continue; + + auto key = line.substr(0, pos); + auto value = line.substr(pos + 1); + + result[String(key)] = String(value); + } + + return result; +} + +TJP_CORE_HEADER_ONLY_INLINE +void mergeArgs(Args &args, Args &rhs) +{ + args.merge(rhs); +} + + +} // namespace + diff --git a/tjp/core/args/ArgsFile.h b/tjp/core/args/ArgsFile.h new file mode 100644 index 0000000..9f21b6e --- /dev/null +++ b/tjp/core/args/ArgsFile.h @@ -0,0 +1,16 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Args.h" + +namespace tjp::core { + +Optional readArgsFile(const String &fileName); +void mergeArgs(Args &args, Args &rhs); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "ArgsFile.cpp" +#endif diff --git a/tjp/core/bases/Base16.cpp b/tjp/core/bases/Base16.cpp new file mode 100755 index 0000000..8ccd826 --- /dev/null +++ b/tjp/core/bases/Base16.cpp @@ -0,0 +1,96 @@ +// TJP COPYRIGHT HEADER + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#include + +#include "Base16.h" + +#include +#include +#include + +#include +#include + +namespace tjp::core { + +using b16d = b16_bin; + +TJP_CORE_HEADER_ONLY_INLINE +size_t toBase16 (const b16d *in, size_t inSize, b16_char *out, size_t outSize) +{ + const char *base16_charset = "0123456789abcdef"; + + handle_assert(outSize == 2 * inSize) { + return 0; + } + + auto begin = in; + auto end = in + inSize; + + for (auto *i = begin; i!=end; ++i) + { + int n1 = (int)*i & 0x0F; + int n2 = (int)*i >> 4 & 0x0F; + + *out++ = base16_charset[n2]; + *out++ = base16_charset[n1]; + } + + return 2*inSize; +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string toBase16 (const b16d *dataToEncode, size_t dataToEncodeLength) +{ + std::string encodedData; + encodedData.resize(dataToEncodeLength*2); + + toBase16(dataToEncode, dataToEncodeLength, encodedData.data(), encodedData.size()); + + return encodedData; +} + + +inline int getNibble (char c) +{ + if (c >= '0' && c <= '9') + return c - '0'; + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + + handle_assert(false) + ; + + return 0; +} + +TJP_CORE_HEADER_ONLY_INLINE +size_t fromBase16 (const char *in, size_t inSize, b16d *out, size_t outSize) +{ + handle_assert((inSize % 2 == 0) && (inSize == 2 * outSize)) { + return 0; + } + + auto begin = in; + auto end = in + inSize; + + for (auto *i = begin; i!=end; ) + { + int n2 = getNibble(*i++); + int n1 = getNibble(*i++); + + unsigned char c = n2 << 4 | n1; + + *out++ = c; + } + + return outSize; +} + +} // namespace diff --git a/tjp/core/bases/Base16.h b/tjp/core/bases/Base16.h new file mode 100755 index 0000000..dcbf4d3 --- /dev/null +++ b/tjp/core/bases/Base16.h @@ -0,0 +1,104 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include + +namespace tjp::core { + +using b16_bin = u8; +using b16_char = char; + +size_t fromBase16 (const b16_char *in, size_t inSize, b16_bin *out, size_t outSize); + +inline +size_t base16EncodeSize(size_t size) +{ + return size * 2; +} + +inline +size_t base16DecodeSize(const b16_char *b, size_t size) +{ + return size / 2; +} + + +template +std::vector fromBase16 (const b16_char *b, size_t size) +{ + auto decodeSize = base16DecodeSize(b, size); + std::vector r(decodeSize / sizeof(T)); + + fromBase16((const b16_char *)b, size, (b16_bin *)r.data(), decodeSize); + return r; +} + +template +U fromBase16v (const b16_char *b, size_t size) +{ + auto decodeSize = base16DecodeSize(b, size); + U r(decodeSize / sizeof(typename U::value_type)); + + fromBase16((const b16_char *)b, size, (b16_bin *)r.data(), decodeSize); + return r; +} + +template +std::vector fromBase16 (const std::string_view &block) +{ + return fromBase16(block.data(), block.size()); +} + +template +bool fromBase16(T &t, const b16_char *b, size_t size) +{ + return fromBase16(b, size, (b16_bin *)&t, sizeof(t)) == sizeof(t); +} + +template +bool fromBase16(T &t, const std::string_view &block) +{ + return fromBase16(t, block.data(), block.size()); +} + +std::string toBase16 (const b16_bin *b, size_t size); +size_t toBase16 (const b16_bin *in, size_t inSize, b16_char *out, size_t outSize); + +template +std::string toBase16 (const T *b, size_t size) +{ + return toBase16((const b16_bin *)b, sizeof(*b) * size); +} + +//template +//std::string toBase16 (const std::vector &block) +//{ +// return toBase16((b16_bin *)block.data(), sizeof(block[0]) * block.size()); +//} + +template +std::string toBase16 (const T &block) +{ + return toBase16((b16_bin *)block.data(), sizeof(typename T::value_type) * block.size()); +} + +template +bool toBase16(const T &t, b16_char *out, size_t outSize) +{ + return toBase16((b16_bin *)&t, sizeof(t), out, outSize) == outSize; +} + +template +bool toBase16(const T &t, const std::string_view &block) +{ + return toBase16(t, (b16_char *)block.data(), block.size()); +} + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "Base16.cpp" +#endif diff --git a/tjp/core/bases/Base32.cpp b/tjp/core/bases/Base32.cpp new file mode 100755 index 0000000..3f4bd4c --- /dev/null +++ b/tjp/core/bases/Base32.cpp @@ -0,0 +1,88 @@ +// TJP COPYRIGHT HEADER + +#include "Base32.h" + +#include +#include + +#include +#include +#include + +#include +#include + +namespace tjp::core { + +//using base32 = cppcodec::base32_rfc4648; +//using base32base = cppcodec::detail::base32_rfc4648; + +using base32 = cppcodec::base32_crockford; +using base32base = cppcodec::detail::base32_crockford; + +size_t base32EncodeSize(size_t size) +{ + return base32::encoded_size(size); +} + +size_t base32DecodeSize(const b32_char *b, size_t size) +{ + auto maxSize = base32::decoded_max_size(size); + auto actualSize = maxSize; + + if constexpr (base32base::generates_padding()) + { + auto end = size; + auto i = end; + for (; i>0; --i) + { + if (!base32base::is_padding_symbol(b[i-1])) + break; + } + + auto numPadding = end - i; + + int paddingRemove = 0; + switch (numPadding) { + case 1: + case 2: paddingRemove = 1; break; + case 3: paddingRemove = 2; break; + case 4: paddingRemove = 3; break; + case 5: + case 6: paddingRemove = 4; break; + case 7: + case 8: paddingRemove = 5; break; + } + + actualSize = maxSize - paddingRemove; + } + + return actualSize; +} + +using b32d = b32_bin; + +size_t toBase32 (const b32d *in, size_t inSize, b32_char *out, size_t outSize) +{ + auto size = base32::encode(out, outSize, in, inSize); + + return size; +} + +std::string toBase32 (const b32d *dataToEncode, size_t dataToEncodeLength) +{ + std::string encodedData; + encodedData.resize(dataToEncodeLength*2); + + return base32::encode(dataToEncode, dataToEncodeLength); +} + +size_t fromBase32 (const char *in, size_t inSize, b32d *out, size_t outSize) +{ + debug_assert(outSize == base32DecodeSize(in, inSize)); + + auto size = base32::decode(out, base32::decoded_max_size(inSize), in, inSize); + return size; +} + +} // namespace diff --git a/tjp/core/bases/Base32.h b/tjp/core/bases/Base32.h new file mode 100755 index 0000000..2323cc8 --- /dev/null +++ b/tjp/core/bases/Base32.h @@ -0,0 +1,91 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include + +namespace tjp::core { + +using b32_bin = u8; +using b32_char = char; + +size_t fromBase32 (const b32_char *in, size_t inSize, b32_bin *out, size_t outSize); + +size_t base32EncodeSize(size_t size); +size_t base32DecodeSize(const b32_char *b, size_t size); + +template +std::vector fromBase32 (const b32_char *b, size_t size) +{ + auto decodeSize = base32DecodeSize(b, size); + std::vector r(decodeSize / sizeof(T)); + + fromBase32((const b32_char *)b, size, (b32_bin *)r.data(), decodeSize); + return r; +} + +template +U fromBase32v (const b32_char *b, size_t size) +{ + auto decodeSize = base32DecodeSize(b, size); + U r(decodeSize / sizeof(typename U::value_type)); + + fromBase32((const b32_char *)b, size, (b32_bin *)r.data(), decodeSize); + return r; +} + +template +std::vector fromBase32 (const std::string_view &block) +{ + return fromBase32(block.data(), block.size()); +} + +template +bool fromBase32(T &t, const b32_char *b, size_t size) +{ + return fromBase32(b, size, (b32_bin *)&t, sizeof(t)) == sizeof(t); +} + +template +bool fromBase32(T &t, const std::string_view &block) +{ + return fromBase32(t, block.data(), block.size()); +} + +std::string toBase32 (const b32_bin *b, size_t size); +size_t toBase32 (const b32_bin *in, size_t inSize, b32_char *out, size_t outSize); + +template +std::string toBase32 (const T *b, size_t size) +{ + return toBase32((const b32_bin *)b, sizeof(*b) * size); +} + +//template +//std::string toBase32 (const std::vector &block) +//{ +// return toBase32((b32_bin *)block.data(), sizeof(block[0]) * block.size()); +//} + +template +std::string toBase32 (const T &block) +{ + return toBase32((b32_bin *)block.data(), sizeof(typename T::value_type) * block.size()); +} + +template +bool toBase32(const T &t, b32_char *out, size_t outSize) +{ + return toBase32((b32_bin *)&t, sizeof(t), out, outSize) == outSize; +} + +template +bool toBase32(const T &t, const std::string_view &block) +{ + return toBase32(t, (b32_char *)block.data(), block.size()); +} + +} // namespace + diff --git a/tjp/core/bases/Base64.cpp b/tjp/core/bases/Base64.cpp new file mode 100755 index 0000000..ff62b37 --- /dev/null +++ b/tjp/core/bases/Base64.cpp @@ -0,0 +1,362 @@ +// TJP COPYRIGHT HEADER + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#include + +#include "Base64.h" + +//#include +//#include +//#include + +#include + +#include +#include + +namespace tjp { +namespace core { +namespace detail { + +static const char Base64[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static const char Pad64 = '='; + +#define Assert debug_assert + +using u_char = unsigned char; +using u_int = unsigned int; + +TJP_CORE_HEADER_ONLY_INLINE +int b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize) { + size_t datalength = 0; + u_char input[3]; + u_char output[4]; + size_t i; + + while (2 < srclength) { + input[0] = *src++; + input[1] = *src++; + input[2] = *src++; + srclength -= 3; + + output[0] = input[0] >> 2; + output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); + output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); + output[3] = input[2] & 0x3f; + Assert(output[0] < 64); + Assert(output[1] < 64); + Assert(output[2] < 64); + Assert(output[3] < 64); + + if (datalength + 4 > targsize) + return (-1); + target[datalength++] = Base64[output[0]]; + target[datalength++] = Base64[output[1]]; + target[datalength++] = Base64[output[2]]; + target[datalength++] = Base64[output[3]]; + } + + /* Now we worry about padding. */ + if (0 != srclength) { + /* Get what's left. */ + input[0] = input[1] = input[2] = '\0'; + for (i = 0; i < srclength; i++) + input[i] = *src++; + + output[0] = input[0] >> 2; + output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); + output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); + Assert(output[0] < 64); + Assert(output[1] < 64); + Assert(output[2] < 64); + + if (datalength >= targsize) + return -1; + + target[datalength++] = Base64[output[0]]; + + if (datalength >= targsize) + return -1; + + target[datalength++] = Base64[output[1]]; + + + if (srclength != 1) + { + if (datalength >= targsize) + return -1; + + target[datalength++] = Base64[output[2]]; + } + } + if (datalength > targsize) + return (-1); + + if (targsize > datalength) + target[datalength] = '\0'; /* Returned value doesn't count \0. */ + + return (int)datalength; +} + + +/* + +TJP, man I can't believe I had to do this. +So I made the b64_pton_l work with sizes for input, and outputs which don't have \0 +*/ + +TJP_CORE_HEADER_ONLY_INLINE +int b64_pton_l(char const *src, size_t srcLength, u_char *target, size_t targsize) +{ + const char *end = src + srcLength; + u_int tarindex, state; + int ch; + const char *pos; + + state = 0; + tarindex = 0; + + while (src != end && (ch = *src++) != '\0') { + if (isspace(ch)) /* Skip whitespace anywhere. */ + continue; + + if (ch == Pad64) + break; + + pos = strchr(Base64, ch); + if (pos == 0) /* A non-base64 character. */ + return (-1); + + switch (state) { + case 0: + if (target) { + if (tarindex >= targsize) + return (-1); + target[tarindex] = (pos - Base64) << 2; + } + state = 1; + break; + case 1: + if (target) { + target[tarindex] |= (pos - Base64) >> 4; + if (tarindex + 1 >= targsize) + return (-1); + target[tarindex+1] = ((pos - Base64) & 0x0f) + << 4 ; + } + tarindex++; + state = 2; + break; + case 2: + if (target) { + target[tarindex] |= (pos - Base64) >> 2; + if (tarindex + 1 >= targsize) + return (-1); + target[tarindex+1] = ((pos - Base64) & 0x03) + << 6; + } + tarindex++; + state = 3; + break; + case 3: + if (target) { + if (tarindex >= targsize) + return (-1); + target[tarindex] |= (pos - Base64); + } + tarindex++; + state = 0; + break; + } + } + + /* + * We are done decoding Base-64 chars. Let's see if we ended + * on a byte boundary, and/or with erroneous trailing characters. + */ + +// if (ch == Pad64) { /* We got a pad char. */ +// ch = *src++; /* Skip it, get next. */ +// switch (state) { +// case 0: /* Invalid = in first position */ +// case 1: /* Invalid = in second position */ +// return (-1); +// +// case 2: /* Valid, means one byte of info */ +// /* Skip any number of spaces. */ +// for (; ch != '\0'; ch = *src++) +// if (!isspace(ch)) +// break; +// /* Make sure there is another trailing = sign. */ +// if (ch != Pad64) +// return (-1); +// ch = *src++; /* Skip the = */ +// /* Fall through to "single trailing =" case. */ +// /* FALLTHROUGH */ +// +// case 3: /* Valid, means two bytes of info */ +// /* +// * We know this char is an =. Is there anything but +// * whitespace after it? +// */ +// for (; ch != '\0'; ch = *src++) +// if (!isspace(ch)) +// return (-1); +// +// /* +// * Now make sure for cases 2 and 3 that the "extra" +// * bits that slopped past the last full byte were +// * zeros. If we don't check them, they become a +// * subliminal channel. +// */ +// if (target && target[tarindex] != 0) +// return (-1); +// } +// } else { +// /* +// * We ended by seeing the end of the string. Make sure we +// * have no partial bytes lying around. +// */ +// if (state != 0) +// return (-1); +// } + + return (tarindex); +} + +} // namespace + +TJP_CORE_HEADER_ONLY_INLINE +size_t base64EncodeSize(size_t v) +{ + if (v == 0) + return 0; + + auto vm1 = v-1; + + // 1 = 2 = 0 * 4 + 2 + // 2 = 3 = 0 * 4 + 2 + 1 + // 3 = 4 = 0 * 4 + 2 + 2 + + // 4 = 6 = 1 * 4 + 2 + 0 + // 5 = 7 = 1 * 4 + 2 + 1 + // 6 = 8 + + // 7 = 10 = 6+4 + + return (vm1 / 3 * 4 + 2) + (vm1 % 3); +} + +TJP_CORE_HEADER_ONLY_INLINE +size_t base64DecodeSize(size_t v) +{ + return v * 3 / 4; +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string toBase64 (const std::vector &dataToEncode) +{ + return toBase64(dataToEncode.data(), (int)dataToEncode.size()); +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string toBase64 (const std::string_view &dataToEncode) +{ + return toBase64(dataToEncode.data(), (int)dataToEncode.size()); +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string toBase64 (const std::string &dataToEncode) +{ + return toBase64(dataToEncode.data(), (int)dataToEncode.size()); +} + +TJP_CORE_HEADER_ONLY_INLINE +size_t toBase64 (const char *in, size_t inSize, char *out, size_t outSize) +{ + return detail::b64_ntop( + (const unsigned char *)in, inSize, + out, outSize + ); +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string toBase64 (const char *dataToEncode, size_t dataToEncodeLength) +{ + std::string encodedData; + auto encodeSize = base64EncodeSize(dataToEncodeLength); + encodedData.resize(encodeSize); + + auto encodedRealLength = toBase64( + dataToEncode, dataToEncodeLength, + encodedData.data(), encodeSize + ); + + debug_assert(encodedRealLength == encodeSize); + + if (encodedRealLength == -1) + return {}; + + return encodedData; +} + +TJP_CORE_HEADER_ONLY_INLINE +void padBase64(std::string &v) +{ + auto paddingLength = (4 - (v.size() % 4)) % 4; + for (auto _ : range(paddingLength)) + v.push_back('='); +} + +TJP_CORE_HEADER_ONLY_INLINE +std::vector fromBase64 (const std::string_view &dataToDecode) +{ + return fromBase64(dataToDecode.data(), (int)dataToDecode.size()); +} + +TJP_CORE_HEADER_ONLY_INLINE +size_t fromBase64(const char *in, size_t inSize, char *out, size_t outSize) +{ + return detail::b64_pton_l( + in, inSize, + (unsigned char *)out, outSize + ); +} + +TJP_CORE_HEADER_ONLY_INLINE +size_t fromBase64(const std::string_view &in, char *out, size_t outSize) +{ + return detail::b64_pton_l( + in.data(), in.size(), + (unsigned char *)out, outSize + ); +} + +TJP_CORE_HEADER_ONLY_INLINE +std::vector fromBase64 (const char *dataToDecode, size_t dataToDecodeLength) +{ + std::vector decodedData; + + auto decodedBufferLength = dataToDecodeLength * 3 / 4 + 1 + 128; + decodedData.resize(decodedBufferLength); + + auto decodedBufferRealLength = fromBase64( + dataToDecode, dataToDecodeLength, + decodedData.data(), + decodedBufferLength + ); + + debug_assert(decodedBufferRealLength <= base64DecodeSize(dataToDecodeLength)); + + decodedData.resize(decodedBufferRealLength); + return decodedData; +} + + +} // namespace +} // namespace + diff --git a/tjp/core/bases/Base64.h b/tjp/core/bases/Base64.h new file mode 100755 index 0000000..2a25271 --- /dev/null +++ b/tjp/core/bases/Base64.h @@ -0,0 +1,46 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include + +namespace tjp::core { + +void padBase64 (std::string &); + +size_t base64EncodeSize(size_t); +size_t base64DecodeSize(size_t); + +std::string toBase64 (const std::vector &block); +std::string toBase64 (const std::string_view &block); +std::string toBase64 (const std::string &block); +std::vector fromBase64 (const std::string_view &b64); + +std::string toBase64 (const char *b, size_t size); +std::vector fromBase64 (const char *b, size_t size); + +size_t toBase64 (const char *in, size_t inSize, char *out, size_t outSize); +size_t fromBase64(const char *in, size_t inSize, char *out, size_t outSize); +size_t fromBase64(const std::string_view &in, char *out, size_t outSize); + +template +T fromBase64(const std::string_view &in) +{ + T t; + fromBase64((const char *)in.data(), in.size(), (char *)&t, sizeof(t)); + return t; +} + +template +std::string toBase64(const T &t) +{ + return toBase64((const char *)&t, sizeof(t)); +} + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "Base64.cpp" +#endif diff --git a/tjp/core/bases/Base64URL.cpp b/tjp/core/bases/Base64URL.cpp new file mode 100755 index 0000000..32dcf5e --- /dev/null +++ b/tjp/core/bases/Base64URL.cpp @@ -0,0 +1,114 @@ +// TJP COPYRIGHT HEADER + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#include + +#include "Base64URL.h" + + +namespace tjp::core { + +/* +Base64 translates 24 bits into 4 ASCII characters at a time. First, +3 8-bit bytes are treated as 4 6-bit groups. Those 4 groups are +translated into ASCII characters. That is, each 6-bit number is treated +as an index into the ASCII character array. + +If the final set of bits is less 8 or 16 instead of 24, traditional base64 +would add a padding character. However, if the length of the data is +known, then padding can be eliminated. + +One difference between the "standard" Base64 is two characters are different. +See RFC 4648 for details. +This is how we end up with the Base64 URL encoding. +*/ + +const char base64_url_alphabet[] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_' +}; + +TJP_CORE_HEADER_ONLY_INLINE +std::string toBase64URL(const char *in, int len) { + std::string out; + int val =0, valb=-6; + unsigned int i = 0; + for (i = 0; i < len; i++) { + unsigned char c = in[i]; + val = (val<<8) + c; + valb += 8; + while (valb >= 0) { + out.push_back(base64_url_alphabet[(val>>valb)&0x3F]); + valb -= 6; + } + } + if (valb > -6) { + out.push_back(base64_url_alphabet[((val<<8)>>(valb+8))&0x3F]); + } + return out; +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string toBase64URL(const char *in, size_t len) { + return toBase64URL(in, (int)len); +} + +TJP_CORE_HEADER_ONLY_INLINE +std::vector fromBase64URL(const char *in, int len) { + std::vector out; + + static std::vector T; + + if (T.empty()) + { + T.resize(256, -1); + for (auto i =0; i < 64; i++) T[base64_url_alphabet[i]] = i; + } + + unsigned int i; + int val = 0, valb = -8; + for (i = 0; i < len; i++) { + unsigned char c = in[i]; + if (T[c] == -1) break; + val = (val<<6) + T[c]; + valb += 6; + if (valb >= 0) { + out.push_back(char((val>>valb)&0xFF)); + valb -= 8; + } + } + return out; +} + +TJP_CORE_HEADER_ONLY_INLINE +std::vector fromBase64URL(const char *in, size_t len) { + return fromBase64URL(in, (int)len); +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string toBase64URL (const std::vector &block) +{ + return toBase64URL(block.data(), block.size()); +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string toBase64URL (const std::string_view &block) +{ + return toBase64URL(block.data(), block.size()); +} + +TJP_CORE_HEADER_ONLY_INLINE +std::vector fromBase64URL (const std::string_view &b64) +{ + return fromBase64URL(b64.data(), b64.size()); +} + + +} // namespace + diff --git a/tjp/core/bases/Base64URL.h b/tjp/core/bases/Base64URL.h new file mode 100755 index 0000000..9ec5a62 --- /dev/null +++ b/tjp/core/bases/Base64URL.h @@ -0,0 +1,22 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include + +namespace tjp::core { + +std::string toBase64URL (const std::vector &block); +std::string toBase64URL (const std::string_view &block); +std::vector fromBase64URL (const std::string_view &b64); + +std::string toBase64URL (const char *b, size_t size); +std::vector fromBase64URL (const char *b, size_t size); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "Base64URL.cpp" +#endif diff --git a/tjp/core/bases/_remove/ntop.c b/tjp/core/bases/_remove/ntop.c new file mode 100755 index 0000000..fedc086 --- /dev/null +++ b/tjp/core/bases/_remove/ntop.c @@ -0,0 +1,310 @@ +/* + * Copyright (c) 1996-1999 by Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + */ + +/* + * Portions Copyright (c) 1995 by International Business Machines, Inc. + * + * International Business Machines, Inc. (hereinafter called IBM) grants + * permission under its copyrights to use, copy, modify, and distribute this + * Software with or without fee, provided that the above copyright notice and + * all paragraphs of this notice appear in all copies, and that the name of IBM + * not be used in connection with the marketing of any product incorporating + * the Software or modifications thereof, without specific, written prior + * permission. + * + * To the extent it has a right to do so, IBM grants an immunity from suit + * under its patents, if any, for the use, sale or manufacture of products to + * the extent that such products are used for performing Domain Name System + * dynamic updates in TCP/IP networks by means of the Software. No immunity is + * granted for any product per se or for any other function of any product. + * + * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, + * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN + * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#define Assert(Cond) if (!(Cond)) abort() + +static const char Base64[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static const char Pad64 = '='; + +/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt) + The following encoding technique is taken from RFC 1521 by Borenstein + and Freed. It is reproduced here in a slightly edited form for + convenience. + + A 65-character subset of US-ASCII is used, enabling 6 bits to be + represented per printable character. (The extra 65th character, "=", + is used to signify a special processing function.) + + The encoding process represents 24-bit groups of input bits as output + strings of 4 encoded characters. Proceeding from left to right, a + 24-bit input group is formed by concatenating 3 8-bit input groups. + These 24 bits are then treated as 4 concatenated 6-bit groups, each + of which is translated into a single digit in the base64 alphabet. + + Each 6-bit group is used as an index into an array of 64 printable + characters. The character referenced by the index is placed in the + output string. + + Table 1: The Base64 Alphabet + + Value Encoding Value Encoding Value Encoding Value Encoding + 0 A 17 R 34 i 51 z + 1 B 18 S 35 j 52 0 + 2 C 19 T 36 k 53 1 + 3 D 20 U 37 l 54 2 + 4 E 21 V 38 m 55 3 + 5 F 22 W 39 n 56 4 + 6 G 23 X 40 o 57 5 + 7 H 24 Y 41 p 58 6 + 8 I 25 Z 42 q 59 7 + 9 J 26 a 43 r 60 8 + 10 K 27 b 44 s 61 9 + 11 L 28 c 45 t 62 + + 12 M 29 d 46 u 63 / + 13 N 30 e 47 v + 14 O 31 f 48 w (pad) = + 15 P 32 g 49 x + 16 Q 33 h 50 y + + Special processing is performed if fewer than 24 bits are available + at the end of the data being encoded. A full encoding quantum is + always completed at the end of a quantity. When fewer than 24 input + bits are available in an input group, zero bits are added (on the + right) to form an integral number of 6-bit groups. Padding at the + end of the data is performed using the '=' character. + + Since all base64 input is an integral number of octets, only the + ------------------------------------------------- + following cases can arise: + + (1) the final quantum of encoding input is an integral + multiple of 24 bits; here, the final unit of encoded + output will be an integral multiple of 4 characters + with no "=" padding, + (2) the final quantum of encoding input is exactly 8 bits; + here, the final unit of encoded output will be two + characters followed by two "=" padding characters, or + (3) the final quantum of encoding input is exactly 16 bits; + here, the final unit of encoded output will be three + characters followed by one "=" padding character. + */ + +int +b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize) { + size_t datalength = 0; + u_char input[3]; + u_char output[4]; + size_t i; + + while (2 < srclength) { + input[0] = *src++; + input[1] = *src++; + input[2] = *src++; + srclength -= 3; + + output[0] = input[0] >> 2; + output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); + output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); + output[3] = input[2] & 0x3f; + Assert(output[0] < 64); + Assert(output[1] < 64); + Assert(output[2] < 64); + Assert(output[3] < 64); + + if (datalength + 4 > targsize) + return (-1); + target[datalength++] = Base64[output[0]]; + target[datalength++] = Base64[output[1]]; + target[datalength++] = Base64[output[2]]; + target[datalength++] = Base64[output[3]]; + } + + /* Now we worry about padding. */ + if (0 != srclength) { + /* Get what's left. */ + input[0] = input[1] = input[2] = '\0'; + for (i = 0; i < srclength; i++) + input[i] = *src++; + + output[0] = input[0] >> 2; + output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); + output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); + Assert(output[0] < 64); + Assert(output[1] < 64); + Assert(output[2] < 64); + + if (datalength + 4 > targsize) + return (-1); + target[datalength++] = Base64[output[0]]; + target[datalength++] = Base64[output[1]]; + if (srclength == 1) + target[datalength++] = Pad64; + else + target[datalength++] = Base64[output[2]]; + target[datalength++] = Pad64; + } + if (datalength >= targsize) + return (-1); + target[datalength] = '\0'; /* Returned value doesn't count \0. */ + return (datalength); +} +libresolv_hidden_def (b64_ntop) + +/* skips all whitespace anywhere. + converts characters, four at a time, starting at (or after) + src from base - 64 numbers into three 8 bit bytes in the target area. + it returns the number of data bytes stored at the target, or -1 on error. + */ + +int +b64_pton (char const *src, u_char *target, size_t targsize) +{ + int tarindex, state, ch; + char *pos; + + state = 0; + tarindex = 0; + + while ((ch = *src++) != '\0') { + if (isspace(ch)) /* Skip whitespace anywhere. */ + continue; + + if (ch == Pad64) + break; + + pos = strchr(Base64, ch); + if (pos == 0) /* A non-base64 character. */ + return (-1); + + switch (state) { + case 0: + if (target) { + if ((size_t)tarindex >= targsize) + return (-1); + target[tarindex] = (pos - Base64) << 2; + } + state = 1; + break; + case 1: + if (target) { + if ((size_t)tarindex + 1 >= targsize) + return (-1); + target[tarindex] |= (pos - Base64) >> 4; + target[tarindex+1] = ((pos - Base64) & 0x0f) + << 4 ; + } + tarindex++; + state = 2; + break; + case 2: + if (target) { + if ((size_t)tarindex + 1 >= targsize) + return (-1); + target[tarindex] |= (pos - Base64) >> 2; + target[tarindex+1] = ((pos - Base64) & 0x03) + << 6; + } + tarindex++; + state = 3; + break; + case 3: + if (target) { + if ((size_t)tarindex >= targsize) + return (-1); + target[tarindex] |= (pos - Base64); + } + tarindex++; + state = 0; + break; + default: + abort(); + } + } + + /* + * We are done decoding Base-64 chars. Let's see if we ended + * on a byte boundary, and/or with erroneous trailing characters. + */ + + if (ch == Pad64) { /* We got a pad char. */ + ch = *src++; /* Skip it, get next. */ + switch (state) { + case 0: /* Invalid = in first position */ + case 1: /* Invalid = in second position */ + return (-1); + + case 2: /* Valid, means one byte of info */ + /* Skip any number of spaces. */ + for ((void)NULL; ch != '\0'; ch = *src++) + if (!isspace(ch)) + break; + /* Make sure there is another trailing = sign. */ + if (ch != Pad64) + return (-1); + ch = *src++; /* Skip the = */ + /* Fall through to "single trailing =" case. */ + /* FALLTHROUGH */ + + case 3: /* Valid, means two bytes of info */ + /* + * We know this char is an =. Is there anything but + * whitespace after it? + */ + for ((void)NULL; ch != '\0'; ch = *src++) + if (!isspace(ch)) + return (-1); + + /* + * Now make sure for cases 2 and 3 that the "extra" + * bits that slopped past the last full byte were + * zeros. If we don't check them, they become a + * subliminal channel. + */ + if (target && target[tarindex] != 0) + return (-1); + } + } else { + /* + * We ended by seeing the end of the string. Make sure we + * have no partial bytes lying around. + */ + if (state != 0) + return (-1); + } + + return (tarindex); +} diff --git a/tjp/core/bases/_tests/Base16.cpp b/tjp/core/bases/_tests/Base16.cpp new file mode 100644 index 0000000..1d54121 --- /dev/null +++ b/tjp/core/bases/_tests/Base16.cpp @@ -0,0 +1,36 @@ +// TJP COPYRIGHT HEADER + +#include +#include + +namespace tjp::core { +namespace { + +SCENARIO("base16") +{ + GIVEN("size calculations") + { + std::vector x; + x.reserve(512); + for (auto i=0; i<512; i++) + { + x.resize(i); + for (auto j=0; j +#include + +namespace tjp::core { +namespace { + +SCENARIO("base32") +{ + GIVEN("size calculations") + { + std::vector x; + x.reserve(512); + for (auto i=0; i<512; i++) + { + x.resize(i); + for (auto j=0; j +#include + +namespace tjp::core { +namespace { + +SCENARIO("base64") +{ + GIVEN("size calculations") + { + std::vector x; + x.reserve(512); + for (auto i=0; i<512; i++) + { + x.resize(i); + for (auto j=0; j +#include +#include + +#include + +namespace tjp::core::compression::zlib { + +struct CompressContexts { + Mutex mutex; + Map> contextsByLevel; + + ~CompressContexts() + { + for (auto &[l, contexts] : contextsByLevel) + for (auto *c : contexts) + free((z_streamp)c); + } + + void *getContext(int level) + { + { + auto lock = lock_of(mutex); + + auto &contexts = contextsByLevel[level]; + if (!contexts.empty()) + { + auto *v = contexts.back(); + contexts.pop_back(); + return v; + } + } + + auto z = (z_streamp)calloc(1, sizeof(z_stream)); + deflateInit(z, level); + return z; + } + + void putContext(int level, void *v) + { + auto lock = lock_of(mutex); + contextsByLevel[level].push_back(v); + } +} ; + +struct DecompressContexts +{ + Mutex mutex; + Vector contexts; + + ~DecompressContexts() + { + for (auto *c : contexts) + free((z_streamp)c); + } + + void *getContext() + { + { + auto lock = lock_of(mutex); + + if (!contexts.empty()) + { + auto *v = contexts.back(); + contexts.pop_back(); + return v; + } + } + + auto z = (z_streamp)calloc(1, sizeof(z_stream)); + inflateInit(z); + return z; + } + + void putContext(void *v) + { + auto lock = lock_of(mutex); + contexts.push_back(v); + } +} ; + +CompressContexts compressContexts; +DecompressContexts decompressContexts; + +// -------------- + +bool clearStream(void *p, bool inflate) +{ + auto z = (z_streamp)p; + + if (inflate) + return (inflateReset(z) == Z_OK); + else + return (deflateReset(z) == Z_OK); +} + + +bool setDict(void *p, bool inflate, char *dict, size_t size) +{ + auto z = (z_streamp)p; + + int error = 0; + if (inflate) + error = inflateSetDictionary(z, (const Bytef *)dict, (uInt)size); + else + error = deflateSetDictionary(z, (const Bytef *)dict, (uInt)size); + + return error == Z_OK; +} + +bool getDict(void *p, bool inflate, char *dict, size_t size) +{ + auto z = (z_streamp)p; + + auto size_ = (uInt)size; + + char store[32768]; + + int error = 0; + if (inflate) + error = inflateGetDictionary(z, (Bytef *)&store[0], &size_); + else + error = deflateGetDictionary(z, (Bytef *)&store[0], &size_); + + // zlib seems to store from end to front + auto offset = + size_ > size ? + size_ - size : + 0; + + mem_copy(dict, &store[offset], size); + + return error == Z_OK; +} + + +// -------------- + +CompressUnordered::CompressUnordered(int level_) : + level(level_) +{ +} + +size_t CompressUnordered::execute(char *dest_, size_t destLen_, const char *source_, size_t sourceLen_) +{ + const Bytef *source = (const Bytef *)source_; + uLongf sourceLen = sourceLen_; + Bytef *dest = (Bytef *)dest_; + uLongf destLen = destLen_; + + auto context = (z_streamp)compressContexts.getContext(level); + + context->next_in = (Bytef *)source; + context->avail_in = (uInt)sourceLen; + + context->next_out = dest; + context->avail_out = (uInt)destLen; + + auto result = deflate(context, Z_FINISH); + auto toSize = context->total_out; + + compressContexts.putContext(level, context); + + if (result != Z_STREAM_END) + return 0; + + return toSize; +} + +size_t UncompressUnordered::execute(char *dest_, size_t destLen_, const char *source_, size_t sourceLen_) +{ + const Bytef *source = (const Bytef *)source_; + uLongf sourceLen = sourceLen_; + Bytef *dest = (Bytef *)dest_; + uLongf destLen = destLen_; + + auto context = (z_streamp)decompressContexts.getContext(); + + context->next_in = (Bytef *)source; + context->avail_in = (uInt)sourceLen; + + context->next_out = dest; + context->avail_out = (uInt)destLen; + + auto result = inflate(context, Z_FINISH); + auto toSize = context->total_out; + + decompressContexts.putContext(context); + + if (result != Z_STREAM_END) + return 0; + + return toSize; +} + + +// -------- + + + +CompressOrdered::CompressOrdered(int level_) : + level(level_) +{ + memset(dictionary, 0, dictionarySize); +} + +CompressOrdered::~CompressOrdered() +{ +} + +size_t CompressOrdered::execute(char *dest_, size_t destLen_, const char *source_, size_t sourceLen_) +{ + const Bytef *source = (const Bytef *)source_; + uLongf sourceLen = sourceLen_; + Bytef *dest = (Bytef *)dest_; + uLongf destLen = destLen_; + + auto context = (z_streamp)compressContexts.getContext(level); + + if (!clearStream(context, false)) + return 0; + + if (!setDict(context, false, dictionary, dictionarySize)) + return 0; + + context->next_in = (Bytef *)source; + context->avail_in = (uInt)sourceLen; + + context->next_out = dest; + context->avail_out = (uInt)destLen; + + auto result = deflate(context, Z_FINISH); + auto toSize = context->total_out; + + if (result == Z_STREAM_END) + getDict(context, false, dictionary, dictionarySize); + + compressContexts.putContext(level, context); + + if (result != Z_STREAM_END) + return 0; + + return toSize; +} + +size_t CompressOrdered::execute(Vector &dest, const containers::MemorySegment &source) +{ + auto context = (z_streamp)compressContexts.getContext(level); + + if (!clearStream(context, false)) + return 0; + + if (!setDict(context, false, dictionary, dictionarySize)) + return 0; + + context->next_in = (Bytef *)source.data(); + context->avail_in = (uInt)source.size(); + + auto sizeOffset = dest.size(); + dest.resize(dest.size() + sizeof(u32)); + auto bos = dest.size(); + + int result = 0; + + while(1) + { + auto newSize = std::max(bos + context->total_out + 2048, (size_t)16384); + dest.resize(newSize); + + auto eos = bos + context->total_out; + context->next_out = (Bytef *)dest.data() + eos; + context->avail_out = (uInt)dest.size() - eos; + + result = deflate(context, Z_FINISH); + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + break; + } + + getDict(context, false, dictionary, dictionarySize); + + u32 toSize = context->total_out; + mem_copy(dest.data() + sizeOffset, (char *)&toSize, sizeof(u32)); + + auto eos = bos + context->total_out; + dest.resize(eos); + + compressContexts.putContext(level, context); + + if (result != Z_STREAM_END) + return 0; + + return toSize; +} + +UncompressOrdered::UncompressOrdered() +{ + memset(dictionary, 0, dictionarySize); +} + +UncompressOrdered::~UncompressOrdered() +{ +} + + +size_t UncompressOrdered::execute(char *dest_, size_t destLen_, const char *source_, size_t sourceLen_) +{ + const Bytef *source = (const Bytef *)source_; + uLongf sourceLen = sourceLen_; + Bytef *dest = (Bytef *)dest_; + uLongf destLen = destLen_; + + auto context = (z_streamp)decompressContexts.getContext(); + + if (!clearStream(context, true)) + return 0; + + context->next_in = (Bytef *)source; + context->avail_in = (uInt)sourceLen; + + context->next_out = dest; + context->avail_out = (uInt)destLen; + + auto result = inflate(context, Z_FINISH); + + if (result == Z_NEED_DICT) + { + if (!setDict(context, true, dictionary, dictionarySize)) + return 0; + + result = inflate(context, Z_FINISH); + } + + auto toSize = context->total_out; + + if (result == Z_STREAM_END) + getDict(context, true, dictionary, dictionarySize); + + decompressContexts.putContext(context); + + if (result != Z_STREAM_END) + return 0; + + return toSize; +} + +size_t UncompressOrdered::execute(const containers::MemorySegment &dest, const containers::MemorySegment &source) +{ + u32 size = 0; + mem_copy((char *)&size, source.data(), sizeof(u32)); + auto dataOffset = sizeof(u32); + + auto context = (z_streamp)decompressContexts.getContext(); + + if (!clearStream(context, true)) + return 0; + + context->next_in = (Bytef *)source.data() + dataOffset; + context->avail_in = size; + + int result = 0; + + while(1) + { + context->next_out = (Bytef *)dest.data() + context->total_out; + context->avail_out = (uInt)dest.size() - context->total_out; + + result = inflate(context, Z_FINISH); + + if (result == Z_NEED_DICT) + { + if (!setDict(context, true, dictionary, dictionarySize)) + return 0; + + result = inflate(context, Z_FINISH); + } + + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + break; + } + + auto toSize = context->total_out; + debug_assert(toSize == dest.size()); + + getDict(context, true, dictionary, dictionarySize); + + decompressContexts.putContext(context); + + if (result != Z_STREAM_END) + return 0; + + return size + sizeof(32); +} + +size_t UncompressOrdered::execute(Vector &dest, const containers::MemorySegment &source) +{ + u32 size = 0; + mem_copy((char *)&size, source.data(), sizeof(u32)); + auto dataOffset = sizeof(u32); + + auto context = (z_streamp)decompressContexts.getContext(); + + if (!clearStream(context, true)) + return 0; + + context->next_in = (Bytef *)source.data() + dataOffset; + context->avail_in = size; + + auto offset = 0; + int result = 0; + + while(1) + { + auto newSize = std::max(offset + context->total_out + 2048, (size_t)16384); + dest.resize(newSize); + + context->next_out = (Bytef *)dest.data() + offset + context->total_out; + context->avail_out = (uInt)dest.size() - offset - context->total_out; + + result = inflate(context, Z_FINISH); + + if (result == Z_NEED_DICT) + { + if (!setDict(context, true, dictionary, DictionarySize)) + return 0; + + result = inflate(context, Z_FINISH); + } + + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + break; + } + + auto toSize = context->total_out; + dest.resize(toSize); + + getDict(context, true, dictionary, dictionarySize); + + decompressContexts.putContext(context); + + if (result != Z_STREAM_END) + return 0; + + return size + sizeof(32); +} + +// --------- + +CompressStream::CompressStream() +{ + memset(dictionary, 0, dictionarySize); +} + +size_t CompressStream::with(Vector &v, Function &&f) +{ + auto context = (z_streamp)decompressContexts.getContext(); + + if (!clearStream(context, true)) + return 0; + + if (!setDict(context, true, dictionary, dictionarySize)) + return 0; + + i = (I *)context; + v_ = &v; + p = v.size(); + + auto initialSize = v.size(); + f(*this); + + v.resize(p); + auto finalSize = v.size(); + + decompressContexts.putContext(context); + + return finalSize - initialSize; +} + +size_t CompressStream::write(const containers::MemorySegment &s) +{ + auto &v = *v_; + + auto context = (z_streamp)i; + context->next_in = (Bytef *)s.data(); + context->avail_in = (uInt)s.size(); + + // beginning of stream + auto bos = p; + + int result = 0; + + while(1) + { + auto newSize = std::max(bos + context->total_out + 2048, (size_t)16384); + v.resize(newSize); + + context->next_out = (Bytef *)v.data() + bos + context->total_out; + context->avail_out = (uInt)v.size() - bos - context->total_out; + + result = deflate(context, Z_FINISH); + + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + break; + } + + auto toSize = context->total_out; + p += toSize; + + return toSize; +} + +DecompressStream::DecompressStream() +{ + memset(dictionary, 0, dictionarySize); +} + +size_t DecompressStream::with(const containers::MemorySegment &v, Function &&f) +{ + auto context = (z_streamp)decompressContexts.getContext(); + + if (!clearStream(context, true)) + return 0; + + if (!setDict(context, true, dictionary, dictionarySize)) + return 0; + + i = (I *)context; + + context->next_in = (Bytef *)v.data(); + context->avail_in = (uInt)v.size(); + + f(*this); + + auto sizeRead = context->total_in; + decompressContexts.putContext(context); + + return sizeRead; +} + +size_t DecompressStream::read(const containers::MemorySegment &s) +{ + // beginning of stream + int result = 0; + + auto context = (z_streamp)i; + context->next_out = (Bytef *)s.data(); + context->avail_out = (uInt)s.size(); + + while(1) + { + result = inflate(context, Z_FINISH); + + if (result == Z_NEED_DICT) + { + if (!setDict(context, true, dictionary, DictionarySize)) + return 0; + + result = inflate(context, Z_FINISH); + } + + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + break; + } + + auto toSize = context->total_out; + + return toSize; +} + +} // namespace diff --git a/tjp/core/compression/_delete/zlib.hpp b/tjp/core/compression/_delete/zlib.hpp new file mode 100644 index 0000000..7649975 --- /dev/null +++ b/tjp/core/compression/_delete/zlib.hpp @@ -0,0 +1,88 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "zlib.h" +#include +#include +#include + +namespace tjp::core::compression::zlib { + +constexpr size_t DictionarySize = 256; +using Dictionary = char[DictionarySize]; + +struct CompressUnordered +{ + static constexpr auto dictionarySize = DictionarySize; + + CompressUnordered(int level); + int level; + + size_t execute(char *dest, size_t destLen, const char *source, size_t sourceLen); +}; + +struct UncompressUnordered +{ + size_t execute(char *dest, size_t destLen, const char *source, size_t sourceLen); +} ; + +struct CompressOrdered { + + static constexpr auto dictionarySize = DictionarySize; + + CompressOrdered(int level); + ~CompressOrdered(); + + int level; + Dictionary dictionary; + + size_t execute(char *dest, size_t destLen, const char *source, size_t sourceLen); + size_t execute(Vector &, const containers::MemorySegment &); +}; + +struct UncompressOrdered +{ + static constexpr auto dictionarySize = DictionarySize; + + UncompressOrdered(); + ~UncompressOrdered(); + + Dictionary dictionary; + + size_t execute(char *dest, size_t destLen, const char *source, size_t sourceLen); + size_t execute(const containers::MemorySegment &, const containers::MemorySegment &); + size_t execute(Vector &, const containers::MemorySegment &); +} ; + +struct CompressStream +{ + static constexpr auto dictionarySize = DictionarySize; + Dictionary dictionary; + + using I = void; + I *i; + Vector *v_; + size_t p; + + CompressStream(); + + size_t with(Vector &, Function &&f); + size_t write(const containers::MemorySegment &); +} ; + +struct DecompressStream +{ + static constexpr auto dictionarySize = DictionarySize; + Dictionary dictionary; + + using I = void; + I *i; + + DecompressStream(); + + size_t with(const containers::MemorySegment &, Function &&f); + size_t read(const containers::MemorySegment &); +} ; + +} // namespace diff --git a/tjp/core/compression/_tests/zlib.cpp b/tjp/core/compression/_tests/zlib.cpp new file mode 100644 index 0000000..776ef8d --- /dev/null +++ b/tjp/core/compression/_tests/zlib.cpp @@ -0,0 +1,282 @@ +// TJP COPYRIGHT HEADER + +#include +#include + +#include +#include + +namespace tjp::core::compression::zlib { +namespace { + +SCENARIO("tjp::core::compression::zlib") +{ + GIVEN("read and write") + { + CompressStream out(9); + Vector packet; + + struct Values { + s16 a; + Vector b; + u32 c; + Vector d; + }; + + Values expected { + .a = 42, + .b = { 1, 2, 3, 4, 5 }, + .c = 13 + }; + + auto largeNumber = 235324; + expected.d.assign(largeNumber, 12345); + + out.with(packet, [&](auto &s) { + s.value(expected.a); + s.vector(expected.b); + s.value(expected.c); + s.vector(expected.d); + }); + + Values expectedB { + .a = 124, + .b = { 5, 2, 3, 4, 5 }, + .c = 23 + }; + expectedB.d.assign(largeNumber, 6666); + + out.with(packet, [&](auto &s) { + s.value(expectedB.a); + s.vector(expectedB.b); + s.value(expectedB.c); + s.vector(expectedB.d); + }); + + Values real; + real.b.resize(5); + real.d.resize(largeNumber); + + DecompressStream in; + + auto segment = containers::memory_segment_vector(packet); + + in.with(segment, [&](auto &s) { + s.value(real.a); + s.vector(real.b); + s.value(real.c); + s.vector(real.d); + }); + + REQUIRE(real.a == expected.a); + REQUIRE(real.b == expected.b); + REQUIRE(real.c == expected.c); + + auto d_hash = hash_of(real.d); + auto d_expectedHash = hash_of(expected.d); + REQUIRE(d_hash == d_expectedHash); + + + Values realB; + realB.b.resize(5); + realB.d.resize(largeNumber); + + in.with(segment, [&](auto &s) { + s.value(realB.a); + s.vector(realB.b); + s.value(realB.c); + s.vector(realB.d); + }); + + REQUIRE(realB.a == expectedB.a); + REQUIRE(realB.b == expectedB.b); + REQUIRE(realB.c == expectedB.c); + + auto bd_hash = hash_of(realB.d); + auto bd_expectedHash = hash_of(expectedB.d); + REQUIRE(bd_hash == bd_expectedHash); + } + + GIVEN("back-to-back streams") + { + CompressStream out(9); + Vector packet; + + Vector expectedA; + Vector expectedB; + expectedA.assign(32768, 1111); + expectedB.assign(32768, 2222); + + auto firstCompressedSize = out.with(packet, [&](auto &s) { + s.vector(expectedA); + }); + auto secondCompressedSize = out.with(packet, [&](auto &s) { + s.vector(expectedB); + }); + + REQUIRE(firstCompressedSize > 0); + REQUIRE(secondCompressedSize > 0); + + DecompressStream in; + auto segment = containers::memory_segment_vector(packet); + + Vector realA; + realA.resize(expectedA.size()); + in.with(segment, [&](auto &s) { + s.vector(realA); + }); + + auto a_hash = hash_of(realA); + auto a_expectedHash = hash_of(expectedA); + REQUIRE(a_hash == a_expectedHash); + REQUIRE(segment.size() == secondCompressedSize); + + Vector realB; + realB.resize(expectedB.size()); + in.with(segment, [&](auto &s) { + s.vector(realB); + }); + + auto b_hash = hash_of(realB); + auto b_expectedHash = hash_of(expectedB); + REQUIRE(b_hash == b_expectedHash); + REQUIRE(segment.size() == 0); + } + + GIVEN("gzip read and write") + { + CompressStream out(9, Format::Gzip); + Vector packet; + + struct Values { + s16 a; + Vector b; + u32 c; + Vector d; + }; + + Values expected { + .a = 17, + .b = { 9, 8, 7, 6, 5 }, + .c = 99 + }; + + auto largeNumber = 32768; + expected.d.assign(largeNumber, 4321); + + Values expectedB { + .a = 18, + .b = { 1, 3, 5, 7, 9 }, + .c = 100 + }; + expectedB.d.assign(largeNumber, 8765); + + out.with(packet, [&](auto &s) { + s.value(expected.a); + s.vector(expected.b); + s.value(expected.c); + s.vector(expected.d); + }); + + out.with(packet, [&](auto &s) { + s.value(expectedB.a); + s.vector(expectedB.b); + s.value(expectedB.c); + s.vector(expectedB.d); + }); + + REQUIRE(packet.size() >= 2); + REQUIRE((unsigned char)packet[0] == 0x1f); + REQUIRE((unsigned char)packet[1] == 0x8b); + + Values real; + real.b.resize(expected.b.size()); + real.d.resize(largeNumber); + + DecompressStream in; + auto segment = containers::memory_segment_vector(packet); + in.with(segment, [&](auto &s) { + s.value(real.a); + s.vector(real.b); + s.value(real.c); + s.vector(real.d); + }); + + REQUIRE(real.a == expected.a); + REQUIRE(real.b == expected.b); + REQUIRE(real.c == expected.c); + auto d_hash = hash_of(real.d); + auto d_expectedHash = hash_of(expected.d); + REQUIRE(d_hash == d_expectedHash); + + Values realB; + realB.b.resize(expectedB.b.size()); + realB.d.resize(largeNumber); + in.with(segment, [&](auto &s) { + s.value(realB.a); + s.vector(realB.b); + s.value(realB.c); + s.vector(realB.d); + }); + + REQUIRE(realB.a == expectedB.a); + REQUIRE(realB.b == expectedB.b); + REQUIRE(realB.c == expectedB.c); + auto bd_hash = hash_of(realB.d); + auto bd_expectedHash = hash_of(expectedB.d); + REQUIRE(bd_hash == bd_expectedHash); + REQUIRE(segment.size() == 0); + } + + GIVEN("gzip back-to-back streams") + { + CompressStream out(9, Format::Gzip); + Vector packet; + + Vector expectedA; + Vector expectedB; + expectedA.assign(32768, 1111); + expectedB.assign(32768, 2222); + + auto firstCompressedSize = out.with(packet, [&](auto &s) { + s.vector(expectedA); + }); + auto secondCompressedSize = out.with(packet, [&](auto &s) { + s.vector(expectedB); + }); + + REQUIRE(firstCompressedSize > 0); + REQUIRE(secondCompressedSize > 0); + REQUIRE(packet.size() >= 2); + REQUIRE((unsigned char)packet[0] == 0x1f); + REQUIRE((unsigned char)packet[1] == 0x8b); + + DecompressStream in; + auto segment = containers::memory_segment_vector(packet); + + Vector realA; + realA.resize(expectedA.size()); + in.with(segment, [&](auto &s) { + s.vector(realA); + }); + + auto a_hash = hash_of(realA); + auto a_expectedHash = hash_of(expectedA); + REQUIRE(a_hash == a_expectedHash); + REQUIRE(segment.size() == secondCompressedSize); + + Vector realB; + realB.resize(expectedB.size()); + in.with(segment, [&](auto &s) { + s.vector(realB); + }); + + auto b_hash = hash_of(realB); + auto b_expectedHash = hash_of(expectedB); + REQUIRE(b_hash == b_expectedHash); + REQUIRE(segment.size() == 0); + } +} + +} // namespace +} // namespace diff --git a/tjp/core/compression/_tests/zstd.cpp b/tjp/core/compression/_tests/zstd.cpp new file mode 100644 index 0000000..ec5032e --- /dev/null +++ b/tjp/core/compression/_tests/zstd.cpp @@ -0,0 +1,148 @@ +// TJP COPYRIGHT HEADER + +#include +#include + +#include +#include + +namespace tjp::core::compression::zstd { +namespace { + +SCENARIO("tjp::core::compression::zstd") +{ + GIVEN("read and write") + { + CompressStream out(9); + Vector packet; + + struct Values { + s16 a; + Vector b; + u32 c; + Vector d; + }; + + Values expected { + .a = 42, + .b = { 1, 2, 3, 4, 5 }, + .c = 13 + }; + + auto largeNumber = 235324; + expected.d.assign(largeNumber, 12345); + + out.with(packet, [&](auto &s) { + s.value(expected.a); + s.vector(expected.b); + s.value(expected.c); + s.vector(expected.d); + }); + + Values expectedB { + .a = 124, + .b = { 5, 2, 3, 4, 5 }, + .c = 23 + }; + expectedB.d.assign(largeNumber, 6666); + + out.with(packet, [&](auto &s) { + s.value(expectedB.a); + s.vector(expectedB.b); + s.value(expectedB.c); + s.vector(expectedB.d); + }); + + Values real; + real.b.resize(5); + real.d.resize(largeNumber); + + DecompressStream in; + + auto segment = containers::memory_segment_vector(packet); + + in.with(segment, [&](auto &s) { + s.value(real.a); + s.vector(real.b); + s.value(real.c); + s.vector(real.d); + }); + + REQUIRE(real.a == expected.a); + REQUIRE(real.b == expected.b); + REQUIRE(real.c == expected.c); + + auto d_hash = hash_of(real.d); + auto d_expectedHash = hash_of(expected.d); + REQUIRE(d_hash == d_expectedHash); + + + Values realB; + realB.b.resize(5); + realB.d.resize(largeNumber); + + in.with(segment, [&](auto &s) { + s.value(realB.a); + s.vector(realB.b); + s.value(realB.c); + s.vector(realB.d); + }); + + REQUIRE(realB.a == expectedB.a); + REQUIRE(realB.b == expectedB.b); + REQUIRE(realB.c == expectedB.c); + + auto bd_hash = hash_of(realB.d); + auto bd_expectedHash = hash_of(expectedB.d); + REQUIRE(bd_hash == bd_expectedHash); + } + + GIVEN("back-to-back streams") + { + CompressStream out(9); + Vector packet; + + Vector expectedA; + Vector expectedB; + expectedA.assign(32768, 1111); + expectedB.assign(32768, 2222); + + auto firstCompressedSize = out.with(packet, [&](auto &s) { + s.vector(expectedA); + }); + auto secondCompressedSize = out.with(packet, [&](auto &s) { + s.vector(expectedB); + }); + + REQUIRE(firstCompressedSize > 0); + REQUIRE(secondCompressedSize > 0); + + DecompressStream in; + auto segment = containers::memory_segment_vector(packet); + + Vector realA; + realA.resize(expectedA.size()); + in.with(segment, [&](auto &s) { + s.vector(realA); + }); + + auto a_hash = hash_of(realA); + auto a_expectedHash = hash_of(expectedA); + REQUIRE(a_hash == a_expectedHash); + REQUIRE(segment.size() == secondCompressedSize); + + Vector realB; + realB.resize(expectedB.size()); + in.with(segment, [&](auto &s) { + s.vector(realB); + }); + + auto b_hash = hash_of(realB); + auto b_expectedHash = hash_of(expectedB); + REQUIRE(b_hash == b_expectedHash); + REQUIRE(segment.size() == 0); + } +} + +} // namespace +} // namespace diff --git a/tjp/core/compression/remote-run b/tjp/core/compression/remote-run new file mode 100755 index 0000000..238f178 --- /dev/null +++ b/tjp/core/compression/remote-run @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +set -euo pipefail + +usage() { + cat <<'EOF' +Usage: + ./remote-run + +Environment variables: + REMOTE_HOST Default: host.docker.internal + REMOTE_PORT Default: 6666 + REMOTE_TOKEN Optional shared token +EOF +} + +if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then + usage + exit 0 +fi + +if [[ $# -lt 1 ]]; then + usage + exit 2 +fi + +cmd="$1" +host="${REMOTE_HOST:-host.docker.internal}" +port="${REMOTE_PORT:-6666}" +token="${REMOTE_TOKEN:-}" + +url="http://${host}:${port}/run/${cmd}" + +curl_args=(--silent --show-error --no-buffer "$url") +if [[ -n "$token" ]]; then + curl_args=(-H "X-Remote-Token: ${token}" "${curl_args[@]}") +fi + +exec curl "${curl_args[@]}" diff --git a/tjp/core/compression/zlib.cpp b/tjp/core/compression/zlib.cpp new file mode 100644 index 0000000..30fc5fe --- /dev/null +++ b/tjp/core/compression/zlib.cpp @@ -0,0 +1,525 @@ +// TJP COPYRIGHT HEADER + +#include "zlib.hpp" + +#include +#include +#include +#include + +#include + +namespace tjp::core::compression::zlib { + +enum class ContextType { + Compress, + Decompress +} ; + +namespace { + +constexpr int ZlibWindowBits = 15; +constexpr int GzipWindowBits = ZlibWindowBits + 16; +constexpr int InflateAutoWindowBits = ZlibWindowBits + 32; + +int window_bits_for(Format format) +{ + switch (format) + { + case Format::Zlib: + return ZlibWindowBits; + case Format::Gzip: + return GzipWindowBits; + default: + return ZlibWindowBits; + } +} + +size_t compress_key(int level, Format format) +{ + return (size_t(level) << 2) | size_t(format); +} + +} // namespace + +struct CompressContexts { + Mutex mutex; + Map> contextsByKey; + + ~CompressContexts() + { + for (auto &[k, contexts] : contextsByKey) + for (auto *c : contexts) + free((z_streamp)c); + } + + void *getContext(int level, Format format) + { + auto key = compress_key(level, format); + { + auto lock = lock_of(mutex); + + auto &contexts = contextsByKey[key]; + if (!contexts.empty()) + { + auto *v = contexts.back(); + contexts.pop_back(); + return v; + } + } + + auto z = (z_streamp)calloc(1, sizeof(z_stream)); + auto result = deflateInit2( + z, + level, + Z_DEFLATED, + window_bits_for(format), + 8, + Z_DEFAULT_STRATEGY + ); + if (result != Z_OK) + { + free((void *)z); + return nullptr; + } + return z; + } + + void putContext(int level, Format format, void *v) + { + auto key = compress_key(level, format); + auto lock = lock_of(mutex); + contextsByKey[key].push_back(v); + } +} ; + +struct DecompressContexts +{ + Mutex mutex; + Vector contexts; + + ~DecompressContexts() + { + for (auto *c : contexts) + free((z_streamp)c); + } + + void *getContext() + { + { + auto lock = lock_of(mutex); + + if (!contexts.empty()) + { + auto *v = contexts.back(); + contexts.pop_back(); + return v; + } + } + + auto z = (z_streamp)calloc(1, sizeof(z_stream)); + auto result = inflateInit2(z, InflateAutoWindowBits); + if (result != Z_OK) + { + free((void *)z); + return nullptr; + } + return z; + } + + void putContext(void *v) + { + auto lock = lock_of(mutex); + contexts.push_back(v); + } +} ; + +CompressContexts compressContexts; +DecompressContexts decompressContexts; + +#ifdef TIMPREPSCIUS_ZLIB_USE_DICTIONARY + +Dictionary::Dictionary() +{ + memset(block, 0, size); +} + +bool setDict(void *p, ContextType type, const Dictionary &d) +{ + auto z = (z_streamp)p; + + int error = 0; + if (type == ContextType::Decompress) + error = inflateSetDictionary(z, (const Bytef *)d.block, (uInt)d.size); + else + error = deflateSetDictionary(z, (const Bytef *)d.block, (uInt)d.size); + + return error == Z_OK; +} + +bool getDict(void *p, ContextType type, Dictionary &d) +{ + auto z = (z_streamp)p; + + constexpr auto DictStoreSize = 32768; + char store[DictStoreSize]; + memset(store, 0, DictStoreSize); + auto size_ = (uInt)DictStoreSize; + + int error = 0; + if (type == ContextType::Decompress) + error = inflateGetDictionary(z, (Bytef *)&store[0], &size_); + else + error = deflateGetDictionary(z, (Bytef *)&store[0], &size_); + + // zlib seems to store from end to front + auto offset = + size_ > d.size ? + size_ - d.size : + 0; + + int firstIndex = -1, lastIndex = -1; + for (auto i=0; inext_out = (Bytef *)v.data() + p; + context->avail_out = uInt(newSize - p); + + auto result = deflate(context, Z_FINISH); + p = size_t(context->next_out - (Bytef *)v.data()); + + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + { + error_ = result; + break; + } + } + + getDict(context, ContextType::Compress, dictionary); + + p = size_t(context->next_out - (Bytef *)v.data()); + v.resize(p); + + return p - p_; +} + +size_t Compressor::write(const containers::MemorySegment &s) +{ + auto &v = *v_; + + auto context = (z_streamp)i; + + context->next_in = (Bytef *)s.data(); + context->avail_in = (uInt)s.size(); + + // beginning of stream + int result = 0; + size_t written = 0; + + while(1) + { + auto newSize = round_up_to_multiple(p + 1, sizeMultiples); + v.resize(newSize); + + context->next_out = (Bytef *)v.data() + p; + context->avail_out = uInt(newSize - p); + + result = deflate(context, Z_NO_FLUSH); + p = size_t(context->next_out - (Bytef *)v.data()); + written = s.size() - (size_t)context->avail_in; + + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + { + error_ = result; + break; + } + + if (context->avail_in == 0) + break; + } + + + return written; +} + +void Compressor::flush() +{ + auto &v = *v_; + + auto context = (z_streamp)i; + + context->next_in = nullptr; + context->avail_in = 0; + + // beginning of stream + int result = 0; + + while(1) + { + auto newSize = round_up_to_multiple(p + 1, sizeMultiples); + v.resize(newSize); + + context->next_out = (Bytef *)v.data() + p; + context->avail_out = uInt(newSize - p); + + result = deflate(context, Z_PARTIAL_FLUSH); + p = size_t(context->next_out - (Bytef *)v.data()); + + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + { + error_ = result; + break; + } + + if (context->avail_out > 0) + break; + } +} + +size_t Compressor::size() const +{ + return p; +} + +bool Compressor::error () const +{ + return error_; +} + +CompressStream::CompressStream(int level, Format format) : + compressor(level, format) +{ +} + +size_t CompressStream::with(V &v, F &&f) +{ + return compressor.with(v, std::move(f)); +} + +CompressPacket::CompressPacket(int level_, Format format_) : + level(level_), + format(format_) +{ + +} + +size_t CompressPacket::with(V &v, F &&f) +{ + Compressor compressor(level, format); + return compressor.with(v, std::move(f)); +} + +Decompressor::Decompressor() +{ +} + +size_t Decompressor::with(V &v, F &&f) +{ + error_ = 0; + + auto context = (z_streamp)decompressContexts.getContext(); + if (!context) + return 0; + + i = (I *)context; + v_ = &v; + + auto _ = ExecuteOnDestruct([&]() { + decompressContexts.putContext(context); + i = nullptr; + v_ = nullptr; + }); + + if (!clearStream(context, ContextType::Decompress)) + return 0; + + context->next_in = (Bytef *)v.data(); + context->avail_in = uInt(v.size()); + p = 0; + + f(*this); + + getDict(context, ContextType::Decompress, dictionary); + + auto written = context->total_out; + auto consumed = size_t(context->next_in - (Bytef *)v.data()); + + v.data_ = (char *)context->next_in; + if (consumed > v.size_) + v.size_ = 0; + else + v.size_ -= consumed; + return written; +} + +size_t Decompressor::read(const containers::MemorySegment &s) +{ + auto &v = *v_; + auto context = (z_streamp)i; + + context->next_out = (Bytef *)s.data(); + context->avail_out = (uInt)s.size(); + + int result = 0; + size_t written = 0; + + while(1) + { + result = inflate(context, Z_SYNC_FLUSH); + written = size_t(context->next_out - (Bytef *)s.data()); + p = size_t(context->next_in - (Bytef *)v.data()); + + if (result == Z_NEED_DICT) + { + if (!setDict(context, ContextType::Decompress, dictionary)) + return 0; + } + + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + { + error_ = result; + break; + } + + if (written == s.size()) + break; + } + + return written; +} + +bool Decompressor::eos() const +{ + return error() || p >= v_->size(); +} + +bool Decompressor::error () const +{ + return error_; +} + +DecompressStream::DecompressStream() +{ + +} + +size_t DecompressStream::with(V &v, F &&f) +{ + return decompressor.with(v, std::move(f)); +} + +DecompressPacket::DecompressPacket() +{ + +} + +size_t DecompressPacket::with(V &v, F &&f) +{ + Decompressor decompressor; + + return decompressor.with(v, std::move(f)); +} + + + +} // namespace diff --git a/tjp/core/compression/zlib.h b/tjp/core/compression/zlib.h new file mode 100644 index 0000000..660c159 --- /dev/null +++ b/tjp/core/compression/zlib.h @@ -0,0 +1,11 @@ +// TJP COPYRIGHT HEADER + +#pragma once + + +namespace tjp::core::compression::zlib { + +struct CompressStream; +struct DecompressStream; + +} // namespace diff --git a/tjp/core/compression/zlib.hpp b/tjp/core/compression/zlib.hpp new file mode 100644 index 0000000..3dd12d9 --- /dev/null +++ b/tjp/core/compression/zlib.hpp @@ -0,0 +1,158 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "zlib.h" +#include +#include +#include + +namespace tjp::core::compression::zlib { + +enum class Format +{ + Zlib, + Gzip +} ; + +//#define TIMPREPSCIUS_ZLIB_USE_DICTIONARY +#ifdef TIMPREPSCIUS_ZLIB_USE_DICTIONARY +struct Dictionary +{ + static constexpr auto size = 256; + char block[size]; + + Dictionary(); +} ; +#else +struct Dictionary {}; +#endif + + + +struct Compressor +{ + [[no_unique_address]] + Dictionary dictionary; + + static constexpr auto sizeMultiples = 512; + using V = Vector; + using F = Function; + + int level; + Format format; + using I = void; + I *i = nullptr; + V *v_ = nullptr; + size_t p = 0; + int error_ = 0; + + Compressor(int level, Format format = Format::Zlib); + + size_t size() const; + + size_t with(V &, F &&f); + size_t write(const containers::MemorySegment &); + void flush(); + + template + auto value(T &t) { + auto segment = containers::memory_segment_value(t); + return write(segment) == segment.size(); + } + + template + auto vector(T &t) { + auto segment = containers::memory_segment_vector(t); + return write(segment) == segment.size(); + } + + bool error() const; +} ; + +struct CompressStream +{ + using V = Compressor::V; + using F = Compressor::F; + + Compressor compressor; + + CompressStream(int level, Format format = Format::Zlib); + + size_t with(V &, F &&f); +} ; + +struct CompressPacket +{ + using V = Compressor::V; + using F = Compressor::F; + + int level; + Format format; + + CompressPacket(int level, Format format = Format::Zlib); + + size_t with(V &, F &&f); +} ; + + +struct Decompressor +{ + [[no_unique_address]] + Dictionary dictionary; + + using V = containers::MemorySegment; + using F = Function; + + using I = void; + I *i = nullptr; + V *v_ = nullptr; + size_t p = 0; + int error_ = 0; + + Decompressor(); + + bool eos() const; + + size_t with(V &, F &&f); + size_t read(const containers::MemorySegment &); + + template + auto value(T &t) { + auto segment = containers::memory_segment_value(t); + return read(segment) == segment.size(); + } + + template + auto vector(T &t) { + auto segment = containers::memory_segment_vector(t); + return read(segment) == segment.size(); + } + + bool error () const; +} ; + +struct DecompressStream +{ + using V = Decompressor::V; + using F = Decompressor::F; + + Decompressor decompressor; + + DecompressStream(); + + size_t with(V &, F &&f); +} ; + +struct DecompressPacket +{ + using V = Decompressor::V; + using F = Decompressor::F; + + DecompressPacket(); + + size_t with(V &, F &&f); +} ; + + +} // namespace diff --git a/tjp/core/compression/zstd.cpp b/tjp/core/compression/zstd.cpp new file mode 100644 index 0000000..fed6e4d --- /dev/null +++ b/tjp/core/compression/zstd.cpp @@ -0,0 +1,356 @@ +// TJP COPYRIGHT HEADER + +#include "zstd.hpp" + +#include +#include +#include +#include + +#include + +namespace tjp::core::compression::zstd { + +enum class ContextType { + Compress, + Decompress +} ; + +struct CompressContexts { + Mutex mutex; + Map> contextsByLevel; + + ~CompressContexts() + { + for (auto &[l, contexts] : contextsByLevel) + for (auto *c : contexts) + ZSTD_freeCStream((ZSTD_CStream *)c); + } + + void *getContext(int level) + { + { + auto lock = lock_of(mutex); + + auto &contexts = contextsByLevel[level]; + if (!contexts.empty()) + { + auto *v = contexts.back(); + contexts.pop_back(); + return v; + } + } + + auto *v = ZSTD_createCStream(); + ZSTD_initCStream(v, level); + return v; + } + + void putContext(int level, void *v) + { + auto lock = lock_of(mutex); + contextsByLevel[level].push_back(v); + } +} ; + +struct DecompressContexts +{ + Mutex mutex; + Vector contexts; + + ~DecompressContexts() + { + for (auto *c : contexts) + ZSTD_freeDStream((ZSTD_DStream *)c); + } + + void *getContext() + { + { + auto lock = lock_of(mutex); + + if (!contexts.empty()) + { + auto *v = contexts.back(); + contexts.pop_back(); + return v; + } + } + + void *v = ZSTD_createDStream(); + return v; + } + + void putContext(void *v) + { + auto lock = lock_of(mutex); + contexts.push_back(v); + } +} ; + +CompressContexts compressContexts; +DecompressContexts decompressContexts; + + +// -------------- + +bool clearStream(void *p, ContextType type) +{ + return true; +} + + +// -------------- + +static size_t round_up_to_multiple(size_t number, size_t multiple) { + if (multiple == 0) { + return number; // Avoid division by zero + } + auto remainder = number % multiple; + if (remainder == 0) { + return number; // Already a multiple + } + return number + multiple - remainder; +} + +Compressor::Compressor(int level_) : + level(level_) +{ +} + +size_t Compressor::with(V &v, F &&f) +{ + auto lock = lock_of(mutex); + auto context = (ZSTD_CStream *)compressContexts.getContext(level); + + auto _ = ExecuteOnDestruct([&]() { + compressContexts.putContext(level, context); + i = nullptr; + v_ = nullptr; + }); + + if (!clearStream(context, ContextType::Compress)) + return 0; + + i = (I *)context; + v_ = &v; + p = v.size(); + + auto p_ = p; + + f(*this); + + // finish the stream + while(1) + { + auto newSize = round_up_to_multiple(p + 1, sizeMultiples); + v.resize(newSize); + + ZSTD_outBuffer out; + out.dst = v.data(); + out.pos = p; + out.size = v.size(); + + auto result = ZSTD_endStream(context, &out); + p = out.pos; + + if (ZSTD_isError(result)) + { + error_ = result; + break; + } + + if (result == 0) + break; + } + + v.resize(p); + + return p - p_; +} + +size_t Compressor::write(const containers::MemorySegment &s) +{ + auto &v = *v_; + + auto context = (ZSTD_CStream *)i; + + ZSTD_inBuffer in; + in.size = s.size(); + in.src = s.data(); + in.pos = 0; + + // beginning of stream + size_t result = 0; + + while(1) + { + auto newSize = round_up_to_multiple(p + 1, sizeMultiples); + v.resize(newSize); + + ZSTD_outBuffer out; + out.dst = v.data(); + out.pos = p; + out.size = v.size(); + + result = ZSTD_compressStream(context, &out, &in); + p = out.pos; + + if (ZSTD_isError(result)) + { + error_ = result; + break; + } + + if (in.pos == in.size) + break; + } + + return in.pos; +} + +void Compressor::flush() +{ + auto &v = *v_; + + auto context = (ZSTD_CStream *)i; + + // beginning of stream + size_t result = 0; + + while(1) + { + auto newSize = round_up_to_multiple(p + 1, sizeMultiples); + v.resize(newSize); + + ZSTD_outBuffer out; + out.dst = v.data(); + out.pos = p; + out.size = v.size(); + + result = ZSTD_flushStream(context, &out); + p = out.pos; + + if (ZSTD_isError(result)) + { + error_ = result; + break; + } + + if (out.pos < out.size) + break; + } +} + +size_t Compressor::size() const +{ + return p; +} + +bool Compressor::error () const +{ + return error_; +} + +CompressPacket::CompressPacket(int level_) : + level(level_) +{ +} + +size_t CompressPacket::with(V &v, F &&f) +{ + Compressor compressor(level); + return compressor.with(v, std::move(f)); +} + + + +Decompressor::Decompressor() +{ +} + +size_t Decompressor::with(V &v, F &&f) +{ + auto lock = lock_of(mutex); + auto context = (ZSTD_DCtx *)decompressContexts.getContext(); + + i = (I *)context; + v_ = &v; + + auto _ = ExecuteOnDestruct([&]() { + decompressContexts.putContext(context); + i = nullptr; + v_ = nullptr; + }); + + if (!clearStream(context, ContextType::Decompress)) + return 0; + + p = 0; + f(*this); + + v.data_ += p; + v.size_ -= p; + + return p; +} + +size_t Decompressor::read(const containers::MemorySegment &s) +{ + auto &v = *v_; + auto context = (ZSTD_DCtx *)i; + + ZSTD_inBuffer in; + in.size = v.size(); + in.src = v.data(); + in.pos = p; + + ZSTD_outBuffer out; + out.dst = (void *)s.data(); + out.pos = 0; + out.size = s.size(); + + size_t result = 0; + + while(1) + { + result = ZSTD_decompressStream(context, &out, &in); + p = in.pos; + + if (ZSTD_isError(result)) + { + error_ = result; + break; + } + + if (in.pos == in.size) + break; + + if (out.pos == out.size) + break; + } + + return out.pos; +} + +bool Decompressor::eos() const +{ + return error() || p >= v_->size(); +} + +bool Decompressor::error () const +{ + return error_; +} + +DecompressPacket::DecompressPacket() +{ +} + +size_t DecompressPacket::with(V &v, F &&f) +{ + Decompressor decompressor; + return decompressor.with(v, std::move(f)); +} + +} // namespace diff --git a/tjp/core/compression/zstd.h b/tjp/core/compression/zstd.h new file mode 100644 index 0000000..86b8041 --- /dev/null +++ b/tjp/core/compression/zstd.h @@ -0,0 +1,14 @@ +// TJP COPYRIGHT HEADER + +#pragma once + + +namespace tjp::core::compression::zstd { + +struct CompressPacket; +struct DecompressPacket; + +using CompressStream = CompressPacket; +using DecompressStream = DecompressPacket; + +} // namespace diff --git a/tjp/core/compression/zstd.hpp b/tjp/core/compression/zstd.hpp new file mode 100644 index 0000000..531c33c --- /dev/null +++ b/tjp/core/compression/zstd.hpp @@ -0,0 +1,111 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "zstd.h" +#include +#include +#include +#include + +namespace tjp::core::compression::zstd { + +struct Compressor +{ + core::TestMutex mutex; + + static constexpr auto sizeMultiples = 512; + using V = Vector; + using I = void; + using F = Function; + + int level; + I *i = nullptr; + V *v_ = nullptr; + size_t p = 0; + size_t error_ = 0; + + Compressor(int level); + + size_t with(V &, F &&f); + + size_t size() const; + + size_t write(const containers::MemorySegment &); + void flush(); + + template + auto value(const T &t) { + auto segment = containers::memory_segment_value(t); + return write(segment) == segment.size(); + } + + template + auto vector(const T &t) { + auto segment = containers::memory_segment_vector(t); + return write(segment) == segment.size(); + } + + bool error () const; +} ; + +struct CompressPacket +{ + using V = Compressor::V; + using F = Compressor::F; + + int level; + + CompressPacket(int level); + + size_t with(V &, F &&f); +} ; + +struct Decompressor +{ + core::TestMutex mutex; + + using V = containers::MemorySegment; + using I = void; + using F = Function; + + I *i = nullptr; + V *v_ = nullptr; + size_t p = 0; + size_t error_ = 0; + + Decompressor(); + + bool eos() const; + + size_t with(V &, F &&f); + + size_t read(const containers::MemorySegment &); + + template + auto value(T &t) { + auto segment = containers::memory_segment_value(t); + return read(segment) == segment.size(); + } + + template + auto vector(T &t) { + auto segment = containers::memory_segment_vector(t); + return read(segment) == segment.size(); + } + + bool error () const; +}; + +struct DecompressPacket +{ + using V = Decompressor::V; + using F = Decompressor::F; + + DecompressPacket(); + + size_t with(V &, F &&); +} ; + + +} // namespace diff --git a/tjp/core/containers/Alias.hpp b/tjp/core/containers/Alias.hpp new file mode 100644 index 0000000..7db4129 --- /dev/null +++ b/tjp/core/containers/Alias.hpp @@ -0,0 +1,63 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +namespace tjp::core { + +template +struct Alias : T { + using Super = T; + using Self = Alias; + + template + Alias(Args&& ... args) : + Super(std::forward(args)...) + {} +} ; + +#define ALIAS_OF_HEADER(U, ...) \ + struct U; + +// https://stackoverflow.com/questions/8942912/how-to-pass-multi-argument-templates-to-macros +#define ALIAS_OF(U, ...) \ + struct U : __VA_ARGS__ { \ + using Super = __VA_ARGS__; \ + using Self = U; \ + template \ + U(Args&& ... args) : \ + Super(std::forward(args)...) \ + {} \ + } \ + +#define ALIAS_OF_WITH_SIMPLE_ASSIGNMENT(U, ...) \ + struct U : __VA_ARGS__ { \ + using Super = __VA_ARGS__; \ + using Self = U; \ + template \ + U(Args&& ... args) : \ + Super(std::forward(args)...) \ + {} \ + \ + template, U> && !std::is_same_v, Super>>> \ + U &operator =(V &&u) = delete; \ + \ + U &operator =(const Super &u) { Super::operator=(u); return *this; } \ + U &operator =(Super &&u) { Super::operator=(std::move(u)); return *this; } \ + } \ + +// U &operator =(const U &u) { Super::operator=(u); return *this; } \ + +#define ALIAS_OF_WITH_TEMPLATE_ASSIGNMENT(U, ...) \ + struct U : __VA_ARGS__ { \ + using Super = __VA_ARGS__; \ + using Self = U; \ + template \ + U(Args&& ... args) : \ + Super(std::forward(args)...) \ + {} \ + \ + template \ + auto &operator =(V &&t) { Super::operator=(std::forward(t)); return *this; } \ + } \ + +} // namespace diff --git a/tjp/core/containers/Changed.hpp b/tjp/core/containers/Changed.hpp new file mode 100644 index 0000000..6b83a3d --- /dev/null +++ b/tjp/core/containers/Changed.hpp @@ -0,0 +1,88 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +namespace tjp::core { + +template +struct Changed +{ + T value; + bool changed_ = true; + + using Self = Changed; + + Changed(T value_) : + value(value_) + { + } + + Changed(): + changed_(false) + { + } + + operator T &() + { + return value; + } + + void set(const T &rhs) + { + if (rhs == value) + return; + + value = rhs; + changed_ = true; + } + + Self &operator =(const T &rhs) + { + set(rhs); + return *this; + } + + void reset() + { + changed_ = false; + } + + void mark() + { + changed_ = true; + } + + bool use() + { + bool result = changed_; + changed_ = false; + return result; + } + + bool changed() const + { + return changed_; + } + + auto *operator ->() const + { + return &value; + } + + auto *operator ->() + { + return &value; + } + + auto &operator *() const + { + return value; + } + + auto &operator *() + { + return value; + } +} ; + +} // namespace diff --git a/tjp/core/containers/CircularBuffer.hpp b/tjp/core/containers/CircularBuffer.hpp new file mode 100644 index 0000000..6e4bd49 --- /dev/null +++ b/tjp/core/containers/CircularBuffer.hpp @@ -0,0 +1,198 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +#include +#include + +#include + +namespace tjp::core { + +template +struct CircularBuffer { + Vector buffer; + size_t begin_ = 0, end_ = 0; + size_t size_ = 0; + + size_t begin() const + { + return begin_; + } + + size_t end() const + { + return end_; + } + + void clear () + { + size_ = end_ = begin_ = 0; + } + + size_t size() const + { + return size_; + } + + bool empty() const + { + return size() == 0; + } + + size_t available() const + { + return buffer.size() - size(); + } + + struct Segment { + size_t to, from, size; + } ; + + const T *data () const + { + return buffer.data(); + } + + T *data () + { + return buffer.data(); + } + + Array segments(size_t from_, size_t dataSize) const + { + auto bufferSize = buffer.size(); + if (bufferSize == 0) + { + return { + Segment { 0, 0, 0 }, + Segment { 0, 0, 0 } + } ; + } + + auto from = from_ % bufferSize; + auto to_0 = from + dataSize; + auto to_1 = (size_t)0; + + if (to_0 > bufferSize) + { + to_1 = to_0 % bufferSize; + to_0 = to_0 - to_1; + } + + auto seg_0 = (to_0 - from); + auto seg_1 = (size_t)to_1; + + return { + Segment { from, 0, seg_0 }, + Segment { 0, seg_0, seg_1 } + } ; + } + + void grow(size_t dataSize) + { + if (dataSize == 0) + return; + + if (available() <= dataSize) + { + auto previousBufferSize = buffer.size(); + buffer.resize(previousBufferSize + dataSize); + + // if there is data at the beginning of the buffer, we need to + // put some of it at the end + if (end_ <= begin_) + { + auto previousEnd = end_; + + end_ = previousBufferSize; + + if (previousEnd > 0) + { + size_ -= previousEnd; + write(buffer.data(), previousEnd); + } + } + } + + size_ += dataSize; + } + + size_t write(const T *data, size_t dataSize) + { + auto size = size_; + grow(dataSize); + + auto segments_ = segments(begin_ + size, dataSize); + + for (auto &segment: segments_) + { + if (segment.size) + { + debug_assert(segment.to + segment.size <= buffer.size()); + + if (data) + std::memmove(buffer.data() + segment.to, data + segment.from, segment.size * sizeof(T)); + else + std::memset(buffer.data() + segment.to, 0, segment.size * sizeof(T)); + + end_ = segment.to + segment.size; + end_ = end_ % buffer.size(); + } + } + + return dataSize; + } + + size_t peek(T *data, size_t dataSize) const + { + if (dataSize == 0) + return dataSize; + + if (size_ < dataSize) + dataSize = size_; + + auto segments_ = segments(begin_, dataSize); + for (auto &segment: segments_) + { + if (segment.size) + { + std::memcpy(data + segment.from, buffer.data() + segment.to, segment.size * sizeof(T)); + } + } + + return dataSize; + } + + size_t read(T *data, size_t dataSize) + { + if (dataSize == 0) + return dataSize; + + if (size_ < dataSize) + dataSize = size_; + + peek(data, dataSize); + skip(dataSize); + + return dataSize; + } + + size_t skip(size_t dataSize) + { + if (dataSize == 0) + return dataSize; + + debug_assert(size_ >= dataSize); + + begin_ += dataSize; + begin_ = begin_ % buffer.size(); + size_ -= dataSize; + + return dataSize; + } +} ; + +} // namespace diff --git a/tjp/core/containers/InvalidatingCache.hpp b/tjp/core/containers/InvalidatingCache.hpp new file mode 100644 index 0000000..675de11 --- /dev/null +++ b/tjp/core/containers/InvalidatingCache.hpp @@ -0,0 +1,42 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +namespace tjp::core { + +template +struct InvalidatingCache +{ + Optional value; + + bool hasValue(time::Time now) + { + return value; + } + + template + T &cache(U &&u) + { + value.emplace(std::forward(u)); + return *value; + } + + template + T &cacheIf(time::Time now, F &&f) + { + if (!hasValue(now)) + return cache(f()); + + return *value; + } + + void invalidate() + { + value.reset(); + } +} ; + +} // namespace diff --git a/tjp/core/containers/InvalidatingTimedCache.hpp b/tjp/core/containers/InvalidatingTimedCache.hpp new file mode 100644 index 0000000..bd92630 --- /dev/null +++ b/tjp/core/containers/InvalidatingTimedCache.hpp @@ -0,0 +1,52 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +namespace tjp::core { + +template +struct InvalidatingTimedCache +{ + time::Duration invalidAfter = time::Duration::Zero; + + Optional value; + Optional invalidatedAt; + + bool hasValue(time::Time now) + { + if (!value) + return false; + + if (invalidatedAt) + return now < (*invalidatedAt + invalidAfter); + + return true; + } + + template + T &cache(U &&u) + { + value.emplace(std::forward(u)); + invalidatedAt.reset(); + return *value; + } + + template + T &cacheIf(time::Time now, F &&f) + { + if (!hasValue(now)) + return cache(f()); + + return *value; + } + + void invalidate(time::Time now) + { + invalidatedAt.emplace(now); + } +} ; + +} // namespace diff --git a/tjp/core/containers/SizedVector.hpp b/tjp/core/containers/SizedVector.hpp new file mode 100644 index 0000000..ee7cb40 --- /dev/null +++ b/tjp/core/containers/SizedVector.hpp @@ -0,0 +1,101 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +namespace tjp::core { + +template +struct SizedVector +{ + using V = Vector; + using iterator = typename V::iterator; + using const_iterator = typename V::const_iterator; + using value_type = typename V::value_type; + using reference = typename V::reference; + + V v; + size_t size_ = 0; + + auto reserved() const { return v.size(); } + + void reserve(size_t size) + { + if (size > v.size()) + v.resize(size); + } + + auto capacity () const { + return v.size(); + } + + auto resize (size_t size) + { + reserve(size); + return size_ = size; + } + + auto &front () { return v.front(); } + auto &front () const { return v.front(); } + + auto &back () { return at(size_-1); } + auto &back () const { return at(size_-1); } + + auto begin () { return v.begin(); } + auto end() { return v.begin() + size(); }; + auto begin () const { return v.begin(); } + auto end() const { return v.begin() + size(); }; + + auto &at(size_t i) + { + debug_assert(i < size_); + return v[i]; + } + + auto &at(size_t i) const + { + debug_assert(i < size_); + return v[i]; + } + + auto &operator[](size_t i) { return at(i); } + auto &operator[](size_t i) const { return at(i); } + + auto *data() { return v.data(); } + auto *data() const { return v.data(); } + + auto empty() const { return size_ == 0; } + auto size() const { return size_; } + + auto push_back(const T &t) + { + debug_assert(size_ < v.size()); + + if (size_ < v.size()) + { + size_++; + back() = t; + } + } + + auto pop_back(const T &t) + { + debug_assert(size_ > 0); + if (size_ > 0) + size_--; + } + + // can only be used to remove a portion at the end (as of now) + template + auto erase(A &&a, B &&b) + { + debug_assert(b == end()); + auto distance = std::distance(a, b); + size_ -= distance; + } + +} ; + +} // namespace diff --git a/tjp/core/containers/TimedCache.hpp b/tjp/core/containers/TimedCache.hpp new file mode 100644 index 0000000..a8a56ba --- /dev/null +++ b/tjp/core/containers/TimedCache.hpp @@ -0,0 +1,39 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp::core { + +template +struct TimedCache +{ + time::Duration expiresAfter; + + Optional value; + time::Time cachedAt = time::Time::Zero; + + bool hasValue(time::Time now) + { + return value && now < (cachtedAt + expiresAfter); + } + + template + T &cache(U &&u) + { + value.emplace(std::forward(u)); + return *value; + } + + template + T &cacheIf(time::Time now, F &&f) + { + if (!hasValue(now)) + return cache(f()); + + return *value; + } +} ; + +} // namespace diff --git a/tjp/core/containers/_tests/CircularBuffer.cpp b/tjp/core/containers/_tests/CircularBuffer.cpp new file mode 100644 index 0000000..a7ce077 --- /dev/null +++ b/tjp/core/containers/_tests/CircularBuffer.cpp @@ -0,0 +1,71 @@ +// TJP COPYRIGHT HEADER + +#include +#include + +namespace tjp::core { +namespace { + +SCENARIO("CircularBuffer") +{ + GIVEN("a circular buffer") + { + typedef int T; + CircularBuffer c; + + WHEN("adding in blocks") + { + constexpr int blockSize = 1024; + T removes[blockSize]; + T adds[blockSize]; + + for (auto i=0; i +#include + +namespace tjp { +namespace core { + +CSV::CSV(std::istream& str_, bool readHeader_) : + str(str_) +{ + if (readHeader_) + readHeader(); +} + +void CSV::readHeader () +{ + readNextRow(); + int i=0; + for (auto &s : m_data) + { + m_header[s] = i; + ++i; + } +} + +std::string const& CSV::operator[](std::size_t index) const +{ + return m_data[index]; +} + +std::string const& CSV::operator[](const std::string &index) const +{ + auto i = m_header.find(index); + return m_data[i->second]; +} + +std::size_t CSV::size() const +{ + return m_data.size(); +} + +bool CSV::readNextRow() +{ + std::string line; + std::getline(str, line); + if (!line.empty() && line.back() == '\r') + line.resize(line.size()-1); + + std::stringstream lineStream(line); + + std::string cell; + + m_data.clear(); + while(std::getline(lineStream, cell, ',')) + { + m_data.push_back(cell); + } + // This checks for a trailing comma with no data after it. + if (!lineStream && cell.empty()) + { + // If there was a trailing comma then add an empty element. + m_data.push_back(""); + } + + return str.good(); +} + +FastCSV::FastCSV(std::istream& str_, bool readHeader_) : + str(str_), + lineTokens(Tokenizer::Tokens("\n")), + csvTokens(Tokenizer::Tokens(",")) +{ + const auto bufferSize = 1024; + char buffer[bufferSize]; + while (str_) + { + str_.read(buffer, bufferSize); + data.insert(data.end(), buffer, buffer + str_.gcount()); + } + + tokenizer = Tokenizer(data.data(), data.size(), lineTokens); + + if (readHeader_) + readHeader(); +} + +void FastCSV::readHeader () +{ + readNextRow(); + int i=0; + for (auto &s : m_data) + { + m_header[s] = i; + ++i; + } +} + +FastCSV::Result FastCSV::operator[](std::size_t index) const +{ + return Result(m_data[index]); +} + +FastCSV::Segment FastCSV::segment(std::size_t index) const +{ + return m_data[index]; +}; + +std::size_t FastCSV::index(const Segment &index) const +{ + auto i = m_header.find(index); + if (i == m_header.end()) + return -1; + + return i->second; +} + +FastCSV::Result FastCSV::operator[](const Segment &index) const +{ + auto i = m_header.find(index); + return Result(m_data[i->second]); +} + +std::size_t FastCSV::size() const +{ + return m_data.size(); +} + +bool FastCSV::readNextRow() +{ + Segment line; + char token; + bool result = tokenizer.next(line, token); + + if (!result) + return false; + + if (!line.empty() && line.back() == '\r') + line.remove_suffix(1); + + Tokenizer lineTokenizer(line.data(), line.size(), csvTokens); + + Segment cell; + char cellToken; + m_data.clear(); + + while (lineTokenizer.next(cell, cellToken)) + { + m_data.push_back(cell); + } + // This checks for a trailing comma with no data after it. + if (cell.empty()) + { + // If there was a trailing comma then add an empty element. + m_data.push_back(Segment()); + } + + return true; +} + +} // namespace +} // namespace + diff --git a/tjp/core/csv/Csv.h b/tjp/core/csv/Csv.h new file mode 100644 index 0000000..9807b28 --- /dev/null +++ b/tjp/core/csv/Csv.h @@ -0,0 +1,208 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +// Adapted from +// https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c + +#include + +namespace tjp { +namespace core { + +class CSV +{ +public: + CSV(std::istream& str_, bool readHeader_ = true) : + str(str_) + { + if (readHeader_) + readHeader(); + } + + void readHeader () + { + readNextRow(); + int i=0; + for (auto &s : m_data) + { + m_header[s] = i; + ++i; + } + } + +public: + std::string const& operator[](std::size_t index) const + { + return m_data[index]; + } + + std::string const& operator[](const std::string &index) const + { + auto i = m_header.find(index); + return m_data[i->second]; + } + + std::size_t size() const + { + return m_data.size(); + } + + bool readNextRow() + { + std::string line; + std::getline(str, line); + if (!line.empty() && line.back() == '\r') + line.resize(line.size()-1); + + std::stringstream lineStream(line); + + std::string cell; + + m_data.clear(); + while(std::getline(lineStream, cell, ',')) + { + m_data.push_back(cell); + } + // This checks for a trailing comma with no data after it. + if (!lineStream && cell.empty()) + { + // If there was a trailing comma then add an empty element. + m_data.push_back(""); + } + + return str.good(); + } + +protected: + std::istream& str; + std::map m_header; + std::vector m_data; +}; + +class FastCSV +{ +protected: + typedef std::string_view Segment; + typedef std::string Result; + + std::vector data; + + typedef Tokenizer Tokenizer; + + Tokenizer::Tokens lineTokens; + Tokenizer::Tokens csvTokens; + Tokenizer tokenizer; + +public: + FastCSV(std::istream& str_, bool readHeader_ = true) : + str(str_), + lineTokens(Tokenizer::Tokens("\n")), + csvTokens(Tokenizer::Tokens(",")) + { + const auto bufferSize = 1024; + char buffer[bufferSize]; + while (str_) + { + str_.read(buffer, bufferSize); + data.insert(data.end(), buffer, buffer + str_.gcount()); + } + + tokenizer = Tokenizer(data.data(), data.size(), lineTokens); + + if (readHeader_) + readHeader(); + } + + void readHeader () + { + readNextRow(); + int i=0; + for (auto &s : m_data) + { + m_header[s] = i; + ++i; + } + } + +public: + Result operator[](std::size_t index) const + { + return Result(m_data[index]); + } + + Segment segment(std::size_t index) const + { + return m_data[index]; + }; + + std::size_t index(const Segment &index) const + { + auto i = m_header.find(index); + if (i == m_header.end()) + return -1; + + return i->second; + } + + Result operator[](const Segment &index) const + { + auto i = m_header.find(index); + return Result(m_data[i->second]); + } + + std::size_t size() const + { + return m_data.size(); + } + + bool readNextRow() + { + Segment line; + char token; + bool result = tokenizer.next(line, token); + + if (!result) + return false; + + if (!line.empty() && line.back() == '\r') + line.remove_suffix(1); + + Tokenizer lineTokenizer(line.data(), line.size(), csvTokens); + + Segment cell; + char cellToken; + m_data.clear(); + + while (lineTokenizer.next(cell, cellToken)) + { + m_data.push_back(cell); + } + // This checks for a trailing comma with no data after it. + if (cell.empty()) + { + // If there was a trailing comma then add an empty element. + m_data.push_back(Segment()); + } + + return true; + } + +protected: + std::istream& str; + std::map m_header; + std::vector m_data; +}; + +} // namespace +} // namespace + diff --git a/tjp/core/csv/Csv.hpp b/tjp/core/csv/Csv.hpp new file mode 100644 index 0000000..599fbe0 --- /dev/null +++ b/tjp/core/csv/Csv.hpp @@ -0,0 +1,82 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include +#include + +#include + +// Adapted from +// https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c + +#include + +namespace tjp { +namespace core { + +class CSV +{ +public: + CSV(std::istream& str_, bool readHeader_ = true); + + void readHeader (); + +public: + std::string const& operator[](std::size_t index) const; + + std::string const& operator[](const std::string &index) const; + + std::size_t size() const; + + bool readNextRow(); + +protected: + std::istream& str; + std::map m_header; + std::vector m_data; +}; + +class FastCSV +{ +protected: + typedef std::string_view Segment; + typedef std::string Result; + + std::vector data; + + typedef core::Tokenizer Tokenizer; + + Tokenizer::Tokens lineTokens; + Tokenizer::Tokens csvTokens; + Tokenizer tokenizer; + +public: + FastCSV(std::istream& str_, bool readHeader_ = true); + + void readHeader (); + +public: + Result operator[](std::size_t index) const; + + Segment segment(std::size_t index) const; + + std::size_t index(const Segment &index) const; + + Result operator[](const Segment &index) const; + + std::size_t size() const; + + bool readNextRow(); + +protected: + std::istream& str; + std::map m_header; + std::vector m_data; +}; + +} // namespace +} // namespace + diff --git a/tjp/core/csv/_tests/Csv.cpp b/tjp/core/csv/_tests/Csv.cpp new file mode 100644 index 0000000..cb9a84b --- /dev/null +++ b/tjp/core/csv/_tests/Csv.cpp @@ -0,0 +1,16 @@ +// TJP COPYRIGHT HEADER + +#include + +using namespace tjp::core; + +void compile() +{ + std::ifstream file("plop.csv"); + + CSV csv(file); + while(csv.readNextRow()) + { + std::cout << "4th Element(" << csv[3] << ")\n"; + } +} diff --git a/tjp/core/delegate/Delegate+Single.h b/tjp/core/delegate/Delegate+Single.h new file mode 100644 index 0000000..900657a --- /dev/null +++ b/tjp/core/delegate/Delegate+Single.h @@ -0,0 +1,25 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Delegate.h" + +namespace tjp::core::delegate_single { + +template +struct Delegator; + +template +using SpecializedDelegator = delegate::SpecializedDelegator; + +template +using Token = delegate::Token>; + +template +using WeakToken = delegate::WeakToken; + +template> +using TokenTyped = delegate::TokenTyped; + + +} // namespace diff --git a/tjp/core/delegate/Delegate+Single.hpp b/tjp/core/delegate/Delegate+Single.hpp new file mode 100644 index 0000000..f8aa86b --- /dev/null +++ b/tjp/core/delegate/Delegate+Single.hpp @@ -0,0 +1,54 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Delegate+Single.h" +#include "Delegate.hpp" +#include + +namespace tjp { +namespace core { +namespace delegate_single { + +template +struct Delegator +{ + using Delegate = Delegate_; + using TokenType = Token; + + Delegate *delegate = nullptr; + + virtual TokenType addDelegate(Delegate *delegate) = 0; + + virtual void insertDelegate(Delegate *delegate_) + { + debug_assert(delegate == nullptr); + delegate = delegate_; + } + + virtual void eraseDelegate(Delegate *delegate_) + { + debug_assert(delegate == delegate_); + delegate = nullptr; + } + + template + void eachDelegate(F &&f) + { + if (delegate) + f(delegate); + } +} ; + +template +using SpecializedDelegator = + delegate::SpecializedDelegator; + +template +using LockingDelegator = + delegate::LockingDelegatorOf>>; + + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/delegate/Delegate.h b/tjp/core/delegate/Delegate.h new file mode 100644 index 0000000..707a9fb --- /dev/null +++ b/tjp/core/delegate/Delegate.h @@ -0,0 +1,25 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp::core::delegate { + +template +struct Delegator; + +template +struct SpecializedDelegator; + +template> +struct Token; + +template +struct WeakToken; + +template> +struct TokenTyped; + + +} // namespace diff --git a/tjp/core/delegate/Delegate.hpp b/tjp/core/delegate/Delegate.hpp new file mode 100644 index 0000000..c50476c --- /dev/null +++ b/tjp/core/delegate/Delegate.hpp @@ -0,0 +1,342 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Delegate.h" +#include +#include +#include +#include +#include + +#include + +namespace tjp { +namespace core { +namespace delegate { + +template +auto strong_(T &&t) +{ + return strong(std::forward(t)); +} + +template +auto cast_to_strong(const StrongPtr &t) +{ + return std::static_pointer_cast(t); +} + +template +auto cast_to_strong(const WeakPtr &t) +{ + return cast_to_strong(strong(t)); +} + +template +struct Token +{ + using Delegate = Delegate_; + using DelegatorType = DelegatorType_; + + StrongPtr delegator; + + Token (const Token &) = delete; + + Token (Token &&rhs) : + delegator(std::move(rhs.delegator)) + {} + + Token() {} + + Token(const StrongPtr &delegator_) : + delegator(delegator_) + { + } + + ~Token () + { + debug_assert(delegator == nullptr); + } + + Token &operator =(Token &&token_) + { + delegator = std::move(token_.delegator); + return *this; + } + + void release(Delegate *delegate) + { + if (!delegator) + return; + + delegator->eraseDelegate(delegate); + delegator = nullptr; + } + + auto &get () { return delegator; } + auto &get () const { return delegator; } + + template + auto strong () + { + return cast_to_strong(delegator); + } + + template + auto strong () const + { + return cast_to_strong(delegator); + } + + template + auto ptr () + { + return static_cast(ptr_of(delegator)); + } + + template + auto ptr () const + { + return static_cast(ptr_of(delegator)); + } + + auto &operator ->() { return get(); } + auto &operator ->() const { return get(); } + + operator bool() const + { + return delegator != nullptr; + } +} ; + +template +struct WeakToken +{ + using TokenType = Token_; + using Delegate = typename TokenType::Delegate; + using DelegatorType = typename TokenType::DelegatorType; + + WeakPtr delegator; + + WeakToken() {} + + WeakToken (const WeakToken &) = delete; + + WeakToken (TokenType &&rhs) : + delegator(weak(rhs.delegator)) + { + rhs.delegator = nullptr; + } + + WeakToken (WeakToken &&rhs) : + delegator(std::move(rhs.delegator)) + { + } + + + WeakToken(const StrongPtr &delegator_) : + delegator(weak(delegator_)) + { + } + + ~WeakToken () + { + debug_assert(!strong_(delegator)); + } + + WeakToken &operator =(TokenType &&token_) + { + auto token = std::move(token_); + delegator = weak(token.delegator); + return *this; + } + + WeakToken &operator =(WeakToken &&token_) + { + delegator = std::move(token_.delegator); + return *this; + } + + void release(Delegate *delegate) + { + if (auto delegator_ = strong_(delegator)) + delegator_->eraseDelegate(delegate); + + delegator = {}; + } + + auto &get () { return delegator; } + auto &get () const { return delegator; } + + template + auto strong () + { + return cast_to_strong(delegator); + } + + template + auto strong () const + { + return cast_to_strong(delegator); + } +} ; + +template +struct TokenTyped : Token_ +{ + using Super = Token_; + + using As = As_; + using Into = Into_; + using Token = Token_; + + TokenTyped (const TokenTyped &) = delete; + + TokenTyped() {} + + TokenTyped(Token &&token_) : + Super(std::move(token_)) + { + } + + TokenTyped(TokenTyped &&token_) : + Super(std::move(token_)) + { + } + + TokenTyped &operator =(Token &&token_) + { + Super::operator=(std::move(token_)); + return *this; + } + + TokenTyped &operator =(TokenTyped &&token_) + { + Super::operator=(std::move(token_)); + return *this; + } + + auto strong () + { + return Super::template strong(); + } + + auto strong () const + { + return Super::template strong(); + } + + auto ptr () + { + return Super::template ptr(); + } + + auto ptr () const + { + return Super::template ptr(); + } + + auto operator ->() { return ptr(); } + const auto operator ->() const { return ptr(); } +} ; + +template +auto strong_token(TokenTyped &t) +{ + return t.strong(); +} + +template +struct Delegator +{ + using Delegate = Delegate_; + + using Delegates = SafeIteration>; + using TokenType = Token; + Delegates delegates; + + virtual TokenType addDelegate(Delegate *delegate) = 0; + + virtual void insertDelegate(Delegate *delegate) + { + delegates.push_back(delegate); + } + + virtual void eraseDelegate(Delegate *delegate) + { + list_erase_value_one(delegates, delegate); + } + + template + void eachDelegate(F &&f) + { + for (auto *delegate: delegates) + f(delegate); + } +} ; + +template +struct SpecializedDelegator : Super_ +{ + using Super = Super_; + using DelegatorType = Super; + using Delegate = typename Super::Delegate; + using TokenType = typename Super::TokenType; + using As = As_; + + static void eraser(const StrongPtr &delegator, Delegate *delegate) + { + delegator->eraseDelegate(delegate); + } + + TokenType addDelegate(Delegate *delegate) override + { + this->insertDelegate(delegate); + return TokenType( + std::static_pointer_cast( + strong_this( + static_cast(this) + ) + ) + ); + } +} ; + +template +struct LockingDelegatorOf : Super_ +{ + TestMutex m; + + using Super = Super_; + using Delegate = typename Super::Delegate; + using TokenType = typename Super::TokenType; + + void insertDelegate(Delegate *delegate) override + { + auto l = lock_of(m); + return Super::insertDelegate(delegate); + } + + void eraseDelegate(Delegate *delegate) override + { + auto l = lock_of(m); + return Super::eraseDelegate(delegate); + } + + template + void foreach(F &&f) + { + auto l = lock_of(m); + Super::foreach(std::forward(f)); + } +} ; + +template +using LockingDelegator = + SpecializedDelegator>>; + + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/delegate/_tests/Delegate+Single.cpp b/tjp/core/delegate/_tests/Delegate+Single.cpp new file mode 100644 index 0000000..a1742c1 --- /dev/null +++ b/tjp/core/delegate/_tests/Delegate+Single.cpp @@ -0,0 +1,141 @@ +// TJP COPYRIGHT HEADER + +#include + + +#include +#include +#include + +#include +#include + +#include + +namespace tjp::core::delegate_single { +namespace T1 { + +struct D; + +struct A { + A(const StrongPtr &d); + ~A(); + + using Token = Token; + Token token; + int i; +}; + +struct D : + SpecializedDelegator>, + StrongThis +{ + +} ; + +A::A(const StrongPtr &d) : + token(d->addDelegate(this)) +{ + i = 5; +} + +A::~A() +{ + token.release(this); +} + +SCENARIO("delegate_single") +{ +// xLogActivateStory("core::delegate"); + + GIVEN("structs") + { + auto d = strong(); + + A a(d); + + REQUIRE(a.i == 5); + REQUIRE(a.token.delegator == d); + } + + GIVEN("sizeof") + { + sLogTest("testing", logVar(sizeof(std::any)) << logVar(sizeof(A)) << logVar(sizeof(D)) << logVar(sizeof(A::Token))); + } +} + +} // namespace + +namespace T2 { + +struct A { + int a; +} ; + +struct B : A, StrongThis { + int b; +} ; + +void doMemory() +{ + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + + using F = Function; + + auto a = strong(); + F f = [a]() { + + }; + + f(); + + std::this_thread::sleep_for(std::chrono::milliseconds(10)); +} + +SCENARIO("delegate_single_memory") +{ +// xLogActivateStory("core::delegate"); + + GIVEN("structs") + { + auto b = strong(); + b->a = 1; + b->b = 2; + auto a = strong_ptr_cast(b); + + REQUIRE(a->a == 1); + auto ab = std::static_pointer_cast(a); + + REQUIRE(ab->b == 2); + + } + + GIVEN("memory") + { + doMemory(); + } + + GIVEN("sizeof") + { + std::this_thread::sleep_for(std::chrono::milliseconds(250)); + + using F = Function; + + auto a = strong(); + F f = [a]() {}; + auto g = []() {}; + + unused(a); + unused(f); + unused(g); + + std::this_thread::sleep_for(std::chrono::milliseconds(250)); + + sLogTest("testing", logVar(sizeof(std::any)) << logVar(sizeof(Token)) << logVar(sizeof(a)) << logVar(sizeof(F)) << logVar(sizeof(f))<< logVar(sizeof(g))); + } + + +} + +} // namespace +} // namespace diff --git a/tjp/core/delegate/_tests/Delegate.cpp b/tjp/core/delegate/_tests/Delegate.cpp new file mode 100644 index 0000000..5e89621 --- /dev/null +++ b/tjp/core/delegate/_tests/Delegate.cpp @@ -0,0 +1,201 @@ +// TJP COPYRIGHT HEADER + +#include + + +#include +#include +#include + +#include +#include + +#include + +namespace tjp::core::delegate { +namespace T1 { + +struct D; + +struct To +{ + To(); + + int i; +} ; + +struct A : To { + A(const StrongPtr &d); + A(){} + ~A(); + + using Token = Token; + Token token; +}; + +struct WA : To { + WA(const StrongPtr &d); + WA() {} + ~WA(); + + using Token = WeakToken>; + Token token; +}; + +struct D : + SpecializedDelegator>, + StrongThis +{ + +} ; + +To::To() +{ + i = 5; +} + +A::A(const StrongPtr &d) : + token(d->addDelegate(this)) +{ +} + +A::~A() +{ + token.release(this); +} + +WA::WA(const StrongPtr &d) : + token(d->addDelegate(this)) +{ +} + +WA::~WA() +{ + token.release(this); +} + +SCENARIO("delegate") +{ +// xLogActivateStory("core::delegate"); + + GIVEN("strong") + { + auto d = strong(); + + A a(d); + A b; + + + REQUIRE(a.i == 5); + REQUIRE(a.token.delegator == d); + + { + TokenTyped> ad(d->addDelegate(&b)); + REQUIRE(strong_token(ad) == d); + + ad.release(&b); + } + } + + GIVEN("weak") + { + auto d = strong(); + + WA a(d); + + REQUIRE(a.i == 5); + REQUIRE(strong(a.token.get()) == d); + } + + GIVEN("weak") + { + auto d = strong(); + + WA a(d); + + REQUIRE(a.i == 5); + REQUIRE(strong(a.token.get()) == d); + } + GIVEN("sizeof") + { + sLogTest("testing", logVar(sizeof(std::any)) << logVar(sizeof(A)) << logVar(sizeof(D)) << logVar(sizeof(A::Token))); + } +} + +} // namespace + +namespace T2 { + +struct A { + int a; +} ; + +struct B : A, StrongThis { + int b; +} ; + +void doMemory() +{ + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + + using F = Function; + + auto a = strong(); + F f = [a]() { + + }; + + f(); + + std::this_thread::sleep_for(std::chrono::milliseconds(10)); +} + +SCENARIO("delegate_memory") +{ +// xLogActivateStory("core::delegate"); + + GIVEN("structs") + { + auto b = strong(); + b->a = 1; + b->b = 2; + auto a = strong_ptr_cast(b); + + REQUIRE(a->a == 1); + auto ab = std::static_pointer_cast(a); + + REQUIRE(ab->b == 2); + + } + + GIVEN("memory") + { + doMemory(); + } + + GIVEN("sizeof") + { + std::this_thread::sleep_for(std::chrono::milliseconds(250)); + + using F = Function; + + auto a = strong(); + F f = [a]() {}; + + auto g = []() {}; + + unused(a); + unused(f); + unused(g); + + + std::this_thread::sleep_for(std::chrono::milliseconds(250)); + + sLogTest("testing", logVar(sizeof(std::any)) << logVar(sizeof(Token)) << logVar(sizeof(a)) << logVar(sizeof(F)) << logVar(sizeof(f))<< logVar(sizeof(g))); + } + + +} + +} // namespace +} // namespace diff --git a/tjp/core/endian/Endian.cpp b/tjp/core/endian/Endian.cpp new file mode 100755 index 0000000..35ca1c2 --- /dev/null +++ b/tjp/core/endian/Endian.cpp @@ -0,0 +1,16 @@ +// TJP COPYRIGHT HEADER + +#include "Endian.h" + +using namespace tjp::core; + +void endian::swap (char *buffer, int size) +{ + int i, j; + for (i = 0, j = size-1 ; i < size/2; ++i, --j) + { + char switcheroo = buffer[i]; + buffer[i] = buffer[j]; + buffer[j] = switcheroo; + } +} diff --git a/tjp/core/endian/Endian.h b/tjp/core/endian/Endian.h new file mode 100755 index 0000000..9b8e486 --- /dev/null +++ b/tjp/core/endian/Endian.h @@ -0,0 +1,72 @@ +// TJP COPYRIGHT HEADER + +#ifndef __Utilities_Endian_h__ +#define __Utilities_Endian_h__ + +namespace tjp { +namespace core { +namespace endian { + +/** + * Swaps a buffer that represents one variable + */ +void swap (char *, int size); + +/** + * Swaps the specified buffer if the processor is big endian. + */ +inline void toLittleEndian (char *buffer, int size) +{ + #ifdef __BIG_ENDIAN__ + swap(buffer, size); + #endif +} + +template +inline void toLittleEndian (T &t) +{ + #ifdef __BIG_ENDIAN__ + swap((char*)&t, sizeof(T)); + #endif + + return t; +} + +/** + * Swaps the specified buffer if the processor is little endian. + */ +inline void toBigEndian (char *buffer, int size) +{ + #ifndef __BIG_ENDIAN__ + swap(buffer, size); + #endif +} + +template +inline void toBigEndian (T &t) +{ + #ifndef __BIG_ENDIAN__ + swap((char*)&t, sizeof(T)); + #endif + + return t; +} + +/** + * Swaps a single variable. + */ +template +inline T &swap (T &t) +{ + #ifdef __BIG_ENDIAN__ + swap((char*)&t, sizeof(T)); + #endif + + return t; +} + +} // namespace endian +} // namespace core +} // namespace + +#endif diff --git a/tjp/core/events/Signal.cpp b/tjp/core/events/Signal.cpp new file mode 100644 index 0000000..45236f0 --- /dev/null +++ b/tjp/core/events/Signal.cpp @@ -0,0 +1,75 @@ +// TJP COPYRIGHT HEADER + +#include "Signal.hpp" + +#include +#include + +namespace tjp::core::signal { + +Listener *Signal::insert(Callback &&callback) +{ + auto lock = lock_of(mutex); + auto listener = strong(std::move(callback)); + auto r = listeners.emplace(listener); + debug_assert(r.second); + + return ptr_of(listener); +} + +void Signal::erase(Listener *listener) +{ + auto lock = lock_of(mutex); + + invalidated = true; + set_erase(listeners, listener); +} + +void Signal::signal() +{ + // this is correct, below, we reset to begin if it is the special case of the end + Listeners::iterator i = listeners.end(); + + invalidated = false; + + StrongPtr listener = nullptr; + do + { + mutex.lock(); + if (i == listeners.end()) + { + i = listeners.begin(); + } + else + { + if (invalidated) + { + invalidated = false; + i = listeners.upper_bound(listener); + } + else + { + ++i; + } + } + + if (i == listeners.end()) + break; + + listener = *i; + mutex.unlock(); + + (*listener)(); + } + while (true); + + mutex.unlock(); +} + +void Signal::clear() +{ + mutex.lock(); + listeners.clear(); +} + +} // namespace diff --git a/tjp/core/events/Signal.h b/tjp/core/events/Signal.h new file mode 100644 index 0000000..4eb1dd5 --- /dev/null +++ b/tjp/core/events/Signal.h @@ -0,0 +1,21 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp::core { +namespace signal { + +using Callback = Function; +using Listener = Callback; + +struct Signal; + + +} // namespace + +using signal::Signal; +using SignalListener = signal::Listener; + +} // namespace diff --git a/tjp/core/events/Signal.hpp b/tjp/core/events/Signal.hpp new file mode 100644 index 0000000..e49354d --- /dev/null +++ b/tjp/core/events/Signal.hpp @@ -0,0 +1,40 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Signal.h" +#include + +#include +#include + +namespace tjp::core { +namespace signal { + +using Callback = Function; +using Listener = Callback; + +struct Signal +{ + [[no_unique_address]] + TestMutex mutex; + + typedef SetSortBy, core::ByPtr> Listeners; + Listeners listeners; + + bool invalidated = false; + + Listener *insert(Callback &&callback); + void erase(Listener *listener); + + // todo change to emit + void signal(); + void clear(); +} ; + +} // namespace + +using signal::Signal; +using SignalListener = signal::Listener; + +} // namespace diff --git a/tjp/core/extensible/Extensible+CacheViaArray.h b/tjp/core/extensible/Extensible+CacheViaArray.h new file mode 100644 index 0000000..beb96c5 --- /dev/null +++ b/tjp/core/extensible/Extensible+CacheViaArray.h @@ -0,0 +1,188 @@ +// TJP COPYRIGHT HEADER + + +#pragma once + +#include "Extensible+Enumerated.h" + +#ifdef _DEBUG + void onExtensibleCacheNew_(void *); + void onExtensibleCache_(void *, size_t); + void onExtensibleCacheDelete_(void *, size_t); + void onExtensibleCacheClear_(void *, size_t); + + inline void onExtensibleCacheNew(void *t) { onExtensibleCacheNew_(t); } + inline void onExtensibleCache(void *t, size_t v) { onExtensibleCache_(t, v); } + inline void onExtensibleCacheDelete(void *t, size_t v) { onExtensibleCacheDelete_(t, v); } + inline void onExtensibleCacheClear(void *t, size_t v) { onExtensibleCacheDelete_(t, v); } +#else + inline void onExtensibleCacheNew(void *) {} + inline void onExtensibleCache(void *, size_t) {} + inline void onExtensibleCacheDelete(void *, size_t) {} + inline void onExtensibleCacheClear(void *, size_t) {} +#endif + +struct ExtensibleCacheViaArray +{ + [[no_unique_address]] + TestMutex mutex; + + typedef ExtensionInterface *ExtensionInterfacePtr; + + constexpr static size_t REDIRECT_TYPE_BITSIZE = 8; + constexpr static size_t REDIRECTS_PER_BYTE = 8 / REDIRECT_TYPE_BITSIZE; + + // leave one left over for the size variable + constexpr static size_t MAX_TYPE = 48 - REDIRECTS_PER_BYTE; + + constexpr static size_t REDIRECT_CACHE_BYTE_SIZE = MAX_TYPE / REDIRECTS_PER_BYTE; + constexpr static size_t + MAX_CACHE = (1 << REDIRECT_TYPE_BITSIZE) - 1 + - 1; // 0xF is NULL_REDIRECT + + constexpr static u8 CACHE_MASK = (1 << REDIRECT_TYPE_BITSIZE) - 1; + + constexpr static size_t NULL_REDIRECT = (1 << REDIRECT_TYPE_BITSIZE) - 1; + +// using Cache = InPlaceArray; + using Cache = StackArray; +// using Cache = Vector; + Cache cache; + + using Redirects = u8[REDIRECT_CACHE_BYTE_SIZE]; + Redirects redirect; + + ExtensibleCacheViaArray() + { + onExtensibleCacheNew(this); + + memset(redirect, CACHE_MASK, REDIRECT_CACHE_BYTE_SIZE); + } + + ~ExtensibleCacheViaArray() + { + debug_assert(cache.empty()); + } + + void clear() + { + onExtensibleCacheDelete(this, cache.size()); + + memset(redirect, 0xFF, REDIRECT_CACHE_BYTE_SIZE); + + for (auto p : cache) + delete p; + + cache.clear(); + } + + inline + size_t getRedirect(size_t index) + { + debug_assert(index < MAX_TYPE); + + if constexpr(REDIRECTS_PER_BYTE == 1) + { + return redirect[index] ; + } + else + { + auto redirectByte = index / REDIRECTS_PER_BYTE; + auto redirectNibble = index % REDIRECTS_PER_BYTE; + auto redirectShift = redirectNibble * REDIRECT_TYPE_BITSIZE; + + auto byte = redirect[redirectByte] >> redirectShift; + return byte & CACHE_MASK; + } + } + + inline + void setRedirect(size_t index, u8 value) + { + debug_assert(index < MAX_TYPE); + + if constexpr(REDIRECTS_PER_BYTE == 1) + { + redirect[index] = value; + } + else + { + auto redirectByte = index / REDIRECTS_PER_BYTE; + auto redirectNibble = index % REDIRECTS_PER_BYTE; + auto redirectShift = redirectNibble * REDIRECT_TYPE_BITSIZE; + + u8 writeBits = 0xF << redirectShift; + u8 keepBits = ~writeBits; + auto &byte = redirect[redirectByte]; + byte = (byte & keepBits) | (value << redirectShift); + } + } + + template + T *find() + { + auto lock = lock_of(mutex); + (void)lock; + + auto index = T::getExtensionIndex(); + debug_assert(index < MAX_TYPE); + + auto redirect = getRedirect(index); + if (redirect == NULL_REDIRECT) + return nullptr; + + debug_assert(redirect < MAX_CACHE); + + return static_cast(cache[redirect]); + } + + template + void insert(T *t) + { + auto lock = lock_of(mutex); + (void)lock; + + auto index = T::getExtensionIndex(); + debug_assert(index < MAX_TYPE); + + auto redirect = getRedirect(index); + if (redirect != NULL_REDIRECT) + { + debug_assert(cache[redirect] == nullptr); + cache[redirect] = t; + return; + } + + redirect = cache.size(); + cache.push_back(t); + + onExtensibleCache(this, cache.size()); + debug_assert(redirect < MAX_CACHE); + + setRedirect(index, redirect); + debug_assert(redirect < MAX_CACHE); + } + + template + void erase(T *t) + { + auto lock = lock_of(mutex); + (void)lock; + + auto index = T::getExtensionIndex(); + debug_assert(index < MAX_TYPE); + + auto redirect = getRedirect(index); + if (redirect == NULL_REDIRECT) + return ; + + debug_assert(redirect < MAX_CACHE); + cache[redirect] = nullptr; + } +} ; + + +using ExtensibleCache = ExtensibleCacheViaArray; + +template +using Extension = ExtensionEnumerated; diff --git a/tjp/core/extensible/Extensible+CacheViaArrayAndMap.h b/tjp/core/extensible/Extensible+CacheViaArrayAndMap.h new file mode 100644 index 0000000..0c44026 --- /dev/null +++ b/tjp/core/extensible/Extensible+CacheViaArrayAndMap.h @@ -0,0 +1,72 @@ +// TJP COPYRIGHT HEADER + + +#pragma once + +#include "Extensible+CacheViaMap.h" +#include "Extensible+Enumerated.h" + +struct ExtensibleCacheViaArrayAndMap +{ + typedef ExtensionInterface *ExtensionInterfacePtr; + + constexpr static size_t MAX_CACHE=64; + ExtensionInterfacePtr cache[MAX_CACHE]; + ExtensibleCacheViaMap map; + + ExtensibleCacheViaArrayAndMap() + { + memset(cache, 0, sizeof(ExtensionInterfacePtr) * MAX_CACHE); + } + + ~ExtensibleCacheViaArrayAndMap() + { + clear(); + } + + void clear() + { + auto *begin = &cache[0]; + auto *end = begin + MAX_CACHE; + for (auto *p=begin; p!=end; ++p) + { + if (*p) + { + delete *p; + *p = nullptr; + } + } + + map.clear(); + } + + template + T *find() + { + auto index = T::getExtensionIndex(); + if (index < MAX_CACHE) + return static_cast(cache[index]); + + return map.find(); + } + + template + void insert(T *t) + { + auto index = T::getExtensionIndex(); + if (index < MAX_CACHE) + { + cache[index] = t; + } + else + { + debug_assert(false); + map.insert(t); + } + } +} ; + +using ExtensibleCache = ExtensibleCacheViaArrayAndMap; + +template +using Extension = ExtensionEnumerated; diff --git a/tjp/core/extensible/Extensible+CacheViaMap.h b/tjp/core/extensible/Extensible+CacheViaMap.h new file mode 100644 index 0000000..6a83397 --- /dev/null +++ b/tjp/core/extensible/Extensible+CacheViaMap.h @@ -0,0 +1,51 @@ +// TJP COPYRIGHT HEADER + + +#pragma once + +#include "Extensible+TypeInfo.h" + +struct ExtensibleCacheViaMap +{ + typedef ExtensionInterface *ExtensionInterfacePtr; + std::map extensions; + + ~ExtensibleCacheViaMap() + { + clear(); + } + + void clear() + { + for (auto &kv : extensions) + delete kv.second; + + extensions.clear(); + } + + template + T *find() + { + const auto key = type_id(); + + auto i = extensions.find(key); + if (i == extensions.end()) + return nullptr; + + return static_cast(i->second); + } + + template + void insert(T *t) + { + const auto key = type_id(); + + debug_assert(extensions.find(key) == extensions.end()); + extensions.emplace(key, t); + } +} ; + +//using ExtensibleCache = ExtensibleCacheViaMap; +// +//template +//using Extension = ExtensionTypeInfo; diff --git a/tjp/core/extensible/Extensible+Enumerated.h b/tjp/core/extensible/Extensible+Enumerated.h new file mode 100644 index 0000000..a71093f --- /dev/null +++ b/tjp/core/extensible/Extensible+Enumerated.h @@ -0,0 +1,27 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +template +struct ExtensionEnumerated : ExtensionInterface +{ + static size_t extension_index; + + static size_t getExtensionIndex(); +} ; + +size_t getNextExtensionIndex(); + +template +size_t ExtensionEnumerated::extension_index = -1; + +template +size_t ExtensionEnumerated::getExtensionIndex() +{ + if (extension_index != -1) + return extension_index; + + extension_index = getNextExtensionIndex(); + return extension_index; +} + diff --git a/tjp/core/extensible/Extensible+TypeInfo.h b/tjp/core/extensible/Extensible+TypeInfo.h new file mode 100644 index 0000000..c001f01 --- /dev/null +++ b/tjp/core/extensible/Extensible+TypeInfo.h @@ -0,0 +1,7 @@ +// TJP COPYRIGHT HEADER + + +template +struct ExtensionTypeInfo : ExtensionInterface +{ +} ; diff --git a/tjp/core/extensible/Extensible.cpp b/tjp/core/extensible/Extensible.cpp new file mode 100644 index 0000000..eeca48e --- /dev/null +++ b/tjp/core/extensible/Extensible.cpp @@ -0,0 +1,67 @@ +// TJP COPYRIGHT HEADER + +#include "Extensible.h" +#include + +#include +#include + +namespace tjp::core { + +std::atomic nextExtensionIndex = 0; +size_t getNextExtensionIndex() +{ + sLogDebug("core::extensible", logVar(nextExtensionIndex)); + return nextExtensionIndex++; +} + +constexpr size_t MAX_CACHE_STATISTICS = 64; +static std::array numPerSizeCurrent = {0}; +static std::array numPerSizeTotals = {0}; + +void onExtensibleCache_(void *t, size_t size) +{ + if (size < MAX_CACHE_STATISTICS) + { + if (size > 0) + { + numPerSizeCurrent[size-1]--; + numPerSizeTotals[size-1]--; + } + + numPerSizeCurrent[size]++; + numPerSizeTotals[size]++; + } + + + sLogDebug("core::extensible", "C " << logVar(numPerSizeCurrent)); + sLogDebug("core::extensible", "T " << logVar(numPerSizeTotals)); +} + +void onExtensibleCacheNew_(void *t) +{ + onExtensibleCache_(t, 0); +} + +void onExtensibleCacheClear_(void *t, size_t size) +{ + if (size < MAX_CACHE_STATISTICS) + { + numPerSizeCurrent[size]--; + numPerSizeTotals[size]--; + + numPerSizeCurrent[0]++; + numPerSizeTotals[0]++; + } +} + +void onExtensibleCacheDelete_(void *t, size_t size) +{ + numPerSizeCurrent[size]--; + + sLogDebug("core::extensible", "C " << logVar(numPerSizeCurrent)); + sLogDebug("core::extensible", "T " << logVar(numPerSizeTotals)); +} + + +} // namespace diff --git a/tjp/core/extensible/Extensible.h b/tjp/core/extensible/Extensible.h new file mode 100644 index 0000000..1518df8 --- /dev/null +++ b/tjp/core/extensible/Extensible.h @@ -0,0 +1,269 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "ExtensibleWith.h" + +namespace tjp::core { + +struct ExtensionInterface +{ + ExtensionInterface (const ExtensionInterface &) = delete; + ExtensionInterface () {} + + virtual ~ExtensionInterface () {} + + virtual void initializeExtension() {} +} ; + +template +void initializeExtension(T *t) +{ + t->initializeExtension(); +} + +//#include "Extensible+CacheViaArrayAndMap.h" +#include "Extensible+CacheViaArray.h" + +// ----------------- + +struct ExtensibleBaseDefault +{ + virtual ~ExtensibleBaseDefault () {}; +} ; + +template +struct Extensor : B +{ + typedef B Super; + mutable ExtensibleCache extensions; + + Extensor () { } + + template + Extensor(T &&...t) : + B(std::forward(t)...) {} + + virtual ~Extensor () + { + extensions.clear(); + } + + template::value>::type* = nullptr> + T &getExtensionTU (U *self) const + { + if (auto extension = extensions.find()) + return *extension; + + T *t = new T( + static_cast(self) + ); + + extensions.insert(t); + initializeExtension(t); + + return *t; + } + + template::value>::type* = nullptr> + T &getExtensionTU (U *self) const + { + if (auto extension = extensions.find()) + return *extension; + + throw tjp::core::Exception("Extension not available"); + } + + template::value>::type* = nullptr> + T &getExtensionU () const + { + if (auto extension = extensions.find()) + return *extension; + + T *t = new T( + static_cast(remove_const_of_var(this)) + ); + + extensions.insert(t); + initializeExtension(t); + + return *t; + } + + template::value>::type* = nullptr> + T &getExtensionU () const + { + if (auto extension = extensions.find()) + return *extension; + + throw tjp::core::Exception("Extension not available"); + } + + template::value>::type* = nullptr> + bool hasExtensionU () const + { + return true; + } + + template::value>::type* = nullptr> + bool hasExtensionU () const + { + return extensions.find(); + } + + template + T &makeExtensionTU (const U *self, V &&...v) const + { + if (auto extension = extensions.find()) + return *extension; + + T *t = new TI( + static_cast(remove_const_of_var(self)), + v... + ); + + extensions.insert(t); + initializeExtension(t); + + return *t; + } + + template + T &makeExtensionU (V &&...v) const + { + if (auto extension = extensions.find()) + return *extension; + + T *t = new TI( + static_cast(remove_const_of_var(this)), + v... + ); + + extensions.insert(t); + initializeExtension(t); + + return *t; + } + + template + T withExtensionU () const + { + return T( + ExtendingWith {}, + static_cast(remove_const_of_var(this)) + ); + } + + template + void eraseExtension () + { + if (auto extension = extensions.find()) + { + extensions.erase(extension); + delete extension; + } + } +}; + +template +struct Extensible : Extensor +{ + typedef Extensor Extensor_; + typedef Extensible Extensible_; + + Extensible () {} + + template + Extensible(T &&...t) : + Extensor_(std::forward(t)...) {} + + template + bool hasExtension () const + { + return this->template hasExtensionU(); + } + + template + T &getExtension () const + { + return this->template getExtensionU(); + } + + template + T &makeExtension (V &&...v) const + { + return this->template makeExtensionU(v...); + } + + template + T &makeExtension (V &&...v) const + { + return this->template makeExtensionU(v...); + } + + template + T withExtension () const + { + return this->template withExtensionU(); + } +} ; + +template +struct Extensibled : B +{ + typedef B Super; + typedef Extensibled Extensible_; + + template + Extensibled(T &&...t) : + B(std::forward(t)...) {} + + Extensibled() {} + + template + T &getExtension () const + { + return this->template getExtensionU(); + } + + template + bool hasExtension () const + { + return this->template hasExtensionU(); + } + + template + T &makeExtension (V &&...v) const + { + return this->template makeExtensionU(v...); + } + + template + T &makeExtension (V &&...v) const + { + return this->template makeExtensionU(v...); + } + + template + T withExtension () const + { + return this->template withExtensionU(); + } +} ; + +} // namespace diff --git a/tjp/core/extensible/ExtensibleWith.h b/tjp/core/extensible/ExtensibleWith.h new file mode 100644 index 0000000..372ecf6 --- /dev/null +++ b/tjp/core/extensible/ExtensibleWith.h @@ -0,0 +1,33 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp::core { + +template +struct WithExtension {}; + +struct ExtendingWith {}; + +template +struct ExtensibleWith +{ + template + T withExtensionU () const + { + return T( + ExtendingWith {}, + static_cast(remove_const_of_var(this)) + ); + } + + template + T withExtension () const + { + return withExtensionU(); + } +} ; + +} // namespace diff --git a/tjp/core/extensible/_tests/Extensible.cpp b/tjp/core/extensible/_tests/Extensible.cpp new file mode 100644 index 0000000..95f2f6d --- /dev/null +++ b/tjp/core/extensible/_tests/Extensible.cpp @@ -0,0 +1,130 @@ +// TJP COPYRIGHT HEADER + +#include + +#include + +#include +#include + +namespace tjp::core { +namespace { + +struct A : Extensible +{ + int num = 0; +} ; + +struct B : Extensibled +{ + +} ; + +struct C : Extensibled +{ + +} ; + + +struct A_Ext : Extension +{ + A_Ext (A *a) { a->num++; } + + char val() const { return 'A'; } +} ; + +struct B_Ext : Extension +{ + B_Ext (B *b) { b->num++; } + + char val() const { return 'B'; } +} ; + +struct C_Ext : Extension +{ + C_Ext (C *c) { c->num++; } + + char val() const { return 'C'; } +} ; + +SCENARIO("extensible") +{ + xLogActivateStory("core::extensible"); + + GIVEN("sizeof") + { + sLogTest("testing", logVar(sizeof(Extensible)) << logVar(sizeof(ExtensibleCache))); + sLogTest("testing", logVar(sizeof(ExtensibleCache::Cache)) << logVar(sizeof(ExtensibleCache::Redirects))); + } + + GIVEN("a get extension") + { + A a; + B b; + C c; + + auto val = a.getExtension().val() ; + REQUIRE(val == 'A'); + REQUIRE(A_Ext::extension_index == 0); + + REQUIRE(b.getExtension().val() == 'B'); + REQUIRE(B_Ext::extension_index == 1); + +// REQUIRE(c.getExtension().val() == 'C'); + + val = c.getExtension().val(); + REQUIRE(val == 'C'); + REQUIRE(C_Ext::extension_index == 2); + + REQUIRE(a.num == 1); + REQUIRE(b.num == 1); + REQUIRE(c.num == 1); + + + val = c.getExtension().val() ; + REQUIRE(val == 'A'); + + val = c.getExtension().val() ; + REQUIRE(val == 'B'); + + val = c.getExtension().val() ; + REQUIRE(val == 'C'); + + val = c.getExtension().val() ; + REQUIRE(val == 'A'); + + val = c.getExtension().val() ; + REQUIRE(val == 'B'); + + val = c.getExtension().val() ; + REQUIRE(val == 'C'); + + REQUIRE(c.num == 3); + + } + + GIVEN("a make extension") + { + A a; + B b; + C c; + + auto val = a.makeExtension().val() ; + REQUIRE(val == 'A'); + + REQUIRE(b.makeExtension().val() == 'B'); + REQUIRE(c.makeExtension().val() == 'C'); + + val = a.getExtension().val() ; + REQUIRE(val == 'A'); + + + REQUIRE(a.num == 1); + REQUIRE(b.num == 1); + REQUIRE(c.num == 1); + + } +} + +} // namespace +} // namespace diff --git a/tjp/core/file/Contents.h b/tjp/core/file/Contents.h new file mode 100644 index 0000000..ca9a481 --- /dev/null +++ b/tjp/core/file/Contents.h @@ -0,0 +1,98 @@ +// TJP COPYRIGHT HEADER + +#pragma once + + +#include +#include +#include +#include + +namespace tjp::core::file { + +using Contents = Vector; + +struct ContentsView +{ + using value_type = char; + using pointer_type = const value_type *; + + const char *data_; + const size_t size_; + + constexpr + pointer_type data() const + { + return data_; + } + + constexpr + size_t size() const + { + return size_; + } + + constexpr ContentsView() : + data_((char *)nullptr), + size_(0) + { + } + + + template + constexpr ContentsView(const T *data, const size_t size) : + data_((pointer_type)data), + size_(size * sizeof(T)) + { + } + + template + ContentsView(const Vector &v) : + ContentsView(v.data(), v.size()) + { + } + + template + constexpr ContentsView(const T &v) : + ContentsView(v.data(), v.size()) + { + } + + + constexpr ContentsView(const char *data) : + ContentsView(data, core::const_expr::length(data)) + { + } + + constexpr ContentsView(const ContentsView &v) : + ContentsView(v.data(), v.size()) + { + } + + constexpr ContentsView(const StringView &v) : + ContentsView(v.data(), v.size()) + { + } + + ContentsView(const String &v) : + ContentsView(v.c_str(), v.size()) + { + } + + operator Contents() const + { + return Contents(data_, data_ + size_); + } + + operator StringView() const + { + return StringView(data_, size_); + } + + operator String() const + { + return String(data_, size_); + } +} ; + +} // namespace diff --git a/tjp/core/file/Directories.h b/tjp/core/file/Directories.h new file mode 100644 index 0000000..278b031 --- /dev/null +++ b/tjp/core/file/Directories.h @@ -0,0 +1,24 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +#if defined(SYS_APPLE) + + #include "apple/Directories.h" + + namespace tjp::core::file { + using namespace file::apple; + } + +#else + + #include "linux/Directories.h" + + namespace tjp::core::file { + using namespace file::linux; + } + +#endif + diff --git a/tjp/core/file/File.cpp b/tjp/core/file/File.cpp new file mode 100644 index 0000000..fac802e --- /dev/null +++ b/tjp/core/file/File.cpp @@ -0,0 +1,158 @@ +// TJP COPYRIGHT HEADER + +#include "File.h" +#include +#include + +#include +#include +#include + +#include +#include +#include + +namespace tjp::core::file { + +TJP_CORE_HEADER_ONLY_INLINE +Optional size(const StringView& path) +{ + std::ifstream file(String(path), std::ios::ate | std::ios::binary); + bool exists = (bool)file; + + if (!exists || !file.is_open()) + return {}; + + size_t fileSize = (size_t)file.tellg(); + return fileSize; +} + +TJP_CORE_HEADER_ONLY_INLINE +Optional readRange(const StringView& path, s64 offset, size_t length) +{ + std::ifstream file(String(path), std::ios::ate | std::ios::binary); + bool exists = (bool)file; + + if (!exists || !file.is_open()) + return {}; + + size_t fileSize = (size_t)file.tellg(); + if (offset < 0) + offset += s64(fileSize); + + if (offset < 0) + return {}; + + if (fileSize < offset + length) + return {}; + + Contents buffer(length); + + file.seekg(offset); + file.read(buffer.data(), buffer.size()); + file.close(); + + return buffer; +}; + + +TJP_CORE_HEADER_ONLY_INLINE +bool pathExists (const StringView &file) +{ + struct stat status; + return (stat (file.data(), &status)==0); +} + + + + +TJP_CORE_HEADER_ONLY_INLINE +bool ensurePath (const StringView &base_, const StringView &path) +{ + char delimiter = '/'; + + // find the end of the last portion of the path we need to create + auto finalPos = path.rfind(delimiter); + if (finalPos == path.npos) + return true; + + auto base = String(base_); + if (!base.empty() && base.back() != delimiter) + base += delimiter; + + decltype(finalPos) pos = 0; + + auto colonpos = path.find(':'); + auto firstdelimiterpos = path.find(delimiter); + + if (colonpos > 0 && firstdelimiterpos > 0 && colonpos < firstdelimiterpos) + { + // Skip the first one if there's a colon ('c:/blah/whaterver') + pos = path.find (delimiter, pos+1); + } + + while (pos 0; + + if (exists) + { + // if it does exist, but is not a directory, ensurePath fails + if (!(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) + return false; + } + else + { + // if it does not exist create it, if this fails ensurePath fails + if (_wmkdir (directory.c_str())!=0) + return false; + } + +#else + + auto directory_ = path.substr (0, pos); + auto directory = base + directory_; + + struct stat status; + int exists = (stat (directory.c_str(), &status)==0); + + if (exists) + { + // if it does exist, but is not a directory, ensurePath fails + if (!(status.st_mode & S_IFDIR)) + return false; + } + else + { + // if it does not exist create it, if this fails ensurePath fails + auto result = mkdir (directory.c_str(), (S_IRWXU | S_IRWXG | S_IRWXO)); + if (result) + return false; + } +#endif + + } + return true; +} + +TJP_CORE_HEADER_ONLY_INLINE +bool ensurePath (const StringView &path) +{ + return ensurePath("", path); +} + + +} // namespace diff --git a/tjp/core/file/File.h b/tjp/core/file/File.h new file mode 100644 index 0000000..09cbbb7 --- /dev/null +++ b/tjp/core/file/File.h @@ -0,0 +1,46 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +#include "Contents.h" +#include +#include +#include +#include +#include + +#include +#include "file_exists.hpp" +#include "read.hpp" +#include "write.hpp" +#include "times.hpp" + +namespace tjp::core::file { + +Optional size(const StringView &path); + + +Optional readRange(const StringView &path, s64 offset, size_t length); + + +Contents readFile(const StringView &path); + + +bool writeFile(const StringView &path, const ContentsView &contents, const WriteOptions &options = {}); + +bool pathExists (const StringView &path); +bool ensurePath (const StringView &path); +bool ensurePath (const StringView &base, const StringView &path); + + +bool deleteFile(const StringView &path); + +Vector getFiles(const StringView &path, const Optional &extension); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "File.cpp" +#endif diff --git a/tjp/core/file/File_Messy.cpp b/tjp/core/file/File_Messy.cpp new file mode 100644 index 0000000..d28ad01 --- /dev/null +++ b/tjp/core/file/File_Messy.cpp @@ -0,0 +1,328 @@ +// TJP COPYRIGHT HEADER + +#include "File.h" +#include +#include + +#include +#include + +#include +#include + +#include +#include "../random/Random.h" + +#include +#include + +#include +#include +#include +#include +#include + +#include + +namespace tjp::core::file { + +std::vector readFile(const StringView& path) +{ + std::ifstream file(String(path), std::ios::ate | std::ios::binary); + bool exists = (bool)file; + + if (!exists || !file.is_open()) { + throw Exception { "failed to open file " + String(path) }; + } + + size_t fileSize = (size_t)file.tellg(); + std::vector buffer(fileSize); + + file.seekg(0); + file.read(buffer.data(), fileSize); + + file.close(); + + return buffer; +}; + +bool writeFile(const StringView &path_, const ContentsView &contents, const WriteOptions &options) +{ + auto path = String(path_); + auto extension = "." + core::to_string(core::StandardRandom.next()); + auto temporary = path + extension; + + std::ofstream file(temporary, std::ios::binary); + if (!file) + return false; + + file.write(contents.data(), contents.size()); + file.close(); + + remove(path.c_str()); + if (rename(temporary.c_str(), path.c_str())) + return false; + + if (options.lastModifiedTime) + { + setFileLastModifiedTime(path, options.lastModifiedTime); + } + + return true; +} + +/* +bool ensurePath (const StringView &path) +{ + return ensurePath("", path); +} + +bool ensurePath (const StringView &base_, const StringView &path_) +{ + sLogDebug("core::file", logVar(base_) << logVar(path_)); + + char delimiter = '/'; + + auto path = String(path_); + auto base = String(base_); + if (!base.empty() && base.back() != delimiter) + base += delimiter; + + // find the end of the last portion of the path we need to create + auto finalPos = path.rfind (delimiter); + + decltype(finalPos) pos = 0; + + auto colonpos = path.find(':'); + auto firstdelimiterpos = path.find(delimiter); + + if (colonpos > 0 && firstdelimiterpos > 0 && colonpos < firstdelimiterpos) + { + // Skip the first one if there's a colon ('c:/blah/whaterver') + pos = path.find (delimiter, pos+1); + } + + while (pos 0; + + if (exists) + { + // if it does exist, but is not a directory, ensurePath fails + if (!(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) + return false; + } + else + { + // if it does not exist create it, if this fails ensurePath fails + if (_wmkdir (directory.c_str())!=0) + return false; + } + +#else + + std::string directory_ = path.substr (0, pos); + std::string directory = String(base) + directory_; + + sLogDebug("core::file", logVar(path) << logVar(directory)); + + struct stat status; + int exists = (stat (directory.c_str(), &status)==0); + + if (exists) + { + // if it does exist, but is not a directory, ensurePath fails + if (!(status.st_mode & S_IFDIR)) + return false; + } + else + { + // if it does not exist create it, if this fails ensurePath fails + auto result = mkdir (directory.c_str(), (S_IRWXU | S_IRWXG | S_IRWXO)); + if (result) + return false; + } +#endif + + } + return true; +} + +bool fileExists (const StringView &file) +{ +#ifdef WIN32 + + WIN32_FILE_ATTRIBUTE_DATA data; + bool exists = GetFileAttributesExW (toLongPath(file).c_str(), GetFileExInfoStandard, &data) > 0; + + return exists && !(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); + +#else + struct stat status; + if (stat (file.data(), &status)==0) + { + // if it is not a directory return true + if (!(status.st_mode & S_IFDIR)) + return true; + } + + return false; + +#endif +} + +bool pathExists (const StringView &file) +{ +#ifdef WIN32 + + WIN32_FILE_ATTRIBUTE_DATA data; + bool exists = GetFileAttributesExW (toLongPath(file).c_str(), GetFileExInfoStandard, &data) > 0; + + return exists; + +#else + struct stat status; + return (stat (file.data(), &status)==0); + +#endif +} +*/ + + + + + + +bool deleteFile(const StringView &file) +{ + return remove(file.data()) == 0; +} + +#ifndef SYS_IOS + +Vector getFiles(const StringView &path, const Optional &extension) +{ + Vector files; + try + { + std::queue dirs; + std::filesystem::path basePath = std::filesystem::canonical(path); + dirs.push(basePath); + + while (!dirs.empty()) + { + auto current = dirs.front(); + dirs.pop(); + + for (const auto &entry : std::filesystem::directory_iterator(current)) + { + if (entry.is_directory()) + { + dirs.push(entry.path()); + } + else + if (entry.is_regular_file()) + { + if (!extension || entry.path().extension() == *extension) { + files.push_back(std::filesystem::relative(entry.path(), basePath).string()); + } + } + } + } + } + catch (std::filesystem::filesystem_error &) + { + std::cerr << "caught std::filesystem::filesystem_error " << std::endl; + } + catch (std::exception &) + { + std::cerr << "caught std::exception " << std::endl; + } + catch (...) + { + std::cerr << "caught unknown exception " << std::endl; + } + + return files; +} + + + +#else + +Vector getFiles(const StringView &path_, const Optional &extension) +{ + auto path = String(path_); + Vector files; + std::queue dirs; + dirs.push(""); + + while (!dirs.empty()) + { + auto relativeDir = dirs.front(); + std::string absoluteDir = path + relativeDir; + dirs.pop(); + + DIR *dir = opendir(absoluteDir.c_str()); + if (!dir) { + continue; + } + + struct dirent *entry; + while ((entry = readdir(dir)) != nullptr) + { + std::string name(entry->d_name); + + // skip "." and ".." + if (name == "." || name == "..") { + continue; + } + + std::string relativePath = + relativeDir.empty() ? + name : + relativeDir + "/" + name; + + if (entry->d_type == DT_DIR) + { + // enqueue subdirectory + dirs.push(relativePath); + } + else + { + if (extension.has_value()) + { + if (name.size() >= extension->size() && + name.compare(name.size() - extension->size(), extension->size(), *extension) == 0) + { + files.emplace_back(relativePath.c_str()); + } + } + else + { + files.emplace_back(relativePath.c_str()); + } + } + } + closedir(dir); + } + + return files; +} + +#endif + +} // namespace diff --git a/tjp/core/file/Path.cpp b/tjp/core/file/Path.cpp new file mode 100644 index 0000000..ea67cc2 --- /dev/null +++ b/tjp/core/file/Path.cpp @@ -0,0 +1,169 @@ +// TJP COPYRIGHT HEADER + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#include "Path.h" + +namespace tjp::core::file { + +TJP_CORE_HEADER_ONLY_INLINE +std::string_view removeExtensions(const std::string_view &s) +{ + auto lastSlash = s.rfind('/'); + auto firstDotAfterLastSlash = s.find('.', lastSlash+1); + return s.substr(0, firstDotAfterLastSlash); +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string joinDirs(const std::vector &dirs) +{ + std::string joined; + for (auto &dir : dirs) + { + if (dir.empty()) + continue; + + bool first = joined.empty(); + + if (dir.front() == '/' && !first) + joined += dir.substr(1); + else + joined += dir; + + if (joined.back() != '/') + joined.push_back('/'); + } + + return joined; +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string joinPaths(const std::vector &dirs) +{ + std::string joined; + for (auto &dir : dirs) + { + if (dir.empty()) + continue; + + bool first = joined.empty(); + + if (!first) + if (joined.back() != '/') + joined.push_back('/'); + + if (dir.front() == '/' && !first) + joined += dir.substr(1); + else + joined += dir; + } + return joined; +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string joinDirsAndFile(const std::vector &dirs, const std::string &fileName) +{ + return joinDirs(dirs) + fileName; +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string convertDelimiter (const std::string &str, char from, char to) +{ + if (from==to) return str; + if (str.empty()) return str; + + std::string copy = str; + for (auto &c : copy) + if (c == from) + c = to; + + return copy; +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string convertDelimiter (const std::string &path) +{ + return path; +} + +template +T popFileName_(const T &path) +{ + auto lastDelimiter = path.rfind('/'); + return path.substr(0, lastDelimiter+1); +} + +template +T popLast_(const T &path) +{ + if (path.empty()) + return {}; + + if (path.back() != '/') + return popFileName(path); + + if (path.size() < 2) + return {}; + + auto lastDelimiter = path.rfind('/', path.size()-2); + return path.substr(0, lastDelimiter+1); +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string_view popFileName(const std::string_view &path) +{ + return popFileName_(path); +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string_view popLast(const std::string_view &path) +{ + return popLast_(path); +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string popFileName(const std::string &path) +{ + return popFileName_(path); +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string popLast(const std::string &path) +{ + return popLast_(path); +} + + +TJP_CORE_HEADER_ONLY_INLINE +std::string fileName(const std::string &path) +{ + auto lastDelimiter = path.rfind('/'); + if (lastDelimiter == std::string::npos) + return path; + + return path.substr(lastDelimiter+1); +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string_view fileName(const std::string_view &path) +{ + auto lastDelimiter = path.rfind('/'); + if (lastDelimiter == std::string::npos) + return path; + + return path.substr(lastDelimiter+1); +} + +TJP_CORE_HEADER_ONLY_INLINE +std::string extension(const std::string &path) +{ + auto lastDelimiter = path.rfind('.'); + if (lastDelimiter == std::string::npos) + return ""; + + return path.substr(lastDelimiter); +} + +} // namespace diff --git a/tjp/core/file/Path.h b/tjp/core/file/Path.h new file mode 100644 index 0000000..4438bba --- /dev/null +++ b/tjp/core/file/Path.h @@ -0,0 +1,44 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +#include + +namespace tjp::core::file { + +std::string joinDirs(const std::vector &dirs); +std::string joinPaths(const std::vector &paths); +std::string joinDirsAndFile(const std::vector &dirs, const std::string_view &fileName); + +std::string fileName(const std::string &path); +std::string_view fileName(const std::string_view &path); + +std::string extension(const std::string &path); +std::string_view removeExtensions(const std::string_view &path); + +std::string_view popFileName(const std::string_view &path); +std::string_view popLast(const std::string_view &path); + +std::string popFileName(const std::string &path); +std::string popLast(const std::string &path); + + +#ifdef WIN32 +const char LocalDirectoryDelimiter = '\\'; +#else +const char LocalDirectoryDelimiter = '/'; +#endif + +const char ServerDirectoryDelimiter = '/'; + +std::string convertDelimiter (const std::string &path); +std::string convertDelimiter (const std::string &path, char from, char to); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "Path.cpp" +#endif diff --git a/tjp/core/file/VirtualFileSystem.cpp b/tjp/core/file/VirtualFileSystem.cpp new file mode 100644 index 0000000..d3d4332 --- /dev/null +++ b/tjp/core/file/VirtualFileSystem.cpp @@ -0,0 +1,31 @@ +// TJP COPYRIGHT HEADER + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif +#include + +#include "VirtualFileSystem.hpp" + +namespace tjp::core::file { + +TJP_CORE_HEADER_ONLY_INLINE +Contents readFile(const VirtualFileSystem &system, const StringView &path) +{ + return system.get(path); +} + +TJP_CORE_HEADER_ONLY_INLINE +void writeFile(VirtualFileSystem &system, const StringView &path, ContentsView contents) +{ + return system.put(path, contents); +} + +TJP_CORE_HEADER_ONLY_INLINE +bool fileExists(const VirtualFileSystem &system, const StringView &path) +{ + return system.has(path); +} + + +} // namespace diff --git a/tjp/core/file/VirtualFileSystem.h b/tjp/core/file/VirtualFileSystem.h new file mode 100644 index 0000000..1c11113 --- /dev/null +++ b/tjp/core/file/VirtualFileSystem.h @@ -0,0 +1,20 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Contents.h" +#include + +namespace tjp::core::file { + +struct VirtualFileSystem; + +Contents readFile(const VirtualFileSystem &system, const StringView &path); +void writeFile(VirtualFileSystem &system, const StringView &path, ContentsView); +bool fileExists(const VirtualFileSystem &system, const StringView &path); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "VirtualFileSystem.cpp" +#endif diff --git a/tjp/core/file/VirtualFileSystem.hpp b/tjp/core/file/VirtualFileSystem.hpp new file mode 100644 index 0000000..20385cb --- /dev/null +++ b/tjp/core/file/VirtualFileSystem.hpp @@ -0,0 +1,108 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "File.h" +#include "Path.h" + +namespace tjp::core::file { + +struct VirtualFileSystem +{ + virtual ~VirtualFileSystem () {}; + + virtual String pathFor(const StringView &path) const = 0; + virtual Optional get_if(const StringView &path) const = 0; + virtual Contents get(const StringView &path) const = 0; + virtual void put(const StringView &path, ContentsView) = 0; + virtual bool has(const StringView &path) const = 0; +} ; + +struct VirtualFileSystemRoot : VirtualFileSystem +{ + String base; + + VirtualFileSystemRoot(const StringView &base_) : + base(base_) + {} + + inline + String pathFor(const StringView &path) const override + { + return joinPaths({ base, String(path) }); + } + + inline + Optional get_if(const StringView &path) const override + { + return file::read(pathFor(path)); + } + + inline + Contents get(const StringView &path) const override + { + return file::require_read(pathFor(path)); + } + + inline + void put(const StringView &path, ContentsView contents) override + { + file::ensurePath(base, path); + file::require_write(pathFor(path), contents); + } + + inline + bool has(const StringView &path) const override + { + return file::fileExists(pathFor(path)); + } +} ; + +struct VirtualFileSystemRelative : VirtualFileSystem +{ + VirtualFileSystem *to; + String base; + + VirtualFileSystemRelative(VirtualFileSystem *to_, const StringView &base_) : + to(to_), + base(base_) + {} + + String join(const StringView &path) const + { + return joinPaths({ base, String(path) }); + } + + String pathFor(const StringView &path) const override + { + return to->pathFor(join(path)); + } + + inline + Optional get_if(const StringView &path) const override + { + return to->get_if(join(path)); + } + + inline + Contents get(const StringView &path) const override + { + return to->get(join(path)); + } + + inline + void put(const StringView &path, ContentsView contents) override + { + to->put(join(path), contents); + } + + inline + bool has(const StringView &path) const override + { + return to->has(join(path)); + } + +} ; + +} // namespace + diff --git a/tjp/core/file/_tests/Path.cpp b/tjp/core/file/_tests/Path.cpp new file mode 100644 index 0000000..033b1f3 --- /dev/null +++ b/tjp/core/file/_tests/Path.cpp @@ -0,0 +1,32 @@ +// TJP COPYRIGHT HEADER + +#include +#include +#include +#include +#include "../Path.h" + +namespace tjp::core::file { +namespace { + +SCENARIO("core::file::Path") +{ + GIVEN("fileNames") + { + Vector> tests { + { "hello.cpp", "hello" }, + { "asdf.123.345.456", "asdf" }, + { "/asdf/123.asdf.sdf", "/asdf/123" }, + { "/asdf.zxc/123.asdf.sdf", "/asdf.zxc/123" } + }; + + for (auto &[t, e] : tests) + { + auto v = removeExtensions(t); + REQUIRE(v == e); + } + } +} + +} // namespace +} // namespace diff --git a/tjp/core/file/apple/Directories.cpp b/tjp/core/file/apple/Directories.cpp new file mode 100644 index 0000000..ca00a79 --- /dev/null +++ b/tjp/core/file/apple/Directories.cpp @@ -0,0 +1,71 @@ +// TJP COPYRIGHT HEADER + +#include "Directories.h" +#include "../Path.h" + +#include + +#ifdef SYS_MAC +#include +#include "CoreFoundation/CFBundle.h" +#endif + +#ifdef SYS_IOS +#include +#include "CoreFoundation/CFBundle.h" +#endif + +#include + + +namespace tjp { +namespace core { +namespace file { +namespace apple { + +std::string getHomeDirectory () +{ + const char *homeDir = getenv("HOME"); + if (homeDir) + return homeDir; + + return {}; +} + +std::string getExecutablePath () +{ + char path_[PATH_MAX+1]; + + uint32_t size=PATH_MAX; + _NSGetExecutablePath(path_, &size); + if (size > 0) + { + auto path = std::string(path_); + return path; + } + + return {}; +} + +std::string getExecutableDirectory () +{ + auto path = getExecutablePath(); + return popFileName(path); +} + +std::string getExecutableBundleDirectory () +{ + return popLast(getExecutableDirectory()); +} + +std::string getExecutableResourceDirectory () +{ + auto path = getExecutableBundleDirectory(); + return joinDirs({ path, "Resources"}); +} + + +} // namespace +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/file/apple/Directories.h b/tjp/core/file/apple/Directories.h new file mode 100644 index 0000000..bcfcdc2 --- /dev/null +++ b/tjp/core/file/apple/Directories.h @@ -0,0 +1,22 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +namespace tjp { +namespace core { +namespace file { +namespace apple { + +std::string getExecutablePath(); +std::string getExecutableDirectory(); +std::string getExecutableBundleDirectory(); +std::string getExecutableResourceDirectory(); +std::string getHomeDirectory(); + +} // namespace +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/file/copy.cpp b/tjp/core/file/copy.cpp new file mode 100644 index 0000000..5285ecc --- /dev/null +++ b/tjp/core/file/copy.cpp @@ -0,0 +1,28 @@ +// TJP COPYRIGHT HEADER + + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#include + +#include "copy.hpp" +#include "read.hpp" +#include "write.hpp" + +#include + +namespace tjp::core::file { + +TJP_CORE_HEADER_ONLY_INLINE +bool copy(const String &from, const String &to) +{ + if (auto contents = read(from)) + if (write(to, *contents)) + return true; + + return false; +} + +} // namespace diff --git a/tjp/core/file/copy.hpp b/tjp/core/file/copy.hpp new file mode 100644 index 0000000..5dfd9b5 --- /dev/null +++ b/tjp/core/file/copy.hpp @@ -0,0 +1,15 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp::core::file { + +bool copy(const String &path, const String &to); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "copy.cpp" +#endif diff --git a/tjp/core/file/directory_exists.cpp b/tjp/core/file/directory_exists.cpp new file mode 100644 index 0000000..92f2674 --- /dev/null +++ b/tjp/core/file/directory_exists.cpp @@ -0,0 +1,28 @@ +// TJP COPYRIGHT HEADER + + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif +#include + +#include "directory_exists.hpp" +#include + +namespace tjp::core::file { + +TJP_CORE_HEADER_ONLY_INLINE +bool directory_exists (const String &file) +{ + struct stat status; + if (stat (file.c_str(), &status)==0) + { + // if it is not a directory return true + if (status.st_mode & S_IFDIR) + return true; + } + + return false; +} + +} // namespace diff --git a/tjp/core/file/directory_exists.hpp b/tjp/core/file/directory_exists.hpp new file mode 100644 index 0000000..682968c --- /dev/null +++ b/tjp/core/file/directory_exists.hpp @@ -0,0 +1,15 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp::core::file { + +bool directory_exists(const String &path); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "directory_exists.cpp" +#endif diff --git a/tjp/core/file/directory_of.cpp b/tjp/core/file/directory_of.cpp new file mode 100644 index 0000000..55e16f6 --- /dev/null +++ b/tjp/core/file/directory_of.cpp @@ -0,0 +1,20 @@ +// TJP COPYRIGHT HEADER + +#ifdef TJP_HEADER_ONLY + #pragma once +#endif + +#include + +#include "directory_of.hpp" + +namespace tjp::core::file { + +TJP_CORE_HEADER_ONLY_INLINE +StringView directory_of(const StringView &path) +{ + auto lastDelimiter = path.rfind('/'); + return path.substr(0, lastDelimiter+1); +} + +} // namespace diff --git a/tjp/core/file/directory_of.hpp b/tjp/core/file/directory_of.hpp new file mode 100644 index 0000000..552e9c6 --- /dev/null +++ b/tjp/core/file/directory_of.hpp @@ -0,0 +1,15 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp::core::file { + +StringView directory_of(const StringView &path); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "directory_of.cpp" +#endif diff --git a/tjp/core/file/file_exists.cpp b/tjp/core/file/file_exists.cpp new file mode 100644 index 0000000..a3252b7 --- /dev/null +++ b/tjp/core/file/file_exists.cpp @@ -0,0 +1,34 @@ +// TJP COPYRIGHT HEADER + + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif +#include + +#include "file_exists.hpp" +#include + +namespace tjp::core::file { + +TJP_CORE_HEADER_ONLY_INLINE +bool fileExists (const String &file) +{ + struct stat status; + if (stat (file.c_str(), &status)==0) + { + // if it is not a directory return true + if (!(status.st_mode & S_IFDIR)) + return true; + } + + return false; +} + +TJP_CORE_HEADER_ONLY_INLINE +bool fileExists (const StringView &file) +{ + return fileExists(String(file)); +} + +} // namespace diff --git a/tjp/core/file/file_exists.hpp b/tjp/core/file/file_exists.hpp new file mode 100644 index 0000000..110a095 --- /dev/null +++ b/tjp/core/file/file_exists.hpp @@ -0,0 +1,17 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +namespace tjp::core::file { + +bool fileExists(const String &path); +bool fileExists(const StringView &path); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "file_exists.cpp" +#endif diff --git a/tjp/core/file/get_files.cpp b/tjp/core/file/get_files.cpp new file mode 100644 index 0000000..a2bede5 --- /dev/null +++ b/tjp/core/file/get_files.cpp @@ -0,0 +1,135 @@ +// TJP COPYRIGHT HEADER + +#ifdef TJP_HEADER_ONLY + #pragma once +#endif + +#include + +#include "get_files.hpp" +#include + +#include +#include + +#include +#include +#include +#include + +namespace tjp::core::file { + +#ifndef SYS_IOS + +TJP_CORE_HEADER_ONLY_INLINE +Vector get_files(const String &path, const Optional &extension, bool recursive) +{ + Vector files; + try + { + std::queue dirs; + std::filesystem::path basePath = std::filesystem::canonical(path); + dirs.push(basePath); + + while (!dirs.empty()) + { + auto current = dirs.front(); + dirs.pop(); + + for (const auto &entry : std::filesystem::directory_iterator(current)) + { + if (entry.is_directory()) + { + if (recursive) + dirs.push(entry.path()); + } + else + if (entry.is_regular_file()) + { + if (!extension || entry.path().extension() == *extension) { + files.push_back(std::filesystem::relative(entry.path(), basePath).string()); + } + } + } + } + } + catch (std::filesystem::filesystem_error &) + { + } + catch (std::exception &) + { + } + catch (...) + { + } + + return files; +} + +#else + +TJP_CORE_HEADER_ONLY_INLINE +Vector get_files(const String &path_, const Optional &extension, bool recursive) +{ + auto path = String(path_); + Vector files; + std::queue dirs; + dirs.push(""); + + while (!dirs.empty()) + { + auto relativeDir = dirs.front(); + std::string absoluteDir = path + relativeDir; + dirs.pop(); + + DIR *dir = opendir(absoluteDir.c_str()); + if (!dir) { + continue; + } + + struct dirent *entry; + while ((entry = readdir(dir)) != nullptr) + { + std::string name(entry->d_name); + + // skip "." and ".." + if (name == "." || name == "..") { + continue; + } + + std::string relativePath = + relativeDir.empty() ? + name : + relativeDir + "/" + name; + + if (entry->d_type == DT_DIR) + { + // enqueue subdirectory + if (recursive) + dirs.push(relativePath); + } + else + { + if (extension.has_value()) + { + if (name.size() >= extension->size() && + name.compare(name.size() - extension->size(), extension->size(), *extension) == 0) + { + files.emplace_back(relativePath.c_str()); + } + } + else + { + files.emplace_back(relativePath.c_str()); + } + } + } + closedir(dir); + } + + return files; +} + +#endif + +} // namespace diff --git a/tjp/core/file/get_files.hpp b/tjp/core/file/get_files.hpp new file mode 100644 index 0000000..6e9e51d --- /dev/null +++ b/tjp/core/file/get_files.hpp @@ -0,0 +1,17 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include + +namespace tjp::core::file { + +Vector get_files(const String &path, const Optional &extension, bool recursive); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "get_files.cpp" +#endif diff --git a/tjp/core/file/is_relative_path.hpp b/tjp/core/file/is_relative_path.hpp new file mode 100644 index 0000000..763be02 --- /dev/null +++ b/tjp/core/file/is_relative_path.hpp @@ -0,0 +1,15 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp::core::file { + +inline +bool is_relative_path(const StringView &p) +{ + return p.empty() || p[0] != '/'; +} + +} // namespace diff --git a/tjp/core/file/linux/Directories.cpp b/tjp/core/file/linux/Directories.cpp new file mode 100644 index 0000000..07877cb --- /dev/null +++ b/tjp/core/file/linux/Directories.cpp @@ -0,0 +1,47 @@ +// TJP COPYRIGHT HEADER + +#include "Directories.h" +#include "../Path.h" + +#include + +namespace tjp { +namespace core { +namespace file { +namespace linux { + +std::string getHomeDirectory() +{ + const char *homeDir = getenv("HOME"); + if (homeDir) + return homeDir; + + return {}; +} + +std::string getExecutablePath () +{ + return "./"; +} + +std::string getExecutableDirectory () +{ + auto path = getExecutablePath(); + return popFileName(path); +} + +std::string getExecutableBundleDirectory () +{ + return getExecutableDirectory(); +} + +std::string getExecutableResourceDirectory () +{ + return getExecutableDirectory(); +} + + +} // namespace +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/file/linux/Directories.h b/tjp/core/file/linux/Directories.h new file mode 100644 index 0000000..59e245d --- /dev/null +++ b/tjp/core/file/linux/Directories.h @@ -0,0 +1,22 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +namespace tjp { +namespace core { +namespace file { +namespace linux { + +std::string getExecutablePath(); +std::string getExecutableDirectory(); +std::string getExecutableBundleDirectory(); +std::string getExecutableResourceDirectory(); +std::string getHomeDirectory(); + +} // namespace +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/file/move.cpp b/tjp/core/file/move.cpp new file mode 100644 index 0000000..3a7008b --- /dev/null +++ b/tjp/core/file/move.cpp @@ -0,0 +1,32 @@ +// TJP COPYRIGHT HEADER + + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#include + +#include "move.hpp" +#include + +#include + +namespace tjp::core::file { + +TJP_CORE_HEADER_ONLY_INLINE +bool move(const String &from, const String &to) +{ + return std::rename(from.c_str(), to.c_str()) != 0; +} + +TJP_CORE_HEADER_ONLY_INLINE +void require_move(const String &from, const String &to) +{ + if (!move(from, to)) + { + throw Exception { "Move failed" }; + } +} + +} // namespace diff --git a/tjp/core/file/move.hpp b/tjp/core/file/move.hpp new file mode 100644 index 0000000..575cb19 --- /dev/null +++ b/tjp/core/file/move.hpp @@ -0,0 +1,16 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp::core::file { + +void require_move(const String &path, const String &to); +bool move(const String &path, const String &to); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "move.cpp" +#endif diff --git a/tjp/core/file/pop_first.hpp b/tjp/core/file/pop_first.hpp new file mode 100644 index 0000000..cd186f4 --- /dev/null +++ b/tjp/core/file/pop_first.hpp @@ -0,0 +1,21 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +namespace tjp::core::file { + +template +Optional pop_first(const T &path) +{ + auto s = path.find('/'); + if (s != path.npos) + return path.substr(s+1); + + return {}; +} + +} // namespace + diff --git a/tjp/core/file/read.cpp b/tjp/core/file/read.cpp new file mode 100644 index 0000000..1ec6f83 --- /dev/null +++ b/tjp/core/file/read.cpp @@ -0,0 +1,62 @@ +// TJP COPYRIGHT HEADER + + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#include + +#include "read.hpp" + +#include +#include +#include + + +namespace tjp::core::file { + +TJP_CORE_HEADER_ONLY_INLINE +Optional read(const String& path) +{ + std::ifstream file(path, std::ios::ate | std::ios::binary); + bool exists = (bool)file; + + if (!exists || !file.is_open()) + return {}; + + size_t fileSize = (size_t)file.tellg(); + Contents buffer(fileSize); + + file.seekg(0); + file.read(buffer.data(), buffer.size()); + file.close(); + + return buffer; +}; + +TJP_CORE_HEADER_ONLY_INLINE +Contents require_read(const String& path) +{ + if (auto result = read(path)) + return *result; + + throw Exception { "failed to read file " + String(path) }; +} + +TJP_CORE_HEADER_ONLY_INLINE +Optional read(const StringView& path) +{ + return read(String(path)); +} + +TJP_CORE_HEADER_ONLY_INLINE +Contents require_read(const StringView& path) +{ + return require_read(String(path)); +} + + + + +} // namespace diff --git a/tjp/core/file/read.hpp b/tjp/core/file/read.hpp new file mode 100644 index 0000000..d8be9a2 --- /dev/null +++ b/tjp/core/file/read.hpp @@ -0,0 +1,21 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Contents.h" +#include +#include + +namespace tjp::core::file { + +Optional read(const String &path); +Contents require_read(const String &path); + +Optional read(const StringView &path); +Contents require_read(const StringView &path); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "read.cpp" +#endif diff --git a/tjp/core/file/split_first_directory.cpp b/tjp/core/file/split_first_directory.cpp new file mode 100644 index 0000000..5d9f678 --- /dev/null +++ b/tjp/core/file/split_first_directory.cpp @@ -0,0 +1,28 @@ +// TJP COPYRIGHT HEADER + +#ifdef TJP_HEADER_ONLY + #pragma once +#endif + +#include + +#include "split_first_directory.hpp" + +namespace tjp::core::file { + +TJP_CORE_HEADER_ONLY_INLINE +Optional> split_first_directory(const StringView &path) +{ + auto slash = path.find('/'); + if (slash != path.npos) + { + return Tuple { + path.substr(0, slash), + path.substr(slash+1) + } ; + } + + return {}; +} + +} // namespace diff --git a/tjp/core/file/split_first_directory.hpp b/tjp/core/file/split_first_directory.hpp new file mode 100644 index 0000000..7399b87 --- /dev/null +++ b/tjp/core/file/split_first_directory.hpp @@ -0,0 +1,17 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include + +namespace tjp::core::file { + +Optional> split_first_directory(const StringView &path); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "split_first_directory.cpp" +#endif diff --git a/tjp/core/file/times.cpp b/tjp/core/file/times.cpp new file mode 100644 index 0000000..41572f1 --- /dev/null +++ b/tjp/core/file/times.cpp @@ -0,0 +1,74 @@ +// TJP COPYRIGHT HEADER + + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#include + +#include "times.hpp" +#include + +#include +#include +#include + + +namespace tjp::core::file { + +TJP_CORE_HEADER_ONLY_INLINE +time_t getFileLastModifiedTime(const String & fullFileName) +{ + struct stat status; + + int result = stat(fullFileName.c_str(), &status); + + if (result != 0) + return 0; + + return status.st_mtime; +} + +TJP_CORE_HEADER_ONLY_INLINE +bool setFileLastModifiedTime(const String & fullFileName, time_t time) +{ + struct utimbuf timbuf; + timbuf.actime = getFileAccessTime(fullFileName); + timbuf.modtime = time; + + int result = utime(fullFileName.c_str(), &timbuf); + + if (result != 0) + return false; + + return true; +} + +TJP_CORE_HEADER_ONLY_INLINE +time_t getFileCreationTime (const String & fullFileName) +{ + struct stat status; + + int result = stat(fullFileName.c_str(), &status); + + if (result != 0) + return 0; + + return status.st_ctime; +} + +TJP_CORE_HEADER_ONLY_INLINE +time_t getFileAccessTime (const String & fullFileName) +{ + struct stat status; + + int result = stat(fullFileName.c_str(), &status); + + if (result != 0) + return 0; + + return status.st_atime; +} + +} // namespace diff --git a/tjp/core/file/times.hpp b/tjp/core/file/times.hpp new file mode 100644 index 0000000..43d4765 --- /dev/null +++ b/tjp/core/file/times.hpp @@ -0,0 +1,22 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +#include +#include + +namespace tjp::core::file { + +time_t getFileLastModifiedTime(const String & fullFileName); +bool setFileLastModifiedTime(const String & fullFileName, time_t); + +time_t getFileCreationTime (const String & fullFileName); +time_t getFileAccessTime (const String & fullFileName); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "times.cpp" +#endif diff --git a/tjp/core/file/unlink.cpp b/tjp/core/file/unlink.cpp new file mode 100644 index 0000000..4d67f4f --- /dev/null +++ b/tjp/core/file/unlink.cpp @@ -0,0 +1,24 @@ +// TJP COPYRIGHT HEADER + + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#include + + +#include "unlink.hpp" +#include + +#include + +namespace tjp::core::file { + +TJP_CORE_HEADER_ONLY_INLINE +bool unlink (const String &file) +{ + return std::remove(file.data()) == 0; +} + +} // namespace diff --git a/tjp/core/file/unlink.hpp b/tjp/core/file/unlink.hpp new file mode 100644 index 0000000..77bf9ee --- /dev/null +++ b/tjp/core/file/unlink.hpp @@ -0,0 +1,15 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp::core::file { + +bool unlink(const String &path); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "unlink.cpp" +#endif diff --git a/tjp/core/file/validate_safe_relative_path.cpp b/tjp/core/file/validate_safe_relative_path.cpp new file mode 100644 index 0000000..2cfd5eb --- /dev/null +++ b/tjp/core/file/validate_safe_relative_path.cpp @@ -0,0 +1,42 @@ +// TJP COPYRIGHT HEADER + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#include + +#include "validate_safe_relative_path.hpp" + +namespace tjp::core::file { + +TJP_CORE_HEADER_ONLY_INLINE +bool is_safe_relative_path(const StringView &s) +{ + if (s.find("..") == -1) + return false; + + if (s.empty()) + return false; + + if (s[0] == '/') + return false; + + return true; +} + +TJP_CORE_HEADER_ONLY_INLINE +void validate_safe_relative_path(const StringView &s) +{ + if (s.find("..") == -1) + throw Exception { "Path has dots" }; + + if (s.empty()) + throw Exception { "Path is empty" }; + + if (s[0] == '/') + throw Exception { "Path begins with slash" }; +} + + +} // namespace diff --git a/tjp/core/file/validate_safe_relative_path.hpp b/tjp/core/file/validate_safe_relative_path.hpp new file mode 100644 index 0000000..7df55c2 --- /dev/null +++ b/tjp/core/file/validate_safe_relative_path.hpp @@ -0,0 +1,17 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +namespace tjp::core::file { + +bool is_safe_relative_path(const StringView &); +void validate_safe_relative_path(const StringView &); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "validate_safe_relative_path.cpp" +#endif diff --git a/tjp/core/file/write.cpp b/tjp/core/file/write.cpp new file mode 100644 index 0000000..55fbebc --- /dev/null +++ b/tjp/core/file/write.cpp @@ -0,0 +1,66 @@ +// TJP COPYRIGHT HEADER + + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#include + +#include "write.hpp" +#include +#include "../random/file_name.hpp" +#include "times.hpp" + +#include +#include + +namespace tjp::core::file { + +TJP_CORE_HEADER_ONLY_INLINE +bool write(const String &path_, const ContentsView &contents, const WriteOptions &options) +{ + auto path = String(path_); + auto random_ = random::file_name(); + + auto extension = "." + random_; + auto temporary = path + extension; + + std::ofstream file(temporary, std::ios::binary); + if (!file) + return false; + + file.write(contents.data(), contents.size()); + file.close(); + + remove(path.c_str()); + if (rename(temporary.c_str(), path.c_str())) + return false; + + if (options.lastModifiedTime) + setFileLastModifiedTime(path, options.lastModifiedTime); + + return true; +} + +TJP_CORE_HEADER_ONLY_INLINE +void require_write(const String &path, const ContentsView &contents, const WriteOptions &options) +{ + if (!write(path, contents, options)) + throw Exception { "failed to write file " + String(path) }; +} + +TJP_CORE_HEADER_ONLY_INLINE +bool write(const StringView &path, const ContentsView &contents, const WriteOptions &options) +{ + return write(String(path), contents, options); +} + +TJP_CORE_HEADER_ONLY_INLINE +void require_write(const StringView &path, const ContentsView &contents, const WriteOptions &options) +{ + return require_write(String(path), contents, options); +} + + +} // namespace diff --git a/tjp/core/file/write.hpp b/tjp/core/file/write.hpp new file mode 100644 index 0000000..a7bd7a3 --- /dev/null +++ b/tjp/core/file/write.hpp @@ -0,0 +1,25 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Contents.h" +#include +#include + +namespace tjp::core::file { + +struct WriteOptions { + time_t lastModifiedTime = 0; +} ; + +bool write(const String &path, const ContentsView &, const WriteOptions &options = {}); +void require_write(const String &path, const ContentsView &, const WriteOptions &options = {}); + +bool write(const StringView &path, const ContentsView &, const WriteOptions &options = {}); +void require_write(const StringView &path, const ContentsView &, const WriteOptions &options = {}); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "write.cpp" +#endif diff --git a/tjp/core/file/writeSegmentsToFile.cpp b/tjp/core/file/writeSegmentsToFile.cpp new file mode 100644 index 0000000..9913455 --- /dev/null +++ b/tjp/core/file/writeSegmentsToFile.cpp @@ -0,0 +1,53 @@ +// TJP COPYRIGHT HEADER + +#include "writeSegmentsToFile.h" + +#include +#include + +#include +#include + +#include +#include + +#include +#include "../random/Random.h" + +#include + +namespace tjp { +namespace core { +namespace file { + +bool writeSegmentsToFile(const StringView &path_, const StackArray> &segments, const WriteOptions &options) +{ + auto path = String(path_); + auto extension = "." + core::to_string(core::StandardRandom.next()); + auto temporary = path + extension; + + std::ofstream file(temporary, std::ios::binary); + if (!file) + return false; + + for (auto &contents: segments) + file.write((char *)contents.data(), contents.size()); + + file.close(); + + remove(path.c_str()); + if (rename(temporary.c_str(), path.c_str())) + return false; + + if (options.lastModifiedTime) + { + setFileLastModifiedTime(path, options.lastModifiedTime); + } + + return true; +} + + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/file/writeSegmentsToFile.h b/tjp/core/file/writeSegmentsToFile.h new file mode 100644 index 0000000..2a7601f --- /dev/null +++ b/tjp/core/file/writeSegmentsToFile.h @@ -0,0 +1,18 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "File.h" + +#include +#include + +namespace tjp { +namespace core { +namespace file { + +bool writeSegmentsToFile(const StringView &path, const StackArray> &contents, const WriteOptions &options = {}); + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/functions/Dispatch.h b/tjp/core/functions/Dispatch.h new file mode 100644 index 0000000..baa0897 --- /dev/null +++ b/tjp/core/functions/Dispatch.h @@ -0,0 +1,27 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp { +namespace core { + +struct DispatchException; + +struct Variable; + +template +struct StackVariable; + +typedef std::function DispatchFunction; + +template +DispatchFunction asDispatchFunction(std::function &&f); + +struct FunctionDispatchOnParam; +struct FunctionDispatchOnReturn; + + +} // namespace +} // namespace diff --git a/tjp/core/functions/Dispatch.hpp b/tjp/core/functions/Dispatch.hpp new file mode 100644 index 0000000..3689a7c --- /dev/null +++ b/tjp/core/functions/Dispatch.hpp @@ -0,0 +1,199 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Dispatch.h" +#include +#include +#include + +#include +#include + + +namespace tjp { +namespace core { + +struct DispatchException : Exception +{ + typedef Exception Super; + DispatchException(const type_index &what) : Super("DispatchException") {} +}; + +struct Variable +{ + void *v; + type_index type; + + template + Variable(const T &t) : + type(type_id()) + { + v = reinterpret_cast(const_cast(&t)); + } + + template + T &deref () + { + debug_assert(type_id() == type); + return *reinterpret_cast(v); + } + + template + T &&move () + { + debug_assert(type_id() == type); + return std::move(*reinterpret_cast(v)); + } + + template + Variable &operator =(T &&t) + { + deref() = std::move(t); + return *this; + } + + template + Variable &operator =(const T &t) + { + deref() = t; + return *this; + } + +protected: + Variable () : + v(nullptr), + type(type_id()) + {} + + template + void ref(const T &t) + { + type = { type_id() }; + v = reinterpret_cast(const_cast(&t)); + } +} ; + +template +struct StackVariable : Variable +{ + T t; + + StackVariable() + { + ref(t); + } + + operator T() + { + return t; + } + + T &&move() + { + return std::move(t); + } +} ; + +typedef std::function DispatchFunction; + +template +DispatchFunction asDispatchFunction(std::function &&f) +{ + return [f](Variable &result, Variable &¶m) -> void { + auto v = f(param.deref()); + auto &r = result.deref(); + r = v; + } ; +} + +struct FunctionDispatchOnParam +{ + std::map dispatch; + + void execute(Variable &result, Variable &¶m) + { + auto &type = param.type; + + auto i = dispatch.find(type); + if (i == dispatch.end()) + { + debug_assert(false); + throw DispatchException(type); + } + + i->second(result, std::move(param)); + } + + template + T execute(Variable &¶m) + { + StackVariable result; + execute(result, std::move(param)); + return result; + } + + void bind(const type_index &type, DispatchFunction &&f) + { + dispatch[type] = std::move(f); + } + + template + void bind(std::function &&f) + { + bind(type_id(), std::move(f)); + } + + template + void bind(std::function &&f) + { + bind(type_id(), asDispatchFunction(f)); + } +} ; + +struct FunctionDispatchOnReturn +{ + std::map dispatch; + + void execute(Variable &result, Variable &¶m) + { + auto &type = result.type; + auto i = dispatch.find(type); + if (i == dispatch.end()) + { + debug_assert(false); + throw DispatchException(type); + } + + i->second(result, std::move(param)); + } + + template + T execute(Variable &¶m) + { + StackVariable result; + execute(result, std::move(param)); + return result; + } + + void bind(const type_index &type, DispatchFunction &&f) + { + dispatch[type] = f; + } + + template + void bind(std::function &&f) + { + bind(type_id(), std::move(f)); + } + + template + void bind(std::function &&f) + { + bind(type_id(), asDispatchFunction(f)); + } +} ; + + +} // namespace +} // namespace diff --git a/tjp/core/functions/optional_function.h b/tjp/core/functions/optional_function.h new file mode 100644 index 0000000..7acbc05 --- /dev/null +++ b/tjp/core/functions/optional_function.h @@ -0,0 +1,18 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +namespace tjp { +namespace core { + +template +struct optional_function_result; + +template<> +struct optional_function_result; + +template +class optional_function; + +} // namespace +} // namespace diff --git a/tjp/core/functions/optional_function.hpp b/tjp/core/functions/optional_function.hpp new file mode 100644 index 0000000..ea10f8a --- /dev/null +++ b/tjp/core/functions/optional_function.hpp @@ -0,0 +1,132 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "optional_function.h" +#include +#include + +#ifdef SYS_LINUX +//#define HAS_OPTIONAL +#endif + +#ifdef HAS_OPTIONAL +#include +#endif + +namespace tjp { +namespace core { + +#ifdef HAS_OPTIONAL + +template +using optional_type = std::optional; + +#else + +template +struct optional_type { + T t; + + optional_type() {} + optional_type(T &&t_) : t(t_) { hasResult = true; } + + T &operator *() { return t; } + const T &operator *() const { return t; } + + bool hasResult = false; + bool has_result() const { return hasResult; } +} ; + +#endif + +template +struct optional_function_result : optional_type { + typedef optional_type super_type; + + optional_function_result() : + super_type() {} + + optional_function_result(R &&result_) : + super_type(std::forward(result_)) {} + + bool executed() const { return this->has_result(); } +} ; + +template<> +struct optional_function_result +{ + bool executed_; + + optional_function_result(bool &&executed__) : + executed_ (executed__) {} + + bool executed() const { return executed_; } +} ; + +template +class optional_function +{ +public: + typedef std::function function_type; + typedef typename std::function::result_type function_result_type; + typedef optional_function_result result_type; + +protected: + function_type f; + +public: + + template + optional_function operator=(const Fn &f_) + { + f = f_; + return *this; + } + + template + optional_function operator=(Fn &&f_) + { + f = std::forward(f_); + return *this; + } + + operator bool() const + { + return (bool)f; + } + + template< + typename ... Args, + typename FR=function_result_type, + typename std::enable_if::value, FR>::type* = nullptr + > + result_type operator()(Args... args) const + { + if (f) + return { + std::forward(f(args...)) + }; + + return {}; + } + + template< + typename ... Args, + typename FR=function_result_type, + typename std::enable_if::value, FR>::type* = nullptr + > + result_type operator()(Args... args) const + { + if (f) + { + f(args...); + return { true }; + } + + return { false }; + } +} ; + +} // namespace +} // namespace diff --git a/tjp/core/hash/Hash.cpp b/tjp/core/hash/Hash.cpp new file mode 100755 index 0000000..e91174b --- /dev/null +++ b/tjp/core/hash/Hash.cpp @@ -0,0 +1,23 @@ +// TJP COPYRIGHT HEADER + +#include "Hash.h" +#include "fasthash.h" + +namespace tjp { +namespace core { + +u32 hash32(const u8 *begin, size_t size) +{ + static u32 seed = 0x026f23a; + return fasthash32(begin, size, seed); +} + +u64 hash64(const u8 *begin, size_t size) +{ + static u32 seed = 0x026f23a; + return fasthash64(begin, size, seed); +} + +} // namespace +} // namespace + diff --git a/tjp/core/hash/Hash.h b/tjp/core/hash/Hash.h new file mode 100755 index 0000000..f3570ef --- /dev/null +++ b/tjp/core/hash/Hash.h @@ -0,0 +1,58 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp { +namespace core { + +u32 hash32(const u8 *begin, size_t size); +u64 hash64(const u8 *begin, size_t size); + +template +T hash(const u8 *begin, size_t size); + +template +T hash_of(const U &u) +{ + auto *begin_ = u.data(); + auto *end_ = begin_ + u.size(); + + auto *begin = (u8 *)begin_; + auto *end = (u8 *)end_; + + return hash(begin, (end - begin)); +} + +template +T hash_of_value(const U &u) +{ + auto *begin_ = &u; + auto *end_ = begin_ + sizeof(u); + + auto *begin = (u8 *)begin_; + auto *end = (u8 *)end_; + + return hash(begin, (end - begin)); +} + +template<> +inline +u32 hash(const u8 *begin, size_t size) { return hash32(begin, size); } + +template<> +inline +s32 hash(const u8 *begin, size_t size) { return hash32(begin, size); } + +template<> +inline +u64 hash(const u8 *begin, size_t size) { return hash64(begin, size); } + +template<> +inline +s64 hash(const u8 *begin, size_t size) { return hash64(begin, size); } + +} // namespace +} // namespace + diff --git a/tjp/core/hash/fasthash.cpp b/tjp/core/hash/fasthash.cpp new file mode 100644 index 0000000..87bd8f4 --- /dev/null +++ b/tjp/core/hash/fasthash.cpp @@ -0,0 +1,84 @@ +// TJP COPYRIGHT HEADER + +/* The MIT License + + Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation + files (the "Software"), to deal in the Software without + restriction, including without limitation the rights to use, copy, + modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + +#include "fasthash.h" + +// Compression function for Merkle-Damgard construction. +// This function is generated using the framework provided. +static inline uint64_t mix(uint64_t h) { + h ^= h >> 23; + h *= 0x2127599bf4325c37ULL; + h ^= h >> 47; + return h; +} + +// security: if the system allows empty keys (len=3) the seed is exposed, the reverse of mix. +// objsize: 0-1fd: 509 +uint64_t fasthash64(const void *buf, size_t len, uint64_t seed) +{ + const uint64_t m = 0x880355f21e6d1965ULL; + const uint64_t *pos = (const uint64_t *)buf; + const uint64_t *end = pos + (len / 8); + const unsigned char *pos2; + uint64_t h = seed ^ (len * m); + uint64_t v; + + while (pos != end) { + v = *pos++; + h ^= mix(v); + h *= m; + } + + pos2 = (const unsigned char*)pos; + v = 0; + + switch (len & 7) { + case 7: v ^= (uint64_t)pos2[6] << 48; + case 6: v ^= (uint64_t)pos2[5] << 40; + case 5: v ^= (uint64_t)pos2[4] << 32; + case 4: v ^= (uint64_t)pos2[3] << 24; + case 3: v ^= (uint64_t)pos2[2] << 16; + case 2: v ^= (uint64_t)pos2[1] << 8; + case 1: v ^= (uint64_t)pos2[0]; + h ^= mix(v); + h *= m; + } + + return mix(h); +} + +// objsize: 0-236: 566 +uint32_t fasthash32(const void *buf, size_t len, uint32_t seed) +{ + // the following trick converts the 64-bit hashcode to Fermat + // residue, which shall retain information from both the higher + // and lower parts of hashcode. + uint64_t h = fasthash64(buf, len, seed); + auto r = h - (h >> 32); + + return (uint32_t)r; +} diff --git a/tjp/core/hash/fasthash.h b/tjp/core/hash/fasthash.h new file mode 100644 index 0000000..e2d0256 --- /dev/null +++ b/tjp/core/hash/fasthash.h @@ -0,0 +1,58 @@ +// TJP COPYRIGHT HEADER + +/* The MIT License + + Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com) + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation + files (the "Software"), to deal in the Software without + restriction, including without limitation the rights to use, copy, + modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + +#ifndef _FASTHASH_H +#define _FASTHASH_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * fasthash32 - 32-bit implementation of fasthash + * @buf: data buffer + * @len: data size + * @seed: the seed + */ + uint32_t fasthash32(const void *buf, size_t len, uint32_t seed); + +/** + * fasthash64 - 64-bit implementation of fasthash + * @buf: data buffer + * @len: data size + * @seed: the seed + */ + uint64_t fasthash64(const void *buf, size_t len, uint64_t seed); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/tjp/core/http/Component.h b/tjp/core/http/Component.h new file mode 100755 index 0000000..f2b916e --- /dev/null +++ b/tjp/core/http/Component.h @@ -0,0 +1,9 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +namespace tjp::core::http { + +struct Component; + +} // namespace diff --git a/tjp/core/http/Component.hpp b/tjp/core/http/Component.hpp new file mode 100755 index 0000000..794fee5 --- /dev/null +++ b/tjp/core/http/Component.hpp @@ -0,0 +1,25 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Component.h" +#include "Method.h" +#include "Types.h" + +#include "Server.h" +#include + +namespace tjp::core::http { + +struct Component +{ + using Server = http::Server; + using method = http::method; + + virtual void use(Server &, const String &urlPrefix) = 0; + virtual void onBegin() {} + virtual void onEnd() {} +} ; + +} // namespace + diff --git a/tjp/core/http/Method.h b/tjp/core/http/Method.h new file mode 100755 index 0000000..00f1eb9 --- /dev/null +++ b/tjp/core/http/Method.h @@ -0,0 +1,21 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp::core::http { + +using Method = String; + +struct method +{ + static constexpr auto GET = "GET"; + static constexpr auto PUT = "PUT"; + static constexpr auto POST = "POST"; + static constexpr auto HEAD = "HEAD"; + static constexpr auto INFO = "INFO"; + static constexpr auto OPTIONS = "OPTIONS"; +} ; + +} // namespace diff --git a/tjp/core/http/Method.hpp b/tjp/core/http/Method.hpp new file mode 100755 index 0000000..526624d --- /dev/null +++ b/tjp/core/http/Method.hpp @@ -0,0 +1,9 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Method.h" + +namespace tjp::core::http { + +} // namespace diff --git a/tjp/core/http/Server.cpp b/tjp/core/http/Server.cpp new file mode 100755 index 0000000..95cabf9 --- /dev/null +++ b/tjp/core/http/Server.cpp @@ -0,0 +1,9 @@ +// TJP COPYRIGHT HEADER + +#if defined(TJP_CORE_HEADER_ONLY) + #pragma once +#endif + +namespace tjp::core::http { + +} diff --git a/tjp/core/http/Server.h b/tjp/core/http/Server.h new file mode 100755 index 0000000..7832089 --- /dev/null +++ b/tjp/core/http/Server.h @@ -0,0 +1,10 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +namespace tjp::core::http { + +struct Server; +struct Component; + +} // namespace diff --git a/tjp/core/http/Server.hpp b/tjp/core/http/Server.hpp new file mode 100755 index 0000000..54bba38 --- /dev/null +++ b/tjp/core/http/Server.hpp @@ -0,0 +1,96 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Server.h" +#include "Component.h" +#include "Types.hpp" +#include + +#include +#include +#include +#include + +#include "as_future.hpp" + +namespace tjp::core::http { + +struct Server +{ + virtual ~Server() {} + + virtual void add( + const StrongPtr &component, + const String &urlPrefix = {} + ) = 0; + + template + void addAll(Args && ...args) + { + (add(std::forward(args)), ...); + } + + virtual void setNumThreads(size_t) = 0; + virtual void start(const String &bindTo, int port) = 0; + virtual void run(const String &bindTo, int port) = 0; + virtual void stop() = 0; + + virtual bool isRunning() = 0; + + virtual void bind( + const Method &method, + Matcher &&matcher, + FutureHandler && + ) = 0; + + void bind( + const Method &method, + const Pattern &pattern, + FutureHandler &&handler + ) + { + bind( + method, + match_regex(String(pattern)), + std::move(handler) + ); + } + + template + void bind_with( + const Method &method, + Matcher matcher, + F &&f + ) + { + bind( + method, + std::move(matcher), + as_future( + std::forward(f) + ) + ); + } + + template + void bind_with( + const Method &method, + const Pattern &pattern, + F &&f + ) + { + bind_with( + method, + match_regex(String(pattern)), + std::forward(f) + ); + } + +}; + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "Server.cpp" +#endif diff --git a/tjp/core/http/Types.h b/tjp/core/http/Types.h new file mode 100755 index 0000000..7e0bee5 --- /dev/null +++ b/tjp/core/http/Types.h @@ -0,0 +1,43 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Method.h" +#include +#include +#include +#include +#include +#include + +#include + +namespace tjp::core::http { + +using Status = int; + +struct DataSink; +struct PipeState; + +struct HeaderView; +struct Header; + +using Headers = Vector
; +using Matches = StackArray; + +struct Request; +struct Response; + +struct Result; +using BodyType = Result; + +using FutureHandler = core::Function(const Request &)>; +using Matcher = core::Function(StringView)>; +using Pattern = StringView; + +Matcher match_exact(const String &value); +Matcher match_prefix(const String &prefix); + +Matcher match_regex(const String &pattern); + +} // namespace diff --git a/tjp/core/http/Types.hpp b/tjp/core/http/Types.hpp new file mode 100755 index 0000000..50ba7a5 --- /dev/null +++ b/tjp/core/http/Types.hpp @@ -0,0 +1,385 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Types.h" +#include "Method.hpp" +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include "../internet/url_param_decode.hpp" + +namespace tjp::core::http { + +using Status = int; + +struct DataSink +{ + std::any preserve; + + virtual ~DataSink() = default; + virtual void write(const char *data, size_t size) = 0; + virtual void done() = 0; +} ; + +struct PipeState : + std::enable_shared_from_this +{ + struct Sink : DataSink + { + std::weak_ptr state; + + explicit Sink(std::weak_ptr state_) : + state(std::move(state_)) + {} + + void write(const char *data, size_t size) override + { + if (auto state_ = state.lock()) + state_->onData(data, size); + } + + void done() override + { + if (auto state_ = state.lock()) + state_->onDone(); + } + } ; + + static std::shared_ptr make(Optional requestSize = {}) + { + auto state = std::shared_ptr(new PipeState(requestSize)); + state->sink_ = std::make_shared(state); + return state; + } + + std::shared_ptr sink() const + { + return sink_; + } + + void setWake(core::Function wake) + { + std::lock_guard lock(mutex); + wake_ = std::move(wake); + } + + std::shared_ptr popChunk() + { + std::lock_guard lock(mutex); + if (chunks.empty()) + return {}; + + auto out = chunks.front(); + chunks.pop_front(); + return out; + } + + bool popOrWait(String &chunk, bool &isEnded) + { + std::unique_lock lock(mutex); + cv.wait(lock, [&]() { + return !chunks.empty() || ended; + }); + + if (!chunks.empty()) + { + chunk = *chunks.front(); + chunks.pop_front(); + isEnded = false; + return true; + } + + isEnded = ended; + return false; + } + + bool isEnded() const + { + std::lock_guard lock(mutex); + return ended; + } + + Optional size() const + { + std::lock_guard lock(mutex); + return requestSize; + } + +private: + explicit PipeState(Optional requestSize_) : + requestSize(std::move(requestSize_)) + {} + + void onData(const char *data, size_t size) + { + if (size == 0) + return; + + { + std::lock_guard lock(mutex); + if (ended) + return; + + chunks.emplace_back(std::make_shared(data, size)); + } + + cv.notify_one(); + notifyWake(); + } + + void onDone() + { + { + std::lock_guard lock(mutex); + ended = true; + } + + cv.notify_all(); + notifyWake(); + } + + void notifyWake() + { + core::Function wake; + { + std::lock_guard lock(mutex); + wake = wake_; + } + + if (wake) + wake(); + } + + Optional requestSize; + std::shared_ptr sink_; + + mutable std::mutex mutex; + std::condition_variable cv; + std::deque> chunks; + bool ended = false; + core::Function wake_; +} ; + +struct HeaderView { + StringView key; + StringView value; +} ; + +struct Header { + String key; + String value; +} ; + +using Headers = Vector
; +using Matches = StackArray; + +inline +Optional path_param_(const StringView &path, const StringView ¶m) +{ + auto q = path.find('?'); + if (q == path.npos) + return {}; + + auto l = q; + while (true) + { + auto p = path.find(param, l); + if (p == path.npos) + return {}; + + auto before = path[p-1]; + if (before == '?' || before == '&') + { + // if the path ends with ¶m instead of ¶m= + if (path.size() <= p + param.size()) + return { StringView() }; + + auto b = p + param.size(); + auto after = path[b]; + if (after == '=' || after == '&') + { + if (after == '=') + b++; + + auto e = path.find('&', b); + if (e == path.npos) + e = path.size(); + + return path.substr(b, e - b); + } + } + + l = p + param.size(); + } + + return {}; +} + +inline +Optional path_param(const StringView &path, const StringView ¶m) +{ + if (auto encoded_param = path_param_(path, param)) + return internet::url_param_decode(*encoded_param); + + return {}; +} + +struct Request +{ + StringView method; + StringView target; + Vector headers; + Matches matches; + + StringView body; + + Optional header(const StringView &key_) const + { + for (auto &[key, value]: headers) + if (core::equal_case(key, key_)) + return value; + + return {}; + } + + Optional param(const StringView &key_) const + { + return path_param(target, key_); + } +}; + +struct Response +{ + Status status = 200; + Vector
headers; + + bool hasHeader(const StringView &key) + { + for (auto &[key_, _]: headers) + if (core::equal_case(key, key_)) + return true; + + return false; + } + + void header(const String &key, const String &value) + { + headers.emplace_back(Header {key, value}); + } + + void content_length(size_t size) + { + header("Content-Length", std::to_string(size)); + } + + virtual void flushHeaders(const StringView &method) = 0; + virtual void write(const StringView &) = 0; + virtual std::shared_ptr stream() = 0; + + std::shared_ptr stream(size_t size) + { + content_length(size); + return stream(); + } +} ; + +struct Result +{ + Optional body; + Optional type; + Optional status; + Vector
headers; + + std::shared_ptr stream; + std::shared_ptr streamOwner; + Optional streamSize; + + std::shared_ptr stream_body() + { + streamOwner = PipeState::make(); + streamSize = {}; + stream = streamOwner->sink(); + return stream; + } + + std::shared_ptr stream_body(size_t size) + { + streamOwner = PipeState::make(size); + streamSize = size; + stream = streamOwner->sink(); + return stream; + } +} ; + +using FutureHandler = core::Function(const Request &)>; +using Matcher = core::Function(StringView)>; +using Pattern = StringView; + +inline Matcher match_exact(const String &value) +{ + auto value_ = String(value); + return [value_ = std::move(value_)](StringView url) -> Optional { + if (url == value_) + return Matches({ url }); + + return {}; + }; +} + +inline Matcher match_prefix(const String &prefix) +{ + auto prefix_ = String(prefix); + return [prefix_ = std::move(prefix_)](StringView url) -> Optional { + if (core::starts_with(url, prefix_)) + return Matches { url.substr(prefix_.size()) }; + + return {}; + }; +} + +inline Matcher match_regex(const String &pattern) +{ + auto regex = std::make_shared(String(pattern)); + return [regex = std::move(regex), pattern](StringView url) -> Optional { + std::string s(url); + std::match_results m; + + if (!std::regex_match(s, m, *regex)) + return {}; + + Matches out; + + for (size_t i = 0; i < m.size(); ++i) { + auto pos = m.position(i); + auto len = m.length(i); + out.emplace_back(url.data() + pos, len); + } + + return out; + }; +} +using BodyType = Result; + +} // namespace diff --git a/tjp/core/http/as_future.hpp b/tjp/core/http/as_future.hpp new file mode 100755 index 0000000..675d925 --- /dev/null +++ b/tjp/core/http/as_future.hpp @@ -0,0 +1,21 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Types.hpp" +#include "future_response.hpp" +#include + +namespace tjp::core::http { + +template +auto as_future(F &&f) +{ + return + [f=std::move(f)](const Request& req) + { + return future_with(f, req); + }; +} + +} // namespace diff --git a/tjp/core/http/beast/Server.cpp b/tjp/core/http/beast/Server.cpp new file mode 100644 index 0000000..b552687 --- /dev/null +++ b/tjp/core/http/beast/Server.cpp @@ -0,0 +1,985 @@ +// TJP COPYRIGHT HEADER + +#if defined(TJP_CORE_HEADER_ONLY) + #pragma once +#endif +#include + +#include "Server.hpp" + +#include "beast.hpp" + +#include +#include +#include + +#include + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace tjp::core::http::beast { + +namespace net = boost::asio; +namespace beast_ = boost::beast; +namespace http_ = beast_::http; +using tcp = net::ip::tcp; + +struct RequestAdapter : + http::Request, + std::enable_shared_from_this +{ + String methodStorage; + String targetStorage; + String bodyStorage; + Vector
headerStorage; + + RequestAdapter(const http_::request &in) + { + methodStorage = String(in.method_string()); + targetStorage = String(in.target()); + bodyStorage = in.body(); + + method = methodStorage; + target = targetStorage; + body = bodyStorage; + + auto headerCount = static_cast(std::distance(in.base().begin(), in.base().end())); + headerStorage.reserve(headerCount); + headers.reserve(headerCount); + for (const auto &field: in.base()) + { + headerStorage.emplace_back(Header { + String(field.name_string()), + String(field.value()) + }); + auto &header = headerStorage.back(); + headers.emplace_back(HeaderView { + header.key, + header.value + }); + } + } +} ; + +using StreamState = http::PipeState; + +struct ResponseAdapter : + http::Response, + std::enable_shared_from_this +{ + String body; + bool deferred = false; + std::shared_ptr requestHolder; + std::shared_ptr streamState; + std::shared_ptr)>> onReady; + + void notifyReady() + { + if (onReady && *onReady) + (*onReady)(shared_from_this()); + } + + void flushHeaders(const StringView &) override + { + } + + void write(const StringView &data) override + { + body = String(data); + } + + std::shared_ptr stream() override + { + Optional size; + for (const auto &header: headers) + { + if (header.key != "Content-Length") + continue; + + try + { + size = static_cast(std::stoull(String(header.value))); + } + catch (...) + { + // ignore malformed values and keep scanning + } + } + + deferred = true; + streamState = StreamState::make(size); + return streamState->sink(); + } + + http_::response toBeast( + unsigned version, + bool keepAlive + ) + { + http_::response out { + static_cast(status), + version + }; + + std::optional contentType; + std::optional contentLength; + + out.keep_alive(keepAlive); + for (auto &header: headers) + { + if (header.key == "Content-Length") + { + contentLength = header.value; + continue; + } + + if (header.key == "Content-Type") + { + contentType = header.value; + continue; + } + + out.set(String(header.key), String(header.value)); + } + + if (!contentType) + contentType = "text/plain"; + + if (contentType) + out.set(http_::field::content_type, *contentType); + + out.body() = body; + + if (contentLength) + out.set(http_::field::content_length, *contentLength); + else + out.prepare_payload(); + + return out; + } +} ; + +static Optional readContentLengthFromHeaders(const Vector
&headers) +{ + for (const auto &header: headers) + { + if (header.key != "Content-Length") + continue; + + try + { + return static_cast(std::stoull(String(header.value))); + } + catch (...) + { + // Ignore malformed content-length and keep scanning. + } + } + + return {}; +} + +static void applyResult(ResponseAdapter &response, Result result) +{ + response.status = result.status.value_or(200); + + for (auto &[key, value]: result.headers) + response.header(key, value); + + if (result.type) + response.header("Content-Type", *result.type); + + auto streamSize = result.streamSize; + if (!streamSize) + streamSize = readContentLengthFromHeaders(response.headers); + if (streamSize && !response.hasHeader("Content-Length")) + response.content_length(*streamSize); + + if (result.streamOwner) + { + response.deferred = true; + response.streamState = std::move(result.streamOwner); + return; + } + + if (result.stream) + { + response.status = 500; + response.header("Content-Type", "text/plain"); + response.write("Invalid stream owner"); + return; + } + + response.write(result.body.value_or(String {})); +} + +struct Server::I +{ + struct Route + { + Method method; + Matcher matcher; + FutureHandler handler; + } ; + + struct Session; + struct Listener; + + net::io_context io; + std::unique_ptr acceptor; + std::shared_ptr listener; + using WorkGuard = net::executor_work_guard; + std::unique_ptr workGuard; + + std::mutex routesMutex; + Vector routes; + Vector> components; + Futures futures; + + std::vector threads; + std::optional numThreads; + + std::atomic running = false; + std::atomic listening = false; + + size_t threadCount() const + { + if (numThreads && *numThreads > 0) + return *numThreads; + + auto n = std::thread::hardware_concurrency(); + return n == 0 ? 1 : n; + } + + bool findHandler( + Request &request, + FutureHandler &out + ) + { + std::lock_guard lock(routesMutex); + for (const auto &route: routes) + { + if (route.method != request.method) + continue; + + if (route.matcher) + if (auto matches = route.matcher(request.target)) + { + request.matches = *matches; + out = route.handler; + return true; + } + } + + return false; + } +}; + +struct Server::I::Session : std::enable_shared_from_this +{ + I *i; + beast_::tcp_stream stream; + beast_::flat_buffer buffer; + http_::request request; + std::shared_ptr activeStreamResponse; + std::optional> activeStreamHeaderMessage; + std::optional> activeStreamHeader; + std::shared_ptr activeStreamPayload; + std::shared_ptr activeStreamChunkWire; + size_t activeStreamBytesWritten = 0; + bool activeStreamHeaderSent = false; + bool activeStreamWriteInFlight = false; + bool activeStreamChunked = false; + bool activeStreamHeadOnly = false; + + Session(I *i_, tcp::socket &&socket) : + i(i_), + stream(std::move(socket)) + {} + + void run() + { + doRead(); + } + + void doRead() + { + request = {}; + http_::async_read( + stream, + buffer, + request, + [self = shared_from_this()](beast_::error_code ec, size_t bytes) { + self->onRead(ec, bytes); + } + ); + } + + void onRead(beast_::error_code ec, size_t) + { + if (ec == http_::error::end_of_stream) + { + doClose(); + return; + } + + if (ec) + { + doClose(); + return; + } + + handleRequest(); + } + + void handleRequest() + { + auto request_ = std::make_shared(request); + auto response = std::make_shared(); + response->requestHolder = request_; + response->onReady = std::make_shared)>>( + [self = shared_from_this()](std::shared_ptr response_) { + net::dispatch( + self->stream.get_executor(), + [self, response_]() { + if (response_->streamState) + self->startStreaming(response_); + else + self->send(response_->toBeast(self->request.version(), self->request.keep_alive())); + } + ); + } + ); + + try + { + FutureHandler handler; + if (i->findHandler(*request_, handler)) + { + response->deferred = true; + auto future = handler(*request_).then([response, request_](auto &&future_) { + (void)request_; + try + { + applyResult(*response, future_.get()); + } + catch (Result &result) + { + applyResult(*response, Result(result)); + } + catch (Exception &e) + { + response->status = 400; + response->header("Content-Type", "text/plain"); + response->write("Error: " + e.what()); + } + catch (...) + { + response->status = 400; + response->header("Content-Type", "text/plain"); + response->write("Error"); + } + + response->notifyReady(); + }); + + i->futures.add(future); + } + else + { + response->status = 404; + response->header("Content-Type", "text/plain"); + response->write("Not found"); + response->notifyReady(); + } + } + catch (Result &result) + { + applyResult(*response, Result(result)); + response->notifyReady(); + } + catch (Exception &e) + { + response->status = 400; + response->header("Content-Type", "text/plain"); + response->write("Error: " + e.what()); + response->notifyReady(); + } + catch (...) + { + response->status = 400; + response->header("Content-Type", "text/plain"); + response->write("Error"); + response->notifyReady(); + } + } + + void send(http_::response &&response) + { + auto keepAlive = response.keep_alive(); + auto sharedResponse = std::make_shared>(std::move(response)); + http_::async_write( + stream, + *sharedResponse, + [self = shared_from_this(), sharedResponse, keepAlive](beast_::error_code ec, size_t bytes) { + self->onWrite(keepAlive, ec, bytes); + } + ); + } + + void onWrite(bool keepAlive, beast_::error_code ec, size_t) + { + if (ec) + { + doClose(); + return; + } + + if (!keepAlive) + { + doClose(); + return; + } + + doRead(); + } + + void clearStreamingState() + { + activeStreamResponse.reset(); + activeStreamHeaderMessage.reset(); + activeStreamHeader.reset(); + activeStreamPayload.reset(); + activeStreamChunkWire.reset(); + activeStreamBytesWritten = 0; + activeStreamHeaderSent = false; + activeStreamWriteInFlight = false; + activeStreamChunked = false; + activeStreamHeadOnly = false; + } + + void startStreaming(const std::shared_ptr &response) + { + if (!response || !response->streamState) + return; + + if (activeStreamResponse) + return; + + activeStreamResponse = response; + activeStreamHeaderSent = false; + activeStreamWriteInFlight = false; + activeStreamHeaderMessage.reset(); + activeStreamHeader.reset(); + activeStreamPayload.reset(); + activeStreamChunkWire.reset(); + activeStreamBytesWritten = 0; + activeStreamHeadOnly = request.method() == http_::verb::head; + activeStreamChunked = !response->streamState->size().has_value() && !activeStreamHeadOnly; + + response->streamState->setWake([self = shared_from_this()]() { + net::dispatch( + self->stream.get_executor(), + [self]() { + self->pumpStreaming(); + } + ); + }); + + pumpStreaming(); + } + + void pumpStreaming() + { + if (!activeStreamResponse || !activeStreamResponse->streamState) + return; + + if (activeStreamWriteInFlight) + return; + + auto response = activeStreamResponse; + auto state = response->streamState; + auto knownSize = state->size(); + + if (!activeStreamHeaderSent) + { + http_::response header { + static_cast(response->status), + request.version() + }; + + header.keep_alive(request.keep_alive()); + if (knownSize) + header.content_length(*knownSize); + else + if (activeStreamChunked) + header.chunked(true); + + std::optional contentType; + for (auto &header_: response->headers) + { + if (header_.key == "Content-Length") + continue; + + if (header_.key == "Transfer-Encoding") + continue; + + if (header_.key == "Content-Type") + { + contentType = header_.value; + continue; + } + + header.set(String(header_.key), String(header_.value)); + } + + if (!contentType) + contentType = "text/plain"; + + if (contentType) + header.set(http_::field::content_type, *contentType); + + activeStreamHeaderMessage.emplace(std::move(header)); + activeStreamHeader.emplace(*activeStreamHeaderMessage); + activeStreamWriteInFlight = true; + http_::async_write_header( + stream, + *activeStreamHeader, + [self = shared_from_this()](beast_::error_code ec, size_t) { + self->onStreamHeaderWrite(ec); + } + ); + return; + } + + if (activeStreamHeadOnly) + { + auto keepAlive = request.keep_alive(); + clearStreamingState(); + if (keepAlive) + doRead(); + else + doClose(); + return; + } + + if (!activeStreamChunked) + { + if (!knownSize) + { + clearStreamingState(); + doClose(); + return; + } + + if (activeStreamBytesWritten >= *knownSize) + { + auto keepAlive = request.keep_alive(); + clearStreamingState(); + if (keepAlive) + doRead(); + else + doClose(); + return; + } + + auto chunk = state->popChunk(); + if (chunk) + { + auto remaining = *knownSize - activeStreamBytesWritten; + if (chunk->size() > remaining) + chunk->resize(remaining); + + activeStreamPayload = chunk; + activeStreamWriteInFlight = true; + net::async_write( + stream, + net::buffer(*activeStreamPayload), + [self = shared_from_this(), payload = activeStreamPayload](beast_::error_code ec, size_t bytes) { + (void)payload; + self->onStreamChunkWrite(ec, bytes); + } + ); + return; + } + + if (state->isEnded()) + { + // Producer ended early without delivering promised size. + clearStreamingState(); + doClose(); + } + return; + } + + auto chunk = state->popChunk(); + if (chunk) + { + char sizeHex[32] = {}; + auto length = std::snprintf(sizeHex, sizeof(sizeHex), "%zx", chunk->size()); + if (length < 0) + { + clearStreamingState(); + doClose(); + return; + } + activeStreamChunkWire = std::make_shared(); + activeStreamChunkWire->append(sizeHex, static_cast(length)); + activeStreamChunkWire->append("\r\n", 2); + activeStreamChunkWire->append(*chunk); + activeStreamChunkWire->append("\r\n", 2); + activeStreamWriteInFlight = true; + net::async_write( + stream, + net::buffer(*activeStreamChunkWire), + [self = shared_from_this(), chunkWire = activeStreamChunkWire](beast_::error_code ec, size_t) { + (void)chunkWire; + self->onStreamChunkedWrite(ec); + } + ); + return; + } + + if (state->isEnded()) + { + activeStreamChunkWire = std::make_shared("0\r\n\r\n"); + activeStreamWriteInFlight = true; + net::async_write( + stream, + net::buffer(*activeStreamChunkWire), + [self = shared_from_this(), chunkWire = activeStreamChunkWire](beast_::error_code ec, size_t) { + (void)chunkWire; + self->onStreamChunkedDoneWrite(ec); + } + ); + } + } + + void onStreamHeaderWrite(beast_::error_code ec) + { + activeStreamWriteInFlight = false; + activeStreamHeaderSent = true; + activeStreamHeaderMessage.reset(); + activeStreamHeader.reset(); + if (ec) + { + clearStreamingState(); + doClose(); + return; + } + + pumpStreaming(); + } + + void onStreamChunkWrite(beast_::error_code ec, size_t bytes) + { + activeStreamWriteInFlight = false; + activeStreamBytesWritten += bytes; + activeStreamPayload.reset(); + if (ec) + { + clearStreamingState(); + doClose(); + return; + } + + pumpStreaming(); + } + + void onStreamChunkedWrite(beast_::error_code ec) + { + activeStreamWriteInFlight = false; + activeStreamChunkWire.reset(); + if (ec) + { + clearStreamingState(); + doClose(); + return; + } + + pumpStreaming(); + } + + void onStreamChunkedDoneWrite(beast_::error_code ec) + { + activeStreamWriteInFlight = false; + activeStreamChunkWire.reset(); + if (ec) + { + clearStreamingState(); + doClose(); + return; + } + + auto keepAlive = request.keep_alive(); + clearStreamingState(); + if (keepAlive) + doRead(); + else + doClose(); + } + + void doClose() + { + clearStreamingState(); + beast_::error_code ec; + stream.socket().shutdown(tcp::socket::shutdown_send, ec); + stream.socket().close(ec); + } +}; + +struct Server::I::Listener : std::enable_shared_from_this +{ + I *i; + + Listener(I *i_) : + i(i_) + {} + + void run() + { + doAccept(); + } + + void doAccept() + { + if (!i->acceptor || !i->running) + return; + + i->acceptor->async_accept( + net::make_strand(i->io), + [self = shared_from_this()](beast_::error_code ec, tcp::socket socket) { + self->onAccept(ec, std::move(socket)); + } + ); + } + + void onAccept(beast_::error_code ec, tcp::socket socket) + { + if (!i->running) + return; + + if (!ec) + std::make_shared(i, std::move(socket))->run(); + else + if (ec == net::error::operation_aborted) + return; + + doAccept(); + } +}; + +static tcp::endpoint toEndpoint(const String &bindTo, int port) +{ + beast_::error_code ec; + auto address = net::ip::make_address(bindTo, ec); + if (ec) + throw Exception { "Invalid bind address" }; + + if (port < 0 || port > 65535) + throw Exception { "Invalid port" }; + + return tcp::endpoint { + address, + static_cast(port) + }; +} + +static void throwIfError(beast_::error_code ec, const char *message) +{ + if (ec) + throw Exception { message }; +} + +TJP_CORE_HEADER_ONLY_INLINE +Server::Server() +{ + i = strong(); +} + +TJP_CORE_HEADER_ONLY_INLINE +Server::~Server() +{ + stop(); +} + +TJP_CORE_HEADER_ONLY_INLINE +void Server::add(const StrongPtr &component, const String &urlPrefix) +{ + i->components.emplace_back(component); + component->use(*this, urlPrefix); +} + +TJP_CORE_HEADER_ONLY_INLINE +void Server::setNumThreads(size_t n) +{ + i->numThreads = n == 0 ? 1 : n; +} + +TJP_CORE_HEADER_ONLY_INLINE +void Server::start(const String &bindTo, int port) +{ + if (i->running.exchange(true)) + throw Exception { "Webserver already running" }; + + try + { + i->io.restart(); + i->acceptor = std::make_unique(i->io); + i->workGuard = std::make_unique(i->io.get_executor()); + + auto endpoint = toEndpoint(bindTo, port); + beast_::error_code ec; + i->acceptor->open(endpoint.protocol(), ec); + throwIfError(ec, "Could not open socket"); + + i->acceptor->set_option(net::socket_base::reuse_address(true), ec); + throwIfError(ec, "Could not set socket options"); + + i->acceptor->bind(endpoint, ec); + throwIfError(ec, "Could not bind to endpoint"); + + i->acceptor->listen(net::socket_base::max_listen_connections, ec); + throwIfError(ec, "Could not listen"); + + i->listener = std::make_shared(i.get()); + i->listener->run(); + i->listening = true; + + auto count = i->threadCount(); + i->threads.reserve(count); + for (size_t idx = 0; idx < count; ++idx) + { + i->threads.emplace_back([state = i.get()]() { + state->io.run(); + }); + } + } + catch (...) + { + stop(); + throw; + } +} + +TJP_CORE_HEADER_ONLY_INLINE +void Server::run(const String &bindTo, int port) +{ + if (i->running.exchange(true)) + throw Exception { "Webserver already running" }; + + try + { + i->io.restart(); + i->acceptor = std::make_unique(i->io); + i->workGuard = std::make_unique(i->io.get_executor()); + + auto endpoint = toEndpoint(bindTo, port); + beast_::error_code ec; + i->acceptor->open(endpoint.protocol(), ec); + throwIfError(ec, "Could not open socket"); + + i->acceptor->set_option(net::socket_base::reuse_address(true), ec); + throwIfError(ec, "Could not set socket options"); + + i->acceptor->bind(endpoint, ec); + throwIfError(ec, "Could not bind to endpoint"); + + i->acceptor->listen(net::socket_base::max_listen_connections, ec); + throwIfError(ec, "Could not listen"); + + i->listener = std::make_shared(i.get()); + i->listener->run(); + i->listening = true; + + auto count = i->threadCount(); + if (count > 1) + { + i->threads.reserve(count - 1); + for (size_t idx = 1; idx < count; ++idx) + { + i->threads.emplace_back([state = i.get()]() { + state->io.run(); + }); + } + } + + i->io.run(); + + for (auto &thread: i->threads) + if (thread.joinable()) + thread.join(); + + i->threads.clear(); + i->listener.reset(); + i->acceptor.reset(); + i->workGuard.reset(); + i->listening = false; + i->running = false; + } + catch (...) + { + stop(); + throw; + } +} + +TJP_CORE_HEADER_ONLY_INLINE +void Server::stop() +{ + if (!i->running.exchange(false)) + return; + + i->listening = false; + + if (i->acceptor) + { + beast_::error_code ec; + i->acceptor->cancel(ec); + i->acceptor->close(ec); + } + + i->listener.reset(); + i->workGuard.reset(); + i->io.stop(); + + for (auto &thread: i->threads) + if (thread.joinable()) + thread.join(); + + i->threads.clear(); + i->acceptor.reset(); + i->io.restart(); +} + +TJP_CORE_HEADER_ONLY_INLINE +bool Server::isRunning() +{ + return i->running && i->listening; +} + +TJP_CORE_HEADER_ONLY_INLINE +void Server::bind(const Method &method, Matcher &&matcher, FutureHandler &&handler) +{ + if (!matcher) + throw Exception { "Invalid route matcher" }; + + std::lock_guard lock(i->routesMutex); + i->routes.emplace_back(I::Route { + method, + std::move(matcher), + std::move(handler) + }); +} + +} // namespace diff --git a/tjp/core/http/beast/Server.h b/tjp/core/http/beast/Server.h new file mode 100644 index 0000000..39aa495 --- /dev/null +++ b/tjp/core/http/beast/Server.h @@ -0,0 +1,9 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +namespace tjp::core::http::beast { + +struct Server; + +} // namespace diff --git a/tjp/core/http/beast/Server.hpp b/tjp/core/http/beast/Server.hpp new file mode 100644 index 0000000..0d4572e --- /dev/null +++ b/tjp/core/http/beast/Server.hpp @@ -0,0 +1,50 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Server.h" +#include "../Server.hpp" +#include "../Component.hpp" +#include + +#include +#include + +namespace tjp::core::http::beast { + +struct Server : http::Server +{ + using Super = http::Server; + using Super::bind; + + struct I; + StrongPtr i; + + + Server(); + ~Server(); + + void add( + const StrongPtr &component, + const String &urlPrefix = {} + ) override; + + void setNumThreads(size_t) override; + void start(const String &bindTo, int port) override; + void run(const String &bindTo, int port) override; + void stop() override; + + bool isRunning() override; + + void bind( + const Method &method, + Matcher &&matcher, + FutureHandler && + ) override; +}; + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "Server.cpp" +#endif diff --git a/tjp/core/http/beast/_test/Server.cpp b/tjp/core/http/beast/_test/Server.cpp new file mode 100755 index 0000000..089e9fb --- /dev/null +++ b/tjp/core/http/beast/_test/Server.cpp @@ -0,0 +1,505 @@ +// TJP COPYRIGHT HEADER + +#include "../Server.hpp" +#include "../../hirose/httplib.h" + +#include +#include +#include +#include + +#include +#include +#include + +namespace tjp::core::http::beast { + +SCENARIO("tjp::core::http::beast") +{ + GIVEN("server") + { + Server server; + server.setNumThreads(2); + auto make_large_payload = []() { + String data; + data.reserve(64 * 1024); + for (size_t i = 0; i < (64 * 1024); ++i) + data.push_back(static_cast('a' + (i % 26))); + return data; + }; + auto large_payload = make_large_payload(); + + server.bind_with("GET", "/hello", [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + result.body = "hello"; + return result; + }); + server.bind_with("POST", "/echo", [](const Request &req) -> Result { + Result result; + result.status = 201; + result.type = "text/plain"; + result.body = String(req.body); + return result; + }); + server.bind_with("PUT", "/replace", [](const Request &req) -> Result { + Result result; + result.type = "text/plain"; + result.body = "updated:" + String(req.body); + return result; + }); + server.bind_with("HEAD", "/meta", [](const Request &) -> Result { + Result result; + result.headers.push_back(Header { "X-Test-Head", "ok" }); + return result; + }); + server.bind_with("GET", "/empty", [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + result.body = ""; + return result; + }); + server.bind_with("HEAD", "/empty", [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + result.headers.push_back(Header { "Content-Length", "0" }); + return result; + }); + server.bind_with("HEAD", "/head-sized", [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + result.headers.push_back(Header { "Content-Length", "6"} ); + return result; + }); + server.bind_with("GET", "/range", [](const Request &req) -> Result { + Result result; + String full = "0123456789"; + auto range = req.header("Range"); + if (range && *range == "bytes=2-5") + { + result.status = 206; + result.headers.push_back(Header {"Content-Range", "bytes 2-5/10"}); + result.type = "text/plain"; + result.body = "2345"; + return result; + } + + result.type = "text/plain"; + result.body = full; + return result; + }); + server.bind_with("GET", match_exact("/match/exact"), [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + result.body = "exact"; + return result; + }); + server.bind_with("GET", match_prefix("/match/prefix"), [](const Request &req) -> Result { + Result result; + result.type = "text/plain"; + result.body = "prefix:" + String(req.target); + return result; + }); + server.bind_with("GET", match_regex("^/match/regex/[0-9]+$"), [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + result.body = "regex"; + return result; + }); + server.bind_with( + "GET", + [](StringView url) -> Optional + { return url == "/match/lambda" ? Matches() : Optional(); + }, + [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + result.body = "lambda"; + return result; + } + ); + server.bind_with("GET", "/stream", [](const Request &) -> Result { + Result result; + const String expected = "streamed-body"; + result.type = "text/plain"; + auto out = result.stream_body(expected.size()); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + out->write(expected.data(), expected.size()); + out->done(); + return result; + }); + server.bind_with("GET", "/stream-signal", [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + auto out = result.stream_body(6); + std::thread([out]() { + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + out->write("sig", 3); + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + out->write("nal", 3); + out->done(); + }).detach(); + return result; + }); + server.bind_with("GET", "/stream-empty", [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + auto out = result.stream_body(0); + out->done(); + return result; + }); + server.bind_with("GET", "/stream-unknown", [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + auto out = result.stream_body(); + std::thread([out]() { + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + out->write("sig", 3); + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + out->write("nal", 3); + out->done(); + }).detach(); + return result; + }); + server.bind_with("HEAD", "/stream-head-known", [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + auto out = result.stream_body(6); + out->write("ABCDEF", 6); + out->done(); + return result; + }); + server.bind_with("HEAD", "/stream-head-unknown", [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + auto out = result.stream_body(); + out->write("ignored", 7); + out->done(); + return result; + }); + server.bind_with("GET", "/stream-large", [large_payload](const Request &) -> Result { + Result result; + result.type = "text/plain"; + auto out = result.stream_body(large_payload.size()); + const size_t chunk = 8192; + for (size_t offset = 0; offset < large_payload.size(); offset += chunk) + { + auto size = std::min(chunk, large_payload.size() - offset); + out->write(large_payload.data() + offset, size); + } + out->done(); + return result; + }); + server.bind_with("GET", "/future", [](const Request &) -> Result { + std::this_thread::sleep_for(std::chrono::milliseconds(25)); + Result result; + result.status = 202; + result.type = "text/plain"; + result.body = "future-ok"; + return result; + }); + server.bind_with("GET", "/future-stream-known", [](const Request &) -> Result { + Result result; + result.status = 200; + result.type = "text/plain"; + auto out = result.stream_body(6); + std::thread([out]() { + std::this_thread::sleep_for(std::chrono::milliseconds(15)); + out->write("sig", 3); + std::this_thread::sleep_for(std::chrono::milliseconds(15)); + out->write("nal", 3); + out->done(); + }).detach(); + return result; + }); + server.bind("GET", "/future-stream-unknown", FutureHandler([](const Request &) { + Result result; + result.status = 200; + result.type = "text/plain"; + auto out = result.stream_body(); + std::thread([out]() { + std::this_thread::sleep_for(std::chrono::milliseconds(15)); + out->write("sig", 3); + std::this_thread::sleep_for(std::chrono::milliseconds(15)); + out->write("nal", 3); + out->done(); + }).detach(); + return future_with([result]() { + return result; + }); + })); + + auto before = std::chrono::steady_clock::now(); + server.start("127.0.0.1", 4792); + auto after = std::chrono::steady_clock::now(); + + REQUIRE((after - before) < std::chrono::seconds(1)); + core::wait_until(std::chrono::seconds(10), [&]() { return server.isRunning(); }); + + WHEN("a get is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Get("/hello"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "hello"); + } + + WHEN("a post is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Post("/echo", "posted-body", "text/plain"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 201); + REQUIRE(contents->body == "posted-body"); + } + + WHEN("a put is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Put("/replace", "payload", "text/plain"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "updated:payload"); + } + + WHEN("a head is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Head("/meta"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->get_header_value("X-Test-Head") == "ok"); + REQUIRE(contents->body.empty()); + } + + WHEN("a get of empty content is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Get("/empty"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body.empty()); + REQUIRE(contents->get_header_value("Content-Type") == "text/plain"); + REQUIRE(contents->get_header_value("Content-Length") == "0"); + } + + WHEN("a head of empty content is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Head("/empty"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body.empty()); + REQUIRE(contents->get_header_value("Content-Length") == "0"); + } + + WHEN("a head of non-empty content is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Head("/head-sized"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body.empty()); + REQUIRE(contents->get_header_value("Content-Type") == "text/plain"); + REQUIRE(contents->get_header_value("Content-Length") == "6"); + } + + WHEN("a range request is made") + { + httplib::Client client("http://127.0.0.1:4792"); + httplib::Headers headers; + headers.emplace("Range", "bytes=2-5"); + auto contents = client.Get("/range", headers); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 206); + REQUIRE(contents->get_header_value("Content-Range") == "bytes 2-5/10"); + REQUIRE(contents->body == "2345"); + } + + WHEN("an exact matcher route is requested") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Get("/match/exact"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "exact"); + } + + WHEN("a prefix matcher route is requested") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Get("/match/prefix/child"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "prefix:/match/prefix/child"); + } + + WHEN("a regex matcher route is requested") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Get("/match/regex/42"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "regex"); + } + + WHEN("a lambda matcher route is requested") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Get("/match/lambda"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "lambda"); + } + + WHEN("an unknown route is requested") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Get("/missing"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 404); + } + + WHEN("a future backed get is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Get("/future"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 202); + REQUIRE(contents->body == "future-ok"); + } + + WHEN("a future-backed known-size stream get is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Get("/future-stream-known"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "signal"); + REQUIRE(contents->get_header_value("Content-Length") == "6"); + REQUIRE(contents->get_header_value("Transfer-Encoding").empty()); + } + + WHEN("a future-backed unknown-size stream get is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Get("/future-stream-unknown"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "signal"); + REQUIRE(contents->get_header_value("Content-Length").empty()); + REQUIRE(contents->get_header_value("Transfer-Encoding") == "chunked"); + } + + WHEN("a stream backed get is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Get("/stream"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "streamed-body"); + REQUIRE(contents->get_header_value("Content-Type") == "text/plain"); + REQUIRE(contents->get_header_value("Content-Length") == "13"); + REQUIRE(contents->get_header_value("Transfer-Encoding").empty()); + } + + WHEN("a signal-backed stream get is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Get("/stream-signal"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "signal"); + REQUIRE(contents->get_header_value("Content-Length") == "6"); + REQUIRE(contents->get_header_value("Transfer-Encoding").empty()); + } + + WHEN("a zero-sized stream get is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Get("/stream-empty"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body.empty()); + REQUIRE(contents->get_header_value("Content-Type") == "text/plain"); + REQUIRE(contents->get_header_value("Content-Length") == "0"); + REQUIRE(contents->get_header_value("Transfer-Encoding").empty()); + } + + WHEN("an unknown-sized stream get is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Get("/stream-unknown"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "signal"); + REQUIRE(contents->get_header_value("Content-Length").empty()); + REQUIRE(contents->get_header_value("Transfer-Encoding") == "chunked"); + } + + WHEN("a head of known-sized stream is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Head("/stream-head-known"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body.empty()); + REQUIRE(contents->get_header_value("Content-Length") == "6"); + REQUIRE(contents->get_header_value("Transfer-Encoding").empty()); + } + + WHEN("a head of unknown-sized stream is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Head("/stream-head-unknown"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body.empty()); + REQUIRE(contents->get_header_value("Content-Length").empty()); + REQUIRE(contents->get_header_value("Transfer-Encoding").empty()); + } + + WHEN("a large stream get is made") + { + httplib::Client client("http://127.0.0.1:4792"); + auto contents = client.Get("/stream-large"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body.size() > 32 * 1024); + REQUIRE(contents->body.size() == large_payload.size()); + auto body_hash = hash_of(contents->body); + auto expected_hash = hash_of(large_payload); + REQUIRE(body_hash == expected_hash); + REQUIRE(contents->get_header_value("Content-Length") == std::to_string(large_payload.size())); + REQUIRE(contents->get_header_value("Transfer-Encoding").empty()); + } + + server.stop(); + } +} + +} // namespace diff --git a/tjp/core/http/beast/beast.hpp b/tjp/core/http/beast/beast.hpp new file mode 100644 index 0000000..7c6b61b --- /dev/null +++ b/tjp/core/http/beast/beast.hpp @@ -0,0 +1,25 @@ +// TJP COPYRIGHT HEADER + +#if defined __GNUC__ +#pragma GCC diagnostic push + //#pragma GCC diagnostic ignored "-Wcomma" + //#pragma GCC diagnostic ignored "-Wdocumentation" + //#pragma GCC diagnostic ignored "-Wshorten-64-to-32" + #pragma GCC diagnostic ignored "-Wdeprecated-declarations" + //#pragma GCC diagnostic ignored "-W#pragma-messages" + + #pragma clang diagnostic ignored "-Wcomma" + #pragma clang diagnostic ignored "-Wdocumentation" + #pragma clang diagnostic ignored "-Wshorten-64-to-32" + #pragma clang diagnostic ignored "-Wdeprecated-declarations" + #pragma clang diagnostic ignored "-W#pragma-messages" + #pragma clang diagnostic ignored "-Wdocumentation" + +#endif + +#include +#include + +#if defined __GNUC__ +#pragma GCC diagnostic pop +#endif diff --git a/tjp/core/http/collapse_future.hpp b/tjp/core/http/collapse_future.hpp new file mode 100755 index 0000000..9137c9f --- /dev/null +++ b/tjp/core/http/collapse_future.hpp @@ -0,0 +1,26 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "http.hpp" +#include "future_response.hpp" +#include + +namespace tjp::core::http { + +template +auto collapse_future(F &&f) +{ + return + [f=std::move(f)](const Request& req) + { + using R = decltype(f(req)); + + if constexpr (is_future::value) + return f(req).get(); + else + return f(req); + }; +} + +} diff --git a/tjp/core/http/future_response.hpp b/tjp/core/http/future_response.hpp new file mode 100755 index 0000000..e2d65e2 --- /dev/null +++ b/tjp/core/http/future_response.hpp @@ -0,0 +1,19 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Types.hpp" +#include + +namespace tjp::core::http { + +template +auto future_response(F &&f, const Request& req) +{ + return + future_with( + [&]() { return f(req); } + ); +} + +} diff --git a/tjp/core/http/hirose/Server.cpp b/tjp/core/http/hirose/Server.cpp new file mode 100755 index 0000000..96479e8 --- /dev/null +++ b/tjp/core/http/hirose/Server.cpp @@ -0,0 +1,404 @@ +// TJP COPYRIGHT HEADER + +#if defined(TJP_CORE_HEADER_ONLY) + #pragma once +#endif +#include + +#include "Server.hpp" + +#include +#include +#include +#include + +#include +#include "../../internet/Headers.hpp" + +#include "httplib.h" + +#include +#include +#include +#include +#include + +namespace tjp::core::http::hirose { + +using ServerImp = ::httplib::Server; + +using StreamState = http::PipeState; + +struct Request : http::Request +{ + httplib::Request *v; + + Request(const httplib::Request &v) { + target = v.target; + method = v.method; + + headers.reserve(v.headers.size()); + for (auto &[k, v]: v.headers) + headers.emplace_back(HeaderView { k, v }); + + body = v.body; + }; +} ; + +struct Response : http::Response +{ + httplib::Response *v; + String method; + bool headRequest = false; + std::shared_ptr streamState; + + Optional headerValue(const StringView &name) const + { + for (auto &header: headers) + if (header.key == name) + return header.value; + + return {}; + } + + void writeHeaders(bool skipLength) + { + v->status = status; + + for (auto &header: headers) + { + auto &key = header.key; + auto &value = header.value; + + if (key == "Content-Type") {} + else + if (key == "Content-Length" && skipLength) {} + else + v->set_header(key, value); + } + } + + void flushHeaders(const StringView &method) override + { + bool skipLength = method == "PUT" || method == "GET"; + + writeHeaders(skipLength); + } + + void write(const StringView &data) override + { + auto type = headerValue("Content-Type").value_or("text/plain"); + v->set_content(data.data(), data.size(), String(type)); + } + + std::shared_ptr stream() override + { + auto type = headerValue("Content-Type").value_or("text/plain"); + Optional size_; + if (auto contentLength = headerValue("Content-Length")) + { + try + { + size_ = static_cast(std::stoull(String(*contentLength))); + } + catch (...) + { + // ignore malformed values + } + } + + if (!size_ && streamState) + size_ = streamState->size(); + + if (!streamState) + streamState = StreamState::make(size_); + + auto state = streamState; + + if (headRequest) + { + if (size_) + { + v->set_content_provider( + *size_, + String(type), + [](size_t, size_t, httplib::DataSink &) { + return false; + } + ); + } + else + { + v->set_content_provider( + String(type), + [](size_t, httplib::DataSink &) { + return false; + } + ); + } + + return state->sink(); + } + + if (size_) + { + v->set_content_provider( + *size_, + String(type), + [state](size_t, size_t, httplib::DataSink &sink_) { + String chunk; + bool ended = false; + if (state->popOrWait(chunk, ended)) + { + sink_.write(chunk.data(), chunk.size()); + return true; + } + + if (ended) + { + // For fixed-length providers, returning true without writing + // bytes keeps offset unchanged and can spin the write loop. + return false; + } + + return false; + }, + [state](bool) { + if (auto sink = state->sink()) + sink->done(); + } + ); + } + else + { + v->set_chunked_content_provider( + String(type), + [state](size_t, httplib::DataSink &sink_) { + String chunk; + bool ended = false; + if (state->popOrWait(chunk, ended)) + { + sink_.write(chunk.data(), chunk.size()); + return true; + } + + if (ended) + { + if (sink_.done) + sink_.done(); + return true; + } + + return false; + }, + [state](bool) { + if (auto sink = state->sink()) + sink->done(); + } + ); + } + + return state->sink(); + } +} ; + +struct Server::I +{ + struct Route + { + Method method; + Matcher matcher; + FutureHandler handler; + } ; + + ServerImp webServer; + std::thread thread; + bool running = false; + Optional numThreads; + std::mutex routesMutex; + Vector routes; + + Vector> components; + + bool findHandler( + Request &request, + FutureHandler &out + ) + { + std::lock_guard lock(routesMutex); + for (const auto &route: routes) + { + if (route.method != request.method) + continue; + + if (route.matcher) + if (auto matches = route.matcher(request.target)) + { + request.matches = *matches; + out = route.handler; + return true; + } + } + + return false; + } +}; + +static void applyResult(Response &response, Result result) +{ + response.status = result.status.value_or(200); + for (auto &[key, value]: result.headers) + response.header(key, value); + + if (result.type) + response.header("Content-Type", *result.type); + + if (result.streamSize && !response.hasHeader("Content-Length")) + response.content_length(*result.streamSize); + + if (result.streamOwner) + { + response.streamState = std::move(result.streamOwner); + response.stream(); + return; + } + + if (result.stream) + { + response.status = 500; + response.header("Content-Type", "text/plain"); + response.write("Invalid stream owner"); + return; + } + + response.flushHeaders(response.method); + response.write(result.body.value_or(String {})); +} + +TJP_CORE_HEADER_ONLY_INLINE +Server::Server() +{ + i = strong(); + + auto dispatch = [state = i.get()](const httplib::Request &request_, httplib::Response &response_) { + Request request(request_); + + if (request_.method == "HEAD") + core::remove_const_of_var(request_.ranges).clear(); + + Response response; + response.v = &response_; + response.method = request_.method; + response.headRequest = request_.method == "HEAD"; + + try + { + FutureHandler handler; + if (state->findHandler(request, handler)) + applyResult(response, handler(request).get()); + else + { + response.status = 404; + response.header("Content-Type", "text/plain"); + response.write("Not found"); + } + } + catch (Result &result) + { + applyResult(response, Result(result)); + } + catch (Exception &e) + { + response.status = 400; + response.header("Content-Type", "text/plain"); + response.write("Error: " + e.what()); + } + catch (...) + { + response.status = 400; + response.header("Content-Type", "text/plain"); + response.write("Error"); + } + }; + + i->webServer.Get(".*", dispatch); + i->webServer.Post(".*", dispatch); + i->webServer.Put(".*", dispatch); + i->webServer.Options(".*", dispatch); +} + +TJP_CORE_HEADER_ONLY_INLINE +Server::~Server() +{ + stop(); +} + +TJP_CORE_HEADER_ONLY_INLINE +void Server::add(const StrongPtr &component, const String &urlPrefix) +{ + component->use(*this, urlPrefix); +} + +TJP_CORE_HEADER_ONLY_INLINE +void Server::run(const String &bindTo, int port) +{ + if (i->numThreads) + i->webServer.new_task_queue = [n = *i->numThreads] { return new httplib::ThreadPool(n); }; + + i->webServer.listen(bindTo, port); +} + +TJP_CORE_HEADER_ONLY_INLINE +void Server::setNumThreads(size_t n) +{ + i->numThreads = n; +} + +TJP_CORE_HEADER_ONLY_INLINE +void Server::start(const String &bindTo, int port) +{ + if (i->running) + throw Exception { "Webserver already running" }; + + i->running = true; + + i->thread = std::thread([this, bindTo, port]() { + run(bindTo, port); + }); +} + +TJP_CORE_HEADER_ONLY_INLINE +bool Server::isRunning () +{ + return i->webServer.is_running(); +} + +TJP_CORE_HEADER_ONLY_INLINE +void Server::stop() +{ + if (!i->running) + return; + + i->webServer.stop(); + + if (i->thread.joinable()) + i->thread.join(); + + i->running = false; +} + +TJP_CORE_HEADER_ONLY_INLINE +void Server::bind(const Method &method, Matcher &&matcher, FutureHandler &&handler) +{ + if (!matcher) + throw Exception { "Invalid route matcher" }; + + std::lock_guard lock(i->routesMutex); + i->routes.emplace_back(I::Route { + method, + std::move(matcher), + std::move(handler) + }); +} + +} // namespace diff --git a/tjp/core/http/hirose/Server.h b/tjp/core/http/hirose/Server.h new file mode 100755 index 0000000..f770e57 --- /dev/null +++ b/tjp/core/http/hirose/Server.h @@ -0,0 +1,9 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +namespace tjp::core::http::hirose { + +struct Server; + +} // namespace diff --git a/tjp/core/http/hirose/Server.hpp b/tjp/core/http/hirose/Server.hpp new file mode 100755 index 0000000..75224bc --- /dev/null +++ b/tjp/core/http/hirose/Server.hpp @@ -0,0 +1,49 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Server.h" +#include "../Server.hpp" +#include "../Component.hpp" +#include + +#include +#include + +namespace tjp::core::http::hirose { + +struct Server : http::Server +{ + using Super = http::Server; + using Super::bind; + + struct I; + StrongPtr i; + + Server(); + ~Server(); + + void add( + const StrongPtr &component, + const String &urlPrefix = {} + ) override; + + void setNumThreads(size_t) override; + void start(const String &bindTo, int port) override; + void run(const String &bindTo, int port) override; + void stop() override; + + bool isRunning() override; + + void bind( + const Method &method, + Matcher &&matcher, + FutureHandler && + ) override; +}; + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "Server.cpp" +#endif diff --git a/tjp/core/http/hirose/_test/Server.cpp b/tjp/core/http/hirose/_test/Server.cpp new file mode 100755 index 0000000..a8983b6 --- /dev/null +++ b/tjp/core/http/hirose/_test/Server.cpp @@ -0,0 +1,356 @@ +// TJP COPYRIGHT HEADER + +#include "../Server.hpp" +#include "../httplib.h" + +#include +#include +#include +#include +#include + +#include +#include + +namespace tjp::core::http::hirose { + +SCENARIO("tjp::core::http::hirose") +{ + GIVEN("server") + { + Server server; + auto make_large_payload = []() { + String data; + data.reserve(64 * 1024); + for (size_t i = 0; i < (64 * 1024); ++i) + data.push_back(static_cast('a' + (i % 26))); + return data; + }; + auto large_payload = make_large_payload(); + + server.bind_with("GET", "/hello", [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + result.body = "hello"; + return result; + }); + server.bind_with("POST", "/echo", [](const Request &req) -> Result { + Result result; + result.status = 201; + result.type = "text/plain"; + result.body = String(req.body); + return result; + }); + server.bind_with("PUT", "/replace", [](const Request &req) -> Result { + Result result; + result.type = "text/plain"; + result.body = "updated:" + String(req.body); + return result; + }); + server.bind_with("GET", "/range", [](const Request &req) -> Result + { + Result result; + String full = "0123456789"; + String send; + size_t begin = 0; + size_t end = full.size() - 1; + + if (auto range_ = req.header("range")) + if (auto range = core::internet::parseRange(String(*range_))) + { + auto &[begin_, end_] = *range; + begin = begin_; + end = end_; + send = full.substr(begin, (end - begin + 1)); + } + + result.headers.push_back(Header { "Content-Range", core::internet::makeContentRange({ begin, end, full.size() }) }); + result.type = "text/plain"; + result.body = send; + return result; + }); + server.bind_with("GET", match_exact("/match/exact"), [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + result.body = "exact"; + return result; + }); + server.bind_with("GET", match_prefix("/match/prefix"), [](const Request &req) -> Result { + Result result; + result.type = "text/plain"; + result.body = "prefix:" + String(req.target); + return result; + }); + server.bind_with("GET", match_regex("^/match/regex/[0-9]+$"), [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + result.body = "regex"; + return result; + }); + server.bind_with("GET", + [](StringView url) -> Optional { + return url == "/match/lambda" ? Matches() : Optional(); + }, + [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + result.body = "lambda"; + return result; + }); + server.bind_with("GET", "/stream-known", [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + auto out = result.stream_body(6); + out->write("signal", 6); + out->done(); + return result; + }); + server.bind_with("GET", "/stream-unknown", [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + auto out = result.stream_body(); + out->write("sig", 3); + out->write("nal", 3); + out->done(); + return result; + }); + server.bind_with("HEAD", "/stream-head-known", [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + auto out = result.stream_body(6); + out->write("ABCDEF", 6); + out->done(); + return result; + }); + server.bind_with("HEAD", "/stream-head-unknown", [](const Request &) -> Result { + Result result; + result.type = "text/plain"; + auto out = result.stream_body(); + out->write("ignored", 7); + out->done(); + return result; + }); + server.bind_with("GET", "/stream-large", [large_payload](const Request &) -> Result { + Result result; + result.type = "text/plain"; + auto out = result.stream_body(large_payload.size()); + const size_t chunk = 8192; + for (size_t offset = 0; offset < large_payload.size(); offset += chunk) + { + auto size = std::min(chunk, large_payload.size() - offset); + out->write(large_payload.data() + offset, size); + } + out->done(); + return result; + }); + server.bind_with("GET", "/future-stream-known", [](const Request &) -> Result { + Result result; + result.status = 200; + result.type = "text/plain"; + auto out = result.stream_body(6); + std::thread([out]() { + std::this_thread::sleep_for(std::chrono::milliseconds(15)); + out->write("sig", 3); + std::this_thread::sleep_for(std::chrono::milliseconds(15)); + out->write("nal", 3); + out->done(); + }).detach(); + return result; + }); + server.bind("GET", "/future-stream-unknown", FutureHandler([](const Request &) { + Result result; + result.status = 200; + result.type = "text/plain"; + auto out = result.stream_body(); + std::thread([out]() { + std::this_thread::sleep_for(std::chrono::milliseconds(15)); + out->write("sig", 3); + std::this_thread::sleep_for(std::chrono::milliseconds(15)); + out->write("nal", 3); + out->done(); + }).detach(); + return future_with([result]() { + return result; + }); + })); + + server.start("127.0.0.1", 4790); + + core::wait_until(std::chrono::seconds(10), [&]() { return server.isRunning();}); + + WHEN("a get is made") + { + httplib::Client client("http://127.0.0.1:4790"); + auto contents = client.Get("/hello"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "hello"); + } + + WHEN("a post is made") + { + httplib::Client client("http://127.0.0.1:4790"); + auto contents = client.Post("/echo", "posted-body", "text/plain"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 201); + REQUIRE(contents->body == "posted-body"); + } + + WHEN("a put is made") + { + httplib::Client client("http://127.0.0.1:4790"); + auto contents = client.Put("/replace", "payload", "text/plain"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "updated:payload"); + } + + WHEN("a range request is made") + { + httplib::Client client("http://127.0.0.1:4790"); + httplib::Headers headers; + headers.emplace("Range", "bytes=2-5"); + auto contents = client.Get("/range", headers); + + REQUIRE(bool(contents)); +// REQUIRE(contents->status == 206); + REQUIRE(contents->get_header_value("Content-Range") == "bytes 2-5/10"); + REQUIRE(contents->body == "2345"); + } + + WHEN("an exact matcher route is requested") + { + httplib::Client client("http://127.0.0.1:4790"); + auto contents = client.Get("/match/exact"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "exact"); + } + + WHEN("a prefix matcher route is requested") + { + httplib::Client client("http://127.0.0.1:4790"); + auto contents = client.Get("/match/prefix/child"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "prefix:/match/prefix/child"); + } + + WHEN("a regex matcher route is requested") + { + httplib::Client client("http://127.0.0.1:4790"); + auto contents = client.Get("/match/regex/42"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "regex"); + } + + WHEN("a lambda matcher route is requested") + { + httplib::Client client("http://127.0.0.1:4790"); + auto contents = client.Get("/match/lambda"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "lambda"); + } + + WHEN("a known-sized stream get is made") + { + httplib::Client client("http://127.0.0.1:4790"); + auto contents = client.Get("/stream-known"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "signal"); + REQUIRE(contents->get_header_value("Content-Length") == "6"); + REQUIRE(contents->get_header_value("Transfer-Encoding").empty()); + } + + WHEN("an unknown-sized stream get is made") + { + httplib::Client client("http://127.0.0.1:4790"); + auto contents = client.Get("/stream-unknown"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "signal"); + REQUIRE(contents->get_header_value("Content-Length").empty()); + REQUIRE(contents->get_header_value("Transfer-Encoding") == "chunked"); + } + + WHEN("a head of known-sized stream is made") + { + httplib::Client client("http://127.0.0.1:4790"); + auto contents = client.Head("/stream-head-known"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body.empty()); + REQUIRE(contents->get_header_value("Content-Length") == "6"); + REQUIRE(contents->get_header_value("Transfer-Encoding").empty()); + } + + WHEN("a head of unknown-sized stream is made") + { + httplib::Client client("http://127.0.0.1:4790"); + auto contents = client.Head("/stream-head-unknown"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body.empty()); + REQUIRE(contents->get_header_value("Content-Length").empty()); + REQUIRE(contents->get_header_value("Transfer-Encoding").empty()); + } + + WHEN("a large stream get is made") + { + httplib::Client client("http://127.0.0.1:4790"); + auto contents = client.Get("/stream-large"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body.size() > 32 * 1024); + REQUIRE(contents->body.size() == large_payload.size()); + auto body_hash = hash_of(contents->body); + auto expected_hash = hash_of(large_payload); + REQUIRE(body_hash == expected_hash); + REQUIRE(contents->get_header_value("Content-Length") == std::to_string(large_payload.size())); + REQUIRE(contents->get_header_value("Transfer-Encoding").empty()); + } + + WHEN("a future-backed known-size stream get is made") + { + httplib::Client client("http://127.0.0.1:4790"); + auto contents = client.Get("/future-stream-known"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "signal"); + REQUIRE(contents->get_header_value("Content-Length") == "6"); + REQUIRE(contents->get_header_value("Transfer-Encoding").empty()); + } + + WHEN("a future-backed unknown-size stream get is made") + { + httplib::Client client("http://127.0.0.1:4790"); + auto contents = client.Get("/future-stream-unknown"); + + REQUIRE(bool(contents)); + REQUIRE(contents->status == 200); + REQUIRE(contents->body == "signal"); + REQUIRE(contents->get_header_value("Content-Length").empty()); + REQUIRE(contents->get_header_value("Transfer-Encoding") == "chunked"); + } + + server.stop(); + } + } + +} // namespace diff --git a/tjp/core/http/hirose/httplib-0_27_0.h b/tjp/core/http/hirose/httplib-0_27_0.h new file mode 100644 index 0000000..48f1cce --- /dev/null +++ b/tjp/core/http/hirose/httplib-0_27_0.h @@ -0,0 +1,12067 @@ +// TJP COPYRIGHT HEADER + +// +// httplib.h +// +// Copyright (c) 2025 Yuji Hirose. All rights reserved. +// MIT License +// + +#ifndef CPPHTTPLIB_HTTPLIB_H +#define CPPHTTPLIB_HTTPLIB_H + +#define CPPHTTPLIB_VERSION "0.27.0" +#define CPPHTTPLIB_VERSION_NUM "0x001B00" + +/* + * Platform compatibility check + */ + +#if defined(_WIN32) && !defined(_WIN64) +#if defined(_MSC_VER) +#pragma message( \ + "cpp-httplib doesn't support 32-bit Windows. Please use a 64-bit compiler.") +#else +#warning \ + "cpp-httplib doesn't support 32-bit Windows. Please use a 64-bit compiler." +#endif +#elif defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ < 8 +#warning \ + "cpp-httplib doesn't support 32-bit platforms. Please use a 64-bit compiler." +#elif defined(__SIZEOF_SIZE_T__) && __SIZEOF_SIZE_T__ < 8 +#warning \ + "cpp-httplib doesn't support platforms where size_t is less than 64 bits." +#endif + +#ifdef _WIN32 +#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0A00 +#error \ + "cpp-httplib doesn't support Windows 8 or lower. Please use Windows 10 or later." +#endif +#endif + +/* + * Configuration + */ + +#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND +#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND +#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND 10000 +#endif + +#ifndef CPPHTTPLIB_KEEPALIVE_MAX_COUNT +#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 100 +#endif + +#ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND +#define CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND 300 +#endif + +#ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND +#define CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND +#define CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND +#define CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND +#define CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND +#define CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND +#define CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND 300 +#endif + +#ifndef CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND +#define CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND +#define CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND +#define CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_CLIENT_MAX_TIMEOUT_MSECOND +#define CPPHTTPLIB_CLIENT_MAX_TIMEOUT_MSECOND 0 +#endif + +#ifndef CPPHTTPLIB_IDLE_INTERVAL_SECOND +#define CPPHTTPLIB_IDLE_INTERVAL_SECOND 0 +#endif + +#ifndef CPPHTTPLIB_IDLE_INTERVAL_USECOND +#ifdef _WIN32 +#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 1000 +#else +#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 0 +#endif +#endif + +#ifndef CPPHTTPLIB_REQUEST_URI_MAX_LENGTH +#define CPPHTTPLIB_REQUEST_URI_MAX_LENGTH 8192 +#endif + +#ifndef CPPHTTPLIB_HEADER_MAX_LENGTH +#define CPPHTTPLIB_HEADER_MAX_LENGTH 8192 +#endif + +#ifndef CPPHTTPLIB_HEADER_MAX_COUNT +#define CPPHTTPLIB_HEADER_MAX_COUNT 100 +#endif + +#ifndef CPPHTTPLIB_REDIRECT_MAX_COUNT +#define CPPHTTPLIB_REDIRECT_MAX_COUNT 20 +#endif + +#ifndef CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT +#define CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT 1024 +#endif + +#ifndef CPPHTTPLIB_PAYLOAD_MAX_LENGTH +#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH ((std::numeric_limits::max)()) +#endif + +#ifndef CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH +#define CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH 8192 +#endif + +#ifndef CPPHTTPLIB_RANGE_MAX_COUNT +#define CPPHTTPLIB_RANGE_MAX_COUNT 1024 +#endif + +#ifndef CPPHTTPLIB_TCP_NODELAY +#define CPPHTTPLIB_TCP_NODELAY false +#endif + +#ifndef CPPHTTPLIB_IPV6_V6ONLY +#define CPPHTTPLIB_IPV6_V6ONLY false +#endif + +#ifndef CPPHTTPLIB_RECV_BUFSIZ +#define CPPHTTPLIB_RECV_BUFSIZ size_t(16384u) +#endif + +#ifndef CPPHTTPLIB_SEND_BUFSIZ +#define CPPHTTPLIB_SEND_BUFSIZ size_t(16384u) +#endif + +#ifndef CPPHTTPLIB_COMPRESSION_BUFSIZ +#define CPPHTTPLIB_COMPRESSION_BUFSIZ size_t(16384u) +#endif + +#ifndef CPPHTTPLIB_THREAD_POOL_COUNT +#define CPPHTTPLIB_THREAD_POOL_COUNT \ + ((std::max)(8u, std::thread::hardware_concurrency() > 0 \ + ? std::thread::hardware_concurrency() - 1 \ + : 0)) +#endif + +#ifndef CPPHTTPLIB_RECV_FLAGS +#define CPPHTTPLIB_RECV_FLAGS 0 +#endif + +#ifndef CPPHTTPLIB_SEND_FLAGS +#define CPPHTTPLIB_SEND_FLAGS 0 +#endif + +#ifndef CPPHTTPLIB_LISTEN_BACKLOG +#define CPPHTTPLIB_LISTEN_BACKLOG 5 +#endif + +#ifndef CPPHTTPLIB_MAX_LINE_LENGTH +#define CPPHTTPLIB_MAX_LINE_LENGTH 32768 +#endif + +/* + * Headers + */ + +#ifdef _WIN32 +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS +#endif //_CRT_SECURE_NO_WARNINGS + +#ifndef _CRT_NONSTDC_NO_DEPRECATE +#define _CRT_NONSTDC_NO_DEPRECATE +#endif //_CRT_NONSTDC_NO_DEPRECATE + +#if defined(_MSC_VER) +#if _MSC_VER < 1900 +#error Sorry, Visual Studio versions prior to 2015 are not supported +#endif + +#pragma comment(lib, "ws2_32.lib") + +using ssize_t = __int64; +#endif // _MSC_VER + +#ifndef S_ISREG +#define S_ISREG(m) (((m) & S_IFREG) == S_IFREG) +#endif // S_ISREG + +#ifndef S_ISDIR +#define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR) +#endif // S_ISDIR + +#ifndef NOMINMAX +#define NOMINMAX +#endif // NOMINMAX + +#include +#include +#include + +#if defined(__has_include) +#if __has_include() +// afunix.h uses types declared in winsock2.h, so has to be included after it. +#include +#define CPPHTTPLIB_HAVE_AFUNIX_H 1 +#endif +#endif + +#ifndef WSA_FLAG_NO_HANDLE_INHERIT +#define WSA_FLAG_NO_HANDLE_INHERIT 0x80 +#endif + +using nfds_t = unsigned long; +using socket_t = SOCKET; +using socklen_t = int; + +#else // not _WIN32 + +#include +#if !defined(_AIX) && !defined(__MVS__) +#include +#endif +#ifdef __MVS__ +#include +#ifndef NI_MAXHOST +#define NI_MAXHOST 1025 +#endif +#endif +#include +#include +#include +#ifdef __linux__ +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#include + +using socket_t = int; +#ifndef INVALID_SOCKET +#define INVALID_SOCKET (-1) +#endif +#endif //_WIN32 + +#if defined(__APPLE__) +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO) || \ + defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) +#if TARGET_OS_MAC +#include +#include +#endif +#endif // CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO or + // CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef _WIN32 +#include + +// these are defined in wincrypt.h and it breaks compilation if BoringSSL is +// used +#undef X509_NAME +#undef X509_CERT_PAIR +#undef X509_EXTENSIONS +#undef PKCS7_SIGNER_INFO + +#ifdef _MSC_VER +#pragma comment(lib, "crypt32.lib") +#endif +#endif // _WIN32 + +#if defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) +#if TARGET_OS_MAC +#include +#endif +#endif // CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO + +#include +#include +#include +#include + +#if defined(_WIN32) && defined(OPENSSL_USE_APPLINK) +#include +#endif + +#include +#include + +#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) +#if OPENSSL_VERSION_NUMBER < 0x1010107f +#error Please use OpenSSL or a current version of BoringSSL +#endif +#define SSL_get1_peer_certificate SSL_get_peer_certificate +#elif OPENSSL_VERSION_NUMBER < 0x30000000L +#error Sorry, OpenSSL versions prior to 3.0.0 are not supported +#endif + +#endif // CPPHTTPLIB_OPENSSL_SUPPORT + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +#include +#endif + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT +#include +#include +#endif + +#ifdef CPPHTTPLIB_ZSTD_SUPPORT +#include +#endif + +/* + * Declaration + */ +namespace httplib { + +namespace detail { + +/* + * Backport std::make_unique from C++14. + * + * NOTE: This code came up with the following stackoverflow post: + * https://stackoverflow.com/questions/10149840/c-arrays-and-make-unique + * + */ + +template +typename std::enable_if::value, std::unique_ptr>::type +make_unique(Args &&...args) { + return std::unique_ptr(new T(std::forward(args)...)); +} + +template +typename std::enable_if::value, std::unique_ptr>::type +make_unique(std::size_t n) { + typedef typename std::remove_extent::type RT; + return std::unique_ptr(new RT[n]); +} + +namespace case_ignore { + +inline unsigned char to_lower(int c) { + const static unsigned char table[256] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, + 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, + 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, + 122, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, + 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, + 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, + 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, + 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, + 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, + 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 224, 225, 226, + 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, + 242, 243, 244, 245, 246, 215, 248, 249, 250, 251, 252, 253, 254, 223, 224, + 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, + 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, + 255, + }; + return table[(unsigned char)(char)c]; +} + +inline bool equal(const std::string &a, const std::string &b) { + return a.size() == b.size() && + std::equal(a.begin(), a.end(), b.begin(), [](char ca, char cb) { + return to_lower(ca) == to_lower(cb); + }); +} + +struct equal_to { + bool operator()(const std::string &a, const std::string &b) const { + return equal(a, b); + } +}; + +struct hash { + size_t operator()(const std::string &key) const { + return hash_core(key.data(), key.size(), 0); + } + + size_t hash_core(const char *s, size_t l, size_t h) const { + return (l == 0) ? h + : hash_core(s + 1, l - 1, + // Unsets the 6 high bits of h, therefore no + // overflow happens + (((std::numeric_limits::max)() >> 6) & + h * 33) ^ + static_cast(to_lower(*s))); + } +}; + +template +using unordered_set = std::unordered_set; + +} // namespace case_ignore + +// This is based on +// "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189". + +struct scope_exit { + explicit scope_exit(std::function &&f) + : exit_function(std::move(f)), execute_on_destruction{true} {} + + scope_exit(scope_exit &&rhs) noexcept + : exit_function(std::move(rhs.exit_function)), + execute_on_destruction{rhs.execute_on_destruction} { + rhs.release(); + } + + ~scope_exit() { + if (execute_on_destruction) { this->exit_function(); } + } + + void release() { this->execute_on_destruction = false; } + +private: + scope_exit(const scope_exit &) = delete; + void operator=(const scope_exit &) = delete; + scope_exit &operator=(scope_exit &&) = delete; + + std::function exit_function; + bool execute_on_destruction; +}; + +} // namespace detail + +enum SSLVerifierResponse { + // no decision has been made, use the built-in certificate verifier + NoDecisionMade, + // connection certificate is verified and accepted + CertificateAccepted, + // connection certificate was processed but is rejected + CertificateRejected +}; + +enum StatusCode { + // Information responses + Continue_100 = 100, + SwitchingProtocol_101 = 101, + Processing_102 = 102, + EarlyHints_103 = 103, + + // Successful responses + OK_200 = 200, + Created_201 = 201, + Accepted_202 = 202, + NonAuthoritativeInformation_203 = 203, + NoContent_204 = 204, + ResetContent_205 = 205, + PartialContent_206 = 206, + MultiStatus_207 = 207, + AlreadyReported_208 = 208, + IMUsed_226 = 226, + + // Redirection messages + MultipleChoices_300 = 300, + MovedPermanently_301 = 301, + Found_302 = 302, + SeeOther_303 = 303, + NotModified_304 = 304, + UseProxy_305 = 305, + unused_306 = 306, + TemporaryRedirect_307 = 307, + PermanentRedirect_308 = 308, + + // Client error responses + BadRequest_400 = 400, + Unauthorized_401 = 401, + PaymentRequired_402 = 402, + Forbidden_403 = 403, + NotFound_404 = 404, + MethodNotAllowed_405 = 405, + NotAcceptable_406 = 406, + ProxyAuthenticationRequired_407 = 407, + RequestTimeout_408 = 408, + Conflict_409 = 409, + Gone_410 = 410, + LengthRequired_411 = 411, + PreconditionFailed_412 = 412, + PayloadTooLarge_413 = 413, + UriTooLong_414 = 414, + UnsupportedMediaType_415 = 415, + RangeNotSatisfiable_416 = 416, + ExpectationFailed_417 = 417, + ImATeapot_418 = 418, + MisdirectedRequest_421 = 421, + UnprocessableContent_422 = 422, + Locked_423 = 423, + FailedDependency_424 = 424, + TooEarly_425 = 425, + UpgradeRequired_426 = 426, + PreconditionRequired_428 = 428, + TooManyRequests_429 = 429, + RequestHeaderFieldsTooLarge_431 = 431, + UnavailableForLegalReasons_451 = 451, + + // Server error responses + InternalServerError_500 = 500, + NotImplemented_501 = 501, + BadGateway_502 = 502, + ServiceUnavailable_503 = 503, + GatewayTimeout_504 = 504, + HttpVersionNotSupported_505 = 505, + VariantAlsoNegotiates_506 = 506, + InsufficientStorage_507 = 507, + LoopDetected_508 = 508, + NotExtended_510 = 510, + NetworkAuthenticationRequired_511 = 511, +}; + +using Headers = + std::unordered_multimap; + +using Params = std::multimap; +using Match = std::smatch; + +using DownloadProgress = std::function; +using UploadProgress = std::function; + +struct Response; +using ResponseHandler = std::function; + +struct FormData { + std::string name; + std::string content; + std::string filename; + std::string content_type; + Headers headers; +}; + +struct FormField { + std::string name; + std::string content; + Headers headers; +}; +using FormFields = std::multimap; + +using FormFiles = std::multimap; + +struct MultipartFormData { + FormFields fields; // Text fields from multipart + FormFiles files; // Files from multipart + + // Text field access + std::string get_field(const std::string &key, size_t id = 0) const; + std::vector get_fields(const std::string &key) const; + bool has_field(const std::string &key) const; + size_t get_field_count(const std::string &key) const; + + // File access + FormData get_file(const std::string &key, size_t id = 0) const; + std::vector get_files(const std::string &key) const; + bool has_file(const std::string &key) const; + size_t get_file_count(const std::string &key) const; +}; + +struct UploadFormData { + std::string name; + std::string content; + std::string filename; + std::string content_type; +}; +using UploadFormDataItems = std::vector; + +class DataSink { +public: + DataSink() : os(&sb_), sb_(*this) {} + + DataSink(const DataSink &) = delete; + DataSink &operator=(const DataSink &) = delete; + DataSink(DataSink &&) = delete; + DataSink &operator=(DataSink &&) = delete; + + std::function write; + std::function is_writable; + std::function done; + std::function done_with_trailer; + std::ostream os; + +private: + class data_sink_streambuf final : public std::streambuf { + public: + explicit data_sink_streambuf(DataSink &sink) : sink_(sink) {} + + protected: + std::streamsize xsputn(const char *s, std::streamsize n) override { + sink_.write(s, static_cast(n)); + return n; + } + + private: + DataSink &sink_; + }; + + data_sink_streambuf sb_; +}; + +using ContentProvider = + std::function; + +using ContentProviderWithoutLength = + std::function; + +using ContentProviderResourceReleaser = std::function; + +struct FormDataProvider { + std::string name; + ContentProviderWithoutLength provider; + std::string filename; + std::string content_type; +}; +using FormDataProviderItems = std::vector; + +using ContentReceiverWithProgress = std::function; + +using ContentReceiver = + std::function; + +using FormDataHeader = std::function; + +class ContentReader { +public: + using Reader = std::function; + using FormDataReader = + std::function; + + ContentReader(Reader reader, FormDataReader multipart_reader) + : reader_(std::move(reader)), + formdata_reader_(std::move(multipart_reader)) {} + + bool operator()(FormDataHeader header, ContentReceiver receiver) const { + return formdata_reader_(std::move(header), std::move(receiver)); + } + + bool operator()(ContentReceiver receiver) const { + return reader_(std::move(receiver)); + } + + Reader reader_; + FormDataReader formdata_reader_; +}; + +using Range = std::pair; +using Ranges = std::vector; + +struct Request { + std::string method; + std::string path; + std::string matched_route; + Params params; + Headers headers; + Headers trailers; + std::string body; + + std::string remote_addr; + int remote_port = -1; + std::string local_addr; + int local_port = -1; + + // for server + std::string version; + std::string target; + MultipartFormData form; + Ranges ranges; + Match matches; + std::unordered_map path_params; + std::function is_connection_closed = []() { return true; }; + + // for client + std::vector accept_content_types; + ResponseHandler response_handler; + ContentReceiverWithProgress content_receiver; + DownloadProgress download_progress; + UploadProgress upload_progress; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + const SSL *ssl = nullptr; +#endif + + bool has_header(const std::string &key) const; + std::string get_header_value(const std::string &key, const char *def = "", + size_t id = 0) const; + size_t get_header_value_u64(const std::string &key, size_t def = 0, + size_t id = 0) const; + size_t get_header_value_count(const std::string &key) const; + void set_header(const std::string &key, const std::string &val); + + bool has_trailer(const std::string &key) const; + std::string get_trailer_value(const std::string &key, size_t id = 0) const; + size_t get_trailer_value_count(const std::string &key) const; + + bool has_param(const std::string &key) const; + std::string get_param_value(const std::string &key, size_t id = 0) const; + size_t get_param_value_count(const std::string &key) const; + + bool is_multipart_form_data() const; + + // private members... + size_t redirect_count_ = CPPHTTPLIB_REDIRECT_MAX_COUNT; + size_t content_length_ = 0; + ContentProvider content_provider_; + bool is_chunked_content_provider_ = false; + size_t authorization_count_ = 0; + std::chrono::time_point start_time_ = + (std::chrono::steady_clock::time_point::min)(); +}; + +struct Response { + std::string version; + int status = -1; + std::string reason; + Headers headers; + Headers trailers; + std::string body; + std::string location; // Redirect location + + bool has_header(const std::string &key) const; + std::string get_header_value(const std::string &key, const char *def = "", + size_t id = 0) const; + size_t get_header_value_u64(const std::string &key, size_t def = 0, + size_t id = 0) const; + size_t get_header_value_count(const std::string &key) const; + void set_header(const std::string &key, const std::string &val); + + bool has_trailer(const std::string &key) const; + std::string get_trailer_value(const std::string &key, size_t id = 0) const; + size_t get_trailer_value_count(const std::string &key) const; + + void set_redirect(const std::string &url, int status = StatusCode::Found_302); + void set_content(const char *s, size_t n, const std::string &content_type); + void set_content(const std::string &s, const std::string &content_type); + void set_content(std::string &&s, const std::string &content_type); + + void set_content_provider( + size_t length, const std::string &content_type, ContentProvider provider, + ContentProviderResourceReleaser resource_releaser = nullptr); + + void set_content_provider( + const std::string &content_type, ContentProviderWithoutLength provider, + ContentProviderResourceReleaser resource_releaser = nullptr); + + void set_chunked_content_provider( + const std::string &content_type, ContentProviderWithoutLength provider, + ContentProviderResourceReleaser resource_releaser = nullptr); + + void set_file_content(const std::string &path, + const std::string &content_type); + void set_file_content(const std::string &path); + + Response() = default; + Response(const Response &) = default; + Response &operator=(const Response &) = default; + Response(Response &&) = default; + Response &operator=(Response &&) = default; + ~Response() { + if (content_provider_resource_releaser_) { + content_provider_resource_releaser_(content_provider_success_); + } + } + + // private members... + size_t content_length_ = 0; + ContentProvider content_provider_; + ContentProviderResourceReleaser content_provider_resource_releaser_; + bool is_chunked_content_provider_ = false; + bool content_provider_success_ = false; + std::string file_content_path_; + std::string file_content_content_type_; +}; + +class Stream { +public: + virtual ~Stream() = default; + + virtual bool is_readable() const = 0; + virtual bool wait_readable() const = 0; + virtual bool wait_writable() const = 0; + + virtual ssize_t read(char *ptr, size_t size) = 0; + virtual ssize_t write(const char *ptr, size_t size) = 0; + virtual void get_remote_ip_and_port(std::string &ip, int &port) const = 0; + virtual void get_local_ip_and_port(std::string &ip, int &port) const = 0; + virtual socket_t socket() const = 0; + + virtual time_t duration() const = 0; + + ssize_t write(const char *ptr); + ssize_t write(const std::string &s); +}; + +class TaskQueue { +public: + TaskQueue() = default; + virtual ~TaskQueue() = default; + + virtual bool enqueue(std::function fn) = 0; + virtual void shutdown() = 0; + + virtual void on_idle() {} +}; + +class ThreadPool final : public TaskQueue { +public: + explicit ThreadPool(size_t n, size_t mqr = 0) + : shutdown_(false), max_queued_requests_(mqr) { + while (n) { + threads_.emplace_back(worker(*this)); + n--; + } + } + + ThreadPool(const ThreadPool &) = delete; + ~ThreadPool() override = default; + + bool enqueue(std::function fn) override { + { + std::unique_lock lock(mutex_); + if (max_queued_requests_ > 0 && jobs_.size() >= max_queued_requests_) { + return false; + } + jobs_.push_back(std::move(fn)); + } + + cond_.notify_one(); + return true; + } + + void shutdown() override { + // Stop all worker threads... + { + std::unique_lock lock(mutex_); + shutdown_ = true; + } + + cond_.notify_all(); + + // Join... + for (auto &t : threads_) { + t.join(); + } + } + +private: + struct worker { + explicit worker(ThreadPool &pool) : pool_(pool) {} + + void operator()() { + for (;;) { + std::function fn; + { + std::unique_lock lock(pool_.mutex_); + + pool_.cond_.wait( + lock, [&] { return !pool_.jobs_.empty() || pool_.shutdown_; }); + + if (pool_.shutdown_ && pool_.jobs_.empty()) { break; } + + fn = pool_.jobs_.front(); + pool_.jobs_.pop_front(); + } + + assert(true == static_cast(fn)); + fn(); + } + +#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) && \ + !defined(LIBRESSL_VERSION_NUMBER) + OPENSSL_thread_stop(); +#endif + } + + ThreadPool &pool_; + }; + friend struct worker; + + std::vector threads_; + std::list> jobs_; + + bool shutdown_; + size_t max_queued_requests_ = 0; + + std::condition_variable cond_; + std::mutex mutex_; +}; + +using Logger = std::function; + +// Forward declaration for Error type +enum class Error; +using ErrorLogger = std::function; + +using SocketOptions = std::function; + +namespace detail { + +bool set_socket_opt_impl(socket_t sock, int level, int optname, + const void *optval, socklen_t optlen); +bool set_socket_opt(socket_t sock, int level, int optname, int opt); +bool set_socket_opt_time(socket_t sock, int level, int optname, time_t sec, + time_t usec); + +} // namespace detail + +void default_socket_options(socket_t sock); + +const char *status_message(int status); + +std::string get_bearer_token_auth(const Request &req); + +namespace detail { + +class MatcherBase { +public: + MatcherBase(std::string pattern) : pattern_(pattern) {} + virtual ~MatcherBase() = default; + + const std::string &pattern() const { return pattern_; } + + // Match request path and populate its matches and + virtual bool match(Request &request) const = 0; + +private: + std::string pattern_; +}; + +/** + * Captures parameters in request path and stores them in Request::path_params + * + * Capture name is a substring of a pattern from : to /. + * The rest of the pattern is matched against the request path directly + * Parameters are captured starting from the next character after + * the end of the last matched static pattern fragment until the next /. + * + * Example pattern: + * "/path/fragments/:capture/more/fragments/:second_capture" + * Static fragments: + * "/path/fragments/", "more/fragments/" + * + * Given the following request path: + * "/path/fragments/:1/more/fragments/:2" + * the resulting capture will be + * {{"capture", "1"}, {"second_capture", "2"}} + */ +class PathParamsMatcher final : public MatcherBase { +public: + PathParamsMatcher(const std::string &pattern); + + bool match(Request &request) const override; + +private: + // Treat segment separators as the end of path parameter capture + // Does not need to handle query parameters as they are parsed before path + // matching + static constexpr char separator = '/'; + + // Contains static path fragments to match against, excluding the '/' after + // path params + // Fragments are separated by path params + std::vector static_fragments_; + // Stores the names of the path parameters to be used as keys in the + // Request::path_params map + std::vector param_names_; +}; + +/** + * Performs std::regex_match on request path + * and stores the result in Request::matches + * + * Note that regex match is performed directly on the whole request. + * This means that wildcard patterns may match multiple path segments with /: + * "/begin/(.*)/end" will match both "/begin/middle/end" and "/begin/1/2/end". + */ +class RegexMatcher final : public MatcherBase { +public: + RegexMatcher(const std::string &pattern) + : MatcherBase(pattern), regex_(pattern) {} + + bool match(Request &request) const override; + +private: + std::regex regex_; +}; + +ssize_t write_headers(Stream &strm, const Headers &headers); + +std::string make_host_and_port_string(const std::string &host, int port, + bool is_ssl); + +} // namespace detail + +class Server { +public: + using Handler = std::function; + + using ExceptionHandler = + std::function; + + enum class HandlerResponse { + Handled, + Unhandled, + }; + using HandlerWithResponse = + std::function; + + using HandlerWithContentReader = std::function; + + using Expect100ContinueHandler = + std::function; + + Server(); + + virtual ~Server(); + + virtual bool is_valid() const; + + Server &Get(const std::string &pattern, Handler handler); + Server &Post(const std::string &pattern, Handler handler); + Server &Post(const std::string &pattern, HandlerWithContentReader handler); + Server &Put(const std::string &pattern, Handler handler); + Server &Put(const std::string &pattern, HandlerWithContentReader handler); + Server &Patch(const std::string &pattern, Handler handler); + Server &Patch(const std::string &pattern, HandlerWithContentReader handler); + Server &Delete(const std::string &pattern, Handler handler); + Server &Delete(const std::string &pattern, HandlerWithContentReader handler); + Server &Options(const std::string &pattern, Handler handler); + + bool set_base_dir(const std::string &dir, + const std::string &mount_point = std::string()); + bool set_mount_point(const std::string &mount_point, const std::string &dir, + Headers headers = Headers()); + bool remove_mount_point(const std::string &mount_point); + Server &set_file_extension_and_mimetype_mapping(const std::string &ext, + const std::string &mime); + Server &set_default_file_mimetype(const std::string &mime); + Server &set_file_request_handler(Handler handler); + + template + Server &set_error_handler(ErrorHandlerFunc &&handler) { + return set_error_handler_core( + std::forward(handler), + std::is_convertible{}); + } + + Server &set_exception_handler(ExceptionHandler handler); + + Server &set_pre_routing_handler(HandlerWithResponse handler); + Server &set_post_routing_handler(Handler handler); + + Server &set_pre_request_handler(HandlerWithResponse handler); + + Server &set_expect_100_continue_handler(Expect100ContinueHandler handler); + Server &set_logger(Logger logger); + Server &set_pre_compression_logger(Logger logger); + Server &set_error_logger(ErrorLogger error_logger); + + Server &set_address_family(int family); + Server &set_tcp_nodelay(bool on); + Server &set_ipv6_v6only(bool on); + Server &set_socket_options(SocketOptions socket_options); + + Server &set_default_headers(Headers headers); + Server & + set_header_writer(std::function const &writer); + + Server &set_trusted_proxies(const std::vector &proxies); + + Server &set_keep_alive_max_count(size_t count); + Server &set_keep_alive_timeout(time_t sec); + + Server &set_read_timeout(time_t sec, time_t usec = 0); + template + Server &set_read_timeout(const std::chrono::duration &duration); + + Server &set_write_timeout(time_t sec, time_t usec = 0); + template + Server &set_write_timeout(const std::chrono::duration &duration); + + Server &set_idle_interval(time_t sec, time_t usec = 0); + template + Server &set_idle_interval(const std::chrono::duration &duration); + + Server &set_payload_max_length(size_t length); + + bool bind_to_port(const std::string &host, int port, int socket_flags = 0); + int bind_to_any_port(const std::string &host, int socket_flags = 0); + bool listen_after_bind(); + + bool listen(const std::string &host, int port, int socket_flags = 0); + + bool is_running() const; + void wait_until_ready() const; + void stop(); + void decommission(); + + std::function new_task_queue; + +protected: + bool process_request(Stream &strm, const std::string &remote_addr, + int remote_port, const std::string &local_addr, + int local_port, bool close_connection, + bool &connection_closed, + const std::function &setup_request); + + std::atomic svr_sock_{INVALID_SOCKET}; + + std::vector trusted_proxies_; + + size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; + time_t keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND; + time_t read_timeout_sec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND; + time_t read_timeout_usec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND; + time_t write_timeout_sec_ = CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND; + time_t write_timeout_usec_ = CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND; + time_t idle_interval_sec_ = CPPHTTPLIB_IDLE_INTERVAL_SECOND; + time_t idle_interval_usec_ = CPPHTTPLIB_IDLE_INTERVAL_USECOND; + size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; + +private: + using Handlers = + std::vector, Handler>>; + using HandlersForContentReader = + std::vector, + HandlerWithContentReader>>; + + static std::unique_ptr + make_matcher(const std::string &pattern); + + Server &set_error_handler_core(HandlerWithResponse handler, std::true_type); + Server &set_error_handler_core(Handler handler, std::false_type); + + socket_t create_server_socket(const std::string &host, int port, + int socket_flags, + SocketOptions socket_options) const; + int bind_internal(const std::string &host, int port, int socket_flags); + bool listen_internal(); + + bool routing(Request &req, Response &res, Stream &strm); + bool handle_file_request(const Request &req, Response &res); + bool dispatch_request(Request &req, Response &res, + const Handlers &handlers) const; + bool dispatch_request_for_content_reader( + Request &req, Response &res, ContentReader content_reader, + const HandlersForContentReader &handlers) const; + + bool parse_request_line(const char *s, Request &req) const; + void apply_ranges(const Request &req, Response &res, + std::string &content_type, std::string &boundary) const; + bool write_response(Stream &strm, bool close_connection, Request &req, + Response &res); + bool write_response_with_content(Stream &strm, bool close_connection, + const Request &req, Response &res); + bool write_response_core(Stream &strm, bool close_connection, + const Request &req, Response &res, + bool need_apply_ranges); + bool write_content_with_provider(Stream &strm, const Request &req, + Response &res, const std::string &boundary, + const std::string &content_type); + bool read_content(Stream &strm, Request &req, Response &res); + bool read_content_with_content_receiver(Stream &strm, Request &req, + Response &res, + ContentReceiver receiver, + FormDataHeader multipart_header, + ContentReceiver multipart_receiver); + bool read_content_core(Stream &strm, Request &req, Response &res, + ContentReceiver receiver, + FormDataHeader multipart_header, + ContentReceiver multipart_receiver) const; + + virtual bool process_and_close_socket(socket_t sock); + + void output_log(const Request &req, const Response &res) const; + void output_pre_compression_log(const Request &req, + const Response &res) const; + void output_error_log(const Error &err, const Request *req) const; + + std::atomic is_running_{false}; + std::atomic is_decommissioned{false}; + + struct MountPointEntry { + std::string mount_point; + std::string base_dir; + Headers headers; + }; + std::vector base_dirs_; + std::map file_extension_and_mimetype_map_; + std::string default_file_mimetype_ = "application/octet-stream"; + Handler file_request_handler_; + + Handlers get_handlers_; + Handlers post_handlers_; + HandlersForContentReader post_handlers_for_content_reader_; + Handlers put_handlers_; + HandlersForContentReader put_handlers_for_content_reader_; + Handlers patch_handlers_; + HandlersForContentReader patch_handlers_for_content_reader_; + Handlers delete_handlers_; + HandlersForContentReader delete_handlers_for_content_reader_; + Handlers options_handlers_; + + HandlerWithResponse error_handler_; + ExceptionHandler exception_handler_; + HandlerWithResponse pre_routing_handler_; + Handler post_routing_handler_; + HandlerWithResponse pre_request_handler_; + Expect100ContinueHandler expect_100_continue_handler_; + + mutable std::mutex logger_mutex_; + Logger logger_; + Logger pre_compression_logger_; + ErrorLogger error_logger_; + + int address_family_ = AF_UNSPEC; + bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; + bool ipv6_v6only_ = CPPHTTPLIB_IPV6_V6ONLY; + SocketOptions socket_options_ = default_socket_options; + + Headers default_headers_; + std::function header_writer_ = + detail::write_headers; +}; + +enum class Error { + Success = 0, + Unknown, + Connection, + BindIPAddress, + Read, + Write, + ExceedRedirectCount, + Canceled, + SSLConnection, + SSLLoadingCerts, + SSLServerVerification, + SSLServerHostnameVerification, + UnsupportedMultipartBoundaryChars, + Compression, + ConnectionTimeout, + ProxyConnection, + ResourceExhaustion, + TooManyFormDataFiles, + ExceedMaxPayloadSize, + ExceedUriMaxLength, + ExceedMaxSocketDescriptorCount, + InvalidRequestLine, + InvalidHTTPMethod, + InvalidHTTPVersion, + InvalidHeaders, + MultipartParsing, + OpenFile, + Listen, + GetSockName, + UnsupportedAddressFamily, + HTTPParsing, + InvalidRangeHeader, + + // For internal use only + SSLPeerCouldBeClosed_, +}; + +std::string to_string(Error error); + +std::ostream &operator<<(std::ostream &os, const Error &obj); + +class Result { +public: + Result() = default; + Result(std::unique_ptr &&res, Error err, + Headers &&request_headers = Headers{}) + : res_(std::move(res)), err_(err), + request_headers_(std::move(request_headers)) {} +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + Result(std::unique_ptr &&res, Error err, Headers &&request_headers, + int ssl_error) + : res_(std::move(res)), err_(err), + request_headers_(std::move(request_headers)), ssl_error_(ssl_error) {} + Result(std::unique_ptr &&res, Error err, Headers &&request_headers, + int ssl_error, unsigned long ssl_openssl_error) + : res_(std::move(res)), err_(err), + request_headers_(std::move(request_headers)), ssl_error_(ssl_error), + ssl_openssl_error_(ssl_openssl_error) {} +#endif + // Response + operator bool() const { return res_ != nullptr; } + bool operator==(std::nullptr_t) const { return res_ == nullptr; } + bool operator!=(std::nullptr_t) const { return res_ != nullptr; } + const Response &value() const { return *res_; } + Response &value() { return *res_; } + const Response &operator*() const { return *res_; } + Response &operator*() { return *res_; } + const Response *operator->() const { return res_.get(); } + Response *operator->() { return res_.get(); } + + // Error + Error error() const { return err_; } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + // SSL Error + int ssl_error() const { return ssl_error_; } + // OpenSSL Error + unsigned long ssl_openssl_error() const { return ssl_openssl_error_; } +#endif + + // Request Headers + bool has_request_header(const std::string &key) const; + std::string get_request_header_value(const std::string &key, + const char *def = "", + size_t id = 0) const; + size_t get_request_header_value_u64(const std::string &key, size_t def = 0, + size_t id = 0) const; + size_t get_request_header_value_count(const std::string &key) const; + +private: + std::unique_ptr res_; + Error err_ = Error::Unknown; + Headers request_headers_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + int ssl_error_ = 0; + unsigned long ssl_openssl_error_ = 0; +#endif +}; + +class ClientImpl { +public: + explicit ClientImpl(const std::string &host); + + explicit ClientImpl(const std::string &host, int port); + + explicit ClientImpl(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path); + + virtual ~ClientImpl(); + + virtual bool is_valid() const; + + // clang-format off + Result Get(const std::string &path, DownloadProgress progress = nullptr); + Result Get(const std::string &path, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, ResponseHandler response_handler, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Headers &headers, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Headers &headers, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Headers &headers, ResponseHandler response_handler, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, const Headers &headers, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, const Headers &headers, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, const Headers &headers, ResponseHandler response_handler, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + + Result Head(const std::string &path); + Result Head(const std::string &path, const Headers &headers); + + Result Post(const std::string &path); + Result Post(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Params ¶ms); + Result Post(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers); + Result Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const Params ¶ms); + Result Post(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const FormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + + Result Put(const std::string &path); + Result Put(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Params ¶ms); + Result Put(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers); + Result Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const Params ¶ms); + Result Put(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const FormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + + Result Patch(const std::string &path); + Result Patch(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Params ¶ms); + Result Patch(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const Params ¶ms); + Result Patch(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const FormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + + Result Delete(const std::string &path, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const char *body, size_t content_length, const std::string &content_type, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const std::string &body, const std::string &content_type, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Params ¶ms, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Headers &headers, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Headers &headers, const Params ¶ms, DownloadProgress progress = nullptr); + + Result Options(const std::string &path); + Result Options(const std::string &path, const Headers &headers); + // clang-format on + + bool send(Request &req, Response &res, Error &error); + Result send(const Request &req); + + void stop(); + + std::string host() const; + int port() const; + + size_t is_socket_open() const; + socket_t socket() const; + + void set_hostname_addr_map(std::map addr_map); + + void set_default_headers(Headers headers); + + void + set_header_writer(std::function const &writer); + + void set_address_family(int family); + void set_tcp_nodelay(bool on); + void set_ipv6_v6only(bool on); + void set_socket_options(SocketOptions socket_options); + + void set_connection_timeout(time_t sec, time_t usec = 0); + template + void + set_connection_timeout(const std::chrono::duration &duration); + + void set_read_timeout(time_t sec, time_t usec = 0); + template + void set_read_timeout(const std::chrono::duration &duration); + + void set_write_timeout(time_t sec, time_t usec = 0); + template + void set_write_timeout(const std::chrono::duration &duration); + + void set_max_timeout(time_t msec); + template + void set_max_timeout(const std::chrono::duration &duration); + + void set_basic_auth(const std::string &username, const std::string &password); + void set_bearer_token_auth(const std::string &token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_digest_auth(const std::string &username, + const std::string &password); +#endif + + void set_keep_alive(bool on); + void set_follow_location(bool on); + + void set_path_encode(bool on); + + void set_compress(bool on); + + void set_decompress(bool on); + + void set_interface(const std::string &intf); + + void set_proxy(const std::string &host, int port); + void set_proxy_basic_auth(const std::string &username, + const std::string &password); + void set_proxy_bearer_token_auth(const std::string &token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_proxy_digest_auth(const std::string &username, + const std::string &password); +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path = std::string()); + void set_ca_cert_store(X509_STORE *ca_cert_store); + X509_STORE *create_ca_cert_store(const char *ca_cert, std::size_t size) const; +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void enable_server_certificate_verification(bool enabled); + void enable_server_hostname_verification(bool enabled); + void set_server_certificate_verifier( + std::function verifier); +#endif + + void set_logger(Logger logger); + void set_error_logger(ErrorLogger error_logger); + +protected: + struct Socket { + socket_t sock = INVALID_SOCKET; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + SSL *ssl = nullptr; +#endif + + bool is_open() const { return sock != INVALID_SOCKET; } + }; + + virtual bool create_and_connect_socket(Socket &socket, Error &error); + + // All of: + // shutdown_ssl + // shutdown_socket + // close_socket + // should ONLY be called when socket_mutex_ is locked. + // Also, shutdown_ssl and close_socket should also NOT be called concurrently + // with a DIFFERENT thread sending requests using that socket. + virtual void shutdown_ssl(Socket &socket, bool shutdown_gracefully); + void shutdown_socket(Socket &socket) const; + void close_socket(Socket &socket); + + bool process_request(Stream &strm, Request &req, Response &res, + bool close_connection, Error &error); + + bool write_content_with_provider(Stream &strm, const Request &req, + Error &error) const; + + void copy_settings(const ClientImpl &rhs); + + void output_log(const Request &req, const Response &res) const; + void output_error_log(const Error &err, const Request *req) const; + + // Socket endpoint information + const std::string host_; + const int port_; + const std::string host_and_port_; + + // Current open socket + Socket socket_; + mutable std::mutex socket_mutex_; + std::recursive_mutex request_mutex_; + + // These are all protected under socket_mutex + size_t socket_requests_in_flight_ = 0; + std::thread::id socket_requests_are_from_thread_ = std::thread::id(); + bool socket_should_be_closed_when_request_is_done_ = false; + + // Hostname-IP map + std::map addr_map_; + + // Default headers + Headers default_headers_; + + // Header writer + std::function header_writer_ = + detail::write_headers; + + // Settings + std::string client_cert_path_; + std::string client_key_path_; + + time_t connection_timeout_sec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND; + time_t connection_timeout_usec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND; + time_t read_timeout_sec_ = CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND; + time_t read_timeout_usec_ = CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND; + time_t write_timeout_sec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND; + time_t write_timeout_usec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND; + time_t max_timeout_msec_ = CPPHTTPLIB_CLIENT_MAX_TIMEOUT_MSECOND; + + std::string basic_auth_username_; + std::string basic_auth_password_; + std::string bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + std::string digest_auth_username_; + std::string digest_auth_password_; +#endif + + bool keep_alive_ = false; + bool follow_location_ = false; + + bool path_encode_ = true; + + int address_family_ = AF_UNSPEC; + bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; + bool ipv6_v6only_ = CPPHTTPLIB_IPV6_V6ONLY; + SocketOptions socket_options_ = nullptr; + + bool compress_ = false; + bool decompress_ = true; + + std::string interface_; + + std::string proxy_host_; + int proxy_port_ = -1; + + std::string proxy_basic_auth_username_; + std::string proxy_basic_auth_password_; + std::string proxy_bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + std::string proxy_digest_auth_username_; + std::string proxy_digest_auth_password_; +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + std::string ca_cert_file_path_; + std::string ca_cert_dir_path_; + + X509_STORE *ca_cert_store_ = nullptr; +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + bool server_certificate_verification_ = true; + bool server_hostname_verification_ = true; + std::function server_certificate_verifier_; +#endif + + mutable std::mutex logger_mutex_; + Logger logger_; + ErrorLogger error_logger_; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + int last_ssl_error_ = 0; + unsigned long last_openssl_error_ = 0; +#endif + +private: + bool send_(Request &req, Response &res, Error &error); + Result send_(Request &&req); + + socket_t create_client_socket(Error &error) const; + bool read_response_line(Stream &strm, const Request &req, + Response &res) const; + bool write_request(Stream &strm, Request &req, bool close_connection, + Error &error); + bool redirect(Request &req, Response &res, Error &error); + bool create_redirect_client(const std::string &scheme, + const std::string &host, int port, Request &req, + Response &res, const std::string &path, + const std::string &location, Error &error); + template void setup_redirect_client(ClientType &client); + bool handle_request(Stream &strm, Request &req, Response &res, + bool close_connection, Error &error); + std::unique_ptr send_with_content_provider( + Request &req, const char *body, size_t content_length, + ContentProvider content_provider, + ContentProviderWithoutLength content_provider_without_length, + const std::string &content_type, Error &error); + Result send_with_content_provider( + const std::string &method, const std::string &path, + const Headers &headers, const char *body, size_t content_length, + ContentProvider content_provider, + ContentProviderWithoutLength content_provider_without_length, + const std::string &content_type, UploadProgress progress); + ContentProviderWithoutLength get_multipart_content_provider( + const std::string &boundary, const UploadFormDataItems &items, + const FormDataProviderItems &provider_items) const; + + virtual bool + process_socket(const Socket &socket, + std::chrono::time_point start_time, + std::function callback); + virtual bool is_ssl() const; +}; + +class Client { +public: + // Universal interface + explicit Client(const std::string &scheme_host_port); + + explicit Client(const std::string &scheme_host_port, + const std::string &client_cert_path, + const std::string &client_key_path); + + // HTTP only interface + explicit Client(const std::string &host, int port); + + explicit Client(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path); + + Client(Client &&) = default; + Client &operator=(Client &&) = default; + + ~Client(); + + bool is_valid() const; + + // clang-format off + Result Get(const std::string &path, DownloadProgress progress = nullptr); + Result Get(const std::string &path, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, ResponseHandler response_handler, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Headers &headers, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Headers &headers, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Headers &headers, ResponseHandler response_handler, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, const Headers &headers, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, const Headers &headers, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, const Headers &headers, ResponseHandler response_handler, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + + Result Head(const std::string &path); + Result Head(const std::string &path, const Headers &headers); + + Result Post(const std::string &path); + Result Post(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Params ¶ms); + Result Post(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers); + Result Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const Params ¶ms); + Result Post(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const FormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + + Result Put(const std::string &path); + Result Put(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Params ¶ms); + Result Put(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers); + Result Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const Params ¶ms); + Result Put(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const FormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + + Result Patch(const std::string &path); + Result Patch(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Params ¶ms); + Result Patch(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers); + Result Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const Params ¶ms); + Result Patch(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const FormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + + Result Delete(const std::string &path, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const char *body, size_t content_length, const std::string &content_type, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const std::string &body, const std::string &content_type, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Params ¶ms, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Headers &headers, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Headers &headers, const Params ¶ms, DownloadProgress progress = nullptr); + + Result Options(const std::string &path); + Result Options(const std::string &path, const Headers &headers); + // clang-format on + + bool send(Request &req, Response &res, Error &error); + Result send(const Request &req); + + void stop(); + + std::string host() const; + int port() const; + + size_t is_socket_open() const; + socket_t socket() const; + + void set_hostname_addr_map(std::map addr_map); + + void set_default_headers(Headers headers); + + void + set_header_writer(std::function const &writer); + + void set_address_family(int family); + void set_tcp_nodelay(bool on); + void set_socket_options(SocketOptions socket_options); + + void set_connection_timeout(time_t sec, time_t usec = 0); + template + void + set_connection_timeout(const std::chrono::duration &duration); + + void set_read_timeout(time_t sec, time_t usec = 0); + template + void set_read_timeout(const std::chrono::duration &duration); + + void set_write_timeout(time_t sec, time_t usec = 0); + template + void set_write_timeout(const std::chrono::duration &duration); + + void set_max_timeout(time_t msec); + template + void set_max_timeout(const std::chrono::duration &duration); + + void set_basic_auth(const std::string &username, const std::string &password); + void set_bearer_token_auth(const std::string &token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_digest_auth(const std::string &username, + const std::string &password); +#endif + + void set_keep_alive(bool on); + void set_follow_location(bool on); + + void set_path_encode(bool on); + void set_url_encode(bool on); + + void set_compress(bool on); + + void set_decompress(bool on); + + void set_interface(const std::string &intf); + + void set_proxy(const std::string &host, int port); + void set_proxy_basic_auth(const std::string &username, + const std::string &password); + void set_proxy_bearer_token_auth(const std::string &token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_proxy_digest_auth(const std::string &username, + const std::string &password); +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void enable_server_certificate_verification(bool enabled); + void enable_server_hostname_verification(bool enabled); + void set_server_certificate_verifier( + std::function verifier); +#endif + + void set_logger(Logger logger); + void set_error_logger(ErrorLogger error_logger); + + // SSL +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path = std::string()); + + void set_ca_cert_store(X509_STORE *ca_cert_store); + void load_ca_cert_store(const char *ca_cert, std::size_t size); + + long get_openssl_verify_result() const; + + SSL_CTX *ssl_context() const; +#endif + +private: + std::unique_ptr cli_; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + bool is_ssl_ = false; +#endif +}; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +class SSLServer : public Server { +public: + SSLServer(const char *cert_path, const char *private_key_path, + const char *client_ca_cert_file_path = nullptr, + const char *client_ca_cert_dir_path = nullptr, + const char *private_key_password = nullptr); + + SSLServer(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store = nullptr); + + SSLServer( + const std::function &setup_ssl_ctx_callback); + + ~SSLServer() override; + + bool is_valid() const override; + + SSL_CTX *ssl_context() const; + + void update_certs(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store = nullptr); + + int ssl_last_error() const { return last_ssl_error_; } + +private: + bool process_and_close_socket(socket_t sock) override; + + STACK_OF(X509_NAME) * extract_ca_names_from_x509_store(X509_STORE *store); + + SSL_CTX *ctx_; + std::mutex ctx_mutex_; + + int last_ssl_error_ = 0; +}; + +class SSLClient final : public ClientImpl { +public: + explicit SSLClient(const std::string &host); + + explicit SSLClient(const std::string &host, int port); + + explicit SSLClient(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path, + const std::string &private_key_password = std::string()); + + explicit SSLClient(const std::string &host, int port, X509 *client_cert, + EVP_PKEY *client_key, + const std::string &private_key_password = std::string()); + + ~SSLClient() override; + + bool is_valid() const override; + + void set_ca_cert_store(X509_STORE *ca_cert_store); + void load_ca_cert_store(const char *ca_cert, std::size_t size); + + long get_openssl_verify_result() const; + + SSL_CTX *ssl_context() const; + +private: + bool create_and_connect_socket(Socket &socket, Error &error) override; + void shutdown_ssl(Socket &socket, bool shutdown_gracefully) override; + void shutdown_ssl_impl(Socket &socket, bool shutdown_gracefully); + + bool + process_socket(const Socket &socket, + std::chrono::time_point start_time, + std::function callback) override; + bool is_ssl() const override; + + bool connect_with_proxy( + Socket &sock, + std::chrono::time_point start_time, + Response &res, bool &success, Error &error); + bool initialize_ssl(Socket &socket, Error &error); + + bool load_certs(); + + bool verify_host(X509 *server_cert) const; + bool verify_host_with_subject_alt_name(X509 *server_cert) const; + bool verify_host_with_common_name(X509 *server_cert) const; + bool check_host_name(const char *pattern, size_t pattern_len) const; + + SSL_CTX *ctx_; + std::mutex ctx_mutex_; + std::once_flag initialize_cert_; + + std::vector host_components_; + + long verify_result_ = 0; + + friend class ClientImpl; +}; +#endif + +/* + * Implementation of template methods. + */ + +namespace detail { + +template +inline void duration_to_sec_and_usec(const T &duration, U callback) { + auto sec = std::chrono::duration_cast(duration).count(); + auto usec = std::chrono::duration_cast( + duration - std::chrono::seconds(sec)) + .count(); + callback(static_cast(sec), static_cast(usec)); +} + +template inline constexpr size_t str_len(const char (&)[N]) { + return N - 1; +} + +inline bool is_numeric(const std::string &str) { + return !str.empty() && + std::all_of(str.cbegin(), str.cend(), + [](unsigned char c) { return std::isdigit(c); }); +} + +inline size_t get_header_value_u64(const Headers &headers, + const std::string &key, size_t def, + size_t id, bool &is_invalid_value) { + is_invalid_value = false; + auto rng = headers.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { + if (is_numeric(it->second)) { + return std::strtoull(it->second.data(), nullptr, 10); + } else { + is_invalid_value = true; + } + } + return def; +} + +inline size_t get_header_value_u64(const Headers &headers, + const std::string &key, size_t def, + size_t id) { + auto dummy = false; + return get_header_value_u64(headers, key, def, id, dummy); +} + +} // namespace detail + +inline size_t Request::get_header_value_u64(const std::string &key, size_t def, + size_t id) const { + return detail::get_header_value_u64(headers, key, def, id); +} + +inline size_t Response::get_header_value_u64(const std::string &key, size_t def, + size_t id) const { + return detail::get_header_value_u64(headers, key, def, id); +} + +namespace detail { + +inline bool set_socket_opt_impl(socket_t sock, int level, int optname, + const void *optval, socklen_t optlen) { + return setsockopt(sock, level, optname, +#ifdef _WIN32 + reinterpret_cast(optval), +#else + optval, +#endif + optlen) == 0; +} + +inline bool set_socket_opt(socket_t sock, int level, int optname, int optval) { + return set_socket_opt_impl(sock, level, optname, &optval, sizeof(optval)); +} + +inline bool set_socket_opt_time(socket_t sock, int level, int optname, + time_t sec, time_t usec) { +#ifdef _WIN32 + auto timeout = static_cast(sec * 1000 + usec / 1000); +#else + timeval timeout; + timeout.tv_sec = static_cast(sec); + timeout.tv_usec = static_cast(usec); +#endif + return set_socket_opt_impl(sock, level, optname, &timeout, sizeof(timeout)); +} + +} // namespace detail + +inline void default_socket_options(socket_t sock) { + detail::set_socket_opt(sock, SOL_SOCKET, +#ifdef SO_REUSEPORT + SO_REUSEPORT, +#else + SO_REUSEADDR, +#endif + 1); +} + +inline const char *status_message(int status) { + switch (status) { + case StatusCode::Continue_100: return "Continue"; + case StatusCode::SwitchingProtocol_101: return "Switching Protocol"; + case StatusCode::Processing_102: return "Processing"; + case StatusCode::EarlyHints_103: return "Early Hints"; + case StatusCode::OK_200: return "OK"; + case StatusCode::Created_201: return "Created"; + case StatusCode::Accepted_202: return "Accepted"; + case StatusCode::NonAuthoritativeInformation_203: + return "Non-Authoritative Information"; + case StatusCode::NoContent_204: return "No Content"; + case StatusCode::ResetContent_205: return "Reset Content"; + case StatusCode::PartialContent_206: return "Partial Content"; + case StatusCode::MultiStatus_207: return "Multi-Status"; + case StatusCode::AlreadyReported_208: return "Already Reported"; + case StatusCode::IMUsed_226: return "IM Used"; + case StatusCode::MultipleChoices_300: return "Multiple Choices"; + case StatusCode::MovedPermanently_301: return "Moved Permanently"; + case StatusCode::Found_302: return "Found"; + case StatusCode::SeeOther_303: return "See Other"; + case StatusCode::NotModified_304: return "Not Modified"; + case StatusCode::UseProxy_305: return "Use Proxy"; + case StatusCode::unused_306: return "unused"; + case StatusCode::TemporaryRedirect_307: return "Temporary Redirect"; + case StatusCode::PermanentRedirect_308: return "Permanent Redirect"; + case StatusCode::BadRequest_400: return "Bad Request"; + case StatusCode::Unauthorized_401: return "Unauthorized"; + case StatusCode::PaymentRequired_402: return "Payment Required"; + case StatusCode::Forbidden_403: return "Forbidden"; + case StatusCode::NotFound_404: return "Not Found"; + case StatusCode::MethodNotAllowed_405: return "Method Not Allowed"; + case StatusCode::NotAcceptable_406: return "Not Acceptable"; + case StatusCode::ProxyAuthenticationRequired_407: + return "Proxy Authentication Required"; + case StatusCode::RequestTimeout_408: return "Request Timeout"; + case StatusCode::Conflict_409: return "Conflict"; + case StatusCode::Gone_410: return "Gone"; + case StatusCode::LengthRequired_411: return "Length Required"; + case StatusCode::PreconditionFailed_412: return "Precondition Failed"; + case StatusCode::PayloadTooLarge_413: return "Payload Too Large"; + case StatusCode::UriTooLong_414: return "URI Too Long"; + case StatusCode::UnsupportedMediaType_415: return "Unsupported Media Type"; + case StatusCode::RangeNotSatisfiable_416: return "Range Not Satisfiable"; + case StatusCode::ExpectationFailed_417: return "Expectation Failed"; + case StatusCode::ImATeapot_418: return "I'm a teapot"; + case StatusCode::MisdirectedRequest_421: return "Misdirected Request"; + case StatusCode::UnprocessableContent_422: return "Unprocessable Content"; + case StatusCode::Locked_423: return "Locked"; + case StatusCode::FailedDependency_424: return "Failed Dependency"; + case StatusCode::TooEarly_425: return "Too Early"; + case StatusCode::UpgradeRequired_426: return "Upgrade Required"; + case StatusCode::PreconditionRequired_428: return "Precondition Required"; + case StatusCode::TooManyRequests_429: return "Too Many Requests"; + case StatusCode::RequestHeaderFieldsTooLarge_431: + return "Request Header Fields Too Large"; + case StatusCode::UnavailableForLegalReasons_451: + return "Unavailable For Legal Reasons"; + case StatusCode::NotImplemented_501: return "Not Implemented"; + case StatusCode::BadGateway_502: return "Bad Gateway"; + case StatusCode::ServiceUnavailable_503: return "Service Unavailable"; + case StatusCode::GatewayTimeout_504: return "Gateway Timeout"; + case StatusCode::HttpVersionNotSupported_505: + return "HTTP Version Not Supported"; + case StatusCode::VariantAlsoNegotiates_506: return "Variant Also Negotiates"; + case StatusCode::InsufficientStorage_507: return "Insufficient Storage"; + case StatusCode::LoopDetected_508: return "Loop Detected"; + case StatusCode::NotExtended_510: return "Not Extended"; + case StatusCode::NetworkAuthenticationRequired_511: + return "Network Authentication Required"; + + default: + case StatusCode::InternalServerError_500: return "Internal Server Error"; + } +} + +inline std::string get_bearer_token_auth(const Request &req) { + if (req.has_header("Authorization")) { + constexpr auto bearer_header_prefix_len = detail::str_len("Bearer "); + return req.get_header_value("Authorization") + .substr(bearer_header_prefix_len); + } + return ""; +} + +template +inline Server & +Server::set_read_timeout(const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_read_timeout(sec, usec); }); + return *this; +} + +template +inline Server & +Server::set_write_timeout(const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_write_timeout(sec, usec); }); + return *this; +} + +template +inline Server & +Server::set_idle_interval(const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_idle_interval(sec, usec); }); + return *this; +} + +inline std::string to_string(const Error error) { + switch (error) { + case Error::Success: return "Success (no error)"; + case Error::Unknown: return "Unknown"; + case Error::Connection: return "Could not establish connection"; + case Error::BindIPAddress: return "Failed to bind IP address"; + case Error::Read: return "Failed to read connection"; + case Error::Write: return "Failed to write connection"; + case Error::ExceedRedirectCount: return "Maximum redirect count exceeded"; + case Error::Canceled: return "Connection handling canceled"; + case Error::SSLConnection: return "SSL connection failed"; + case Error::SSLLoadingCerts: return "SSL certificate loading failed"; + case Error::SSLServerVerification: return "SSL server verification failed"; + case Error::SSLServerHostnameVerification: + return "SSL server hostname verification failed"; + case Error::UnsupportedMultipartBoundaryChars: + return "Unsupported HTTP multipart boundary characters"; + case Error::Compression: return "Compression failed"; + case Error::ConnectionTimeout: return "Connection timed out"; + case Error::ProxyConnection: return "Proxy connection failed"; + case Error::ResourceExhaustion: return "Resource exhaustion"; + case Error::TooManyFormDataFiles: return "Too many form data files"; + case Error::ExceedMaxPayloadSize: return "Exceeded maximum payload size"; + case Error::ExceedUriMaxLength: return "Exceeded maximum URI length"; + case Error::ExceedMaxSocketDescriptorCount: + return "Exceeded maximum socket descriptor count"; + case Error::InvalidRequestLine: return "Invalid request line"; + case Error::InvalidHTTPMethod: return "Invalid HTTP method"; + case Error::InvalidHTTPVersion: return "Invalid HTTP version"; + case Error::InvalidHeaders: return "Invalid headers"; + case Error::MultipartParsing: return "Multipart parsing failed"; + case Error::OpenFile: return "Failed to open file"; + case Error::Listen: return "Failed to listen on socket"; + case Error::GetSockName: return "Failed to get socket name"; + case Error::UnsupportedAddressFamily: return "Unsupported address family"; + case Error::HTTPParsing: return "HTTP parsing failed"; + case Error::InvalidRangeHeader: return "Invalid Range header"; + default: break; + } + + return "Invalid"; +} + +inline std::ostream &operator<<(std::ostream &os, const Error &obj) { + os << to_string(obj); + os << " (" << static_cast::type>(obj) << ')'; + return os; +} + +inline size_t Result::get_request_header_value_u64(const std::string &key, + size_t def, + size_t id) const { + return detail::get_header_value_u64(request_headers_, key, def, id); +} + +template +inline void ClientImpl::set_connection_timeout( + const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec(duration, [&](time_t sec, time_t usec) { + set_connection_timeout(sec, usec); + }); +} + +template +inline void ClientImpl::set_read_timeout( + const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_read_timeout(sec, usec); }); +} + +template +inline void ClientImpl::set_write_timeout( + const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_write_timeout(sec, usec); }); +} + +template +inline void ClientImpl::set_max_timeout( + const std::chrono::duration &duration) { + auto msec = + std::chrono::duration_cast(duration).count(); + set_max_timeout(msec); +} + +template +inline void Client::set_connection_timeout( + const std::chrono::duration &duration) { + cli_->set_connection_timeout(duration); +} + +template +inline void +Client::set_read_timeout(const std::chrono::duration &duration) { + cli_->set_read_timeout(duration); +} + +template +inline void +Client::set_write_timeout(const std::chrono::duration &duration) { + cli_->set_write_timeout(duration); +} + +inline void Client::set_max_timeout(time_t msec) { + cli_->set_max_timeout(msec); +} + +template +inline void +Client::set_max_timeout(const std::chrono::duration &duration) { + cli_->set_max_timeout(duration); +} + +/* + * Forward declarations and types that will be part of the .h file if split into + * .h + .cc. + */ + +std::string hosted_at(const std::string &hostname); + +void hosted_at(const std::string &hostname, std::vector &addrs); + +// JavaScript-style URL encoding/decoding functions +std::string encode_uri_component(const std::string &value); +std::string encode_uri(const std::string &value); +std::string decode_uri_component(const std::string &value); +std::string decode_uri(const std::string &value); + +// RFC 3986 compliant URL component encoding/decoding functions +std::string encode_path_component(const std::string &component); +std::string decode_path_component(const std::string &component); +std::string encode_query_component(const std::string &component, + bool space_as_plus = true); +std::string decode_query_component(const std::string &component, + bool plus_as_space = true); + +std::string append_query_params(const std::string &path, const Params ¶ms); + +std::pair make_range_header(const Ranges &ranges); + +std::pair +make_basic_authentication_header(const std::string &username, + const std::string &password, + bool is_proxy = false); + +namespace detail { + +#if defined(_WIN32) +inline std::wstring u8string_to_wstring(const char *s) { + std::wstring ws; + auto len = static_cast(strlen(s)); + auto wlen = ::MultiByteToWideChar(CP_UTF8, 0, s, len, nullptr, 0); + if (wlen > 0) { + ws.resize(wlen); + wlen = ::MultiByteToWideChar( + CP_UTF8, 0, s, len, + const_cast(reinterpret_cast(ws.data())), wlen); + if (wlen != static_cast(ws.size())) { ws.clear(); } + } + return ws; +} +#endif + +struct FileStat { + FileStat(const std::string &path); + bool is_file() const; + bool is_dir() const; + +private: +#if defined(_WIN32) + struct _stat st_; +#else + struct stat st_; +#endif + int ret_ = -1; +}; + +std::string trim_copy(const std::string &s); + +void divide( + const char *data, std::size_t size, char d, + std::function + fn); + +void divide( + const std::string &str, char d, + std::function + fn); + +void split(const char *b, const char *e, char d, + std::function fn); + +void split(const char *b, const char *e, char d, size_t m, + std::function fn); + +bool process_client_socket( + socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t max_timeout_msec, + std::chrono::time_point start_time, + std::function callback); + +socket_t create_client_socket(const std::string &host, const std::string &ip, + int port, int address_family, bool tcp_nodelay, + bool ipv6_v6only, SocketOptions socket_options, + time_t connection_timeout_sec, + time_t connection_timeout_usec, + time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, + time_t write_timeout_usec, + const std::string &intf, Error &error); + +const char *get_header_value(const Headers &headers, const std::string &key, + const char *def, size_t id); + +std::string params_to_query_str(const Params ¶ms); + +void parse_query_text(const char *data, std::size_t size, Params ¶ms); + +void parse_query_text(const std::string &s, Params ¶ms); + +bool parse_multipart_boundary(const std::string &content_type, + std::string &boundary); + +bool parse_range_header(const std::string &s, Ranges &ranges); + +bool parse_accept_header(const std::string &s, + std::vector &content_types); + +int close_socket(socket_t sock); + +ssize_t send_socket(socket_t sock, const void *ptr, size_t size, int flags); + +ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags); + +enum class EncodingType { None = 0, Gzip, Brotli, Zstd }; + +EncodingType encoding_type(const Request &req, const Response &res); + +class BufferStream final : public Stream { +public: + BufferStream() = default; + ~BufferStream() override = default; + + bool is_readable() const override; + bool wait_readable() const override; + bool wait_writable() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; + void get_local_ip_and_port(std::string &ip, int &port) const override; + socket_t socket() const override; + time_t duration() const override; + + const std::string &get_buffer() const; + +private: + std::string buffer; + size_t position = 0; +}; + +class compressor { +public: + virtual ~compressor() = default; + + typedef std::function Callback; + virtual bool compress(const char *data, size_t data_length, bool last, + Callback callback) = 0; +}; + +class decompressor { +public: + virtual ~decompressor() = default; + + virtual bool is_valid() const = 0; + + typedef std::function Callback; + virtual bool decompress(const char *data, size_t data_length, + Callback callback) = 0; +}; + +class nocompressor final : public compressor { +public: + ~nocompressor() override = default; + + bool compress(const char *data, size_t data_length, bool /*last*/, + Callback callback) override; +}; + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +class gzip_compressor final : public compressor { +public: + gzip_compressor(); + ~gzip_compressor() override; + + bool compress(const char *data, size_t data_length, bool last, + Callback callback) override; + +private: + bool is_valid_ = false; + z_stream strm_; +}; + +class gzip_decompressor final : public decompressor { +public: + gzip_decompressor(); + ~gzip_decompressor() override; + + bool is_valid() const override; + + bool decompress(const char *data, size_t data_length, + Callback callback) override; + +private: + bool is_valid_ = false; + z_stream strm_; +}; +#endif + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT +class brotli_compressor final : public compressor { +public: + brotli_compressor(); + ~brotli_compressor(); + + bool compress(const char *data, size_t data_length, bool last, + Callback callback) override; + +private: + BrotliEncoderState *state_ = nullptr; +}; + +class brotli_decompressor final : public decompressor { +public: + brotli_decompressor(); + ~brotli_decompressor(); + + bool is_valid() const override; + + bool decompress(const char *data, size_t data_length, + Callback callback) override; + +private: + BrotliDecoderResult decoder_r; + BrotliDecoderState *decoder_s = nullptr; +}; +#endif + +#ifdef CPPHTTPLIB_ZSTD_SUPPORT +class zstd_compressor : public compressor { +public: + zstd_compressor(); + ~zstd_compressor(); + + bool compress(const char *data, size_t data_length, bool last, + Callback callback) override; + +private: + ZSTD_CCtx *ctx_ = nullptr; +}; + +class zstd_decompressor : public decompressor { +public: + zstd_decompressor(); + ~zstd_decompressor(); + + bool is_valid() const override; + + bool decompress(const char *data, size_t data_length, + Callback callback) override; + +private: + ZSTD_DCtx *ctx_ = nullptr; +}; +#endif + +// NOTE: until the read size reaches `fixed_buffer_size`, use `fixed_buffer` +// to store data. The call can set memory on stack for performance. +class stream_line_reader { +public: + stream_line_reader(Stream &strm, char *fixed_buffer, + size_t fixed_buffer_size); + const char *ptr() const; + size_t size() const; + bool end_with_crlf() const; + bool getline(); + +private: + void append(char c); + + Stream &strm_; + char *fixed_buffer_; + const size_t fixed_buffer_size_; + size_t fixed_buffer_used_size_ = 0; + std::string growable_buffer_; +}; + +class mmap { +public: + mmap(const char *path); + ~mmap(); + + bool open(const char *path); + void close(); + + bool is_open() const; + size_t size() const; + const char *data() const; + +private: +#if defined(_WIN32) + HANDLE hFile_ = NULL; + HANDLE hMapping_ = NULL; +#else + int fd_ = -1; +#endif + size_t size_ = 0; + void *addr_ = nullptr; + bool is_open_empty_file = false; +}; + +// NOTE: https://www.rfc-editor.org/rfc/rfc9110#section-5 +namespace fields { + +inline bool is_token_char(char c) { + return std::isalnum(c) || c == '!' || c == '#' || c == '$' || c == '%' || + c == '&' || c == '\'' || c == '*' || c == '+' || c == '-' || + c == '.' || c == '^' || c == '_' || c == '`' || c == '|' || c == '~'; +} + +inline bool is_token(const std::string &s) { + if (s.empty()) { return false; } + for (auto c : s) { + if (!is_token_char(c)) { return false; } + } + return true; +} + +inline bool is_field_name(const std::string &s) { return is_token(s); } + +inline bool is_vchar(char c) { return c >= 33 && c <= 126; } + +inline bool is_obs_text(char c) { return 128 <= static_cast(c); } + +inline bool is_field_vchar(char c) { return is_vchar(c) || is_obs_text(c); } + +inline bool is_field_content(const std::string &s) { + if (s.empty()) { return true; } + + if (s.size() == 1) { + return is_field_vchar(s[0]); + } else if (s.size() == 2) { + return is_field_vchar(s[0]) && is_field_vchar(s[1]); + } else { + size_t i = 0; + + if (!is_field_vchar(s[i])) { return false; } + i++; + + while (i < s.size() - 1) { + auto c = s[i++]; + if (c == ' ' || c == '\t' || is_field_vchar(c)) { + } else { + return false; + } + } + + return is_field_vchar(s[i]); + } +} + +inline bool is_field_value(const std::string &s) { return is_field_content(s); } + +} // namespace fields + +} // namespace detail + +// ---------------------------------------------------------------------------- + +/* + * Implementation that will be part of the .cc file if split into .h + .cc. + */ + +namespace detail { + +inline bool is_hex(char c, int &v) { + if (0x20 <= c && isdigit(c)) { + v = c - '0'; + return true; + } else if ('A' <= c && c <= 'F') { + v = c - 'A' + 10; + return true; + } else if ('a' <= c && c <= 'f') { + v = c - 'a' + 10; + return true; + } + return false; +} + +inline bool from_hex_to_i(const std::string &s, size_t i, size_t cnt, + int &val) { + if (i >= s.size()) { return false; } + + val = 0; + for (; cnt; i++, cnt--) { + if (!s[i]) { return false; } + auto v = 0; + if (is_hex(s[i], v)) { + val = val * 16 + v; + } else { + return false; + } + } + return true; +} + +inline std::string from_i_to_hex(size_t n) { + static const auto charset = "0123456789abcdef"; + std::string ret; + do { + ret = charset[n & 15] + ret; + n >>= 4; + } while (n > 0); + return ret; +} + +inline size_t to_utf8(int code, char *buff) { + if (code < 0x0080) { + buff[0] = static_cast(code & 0x7F); + return 1; + } else if (code < 0x0800) { + buff[0] = static_cast(0xC0 | ((code >> 6) & 0x1F)); + buff[1] = static_cast(0x80 | (code & 0x3F)); + return 2; + } else if (code < 0xD800) { + buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); + buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[2] = static_cast(0x80 | (code & 0x3F)); + return 3; + } else if (code < 0xE000) { // D800 - DFFF is invalid... + return 0; + } else if (code < 0x10000) { + buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); + buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[2] = static_cast(0x80 | (code & 0x3F)); + return 3; + } else if (code < 0x110000) { + buff[0] = static_cast(0xF0 | ((code >> 18) & 0x7)); + buff[1] = static_cast(0x80 | ((code >> 12) & 0x3F)); + buff[2] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[3] = static_cast(0x80 | (code & 0x3F)); + return 4; + } + + // NOTREACHED + return 0; +} + +// NOTE: This code came up with the following stackoverflow post: +// https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c +inline std::string base64_encode(const std::string &in) { + static const auto lookup = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + std::string out; + out.reserve(in.size()); + + auto val = 0; + auto valb = -6; + + for (auto c : in) { + val = (val << 8) + static_cast(c); + valb += 8; + while (valb >= 0) { + out.push_back(lookup[(val >> valb) & 0x3F]); + valb -= 6; + } + } + + if (valb > -6) { out.push_back(lookup[((val << 8) >> (valb + 8)) & 0x3F]); } + + while (out.size() % 4) { + out.push_back('='); + } + + return out; +} + +inline bool is_valid_path(const std::string &path) { + size_t level = 0; + size_t i = 0; + + // Skip slash + while (i < path.size() && path[i] == '/') { + i++; + } + + while (i < path.size()) { + // Read component + auto beg = i; + while (i < path.size() && path[i] != '/') { + if (path[i] == '\0') { + return false; + } else if (path[i] == '\\') { + return false; + } + i++; + } + + auto len = i - beg; + assert(len > 0); + + if (!path.compare(beg, len, ".")) { + ; + } else if (!path.compare(beg, len, "..")) { + if (level == 0) { return false; } + level--; + } else { + level++; + } + + // Skip slash + while (i < path.size() && path[i] == '/') { + i++; + } + } + + return true; +} + +inline FileStat::FileStat(const std::string &path) { +#if defined(_WIN32) + auto wpath = u8string_to_wstring(path.c_str()); + ret_ = _wstat(wpath.c_str(), &st_); +#else + ret_ = stat(path.c_str(), &st_); +#endif +} +inline bool FileStat::is_file() const { + return ret_ >= 0 && S_ISREG(st_.st_mode); +} +inline bool FileStat::is_dir() const { + return ret_ >= 0 && S_ISDIR(st_.st_mode); +} + +inline std::string encode_path(const std::string &s) { + std::string result; + result.reserve(s.size()); + + for (size_t i = 0; s[i]; i++) { + switch (s[i]) { + case ' ': result += "%20"; break; + case '+': result += "%2B"; break; + case '\r': result += "%0D"; break; + case '\n': result += "%0A"; break; + case '\'': result += "%27"; break; + case ',': result += "%2C"; break; + // case ':': result += "%3A"; break; // ok? probably... + case ';': result += "%3B"; break; + default: + auto c = static_cast(s[i]); + if (c >= 0x80) { + result += '%'; + char hex[4]; + auto len = snprintf(hex, sizeof(hex) - 1, "%02X", c); + assert(len == 2); + result.append(hex, static_cast(len)); + } else { + result += s[i]; + } + break; + } + } + + return result; +} + +inline std::string file_extension(const std::string &path) { + std::smatch m; + thread_local auto re = std::regex("\\.([a-zA-Z0-9]+)$"); + if (std::regex_search(path, m, re)) { return m[1].str(); } + return std::string(); +} + +inline bool is_space_or_tab(char c) { return c == ' ' || c == '\t'; } + +inline std::pair trim(const char *b, const char *e, size_t left, + size_t right) { + while (b + left < e && is_space_or_tab(b[left])) { + left++; + } + while (right > 0 && is_space_or_tab(b[right - 1])) { + right--; + } + return std::make_pair(left, right); +} + +inline std::string trim_copy(const std::string &s) { + auto r = trim(s.data(), s.data() + s.size(), 0, s.size()); + return s.substr(r.first, r.second - r.first); +} + +inline std::string trim_double_quotes_copy(const std::string &s) { + if (s.length() >= 2 && s.front() == '"' && s.back() == '"') { + return s.substr(1, s.size() - 2); + } + return s; +} + +inline void +divide(const char *data, std::size_t size, char d, + std::function + fn) { + const auto it = std::find(data, data + size, d); + const auto found = static_cast(it != data + size); + const auto lhs_data = data; + const auto lhs_size = static_cast(it - data); + const auto rhs_data = it + found; + const auto rhs_size = size - lhs_size - found; + + fn(lhs_data, lhs_size, rhs_data, rhs_size); +} + +inline void +divide(const std::string &str, char d, + std::function + fn) { + divide(str.data(), str.size(), d, std::move(fn)); +} + +inline void split(const char *b, const char *e, char d, + std::function fn) { + return split(b, e, d, (std::numeric_limits::max)(), std::move(fn)); +} + +inline void split(const char *b, const char *e, char d, size_t m, + std::function fn) { + size_t i = 0; + size_t beg = 0; + size_t count = 1; + + while (e ? (b + i < e) : (b[i] != '\0')) { + if (b[i] == d && count < m) { + auto r = trim(b, e, beg, i); + if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } + beg = i + 1; + count++; + } + i++; + } + + if (i) { + auto r = trim(b, e, beg, i); + if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } + } +} + +inline stream_line_reader::stream_line_reader(Stream &strm, char *fixed_buffer, + size_t fixed_buffer_size) + : strm_(strm), fixed_buffer_(fixed_buffer), + fixed_buffer_size_(fixed_buffer_size) {} + +inline const char *stream_line_reader::ptr() const { + if (growable_buffer_.empty()) { + return fixed_buffer_; + } else { + return growable_buffer_.data(); + } +} + +inline size_t stream_line_reader::size() const { + if (growable_buffer_.empty()) { + return fixed_buffer_used_size_; + } else { + return growable_buffer_.size(); + } +} + +inline bool stream_line_reader::end_with_crlf() const { + auto end = ptr() + size(); + return size() >= 2 && end[-2] == '\r' && end[-1] == '\n'; +} + +inline bool stream_line_reader::getline() { + fixed_buffer_used_size_ = 0; + growable_buffer_.clear(); + +#ifndef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR + char prev_byte = 0; +#endif + + for (size_t i = 0;; i++) { + if (size() >= CPPHTTPLIB_MAX_LINE_LENGTH) { + // Treat exceptionally long lines as an error to + // prevent infinite loops/memory exhaustion + return false; + } + char byte; + auto n = strm_.read(&byte, 1); + + if (n < 0) { + return false; + } else if (n == 0) { + if (i == 0) { + return false; + } else { + break; + } + } + + append(byte); + +#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR + if (byte == '\n') { break; } +#else + if (prev_byte == '\r' && byte == '\n') { break; } + prev_byte = byte; +#endif + } + + return true; +} + +inline void stream_line_reader::append(char c) { + if (fixed_buffer_used_size_ < fixed_buffer_size_ - 1) { + fixed_buffer_[fixed_buffer_used_size_++] = c; + fixed_buffer_[fixed_buffer_used_size_] = '\0'; + } else { + if (growable_buffer_.empty()) { + assert(fixed_buffer_[fixed_buffer_used_size_] == '\0'); + growable_buffer_.assign(fixed_buffer_, fixed_buffer_used_size_); + } + growable_buffer_ += c; + } +} + +inline mmap::mmap(const char *path) { open(path); } + +inline mmap::~mmap() { close(); } + +inline bool mmap::open(const char *path) { + close(); + +#if defined(_WIN32) + auto wpath = u8string_to_wstring(path); + if (wpath.empty()) { return false; } + + hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, + OPEN_EXISTING, NULL); + + if (hFile_ == INVALID_HANDLE_VALUE) { return false; } + + LARGE_INTEGER size{}; + if (!::GetFileSizeEx(hFile_, &size)) { return false; } + // If the following line doesn't compile due to QuadPart, update Windows SDK. + // See: + // https://github.com/yhirose/cpp-httplib/issues/1903#issuecomment-2316520721 + if (static_cast(size.QuadPart) > + (std::numeric_limits::max)()) { + // `size_t` might be 32-bits, on 32-bits Windows. + return false; + } + size_ = static_cast(size.QuadPart); + + hMapping_ = + ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL); + + // Special treatment for an empty file... + if (hMapping_ == NULL && size_ == 0) { + close(); + is_open_empty_file = true; + return true; + } + + if (hMapping_ == NULL) { + close(); + return false; + } + + addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0); + + if (addr_ == nullptr) { + close(); + return false; + } +#else + fd_ = ::open(path, O_RDONLY); + if (fd_ == -1) { return false; } + + struct stat sb; + if (fstat(fd_, &sb) == -1) { + close(); + return false; + } + size_ = static_cast(sb.st_size); + + addr_ = ::mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0); + + // Special treatment for an empty file... + if (addr_ == MAP_FAILED && size_ == 0) { + close(); + is_open_empty_file = true; + return false; + } +#endif + + return true; +} + +inline bool mmap::is_open() const { + return is_open_empty_file ? true : addr_ != nullptr; +} + +inline size_t mmap::size() const { return size_; } + +inline const char *mmap::data() const { + return is_open_empty_file ? "" : static_cast(addr_); +} + +inline void mmap::close() { +#if defined(_WIN32) + if (addr_) { + ::UnmapViewOfFile(addr_); + addr_ = nullptr; + } + + if (hMapping_) { + ::CloseHandle(hMapping_); + hMapping_ = NULL; + } + + if (hFile_ != INVALID_HANDLE_VALUE) { + ::CloseHandle(hFile_); + hFile_ = INVALID_HANDLE_VALUE; + } + + is_open_empty_file = false; +#else + if (addr_ != nullptr) { + munmap(addr_, size_); + addr_ = nullptr; + } + + if (fd_ != -1) { + ::close(fd_); + fd_ = -1; + } +#endif + size_ = 0; +} +inline int close_socket(socket_t sock) { +#ifdef _WIN32 + return closesocket(sock); +#else + return close(sock); +#endif +} + +template inline ssize_t handle_EINTR(T fn) { + ssize_t res = 0; + while (true) { + res = fn(); + if (res < 0 && errno == EINTR) { + std::this_thread::sleep_for(std::chrono::microseconds{1}); + continue; + } + break; + } + return res; +} + +inline ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags) { + return handle_EINTR([&]() { + return recv(sock, +#ifdef _WIN32 + static_cast(ptr), static_cast(size), +#else + ptr, size, +#endif + flags); + }); +} + +inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size, + int flags) { + return handle_EINTR([&]() { + return send(sock, +#ifdef _WIN32 + static_cast(ptr), static_cast(size), +#else + ptr, size, +#endif + flags); + }); +} + +inline int poll_wrapper(struct pollfd *fds, nfds_t nfds, int timeout) { +#ifdef _WIN32 + return ::WSAPoll(fds, nfds, timeout); +#else + return ::poll(fds, nfds, timeout); +#endif +} + +template +inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) { +#ifdef __APPLE__ + if (sock >= FD_SETSIZE) { return -1; } + + fd_set fds, *rfds, *wfds; + FD_ZERO(&fds); + FD_SET(sock, &fds); + rfds = (Read ? &fds : nullptr); + wfds = (Read ? nullptr : &fds); + + timeval tv; + tv.tv_sec = static_cast(sec); + tv.tv_usec = static_cast(usec); + + return handle_EINTR([&]() { + return select(static_cast(sock + 1), rfds, wfds, nullptr, &tv); + }); +#else + struct pollfd pfd; + pfd.fd = sock; + pfd.events = (Read ? POLLIN : POLLOUT); + + auto timeout = static_cast(sec * 1000 + usec / 1000); + + return handle_EINTR([&]() { return poll_wrapper(&pfd, 1, timeout); }); +#endif +} + +inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { + return select_impl(sock, sec, usec); +} + +inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) { + return select_impl(sock, sec, usec); +} + +inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, + time_t usec) { +#ifdef __APPLE__ + if (sock >= FD_SETSIZE) { return Error::Connection; } + + fd_set fdsr, fdsw; + FD_ZERO(&fdsr); + FD_ZERO(&fdsw); + FD_SET(sock, &fdsr); + FD_SET(sock, &fdsw); + + timeval tv; + tv.tv_sec = static_cast(sec); + tv.tv_usec = static_cast(usec); + + auto ret = handle_EINTR([&]() { + return select(static_cast(sock + 1), &fdsr, &fdsw, nullptr, &tv); + }); + + if (ret == 0) { return Error::ConnectionTimeout; } + + if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { + auto error = 0; + socklen_t len = sizeof(error); + auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, + reinterpret_cast(&error), &len); + auto successful = res >= 0 && !error; + return successful ? Error::Success : Error::Connection; + } + + return Error::Connection; +#else + struct pollfd pfd_read; + pfd_read.fd = sock; + pfd_read.events = POLLIN | POLLOUT; + + auto timeout = static_cast(sec * 1000 + usec / 1000); + + auto poll_res = + handle_EINTR([&]() { return poll_wrapper(&pfd_read, 1, timeout); }); + + if (poll_res == 0) { return Error::ConnectionTimeout; } + + if (poll_res > 0 && pfd_read.revents & (POLLIN | POLLOUT)) { + auto error = 0; + socklen_t len = sizeof(error); + auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, + reinterpret_cast(&error), &len); + auto successful = res >= 0 && !error; + return successful ? Error::Success : Error::Connection; + } + + return Error::Connection; +#endif +} + +inline bool is_socket_alive(socket_t sock) { + const auto val = detail::select_read(sock, 0, 0); + if (val == 0) { + return true; + } else if (val < 0 && errno == EBADF) { + return false; + } + char buf[1]; + return detail::read_socket(sock, &buf[0], sizeof(buf), MSG_PEEK) > 0; +} + +class SocketStream final : public Stream { +public: + SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t max_timeout_msec = 0, + std::chrono::time_point start_time = + (std::chrono::steady_clock::time_point::min)()); + ~SocketStream() override; + + bool is_readable() const override; + bool wait_readable() const override; + bool wait_writable() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; + void get_local_ip_and_port(std::string &ip, int &port) const override; + socket_t socket() const override; + time_t duration() const override; + +private: + socket_t sock_; + time_t read_timeout_sec_; + time_t read_timeout_usec_; + time_t write_timeout_sec_; + time_t write_timeout_usec_; + time_t max_timeout_msec_; + const std::chrono::time_point start_time_; + + std::vector read_buff_; + size_t read_buff_off_ = 0; + size_t read_buff_content_size_ = 0; + + static const size_t read_buff_size_ = 1024l * 4; +}; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +class SSLSocketStream final : public Stream { +public: + SSLSocketStream( + socket_t sock, SSL *ssl, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, time_t max_timeout_msec = 0, + std::chrono::time_point start_time = + (std::chrono::steady_clock::time_point::min)()); + ~SSLSocketStream() override; + + bool is_readable() const override; + bool wait_readable() const override; + bool wait_writable() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; + void get_local_ip_and_port(std::string &ip, int &port) const override; + socket_t socket() const override; + time_t duration() const override; + +private: + socket_t sock_; + SSL *ssl_; + time_t read_timeout_sec_; + time_t read_timeout_usec_; + time_t write_timeout_sec_; + time_t write_timeout_usec_; + time_t max_timeout_msec_; + const std::chrono::time_point start_time_; +}; +#endif + +inline bool keep_alive(const std::atomic &svr_sock, socket_t sock, + time_t keep_alive_timeout_sec) { + using namespace std::chrono; + + const auto interval_usec = + CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND; + + // Avoid expensive `steady_clock::now()` call for the first time + if (select_read(sock, 0, interval_usec) > 0) { return true; } + + const auto start = steady_clock::now() - microseconds{interval_usec}; + const auto timeout = seconds{keep_alive_timeout_sec}; + + while (true) { + if (svr_sock == INVALID_SOCKET) { + break; // Server socket is closed + } + + auto val = select_read(sock, 0, interval_usec); + if (val < 0) { + break; // Ssocket error + } else if (val == 0) { + if (steady_clock::now() - start > timeout) { + break; // Timeout + } + } else { + return true; // Ready for read + } + } + + return false; +} + +template +inline bool +process_server_socket_core(const std::atomic &svr_sock, socket_t sock, + size_t keep_alive_max_count, + time_t keep_alive_timeout_sec, T callback) { + assert(keep_alive_max_count > 0); + auto ret = false; + auto count = keep_alive_max_count; + while (count > 0 && keep_alive(svr_sock, sock, keep_alive_timeout_sec)) { + auto close_connection = count == 1; + auto connection_closed = false; + ret = callback(close_connection, connection_closed); + if (!ret || connection_closed) { break; } + count--; + } + return ret; +} + +template +inline bool +process_server_socket(const std::atomic &svr_sock, socket_t sock, + size_t keep_alive_max_count, + time_t keep_alive_timeout_sec, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { + return process_server_socket_core( + svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, + [&](bool close_connection, bool &connection_closed) { + SocketStream strm(sock, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm, close_connection, connection_closed); + }); +} + +inline bool process_client_socket( + socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t max_timeout_msec, + std::chrono::time_point start_time, + std::function callback) { + SocketStream strm(sock, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec, max_timeout_msec, + start_time); + return callback(strm); +} + +inline int shutdown_socket(socket_t sock) { +#ifdef _WIN32 + return shutdown(sock, SD_BOTH); +#else + return shutdown(sock, SHUT_RDWR); +#endif +} + +inline std::string escape_abstract_namespace_unix_domain(const std::string &s) { + if (s.size() > 1 && s[0] == '\0') { + auto ret = s; + ret[0] = '@'; + return ret; + } + return s; +} + +inline std::string +unescape_abstract_namespace_unix_domain(const std::string &s) { + if (s.size() > 1 && s[0] == '@') { + auto ret = s; + ret[0] = '\0'; + return ret; + } + return s; +} + +inline int getaddrinfo_with_timeout(const char *node, const char *service, + const struct addrinfo *hints, + struct addrinfo **res, time_t timeout_sec) { +#ifdef CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO + if (timeout_sec <= 0) { + // No timeout specified, use standard getaddrinfo + return getaddrinfo(node, service, hints, res); + } + +#ifdef _WIN32 + // Windows-specific implementation using GetAddrInfoEx with overlapped I/O + OVERLAPPED overlapped = {0}; + HANDLE event = CreateEventW(nullptr, TRUE, FALSE, nullptr); + if (!event) { return EAI_FAIL; } + + overlapped.hEvent = event; + + PADDRINFOEXW result_addrinfo = nullptr; + HANDLE cancel_handle = nullptr; + + ADDRINFOEXW hints_ex = {0}; + if (hints) { + hints_ex.ai_flags = hints->ai_flags; + hints_ex.ai_family = hints->ai_family; + hints_ex.ai_socktype = hints->ai_socktype; + hints_ex.ai_protocol = hints->ai_protocol; + } + + auto wnode = u8string_to_wstring(node); + auto wservice = u8string_to_wstring(service); + + auto ret = ::GetAddrInfoExW(wnode.data(), wservice.data(), NS_DNS, nullptr, + hints ? &hints_ex : nullptr, &result_addrinfo, + nullptr, &overlapped, nullptr, &cancel_handle); + + if (ret == WSA_IO_PENDING) { + auto wait_result = + ::WaitForSingleObject(event, static_cast(timeout_sec * 1000)); + if (wait_result == WAIT_TIMEOUT) { + if (cancel_handle) { ::GetAddrInfoExCancel(&cancel_handle); } + ::CloseHandle(event); + return EAI_AGAIN; + } + + DWORD bytes_returned; + if (!::GetOverlappedResult((HANDLE)INVALID_SOCKET, &overlapped, + &bytes_returned, FALSE)) { + ::CloseHandle(event); + return ::WSAGetLastError(); + } + } + + ::CloseHandle(event); + + if (ret == NO_ERROR || ret == WSA_IO_PENDING) { + *res = reinterpret_cast(result_addrinfo); + return 0; + } + + return ret; +#elif TARGET_OS_MAC + // macOS implementation using CFHost API for asynchronous DNS resolution + CFStringRef hostname_ref = CFStringCreateWithCString( + kCFAllocatorDefault, node, kCFStringEncodingUTF8); + if (!hostname_ref) { return EAI_MEMORY; } + + CFHostRef host_ref = CFHostCreateWithName(kCFAllocatorDefault, hostname_ref); + CFRelease(hostname_ref); + if (!host_ref) { return EAI_MEMORY; } + + // Set up context for callback + struct CFHostContext { + bool completed = false; + bool success = false; + CFArrayRef addresses = nullptr; + std::mutex mutex; + std::condition_variable cv; + } context; + + CFHostClientContext client_context; + memset(&client_context, 0, sizeof(client_context)); + client_context.info = &context; + + // Set callback + auto callback = [](CFHostRef theHost, CFHostInfoType /*typeInfo*/, + const CFStreamError *error, void *info) { + auto ctx = static_cast(info); + std::lock_guard lock(ctx->mutex); + + if (error && error->error != 0) { + ctx->success = false; + } else { + Boolean hasBeenResolved; + ctx->addresses = CFHostGetAddressing(theHost, &hasBeenResolved); + if (ctx->addresses && hasBeenResolved) { + CFRetain(ctx->addresses); + ctx->success = true; + } else { + ctx->success = false; + } + } + ctx->completed = true; + ctx->cv.notify_one(); + }; + + if (!CFHostSetClient(host_ref, callback, &client_context)) { + CFRelease(host_ref); + return EAI_SYSTEM; + } + + // Schedule on run loop + CFRunLoopRef run_loop = CFRunLoopGetCurrent(); + CFHostScheduleWithRunLoop(host_ref, run_loop, kCFRunLoopDefaultMode); + + // Start resolution + CFStreamError stream_error; + if (!CFHostStartInfoResolution(host_ref, kCFHostAddresses, &stream_error)) { + CFHostUnscheduleFromRunLoop(host_ref, run_loop, kCFRunLoopDefaultMode); + CFRelease(host_ref); + return EAI_FAIL; + } + + // Wait for completion with timeout + auto timeout_time = + std::chrono::steady_clock::now() + std::chrono::seconds(timeout_sec); + bool timed_out = false; + + { + std::unique_lock lock(context.mutex); + + while (!context.completed) { + auto now = std::chrono::steady_clock::now(); + if (now >= timeout_time) { + timed_out = true; + break; + } + + // Run the runloop for a short time + lock.unlock(); + CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, true); + lock.lock(); + } + } + + // Clean up + CFHostUnscheduleFromRunLoop(host_ref, run_loop, kCFRunLoopDefaultMode); + CFHostSetClient(host_ref, nullptr, nullptr); + + if (timed_out || !context.completed) { + CFHostCancelInfoResolution(host_ref, kCFHostAddresses); + CFRelease(host_ref); + return EAI_AGAIN; + } + + if (!context.success || !context.addresses) { + CFRelease(host_ref); + return EAI_NODATA; + } + + // Convert CFArray to addrinfo + CFIndex count = CFArrayGetCount(context.addresses); + if (count == 0) { + CFRelease(context.addresses); + CFRelease(host_ref); + return EAI_NODATA; + } + + struct addrinfo *result_addrinfo = nullptr; + struct addrinfo **current = &result_addrinfo; + + for (CFIndex i = 0; i < count; i++) { + CFDataRef addr_data = + static_cast(CFArrayGetValueAtIndex(context.addresses, i)); + if (!addr_data) continue; + + const struct sockaddr *sockaddr_ptr = + reinterpret_cast(CFDataGetBytePtr(addr_data)); + socklen_t sockaddr_len = static_cast(CFDataGetLength(addr_data)); + + // Allocate addrinfo structure + *current = static_cast(malloc(sizeof(struct addrinfo))); + if (!*current) { + freeaddrinfo(result_addrinfo); + CFRelease(context.addresses); + CFRelease(host_ref); + return EAI_MEMORY; + } + + memset(*current, 0, sizeof(struct addrinfo)); + + // Set up addrinfo fields + (*current)->ai_family = sockaddr_ptr->sa_family; + (*current)->ai_socktype = hints ? hints->ai_socktype : SOCK_STREAM; + (*current)->ai_protocol = hints ? hints->ai_protocol : IPPROTO_TCP; + (*current)->ai_addrlen = sockaddr_len; + + // Copy sockaddr + (*current)->ai_addr = static_cast(malloc(sockaddr_len)); + if (!(*current)->ai_addr) { + freeaddrinfo(result_addrinfo); + CFRelease(context.addresses); + CFRelease(host_ref); + return EAI_MEMORY; + } + memcpy((*current)->ai_addr, sockaddr_ptr, sockaddr_len); + + // Set port if service is specified + if (service && strlen(service) > 0) { + int port = atoi(service); + if (port > 0) { + if (sockaddr_ptr->sa_family == AF_INET) { + reinterpret_cast((*current)->ai_addr) + ->sin_port = htons(static_cast(port)); + } else if (sockaddr_ptr->sa_family == AF_INET6) { + reinterpret_cast((*current)->ai_addr) + ->sin6_port = htons(static_cast(port)); + } + } + } + + current = &((*current)->ai_next); + } + + CFRelease(context.addresses); + CFRelease(host_ref); + + *res = result_addrinfo; + return 0; +#elif defined(_GNU_SOURCE) && defined(__GLIBC__) && \ + (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) + // Linux implementation using getaddrinfo_a for asynchronous DNS resolution + struct gaicb request; + struct gaicb *requests[1] = {&request}; + struct sigevent sevp; + struct timespec timeout; + + // Initialize the request structure + memset(&request, 0, sizeof(request)); + request.ar_name = node; + request.ar_service = service; + request.ar_request = hints; + + // Set up timeout + timeout.tv_sec = timeout_sec; + timeout.tv_nsec = 0; + + // Initialize sigevent structure (not used, but required) + memset(&sevp, 0, sizeof(sevp)); + sevp.sigev_notify = SIGEV_NONE; + + // Start asynchronous resolution + int start_result = getaddrinfo_a(GAI_NOWAIT, requests, 1, &sevp); + if (start_result != 0) { return start_result; } + + // Wait for completion with timeout + int wait_result = + gai_suspend((const struct gaicb *const *)requests, 1, &timeout); + + if (wait_result == 0 || wait_result == EAI_ALLDONE) { + // Completed successfully, get the result + int gai_result = gai_error(&request); + if (gai_result == 0) { + *res = request.ar_result; + return 0; + } else { + // Clean up on error + if (request.ar_result) { freeaddrinfo(request.ar_result); } + return gai_result; + } + } else if (wait_result == EAI_AGAIN) { + // Timeout occurred, cancel the request + gai_cancel(&request); + return EAI_AGAIN; + } else { + // Other error occurred + gai_cancel(&request); + return wait_result; + } +#else + // Fallback implementation using thread-based timeout for other Unix systems + + struct GetAddrInfoState { + std::mutex mutex; + std::condition_variable result_cv; + bool completed = false; + int result = EAI_SYSTEM; + std::string node = node; + std::string service = service; + struct addrinfo hints = hints; + struct addrinfo *info = nullptr; + }; + + // Allocate on the heap, so the resolver thread can keep using the data. + auto state = std::make_shared(); + + std::thread resolve_thread([=]() { + auto thread_result = getaddrinfo( + state->node.c_str(), state->service.c_str(), hints, &state->info); + + std::lock_guard lock(state->mutex); + state->result = thread_result; + state->completed = true; + state->result_cv.notify_one(); + }); + + // Wait for completion or timeout + std::unique_lock lock(state->mutex); + auto finished = + state->result_cv.wait_for(lock, std::chrono::seconds(timeout_sec), + [&] { return state->completed; }); + + if (finished) { + // Operation completed within timeout + resolve_thread.join(); + *res = state->info; + return state->result; + } else { + // Timeout occurred + resolve_thread.detach(); // Let the thread finish in background + return EAI_AGAIN; // Return timeout error + } +#endif +#else + (void)(timeout_sec); // Unused parameter for non-blocking getaddrinfo + return getaddrinfo(node, service, hints, res); +#endif +} + +template +socket_t create_socket(const std::string &host, const std::string &ip, int port, + int address_family, int socket_flags, bool tcp_nodelay, + bool ipv6_v6only, SocketOptions socket_options, + BindOrConnect bind_or_connect, time_t timeout_sec = 0) { + // Get address info + const char *node = nullptr; + struct addrinfo hints; + struct addrinfo *result; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_IP; + + if (!ip.empty()) { + node = ip.c_str(); + // Ask getaddrinfo to convert IP in c-string to address + hints.ai_family = AF_UNSPEC; + hints.ai_flags = AI_NUMERICHOST; + } else { + if (!host.empty()) { node = host.c_str(); } + hints.ai_family = address_family; + hints.ai_flags = socket_flags; + } + +#if !defined(_WIN32) || defined(CPPHTTPLIB_HAVE_AFUNIX_H) + if (hints.ai_family == AF_UNIX) { + const auto addrlen = host.length(); + if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } + +#ifdef SOCK_CLOEXEC + auto sock = socket(hints.ai_family, hints.ai_socktype | SOCK_CLOEXEC, + hints.ai_protocol); +#else + auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol); +#endif + + if (sock != INVALID_SOCKET) { + sockaddr_un addr{}; + addr.sun_family = AF_UNIX; + + auto unescaped_host = unescape_abstract_namespace_unix_domain(host); + std::copy(unescaped_host.begin(), unescaped_host.end(), addr.sun_path); + + hints.ai_addr = reinterpret_cast(&addr); + hints.ai_addrlen = static_cast( + sizeof(addr) - sizeof(addr.sun_path) + addrlen); + +#ifndef SOCK_CLOEXEC +#ifndef _WIN32 + fcntl(sock, F_SETFD, FD_CLOEXEC); +#endif +#endif + + if (socket_options) { socket_options(sock); } + +#ifdef _WIN32 + // Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so + // remove the option. + detail::set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 0); +#endif + + bool dummy; + if (!bind_or_connect(sock, hints, dummy)) { + close_socket(sock); + sock = INVALID_SOCKET; + } + } + return sock; + } +#endif + + auto service = std::to_string(port); + + if (getaddrinfo_with_timeout(node, service.c_str(), &hints, &result, + timeout_sec)) { +#if defined __linux__ && !defined __ANDROID__ + res_init(); +#endif + return INVALID_SOCKET; + } + auto se = detail::scope_exit([&] { freeaddrinfo(result); }); + + for (auto rp = result; rp; rp = rp->ai_next) { + // Create a socket +#ifdef _WIN32 + auto sock = + WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0, + WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED); + /** + * Since the WSA_FLAG_NO_HANDLE_INHERIT is only supported on Windows 7 SP1 + * and above the socket creation fails on older Windows Systems. + * + * Let's try to create a socket the old way in this case. + * + * Reference: + * https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa + * + * WSA_FLAG_NO_HANDLE_INHERIT: + * This flag is supported on Windows 7 with SP1, Windows Server 2008 R2 with + * SP1, and later + * + */ + if (sock == INVALID_SOCKET) { + sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + } +#else + +#ifdef SOCK_CLOEXEC + auto sock = + socket(rp->ai_family, rp->ai_socktype | SOCK_CLOEXEC, rp->ai_protocol); +#else + auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); +#endif + +#endif + if (sock == INVALID_SOCKET) { continue; } + +#if !defined _WIN32 && !defined SOCK_CLOEXEC + if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { + close_socket(sock); + continue; + } +#endif + + if (tcp_nodelay) { set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1); } + + if (rp->ai_family == AF_INET6) { + set_socket_opt(sock, IPPROTO_IPV6, IPV6_V6ONLY, ipv6_v6only ? 1 : 0); + } + + if (socket_options) { socket_options(sock); } + + // bind or connect + auto quit = false; + if (bind_or_connect(sock, *rp, quit)) { return sock; } + + close_socket(sock); + + if (quit) { break; } + } + + return INVALID_SOCKET; +} + +inline void set_nonblocking(socket_t sock, bool nonblocking) { +#ifdef _WIN32 + auto flags = nonblocking ? 1UL : 0UL; + ioctlsocket(sock, FIONBIO, &flags); +#else + auto flags = fcntl(sock, F_GETFL, 0); + fcntl(sock, F_SETFL, + nonblocking ? (flags | O_NONBLOCK) : (flags & (~O_NONBLOCK))); +#endif +} + +inline bool is_connection_error() { +#ifdef _WIN32 + return WSAGetLastError() != WSAEWOULDBLOCK; +#else + return errno != EINPROGRESS; +#endif +} + +inline bool bind_ip_address(socket_t sock, const std::string &host) { + struct addrinfo hints; + struct addrinfo *result; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = 0; + + if (getaddrinfo_with_timeout(host.c_str(), "0", &hints, &result, 0)) { + return false; + } + + auto se = detail::scope_exit([&] { freeaddrinfo(result); }); + + auto ret = false; + for (auto rp = result; rp; rp = rp->ai_next) { + const auto &ai = *rp; + if (!::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { + ret = true; + break; + } + } + + return ret; +} + +#if !defined _WIN32 && !defined ANDROID && !defined _AIX && !defined __MVS__ +#define USE_IF2IP +#endif + +#ifdef USE_IF2IP +inline std::string if2ip(int address_family, const std::string &ifn) { + struct ifaddrs *ifap; + getifaddrs(&ifap); + auto se = detail::scope_exit([&] { freeifaddrs(ifap); }); + + std::string addr_candidate; + for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) { + if (ifa->ifa_addr && ifn == ifa->ifa_name && + (AF_UNSPEC == address_family || + ifa->ifa_addr->sa_family == address_family)) { + if (ifa->ifa_addr->sa_family == AF_INET) { + auto sa = reinterpret_cast(ifa->ifa_addr); + char buf[INET_ADDRSTRLEN]; + if (inet_ntop(AF_INET, &sa->sin_addr, buf, INET_ADDRSTRLEN)) { + return std::string(buf, INET_ADDRSTRLEN); + } + } else if (ifa->ifa_addr->sa_family == AF_INET6) { + auto sa = reinterpret_cast(ifa->ifa_addr); + if (!IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr)) { + char buf[INET6_ADDRSTRLEN] = {}; + if (inet_ntop(AF_INET6, &sa->sin6_addr, buf, INET6_ADDRSTRLEN)) { + // equivalent to mac's IN6_IS_ADDR_UNIQUE_LOCAL + auto s6_addr_head = sa->sin6_addr.s6_addr[0]; + if (s6_addr_head == 0xfc || s6_addr_head == 0xfd) { + addr_candidate = std::string(buf, INET6_ADDRSTRLEN); + } else { + return std::string(buf, INET6_ADDRSTRLEN); + } + } + } + } + } + } + return addr_candidate; +} +#endif + +inline socket_t create_client_socket( + const std::string &host, const std::string &ip, int port, + int address_family, bool tcp_nodelay, bool ipv6_v6only, + SocketOptions socket_options, time_t connection_timeout_sec, + time_t connection_timeout_usec, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, const std::string &intf, Error &error) { + auto sock = create_socket( + host, ip, port, address_family, 0, tcp_nodelay, ipv6_v6only, + std::move(socket_options), + [&](socket_t sock2, struct addrinfo &ai, bool &quit) -> bool { + if (!intf.empty()) { +#ifdef USE_IF2IP + auto ip_from_if = if2ip(address_family, intf); + if (ip_from_if.empty()) { ip_from_if = intf; } + if (!bind_ip_address(sock2, ip_from_if)) { + error = Error::BindIPAddress; + return false; + } +#endif + } + + set_nonblocking(sock2, true); + + auto ret = + ::connect(sock2, ai.ai_addr, static_cast(ai.ai_addrlen)); + + if (ret < 0) { + if (is_connection_error()) { + error = Error::Connection; + return false; + } + error = wait_until_socket_is_ready(sock2, connection_timeout_sec, + connection_timeout_usec); + if (error != Error::Success) { + if (error == Error::ConnectionTimeout) { quit = true; } + return false; + } + } + + set_nonblocking(sock2, false); + set_socket_opt_time(sock2, SOL_SOCKET, SO_RCVTIMEO, read_timeout_sec, + read_timeout_usec); + set_socket_opt_time(sock2, SOL_SOCKET, SO_SNDTIMEO, write_timeout_sec, + write_timeout_usec); + + error = Error::Success; + return true; + }, + connection_timeout_sec); // Pass DNS timeout + + if (sock != INVALID_SOCKET) { + error = Error::Success; + } else { + if (error == Error::Success) { error = Error::Connection; } + } + + return sock; +} + +inline bool get_ip_and_port(const struct sockaddr_storage &addr, + socklen_t addr_len, std::string &ip, int &port) { + if (addr.ss_family == AF_INET) { + port = ntohs(reinterpret_cast(&addr)->sin_port); + } else if (addr.ss_family == AF_INET6) { + port = + ntohs(reinterpret_cast(&addr)->sin6_port); + } else { + return false; + } + + std::array ipstr{}; + if (getnameinfo(reinterpret_cast(&addr), addr_len, + ipstr.data(), static_cast(ipstr.size()), nullptr, + 0, NI_NUMERICHOST)) { + return false; + } + + ip = ipstr.data(); + return true; +} + +inline void get_local_ip_and_port(socket_t sock, std::string &ip, int &port) { + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); + if (!getsockname(sock, reinterpret_cast(&addr), + &addr_len)) { + get_ip_and_port(addr, addr_len, ip, port); + } +} + +inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) { + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); + + if (!getpeername(sock, reinterpret_cast(&addr), + &addr_len)) { +#ifndef _WIN32 + if (addr.ss_family == AF_UNIX) { +#if defined(__linux__) + struct ucred ucred; + socklen_t len = sizeof(ucred); + if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == 0) { + port = ucred.pid; + } +#elif defined(SOL_LOCAL) && defined(SO_PEERPID) + pid_t pid; + socklen_t len = sizeof(pid); + if (getsockopt(sock, SOL_LOCAL, SO_PEERPID, &pid, &len) == 0) { + port = pid; + } +#endif + return; + } +#endif + get_ip_and_port(addr, addr_len, ip, port); + } +} + +inline constexpr unsigned int str2tag_core(const char *s, size_t l, + unsigned int h) { + return (l == 0) + ? h + : str2tag_core( + s + 1, l - 1, + // Unsets the 6 high bits of h, therefore no overflow happens + (((std::numeric_limits::max)() >> 6) & + h * 33) ^ + static_cast(*s)); +} + +inline unsigned int str2tag(const std::string &s) { + return str2tag_core(s.data(), s.size(), 0); +} + +namespace udl { + +inline constexpr unsigned int operator""_t(const char *s, size_t l) { + return str2tag_core(s, l, 0); +} + +} // namespace udl + +inline std::string +find_content_type(const std::string &path, + const std::map &user_data, + const std::string &default_content_type) { + auto ext = file_extension(path); + + auto it = user_data.find(ext); + if (it != user_data.end()) { return it->second; } + + using udl::operator""_t; + + switch (str2tag(ext)) { + default: return default_content_type; + + case "css"_t: return "text/css"; + case "csv"_t: return "text/csv"; + case "htm"_t: + case "html"_t: return "text/html"; + case "js"_t: + case "mjs"_t: return "text/javascript"; + case "txt"_t: return "text/plain"; + case "vtt"_t: return "text/vtt"; + + case "apng"_t: return "image/apng"; + case "avif"_t: return "image/avif"; + case "bmp"_t: return "image/bmp"; + case "gif"_t: return "image/gif"; + case "png"_t: return "image/png"; + case "svg"_t: return "image/svg+xml"; + case "webp"_t: return "image/webp"; + case "ico"_t: return "image/x-icon"; + case "tif"_t: return "image/tiff"; + case "tiff"_t: return "image/tiff"; + case "jpg"_t: + case "jpeg"_t: return "image/jpeg"; + + case "mp4"_t: return "video/mp4"; + case "mpeg"_t: return "video/mpeg"; + case "webm"_t: return "video/webm"; + + case "mp3"_t: return "audio/mp3"; + case "mpga"_t: return "audio/mpeg"; + case "weba"_t: return "audio/webm"; + case "wav"_t: return "audio/wave"; + + case "otf"_t: return "font/otf"; + case "ttf"_t: return "font/ttf"; + case "woff"_t: return "font/woff"; + case "woff2"_t: return "font/woff2"; + + case "7z"_t: return "application/x-7z-compressed"; + case "atom"_t: return "application/atom+xml"; + case "pdf"_t: return "application/pdf"; + case "json"_t: return "application/json"; + case "rss"_t: return "application/rss+xml"; + case "tar"_t: return "application/x-tar"; + case "xht"_t: + case "xhtml"_t: return "application/xhtml+xml"; + case "xslt"_t: return "application/xslt+xml"; + case "xml"_t: return "application/xml"; + case "gz"_t: return "application/gzip"; + case "zip"_t: return "application/zip"; + case "wasm"_t: return "application/wasm"; + } +} + +inline bool can_compress_content_type(const std::string &content_type) { + using udl::operator""_t; + + auto tag = str2tag(content_type); + + switch (tag) { + case "image/svg+xml"_t: + case "application/javascript"_t: + case "application/json"_t: + case "application/xml"_t: + case "application/protobuf"_t: + case "application/xhtml+xml"_t: return true; + + case "text/event-stream"_t: return false; + + default: return !content_type.rfind("text/", 0); + } +} + +inline EncodingType encoding_type(const Request &req, const Response &res) { + auto ret = + detail::can_compress_content_type(res.get_header_value("Content-Type")); + if (!ret) { return EncodingType::None; } + + const auto &s = req.get_header_value("Accept-Encoding"); + (void)(s); + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + // TODO: 'Accept-Encoding' has br, not br;q=0 + ret = s.find("br") != std::string::npos; + if (ret) { return EncodingType::Brotli; } +#endif + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + // TODO: 'Accept-Encoding' has gzip, not gzip;q=0 + ret = s.find("gzip") != std::string::npos; + if (ret) { return EncodingType::Gzip; } +#endif + +#ifdef CPPHTTPLIB_ZSTD_SUPPORT + // TODO: 'Accept-Encoding' has zstd, not zstd;q=0 + ret = s.find("zstd") != std::string::npos; + if (ret) { return EncodingType::Zstd; } +#endif + + return EncodingType::None; +} + +inline bool nocompressor::compress(const char *data, size_t data_length, + bool /*last*/, Callback callback) { + if (!data_length) { return true; } + return callback(data, data_length); +} + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +inline gzip_compressor::gzip_compressor() { + std::memset(&strm_, 0, sizeof(strm_)); + strm_.zalloc = Z_NULL; + strm_.zfree = Z_NULL; + strm_.opaque = Z_NULL; + + is_valid_ = deflateInit2(&strm_, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8, + Z_DEFAULT_STRATEGY) == Z_OK; +} + +inline gzip_compressor::~gzip_compressor() { deflateEnd(&strm_); } + +inline bool gzip_compressor::compress(const char *data, size_t data_length, + bool last, Callback callback) { + assert(is_valid_); + + do { + constexpr size_t max_avail_in = + (std::numeric_limits::max)(); + + strm_.avail_in = static_cast( + (std::min)(data_length, max_avail_in)); + strm_.next_in = const_cast(reinterpret_cast(data)); + + data_length -= strm_.avail_in; + data += strm_.avail_in; + + auto flush = (last && data_length == 0) ? Z_FINISH : Z_NO_FLUSH; + auto ret = Z_OK; + + std::array buff{}; + do { + strm_.avail_out = static_cast(buff.size()); + strm_.next_out = reinterpret_cast(buff.data()); + + ret = deflate(&strm_, flush); + if (ret == Z_STREAM_ERROR) { return false; } + + if (!callback(buff.data(), buff.size() - strm_.avail_out)) { + return false; + } + } while (strm_.avail_out == 0); + + assert((flush == Z_FINISH && ret == Z_STREAM_END) || + (flush == Z_NO_FLUSH && ret == Z_OK)); + assert(strm_.avail_in == 0); + } while (data_length > 0); + + return true; +} + +inline gzip_decompressor::gzip_decompressor() { + std::memset(&strm_, 0, sizeof(strm_)); + strm_.zalloc = Z_NULL; + strm_.zfree = Z_NULL; + strm_.opaque = Z_NULL; + + // 15 is the value of wbits, which should be at the maximum possible value + // to ensure that any gzip stream can be decoded. The offset of 32 specifies + // that the stream type should be automatically detected either gzip or + // deflate. + is_valid_ = inflateInit2(&strm_, 32 + 15) == Z_OK; +} + +inline gzip_decompressor::~gzip_decompressor() { inflateEnd(&strm_); } + +inline bool gzip_decompressor::is_valid() const { return is_valid_; } + +inline bool gzip_decompressor::decompress(const char *data, size_t data_length, + Callback callback) { + assert(is_valid_); + + auto ret = Z_OK; + + do { + constexpr size_t max_avail_in = + (std::numeric_limits::max)(); + + strm_.avail_in = static_cast( + (std::min)(data_length, max_avail_in)); + strm_.next_in = const_cast(reinterpret_cast(data)); + + data_length -= strm_.avail_in; + data += strm_.avail_in; + + std::array buff{}; + while (strm_.avail_in > 0 && ret == Z_OK) { + strm_.avail_out = static_cast(buff.size()); + strm_.next_out = reinterpret_cast(buff.data()); + + ret = inflate(&strm_, Z_NO_FLUSH); + + assert(ret != Z_STREAM_ERROR); + switch (ret) { + case Z_NEED_DICT: + case Z_DATA_ERROR: + case Z_MEM_ERROR: inflateEnd(&strm_); return false; + } + + if (!callback(buff.data(), buff.size() - strm_.avail_out)) { + return false; + } + } + + if (ret != Z_OK && ret != Z_STREAM_END) { return false; } + + } while (data_length > 0); + + return true; +} +#endif + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT +inline brotli_compressor::brotli_compressor() { + state_ = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr); +} + +inline brotli_compressor::~brotli_compressor() { + BrotliEncoderDestroyInstance(state_); +} + +inline bool brotli_compressor::compress(const char *data, size_t data_length, + bool last, Callback callback) { + std::array buff{}; + + auto operation = last ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS; + auto available_in = data_length; + auto next_in = reinterpret_cast(data); + + for (;;) { + if (last) { + if (BrotliEncoderIsFinished(state_)) { break; } + } else { + if (!available_in) { break; } + } + + auto available_out = buff.size(); + auto next_out = buff.data(); + + if (!BrotliEncoderCompressStream(state_, operation, &available_in, &next_in, + &available_out, &next_out, nullptr)) { + return false; + } + + auto output_bytes = buff.size() - available_out; + if (output_bytes) { + callback(reinterpret_cast(buff.data()), output_bytes); + } + } + + return true; +} + +inline brotli_decompressor::brotli_decompressor() { + decoder_s = BrotliDecoderCreateInstance(0, 0, 0); + decoder_r = decoder_s ? BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT + : BROTLI_DECODER_RESULT_ERROR; +} + +inline brotli_decompressor::~brotli_decompressor() { + if (decoder_s) { BrotliDecoderDestroyInstance(decoder_s); } +} + +inline bool brotli_decompressor::is_valid() const { return decoder_s; } + +inline bool brotli_decompressor::decompress(const char *data, + size_t data_length, + Callback callback) { + if (decoder_r == BROTLI_DECODER_RESULT_SUCCESS || + decoder_r == BROTLI_DECODER_RESULT_ERROR) { + return 0; + } + + auto next_in = reinterpret_cast(data); + size_t avail_in = data_length; + size_t total_out; + + decoder_r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; + + std::array buff{}; + while (decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) { + char *next_out = buff.data(); + size_t avail_out = buff.size(); + + decoder_r = BrotliDecoderDecompressStream( + decoder_s, &avail_in, &next_in, &avail_out, + reinterpret_cast(&next_out), &total_out); + + if (decoder_r == BROTLI_DECODER_RESULT_ERROR) { return false; } + + if (!callback(buff.data(), buff.size() - avail_out)) { return false; } + } + + return decoder_r == BROTLI_DECODER_RESULT_SUCCESS || + decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT; +} +#endif + +#ifdef CPPHTTPLIB_ZSTD_SUPPORT +inline zstd_compressor::zstd_compressor() { + ctx_ = ZSTD_createCCtx(); + ZSTD_CCtx_setParameter(ctx_, ZSTD_c_compressionLevel, ZSTD_fast); +} + +inline zstd_compressor::~zstd_compressor() { ZSTD_freeCCtx(ctx_); } + +inline bool zstd_compressor::compress(const char *data, size_t data_length, + bool last, Callback callback) { + std::array buff{}; + + ZSTD_EndDirective mode = last ? ZSTD_e_end : ZSTD_e_continue; + ZSTD_inBuffer input = {data, data_length, 0}; + + bool finished; + do { + ZSTD_outBuffer output = {buff.data(), CPPHTTPLIB_COMPRESSION_BUFSIZ, 0}; + size_t const remaining = ZSTD_compressStream2(ctx_, &output, &input, mode); + + if (ZSTD_isError(remaining)) { return false; } + + if (!callback(buff.data(), output.pos)) { return false; } + + finished = last ? (remaining == 0) : (input.pos == input.size); + + } while (!finished); + + return true; +} + +inline zstd_decompressor::zstd_decompressor() { ctx_ = ZSTD_createDCtx(); } + +inline zstd_decompressor::~zstd_decompressor() { ZSTD_freeDCtx(ctx_); } + +inline bool zstd_decompressor::is_valid() const { return ctx_ != nullptr; } + +inline bool zstd_decompressor::decompress(const char *data, size_t data_length, + Callback callback) { + std::array buff{}; + ZSTD_inBuffer input = {data, data_length, 0}; + + while (input.pos < input.size) { + ZSTD_outBuffer output = {buff.data(), CPPHTTPLIB_COMPRESSION_BUFSIZ, 0}; + size_t const remaining = ZSTD_decompressStream(ctx_, &output, &input); + + if (ZSTD_isError(remaining)) { return false; } + + if (!callback(buff.data(), output.pos)) { return false; } + } + + return true; +} +#endif + +inline bool is_prohibited_header_name(const std::string &name) { + using udl::operator""_t; + + switch (str2tag(name)) { + case "REMOTE_ADDR"_t: + case "REMOTE_PORT"_t: + case "LOCAL_ADDR"_t: + case "LOCAL_PORT"_t: return true; + default: return false; + } +} + +inline bool has_header(const Headers &headers, const std::string &key) { + if (is_prohibited_header_name(key)) { return false; } + return headers.find(key) != headers.end(); +} + +inline const char *get_header_value(const Headers &headers, + const std::string &key, const char *def, + size_t id) { + if (is_prohibited_header_name(key)) { +#ifndef CPPHTTPLIB_NO_EXCEPTIONS + std::string msg = "Prohibited header name '" + key + "' is specified."; + throw std::invalid_argument(msg); +#else + return ""; +#endif + } + + auto rng = headers.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second.c_str(); } + return def; +} + +template +inline bool parse_header(const char *beg, const char *end, T fn) { + // Skip trailing spaces and tabs. + while (beg < end && is_space_or_tab(end[-1])) { + end--; + } + + auto p = beg; + while (p < end && *p != ':') { + p++; + } + + auto name = std::string(beg, p); + if (!detail::fields::is_field_name(name)) { return false; } + + if (p == end) { return false; } + + auto key_end = p; + + if (*p++ != ':') { return false; } + + while (p < end && is_space_or_tab(*p)) { + p++; + } + + if (p <= end) { + auto key_len = key_end - beg; + if (!key_len) { return false; } + + auto key = std::string(beg, key_end); + auto val = std::string(p, end); + + if (!detail::fields::is_field_value(val)) { return false; } + + if (case_ignore::equal(key, "Location") || + case_ignore::equal(key, "Referer")) { + fn(key, val); + } else { + fn(key, decode_path_component(val)); + } + + return true; + } + + return false; +} + +inline bool read_headers(Stream &strm, Headers &headers) { + const auto bufsiz = 2048; + char buf[bufsiz]; + stream_line_reader line_reader(strm, buf, bufsiz); + + size_t header_count = 0; + + for (;;) { + if (!line_reader.getline()) { return false; } + + // Check if the line ends with CRLF. + auto line_terminator_len = 2; + if (line_reader.end_with_crlf()) { + // Blank line indicates end of headers. + if (line_reader.size() == 2) { break; } + } else { +#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR + // Blank line indicates end of headers. + if (line_reader.size() == 1) { break; } + line_terminator_len = 1; +#else + continue; // Skip invalid line. +#endif + } + + if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + + // Check header count limit + if (header_count >= CPPHTTPLIB_HEADER_MAX_COUNT) { return false; } + + // Exclude line terminator + auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; + + if (!parse_header(line_reader.ptr(), end, + [&](const std::string &key, const std::string &val) { + headers.emplace(key, val); + })) { + return false; + } + + header_count++; + } + + return true; +} + +inline bool read_content_with_length(Stream &strm, size_t len, + DownloadProgress progress, + ContentReceiverWithProgress out) { + char buf[CPPHTTPLIB_RECV_BUFSIZ]; + + size_t r = 0; + while (r < len) { + auto read_len = static_cast(len - r); + auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); + if (n <= 0) { return false; } + + if (!out(buf, static_cast(n), r, len)) { return false; } + r += static_cast(n); + + if (progress) { + if (!progress(r, len)) { return false; } + } + } + + return true; +} + +inline void skip_content_with_length(Stream &strm, size_t len) { + char buf[CPPHTTPLIB_RECV_BUFSIZ]; + size_t r = 0; + while (r < len) { + auto read_len = static_cast(len - r); + auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); + if (n <= 0) { return; } + r += static_cast(n); + } +} + +enum class ReadContentResult { + Success, // Successfully read the content + PayloadTooLarge, // The content exceeds the specified payload limit + Error // An error occurred while reading the content +}; + +inline ReadContentResult +read_content_without_length(Stream &strm, size_t payload_max_length, + ContentReceiverWithProgress out) { + char buf[CPPHTTPLIB_RECV_BUFSIZ]; + size_t r = 0; + for (;;) { + auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ); + if (n == 0) { return ReadContentResult::Success; } + if (n < 0) { return ReadContentResult::Error; } + + // Check if adding this data would exceed the payload limit + if (r > payload_max_length || + payload_max_length - r < static_cast(n)) { + return ReadContentResult::PayloadTooLarge; + } + + if (!out(buf, static_cast(n), r, 0)) { + return ReadContentResult::Error; + } + r += static_cast(n); + } + + return ReadContentResult::Success; +} + +template +inline ReadContentResult read_content_chunked(Stream &strm, T &x, + size_t payload_max_length, + ContentReceiverWithProgress out) { + const auto bufsiz = 16; + char buf[bufsiz]; + + stream_line_reader line_reader(strm, buf, bufsiz); + + if (!line_reader.getline()) { return ReadContentResult::Error; } + + unsigned long chunk_len; + size_t total_len = 0; + while (true) { + char *end_ptr; + + chunk_len = std::strtoul(line_reader.ptr(), &end_ptr, 16); + + if (end_ptr == line_reader.ptr()) { return ReadContentResult::Error; } + if (chunk_len == ULONG_MAX) { return ReadContentResult::Error; } + + if (chunk_len == 0) { break; } + + // Check if adding this chunk would exceed the payload limit + if (total_len > payload_max_length || + payload_max_length - total_len < chunk_len) { + return ReadContentResult::PayloadTooLarge; + } + + total_len += chunk_len; + + if (!read_content_with_length(strm, chunk_len, nullptr, out)) { + return ReadContentResult::Error; + } + + if (!line_reader.getline()) { return ReadContentResult::Error; } + + if (strcmp(line_reader.ptr(), "\r\n") != 0) { + return ReadContentResult::Error; + } + + if (!line_reader.getline()) { return ReadContentResult::Error; } + } + + assert(chunk_len == 0); + + // NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentions "The chunked + // transfer coding is complete when a chunk with a chunk-size of zero is + // received, possibly followed by a trailer section, and finally terminated by + // an empty line". https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1 + // + // In '7.1.3. Decoding Chunked', however, the pseudo-code in the section + // does't care for the existence of the final CRLF. In other words, it seems + // to be ok whether the final CRLF exists or not in the chunked data. + // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1.3 + // + // According to the reference code in RFC 9112, cpp-httplib now allows + // chunked transfer coding data without the final CRLF. + if (!line_reader.getline()) { return ReadContentResult::Success; } + + // RFC 7230 Section 4.1.2 - Headers prohibited in trailers + thread_local case_ignore::unordered_set prohibited_trailers = { + // Message framing + "transfer-encoding", "content-length", + + // Routing + "host", + + // Authentication + "authorization", "www-authenticate", "proxy-authenticate", + "proxy-authorization", "cookie", "set-cookie", + + // Request modifiers + "cache-control", "expect", "max-forwards", "pragma", "range", "te", + + // Response control + "age", "expires", "date", "location", "retry-after", "vary", "warning", + + // Payload processing + "content-encoding", "content-type", "content-range", "trailer"}; + + // Parse declared trailer headers once for performance + case_ignore::unordered_set declared_trailers; + if (has_header(x.headers, "Trailer")) { + auto trailer_header = get_header_value(x.headers, "Trailer", "", 0); + auto len = std::strlen(trailer_header); + + split(trailer_header, trailer_header + len, ',', + [&](const char *b, const char *e) { + std::string key(b, e); + if (prohibited_trailers.find(key) == prohibited_trailers.end()) { + declared_trailers.insert(key); + } + }); + } + + size_t trailer_header_count = 0; + while (strcmp(line_reader.ptr(), "\r\n") != 0) { + if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { + return ReadContentResult::Error; + } + + // Check trailer header count limit + if (trailer_header_count >= CPPHTTPLIB_HEADER_MAX_COUNT) { + return ReadContentResult::Error; + } + + // Exclude line terminator + constexpr auto line_terminator_len = 2; + auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; + + parse_header(line_reader.ptr(), end, + [&](const std::string &key, const std::string &val) { + if (declared_trailers.find(key) != declared_trailers.end()) { + x.trailers.emplace(key, val); + trailer_header_count++; + } + }); + + if (!line_reader.getline()) { return ReadContentResult::Error; } + } + + return ReadContentResult::Success; +} + +inline bool is_chunked_transfer_encoding(const Headers &headers) { + return case_ignore::equal( + get_header_value(headers, "Transfer-Encoding", "", 0), "chunked"); +} + +template +bool prepare_content_receiver(T &x, int &status, + ContentReceiverWithProgress receiver, + bool decompress, U callback) { + if (decompress) { + std::string encoding = x.get_header_value("Content-Encoding"); + std::unique_ptr decompressor; + + if (encoding == "gzip" || encoding == "deflate") { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + decompressor = detail::make_unique(); +#else + status = StatusCode::UnsupportedMediaType_415; + return false; +#endif + } else if (encoding.find("br") != std::string::npos) { +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + decompressor = detail::make_unique(); +#else + status = StatusCode::UnsupportedMediaType_415; + return false; +#endif + } else if (encoding == "zstd") { +#ifdef CPPHTTPLIB_ZSTD_SUPPORT + decompressor = detail::make_unique(); +#else + status = StatusCode::UnsupportedMediaType_415; + return false; +#endif + } + + if (decompressor) { + if (decompressor->is_valid()) { + ContentReceiverWithProgress out = [&](const char *buf, size_t n, + size_t off, size_t len) { + return decompressor->decompress(buf, n, + [&](const char *buf2, size_t n2) { + return receiver(buf2, n2, off, len); + }); + }; + return callback(std::move(out)); + } else { + status = StatusCode::InternalServerError_500; + return false; + } + } + } + + ContentReceiverWithProgress out = [&](const char *buf, size_t n, size_t off, + size_t len) { + return receiver(buf, n, off, len); + }; + return callback(std::move(out)); +} + +template +bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, + DownloadProgress progress, + ContentReceiverWithProgress receiver, bool decompress) { + return prepare_content_receiver( + x, status, std::move(receiver), decompress, + [&](const ContentReceiverWithProgress &out) { + auto ret = true; + auto exceed_payload_max_length = false; + + if (is_chunked_transfer_encoding(x.headers)) { + auto result = read_content_chunked(strm, x, payload_max_length, out); + if (result == ReadContentResult::Success) { + ret = true; + } else if (result == ReadContentResult::PayloadTooLarge) { + exceed_payload_max_length = true; + ret = false; + } else { + ret = false; + } + } else if (!has_header(x.headers, "Content-Length")) { + auto result = + read_content_without_length(strm, payload_max_length, out); + if (result == ReadContentResult::Success) { + ret = true; + } else if (result == ReadContentResult::PayloadTooLarge) { + exceed_payload_max_length = true; + ret = false; + } else { + ret = false; + } + } else { + auto is_invalid_value = false; + auto len = get_header_value_u64(x.headers, "Content-Length", + (std::numeric_limits::max)(), + 0, is_invalid_value); + + if (is_invalid_value) { + ret = false; + } else if (len > payload_max_length) { + exceed_payload_max_length = true; + skip_content_with_length(strm, len); + ret = false; + } else if (len > 0) { + ret = read_content_with_length(strm, len, std::move(progress), out); + } + } + + if (!ret) { + status = exceed_payload_max_length ? StatusCode::PayloadTooLarge_413 + : StatusCode::BadRequest_400; + } + return ret; + }); +} + +inline ssize_t write_request_line(Stream &strm, const std::string &method, + const std::string &path) { + std::string s = method; + s += " "; + s += path; + s += " HTTP/1.1\r\n"; + return strm.write(s.data(), s.size()); +} + +inline ssize_t write_response_line(Stream &strm, int status) { + std::string s = "HTTP/1.1 "; + s += std::to_string(status); + s += " "; + s += httplib::status_message(status); + s += "\r\n"; + return strm.write(s.data(), s.size()); +} + +inline ssize_t write_headers(Stream &strm, const Headers &headers) { + ssize_t write_len = 0; + for (const auto &x : headers) { + std::string s; + s = x.first; + s += ": "; + s += x.second; + s += "\r\n"; + + auto len = strm.write(s.data(), s.size()); + if (len < 0) { return len; } + write_len += len; + } + auto len = strm.write("\r\n"); + if (len < 0) { return len; } + write_len += len; + return write_len; +} + +inline bool write_data(Stream &strm, const char *d, size_t l) { + size_t offset = 0; + while (offset < l) { + auto length = strm.write(d + offset, l - offset); + if (length < 0) { return false; } + offset += static_cast(length); + } + return true; +} + +template +inline bool write_content_with_progress(Stream &strm, + const ContentProvider &content_provider, + size_t offset, size_t length, + T is_shutting_down, + const UploadProgress &upload_progress, + Error &error) { + size_t end_offset = offset + length; + size_t start_offset = offset; + auto ok = true; + DataSink data_sink; + + data_sink.write = [&](const char *d, size_t l) -> bool { + if (ok) { + if (write_data(strm, d, l)) { + offset += l; + + if (upload_progress && length > 0) { + size_t current_written = offset - start_offset; + if (!upload_progress(current_written, length)) { + ok = false; + return false; + } + } + } else { + ok = false; + } + } + return ok; + }; + + data_sink.is_writable = [&]() -> bool { return strm.wait_writable(); }; + + while (offset < end_offset && !is_shutting_down()) { + if (!strm.wait_writable()) { + error = Error::Write; + return false; + } else if (!content_provider(offset, end_offset - offset, data_sink)) { + error = Error::Canceled; + return false; + } else if (!ok) { + error = Error::Write; + return false; + } + } + + error = Error::Success; + return true; +} + +template +inline bool write_content(Stream &strm, const ContentProvider &content_provider, + size_t offset, size_t length, T is_shutting_down, + Error &error) { + return write_content_with_progress(strm, content_provider, offset, length, + is_shutting_down, nullptr, error); +} + +template +inline bool write_content(Stream &strm, const ContentProvider &content_provider, + size_t offset, size_t length, + const T &is_shutting_down) { + auto error = Error::Success; + return write_content(strm, content_provider, offset, length, is_shutting_down, + error); +} + +template +inline bool +write_content_without_length(Stream &strm, + const ContentProvider &content_provider, + const T &is_shutting_down) { + size_t offset = 0; + auto data_available = true; + auto ok = true; + DataSink data_sink; + + data_sink.write = [&](const char *d, size_t l) -> bool { + if (ok) { + offset += l; + if (!write_data(strm, d, l)) { ok = false; } + } + return ok; + }; + + data_sink.is_writable = [&]() -> bool { return strm.wait_writable(); }; + + data_sink.done = [&](void) { data_available = false; }; + + while (data_available && !is_shutting_down()) { + if (!strm.wait_writable()) { + return false; + } else if (!content_provider(offset, 0, data_sink)) { + return false; + } else if (!ok) { + return false; + } + } + return true; +} + +template +inline bool +write_content_chunked(Stream &strm, const ContentProvider &content_provider, + const T &is_shutting_down, U &compressor, Error &error) { + size_t offset = 0; + auto data_available = true; + auto ok = true; + DataSink data_sink; + + data_sink.write = [&](const char *d, size_t l) -> bool { + if (ok) { + data_available = l > 0; + offset += l; + + std::string payload; + if (compressor.compress(d, l, false, + [&](const char *data, size_t data_len) { + payload.append(data, data_len); + return true; + })) { + if (!payload.empty()) { + // Emit chunked response header and footer for each chunk + auto chunk = + from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; + if (!write_data(strm, chunk.data(), chunk.size())) { ok = false; } + } + } else { + ok = false; + } + } + return ok; + }; + + data_sink.is_writable = [&]() -> bool { return strm.wait_writable(); }; + + auto done_with_trailer = [&](const Headers *trailer) { + if (!ok) { return; } + + data_available = false; + + std::string payload; + if (!compressor.compress(nullptr, 0, true, + [&](const char *data, size_t data_len) { + payload.append(data, data_len); + return true; + })) { + ok = false; + return; + } + + if (!payload.empty()) { + // Emit chunked response header and footer for each chunk + auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; + if (!write_data(strm, chunk.data(), chunk.size())) { + ok = false; + return; + } + } + + constexpr const char done_marker[] = "0\r\n"; + if (!write_data(strm, done_marker, str_len(done_marker))) { ok = false; } + + // Trailer + if (trailer) { + for (const auto &kv : *trailer) { + std::string field_line = kv.first + ": " + kv.second + "\r\n"; + if (!write_data(strm, field_line.data(), field_line.size())) { + ok = false; + } + } + } + + constexpr const char crlf[] = "\r\n"; + if (!write_data(strm, crlf, str_len(crlf))) { ok = false; } + }; + + data_sink.done = [&](void) { done_with_trailer(nullptr); }; + + data_sink.done_with_trailer = [&](const Headers &trailer) { + done_with_trailer(&trailer); + }; + + while (data_available && !is_shutting_down()) { + if (!strm.wait_writable()) { + error = Error::Write; + return false; + } else if (!content_provider(offset, 0, data_sink)) { + error = Error::Canceled; + return false; + } else if (!ok) { + error = Error::Write; + return false; + } + } + + error = Error::Success; + return true; +} + +template +inline bool write_content_chunked(Stream &strm, + const ContentProvider &content_provider, + const T &is_shutting_down, U &compressor) { + auto error = Error::Success; + return write_content_chunked(strm, content_provider, is_shutting_down, + compressor, error); +} + +template +inline bool redirect(T &cli, Request &req, Response &res, + const std::string &path, const std::string &location, + Error &error) { + Request new_req = req; + new_req.path = path; + new_req.redirect_count_ -= 1; + + if (res.status == StatusCode::SeeOther_303 && + (req.method != "GET" && req.method != "HEAD")) { + new_req.method = "GET"; + new_req.body.clear(); + new_req.headers.clear(); + } + + Response new_res; + + auto ret = cli.send(new_req, new_res, error); + if (ret) { + req = new_req; + res = new_res; + + if (res.location.empty()) { res.location = location; } + } + return ret; +} + +inline std::string params_to_query_str(const Params ¶ms) { + std::string query; + + for (auto it = params.begin(); it != params.end(); ++it) { + if (it != params.begin()) { query += "&"; } + query += encode_query_component(it->first); + query += "="; + query += encode_query_component(it->second); + } + return query; +} + +inline void parse_query_text(const char *data, std::size_t size, + Params ¶ms) { + std::set cache; + split(data, data + size, '&', [&](const char *b, const char *e) { + std::string kv(b, e); + if (cache.find(kv) != cache.end()) { return; } + cache.insert(std::move(kv)); + + std::string key; + std::string val; + divide(b, static_cast(e - b), '=', + [&](const char *lhs_data, std::size_t lhs_size, const char *rhs_data, + std::size_t rhs_size) { + key.assign(lhs_data, lhs_size); + val.assign(rhs_data, rhs_size); + }); + + if (!key.empty()) { + params.emplace(decode_query_component(key), decode_query_component(val)); + } + }); +} + +inline void parse_query_text(const std::string &s, Params ¶ms) { + parse_query_text(s.data(), s.size(), params); +} + +inline bool parse_multipart_boundary(const std::string &content_type, + std::string &boundary) { + auto boundary_keyword = "boundary="; + auto pos = content_type.find(boundary_keyword); + if (pos == std::string::npos) { return false; } + auto end = content_type.find(';', pos); + auto beg = pos + strlen(boundary_keyword); + boundary = trim_double_quotes_copy(content_type.substr(beg, end - beg)); + return !boundary.empty(); +} + +inline void parse_disposition_params(const std::string &s, Params ¶ms) { + std::set cache; + split(s.data(), s.data() + s.size(), ';', [&](const char *b, const char *e) { + std::string kv(b, e); + if (cache.find(kv) != cache.end()) { return; } + cache.insert(kv); + + std::string key; + std::string val; + split(b, e, '=', [&](const char *b2, const char *e2) { + if (key.empty()) { + key.assign(b2, e2); + } else { + val.assign(b2, e2); + } + }); + + if (!key.empty()) { + params.emplace(trim_double_quotes_copy((key)), + trim_double_quotes_copy((val))); + } + }); +} + +#ifdef CPPHTTPLIB_NO_EXCEPTIONS +inline bool parse_range_header(const std::string &s, Ranges &ranges) { +#else +inline bool parse_range_header(const std::string &s, Ranges &ranges) try { +#endif + auto is_valid = [](const std::string &str) { + return std::all_of(str.cbegin(), str.cend(), + [](unsigned char c) { return std::isdigit(c); }); + }; + + if (s.size() > 7 && s.compare(0, 6, "bytes=") == 0) { + const auto pos = static_cast(6); + const auto len = static_cast(s.size() - 6); + auto all_valid_ranges = true; + split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) { + if (!all_valid_ranges) { return; } + + const auto it = std::find(b, e, '-'); + if (it == e) { + all_valid_ranges = false; + return; + } + + const auto lhs = std::string(b, it); + const auto rhs = std::string(it + 1, e); + if (!is_valid(lhs) || !is_valid(rhs)) { + all_valid_ranges = false; + return; + } + + const auto first = + static_cast(lhs.empty() ? -1 : std::stoll(lhs)); + const auto last = + static_cast(rhs.empty() ? -1 : std::stoll(rhs)); + if ((first == -1 && last == -1) || + (first != -1 && last != -1 && first > last)) { + all_valid_ranges = false; + return; + } + + ranges.emplace_back(first, last); + }); + return all_valid_ranges && !ranges.empty(); + } + return false; +#ifdef CPPHTTPLIB_NO_EXCEPTIONS +} +#else +} catch (...) { return false; } +#endif + +inline bool parse_accept_header(const std::string &s, + std::vector &content_types) { + content_types.clear(); + + // Empty string is considered valid (no preference) + if (s.empty()) { return true; } + + // Check for invalid patterns: leading/trailing commas or consecutive commas + if (s.front() == ',' || s.back() == ',' || + s.find(",,") != std::string::npos) { + return false; + } + + struct AcceptEntry { + std::string media_type; + double quality; + int order; // Original order in header + }; + + std::vector entries; + int order = 0; + bool has_invalid_entry = false; + + // Split by comma and parse each entry + split(s.data(), s.data() + s.size(), ',', [&](const char *b, const char *e) { + std::string entry(b, e); + entry = trim_copy(entry); + + if (entry.empty()) { + has_invalid_entry = true; + return; + } + + AcceptEntry accept_entry; + accept_entry.quality = 1.0; // Default quality + accept_entry.order = order++; + + // Find q= parameter + auto q_pos = entry.find(";q="); + if (q_pos == std::string::npos) { q_pos = entry.find("; q="); } + + if (q_pos != std::string::npos) { + // Extract media type (before q parameter) + accept_entry.media_type = trim_copy(entry.substr(0, q_pos)); + + // Extract quality value + auto q_start = entry.find('=', q_pos) + 1; + auto q_end = entry.find(';', q_start); + if (q_end == std::string::npos) { q_end = entry.length(); } + + std::string quality_str = + trim_copy(entry.substr(q_start, q_end - q_start)); + if (quality_str.empty()) { + has_invalid_entry = true; + return; + } + +#ifdef CPPHTTPLIB_NO_EXCEPTIONS + { + std::istringstream iss(quality_str); + iss >> accept_entry.quality; + + // Check if conversion was successful and entire string was consumed + if (iss.fail() || !iss.eof()) { + has_invalid_entry = true; + return; + } + } +#else + try { + accept_entry.quality = std::stod(quality_str); + } catch (...) { + has_invalid_entry = true; + return; + } +#endif + // Check if quality is in valid range [0.0, 1.0] + if (accept_entry.quality < 0.0 || accept_entry.quality > 1.0) { + has_invalid_entry = true; + return; + } + } else { + // No quality parameter, use entire entry as media type + accept_entry.media_type = entry; + } + + // Remove additional parameters from media type + auto param_pos = accept_entry.media_type.find(';'); + if (param_pos != std::string::npos) { + accept_entry.media_type = + trim_copy(accept_entry.media_type.substr(0, param_pos)); + } + + // Basic validation of media type format + if (accept_entry.media_type.empty()) { + has_invalid_entry = true; + return; + } + + // Check for basic media type format (should contain '/' or be '*') + if (accept_entry.media_type != "*" && + accept_entry.media_type.find('/') == std::string::npos) { + has_invalid_entry = true; + return; + } + + entries.push_back(accept_entry); + }); + + // Return false if any invalid entry was found + if (has_invalid_entry) { return false; } + + // Sort by quality (descending), then by original order (ascending) + std::sort(entries.begin(), entries.end(), + [](const AcceptEntry &a, const AcceptEntry &b) { + if (a.quality != b.quality) { + return a.quality > b.quality; // Higher quality first + } + return a.order < b.order; // Earlier order first for same quality + }); + + // Extract sorted media types + content_types.reserve(entries.size()); + for (const auto &entry : entries) { + content_types.push_back(entry.media_type); + } + + return true; +} + +class FormDataParser { +public: + FormDataParser() = default; + + void set_boundary(std::string &&boundary) { + boundary_ = boundary; + dash_boundary_crlf_ = dash_ + boundary_ + crlf_; + crlf_dash_boundary_ = crlf_ + dash_ + boundary_; + } + + bool is_valid() const { return is_valid_; } + + bool parse(const char *buf, size_t n, const FormDataHeader &header_callback, + const ContentReceiver &content_callback) { + + buf_append(buf, n); + + while (buf_size() > 0) { + switch (state_) { + case 0: { // Initial boundary + auto pos = buf_find(dash_boundary_crlf_); + if (pos == buf_size()) { return true; } + buf_erase(pos + dash_boundary_crlf_.size()); + state_ = 1; + break; + } + case 1: { // New entry + clear_file_info(); + state_ = 2; + break; + } + case 2: { // Headers + auto pos = buf_find(crlf_); + if (pos > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + while (pos < buf_size()) { + // Empty line + if (pos == 0) { + if (!header_callback(file_)) { + is_valid_ = false; + return false; + } + buf_erase(crlf_.size()); + state_ = 3; + break; + } + + const auto header = buf_head(pos); + + if (!parse_header(header.data(), header.data() + header.size(), + [&](const std::string &, const std::string &) {})) { + is_valid_ = false; + return false; + } + + // Parse and emplace space trimmed headers into a map + if (!parse_header( + header.data(), header.data() + header.size(), + [&](const std::string &key, const std::string &val) { + file_.headers.emplace(key, val); + })) { + is_valid_ = false; + return false; + } + + constexpr const char header_content_type[] = "Content-Type:"; + + if (start_with_case_ignore(header, header_content_type)) { + file_.content_type = + trim_copy(header.substr(str_len(header_content_type))); + } else { + thread_local const std::regex re_content_disposition( + R"~(^Content-Disposition:\s*form-data;\s*(.*)$)~", + std::regex_constants::icase); + + std::smatch m; + if (std::regex_match(header, m, re_content_disposition)) { + Params params; + parse_disposition_params(m[1], params); + + auto it = params.find("name"); + if (it != params.end()) { + file_.name = it->second; + } else { + is_valid_ = false; + return false; + } + + it = params.find("filename"); + if (it != params.end()) { file_.filename = it->second; } + + it = params.find("filename*"); + if (it != params.end()) { + // Only allow UTF-8 encoding... + thread_local const std::regex re_rfc5987_encoding( + R"~(^UTF-8''(.+?)$)~", std::regex_constants::icase); + + std::smatch m2; + if (std::regex_match(it->second, m2, re_rfc5987_encoding)) { + file_.filename = decode_path_component(m2[1]); // override... + } else { + is_valid_ = false; + return false; + } + } + } + } + buf_erase(pos + crlf_.size()); + pos = buf_find(crlf_); + } + if (state_ != 3) { return true; } + break; + } + case 3: { // Body + if (crlf_dash_boundary_.size() > buf_size()) { return true; } + auto pos = buf_find(crlf_dash_boundary_); + if (pos < buf_size()) { + if (!content_callback(buf_data(), pos)) { + is_valid_ = false; + return false; + } + buf_erase(pos + crlf_dash_boundary_.size()); + state_ = 4; + } else { + auto len = buf_size() - crlf_dash_boundary_.size(); + if (len > 0) { + if (!content_callback(buf_data(), len)) { + is_valid_ = false; + return false; + } + buf_erase(len); + } + return true; + } + break; + } + case 4: { // Boundary + if (crlf_.size() > buf_size()) { return true; } + if (buf_start_with(crlf_)) { + buf_erase(crlf_.size()); + state_ = 1; + } else { + if (dash_.size() > buf_size()) { return true; } + if (buf_start_with(dash_)) { + buf_erase(dash_.size()); + is_valid_ = true; + buf_erase(buf_size()); // Remove epilogue + } else { + return true; + } + } + break; + } + } + } + + return true; + } + +private: + void clear_file_info() { + file_.name.clear(); + file_.filename.clear(); + file_.content_type.clear(); + file_.headers.clear(); + } + + bool start_with_case_ignore(const std::string &a, const char *b) const { + const auto b_len = strlen(b); + if (a.size() < b_len) { return false; } + for (size_t i = 0; i < b_len; i++) { + if (case_ignore::to_lower(a[i]) != case_ignore::to_lower(b[i])) { + return false; + } + } + return true; + } + + const std::string dash_ = "--"; + const std::string crlf_ = "\r\n"; + std::string boundary_; + std::string dash_boundary_crlf_; + std::string crlf_dash_boundary_; + + size_t state_ = 0; + bool is_valid_ = false; + FormData file_; + + // Buffer + bool start_with(const std::string &a, size_t spos, size_t epos, + const std::string &b) const { + if (epos - spos < b.size()) { return false; } + for (size_t i = 0; i < b.size(); i++) { + if (a[i + spos] != b[i]) { return false; } + } + return true; + } + + size_t buf_size() const { return buf_epos_ - buf_spos_; } + + const char *buf_data() const { return &buf_[buf_spos_]; } + + std::string buf_head(size_t l) const { return buf_.substr(buf_spos_, l); } + + bool buf_start_with(const std::string &s) const { + return start_with(buf_, buf_spos_, buf_epos_, s); + } + + size_t buf_find(const std::string &s) const { + auto c = s.front(); + + size_t off = buf_spos_; + while (off < buf_epos_) { + auto pos = off; + while (true) { + if (pos == buf_epos_) { return buf_size(); } + if (buf_[pos] == c) { break; } + pos++; + } + + auto remaining_size = buf_epos_ - pos; + if (s.size() > remaining_size) { return buf_size(); } + + if (start_with(buf_, pos, buf_epos_, s)) { return pos - buf_spos_; } + + off = pos + 1; + } + + return buf_size(); + } + + void buf_append(const char *data, size_t n) { + auto remaining_size = buf_size(); + if (remaining_size > 0 && buf_spos_ > 0) { + for (size_t i = 0; i < remaining_size; i++) { + buf_[i] = buf_[buf_spos_ + i]; + } + } + buf_spos_ = 0; + buf_epos_ = remaining_size; + + if (remaining_size + n > buf_.size()) { buf_.resize(remaining_size + n); } + + for (size_t i = 0; i < n; i++) { + buf_[buf_epos_ + i] = data[i]; + } + buf_epos_ += n; + } + + void buf_erase(size_t size) { buf_spos_ += size; } + + std::string buf_; + size_t buf_spos_ = 0; + size_t buf_epos_ = 0; +}; + +inline std::string random_string(size_t length) { + constexpr const char data[] = + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + thread_local auto engine([]() { + // std::random_device might actually be deterministic on some + // platforms, but due to lack of support in the c++ standard library, + // doing better requires either some ugly hacks or breaking portability. + std::random_device seed_gen; + // Request 128 bits of entropy for initialization + std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), seed_gen()}; + return std::mt19937(seed_sequence); + }()); + + std::string result; + for (size_t i = 0; i < length; i++) { + result += data[engine() % (sizeof(data) - 1)]; + } + return result; +} + +inline std::string make_multipart_data_boundary() { + return "--cpp-httplib-multipart-data-" + detail::random_string(16); +} + +inline bool is_multipart_boundary_chars_valid(const std::string &boundary) { + auto valid = true; + for (size_t i = 0; i < boundary.size(); i++) { + auto c = boundary[i]; + if (!std::isalnum(c) && c != '-' && c != '_') { + valid = false; + break; + } + } + return valid; +} + +template +inline std::string +serialize_multipart_formdata_item_begin(const T &item, + const std::string &boundary) { + std::string body = "--" + boundary + "\r\n"; + body += "Content-Disposition: form-data; name=\"" + item.name + "\""; + if (!item.filename.empty()) { + body += "; filename=\"" + item.filename + "\""; + } + body += "\r\n"; + if (!item.content_type.empty()) { + body += "Content-Type: " + item.content_type + "\r\n"; + } + body += "\r\n"; + + return body; +} + +inline std::string serialize_multipart_formdata_item_end() { return "\r\n"; } + +inline std::string +serialize_multipart_formdata_finish(const std::string &boundary) { + return "--" + boundary + "--\r\n"; +} + +inline std::string +serialize_multipart_formdata_get_content_type(const std::string &boundary) { + return "multipart/form-data; boundary=" + boundary; +} + +inline std::string +serialize_multipart_formdata(const UploadFormDataItems &items, + const std::string &boundary, bool finish = true) { + std::string body; + + for (const auto &item : items) { + body += serialize_multipart_formdata_item_begin(item, boundary); + body += item.content + serialize_multipart_formdata_item_end(); + } + + if (finish) { body += serialize_multipart_formdata_finish(boundary); } + + return body; +} + +inline void coalesce_ranges(Ranges &ranges, size_t content_length) { + if (ranges.size() <= 1) return; + + // Sort ranges by start position + std::sort(ranges.begin(), ranges.end(), + [](const Range &a, const Range &b) { return a.first < b.first; }); + + Ranges coalesced; + coalesced.reserve(ranges.size()); + + for (auto &r : ranges) { + auto first_pos = r.first; + auto last_pos = r.second; + + // Handle special cases like in range_error + if (first_pos == -1 && last_pos == -1) { + first_pos = 0; + last_pos = static_cast(content_length); + } + + if (first_pos == -1) { + first_pos = static_cast(content_length) - last_pos; + last_pos = static_cast(content_length) - 1; + } + + if (last_pos == -1 || last_pos >= static_cast(content_length)) { + last_pos = static_cast(content_length) - 1; + } + + // Skip invalid ranges + if (!(0 <= first_pos && first_pos <= last_pos && + last_pos < static_cast(content_length))) { + continue; + } + + // Coalesce with previous range if overlapping or adjacent (but not + // identical) + if (!coalesced.empty()) { + auto &prev = coalesced.back(); + // Check if current range overlaps or is adjacent to previous range + // but don't coalesce identical ranges (allow duplicates) + if (first_pos <= prev.second + 1 && + !(first_pos == prev.first && last_pos == prev.second)) { + // Extend the previous range + prev.second = (std::max)(prev.second, last_pos); + continue; + } + } + + // Add new range + coalesced.emplace_back(first_pos, last_pos); + } + + ranges = std::move(coalesced); +} + +inline bool range_error(Request &req, Response &res) { + if (!req.ranges.empty() && 200 <= res.status && res.status < 300) { + ssize_t content_len = static_cast( + res.content_length_ ? res.content_length_ : res.body.size()); + + std::vector> processed_ranges; + size_t overwrapping_count = 0; + + // NOTE: The following Range check is based on '14.2. Range' in RFC 9110 + // 'HTTP Semantics' to avoid potential denial-of-service attacks. + // https://www.rfc-editor.org/rfc/rfc9110#section-14.2 + + // Too many ranges + if (req.ranges.size() > CPPHTTPLIB_RANGE_MAX_COUNT) { return true; } + + for (auto &r : req.ranges) { + auto &first_pos = r.first; + auto &last_pos = r.second; + + if (first_pos == -1 && last_pos == -1) { + first_pos = 0; + last_pos = content_len; + } + + if (first_pos == -1) { + first_pos = content_len - last_pos; + last_pos = content_len - 1; + } + + // NOTE: RFC-9110 '14.1.2. Byte Ranges': + // A client can limit the number of bytes requested without knowing the + // size of the selected representation. If the last-pos value is absent, + // or if the value is greater than or equal to the current length of the + // representation data, the byte range is interpreted as the remainder of + // the representation (i.e., the server replaces the value of last-pos + // with a value that is one less than the current length of the selected + // representation). + // https://www.rfc-editor.org/rfc/rfc9110.html#section-14.1.2-6 + if (last_pos == -1 || last_pos >= content_len) { + last_pos = content_len - 1; + } + + // Range must be within content length + if (!(0 <= first_pos && first_pos <= last_pos && + last_pos <= content_len - 1)) { + return true; + } + + // Request must not have more than two overlapping ranges + for (const auto &processed_range : processed_ranges) { + if (!(last_pos < processed_range.first || + first_pos > processed_range.second)) { + overwrapping_count++; + if (overwrapping_count > 2) { return true; } + break; // Only count once per range + } + } + + processed_ranges.emplace_back(first_pos, last_pos); + } + + // After validation, coalesce overlapping ranges as per RFC 9110 + coalesce_ranges(req.ranges, static_cast(content_len)); + } + + return false; +} + +inline std::pair +get_range_offset_and_length(Range r, size_t content_length) { + assert(r.first != -1 && r.second != -1); + assert(0 <= r.first && r.first < static_cast(content_length)); + assert(r.first <= r.second && + r.second < static_cast(content_length)); + (void)(content_length); + return std::make_pair(r.first, static_cast(r.second - r.first) + 1); +} + +inline std::string make_content_range_header_field( + const std::pair &offset_and_length, size_t content_length) { + auto st = offset_and_length.first; + auto ed = st + offset_and_length.second - 1; + + std::string field = "bytes "; + field += std::to_string(st); + field += "-"; + field += std::to_string(ed); + field += "/"; + field += std::to_string(content_length); + return field; +} + +template +bool process_multipart_ranges_data(const Request &req, + const std::string &boundary, + const std::string &content_type, + size_t content_length, SToken stoken, + CToken ctoken, Content content) { + for (size_t i = 0; i < req.ranges.size(); i++) { + ctoken("--"); + stoken(boundary); + ctoken("\r\n"); + if (!content_type.empty()) { + ctoken("Content-Type: "); + stoken(content_type); + ctoken("\r\n"); + } + + auto offset_and_length = + get_range_offset_and_length(req.ranges[i], content_length); + + ctoken("Content-Range: "); + stoken(make_content_range_header_field(offset_and_length, content_length)); + ctoken("\r\n"); + ctoken("\r\n"); + + if (!content(offset_and_length.first, offset_and_length.second)) { + return false; + } + ctoken("\r\n"); + } + + ctoken("--"); + stoken(boundary); + ctoken("--"); + + return true; +} + +inline void make_multipart_ranges_data(const Request &req, Response &res, + const std::string &boundary, + const std::string &content_type, + size_t content_length, + std::string &data) { + process_multipart_ranges_data( + req, boundary, content_type, content_length, + [&](const std::string &token) { data += token; }, + [&](const std::string &token) { data += token; }, + [&](size_t offset, size_t length) { + assert(offset + length <= content_length); + data += res.body.substr(offset, length); + return true; + }); +} + +inline size_t get_multipart_ranges_data_length(const Request &req, + const std::string &boundary, + const std::string &content_type, + size_t content_length) { + size_t data_length = 0; + + process_multipart_ranges_data( + req, boundary, content_type, content_length, + [&](const std::string &token) { data_length += token.size(); }, + [&](const std::string &token) { data_length += token.size(); }, + [&](size_t /*offset*/, size_t length) { + data_length += length; + return true; + }); + + return data_length; +} + +template +inline bool +write_multipart_ranges_data(Stream &strm, const Request &req, Response &res, + const std::string &boundary, + const std::string &content_type, + size_t content_length, const T &is_shutting_down) { + return process_multipart_ranges_data( + req, boundary, content_type, content_length, + [&](const std::string &token) { strm.write(token); }, + [&](const std::string &token) { strm.write(token); }, + [&](size_t offset, size_t length) { + return write_content(strm, res.content_provider_, offset, length, + is_shutting_down); + }); +} + +inline bool expect_content(const Request &req) { + if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" || + req.method == "DELETE") { + return true; + } + if (req.has_header("Content-Length") && + req.get_header_value_u64("Content-Length") > 0) { + return true; + } + if (is_chunked_transfer_encoding(req.headers)) { return true; } + return false; +} + +inline bool has_crlf(const std::string &s) { + auto p = s.c_str(); + while (*p) { + if (*p == '\r' || *p == '\n') { return true; } + p++; + } + return false; +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline std::string message_digest(const std::string &s, const EVP_MD *algo) { + auto context = std::unique_ptr( + EVP_MD_CTX_new(), EVP_MD_CTX_free); + + unsigned int hash_length = 0; + unsigned char hash[EVP_MAX_MD_SIZE]; + + EVP_DigestInit_ex(context.get(), algo, nullptr); + EVP_DigestUpdate(context.get(), s.c_str(), s.size()); + EVP_DigestFinal_ex(context.get(), hash, &hash_length); + + std::stringstream ss; + for (auto i = 0u; i < hash_length; ++i) { + ss << std::hex << std::setw(2) << std::setfill('0') + << static_cast(hash[i]); + } + + return ss.str(); +} + +inline std::string MD5(const std::string &s) { + return message_digest(s, EVP_md5()); +} + +inline std::string SHA_256(const std::string &s) { + return message_digest(s, EVP_sha256()); +} + +inline std::string SHA_512(const std::string &s) { + return message_digest(s, EVP_sha512()); +} + +inline std::pair make_digest_authentication_header( + const Request &req, const std::map &auth, + size_t cnonce_count, const std::string &cnonce, const std::string &username, + const std::string &password, bool is_proxy = false) { + std::string nc; + { + std::stringstream ss; + ss << std::setfill('0') << std::setw(8) << std::hex << cnonce_count; + nc = ss.str(); + } + + std::string qop; + if (auth.find("qop") != auth.end()) { + qop = auth.at("qop"); + if (qop.find("auth-int") != std::string::npos) { + qop = "auth-int"; + } else if (qop.find("auth") != std::string::npos) { + qop = "auth"; + } else { + qop.clear(); + } + } + + std::string algo = "MD5"; + if (auth.find("algorithm") != auth.end()) { algo = auth.at("algorithm"); } + + std::string response; + { + auto H = algo == "SHA-256" ? detail::SHA_256 + : algo == "SHA-512" ? detail::SHA_512 + : detail::MD5; + + auto A1 = username + ":" + auth.at("realm") + ":" + password; + + auto A2 = req.method + ":" + req.path; + if (qop == "auth-int") { A2 += ":" + H(req.body); } + + if (qop.empty()) { + response = H(H(A1) + ":" + auth.at("nonce") + ":" + H(A2)); + } else { + response = H(H(A1) + ":" + auth.at("nonce") + ":" + nc + ":" + cnonce + + ":" + qop + ":" + H(A2)); + } + } + + auto opaque = (auth.find("opaque") != auth.end()) ? auth.at("opaque") : ""; + + auto field = "Digest username=\"" + username + "\", realm=\"" + + auth.at("realm") + "\", nonce=\"" + auth.at("nonce") + + "\", uri=\"" + req.path + "\", algorithm=" + algo + + (qop.empty() ? ", response=\"" + : ", qop=" + qop + ", nc=" + nc + ", cnonce=\"" + + cnonce + "\", response=\"") + + response + "\"" + + (opaque.empty() ? "" : ", opaque=\"" + opaque + "\""); + + auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; + return std::make_pair(key, field); +} + +inline bool is_ssl_peer_could_be_closed(SSL *ssl, socket_t sock) { + detail::set_nonblocking(sock, true); + auto se = detail::scope_exit([&]() { detail::set_nonblocking(sock, false); }); + + char buf[1]; + return !SSL_peek(ssl, buf, 1) && + SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN; +} + +#ifdef _WIN32 +// NOTE: This code came up with the following stackoverflow post: +// https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store +inline bool load_system_certs_on_windows(X509_STORE *store) { + auto hStore = CertOpenSystemStoreW((HCRYPTPROV_LEGACY)NULL, L"ROOT"); + if (!hStore) { return false; } + + auto result = false; + PCCERT_CONTEXT pContext = NULL; + while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != + nullptr) { + auto encoded_cert = + static_cast(pContext->pbCertEncoded); + + auto x509 = d2i_X509(NULL, &encoded_cert, pContext->cbCertEncoded); + if (x509) { + X509_STORE_add_cert(store, x509); + X509_free(x509); + result = true; + } + } + + CertFreeCertificateContext(pContext); + CertCloseStore(hStore, 0); + + return result; +} +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && TARGET_OS_MAC +template +using CFObjectPtr = + std::unique_ptr::type, void (*)(CFTypeRef)>; + +inline void cf_object_ptr_deleter(CFTypeRef obj) { + if (obj) { CFRelease(obj); } +} + +inline bool retrieve_certs_from_keychain(CFObjectPtr &certs) { + CFStringRef keys[] = {kSecClass, kSecMatchLimit, kSecReturnRef}; + CFTypeRef values[] = {kSecClassCertificate, kSecMatchLimitAll, + kCFBooleanTrue}; + + CFObjectPtr query( + CFDictionaryCreate(nullptr, reinterpret_cast(keys), values, + sizeof(keys) / sizeof(keys[0]), + &kCFTypeDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks), + cf_object_ptr_deleter); + + if (!query) { return false; } + + CFTypeRef security_items = nullptr; + if (SecItemCopyMatching(query.get(), &security_items) != errSecSuccess || + CFArrayGetTypeID() != CFGetTypeID(security_items)) { + return false; + } + + certs.reset(reinterpret_cast(security_items)); + return true; +} + +inline bool retrieve_root_certs_from_keychain(CFObjectPtr &certs) { + CFArrayRef root_security_items = nullptr; + if (SecTrustCopyAnchorCertificates(&root_security_items) != errSecSuccess) { + return false; + } + + certs.reset(root_security_items); + return true; +} + +inline bool add_certs_to_x509_store(CFArrayRef certs, X509_STORE *store) { + auto result = false; + for (auto i = 0; i < CFArrayGetCount(certs); ++i) { + const auto cert = reinterpret_cast( + CFArrayGetValueAtIndex(certs, i)); + + if (SecCertificateGetTypeID() != CFGetTypeID(cert)) { continue; } + + CFDataRef cert_data = nullptr; + if (SecItemExport(cert, kSecFormatX509Cert, 0, nullptr, &cert_data) != + errSecSuccess) { + continue; + } + + CFObjectPtr cert_data_ptr(cert_data, cf_object_ptr_deleter); + + auto encoded_cert = static_cast( + CFDataGetBytePtr(cert_data_ptr.get())); + + auto x509 = + d2i_X509(NULL, &encoded_cert, CFDataGetLength(cert_data_ptr.get())); + + if (x509) { + X509_STORE_add_cert(store, x509); + X509_free(x509); + result = true; + } + } + + return result; +} + +inline bool load_system_certs_on_macos(X509_STORE *store) { + auto result = false; + CFObjectPtr certs(nullptr, cf_object_ptr_deleter); + if (retrieve_certs_from_keychain(certs) && certs) { + result = add_certs_to_x509_store(certs.get(), store); + } + + if (retrieve_root_certs_from_keychain(certs) && certs) { + result = add_certs_to_x509_store(certs.get(), store) || result; + } + + return result; +} +#endif // _WIN32 +#endif // CPPHTTPLIB_OPENSSL_SUPPORT + +#ifdef _WIN32 +class WSInit { +public: + WSInit() { + WSADATA wsaData; + if (WSAStartup(0x0002, &wsaData) == 0) is_valid_ = true; + } + + ~WSInit() { + if (is_valid_) WSACleanup(); + } + + bool is_valid_ = false; +}; + +static WSInit wsinit_; +#endif + +inline bool parse_www_authenticate(const Response &res, + std::map &auth, + bool is_proxy) { + auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate"; + if (res.has_header(auth_key)) { + thread_local auto re = + std::regex(R"~((?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~"); + auto s = res.get_header_value(auth_key); + auto pos = s.find(' '); + if (pos != std::string::npos) { + auto type = s.substr(0, pos); + if (type == "Basic") { + return false; + } else if (type == "Digest") { + s = s.substr(pos + 1); + auto beg = std::sregex_iterator(s.begin(), s.end(), re); + for (auto i = beg; i != std::sregex_iterator(); ++i) { + const auto &m = *i; + auto key = s.substr(static_cast(m.position(1)), + static_cast(m.length(1))); + auto val = m.length(2) > 0 + ? s.substr(static_cast(m.position(2)), + static_cast(m.length(2))) + : s.substr(static_cast(m.position(3)), + static_cast(m.length(3))); + auth[key] = val; + } + return true; + } + } + } + return false; +} + +class ContentProviderAdapter { +public: + explicit ContentProviderAdapter( + ContentProviderWithoutLength &&content_provider) + : content_provider_(content_provider) {} + + bool operator()(size_t offset, size_t, DataSink &sink) { + return content_provider_(offset, sink); + } + +private: + ContentProviderWithoutLength content_provider_; +}; + +} // namespace detail + +inline std::string hosted_at(const std::string &hostname) { + std::vector addrs; + hosted_at(hostname, addrs); + if (addrs.empty()) { return std::string(); } + return addrs[0]; +} + +inline void hosted_at(const std::string &hostname, + std::vector &addrs) { + struct addrinfo hints; + struct addrinfo *result; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = 0; + + if (detail::getaddrinfo_with_timeout(hostname.c_str(), nullptr, &hints, + &result, 0)) { +#if defined __linux__ && !defined __ANDROID__ + res_init(); +#endif + return; + } + auto se = detail::scope_exit([&] { freeaddrinfo(result); }); + + for (auto rp = result; rp; rp = rp->ai_next) { + const auto &addr = + *reinterpret_cast(rp->ai_addr); + std::string ip; + auto dummy = -1; + if (detail::get_ip_and_port(addr, sizeof(struct sockaddr_storage), ip, + dummy)) { + addrs.push_back(ip); + } + } +} + +inline std::string encode_uri_component(const std::string &value) { + std::ostringstream escaped; + escaped.fill('0'); + escaped << std::hex; + + for (auto c : value) { + if (std::isalnum(static_cast(c)) || c == '-' || c == '_' || + c == '.' || c == '!' || c == '~' || c == '*' || c == '\'' || c == '(' || + c == ')') { + escaped << c; + } else { + escaped << std::uppercase; + escaped << '%' << std::setw(2) + << static_cast(static_cast(c)); + escaped << std::nouppercase; + } + } + + return escaped.str(); +} + +inline std::string encode_uri(const std::string &value) { + std::ostringstream escaped; + escaped.fill('0'); + escaped << std::hex; + + for (auto c : value) { + if (std::isalnum(static_cast(c)) || c == '-' || c == '_' || + c == '.' || c == '!' || c == '~' || c == '*' || c == '\'' || c == '(' || + c == ')' || c == ';' || c == '/' || c == '?' || c == ':' || c == '@' || + c == '&' || c == '=' || c == '+' || c == '$' || c == ',' || c == '#') { + escaped << c; + } else { + escaped << std::uppercase; + escaped << '%' << std::setw(2) + << static_cast(static_cast(c)); + escaped << std::nouppercase; + } + } + + return escaped.str(); +} + +inline std::string decode_uri_component(const std::string &value) { + std::string result; + + for (size_t i = 0; i < value.size(); i++) { + if (value[i] == '%' && i + 2 < value.size()) { + auto val = 0; + if (detail::from_hex_to_i(value, i + 1, 2, val)) { + result += static_cast(val); + i += 2; + } else { + result += value[i]; + } + } else { + result += value[i]; + } + } + + return result; +} + +inline std::string decode_uri(const std::string &value) { + std::string result; + + for (size_t i = 0; i < value.size(); i++) { + if (value[i] == '%' && i + 2 < value.size()) { + auto val = 0; + if (detail::from_hex_to_i(value, i + 1, 2, val)) { + result += static_cast(val); + i += 2; + } else { + result += value[i]; + } + } else { + result += value[i]; + } + } + + return result; +} + +inline std::string encode_path_component(const std::string &component) { + std::string result; + result.reserve(component.size() * 3); + + for (size_t i = 0; i < component.size(); i++) { + auto c = static_cast(component[i]); + + // Unreserved characters per RFC 3986: ALPHA / DIGIT / "-" / "." / "_" / "~" + if (std::isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~') { + result += static_cast(c); + } + // Path-safe sub-delimiters: "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / + // "," / ";" / "=" + else if (c == '!' || c == '$' || c == '&' || c == '\'' || c == '(' || + c == ')' || c == '*' || c == '+' || c == ',' || c == ';' || + c == '=') { + result += static_cast(c); + } + // Colon is allowed in path segments except first segment + else if (c == ':') { + result += static_cast(c); + } + // @ is allowed in path + else if (c == '@') { + result += static_cast(c); + } else { + result += '%'; + char hex[3]; + snprintf(hex, sizeof(hex), "%02X", c); + result.append(hex, 2); + } + } + return result; +} + +inline std::string decode_path_component(const std::string &component) { + std::string result; + result.reserve(component.size()); + + for (size_t i = 0; i < component.size(); i++) { + if (component[i] == '%' && i + 1 < component.size()) { + if (component[i + 1] == 'u') { + // Unicode %uXXXX encoding + auto val = 0; + if (detail::from_hex_to_i(component, i + 2, 4, val)) { + // 4 digits Unicode codes + char buff[4]; + size_t len = detail::to_utf8(val, buff); + if (len > 0) { result.append(buff, len); } + i += 5; // 'u0000' + } else { + result += component[i]; + } + } else { + // Standard %XX encoding + auto val = 0; + if (detail::from_hex_to_i(component, i + 1, 2, val)) { + // 2 digits hex codes + result += static_cast(val); + i += 2; // 'XX' + } else { + result += component[i]; + } + } + } else { + result += component[i]; + } + } + return result; +} + +inline std::string encode_query_component(const std::string &component, + bool space_as_plus) { + std::string result; + result.reserve(component.size() * 3); + + for (size_t i = 0; i < component.size(); i++) { + auto c = static_cast(component[i]); + + // Unreserved characters per RFC 3986 + if (std::isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~') { + result += static_cast(c); + } + // Space handling + else if (c == ' ') { + if (space_as_plus) { + result += '+'; + } else { + result += "%20"; + } + } + // Plus sign handling + else if (c == '+') { + if (space_as_plus) { + result += "%2B"; + } else { + result += static_cast(c); + } + } + // Query-safe sub-delimiters (excluding & and = which are query delimiters) + else if (c == '!' || c == '$' || c == '\'' || c == '(' || c == ')' || + c == '*' || c == ',' || c == ';') { + result += static_cast(c); + } + // Colon and @ are allowed in query + else if (c == ':' || c == '@') { + result += static_cast(c); + } + // Forward slash is allowed in query values + else if (c == '/') { + result += static_cast(c); + } + // Question mark is allowed in query values (after first ?) + else if (c == '?') { + result += static_cast(c); + } else { + result += '%'; + char hex[3]; + snprintf(hex, sizeof(hex), "%02X", c); + result.append(hex, 2); + } + } + return result; +} + +inline std::string decode_query_component(const std::string &component, + bool plus_as_space) { + std::string result; + result.reserve(component.size()); + + for (size_t i = 0; i < component.size(); i++) { + if (component[i] == '%' && i + 2 < component.size()) { + std::string hex = component.substr(i + 1, 2); + char *end; + unsigned long value = std::strtoul(hex.c_str(), &end, 16); + if (end == hex.c_str() + 2) { + result += static_cast(value); + i += 2; + } else { + result += component[i]; + } + } else if (component[i] == '+' && plus_as_space) { + result += ' '; // + becomes space in form-urlencoded + } else { + result += component[i]; + } + } + return result; +} + +inline std::string append_query_params(const std::string &path, + const Params ¶ms) { + std::string path_with_query = path; + thread_local const std::regex re("[^?]+\\?.*"); + auto delm = std::regex_match(path, re) ? '&' : '?'; + path_with_query += delm + detail::params_to_query_str(params); + return path_with_query; +} + +// Header utilities +inline std::pair +make_range_header(const Ranges &ranges) { + std::string field = "bytes="; + auto i = 0; + for (const auto &r : ranges) { + if (i != 0) { field += ", "; } + if (r.first != -1) { field += std::to_string(r.first); } + field += '-'; + if (r.second != -1) { field += std::to_string(r.second); } + i++; + } + return std::make_pair("Range", std::move(field)); +} + +inline std::pair +make_basic_authentication_header(const std::string &username, + const std::string &password, bool is_proxy) { + auto field = "Basic " + detail::base64_encode(username + ":" + password); + auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; + return std::make_pair(key, std::move(field)); +} + +inline std::pair +make_bearer_token_authentication_header(const std::string &token, + bool is_proxy = false) { + auto field = "Bearer " + token; + auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; + return std::make_pair(key, std::move(field)); +} + +// Request implementation +inline bool Request::has_header(const std::string &key) const { + return detail::has_header(headers, key); +} + +inline std::string Request::get_header_value(const std::string &key, + const char *def, size_t id) const { + return detail::get_header_value(headers, key, def, id); +} + +inline size_t Request::get_header_value_count(const std::string &key) const { + auto r = headers.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +inline void Request::set_header(const std::string &key, + const std::string &val) { + if (detail::fields::is_field_name(key) && + detail::fields::is_field_value(val)) { + headers.emplace(key, val); + } +} + +inline bool Request::has_trailer(const std::string &key) const { + return trailers.find(key) != trailers.end(); +} + +inline std::string Request::get_trailer_value(const std::string &key, + size_t id) const { + auto rng = trailers.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second; } + return std::string(); +} + +inline size_t Request::get_trailer_value_count(const std::string &key) const { + auto r = trailers.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +inline bool Request::has_param(const std::string &key) const { + return params.find(key) != params.end(); +} + +inline std::string Request::get_param_value(const std::string &key, + size_t id) const { + auto rng = params.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second; } + return std::string(); +} + +inline size_t Request::get_param_value_count(const std::string &key) const { + auto r = params.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +inline bool Request::is_multipart_form_data() const { + const auto &content_type = get_header_value("Content-Type"); + return !content_type.rfind("multipart/form-data", 0); +} + +// Multipart FormData implementation +inline std::string MultipartFormData::get_field(const std::string &key, + size_t id) const { + auto rng = fields.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second.content; } + return std::string(); +} + +inline std::vector +MultipartFormData::get_fields(const std::string &key) const { + std::vector values; + auto rng = fields.equal_range(key); + for (auto it = rng.first; it != rng.second; it++) { + values.push_back(it->second.content); + } + return values; +} + +inline bool MultipartFormData::has_field(const std::string &key) const { + return fields.find(key) != fields.end(); +} + +inline size_t MultipartFormData::get_field_count(const std::string &key) const { + auto r = fields.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +inline FormData MultipartFormData::get_file(const std::string &key, + size_t id) const { + auto rng = files.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second; } + return FormData(); +} + +inline std::vector +MultipartFormData::get_files(const std::string &key) const { + std::vector values; + auto rng = files.equal_range(key); + for (auto it = rng.first; it != rng.second; it++) { + values.push_back(it->second); + } + return values; +} + +inline bool MultipartFormData::has_file(const std::string &key) const { + return files.find(key) != files.end(); +} + +inline size_t MultipartFormData::get_file_count(const std::string &key) const { + auto r = files.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +// Response implementation +inline bool Response::has_header(const std::string &key) const { + return headers.find(key) != headers.end(); +} + +inline std::string Response::get_header_value(const std::string &key, + const char *def, + size_t id) const { + return detail::get_header_value(headers, key, def, id); +} + +inline size_t Response::get_header_value_count(const std::string &key) const { + auto r = headers.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +inline void Response::set_header(const std::string &key, + const std::string &val) { + if (detail::fields::is_field_name(key) && + detail::fields::is_field_value(val)) { + headers.emplace(key, val); + } +} +inline bool Response::has_trailer(const std::string &key) const { + return trailers.find(key) != trailers.end(); +} + +inline std::string Response::get_trailer_value(const std::string &key, + size_t id) const { + auto rng = trailers.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second; } + return std::string(); +} + +inline size_t Response::get_trailer_value_count(const std::string &key) const { + auto r = trailers.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +inline void Response::set_redirect(const std::string &url, int stat) { + if (detail::fields::is_field_value(url)) { + set_header("Location", url); + if (300 <= stat && stat < 400) { + this->status = stat; + } else { + this->status = StatusCode::Found_302; + } + } +} + +inline void Response::set_content(const char *s, size_t n, + const std::string &content_type) { + body.assign(s, n); + + auto rng = headers.equal_range("Content-Type"); + headers.erase(rng.first, rng.second); + set_header("Content-Type", content_type); +} + +inline void Response::set_content(const std::string &s, + const std::string &content_type) { + set_content(s.data(), s.size(), content_type); +} + +inline void Response::set_content(std::string &&s, + const std::string &content_type) { + body = std::move(s); + + auto rng = headers.equal_range("Content-Type"); + headers.erase(rng.first, rng.second); + set_header("Content-Type", content_type); +} + +inline void Response::set_content_provider( + size_t in_length, const std::string &content_type, ContentProvider provider, + ContentProviderResourceReleaser resource_releaser) { + set_header("Content-Type", content_type); + content_length_ = in_length; + if (in_length > 0) { content_provider_ = std::move(provider); } + content_provider_resource_releaser_ = std::move(resource_releaser); + is_chunked_content_provider_ = false; +} + +inline void Response::set_content_provider( + const std::string &content_type, ContentProviderWithoutLength provider, + ContentProviderResourceReleaser resource_releaser) { + set_header("Content-Type", content_type); + content_length_ = 0; + content_provider_ = detail::ContentProviderAdapter(std::move(provider)); + content_provider_resource_releaser_ = std::move(resource_releaser); + is_chunked_content_provider_ = false; +} + +inline void Response::set_chunked_content_provider( + const std::string &content_type, ContentProviderWithoutLength provider, + ContentProviderResourceReleaser resource_releaser) { + set_header("Content-Type", content_type); + content_length_ = 0; + content_provider_ = detail::ContentProviderAdapter(std::move(provider)); + content_provider_resource_releaser_ = std::move(resource_releaser); + is_chunked_content_provider_ = true; +} + +inline void Response::set_file_content(const std::string &path, + const std::string &content_type) { + file_content_path_ = path; + file_content_content_type_ = content_type; +} + +inline void Response::set_file_content(const std::string &path) { + file_content_path_ = path; +} + +// Result implementation +inline bool Result::has_request_header(const std::string &key) const { + return request_headers_.find(key) != request_headers_.end(); +} + +inline std::string Result::get_request_header_value(const std::string &key, + const char *def, + size_t id) const { + return detail::get_header_value(request_headers_, key, def, id); +} + +inline size_t +Result::get_request_header_value_count(const std::string &key) const { + auto r = request_headers_.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +// Stream implementation +inline ssize_t Stream::write(const char *ptr) { + return write(ptr, strlen(ptr)); +} + +inline ssize_t Stream::write(const std::string &s) { + return write(s.data(), s.size()); +} + +namespace detail { + +inline void calc_actual_timeout(time_t max_timeout_msec, time_t duration_msec, + time_t timeout_sec, time_t timeout_usec, + time_t &actual_timeout_sec, + time_t &actual_timeout_usec) { + auto timeout_msec = (timeout_sec * 1000) + (timeout_usec / 1000); + + auto actual_timeout_msec = + (std::min)(max_timeout_msec - duration_msec, timeout_msec); + + if (actual_timeout_msec < 0) { actual_timeout_msec = 0; } + + actual_timeout_sec = actual_timeout_msec / 1000; + actual_timeout_usec = (actual_timeout_msec % 1000) * 1000; +} + +// Socket stream implementation +inline SocketStream::SocketStream( + socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t max_timeout_msec, + std::chrono::time_point start_time) + : sock_(sock), read_timeout_sec_(read_timeout_sec), + read_timeout_usec_(read_timeout_usec), + write_timeout_sec_(write_timeout_sec), + write_timeout_usec_(write_timeout_usec), + max_timeout_msec_(max_timeout_msec), start_time_(start_time), + read_buff_(read_buff_size_, 0) {} + +inline SocketStream::~SocketStream() = default; + +inline bool SocketStream::is_readable() const { + return read_buff_off_ < read_buff_content_size_; +} + +inline bool SocketStream::wait_readable() const { + if (max_timeout_msec_ <= 0) { + return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; + } + + time_t read_timeout_sec; + time_t read_timeout_usec; + calc_actual_timeout(max_timeout_msec_, duration(), read_timeout_sec_, + read_timeout_usec_, read_timeout_sec, read_timeout_usec); + + return select_read(sock_, read_timeout_sec, read_timeout_usec) > 0; +} + +inline bool SocketStream::wait_writable() const { + return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && + is_socket_alive(sock_); +} + +inline ssize_t SocketStream::read(char *ptr, size_t size) { +#ifdef _WIN32 + size = + (std::min)(size, static_cast((std::numeric_limits::max)())); +#else + size = (std::min)(size, + static_cast((std::numeric_limits::max)())); +#endif + + if (read_buff_off_ < read_buff_content_size_) { + auto remaining_size = read_buff_content_size_ - read_buff_off_; + if (size <= remaining_size) { + memcpy(ptr, read_buff_.data() + read_buff_off_, size); + read_buff_off_ += size; + return static_cast(size); + } else { + memcpy(ptr, read_buff_.data() + read_buff_off_, remaining_size); + read_buff_off_ += remaining_size; + return static_cast(remaining_size); + } + } + + if (!wait_readable()) { return -1; } + + read_buff_off_ = 0; + read_buff_content_size_ = 0; + + if (size < read_buff_size_) { + auto n = read_socket(sock_, read_buff_.data(), read_buff_size_, + CPPHTTPLIB_RECV_FLAGS); + if (n <= 0) { + return n; + } else if (n <= static_cast(size)) { + memcpy(ptr, read_buff_.data(), static_cast(n)); + return n; + } else { + memcpy(ptr, read_buff_.data(), size); + read_buff_off_ = size; + read_buff_content_size_ = static_cast(n); + return static_cast(size); + } + } else { + return read_socket(sock_, ptr, size, CPPHTTPLIB_RECV_FLAGS); + } +} + +inline ssize_t SocketStream::write(const char *ptr, size_t size) { + if (!wait_writable()) { return -1; } + +#if defined(_WIN32) && !defined(_WIN64) + size = + (std::min)(size, static_cast((std::numeric_limits::max)())); +#endif + + return send_socket(sock_, ptr, size, CPPHTTPLIB_SEND_FLAGS); +} + +inline void SocketStream::get_remote_ip_and_port(std::string &ip, + int &port) const { + return detail::get_remote_ip_and_port(sock_, ip, port); +} + +inline void SocketStream::get_local_ip_and_port(std::string &ip, + int &port) const { + return detail::get_local_ip_and_port(sock_, ip, port); +} + +inline socket_t SocketStream::socket() const { return sock_; } + +inline time_t SocketStream::duration() const { + return std::chrono::duration_cast( + std::chrono::steady_clock::now() - start_time_) + .count(); +} + +// Buffer stream implementation +inline bool BufferStream::is_readable() const { return true; } + +inline bool BufferStream::wait_readable() const { return true; } + +inline bool BufferStream::wait_writable() const { return true; } + +inline ssize_t BufferStream::read(char *ptr, size_t size) { +#if defined(_MSC_VER) && _MSC_VER < 1910 + auto len_read = buffer._Copy_s(ptr, size, size, position); +#else + auto len_read = buffer.copy(ptr, size, position); +#endif + position += static_cast(len_read); + return static_cast(len_read); +} + +inline ssize_t BufferStream::write(const char *ptr, size_t size) { + buffer.append(ptr, size); + return static_cast(size); +} + +inline void BufferStream::get_remote_ip_and_port(std::string & /*ip*/, + int & /*port*/) const {} + +inline void BufferStream::get_local_ip_and_port(std::string & /*ip*/, + int & /*port*/) const {} + +inline socket_t BufferStream::socket() const { return 0; } + +inline time_t BufferStream::duration() const { return 0; } + +inline const std::string &BufferStream::get_buffer() const { return buffer; } + +inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) + : MatcherBase(pattern) { + constexpr const char marker[] = "/:"; + + // One past the last ending position of a path param substring + std::size_t last_param_end = 0; + +#ifndef CPPHTTPLIB_NO_EXCEPTIONS + // Needed to ensure that parameter names are unique during matcher + // construction + // If exceptions are disabled, only last duplicate path + // parameter will be set + std::unordered_set param_name_set; +#endif + + while (true) { + const auto marker_pos = pattern.find( + marker, last_param_end == 0 ? last_param_end : last_param_end - 1); + if (marker_pos == std::string::npos) { break; } + + static_fragments_.push_back( + pattern.substr(last_param_end, marker_pos - last_param_end + 1)); + + const auto param_name_start = marker_pos + str_len(marker); + + auto sep_pos = pattern.find(separator, param_name_start); + if (sep_pos == std::string::npos) { sep_pos = pattern.length(); } + + auto param_name = + pattern.substr(param_name_start, sep_pos - param_name_start); + +#ifndef CPPHTTPLIB_NO_EXCEPTIONS + if (param_name_set.find(param_name) != param_name_set.cend()) { + std::string msg = "Encountered path parameter '" + param_name + + "' multiple times in route pattern '" + pattern + "'."; + throw std::invalid_argument(msg); + } +#endif + + param_names_.push_back(std::move(param_name)); + + last_param_end = sep_pos + 1; + } + + if (last_param_end < pattern.length()) { + static_fragments_.push_back(pattern.substr(last_param_end)); + } +} + +inline bool PathParamsMatcher::match(Request &request) const { + request.matches = std::smatch(); + request.path_params.clear(); + request.path_params.reserve(param_names_.size()); + + // One past the position at which the path matched the pattern last time + std::size_t starting_pos = 0; + for (size_t i = 0; i < static_fragments_.size(); ++i) { + const auto &fragment = static_fragments_[i]; + + if (starting_pos + fragment.length() > request.path.length()) { + return false; + } + + // Avoid unnecessary allocation by using strncmp instead of substr + + // comparison + if (std::strncmp(request.path.c_str() + starting_pos, fragment.c_str(), + fragment.length()) != 0) { + return false; + } + + starting_pos += fragment.length(); + + // Should only happen when we have a static fragment after a param + // Example: '/users/:id/subscriptions' + // The 'subscriptions' fragment here does not have a corresponding param + if (i >= param_names_.size()) { continue; } + + auto sep_pos = request.path.find(separator, starting_pos); + if (sep_pos == std::string::npos) { sep_pos = request.path.length(); } + + const auto ¶m_name = param_names_[i]; + + request.path_params.emplace( + param_name, request.path.substr(starting_pos, sep_pos - starting_pos)); + + // Mark everything up to '/' as matched + starting_pos = sep_pos + 1; + } + // Returns false if the path is longer than the pattern + return starting_pos >= request.path.length(); +} + +inline bool RegexMatcher::match(Request &request) const { + request.path_params.clear(); + return std::regex_match(request.path, request.matches, regex_); +} + +inline std::string make_host_and_port_string(const std::string &host, int port, + bool is_ssl) { + std::string result; + + // Enclose IPv6 address in brackets (but not if already enclosed) + if (host.find(':') == std::string::npos || + (!host.empty() && host[0] == '[')) { + // IPv4, hostname, or already bracketed IPv6 + result = host; + } else { + // IPv6 address without brackets + result = "[" + host + "]"; + } + + // Append port if not default + if ((!is_ssl && port == 80) || (is_ssl && port == 443)) { + ; // do nothing + } else { + result += ":" + std::to_string(port); + } + + return result; +} + +} // namespace detail + +// HTTP server implementation +inline Server::Server() + : new_task_queue( + [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }) { +#ifndef _WIN32 + signal(SIGPIPE, SIG_IGN); +#endif +} + +inline Server::~Server() = default; + +inline std::unique_ptr +Server::make_matcher(const std::string &pattern) { + if (pattern.find("/:") != std::string::npos) { + return detail::make_unique(pattern); + } else { + return detail::make_unique(pattern); + } +} + +inline Server &Server::Get(const std::string &pattern, Handler handler) { + get_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Post(const std::string &pattern, Handler handler) { + post_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Post(const std::string &pattern, + HandlerWithContentReader handler) { + post_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); + return *this; +} + +inline Server &Server::Put(const std::string &pattern, Handler handler) { + put_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Put(const std::string &pattern, + HandlerWithContentReader handler) { + put_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); + return *this; +} + +inline Server &Server::Patch(const std::string &pattern, Handler handler) { + patch_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Patch(const std::string &pattern, + HandlerWithContentReader handler) { + patch_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); + return *this; +} + +inline Server &Server::Delete(const std::string &pattern, Handler handler) { + delete_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Delete(const std::string &pattern, + HandlerWithContentReader handler) { + delete_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); + return *this; +} + +inline Server &Server::Options(const std::string &pattern, Handler handler) { + options_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline bool Server::set_base_dir(const std::string &dir, + const std::string &mount_point) { + return set_mount_point(mount_point, dir); +} + +inline bool Server::set_mount_point(const std::string &mount_point, + const std::string &dir, Headers headers) { + detail::FileStat stat(dir); + if (stat.is_dir()) { + std::string mnt = !mount_point.empty() ? mount_point : "/"; + if (!mnt.empty() && mnt[0] == '/') { + base_dirs_.push_back({mnt, dir, std::move(headers)}); + return true; + } + } + return false; +} + +inline bool Server::remove_mount_point(const std::string &mount_point) { + for (auto it = base_dirs_.begin(); it != base_dirs_.end(); ++it) { + if (it->mount_point == mount_point) { + base_dirs_.erase(it); + return true; + } + } + return false; +} + +inline Server & +Server::set_file_extension_and_mimetype_mapping(const std::string &ext, + const std::string &mime) { + file_extension_and_mimetype_map_[ext] = mime; + return *this; +} + +inline Server &Server::set_default_file_mimetype(const std::string &mime) { + default_file_mimetype_ = mime; + return *this; +} + +inline Server &Server::set_file_request_handler(Handler handler) { + file_request_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_error_handler_core(HandlerWithResponse handler, + std::true_type) { + error_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_error_handler_core(Handler handler, + std::false_type) { + error_handler_ = [handler](const Request &req, Response &res) { + handler(req, res); + return HandlerResponse::Handled; + }; + return *this; +} + +inline Server &Server::set_exception_handler(ExceptionHandler handler) { + exception_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_pre_routing_handler(HandlerWithResponse handler) { + pre_routing_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_post_routing_handler(Handler handler) { + post_routing_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_pre_request_handler(HandlerWithResponse handler) { + pre_request_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_logger(Logger logger) { + logger_ = std::move(logger); + return *this; +} + +inline Server &Server::set_error_logger(ErrorLogger error_logger) { + error_logger_ = std::move(error_logger); + return *this; +} + +inline Server &Server::set_pre_compression_logger(Logger logger) { + pre_compression_logger_ = std::move(logger); + return *this; +} + +inline Server & +Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) { + expect_100_continue_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_address_family(int family) { + address_family_ = family; + return *this; +} + +inline Server &Server::set_tcp_nodelay(bool on) { + tcp_nodelay_ = on; + return *this; +} + +inline Server &Server::set_ipv6_v6only(bool on) { + ipv6_v6only_ = on; + return *this; +} + +inline Server &Server::set_socket_options(SocketOptions socket_options) { + socket_options_ = std::move(socket_options); + return *this; +} + +inline Server &Server::set_default_headers(Headers headers) { + default_headers_ = std::move(headers); + return *this; +} + +inline Server &Server::set_header_writer( + std::function const &writer) { + header_writer_ = writer; + return *this; +} + +inline Server & +Server::set_trusted_proxies(const std::vector &proxies) { + trusted_proxies_ = proxies; + return *this; +} + +inline Server &Server::set_keep_alive_max_count(size_t count) { + keep_alive_max_count_ = count; + return *this; +} + +inline Server &Server::set_keep_alive_timeout(time_t sec) { + keep_alive_timeout_sec_ = sec; + return *this; +} + +inline Server &Server::set_read_timeout(time_t sec, time_t usec) { + read_timeout_sec_ = sec; + read_timeout_usec_ = usec; + return *this; +} + +inline Server &Server::set_write_timeout(time_t sec, time_t usec) { + write_timeout_sec_ = sec; + write_timeout_usec_ = usec; + return *this; +} + +inline Server &Server::set_idle_interval(time_t sec, time_t usec) { + idle_interval_sec_ = sec; + idle_interval_usec_ = usec; + return *this; +} + +inline Server &Server::set_payload_max_length(size_t length) { + payload_max_length_ = length; + return *this; +} + +inline bool Server::bind_to_port(const std::string &host, int port, + int socket_flags) { + auto ret = bind_internal(host, port, socket_flags); + if (ret == -1) { is_decommissioned = true; } + return ret >= 0; +} +inline int Server::bind_to_any_port(const std::string &host, int socket_flags) { + auto ret = bind_internal(host, 0, socket_flags); + if (ret == -1) { is_decommissioned = true; } + return ret; +} + +inline bool Server::listen_after_bind() { return listen_internal(); } + +inline bool Server::listen(const std::string &host, int port, + int socket_flags) { + return bind_to_port(host, port, socket_flags) && listen_internal(); +} + +inline bool Server::is_running() const { return is_running_; } + +inline void Server::wait_until_ready() const { + while (!is_running_ && !is_decommissioned) { + std::this_thread::sleep_for(std::chrono::milliseconds{1}); + } +} + +inline void Server::stop() { + if (is_running_) { + assert(svr_sock_ != INVALID_SOCKET); + std::atomic sock(svr_sock_.exchange(INVALID_SOCKET)); + detail::shutdown_socket(sock); + detail::close_socket(sock); + } + is_decommissioned = false; +} + +inline void Server::decommission() { is_decommissioned = true; } + +inline bool Server::parse_request_line(const char *s, Request &req) const { + auto len = strlen(s); + if (len < 2 || s[len - 2] != '\r' || s[len - 1] != '\n') { return false; } + len -= 2; + + { + size_t count = 0; + + detail::split(s, s + len, ' ', [&](const char *b, const char *e) { + switch (count) { + case 0: req.method = std::string(b, e); break; + case 1: req.target = std::string(b, e); break; + case 2: req.version = std::string(b, e); break; + default: break; + } + count++; + }); + + if (count != 3) { return false; } + } + + thread_local const std::set methods{ + "GET", "HEAD", "POST", "PUT", "DELETE", + "CONNECT", "OPTIONS", "TRACE", "PATCH", "PRI"}; + + if (methods.find(req.method) == methods.end()) { + output_error_log(Error::InvalidHTTPMethod, &req); + return false; + } + + if (req.version != "HTTP/1.1" && req.version != "HTTP/1.0") { + output_error_log(Error::InvalidHTTPVersion, &req); + return false; + } + + { + // Skip URL fragment + for (size_t i = 0; i < req.target.size(); i++) { + if (req.target[i] == '#') { + req.target.erase(i); + break; + } + } + + detail::divide(req.target, '?', + [&](const char *lhs_data, std::size_t lhs_size, + const char *rhs_data, std::size_t rhs_size) { + req.path = + decode_path_component(std::string(lhs_data, lhs_size)); + detail::parse_query_text(rhs_data, rhs_size, req.params); + }); + } + + return true; +} + +inline bool Server::write_response(Stream &strm, bool close_connection, + Request &req, Response &res) { + // NOTE: `req.ranges` should be empty, otherwise it will be applied + // incorrectly to the error content. + req.ranges.clear(); + return write_response_core(strm, close_connection, req, res, false); +} + +inline bool Server::write_response_with_content(Stream &strm, + bool close_connection, + const Request &req, + Response &res) { + return write_response_core(strm, close_connection, req, res, true); +} + +inline bool Server::write_response_core(Stream &strm, bool close_connection, + const Request &req, Response &res, + bool need_apply_ranges) { + assert(res.status != -1); + + if (400 <= res.status && error_handler_ && + error_handler_(req, res) == HandlerResponse::Handled) { + need_apply_ranges = true; + } + + std::string content_type; + std::string boundary; + if (need_apply_ranges) { apply_ranges(req, res, content_type, boundary); } + + // Prepare additional headers + if (close_connection || req.get_header_value("Connection") == "close") { + res.set_header("Connection", "close"); + } else { + std::string s = "timeout="; + s += std::to_string(keep_alive_timeout_sec_); + s += ", max="; + s += std::to_string(keep_alive_max_count_); + res.set_header("Keep-Alive", s); + } + + if ((!res.body.empty() || res.content_length_ > 0 || res.content_provider_) && + !res.has_header("Content-Type")) { + res.set_header("Content-Type", "text/plain"); + } + + if (res.body.empty() && !res.content_length_ && !res.content_provider_ && + !res.has_header("Content-Length")) { + res.set_header("Content-Length", "0"); + } + + if (req.method == "HEAD" && !res.has_header("Accept-Ranges")) { + res.set_header("Accept-Ranges", "bytes"); + } + + if (post_routing_handler_) { post_routing_handler_(req, res); } + + // Response line and headers + { + detail::BufferStream bstrm; + if (!detail::write_response_line(bstrm, res.status)) { return false; } + if (!header_writer_(bstrm, res.headers)) { return false; } + + // Flush buffer + auto &data = bstrm.get_buffer(); + detail::write_data(strm, data.data(), data.size()); + } + + // Body + auto ret = true; + if (req.method != "HEAD") { + if (!res.body.empty()) { + if (!detail::write_data(strm, res.body.data(), res.body.size())) { + ret = false; + } + } else if (res.content_provider_) { + if (write_content_with_provider(strm, req, res, boundary, content_type)) { + res.content_provider_success_ = true; + } else { + ret = false; + } + } + } + + // Log + output_log(req, res); + + return ret; +} + +inline bool +Server::write_content_with_provider(Stream &strm, const Request &req, + Response &res, const std::string &boundary, + const std::string &content_type) { + auto is_shutting_down = [this]() { + return this->svr_sock_ == INVALID_SOCKET; + }; + + if (res.content_length_ > 0) { + if (req.ranges.empty()) { + return detail::write_content(strm, res.content_provider_, 0, + res.content_length_, is_shutting_down); + } else if (req.ranges.size() == 1) { + auto offset_and_length = detail::get_range_offset_and_length( + req.ranges[0], res.content_length_); + + return detail::write_content(strm, res.content_provider_, + offset_and_length.first, + offset_and_length.second, is_shutting_down); + } else { + return detail::write_multipart_ranges_data( + strm, req, res, boundary, content_type, res.content_length_, + is_shutting_down); + } + } else { + if (res.is_chunked_content_provider_) { + auto type = detail::encoding_type(req, res); + + std::unique_ptr compressor; + if (type == detail::EncodingType::Gzip) { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + compressor = detail::make_unique(); +#endif + } else if (type == detail::EncodingType::Brotli) { +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + compressor = detail::make_unique(); +#endif + } else if (type == detail::EncodingType::Zstd) { +#ifdef CPPHTTPLIB_ZSTD_SUPPORT + compressor = detail::make_unique(); +#endif + } else { + compressor = detail::make_unique(); + } + assert(compressor != nullptr); + + return detail::write_content_chunked(strm, res.content_provider_, + is_shutting_down, *compressor); + } else { + return detail::write_content_without_length(strm, res.content_provider_, + is_shutting_down); + } + } +} + +inline bool Server::read_content(Stream &strm, Request &req, Response &res) { + FormFields::iterator cur_field; + FormFiles::iterator cur_file; + auto is_text_field = false; + size_t count = 0; + if (read_content_core( + strm, req, res, + // Regular + [&](const char *buf, size_t n) { + if (req.body.size() + n > req.body.max_size()) { return false; } + req.body.append(buf, n); + return true; + }, + // Multipart FormData + [&](const FormData &file) { + if (count++ == CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT) { + output_error_log(Error::TooManyFormDataFiles, &req); + return false; + } + + if (file.filename.empty()) { + cur_field = req.form.fields.emplace( + file.name, FormField{file.name, file.content, file.headers}); + is_text_field = true; + } else { + cur_file = req.form.files.emplace(file.name, file); + is_text_field = false; + } + return true; + }, + [&](const char *buf, size_t n) { + if (is_text_field) { + auto &content = cur_field->second.content; + if (content.size() + n > content.max_size()) { return false; } + content.append(buf, n); + } else { + auto &content = cur_file->second.content; + if (content.size() + n > content.max_size()) { return false; } + content.append(buf, n); + } + return true; + })) { + const auto &content_type = req.get_header_value("Content-Type"); + if (!content_type.find("application/x-www-form-urlencoded")) { + if (req.body.size() > CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH) { + res.status = StatusCode::PayloadTooLarge_413; // NOTE: should be 414? + output_error_log(Error::ExceedMaxPayloadSize, &req); + return false; + } + detail::parse_query_text(req.body, req.params); + } + return true; + } + return false; +} + +inline bool Server::read_content_with_content_receiver( + Stream &strm, Request &req, Response &res, ContentReceiver receiver, + FormDataHeader multipart_header, ContentReceiver multipart_receiver) { + return read_content_core(strm, req, res, std::move(receiver), + std::move(multipart_header), + std::move(multipart_receiver)); +} + +inline bool Server::read_content_core( + Stream &strm, Request &req, Response &res, ContentReceiver receiver, + FormDataHeader multipart_header, ContentReceiver multipart_receiver) const { + detail::FormDataParser multipart_form_data_parser; + ContentReceiverWithProgress out; + + if (req.is_multipart_form_data()) { + const auto &content_type = req.get_header_value("Content-Type"); + std::string boundary; + if (!detail::parse_multipart_boundary(content_type, boundary)) { + res.status = StatusCode::BadRequest_400; + output_error_log(Error::MultipartParsing, &req); + return false; + } + + multipart_form_data_parser.set_boundary(std::move(boundary)); + out = [&](const char *buf, size_t n, size_t /*off*/, size_t /*len*/) { + return multipart_form_data_parser.parse(buf, n, multipart_header, + multipart_receiver); + }; + } else { + out = [receiver](const char *buf, size_t n, size_t /*off*/, + size_t /*len*/) { return receiver(buf, n); }; + } + + if (req.method == "DELETE" && !req.has_header("Content-Length")) { + return true; + } + + if (!detail::read_content(strm, req, payload_max_length_, res.status, nullptr, + out, true)) { + return false; + } + + if (req.is_multipart_form_data()) { + if (!multipart_form_data_parser.is_valid()) { + res.status = StatusCode::BadRequest_400; + output_error_log(Error::MultipartParsing, &req); + return false; + } + } + + return true; +} + +inline bool Server::handle_file_request(const Request &req, Response &res) { + for (const auto &entry : base_dirs_) { + // Prefix match + if (!req.path.compare(0, entry.mount_point.size(), entry.mount_point)) { + std::string sub_path = "/" + req.path.substr(entry.mount_point.size()); + if (detail::is_valid_path(sub_path)) { + auto path = entry.base_dir + sub_path; + if (path.back() == '/') { path += "index.html"; } + + detail::FileStat stat(path); + + if (stat.is_dir()) { + res.set_redirect(sub_path + "/", StatusCode::MovedPermanently_301); + return true; + } + + if (stat.is_file()) { + for (const auto &kv : entry.headers) { + res.set_header(kv.first, kv.second); + } + + auto mm = std::make_shared(path.c_str()); + if (!mm->is_open()) { + output_error_log(Error::OpenFile, &req); + return false; + } + + res.set_content_provider( + mm->size(), + detail::find_content_type(path, file_extension_and_mimetype_map_, + default_file_mimetype_), + [mm](size_t offset, size_t length, DataSink &sink) -> bool { + sink.write(mm->data() + offset, length); + return true; + }); + + if (req.method != "HEAD" && file_request_handler_) { + file_request_handler_(req, res); + } + + return true; + } else { + output_error_log(Error::OpenFile, &req); + } + } + } + } + return false; +} + +inline socket_t +Server::create_server_socket(const std::string &host, int port, + int socket_flags, + SocketOptions socket_options) const { + return detail::create_socket( + host, std::string(), port, address_family_, socket_flags, tcp_nodelay_, + ipv6_v6only_, std::move(socket_options), + [&](socket_t sock, struct addrinfo &ai, bool & /*quit*/) -> bool { + if (::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { + output_error_log(Error::BindIPAddress, nullptr); + return false; + } + if (::listen(sock, CPPHTTPLIB_LISTEN_BACKLOG)) { + output_error_log(Error::Listen, nullptr); + return false; + } + return true; + }); +} + +inline int Server::bind_internal(const std::string &host, int port, + int socket_flags) { + if (is_decommissioned) { return -1; } + + if (!is_valid()) { return -1; } + + svr_sock_ = create_server_socket(host, port, socket_flags, socket_options_); + if (svr_sock_ == INVALID_SOCKET) { return -1; } + + if (port == 0) { + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); + if (getsockname(svr_sock_, reinterpret_cast(&addr), + &addr_len) == -1) { + output_error_log(Error::GetSockName, nullptr); + return -1; + } + if (addr.ss_family == AF_INET) { + return ntohs(reinterpret_cast(&addr)->sin_port); + } else if (addr.ss_family == AF_INET6) { + return ntohs(reinterpret_cast(&addr)->sin6_port); + } else { + output_error_log(Error::UnsupportedAddressFamily, nullptr); + return -1; + } + } else { + return port; + } +} + +inline bool Server::listen_internal() { + if (is_decommissioned) { return false; } + + auto ret = true; + is_running_ = true; + auto se = detail::scope_exit([&]() { is_running_ = false; }); + + { + std::unique_ptr task_queue(new_task_queue()); + + while (svr_sock_ != INVALID_SOCKET) { +#ifndef _WIN32 + if (idle_interval_sec_ > 0 || idle_interval_usec_ > 0) { +#endif + auto val = detail::select_read(svr_sock_, idle_interval_sec_, + idle_interval_usec_); + if (val == 0) { // Timeout + task_queue->on_idle(); + continue; + } +#ifndef _WIN32 + } +#endif + +#if defined _WIN32 + // sockets connected via WASAccept inherit flags NO_HANDLE_INHERIT, + // OVERLAPPED + socket_t sock = WSAAccept(svr_sock_, nullptr, nullptr, nullptr, 0); +#elif defined SOCK_CLOEXEC + socket_t sock = accept4(svr_sock_, nullptr, nullptr, SOCK_CLOEXEC); +#else + socket_t sock = accept(svr_sock_, nullptr, nullptr); +#endif + + if (sock == INVALID_SOCKET) { + if (errno == EMFILE) { + // The per-process limit of open file descriptors has been reached. + // Try to accept new connections after a short sleep. + std::this_thread::sleep_for(std::chrono::microseconds{1}); + continue; + } else if (errno == EINTR || errno == EAGAIN) { + continue; + } + if (svr_sock_ != INVALID_SOCKET) { + detail::close_socket(svr_sock_); + ret = false; + output_error_log(Error::Connection, nullptr); + } else { + ; // The server socket was closed by user. + } + break; + } + + detail::set_socket_opt_time(sock, SOL_SOCKET, SO_RCVTIMEO, + read_timeout_sec_, read_timeout_usec_); + detail::set_socket_opt_time(sock, SOL_SOCKET, SO_SNDTIMEO, + write_timeout_sec_, write_timeout_usec_); + + if (!task_queue->enqueue( + [this, sock]() { process_and_close_socket(sock); })) { + output_error_log(Error::ResourceExhaustion, nullptr); + detail::shutdown_socket(sock); + detail::close_socket(sock); + } + } + + task_queue->shutdown(); + } + + is_decommissioned = !ret; + return ret; +} + +inline bool Server::routing(Request &req, Response &res, Stream &strm) { + if (pre_routing_handler_ && + pre_routing_handler_(req, res) == HandlerResponse::Handled) { + return true; + } + + // File handler + if ((req.method == "GET" || req.method == "HEAD") && + handle_file_request(req, res)) { + return true; + } + + if (detail::expect_content(req)) { + // Content reader handler + { + ContentReader reader( + [&](ContentReceiver receiver) { + auto result = read_content_with_content_receiver( + strm, req, res, std::move(receiver), nullptr, nullptr); + if (!result) { output_error_log(Error::Read, &req); } + return result; + }, + [&](FormDataHeader header, ContentReceiver receiver) { + auto result = read_content_with_content_receiver( + strm, req, res, nullptr, std::move(header), + std::move(receiver)); + if (!result) { output_error_log(Error::Read, &req); } + return result; + }); + + if (req.method == "POST") { + if (dispatch_request_for_content_reader( + req, res, std::move(reader), + post_handlers_for_content_reader_)) { + return true; + } + } else if (req.method == "PUT") { + if (dispatch_request_for_content_reader( + req, res, std::move(reader), + put_handlers_for_content_reader_)) { + return true; + } + } else if (req.method == "PATCH") { + if (dispatch_request_for_content_reader( + req, res, std::move(reader), + patch_handlers_for_content_reader_)) { + return true; + } + } else if (req.method == "DELETE") { + if (dispatch_request_for_content_reader( + req, res, std::move(reader), + delete_handlers_for_content_reader_)) { + return true; + } + } + } + + // Read content into `req.body` + if (!read_content(strm, req, res)) { + output_error_log(Error::Read, &req); + return false; + } + } + + // Regular handler + if (req.method == "GET" || req.method == "HEAD") { + return dispatch_request(req, res, get_handlers_); + } else if (req.method == "POST") { + return dispatch_request(req, res, post_handlers_); + } else if (req.method == "PUT") { + return dispatch_request(req, res, put_handlers_); + } else if (req.method == "DELETE") { + return dispatch_request(req, res, delete_handlers_); + } else if (req.method == "OPTIONS") { + return dispatch_request(req, res, options_handlers_); + } else if (req.method == "PATCH") { + return dispatch_request(req, res, patch_handlers_); + } + + res.status = StatusCode::BadRequest_400; + return false; +} + +inline bool Server::dispatch_request(Request &req, Response &res, + const Handlers &handlers) const { + for (const auto &x : handlers) { + const auto &matcher = x.first; + const auto &handler = x.second; + + if (matcher->match(req)) { + req.matched_route = matcher->pattern(); + if (!pre_request_handler_ || + pre_request_handler_(req, res) != HandlerResponse::Handled) { + handler(req, res); + } + return true; + } + } + return false; +} + +inline void Server::apply_ranges(const Request &req, Response &res, + std::string &content_type, + std::string &boundary) const { + if (req.ranges.size() > 1 && res.status == StatusCode::PartialContent_206) { + auto it = res.headers.find("Content-Type"); + if (it != res.headers.end()) { + content_type = it->second; + res.headers.erase(it); + } + + boundary = detail::make_multipart_data_boundary(); + + res.set_header("Content-Type", + "multipart/byteranges; boundary=" + boundary); + } + + auto type = detail::encoding_type(req, res); + + if (res.body.empty()) { + if (res.content_length_ > 0) { + size_t length = 0; + if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) { + length = res.content_length_; + } else if (req.ranges.size() == 1) { + auto offset_and_length = detail::get_range_offset_and_length( + req.ranges[0], res.content_length_); + + length = offset_and_length.second; + + auto content_range = detail::make_content_range_header_field( + offset_and_length, res.content_length_); + res.set_header("Content-Range", content_range); + } else { + length = detail::get_multipart_ranges_data_length( + req, boundary, content_type, res.content_length_); + } + res.set_header("Content-Length", std::to_string(length)); + } else { + if (res.content_provider_) { + if (res.is_chunked_content_provider_) { + res.set_header("Transfer-Encoding", "chunked"); + if (type == detail::EncodingType::Gzip) { + res.set_header("Content-Encoding", "gzip"); + } else if (type == detail::EncodingType::Brotli) { + res.set_header("Content-Encoding", "br"); + } else if (type == detail::EncodingType::Zstd) { + res.set_header("Content-Encoding", "zstd"); + } + } + } + } + } else { + if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) { + ; + } else if (req.ranges.size() == 1) { + auto offset_and_length = + detail::get_range_offset_and_length(req.ranges[0], res.body.size()); + auto offset = offset_and_length.first; + auto length = offset_and_length.second; + + auto content_range = detail::make_content_range_header_field( + offset_and_length, res.body.size()); + res.set_header("Content-Range", content_range); + + assert(offset + length <= res.body.size()); + res.body = res.body.substr(offset, length); + } else { + std::string data; + detail::make_multipart_ranges_data(req, res, boundary, content_type, + res.body.size(), data); + res.body.swap(data); + } + + if (type != detail::EncodingType::None) { + output_pre_compression_log(req, res); + + std::unique_ptr compressor; + std::string content_encoding; + + if (type == detail::EncodingType::Gzip) { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + compressor = detail::make_unique(); + content_encoding = "gzip"; +#endif + } else if (type == detail::EncodingType::Brotli) { +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + compressor = detail::make_unique(); + content_encoding = "br"; +#endif + } else if (type == detail::EncodingType::Zstd) { +#ifdef CPPHTTPLIB_ZSTD_SUPPORT + compressor = detail::make_unique(); + content_encoding = "zstd"; +#endif + } + + if (compressor) { + std::string compressed; + if (compressor->compress(res.body.data(), res.body.size(), true, + [&](const char *data, size_t data_len) { + compressed.append(data, data_len); + return true; + })) { + res.body.swap(compressed); + res.set_header("Content-Encoding", content_encoding); + } + } + } + + auto length = std::to_string(res.body.size()); + res.set_header("Content-Length", length); + } +} + +inline bool Server::dispatch_request_for_content_reader( + Request &req, Response &res, ContentReader content_reader, + const HandlersForContentReader &handlers) const { + for (const auto &x : handlers) { + const auto &matcher = x.first; + const auto &handler = x.second; + + if (matcher->match(req)) { + req.matched_route = matcher->pattern(); + if (!pre_request_handler_ || + pre_request_handler_(req, res) != HandlerResponse::Handled) { + handler(req, res, content_reader); + } + return true; + } + } + return false; +} + +inline std::string +get_client_ip(const std::string &x_forwarded_for, + const std::vector &trusted_proxies) { + // X-Forwarded-For is a comma-separated list per RFC 7239 + std::vector ip_list; + detail::split(x_forwarded_for.data(), + x_forwarded_for.data() + x_forwarded_for.size(), ',', + [&](const char *b, const char *e) { + auto r = detail::trim(b, e, 0, static_cast(e - b)); + ip_list.emplace_back(std::string(b + r.first, b + r.second)); + }); + + for (size_t i = 0; i < ip_list.size(); ++i) { + auto ip = ip_list[i]; + + auto is_trusted_proxy = + std::any_of(trusted_proxies.begin(), trusted_proxies.end(), + [&](const std::string &proxy) { return ip == proxy; }); + + if (is_trusted_proxy) { + if (i == 0) { + // If the trusted proxy is the first IP, there's no preceding client IP + return ip; + } else { + // Return the IP immediately before the trusted proxy + return ip_list[i - 1]; + } + } + } + + // If no trusted proxy is found, return the first IP in the list + return ip_list.front(); +} + +inline bool +Server::process_request(Stream &strm, const std::string &remote_addr, + int remote_port, const std::string &local_addr, + int local_port, bool close_connection, + bool &connection_closed, + const std::function &setup_request) { + std::array buf{}; + + detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); + + // Connection has been closed on client + if (!line_reader.getline()) { return false; } + + Request req; + req.start_time_ = std::chrono::steady_clock::now(); + + Response res; + res.version = "HTTP/1.1"; + res.headers = default_headers_; + +#ifdef __APPLE__ + // Socket file descriptor exceeded FD_SETSIZE... + if (strm.socket() >= FD_SETSIZE) { + Headers dummy; + detail::read_headers(strm, dummy); + res.status = StatusCode::InternalServerError_500; + output_error_log(Error::ExceedMaxSocketDescriptorCount, &req); + return write_response(strm, close_connection, req, res); + } +#endif + + // Request line and headers + if (!parse_request_line(line_reader.ptr(), req)) { + res.status = StatusCode::BadRequest_400; + output_error_log(Error::InvalidRequestLine, &req); + return write_response(strm, close_connection, req, res); + } + + // Request headers + if (!detail::read_headers(strm, req.headers)) { + res.status = StatusCode::BadRequest_400; + output_error_log(Error::InvalidHeaders, &req); + return write_response(strm, close_connection, req, res); + } + + // Check if the request URI doesn't exceed the limit + if (req.target.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) { + Headers dummy; + detail::read_headers(strm, dummy); + res.status = StatusCode::UriTooLong_414; + output_error_log(Error::ExceedUriMaxLength, &req); + return write_response(strm, close_connection, req, res); + } + + if (req.get_header_value("Connection") == "close") { + connection_closed = true; + } + + if (req.version == "HTTP/1.0" && + req.get_header_value("Connection") != "Keep-Alive") { + connection_closed = true; + } + + if (!trusted_proxies_.empty() && req.has_header("X-Forwarded-For")) { + auto x_forwarded_for = req.get_header_value("X-Forwarded-For"); + req.remote_addr = get_client_ip(x_forwarded_for, trusted_proxies_); + } else { + req.remote_addr = remote_addr; + } + req.remote_port = remote_port; + + req.local_addr = local_addr; + req.local_port = local_port; + + if (req.has_header("Accept")) { + const auto &accept_header = req.get_header_value("Accept"); + if (!detail::parse_accept_header(accept_header, req.accept_content_types)) { + res.status = StatusCode::BadRequest_400; + output_error_log(Error::HTTPParsing, &req); + return write_response(strm, close_connection, req, res); + } + } + + if (req.has_header("Range")) { + const auto &range_header_value = req.get_header_value("Range"); + if (!detail::parse_range_header(range_header_value, req.ranges)) { + res.status = StatusCode::RangeNotSatisfiable_416; + output_error_log(Error::InvalidRangeHeader, &req); + return write_response(strm, close_connection, req, res); + } + } + + if (setup_request) { setup_request(req); } + + if (req.get_header_value("Expect") == "100-continue") { + int status = StatusCode::Continue_100; + if (expect_100_continue_handler_) { + status = expect_100_continue_handler_(req, res); + } + switch (status) { + case StatusCode::Continue_100: + case StatusCode::ExpectationFailed_417: + detail::write_response_line(strm, status); + strm.write("\r\n"); + break; + default: + connection_closed = true; + return write_response(strm, true, req, res); + } + } + + // Setup `is_connection_closed` method + auto sock = strm.socket(); + req.is_connection_closed = [sock]() { + return !detail::is_socket_alive(sock); + }; + + // Routing + auto routed = false; +#ifdef CPPHTTPLIB_NO_EXCEPTIONS + routed = routing(req, res, strm); +#else + try { + routed = routing(req, res, strm); + } catch (std::exception &e) { + if (exception_handler_) { + auto ep = std::current_exception(); + exception_handler_(req, res, ep); + routed = true; + } else { + res.status = StatusCode::InternalServerError_500; + std::string val; + auto s = e.what(); + for (size_t i = 0; s[i]; i++) { + switch (s[i]) { + case '\r': val += "\\r"; break; + case '\n': val += "\\n"; break; + default: val += s[i]; break; + } + } + res.set_header("EXCEPTION_WHAT", val); + } + } catch (...) { + if (exception_handler_) { + auto ep = std::current_exception(); + exception_handler_(req, res, ep); + routed = true; + } else { + res.status = StatusCode::InternalServerError_500; + res.set_header("EXCEPTION_WHAT", "UNKNOWN"); + } + } +#endif + if (routed) { + if (res.status == -1) { + res.status = req.ranges.empty() ? StatusCode::OK_200 + : StatusCode::PartialContent_206; + } + + // Serve file content by using a content provider + if (!res.file_content_path_.empty()) { + const auto &path = res.file_content_path_; + auto mm = std::make_shared(path.c_str()); + if (!mm->is_open()) { + res.body.clear(); + res.content_length_ = 0; + res.content_provider_ = nullptr; + res.status = StatusCode::NotFound_404; + output_error_log(Error::OpenFile, &req); + return write_response(strm, close_connection, req, res); + } + + auto content_type = res.file_content_content_type_; + if (content_type.empty()) { + content_type = detail::find_content_type( + path, file_extension_and_mimetype_map_, default_file_mimetype_); + } + + res.set_content_provider( + mm->size(), content_type, + [mm](size_t offset, size_t length, DataSink &sink) -> bool { + sink.write(mm->data() + offset, length); + return true; + }); + } + + if (detail::range_error(req, res)) { + res.body.clear(); + res.content_length_ = 0; + res.content_provider_ = nullptr; + res.status = StatusCode::RangeNotSatisfiable_416; + return write_response(strm, close_connection, req, res); + } + + return write_response_with_content(strm, close_connection, req, res); + } else { + if (res.status == -1) { res.status = StatusCode::NotFound_404; } + + return write_response(strm, close_connection, req, res); + } +} + +inline bool Server::is_valid() const { return true; } + +inline bool Server::process_and_close_socket(socket_t sock) { + std::string remote_addr; + int remote_port = 0; + detail::get_remote_ip_and_port(sock, remote_addr, remote_port); + + std::string local_addr; + int local_port = 0; + detail::get_local_ip_and_port(sock, local_addr, local_port); + + auto ret = detail::process_server_socket( + svr_sock_, sock, keep_alive_max_count_, keep_alive_timeout_sec_, + read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, + [&](Stream &strm, bool close_connection, bool &connection_closed) { + return process_request(strm, remote_addr, remote_port, local_addr, + local_port, close_connection, connection_closed, + nullptr); + }); + + detail::shutdown_socket(sock); + detail::close_socket(sock); + return ret; +} + +inline void Server::output_log(const Request &req, const Response &res) const { + if (logger_) { + std::lock_guard guard(logger_mutex_); + logger_(req, res); + } +} + +inline void Server::output_pre_compression_log(const Request &req, + const Response &res) const { + if (pre_compression_logger_) { + std::lock_guard guard(logger_mutex_); + pre_compression_logger_(req, res); + } +} + +inline void Server::output_error_log(const Error &err, + const Request *req) const { + if (error_logger_) { + std::lock_guard guard(logger_mutex_); + error_logger_(err, req); + } +} + +// HTTP client implementation +inline ClientImpl::ClientImpl(const std::string &host) + : ClientImpl(host, 80, std::string(), std::string()) {} + +inline ClientImpl::ClientImpl(const std::string &host, int port) + : ClientImpl(host, port, std::string(), std::string()) {} + +inline ClientImpl::ClientImpl(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path) + : host_(detail::escape_abstract_namespace_unix_domain(host)), port_(port), + host_and_port_(detail::make_host_and_port_string(host_, port, is_ssl())), + client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} + +inline ClientImpl::~ClientImpl() { + // Wait until all the requests in flight are handled. + size_t retry_count = 10; + while (retry_count-- > 0) { + { + std::lock_guard guard(socket_mutex_); + if (socket_requests_in_flight_ == 0) { break; } + } + std::this_thread::sleep_for(std::chrono::milliseconds{1}); + } + + std::lock_guard guard(socket_mutex_); + shutdown_socket(socket_); + close_socket(socket_); +} + +inline bool ClientImpl::is_valid() const { return true; } + +inline void ClientImpl::copy_settings(const ClientImpl &rhs) { + client_cert_path_ = rhs.client_cert_path_; + client_key_path_ = rhs.client_key_path_; + connection_timeout_sec_ = rhs.connection_timeout_sec_; + read_timeout_sec_ = rhs.read_timeout_sec_; + read_timeout_usec_ = rhs.read_timeout_usec_; + write_timeout_sec_ = rhs.write_timeout_sec_; + write_timeout_usec_ = rhs.write_timeout_usec_; + max_timeout_msec_ = rhs.max_timeout_msec_; + basic_auth_username_ = rhs.basic_auth_username_; + basic_auth_password_ = rhs.basic_auth_password_; + bearer_token_auth_token_ = rhs.bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + digest_auth_username_ = rhs.digest_auth_username_; + digest_auth_password_ = rhs.digest_auth_password_; +#endif + keep_alive_ = rhs.keep_alive_; + follow_location_ = rhs.follow_location_; + path_encode_ = rhs.path_encode_; + address_family_ = rhs.address_family_; + tcp_nodelay_ = rhs.tcp_nodelay_; + ipv6_v6only_ = rhs.ipv6_v6only_; + socket_options_ = rhs.socket_options_; + compress_ = rhs.compress_; + decompress_ = rhs.decompress_; + interface_ = rhs.interface_; + proxy_host_ = rhs.proxy_host_; + proxy_port_ = rhs.proxy_port_; + proxy_basic_auth_username_ = rhs.proxy_basic_auth_username_; + proxy_basic_auth_password_ = rhs.proxy_basic_auth_password_; + proxy_bearer_token_auth_token_ = rhs.proxy_bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + proxy_digest_auth_username_ = rhs.proxy_digest_auth_username_; + proxy_digest_auth_password_ = rhs.proxy_digest_auth_password_; +#endif +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + ca_cert_file_path_ = rhs.ca_cert_file_path_; + ca_cert_dir_path_ = rhs.ca_cert_dir_path_; + ca_cert_store_ = rhs.ca_cert_store_; +#endif +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + server_certificate_verification_ = rhs.server_certificate_verification_; + server_hostname_verification_ = rhs.server_hostname_verification_; + server_certificate_verifier_ = rhs.server_certificate_verifier_; +#endif + logger_ = rhs.logger_; + error_logger_ = rhs.error_logger_; +} + +inline socket_t ClientImpl::create_client_socket(Error &error) const { + if (!proxy_host_.empty() && proxy_port_ != -1) { + return detail::create_client_socket( + proxy_host_, std::string(), proxy_port_, address_family_, tcp_nodelay_, + ipv6_v6only_, socket_options_, connection_timeout_sec_, + connection_timeout_usec_, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, interface_, error); + } + + // Check is custom IP specified for host_ + std::string ip; + auto it = addr_map_.find(host_); + if (it != addr_map_.end()) { ip = it->second; } + + return detail::create_client_socket( + host_, ip, port_, address_family_, tcp_nodelay_, ipv6_v6only_, + socket_options_, connection_timeout_sec_, connection_timeout_usec_, + read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, interface_, error); +} + +inline bool ClientImpl::create_and_connect_socket(Socket &socket, + Error &error) { + auto sock = create_client_socket(error); + if (sock == INVALID_SOCKET) { return false; } + socket.sock = sock; + return true; +} + +inline void ClientImpl::shutdown_ssl(Socket & /*socket*/, + bool /*shutdown_gracefully*/) { + // If there are any requests in flight from threads other than us, then it's + // a thread-unsafe race because individual ssl* objects are not thread-safe. + assert(socket_requests_in_flight_ == 0 || + socket_requests_are_from_thread_ == std::this_thread::get_id()); +} + +inline void ClientImpl::shutdown_socket(Socket &socket) const { + if (socket.sock == INVALID_SOCKET) { return; } + detail::shutdown_socket(socket.sock); +} + +inline void ClientImpl::close_socket(Socket &socket) { + // If there are requests in flight in another thread, usually closing + // the socket will be fine and they will simply receive an error when + // using the closed socket, but it is still a bug since rarely the OS + // may reassign the socket id to be used for a new socket, and then + // suddenly they will be operating on a live socket that is different + // than the one they intended! + assert(socket_requests_in_flight_ == 0 || + socket_requests_are_from_thread_ == std::this_thread::get_id()); + + // It is also a bug if this happens while SSL is still active +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + assert(socket.ssl == nullptr); +#endif + if (socket.sock == INVALID_SOCKET) { return; } + detail::close_socket(socket.sock); + socket.sock = INVALID_SOCKET; +} + +inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, + Response &res) const { + std::array buf{}; + + detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); + + if (!line_reader.getline()) { return false; } + +#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR + thread_local const std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r?\n"); +#else + thread_local const std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n"); +#endif + + std::cmatch m; + if (!std::regex_match(line_reader.ptr(), m, re)) { + return req.method == "CONNECT"; + } + res.version = std::string(m[1]); + res.status = std::stoi(std::string(m[2])); + res.reason = std::string(m[3]); + + // Ignore '100 Continue' + while (res.status == StatusCode::Continue_100) { + if (!line_reader.getline()) { return false; } // CRLF + if (!line_reader.getline()) { return false; } // next response line + + if (!std::regex_match(line_reader.ptr(), m, re)) { return false; } + res.version = std::string(m[1]); + res.status = std::stoi(std::string(m[2])); + res.reason = std::string(m[3]); + } + + return true; +} + +inline bool ClientImpl::send(Request &req, Response &res, Error &error) { + std::lock_guard request_mutex_guard(request_mutex_); + auto ret = send_(req, res, error); + if (error == Error::SSLPeerCouldBeClosed_) { + assert(!ret); + ret = send_(req, res, error); + } + return ret; +} + +inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { + { + std::lock_guard guard(socket_mutex_); + + // Set this to false immediately - if it ever gets set to true by the end + // of the request, we know another thread instructed us to close the + // socket. + socket_should_be_closed_when_request_is_done_ = false; + + auto is_alive = false; + if (socket_.is_open()) { + is_alive = detail::is_socket_alive(socket_.sock); + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (is_alive && is_ssl()) { + if (detail::is_ssl_peer_could_be_closed(socket_.ssl, socket_.sock)) { + is_alive = false; + } + } +#endif + + if (!is_alive) { + // Attempt to avoid sigpipe by shutting down non-gracefully if it + // seems like the other side has already closed the connection Also, + // there cannot be any requests in flight from other threads since we + // locked request_mutex_, so safe to close everything immediately + const bool shutdown_gracefully = false; + shutdown_ssl(socket_, shutdown_gracefully); + shutdown_socket(socket_); + close_socket(socket_); + } + } + + if (!is_alive) { + if (!create_and_connect_socket(socket_, error)) { + output_error_log(error, &req); + return false; + } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + // TODO: refactoring + if (is_ssl()) { + auto &scli = static_cast(*this); + if (!proxy_host_.empty() && proxy_port_ != -1) { + auto success = false; + if (!scli.connect_with_proxy(socket_, req.start_time_, res, success, + error)) { + if (!success) { output_error_log(error, &req); } + return success; + } + } + + if (!scli.initialize_ssl(socket_, error)) { + output_error_log(error, &req); + return false; + } + } +#endif + } + + // Mark the current socket as being in use so that it cannot be closed by + // anyone else while this request is ongoing, even though we will be + // releasing the mutex. + if (socket_requests_in_flight_ > 1) { + assert(socket_requests_are_from_thread_ == std::this_thread::get_id()); + } + socket_requests_in_flight_ += 1; + socket_requests_are_from_thread_ = std::this_thread::get_id(); + } + + for (const auto &header : default_headers_) { + if (req.headers.find(header.first) == req.headers.end()) { + req.headers.insert(header); + } + } + + auto ret = false; + auto close_connection = !keep_alive_; + + auto se = detail::scope_exit([&]() { + // Briefly lock mutex in order to mark that a request is no longer ongoing + std::lock_guard guard(socket_mutex_); + socket_requests_in_flight_ -= 1; + if (socket_requests_in_flight_ <= 0) { + assert(socket_requests_in_flight_ == 0); + socket_requests_are_from_thread_ = std::thread::id(); + } + + if (socket_should_be_closed_when_request_is_done_ || close_connection || + !ret) { + shutdown_ssl(socket_, true); + shutdown_socket(socket_); + close_socket(socket_); + } + }); + + ret = process_socket(socket_, req.start_time_, [&](Stream &strm) { + return handle_request(strm, req, res, close_connection, error); + }); + + if (!ret) { + if (error == Error::Success) { + error = Error::Unknown; + output_error_log(error, &req); + } + } + + return ret; +} + +inline Result ClientImpl::send(const Request &req) { + auto req2 = req; + return send_(std::move(req2)); +} + +inline Result ClientImpl::send_(Request &&req) { + auto res = detail::make_unique(); + auto error = Error::Success; + auto ret = send(req, *res, error); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + return Result{ret ? std::move(res) : nullptr, error, std::move(req.headers), + last_ssl_error_, last_openssl_error_}; +#else + return Result{ret ? std::move(res) : nullptr, error, std::move(req.headers)}; +#endif +} + +inline bool ClientImpl::handle_request(Stream &strm, Request &req, + Response &res, bool close_connection, + Error &error) { + if (req.path.empty()) { + error = Error::Connection; + output_error_log(error, &req); + return false; + } + + auto req_save = req; + + bool ret; + + if (!is_ssl() && !proxy_host_.empty() && proxy_port_ != -1) { + auto req2 = req; + req2.path = "http://" + host_and_port_ + req.path; + ret = process_request(strm, req2, res, close_connection, error); + req = req2; + req.path = req_save.path; + } else { + ret = process_request(strm, req, res, close_connection, error); + } + + if (!ret) { return false; } + + if (res.get_header_value("Connection") == "close" || + (res.version == "HTTP/1.0" && res.reason != "Connection established")) { + // TODO this requires a not-entirely-obvious chain of calls to be correct + // for this to be safe. + + // This is safe to call because handle_request is only called by send_ + // which locks the request mutex during the process. It would be a bug + // to call it from a different thread since it's a thread-safety issue + // to do these things to the socket if another thread is using the socket. + std::lock_guard guard(socket_mutex_); + shutdown_ssl(socket_, true); + shutdown_socket(socket_); + close_socket(socket_); + } + + if (300 < res.status && res.status < 400 && follow_location_) { + req = req_save; + ret = redirect(req, res, error); + } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if ((res.status == StatusCode::Unauthorized_401 || + res.status == StatusCode::ProxyAuthenticationRequired_407) && + req.authorization_count_ < 5) { + auto is_proxy = res.status == StatusCode::ProxyAuthenticationRequired_407; + const auto &username = + is_proxy ? proxy_digest_auth_username_ : digest_auth_username_; + const auto &password = + is_proxy ? proxy_digest_auth_password_ : digest_auth_password_; + + if (!username.empty() && !password.empty()) { + std::map auth; + if (detail::parse_www_authenticate(res, auth, is_proxy)) { + Request new_req = req; + new_req.authorization_count_ += 1; + new_req.headers.erase(is_proxy ? "Proxy-Authorization" + : "Authorization"); + new_req.headers.insert(detail::make_digest_authentication_header( + req, auth, new_req.authorization_count_, detail::random_string(10), + username, password, is_proxy)); + + Response new_res; + + ret = send(new_req, new_res, error); + if (ret) { res = new_res; } + } + } + } +#endif + + return ret; +} + +inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { + if (req.redirect_count_ == 0) { + error = Error::ExceedRedirectCount; + output_error_log(error, &req); + return false; + } + + auto location = res.get_header_value("location"); + if (location.empty()) { return false; } + + thread_local const std::regex re( + R"((?:(https?):)?(?://(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)?([^?#]*)(\?[^#]*)?(?:#.*)?)"); + + std::smatch m; + if (!std::regex_match(location, m, re)) { return false; } + + auto scheme = is_ssl() ? "https" : "http"; + + auto next_scheme = m[1].str(); + auto next_host = m[2].str(); + if (next_host.empty()) { next_host = m[3].str(); } + auto port_str = m[4].str(); + auto next_path = m[5].str(); + auto next_query = m[6].str(); + + auto next_port = port_; + if (!port_str.empty()) { + next_port = std::stoi(port_str); + } else if (!next_scheme.empty()) { + next_port = next_scheme == "https" ? 443 : 80; + } + + if (next_scheme.empty()) { next_scheme = scheme; } + if (next_host.empty()) { next_host = host_; } + if (next_path.empty()) { next_path = "/"; } + + auto path = decode_query_component(next_path, true) + next_query; + + // Same host redirect - use current client + if (next_scheme == scheme && next_host == host_ && next_port == port_) { + return detail::redirect(*this, req, res, path, location, error); + } + + // Cross-host/scheme redirect - create new client with robust setup + return create_redirect_client(next_scheme, next_host, next_port, req, res, + path, location, error); +} + +// New method for robust redirect client creation +inline bool ClientImpl::create_redirect_client( + const std::string &scheme, const std::string &host, int port, Request &req, + Response &res, const std::string &path, const std::string &location, + Error &error) { + // Determine if we need SSL + auto need_ssl = (scheme == "https"); + + // Clean up request headers that are host/client specific + // Remove headers that should not be carried over to new host + auto headers_to_remove = + std::vector{"Host", "Proxy-Authorization", "Authorization"}; + + for (const auto &header_name : headers_to_remove) { + auto it = req.headers.find(header_name); + while (it != req.headers.end()) { + it = req.headers.erase(it); + it = req.headers.find(header_name); + } + } + + // Create appropriate client type and handle redirect + if (need_ssl) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + // Create SSL client for HTTPS redirect + SSLClient redirect_client(host, port); + + // Setup basic client configuration first + setup_redirect_client(redirect_client); + + // SSL-specific configuration for proxy environments + if (!proxy_host_.empty() && proxy_port_ != -1) { + // Critical: Disable SSL verification for proxy environments + redirect_client.enable_server_certificate_verification(false); + redirect_client.enable_server_hostname_verification(false); + } else { + // For direct SSL connections, copy SSL verification settings + redirect_client.enable_server_certificate_verification( + server_certificate_verification_); + redirect_client.enable_server_hostname_verification( + server_hostname_verification_); + } + + // Handle CA certificate store and paths if available + if (ca_cert_store_ && X509_STORE_up_ref(ca_cert_store_)) { + redirect_client.set_ca_cert_store(ca_cert_store_); + } + if (!ca_cert_file_path_.empty()) { + redirect_client.set_ca_cert_path(ca_cert_file_path_, ca_cert_dir_path_); + } + + // Client certificates are set through constructor for SSLClient + // NOTE: SSLClient constructor already takes client_cert_path and + // client_key_path so we need to create it properly if client certs are + // needed + + // Execute the redirect + return detail::redirect(redirect_client, req, res, path, location, error); +#else + // SSL not supported - set appropriate error + error = Error::SSLConnection; + output_error_log(error, &req); + return false; +#endif + } else { + // HTTP redirect + ClientImpl redirect_client(host, port); + + // Setup client with robust configuration + setup_redirect_client(redirect_client); + + // Execute the redirect + return detail::redirect(redirect_client, req, res, path, location, error); + } +} + +// New method for robust client setup (based on basic_manual_redirect.cpp +// logic) +template +inline void ClientImpl::setup_redirect_client(ClientType &client) { + // Copy basic settings first + client.set_connection_timeout(connection_timeout_sec_); + client.set_read_timeout(read_timeout_sec_, read_timeout_usec_); + client.set_write_timeout(write_timeout_sec_, write_timeout_usec_); + client.set_keep_alive(keep_alive_); + client.set_follow_location( + true); // Enable redirects to handle multi-step redirects + client.set_path_encode(path_encode_); + client.set_compress(compress_); + client.set_decompress(decompress_); + + // Copy authentication settings BEFORE proxy setup + if (!basic_auth_username_.empty()) { + client.set_basic_auth(basic_auth_username_, basic_auth_password_); + } + if (!bearer_token_auth_token_.empty()) { + client.set_bearer_token_auth(bearer_token_auth_token_); + } +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (!digest_auth_username_.empty()) { + client.set_digest_auth(digest_auth_username_, digest_auth_password_); + } +#endif + + // Setup proxy configuration (CRITICAL ORDER - proxy must be set + // before proxy auth) + if (!proxy_host_.empty() && proxy_port_ != -1) { + // First set proxy host and port + client.set_proxy(proxy_host_, proxy_port_); + + // Then set proxy authentication (order matters!) + if (!proxy_basic_auth_username_.empty()) { + client.set_proxy_basic_auth(proxy_basic_auth_username_, + proxy_basic_auth_password_); + } + if (!proxy_bearer_token_auth_token_.empty()) { + client.set_proxy_bearer_token_auth(proxy_bearer_token_auth_token_); + } +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (!proxy_digest_auth_username_.empty()) { + client.set_proxy_digest_auth(proxy_digest_auth_username_, + proxy_digest_auth_password_); + } +#endif + } + + // Copy network and socket settings + client.set_address_family(address_family_); + client.set_tcp_nodelay(tcp_nodelay_); + client.set_ipv6_v6only(ipv6_v6only_); + if (socket_options_) { client.set_socket_options(socket_options_); } + if (!interface_.empty()) { client.set_interface(interface_); } + + // Copy logging and headers + if (logger_) { client.set_logger(logger_); } + if (error_logger_) { client.set_error_logger(error_logger_); } + + // NOTE: DO NOT copy default_headers_ as they may contain stale Host headers + // Each new client should generate its own headers based on its target host +} + +inline bool ClientImpl::write_content_with_provider(Stream &strm, + const Request &req, + Error &error) const { + auto is_shutting_down = []() { return false; }; + + if (req.is_chunked_content_provider_) { + // TODO: Brotli support + std::unique_ptr compressor; +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + if (compress_) { + compressor = detail::make_unique(); + } else +#endif + { + compressor = detail::make_unique(); + } + + return detail::write_content_chunked(strm, req.content_provider_, + is_shutting_down, *compressor, error); + } else { + return detail::write_content_with_progress( + strm, req.content_provider_, 0, req.content_length_, is_shutting_down, + req.upload_progress, error); + } +} + +inline bool ClientImpl::write_request(Stream &strm, Request &req, + bool close_connection, Error &error) { + // Prepare additional headers + if (close_connection) { + if (!req.has_header("Connection")) { + req.set_header("Connection", "close"); + } + } + + if (!req.has_header("Host")) { + // For Unix socket connections, use "localhost" as Host header (similar to + // curl behavior) + if (address_family_ == AF_UNIX) { + req.set_header("Host", "localhost"); + } else { + req.set_header("Host", host_and_port_); + } + } + + if (!req.has_header("Accept")) { req.set_header("Accept", "*/*"); } + + if (!req.content_receiver) { + if (!req.has_header("Accept-Encoding")) { + std::string accept_encoding; +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + accept_encoding = "br"; +#endif +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + if (!accept_encoding.empty()) { accept_encoding += ", "; } + accept_encoding += "gzip, deflate"; +#endif +#ifdef CPPHTTPLIB_ZSTD_SUPPORT + if (!accept_encoding.empty()) { accept_encoding += ", "; } + accept_encoding += "zstd"; +#endif + req.set_header("Accept-Encoding", accept_encoding); + } + +#ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT + if (!req.has_header("User-Agent")) { + auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION; + req.set_header("User-Agent", agent); + } +#endif + }; + + if (req.body.empty()) { + if (req.content_provider_) { + if (!req.is_chunked_content_provider_) { + if (!req.has_header("Content-Length")) { + auto length = std::to_string(req.content_length_); + req.set_header("Content-Length", length); + } + } + } else { + if (req.method == "POST" || req.method == "PUT" || + req.method == "PATCH") { + req.set_header("Content-Length", "0"); + } + } + } else { + if (!req.has_header("Content-Type")) { + req.set_header("Content-Type", "text/plain"); + } + + if (!req.has_header("Content-Length")) { + auto length = std::to_string(req.body.size()); + req.set_header("Content-Length", length); + } + } + + if (!basic_auth_password_.empty() || !basic_auth_username_.empty()) { + if (!req.has_header("Authorization")) { + req.headers.insert(make_basic_authentication_header( + basic_auth_username_, basic_auth_password_, false)); + } + } + + if (!proxy_basic_auth_username_.empty() && + !proxy_basic_auth_password_.empty()) { + if (!req.has_header("Proxy-Authorization")) { + req.headers.insert(make_basic_authentication_header( + proxy_basic_auth_username_, proxy_basic_auth_password_, true)); + } + } + + if (!bearer_token_auth_token_.empty()) { + if (!req.has_header("Authorization")) { + req.headers.insert(make_bearer_token_authentication_header( + bearer_token_auth_token_, false)); + } + } + + if (!proxy_bearer_token_auth_token_.empty()) { + if (!req.has_header("Proxy-Authorization")) { + req.headers.insert(make_bearer_token_authentication_header( + proxy_bearer_token_auth_token_, true)); + } + } + + // Request line and headers + { + detail::BufferStream bstrm; + + // Extract path and query from req.path + std::string path_part, query_part; + auto query_pos = req.path.find('?'); + if (query_pos != std::string::npos) { + path_part = req.path.substr(0, query_pos); + query_part = req.path.substr(query_pos + 1); + } else { + path_part = req.path; + query_part = ""; + } + + // Encode path and query + auto path_with_query = + path_encode_ ? detail::encode_path(path_part) : path_part; + + detail::parse_query_text(query_part, req.params); + if (!req.params.empty()) { + path_with_query = append_query_params(path_with_query, req.params); + } + + // Write request line and headers + detail::write_request_line(bstrm, req.method, path_with_query); + header_writer_(bstrm, req.headers); + + // Flush buffer + auto &data = bstrm.get_buffer(); + if (!detail::write_data(strm, data.data(), data.size())) { + error = Error::Write; + output_error_log(error, &req); + return false; + } + } + + // Body + if (req.body.empty()) { + return write_content_with_provider(strm, req, error); + } + + if (req.upload_progress) { + auto body_size = req.body.size(); + size_t written = 0; + auto data = req.body.data(); + + while (written < body_size) { + size_t to_write = (std::min)(CPPHTTPLIB_SEND_BUFSIZ, body_size - written); + if (!detail::write_data(strm, data + written, to_write)) { + error = Error::Write; + output_error_log(error, &req); + return false; + } + written += to_write; + + if (!req.upload_progress(written, body_size)) { + error = Error::Canceled; + output_error_log(error, &req); + return false; + } + } + } else { + if (!detail::write_data(strm, req.body.data(), req.body.size())) { + error = Error::Write; + output_error_log(error, &req); + return false; + } + } + + return true; +} + +inline std::unique_ptr ClientImpl::send_with_content_provider( + Request &req, const char *body, size_t content_length, + ContentProvider content_provider, + ContentProviderWithoutLength content_provider_without_length, + const std::string &content_type, Error &error) { + if (!content_type.empty()) { req.set_header("Content-Type", content_type); } + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + if (compress_) { req.set_header("Content-Encoding", "gzip"); } +#endif + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + if (compress_ && !content_provider_without_length) { + // TODO: Brotli support + detail::gzip_compressor compressor; + + if (content_provider) { + auto ok = true; + size_t offset = 0; + DataSink data_sink; + + data_sink.write = [&](const char *data, size_t data_len) -> bool { + if (ok) { + auto last = offset + data_len == content_length; + + auto ret = compressor.compress( + data, data_len, last, + [&](const char *compressed_data, size_t compressed_data_len) { + req.body.append(compressed_data, compressed_data_len); + return true; + }); + + if (ret) { + offset += data_len; + } else { + ok = false; + } + } + return ok; + }; + + while (ok && offset < content_length) { + if (!content_provider(offset, content_length - offset, data_sink)) { + error = Error::Canceled; + output_error_log(error, &req); + return nullptr; + } + } + } else { + if (!compressor.compress(body, content_length, true, + [&](const char *data, size_t data_len) { + req.body.append(data, data_len); + return true; + })) { + error = Error::Compression; + output_error_log(error, &req); + return nullptr; + } + } + } else +#endif + { + if (content_provider) { + req.content_length_ = content_length; + req.content_provider_ = std::move(content_provider); + req.is_chunked_content_provider_ = false; + } else if (content_provider_without_length) { + req.content_length_ = 0; + req.content_provider_ = detail::ContentProviderAdapter( + std::move(content_provider_without_length)); + req.is_chunked_content_provider_ = true; + req.set_header("Transfer-Encoding", "chunked"); + } else { + req.body.assign(body, content_length); + } + } + + auto res = detail::make_unique(); + return send(req, *res, error) ? std::move(res) : nullptr; +} + +inline Result ClientImpl::send_with_content_provider( + const std::string &method, const std::string &path, const Headers &headers, + const char *body, size_t content_length, ContentProvider content_provider, + ContentProviderWithoutLength content_provider_without_length, + const std::string &content_type, UploadProgress progress) { + Request req; + req.method = method; + req.headers = headers; + req.path = path; + req.upload_progress = std::move(progress); + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + auto error = Error::Success; + + auto res = send_with_content_provider( + req, body, content_length, std::move(content_provider), + std::move(content_provider_without_length), content_type, error); + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + return Result{std::move(res), error, std::move(req.headers), last_ssl_error_, + last_openssl_error_}; +#else + return Result{std::move(res), error, std::move(req.headers)}; +#endif +} + +inline void ClientImpl::output_log(const Request &req, + const Response &res) const { + if (logger_) { + std::lock_guard guard(logger_mutex_); + logger_(req, res); + } +} + +inline void ClientImpl::output_error_log(const Error &err, + const Request *req) const { + if (error_logger_) { + std::lock_guard guard(logger_mutex_); + error_logger_(err, req); + } +} + +inline bool ClientImpl::process_request(Stream &strm, Request &req, + Response &res, bool close_connection, + Error &error) { + // Send request + if (!write_request(strm, req, close_connection, error)) { return false; } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (is_ssl()) { + auto is_proxy_enabled = !proxy_host_.empty() && proxy_port_ != -1; + if (!is_proxy_enabled) { + if (detail::is_ssl_peer_could_be_closed(socket_.ssl, socket_.sock)) { + error = Error::SSLPeerCouldBeClosed_; + output_error_log(error, &req); + return false; + } + } + } +#endif + + // Receive response and headers + if (!read_response_line(strm, req, res) || + !detail::read_headers(strm, res.headers)) { + error = Error::Read; + output_error_log(error, &req); + return false; + } + + // Body + if ((res.status != StatusCode::NoContent_204) && req.method != "HEAD" && + req.method != "CONNECT") { + auto redirect = 300 < res.status && res.status < 400 && + res.status != StatusCode::NotModified_304 && + follow_location_; + + if (req.response_handler && !redirect) { + if (!req.response_handler(res)) { + error = Error::Canceled; + output_error_log(error, &req); + return false; + } + } + + auto out = + req.content_receiver + ? static_cast( + [&](const char *buf, size_t n, size_t off, size_t len) { + if (redirect) { return true; } + auto ret = req.content_receiver(buf, n, off, len); + if (!ret) { + error = Error::Canceled; + output_error_log(error, &req); + } + return ret; + }) + : static_cast( + [&](const char *buf, size_t n, size_t /*off*/, + size_t /*len*/) { + assert(res.body.size() + n <= res.body.max_size()); + res.body.append(buf, n); + return true; + }); + + auto progress = [&](size_t current, size_t total) { + if (!req.download_progress || redirect) { return true; } + auto ret = req.download_progress(current, total); + if (!ret) { + error = Error::Canceled; + output_error_log(error, &req); + } + return ret; + }; + + if (res.has_header("Content-Length")) { + if (!req.content_receiver) { + auto len = res.get_header_value_u64("Content-Length"); + if (len > res.body.max_size()) { + error = Error::Read; + output_error_log(error, &req); + return false; + } + res.body.reserve(static_cast(len)); + } + } + + if (res.status != StatusCode::NotModified_304) { + int dummy_status; + if (!detail::read_content(strm, res, (std::numeric_limits::max)(), + dummy_status, std::move(progress), + std::move(out), decompress_)) { + if (error != Error::Canceled) { error = Error::Read; } + output_error_log(error, &req); + return false; + } + } + } + + // Log + output_log(req, res); + + return true; +} + +inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider( + const std::string &boundary, const UploadFormDataItems &items, + const FormDataProviderItems &provider_items) const { + size_t cur_item = 0; + size_t cur_start = 0; + // cur_item and cur_start are copied to within the std::function and + // maintain state between successive calls + return [&, cur_item, cur_start](size_t offset, + DataSink &sink) mutable -> bool { + if (!offset && !items.empty()) { + sink.os << detail::serialize_multipart_formdata(items, boundary, false); + return true; + } else if (cur_item < provider_items.size()) { + if (!cur_start) { + const auto &begin = detail::serialize_multipart_formdata_item_begin( + provider_items[cur_item], boundary); + offset += begin.size(); + cur_start = offset; + sink.os << begin; + } + + DataSink cur_sink; + auto has_data = true; + cur_sink.write = sink.write; + cur_sink.done = [&]() { has_data = false; }; + + if (!provider_items[cur_item].provider(offset - cur_start, cur_sink)) { + return false; + } + + if (!has_data) { + sink.os << detail::serialize_multipart_formdata_item_end(); + cur_item++; + cur_start = 0; + } + return true; + } else { + sink.os << detail::serialize_multipart_formdata_finish(boundary); + sink.done(); + return true; + } + }; +} + +inline bool ClientImpl::process_socket( + const Socket &socket, + std::chrono::time_point start_time, + std::function callback) { + return detail::process_client_socket( + socket.sock, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, max_timeout_msec_, start_time, std::move(callback)); +} + +inline bool ClientImpl::is_ssl() const { return false; } + +inline Result ClientImpl::Get(const std::string &path, + DownloadProgress progress) { + return Get(path, Headers(), std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + DownloadProgress progress) { + if (params.empty()) { return Get(path, headers); } + + std::string path_with_query = append_query_params(path, params); + return Get(path_with_query, headers, std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + DownloadProgress progress) { + Request req; + req.method = "GET"; + req.path = path; + req.headers = headers; + req.download_progress = std::move(progress); + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + return send_(std::move(req)); +} + +inline Result ClientImpl::Get(const std::string &path, + ContentReceiver content_receiver, + DownloadProgress progress) { + return Get(path, Headers(), nullptr, std::move(content_receiver), + std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver, + DownloadProgress progress) { + return Get(path, headers, nullptr, std::move(content_receiver), + std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, + ResponseHandler response_handler, + ContentReceiver content_receiver, + DownloadProgress progress) { + return Get(path, Headers(), std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, + DownloadProgress progress) { + Request req; + req.method = "GET"; + req.path = path; + req.headers = headers; + req.response_handler = std::move(response_handler); + req.content_receiver = + [content_receiver](const char *data, size_t data_length, + size_t /*offset*/, size_t /*total_length*/) { + return content_receiver(data, data_length); + }; + req.download_progress = std::move(progress); + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + return send_(std::move(req)); +} + +inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ContentReceiver content_receiver, + DownloadProgress progress) { + return Get(path, params, headers, nullptr, std::move(content_receiver), + std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, + DownloadProgress progress) { + if (params.empty()) { + return Get(path, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); + } + + std::string path_with_query = append_query_params(path, params); + return Get(path_with_query, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} + +inline Result ClientImpl::Head(const std::string &path) { + return Head(path, Headers()); +} + +inline Result ClientImpl::Head(const std::string &path, + const Headers &headers) { + Request req; + req.method = "HEAD"; + req.headers = headers; + req.path = path; + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + return send_(std::move(req)); +} + +inline Result ClientImpl::Post(const std::string &path) { + return Post(path, std::string(), std::string()); +} + +inline Result ClientImpl::Post(const std::string &path, + const Headers &headers) { + return Post(path, headers, nullptr, 0, std::string()); +} + +inline Result ClientImpl::Post(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return Post(path, Headers(), body, content_length, content_type, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return Post(path, Headers(), body, content_type, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Params ¶ms) { + return Post(path, Headers(), params); +} + +inline Result ClientImpl::Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return Post(path, Headers(), content_length, std::move(content_provider), + content_type, progress); +} + +inline Result ClientImpl::Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return Post(path, Headers(), std::move(content_provider), content_type, + progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const Params ¶ms) { + auto query = detail::params_to_query_str(params); + return Post(path, headers, query, "application/x-www-form-urlencoded"); +} + +inline Result ClientImpl::Post(const std::string &path, + const UploadFormDataItems &items, + UploadProgress progress) { + return Post(path, Headers(), items, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + UploadProgress progress) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); + return Post(path, headers, body, content_type, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const std::string &boundary, + UploadProgress progress) { + if (!detail::is_multipart_boundary_chars_valid(boundary)) { + return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; + } + + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); + return Post(path, headers, body, content_type, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("POST", path, headers, body, content_length, + nullptr, nullptr, content_type, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("POST", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("POST", path, headers, nullptr, + content_length, std::move(content_provider), + nullptr, content_type, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("POST", path, headers, nullptr, 0, nullptr, + std::move(content_provider), content_type, + progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const FormDataProviderItems &provider_items, + UploadProgress progress) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + return send_with_content_provider( + "POST", path, headers, nullptr, 0, nullptr, + get_multipart_content_provider(boundary, items, provider_items), + content_type, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + Request req; + req.method = "POST"; + req.path = path; + req.headers = headers; + req.body = body; + req.content_receiver = + [content_receiver](const char *data, size_t data_length, + size_t /*offset*/, size_t /*total_length*/) { + return content_receiver(data, data_length); + }; + req.download_progress = std::move(progress); + + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + if (!content_type.empty()) { req.set_header("Content-Type", content_type); } + + return send_(std::move(req)); +} + +inline Result ClientImpl::Put(const std::string &path) { + return Put(path, std::string(), std::string()); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers) { + return Put(path, headers, nullptr, 0, std::string()); +} + +inline Result ClientImpl::Put(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return Put(path, Headers(), body, content_length, content_type, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return Put(path, Headers(), body, content_type, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Params ¶ms) { + return Put(path, Headers(), params); +} + +inline Result ClientImpl::Put(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return Put(path, Headers(), content_length, std::move(content_provider), + content_type, progress); +} + +inline Result ClientImpl::Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return Put(path, Headers(), std::move(content_provider), content_type, + progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const Params ¶ms) { + auto query = detail::params_to_query_str(params); + return Put(path, headers, query, "application/x-www-form-urlencoded"); +} + +inline Result ClientImpl::Put(const std::string &path, + const UploadFormDataItems &items, + UploadProgress progress) { + return Put(path, Headers(), items, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + UploadProgress progress) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); + return Put(path, headers, body, content_type, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const std::string &boundary, + UploadProgress progress) { + if (!detail::is_multipart_boundary_chars_valid(boundary)) { + return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; + } + + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); + return Put(path, headers, body, content_type, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("PUT", path, headers, body, content_length, + nullptr, nullptr, content_type, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("PUT", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("PUT", path, headers, nullptr, + content_length, std::move(content_provider), + nullptr, content_type, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("PUT", path, headers, nullptr, 0, nullptr, + std::move(content_provider), content_type, + progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const FormDataProviderItems &provider_items, + UploadProgress progress) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + return send_with_content_provider( + "PUT", path, headers, nullptr, 0, nullptr, + get_multipart_content_provider(boundary, items, provider_items), + content_type, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + Request req; + req.method = "PUT"; + req.path = path; + req.headers = headers; + req.body = body; + req.content_receiver = + [content_receiver](const char *data, size_t data_length, + size_t /*offset*/, size_t /*total_length*/) { + return content_receiver(data, data_length); + }; + req.download_progress = std::move(progress); + + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + if (!content_type.empty()) { req.set_header("Content-Type", content_type); } + + return send_(std::move(req)); +} + +inline Result ClientImpl::Patch(const std::string &path) { + return Patch(path, std::string(), std::string()); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + UploadProgress progress) { + return Patch(path, headers, nullptr, 0, std::string(), progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return Patch(path, Headers(), body, content_length, content_type, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, + const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return Patch(path, Headers(), body, content_type, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Params ¶ms) { + return Patch(path, Headers(), params); +} + +inline Result ClientImpl::Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return Patch(path, Headers(), content_length, std::move(content_provider), + content_type, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return Patch(path, Headers(), std::move(content_provider), content_type, + progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const Params ¶ms) { + auto query = detail::params_to_query_str(params); + return Patch(path, headers, query, "application/x-www-form-urlencoded"); +} + +inline Result ClientImpl::Patch(const std::string &path, + const UploadFormDataItems &items, + UploadProgress progress) { + return Patch(path, Headers(), items, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + UploadProgress progress) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); + return Patch(path, headers, body, content_type, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const std::string &boundary, + UploadProgress progress) { + if (!detail::is_multipart_boundary_chars_valid(boundary)) { + return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; + } + + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); + return Patch(path, headers, body, content_type, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("PATCH", path, headers, body, + content_length, nullptr, nullptr, + content_type, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("PATCH", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("PATCH", path, headers, nullptr, + content_length, std::move(content_provider), + nullptr, content_type, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("PATCH", path, headers, nullptr, 0, nullptr, + std::move(content_provider), content_type, + progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const FormDataProviderItems &provider_items, + UploadProgress progress) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + return send_with_content_provider( + "PATCH", path, headers, nullptr, 0, nullptr, + get_multipart_content_provider(boundary, items, provider_items), + content_type, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + Request req; + req.method = "PATCH"; + req.path = path; + req.headers = headers; + req.body = body; + req.content_receiver = + [content_receiver](const char *data, size_t data_length, + size_t /*offset*/, size_t /*total_length*/) { + return content_receiver(data, data_length); + }; + req.download_progress = std::move(progress); + + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + if (!content_type.empty()) { req.set_header("Content-Type", content_type); } + + return send_(std::move(req)); +} + +inline Result ClientImpl::Delete(const std::string &path, + DownloadProgress progress) { + return Delete(path, Headers(), std::string(), std::string(), progress); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, + DownloadProgress progress) { + return Delete(path, headers, std::string(), std::string(), progress); +} + +inline Result ClientImpl::Delete(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + DownloadProgress progress) { + return Delete(path, Headers(), body, content_length, content_type, progress); +} + +inline Result ClientImpl::Delete(const std::string &path, + const std::string &body, + const std::string &content_type, + DownloadProgress progress) { + return Delete(path, Headers(), body.data(), body.size(), content_type, + progress); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, + const std::string &body, + const std::string &content_type, + DownloadProgress progress) { + return Delete(path, headers, body.data(), body.size(), content_type, + progress); +} + +inline Result ClientImpl::Delete(const std::string &path, const Params ¶ms, + DownloadProgress progress) { + return Delete(path, Headers(), params, progress); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, const Params ¶ms, + DownloadProgress progress) { + auto query = detail::params_to_query_str(params); + return Delete(path, headers, query, "application/x-www-form-urlencoded", + progress); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, const char *body, + size_t content_length, + const std::string &content_type, + DownloadProgress progress) { + Request req; + req.method = "DELETE"; + req.headers = headers; + req.path = path; + req.download_progress = std::move(progress); + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + if (!content_type.empty()) { req.set_header("Content-Type", content_type); } + req.body.assign(body, content_length); + + return send_(std::move(req)); +} + +inline Result ClientImpl::Options(const std::string &path) { + return Options(path, Headers()); +} + +inline Result ClientImpl::Options(const std::string &path, + const Headers &headers) { + Request req; + req.method = "OPTIONS"; + req.headers = headers; + req.path = path; + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + return send_(std::move(req)); +} + +inline void ClientImpl::stop() { + std::lock_guard guard(socket_mutex_); + + // If there is anything ongoing right now, the ONLY thread-safe thing we can + // do is to shutdown_socket, so that threads using this socket suddenly + // discover they can't read/write any more and error out. Everything else + // (closing the socket, shutting ssl down) is unsafe because these actions + // are not thread-safe. + if (socket_requests_in_flight_ > 0) { + shutdown_socket(socket_); + + // Aside from that, we set a flag for the socket to be closed when we're + // done. + socket_should_be_closed_when_request_is_done_ = true; + return; + } + + // Otherwise, still holding the mutex, we can shut everything down ourselves + shutdown_ssl(socket_, true); + shutdown_socket(socket_); + close_socket(socket_); +} + +inline std::string ClientImpl::host() const { return host_; } + +inline int ClientImpl::port() const { return port_; } + +inline size_t ClientImpl::is_socket_open() const { + std::lock_guard guard(socket_mutex_); + return socket_.is_open(); +} + +inline socket_t ClientImpl::socket() const { return socket_.sock; } + +inline void ClientImpl::set_connection_timeout(time_t sec, time_t usec) { + connection_timeout_sec_ = sec; + connection_timeout_usec_ = usec; +} + +inline void ClientImpl::set_read_timeout(time_t sec, time_t usec) { + read_timeout_sec_ = sec; + read_timeout_usec_ = usec; +} + +inline void ClientImpl::set_write_timeout(time_t sec, time_t usec) { + write_timeout_sec_ = sec; + write_timeout_usec_ = usec; +} + +inline void ClientImpl::set_max_timeout(time_t msec) { + max_timeout_msec_ = msec; +} + +inline void ClientImpl::set_basic_auth(const std::string &username, + const std::string &password) { + basic_auth_username_ = username; + basic_auth_password_ = password; +} + +inline void ClientImpl::set_bearer_token_auth(const std::string &token) { + bearer_token_auth_token_ = token; +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void ClientImpl::set_digest_auth(const std::string &username, + const std::string &password) { + digest_auth_username_ = username; + digest_auth_password_ = password; +} +#endif + +inline void ClientImpl::set_keep_alive(bool on) { keep_alive_ = on; } + +inline void ClientImpl::set_follow_location(bool on) { follow_location_ = on; } + +inline void ClientImpl::set_path_encode(bool on) { path_encode_ = on; } + +inline void +ClientImpl::set_hostname_addr_map(std::map addr_map) { + addr_map_ = std::move(addr_map); +} + +inline void ClientImpl::set_default_headers(Headers headers) { + default_headers_ = std::move(headers); +} + +inline void ClientImpl::set_header_writer( + std::function const &writer) { + header_writer_ = writer; +} + +inline void ClientImpl::set_address_family(int family) { + address_family_ = family; +} + +inline void ClientImpl::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; } + +inline void ClientImpl::set_ipv6_v6only(bool on) { ipv6_v6only_ = on; } + +inline void ClientImpl::set_socket_options(SocketOptions socket_options) { + socket_options_ = std::move(socket_options); +} + +inline void ClientImpl::set_compress(bool on) { compress_ = on; } + +inline void ClientImpl::set_decompress(bool on) { decompress_ = on; } + +inline void ClientImpl::set_interface(const std::string &intf) { + interface_ = intf; +} + +inline void ClientImpl::set_proxy(const std::string &host, int port) { + proxy_host_ = host; + proxy_port_ = port; +} + +inline void ClientImpl::set_proxy_basic_auth(const std::string &username, + const std::string &password) { + proxy_basic_auth_username_ = username; + proxy_basic_auth_password_ = password; +} + +inline void ClientImpl::set_proxy_bearer_token_auth(const std::string &token) { + proxy_bearer_token_auth_token_ = token; +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void ClientImpl::set_proxy_digest_auth(const std::string &username, + const std::string &password) { + proxy_digest_auth_username_ = username; + proxy_digest_auth_password_ = password; +} + +inline void ClientImpl::set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path) { + ca_cert_file_path_ = ca_cert_file_path; + ca_cert_dir_path_ = ca_cert_dir_path; +} + +inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) { + if (ca_cert_store && ca_cert_store != ca_cert_store_) { + ca_cert_store_ = ca_cert_store; + } +} + +inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, + std::size_t size) const { + auto mem = BIO_new_mem_buf(ca_cert, static_cast(size)); + auto se = detail::scope_exit([&] { BIO_free_all(mem); }); + if (!mem) { return nullptr; } + + auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr); + if (!inf) { return nullptr; } + + auto cts = X509_STORE_new(); + if (cts) { + for (auto i = 0; i < static_cast(sk_X509_INFO_num(inf)); i++) { + auto itmp = sk_X509_INFO_value(inf, i); + if (!itmp) { continue; } + + if (itmp->x509) { X509_STORE_add_cert(cts, itmp->x509); } + if (itmp->crl) { X509_STORE_add_crl(cts, itmp->crl); } + } + } + + sk_X509_INFO_pop_free(inf, X509_INFO_free); + return cts; +} + +inline void ClientImpl::enable_server_certificate_verification(bool enabled) { + server_certificate_verification_ = enabled; +} + +inline void ClientImpl::enable_server_hostname_verification(bool enabled) { + server_hostname_verification_ = enabled; +} + +inline void ClientImpl::set_server_certificate_verifier( + std::function verifier) { + server_certificate_verifier_ = verifier; +} +#endif + +inline void ClientImpl::set_logger(Logger logger) { + logger_ = std::move(logger); +} + +inline void ClientImpl::set_error_logger(ErrorLogger error_logger) { + error_logger_ = std::move(error_logger); +} + +/* + * SSL Implementation + */ +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +namespace detail { + +inline bool is_ip_address(const std::string &host) { + struct in_addr addr4; + struct in6_addr addr6; + return inet_pton(AF_INET, host.c_str(), &addr4) == 1 || + inet_pton(AF_INET6, host.c_str(), &addr6) == 1; +} + +template +inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex, + U SSL_connect_or_accept, V setup) { + SSL *ssl = nullptr; + { + std::lock_guard guard(ctx_mutex); + ssl = SSL_new(ctx); + } + + if (ssl) { + set_nonblocking(sock, true); + auto bio = BIO_new_socket(static_cast(sock), BIO_NOCLOSE); + BIO_set_nbio(bio, 1); + SSL_set_bio(ssl, bio, bio); + + if (!setup(ssl) || SSL_connect_or_accept(ssl) != 1) { + SSL_shutdown(ssl); + { + std::lock_guard guard(ctx_mutex); + SSL_free(ssl); + } + set_nonblocking(sock, false); + return nullptr; + } + BIO_set_nbio(bio, 0); + set_nonblocking(sock, false); + } + + return ssl; +} + +inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock, + bool shutdown_gracefully) { + // sometimes we may want to skip this to try to avoid SIGPIPE if we know + // the remote has closed the network connection + // Note that it is not always possible to avoid SIGPIPE, this is merely a + // best-efforts. + if (shutdown_gracefully) { + (void)(sock); + // SSL_shutdown() returns 0 on first call (indicating close_notify alert + // sent) and 1 on subsequent call (indicating close_notify alert received) + if (SSL_shutdown(ssl) == 0) { + // Expected to return 1, but even if it doesn't, we free ssl + SSL_shutdown(ssl); + } + } + + std::lock_guard guard(ctx_mutex); + SSL_free(ssl); +} + +template +bool ssl_connect_or_accept_nonblocking(socket_t sock, SSL *ssl, + U ssl_connect_or_accept, + time_t timeout_sec, time_t timeout_usec, + int *ssl_error) { + auto res = 0; + while ((res = ssl_connect_or_accept(ssl)) != 1) { + auto err = SSL_get_error(ssl, res); + switch (err) { + case SSL_ERROR_WANT_READ: + if (select_read(sock, timeout_sec, timeout_usec) > 0) { continue; } + break; + case SSL_ERROR_WANT_WRITE: + if (select_write(sock, timeout_sec, timeout_usec) > 0) { continue; } + break; + default: break; + } + if (ssl_error) { *ssl_error = err; } + return false; + } + return true; +} + +template +inline bool process_server_socket_ssl( + const std::atomic &svr_sock, SSL *ssl, socket_t sock, + size_t keep_alive_max_count, time_t keep_alive_timeout_sec, + time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { + return process_server_socket_core( + svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, + [&](bool close_connection, bool &connection_closed) { + SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm, close_connection, connection_closed); + }); +} + +template +inline bool process_client_socket_ssl( + SSL *ssl, socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t max_timeout_msec, + std::chrono::time_point start_time, T callback) { + SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec, max_timeout_msec, + start_time); + return callback(strm); +} + +// SSL socket stream implementation +inline SSLSocketStream::SSLSocketStream( + socket_t sock, SSL *ssl, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t max_timeout_msec, + std::chrono::time_point start_time) + : sock_(sock), ssl_(ssl), read_timeout_sec_(read_timeout_sec), + read_timeout_usec_(read_timeout_usec), + write_timeout_sec_(write_timeout_sec), + write_timeout_usec_(write_timeout_usec), + max_timeout_msec_(max_timeout_msec), start_time_(start_time) { + SSL_clear_mode(ssl, SSL_MODE_AUTO_RETRY); +} + +inline SSLSocketStream::~SSLSocketStream() = default; + +inline bool SSLSocketStream::is_readable() const { + return SSL_pending(ssl_) > 0; +} + +inline bool SSLSocketStream::wait_readable() const { + if (max_timeout_msec_ <= 0) { + return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; + } + + time_t read_timeout_sec; + time_t read_timeout_usec; + calc_actual_timeout(max_timeout_msec_, duration(), read_timeout_sec_, + read_timeout_usec_, read_timeout_sec, read_timeout_usec); + + return select_read(sock_, read_timeout_sec, read_timeout_usec) > 0; +} + +inline bool SSLSocketStream::wait_writable() const { + return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && + is_socket_alive(sock_) && !is_ssl_peer_could_be_closed(ssl_, sock_); +} + +inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { + if (SSL_pending(ssl_) > 0) { + return SSL_read(ssl_, ptr, static_cast(size)); + } else if (wait_readable()) { + auto ret = SSL_read(ssl_, ptr, static_cast(size)); + if (ret < 0) { + auto err = SSL_get_error(ssl_, ret); + auto n = 1000; +#ifdef _WIN32 + while (--n >= 0 && (err == SSL_ERROR_WANT_READ || + (err == SSL_ERROR_SYSCALL && + WSAGetLastError() == WSAETIMEDOUT))) { +#else + while (--n >= 0 && err == SSL_ERROR_WANT_READ) { +#endif + if (SSL_pending(ssl_) > 0) { + return SSL_read(ssl_, ptr, static_cast(size)); + } else if (wait_readable()) { + std::this_thread::sleep_for(std::chrono::microseconds{10}); + ret = SSL_read(ssl_, ptr, static_cast(size)); + if (ret >= 0) { return ret; } + err = SSL_get_error(ssl_, ret); + } else { + break; + } + } + assert(ret < 0); + } + return ret; + } else { + return -1; + } +} + +inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { + if (wait_writable()) { + auto handle_size = static_cast( + std::min(size, (std::numeric_limits::max)())); + + auto ret = SSL_write(ssl_, ptr, static_cast(handle_size)); + if (ret < 0) { + auto err = SSL_get_error(ssl_, ret); + auto n = 1000; +#ifdef _WIN32 + while (--n >= 0 && (err == SSL_ERROR_WANT_WRITE || + (err == SSL_ERROR_SYSCALL && + WSAGetLastError() == WSAETIMEDOUT))) { +#else + while (--n >= 0 && err == SSL_ERROR_WANT_WRITE) { +#endif + if (wait_writable()) { + std::this_thread::sleep_for(std::chrono::microseconds{10}); + ret = SSL_write(ssl_, ptr, static_cast(handle_size)); + if (ret >= 0) { return ret; } + err = SSL_get_error(ssl_, ret); + } else { + break; + } + } + assert(ret < 0); + } + return ret; + } + return -1; +} + +inline void SSLSocketStream::get_remote_ip_and_port(std::string &ip, + int &port) const { + detail::get_remote_ip_and_port(sock_, ip, port); +} + +inline void SSLSocketStream::get_local_ip_and_port(std::string &ip, + int &port) const { + detail::get_local_ip_and_port(sock_, ip, port); +} + +inline socket_t SSLSocketStream::socket() const { return sock_; } + +inline time_t SSLSocketStream::duration() const { + return std::chrono::duration_cast( + std::chrono::steady_clock::now() - start_time_) + .count(); +} + +} // namespace detail + +// SSL HTTP server implementation +inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, + const char *client_ca_cert_file_path, + const char *client_ca_cert_dir_path, + const char *private_key_password) { + ctx_ = SSL_CTX_new(TLS_server_method()); + + if (ctx_) { + SSL_CTX_set_options(ctx_, + SSL_OP_NO_COMPRESSION | + SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); + + SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); + + if (private_key_password != nullptr && (private_key_password[0] != '\0')) { + SSL_CTX_set_default_passwd_cb_userdata( + ctx_, + reinterpret_cast(const_cast(private_key_password))); + } + + if (SSL_CTX_use_certificate_chain_file(ctx_, cert_path) != 1 || + SSL_CTX_use_PrivateKey_file(ctx_, private_key_path, SSL_FILETYPE_PEM) != + 1 || + SSL_CTX_check_private_key(ctx_) != 1) { + last_ssl_error_ = static_cast(ERR_get_error()); + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } else if (client_ca_cert_file_path || client_ca_cert_dir_path) { + SSL_CTX_load_verify_locations(ctx_, client_ca_cert_file_path, + client_ca_cert_dir_path); + + // Set client CA list to be sent to clients during TLS handshake + if (client_ca_cert_file_path) { + auto ca_list = SSL_load_client_CA_file(client_ca_cert_file_path); + if (ca_list != nullptr) { + SSL_CTX_set_client_CA_list(ctx_, ca_list); + } else { + // Failed to load client CA list, but we continue since + // SSL_CTX_load_verify_locations already succeeded and + // certificate verification will still work + last_ssl_error_ = static_cast(ERR_get_error()); + } + } + + SSL_CTX_set_verify( + ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); + } + } +} + +inline SSLServer::SSLServer(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store) { + ctx_ = SSL_CTX_new(TLS_server_method()); + + if (ctx_) { + SSL_CTX_set_options(ctx_, + SSL_OP_NO_COMPRESSION | + SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); + + SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); + + if (SSL_CTX_use_certificate(ctx_, cert) != 1 || + SSL_CTX_use_PrivateKey(ctx_, private_key) != 1) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } else if (client_ca_cert_store) { + SSL_CTX_set_cert_store(ctx_, client_ca_cert_store); + + // Extract CA names from the store and set them as the client CA list + auto ca_list = extract_ca_names_from_x509_store(client_ca_cert_store); + if (ca_list) { + SSL_CTX_set_client_CA_list(ctx_, ca_list); + } else { + // Failed to extract CA names, record the error + last_ssl_error_ = static_cast(ERR_get_error()); + } + + SSL_CTX_set_verify( + ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); + } + } +} + +inline SSLServer::SSLServer( + const std::function &setup_ssl_ctx_callback) { + ctx_ = SSL_CTX_new(TLS_method()); + if (ctx_) { + if (!setup_ssl_ctx_callback(*ctx_)) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } + } +} + +inline SSLServer::~SSLServer() { + if (ctx_) { SSL_CTX_free(ctx_); } +} + +inline bool SSLServer::is_valid() const { return ctx_; } + +inline SSL_CTX *SSLServer::ssl_context() const { return ctx_; } + +inline void SSLServer::update_certs(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store) { + + std::lock_guard guard(ctx_mutex_); + + SSL_CTX_use_certificate(ctx_, cert); + SSL_CTX_use_PrivateKey(ctx_, private_key); + + if (client_ca_cert_store != nullptr) { + SSL_CTX_set_cert_store(ctx_, client_ca_cert_store); + } +} + +inline bool SSLServer::process_and_close_socket(socket_t sock) { + auto ssl = detail::ssl_new( + sock, ctx_, ctx_mutex_, + [&](SSL *ssl2) { + return detail::ssl_connect_or_accept_nonblocking( + sock, ssl2, SSL_accept, read_timeout_sec_, read_timeout_usec_, + &last_ssl_error_); + }, + [](SSL * /*ssl2*/) { return true; }); + + auto ret = false; + if (ssl) { + std::string remote_addr; + int remote_port = 0; + detail::get_remote_ip_and_port(sock, remote_addr, remote_port); + + std::string local_addr; + int local_port = 0; + detail::get_local_ip_and_port(sock, local_addr, local_port); + + ret = detail::process_server_socket_ssl( + svr_sock_, ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_, + read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, + [&](Stream &strm, bool close_connection, bool &connection_closed) { + return process_request(strm, remote_addr, remote_port, local_addr, + local_port, close_connection, + connection_closed, + [&](Request &req) { req.ssl = ssl; }); + }); + + // Shutdown gracefully if the result seemed successful, non-gracefully if + // the connection appeared to be closed. + const bool shutdown_gracefully = ret; + detail::ssl_delete(ctx_mutex_, ssl, sock, shutdown_gracefully); + } + + detail::shutdown_socket(sock); + detail::close_socket(sock); + return ret; +} + +inline STACK_OF(X509_NAME) * SSLServer::extract_ca_names_from_x509_store( + X509_STORE *store) { + if (!store) { return nullptr; } + + auto ca_list = sk_X509_NAME_new_null(); + if (!ca_list) { return nullptr; } + + // Get all objects from the store + auto objs = X509_STORE_get0_objects(store); + if (!objs) { + sk_X509_NAME_free(ca_list); + return nullptr; + } + + // Iterate through objects and extract certificate subject names + for (int i = 0; i < sk_X509_OBJECT_num(objs); i++) { + auto obj = sk_X509_OBJECT_value(objs, i); + if (X509_OBJECT_get_type(obj) == X509_LU_X509) { + auto cert = X509_OBJECT_get0_X509(obj); + if (cert) { + auto subject = X509_get_subject_name(cert); + if (subject) { + auto name_dup = X509_NAME_dup(subject); + if (name_dup) { sk_X509_NAME_push(ca_list, name_dup); } + } + } + } + } + + // If no names were extracted, free the list and return nullptr + if (sk_X509_NAME_num(ca_list) == 0) { + sk_X509_NAME_free(ca_list); + return nullptr; + } + + return ca_list; +} + +// SSL HTTP client implementation +inline SSLClient::SSLClient(const std::string &host) + : SSLClient(host, 443, std::string(), std::string()) {} + +inline SSLClient::SSLClient(const std::string &host, int port) + : SSLClient(host, port, std::string(), std::string()) {} + +inline SSLClient::SSLClient(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path, + const std::string &private_key_password) + : ClientImpl(host, port, client_cert_path, client_key_path) { + ctx_ = SSL_CTX_new(TLS_client_method()); + + SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); + + detail::split(&host_[0], &host_[host_.size()], '.', + [&](const char *b, const char *e) { + host_components_.emplace_back(b, e); + }); + + if (!client_cert_path.empty() && !client_key_path.empty()) { + if (!private_key_password.empty()) { + SSL_CTX_set_default_passwd_cb_userdata( + ctx_, reinterpret_cast( + const_cast(private_key_password.c_str()))); + } + + if (SSL_CTX_use_certificate_file(ctx_, client_cert_path.c_str(), + SSL_FILETYPE_PEM) != 1 || + SSL_CTX_use_PrivateKey_file(ctx_, client_key_path.c_str(), + SSL_FILETYPE_PEM) != 1) { + last_openssl_error_ = ERR_get_error(); + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } + } +} + +inline SSLClient::SSLClient(const std::string &host, int port, + X509 *client_cert, EVP_PKEY *client_key, + const std::string &private_key_password) + : ClientImpl(host, port) { + ctx_ = SSL_CTX_new(TLS_client_method()); + + detail::split(&host_[0], &host_[host_.size()], '.', + [&](const char *b, const char *e) { + host_components_.emplace_back(b, e); + }); + + if (client_cert != nullptr && client_key != nullptr) { + if (!private_key_password.empty()) { + SSL_CTX_set_default_passwd_cb_userdata( + ctx_, reinterpret_cast( + const_cast(private_key_password.c_str()))); + } + + if (SSL_CTX_use_certificate(ctx_, client_cert) != 1 || + SSL_CTX_use_PrivateKey(ctx_, client_key) != 1) { + last_openssl_error_ = ERR_get_error(); + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } + } +} + +inline SSLClient::~SSLClient() { + if (ctx_) { SSL_CTX_free(ctx_); } + // Make sure to shut down SSL since shutdown_ssl will resolve to the + // base function rather than the derived function once we get to the + // base class destructor, and won't free the SSL (causing a leak). + shutdown_ssl_impl(socket_, true); +} + +inline bool SSLClient::is_valid() const { return ctx_; } + +inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) { + if (ca_cert_store) { + if (ctx_) { + if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) { + // Free memory allocated for old cert and use new store + // `ca_cert_store` + SSL_CTX_set_cert_store(ctx_, ca_cert_store); + ca_cert_store_ = ca_cert_store; + } + } else { + X509_STORE_free(ca_cert_store); + } + } +} + +inline void SSLClient::load_ca_cert_store(const char *ca_cert, + std::size_t size) { + set_ca_cert_store(ClientImpl::create_ca_cert_store(ca_cert, size)); +} + +inline long SSLClient::get_openssl_verify_result() const { + return verify_result_; +} + +inline SSL_CTX *SSLClient::ssl_context() const { return ctx_; } + +inline bool SSLClient::create_and_connect_socket(Socket &socket, Error &error) { + if (!is_valid()) { + error = Error::SSLConnection; + return false; + } + return ClientImpl::create_and_connect_socket(socket, error); +} + +// Assumes that socket_mutex_ is locked and that there are no requests in +// flight +inline bool SSLClient::connect_with_proxy( + Socket &socket, + std::chrono::time_point start_time, + Response &res, bool &success, Error &error) { + success = true; + Response proxy_res; + if (!detail::process_client_socket( + socket.sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, max_timeout_msec_, + start_time, [&](Stream &strm) { + Request req2; + req2.method = "CONNECT"; + req2.path = host_and_port_; + if (max_timeout_msec_ > 0) { + req2.start_time_ = std::chrono::steady_clock::now(); + } + return process_request(strm, req2, proxy_res, false, error); + })) { + // Thread-safe to close everything because we are assuming there are no + // requests in flight + shutdown_ssl(socket, true); + shutdown_socket(socket); + close_socket(socket); + success = false; + return false; + } + + if (proxy_res.status == StatusCode::ProxyAuthenticationRequired_407) { + if (!proxy_digest_auth_username_.empty() && + !proxy_digest_auth_password_.empty()) { + std::map auth; + if (detail::parse_www_authenticate(proxy_res, auth, true)) { + // Close the current socket and create a new one for the authenticated + // request + shutdown_ssl(socket, true); + shutdown_socket(socket); + close_socket(socket); + + // Create a new socket for the authenticated CONNECT request + if (!create_and_connect_socket(socket, error)) { + success = false; + output_error_log(error, nullptr); + return false; + } + + proxy_res = Response(); + if (!detail::process_client_socket( + socket.sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, max_timeout_msec_, + start_time, [&](Stream &strm) { + Request req3; + req3.method = "CONNECT"; + req3.path = host_and_port_; + req3.headers.insert(detail::make_digest_authentication_header( + req3, auth, 1, detail::random_string(10), + proxy_digest_auth_username_, proxy_digest_auth_password_, + true)); + if (max_timeout_msec_ > 0) { + req3.start_time_ = std::chrono::steady_clock::now(); + } + return process_request(strm, req3, proxy_res, false, error); + })) { + // Thread-safe to close everything because we are assuming there are + // no requests in flight + shutdown_ssl(socket, true); + shutdown_socket(socket); + close_socket(socket); + success = false; + return false; + } + } + } + } + + // If status code is not 200, proxy request is failed. + // Set error to ProxyConnection and return proxy response + // as the response of the request + if (proxy_res.status != StatusCode::OK_200) { + error = Error::ProxyConnection; + output_error_log(error, nullptr); + res = std::move(proxy_res); + // Thread-safe to close everything because we are assuming there are + // no requests in flight + shutdown_ssl(socket, true); + shutdown_socket(socket); + close_socket(socket); + return false; + } + + return true; +} + +inline bool SSLClient::load_certs() { + auto ret = true; + + std::call_once(initialize_cert_, [&]() { + std::lock_guard guard(ctx_mutex_); + if (!ca_cert_file_path_.empty()) { + if (!SSL_CTX_load_verify_locations(ctx_, ca_cert_file_path_.c_str(), + nullptr)) { + last_openssl_error_ = ERR_get_error(); + ret = false; + } + } else if (!ca_cert_dir_path_.empty()) { + if (!SSL_CTX_load_verify_locations(ctx_, nullptr, + ca_cert_dir_path_.c_str())) { + last_openssl_error_ = ERR_get_error(); + ret = false; + } + } else { + auto loaded = false; +#ifdef _WIN32 + loaded = + detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && TARGET_OS_MAC + loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_)); +#endif // _WIN32 + if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); } + } + }); + + return ret; +} + +inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { + auto ssl = detail::ssl_new( + socket.sock, ctx_, ctx_mutex_, + [&](SSL *ssl2) { + if (server_certificate_verification_) { + if (!load_certs()) { + error = Error::SSLLoadingCerts; + output_error_log(error, nullptr); + return false; + } + SSL_set_verify(ssl2, SSL_VERIFY_NONE, nullptr); + } + + if (!detail::ssl_connect_or_accept_nonblocking( + socket.sock, ssl2, SSL_connect, connection_timeout_sec_, + connection_timeout_usec_, &last_ssl_error_)) { + error = Error::SSLConnection; + output_error_log(error, nullptr); + return false; + } + + if (server_certificate_verification_) { + auto verification_status = SSLVerifierResponse::NoDecisionMade; + + if (server_certificate_verifier_) { + verification_status = server_certificate_verifier_(ssl2); + } + + if (verification_status == SSLVerifierResponse::CertificateRejected) { + last_openssl_error_ = ERR_get_error(); + error = Error::SSLServerVerification; + output_error_log(error, nullptr); + return false; + } + + if (verification_status == SSLVerifierResponse::NoDecisionMade) { + verify_result_ = SSL_get_verify_result(ssl2); + + if (verify_result_ != X509_V_OK) { + last_openssl_error_ = static_cast(verify_result_); + error = Error::SSLServerVerification; + output_error_log(error, nullptr); + return false; + } + + auto server_cert = SSL_get1_peer_certificate(ssl2); + auto se = detail::scope_exit([&] { X509_free(server_cert); }); + + if (server_cert == nullptr) { + last_openssl_error_ = ERR_get_error(); + error = Error::SSLServerVerification; + output_error_log(error, nullptr); + return false; + } + + if (server_hostname_verification_) { + if (!verify_host(server_cert)) { + last_openssl_error_ = X509_V_ERR_HOSTNAME_MISMATCH; + error = Error::SSLServerHostnameVerification; + output_error_log(error, nullptr); + return false; + } + } + } + } + + return true; + }, + [&](SSL *ssl2) { + // Set SNI only if host is not IP address + if (!detail::is_ip_address(host_)) { +#if defined(OPENSSL_IS_BORINGSSL) + SSL_set_tlsext_host_name(ssl2, host_.c_str()); +#else + // NOTE: Direct call instead of using the OpenSSL macro to suppress + // -Wold-style-cast warning + SSL_ctrl(ssl2, SSL_CTRL_SET_TLSEXT_HOSTNAME, + TLSEXT_NAMETYPE_host_name, + static_cast(const_cast(host_.c_str()))); +#endif + } + return true; + }); + + if (ssl) { + socket.ssl = ssl; + return true; + } + + if (ctx_ == nullptr) { + error = Error::SSLConnection; + last_openssl_error_ = ERR_get_error(); + } + + shutdown_socket(socket); + close_socket(socket); + return false; +} + +inline void SSLClient::shutdown_ssl(Socket &socket, bool shutdown_gracefully) { + shutdown_ssl_impl(socket, shutdown_gracefully); +} + +inline void SSLClient::shutdown_ssl_impl(Socket &socket, + bool shutdown_gracefully) { + if (socket.sock == INVALID_SOCKET) { + assert(socket.ssl == nullptr); + return; + } + if (socket.ssl) { + detail::ssl_delete(ctx_mutex_, socket.ssl, socket.sock, + shutdown_gracefully); + socket.ssl = nullptr; + } + assert(socket.ssl == nullptr); +} + +inline bool SSLClient::process_socket( + const Socket &socket, + std::chrono::time_point start_time, + std::function callback) { + assert(socket.ssl); + return detail::process_client_socket_ssl( + socket.ssl, socket.sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, max_timeout_msec_, start_time, + std::move(callback)); +} + +inline bool SSLClient::is_ssl() const { return true; } + +inline bool SSLClient::verify_host(X509 *server_cert) const { + /* Quote from RFC2818 section 3.1 "Server Identity" + + If a subjectAltName extension of type dNSName is present, that MUST + be used as the identity. Otherwise, the (most specific) Common Name + field in the Subject field of the certificate MUST be used. Although + the use of the Common Name is existing practice, it is deprecated and + Certification Authorities are encouraged to use the dNSName instead. + + Matching is performed using the matching rules specified by + [RFC2459]. If more than one identity of a given type is present in + the certificate (e.g., more than one dNSName name, a match in any one + of the set is considered acceptable.) Names may contain the wildcard + character * which is considered to match any single domain name + component or component fragment. E.g., *.a.com matches foo.a.com but + not bar.foo.a.com. f*.com matches foo.com but not bar.com. + + In some cases, the URI is specified as an IP address rather than a + hostname. In this case, the iPAddress subjectAltName must be present + in the certificate and must exactly match the IP in the URI. + + */ + return verify_host_with_subject_alt_name(server_cert) || + verify_host_with_common_name(server_cert); +} + +inline bool +SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { + auto ret = false; + + auto type = GEN_DNS; + + struct in6_addr addr6 = {}; + struct in_addr addr = {}; + size_t addr_len = 0; + +#ifndef __MINGW32__ + if (inet_pton(AF_INET6, host_.c_str(), &addr6)) { + type = GEN_IPADD; + addr_len = sizeof(struct in6_addr); + } else if (inet_pton(AF_INET, host_.c_str(), &addr)) { + type = GEN_IPADD; + addr_len = sizeof(struct in_addr); + } +#endif + + auto alt_names = static_cast( + X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr)); + + if (alt_names) { + auto dsn_matched = false; + auto ip_matched = false; + + auto count = sk_GENERAL_NAME_num(alt_names); + + for (decltype(count) i = 0; i < count && !dsn_matched; i++) { + auto val = sk_GENERAL_NAME_value(alt_names, i); + if (!val || val->type != type) { continue; } + + auto name = + reinterpret_cast(ASN1_STRING_get0_data(val->d.ia5)); + if (name == nullptr) { continue; } + + auto name_len = static_cast(ASN1_STRING_length(val->d.ia5)); + + switch (type) { + case GEN_DNS: dsn_matched = check_host_name(name, name_len); break; + + case GEN_IPADD: + if (!memcmp(&addr6, name, addr_len) || !memcmp(&addr, name, addr_len)) { + ip_matched = true; + } + break; + } + } + + if (dsn_matched || ip_matched) { ret = true; } + } + + GENERAL_NAMES_free(const_cast( + reinterpret_cast(alt_names))); + return ret; +} + +inline bool SSLClient::verify_host_with_common_name(X509 *server_cert) const { + const auto subject_name = X509_get_subject_name(server_cert); + + if (subject_name != nullptr) { + char name[BUFSIZ]; + auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName, + name, sizeof(name)); + + if (name_len != -1) { + return check_host_name(name, static_cast(name_len)); + } + } + + return false; +} + +inline bool SSLClient::check_host_name(const char *pattern, + size_t pattern_len) const { + if (host_.size() == pattern_len && host_ == pattern) { return true; } + + // Wildcard match + // https://bugs.launchpad.net/ubuntu/+source/firefox-3.0/+bug/376484 + std::vector pattern_components; + detail::split(&pattern[0], &pattern[pattern_len], '.', + [&](const char *b, const char *e) { + pattern_components.emplace_back(b, e); + }); + + if (host_components_.size() != pattern_components.size()) { return false; } + + auto itr = pattern_components.begin(); + for (const auto &h : host_components_) { + auto &p = *itr; + if (p != h && p != "*") { + auto partial_match = (p.size() > 0 && p[p.size() - 1] == '*' && + !p.compare(0, p.size() - 1, h)); + if (!partial_match) { return false; } + } + ++itr; + } + + return true; +} +#endif + +// Universal client implementation +inline Client::Client(const std::string &scheme_host_port) + : Client(scheme_host_port, std::string(), std::string()) {} + +inline Client::Client(const std::string &scheme_host_port, + const std::string &client_cert_path, + const std::string &client_key_path) { + const static std::regex re( + R"((?:([a-z]+):\/\/)?(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)"); + + std::smatch m; + if (std::regex_match(scheme_host_port, m, re)) { + auto scheme = m[1].str(); + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (!scheme.empty() && (scheme != "http" && scheme != "https")) { +#else + if (!scheme.empty() && scheme != "http") { +#endif +#ifndef CPPHTTPLIB_NO_EXCEPTIONS + std::string msg = "'" + scheme + "' scheme is not supported."; + throw std::invalid_argument(msg); +#endif + return; + } + + auto is_ssl = scheme == "https"; + + auto host = m[2].str(); + if (host.empty()) { host = m[3].str(); } + + auto port_str = m[4].str(); + auto port = !port_str.empty() ? std::stoi(port_str) : (is_ssl ? 443 : 80); + + if (is_ssl) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + cli_ = detail::make_unique(host, port, client_cert_path, + client_key_path); + is_ssl_ = is_ssl; +#endif + } else { + cli_ = detail::make_unique(host, port, client_cert_path, + client_key_path); + } + } else { + // NOTE: Update TEST(UniversalClientImplTest, Ipv6LiteralAddress) + // if port param below changes. + cli_ = detail::make_unique(scheme_host_port, 80, + client_cert_path, client_key_path); + } +} // namespace detail + +inline Client::Client(const std::string &host, int port) + : cli_(detail::make_unique(host, port)) {} + +inline Client::Client(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path) + : cli_(detail::make_unique(host, port, client_cert_path, + client_key_path)) {} + +inline Client::~Client() = default; + +inline bool Client::is_valid() const { + return cli_ != nullptr && cli_->is_valid(); +} + +inline Result Client::Get(const std::string &path, DownloadProgress progress) { + return cli_->Get(path, std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + DownloadProgress progress) { + return cli_->Get(path, headers, std::move(progress)); +} +inline Result Client::Get(const std::string &path, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Get(path, std::move(content_receiver), std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Get(path, headers, std::move(content_receiver), + std::move(progress)); +} +inline Result Client::Get(const std::string &path, + ResponseHandler response_handler, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Get(path, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Get(path, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Params ¶ms, + const Headers &headers, DownloadProgress progress) { + return cli_->Get(path, params, headers, std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Get(path, params, headers, std::move(content_receiver), + std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Get(path, params, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} + +inline Result Client::Head(const std::string &path) { return cli_->Head(path); } +inline Result Client::Head(const std::string &path, const Headers &headers) { + return cli_->Head(path, headers); +} + +inline Result Client::Post(const std::string &path) { return cli_->Post(path); } +inline Result Client::Post(const std::string &path, const Headers &headers) { + return cli_->Post(path, headers); +} +inline Result Client::Post(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, body, content_length, content_type, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, headers, body, content_length, content_type, + progress); +} +inline Result Client::Post(const std::string &path, const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, body, content_type, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, headers, body, content_type, progress); +} +inline Result Client::Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, content_length, std::move(content_provider), + content_type, progress); +} +inline Result Client::Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, std::move(content_provider), content_type, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, headers, content_length, std::move(content_provider), + content_type, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, headers, std::move(content_provider), content_type, + progress); +} +inline Result Client::Post(const std::string &path, const Params ¶ms) { + return cli_->Post(path, params); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const Params ¶ms) { + return cli_->Post(path, headers, params); +} +inline Result Client::Post(const std::string &path, + const UploadFormDataItems &items, + UploadProgress progress) { + return cli_->Post(path, items, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + UploadProgress progress) { + return cli_->Post(path, headers, items, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const std::string &boundary, + UploadProgress progress) { + return cli_->Post(path, headers, items, boundary, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const FormDataProviderItems &provider_items, + UploadProgress progress) { + return cli_->Post(path, headers, items, provider_items, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Post(path, headers, body, content_type, content_receiver, + progress); +} + +inline Result Client::Put(const std::string &path) { return cli_->Put(path); } +inline Result Client::Put(const std::string &path, const Headers &headers) { + return cli_->Put(path, headers); +} +inline Result Client::Put(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, body, content_length, content_type, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, headers, body, content_length, content_type, progress); +} +inline Result Client::Put(const std::string &path, const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, body, content_type, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, headers, body, content_type, progress); +} +inline Result Client::Put(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, content_length, std::move(content_provider), + content_type, progress); +} +inline Result Client::Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, std::move(content_provider), content_type, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, headers, content_length, std::move(content_provider), + content_type, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, headers, std::move(content_provider), content_type, + progress); +} +inline Result Client::Put(const std::string &path, const Params ¶ms) { + return cli_->Put(path, params); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const Params ¶ms) { + return cli_->Put(path, headers, params); +} +inline Result Client::Put(const std::string &path, + const UploadFormDataItems &items, + UploadProgress progress) { + return cli_->Put(path, items, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + UploadProgress progress) { + return cli_->Put(path, headers, items, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const std::string &boundary, + UploadProgress progress) { + return cli_->Put(path, headers, items, boundary, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const FormDataProviderItems &provider_items, + UploadProgress progress) { + return cli_->Put(path, headers, items, provider_items, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Put(path, headers, body, content_type, content_receiver, + progress); +} + +inline Result Client::Patch(const std::string &path) { + return cli_->Patch(path); +} +inline Result Client::Patch(const std::string &path, const Headers &headers) { + return cli_->Patch(path, headers); +} +inline Result Client::Patch(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, body, content_length, content_type, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, headers, body, content_length, content_type, + progress); +} +inline Result Client::Patch(const std::string &path, const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, body, content_type, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, headers, body, content_type, progress); +} +inline Result Client::Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, content_length, std::move(content_provider), + content_type, progress); +} +inline Result Client::Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, std::move(content_provider), content_type, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, headers, content_length, std::move(content_provider), + content_type, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, headers, std::move(content_provider), content_type, + progress); +} +inline Result Client::Patch(const std::string &path, const Params ¶ms) { + return cli_->Patch(path, params); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const Params ¶ms) { + return cli_->Patch(path, headers, params); +} +inline Result Client::Patch(const std::string &path, + const UploadFormDataItems &items, + UploadProgress progress) { + return cli_->Patch(path, items, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + UploadProgress progress) { + return cli_->Patch(path, headers, items, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const std::string &boundary, + UploadProgress progress) { + return cli_->Patch(path, headers, items, boundary, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const FormDataProviderItems &provider_items, + UploadProgress progress) { + return cli_->Patch(path, headers, items, provider_items, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Patch(path, headers, body, content_type, content_receiver, + progress); +} + +inline Result Client::Delete(const std::string &path, + DownloadProgress progress) { + return cli_->Delete(path, progress); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + DownloadProgress progress) { + return cli_->Delete(path, headers, progress); +} +inline Result Client::Delete(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + DownloadProgress progress) { + return cli_->Delete(path, body, content_length, content_type, progress); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + DownloadProgress progress) { + return cli_->Delete(path, headers, body, content_length, content_type, + progress); +} +inline Result Client::Delete(const std::string &path, const std::string &body, + const std::string &content_type, + DownloadProgress progress) { + return cli_->Delete(path, body, content_type, progress); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + DownloadProgress progress) { + return cli_->Delete(path, headers, body, content_type, progress); +} +inline Result Client::Delete(const std::string &path, const Params ¶ms, + DownloadProgress progress) { + return cli_->Delete(path, params, progress); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const Params ¶ms, DownloadProgress progress) { + return cli_->Delete(path, headers, params, progress); +} + +inline Result Client::Options(const std::string &path) { + return cli_->Options(path); +} +inline Result Client::Options(const std::string &path, const Headers &headers) { + return cli_->Options(path, headers); +} + +inline bool Client::send(Request &req, Response &res, Error &error) { + return cli_->send(req, res, error); +} + +inline Result Client::send(const Request &req) { return cli_->send(req); } + +inline void Client::stop() { cli_->stop(); } + +inline std::string Client::host() const { return cli_->host(); } + +inline int Client::port() const { return cli_->port(); } + +inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); } + +inline socket_t Client::socket() const { return cli_->socket(); } + +inline void +Client::set_hostname_addr_map(std::map addr_map) { + cli_->set_hostname_addr_map(std::move(addr_map)); +} + +inline void Client::set_default_headers(Headers headers) { + cli_->set_default_headers(std::move(headers)); +} + +inline void Client::set_header_writer( + std::function const &writer) { + cli_->set_header_writer(writer); +} + +inline void Client::set_address_family(int family) { + cli_->set_address_family(family); +} + +inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); } + +inline void Client::set_socket_options(SocketOptions socket_options) { + cli_->set_socket_options(std::move(socket_options)); +} + +inline void Client::set_connection_timeout(time_t sec, time_t usec) { + cli_->set_connection_timeout(sec, usec); +} + +inline void Client::set_read_timeout(time_t sec, time_t usec) { + cli_->set_read_timeout(sec, usec); +} + +inline void Client::set_write_timeout(time_t sec, time_t usec) { + cli_->set_write_timeout(sec, usec); +} + +inline void Client::set_basic_auth(const std::string &username, + const std::string &password) { + cli_->set_basic_auth(username, password); +} +inline void Client::set_bearer_token_auth(const std::string &token) { + cli_->set_bearer_token_auth(token); +} +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_digest_auth(const std::string &username, + const std::string &password) { + cli_->set_digest_auth(username, password); +} +#endif + +inline void Client::set_keep_alive(bool on) { cli_->set_keep_alive(on); } +inline void Client::set_follow_location(bool on) { + cli_->set_follow_location(on); +} + +inline void Client::set_path_encode(bool on) { cli_->set_path_encode(on); } + +[[deprecated("Use set_path_encode instead")]] +inline void Client::set_url_encode(bool on) { + cli_->set_path_encode(on); +} + +inline void Client::set_compress(bool on) { cli_->set_compress(on); } + +inline void Client::set_decompress(bool on) { cli_->set_decompress(on); } + +inline void Client::set_interface(const std::string &intf) { + cli_->set_interface(intf); +} + +inline void Client::set_proxy(const std::string &host, int port) { + cli_->set_proxy(host, port); +} +inline void Client::set_proxy_basic_auth(const std::string &username, + const std::string &password) { + cli_->set_proxy_basic_auth(username, password); +} +inline void Client::set_proxy_bearer_token_auth(const std::string &token) { + cli_->set_proxy_bearer_token_auth(token); +} +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_proxy_digest_auth(const std::string &username, + const std::string &password) { + cli_->set_proxy_digest_auth(username, password); +} +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::enable_server_certificate_verification(bool enabled) { + cli_->enable_server_certificate_verification(enabled); +} + +inline void Client::enable_server_hostname_verification(bool enabled) { + cli_->enable_server_hostname_verification(enabled); +} + +inline void Client::set_server_certificate_verifier( + std::function verifier) { + cli_->set_server_certificate_verifier(verifier); +} +#endif + +inline void Client::set_logger(Logger logger) { + cli_->set_logger(std::move(logger)); +} + +inline void Client::set_error_logger(ErrorLogger error_logger) { + cli_->set_error_logger(std::move(error_logger)); +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path) { + cli_->set_ca_cert_path(ca_cert_file_path, ca_cert_dir_path); +} + +inline void Client::set_ca_cert_store(X509_STORE *ca_cert_store) { + if (is_ssl_) { + static_cast(*cli_).set_ca_cert_store(ca_cert_store); + } else { + cli_->set_ca_cert_store(ca_cert_store); + } +} + +inline void Client::load_ca_cert_store(const char *ca_cert, std::size_t size) { + set_ca_cert_store(cli_->create_ca_cert_store(ca_cert, size)); +} + +inline long Client::get_openssl_verify_result() const { + if (is_ssl_) { + return static_cast(*cli_).get_openssl_verify_result(); + } + return -1; // NOTE: -1 doesn't match any of X509_V_ERR_??? +} + +inline SSL_CTX *Client::ssl_context() const { + if (is_ssl_) { return static_cast(*cli_).ssl_context(); } + return nullptr; +} +#endif + +// ---------------------------------------------------------------------------- + +} // namespace httplib + +#endif // CPPHTTPLIB_HTTPLIB_H diff --git a/tjp/core/http/hirose/httplib.h b/tjp/core/http/hirose/httplib.h new file mode 100644 index 0000000..b9fd62f --- /dev/null +++ b/tjp/core/http/hirose/httplib.h @@ -0,0 +1,3 @@ +// TJP COPYRIGHT HEADER + +#include "httplib-0_27_0.h" diff --git a/tjp/core/http/hirose/write_result.hpp b/tjp/core/http/hirose/write_result.hpp new file mode 100755 index 0000000..a922549 --- /dev/null +++ b/tjp/core/http/hirose/write_result.hpp @@ -0,0 +1,36 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "http.hpp" +#include + +//namespace tjp::core::http::hirose {} + +namespace tjp::core::http::hirose { + +template +auto write_result(F &&f) +{ + return + [f=std::move(f)](const Request& req, Response& res) + { + try + { + auto [body, type] = f(req); + res.set_content(body, type); + } + catch (Exception &e) + { + res.set_content("Error: " + e.what(), "text/plain"); + res.status = 400; + } + catch (...) + { + res.set_content("Error", "text/plain"); + res.status = 400; + } + }; +} + +} diff --git a/tjp/core/http/imp.hpp b/tjp/core/http/imp.hpp new file mode 100755 index 0000000..ec6ae44 --- /dev/null +++ b/tjp/core/http/imp.hpp @@ -0,0 +1,11 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "hirose/Server.hpp" + +namespace tjp::core::http { + +namespace imp = hirose; + +} // namespace diff --git a/tjp/core/http/old/httplib.h b/tjp/core/http/old/httplib.h new file mode 100644 index 0000000..20a6e55 --- /dev/null +++ b/tjp/core/http/old/httplib.h @@ -0,0 +1,8309 @@ +// TJP COPYRIGHT HEADER + +// +// httplib.h +// +// Copyright (c) 2022 Yuji Hirose. All rights reserved. +// MIT License +// + +#ifndef CPPHTTPLIB_HTTPLIB_H +#define CPPHTTPLIB_HTTPLIB_H + +#define CPPHTTPLIB_VERSION "0.11.1" + +/* + * Configuration + */ + +#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND +#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_KEEPALIVE_MAX_COUNT +#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 5 +#endif + +#ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND +#define CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND 300 +#endif + +#ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND +#define CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_READ_TIMEOUT_SECOND +#define CPPHTTPLIB_READ_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_READ_TIMEOUT_USECOND +#define CPPHTTPLIB_READ_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_WRITE_TIMEOUT_SECOND +#define CPPHTTPLIB_WRITE_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_WRITE_TIMEOUT_USECOND +#define CPPHTTPLIB_WRITE_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_IDLE_INTERVAL_SECOND +#define CPPHTTPLIB_IDLE_INTERVAL_SECOND 0 +#endif + +#ifndef CPPHTTPLIB_IDLE_INTERVAL_USECOND +#ifdef _WIN32 +#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 10000 +#else +#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 0 +#endif +#endif + +#ifndef CPPHTTPLIB_REQUEST_URI_MAX_LENGTH +#define CPPHTTPLIB_REQUEST_URI_MAX_LENGTH 8192 +#endif + +#ifndef CPPHTTPLIB_HEADER_MAX_LENGTH +#define CPPHTTPLIB_HEADER_MAX_LENGTH 8192 +#endif + +#ifndef CPPHTTPLIB_REDIRECT_MAX_COUNT +#define CPPHTTPLIB_REDIRECT_MAX_COUNT 20 +#endif + +#ifndef CPPHTTPLIB_PAYLOAD_MAX_LENGTH +#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH ((std::numeric_limits::max)()) +#endif + +#ifndef CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH +#define CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH 8192 +#endif + +#ifndef CPPHTTPLIB_TCP_NODELAY +#define CPPHTTPLIB_TCP_NODELAY false +#endif + +#ifndef CPPHTTPLIB_RECV_BUFSIZ +#define CPPHTTPLIB_RECV_BUFSIZ size_t(4096u) +#endif + +#ifndef CPPHTTPLIB_COMPRESSION_BUFSIZ +#define CPPHTTPLIB_COMPRESSION_BUFSIZ size_t(16384u) +#endif + +#ifndef CPPHTTPLIB_THREAD_POOL_COUNT +#define CPPHTTPLIB_THREAD_POOL_COUNT \ + ((std::max)(8u, std::thread::hardware_concurrency() > 0 \ + ? std::thread::hardware_concurrency() - 1 \ + : 0)) +#endif + +#ifndef CPPHTTPLIB_RECV_FLAGS +#define CPPHTTPLIB_RECV_FLAGS 0 +#endif + +#ifndef CPPHTTPLIB_SEND_FLAGS +#define CPPHTTPLIB_SEND_FLAGS 0 +#endif + +#ifndef CPPHTTPLIB_LISTEN_BACKLOG +#define CPPHTTPLIB_LISTEN_BACKLOG 5 +#endif + +/* + * Headers + */ + +#ifdef _WIN32 +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS +#endif //_CRT_SECURE_NO_WARNINGS + +#ifndef _CRT_NONSTDC_NO_DEPRECATE +#define _CRT_NONSTDC_NO_DEPRECATE +#endif //_CRT_NONSTDC_NO_DEPRECATE + +#if defined(_MSC_VER) +#if _MSC_VER < 1900 +#error Sorry, Visual Studio versions prior to 2015 are not supported +#endif + +#pragma comment(lib, "ws2_32.lib") + +#ifdef _WIN64 +using ssize_t = __int64; +#else +using ssize_t = long; +#endif +#endif // _MSC_VER + +#ifndef S_ISREG +#define S_ISREG(m) (((m)&S_IFREG) == S_IFREG) +#endif // S_ISREG + +#ifndef S_ISDIR +#define S_ISDIR(m) (((m)&S_IFDIR) == S_IFDIR) +#endif // S_ISDIR + +#ifndef NOMINMAX +#define NOMINMAX +#endif // NOMINMAX + +#include +#include +#include + +#ifndef WSA_FLAG_NO_HANDLE_INHERIT +#define WSA_FLAG_NO_HANDLE_INHERIT 0x80 +#endif + +#ifndef strcasecmp +#define strcasecmp _stricmp +#endif // strcasecmp + +using socket_t = SOCKET; +#ifdef CPPHTTPLIB_USE_POLL +#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout) +#endif + +#else // not _WIN32 + +#include +#include +#include +#include +#include +#ifdef __linux__ +#include +#endif +#include +#ifdef CPPHTTPLIB_USE_POLL +#include +#endif +#include +#include +#include +#include +#include +#include + +using socket_t = int; +#ifndef INVALID_SOCKET +#define INVALID_SOCKET (-1) +#endif +#endif //_WIN32 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef _WIN32 +#include + +// these are defined in wincrypt.h and it breaks compilation if BoringSSL is +// used +#undef X509_NAME +#undef X509_CERT_PAIR +#undef X509_EXTENSIONS +#undef PKCS7_SIGNER_INFO + +#ifdef _MSC_VER +#pragma comment(lib, "crypt32.lib") +#pragma comment(lib, "cryptui.lib") +#endif +#endif //_WIN32 + +#include +#include +#include +#include + +#if defined(_WIN32) && defined(OPENSSL_USE_APPLINK) +#include +#endif + +#include +#include + +#if OPENSSL_VERSION_NUMBER < 0x1010100fL +#error Sorry, OpenSSL versions prior to 1.1.1 are not supported +#endif + +#if OPENSSL_VERSION_NUMBER < 0x10100000L +#include +inline const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *asn1) { + return M_ASN1_STRING_data(asn1); +} +#endif +#endif + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +#include +#endif + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT +#include +#include +#endif + +/* + * Declaration + */ +namespace httplib { + +namespace detail { + +/* + * Backport std::make_unique from C++14. + * + * NOTE: This code came up with the following stackoverflow post: + * https://stackoverflow.com/questions/10149840/c-arrays-and-make-unique + * + */ + +template +typename std::enable_if::value, std::unique_ptr>::type +make_unique(Args &&...args) { + return std::unique_ptr(new T(std::forward(args)...)); +} + +template +typename std::enable_if::value, std::unique_ptr>::type +make_unique(std::size_t n) { + typedef typename std::remove_extent::type RT; + return std::unique_ptr(new RT[n]); +} + +struct ci { + bool operator()(const std::string &s1, const std::string &s2) const { + return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), + s2.end(), + [](unsigned char c1, unsigned char c2) { + return ::tolower(c1) < ::tolower(c2); + }); + } +}; + +} // namespace detail + +using Headers = std::multimap; + +using Params = std::multimap; +using Match = std::smatch; + +using Progress = std::function; + +struct Response; +using ResponseHandler = std::function; + +struct MultipartFormData { + std::string name; + std::string content; + std::string filename; + std::string content_type; +}; +using MultipartFormDataItems = std::vector; +using MultipartFormDataMap = std::multimap; + +class DataSink { +public: + DataSink() : os(&sb_), sb_(*this) {} + + DataSink(const DataSink &) = delete; + DataSink &operator=(const DataSink &) = delete; + DataSink(DataSink &&) = delete; + DataSink &operator=(DataSink &&) = delete; + + std::function write; + std::function done; + std::function is_writable; + std::ostream os; + +private: + class data_sink_streambuf : public std::streambuf { + public: + explicit data_sink_streambuf(DataSink &sink) : sink_(sink) {} + + protected: + std::streamsize xsputn(const char *s, std::streamsize n) { + sink_.write(s, static_cast(n)); + return n; + } + + private: + DataSink &sink_; + }; + + data_sink_streambuf sb_; +}; + +using ContentProvider = + std::function; + +using ContentProviderWithoutLength = + std::function; + +using ContentProviderResourceReleaser = std::function; + +using ContentReceiverWithProgress = + std::function; + +using ContentReceiver = + std::function; + +using MultipartContentHeader = + std::function; + +class ContentReader { +public: + using Reader = std::function; + using MultipartReader = std::function; + + ContentReader(Reader reader, MultipartReader multipart_reader) + : reader_(std::move(reader)), + multipart_reader_(std::move(multipart_reader)) {} + + bool operator()(MultipartContentHeader header, + ContentReceiver receiver) const { + return multipart_reader_(std::move(header), std::move(receiver)); + } + + bool operator()(ContentReceiver receiver) const { + return reader_(std::move(receiver)); + } + + Reader reader_; + MultipartReader multipart_reader_; +}; + +using Range = std::pair; +using Ranges = std::vector; + +struct Request { + std::string method; + std::string path; + Headers headers; + std::string body; + + std::string remote_addr; + int remote_port = -1; + + // for server + std::string version; + std::string target; + Params params; + MultipartFormDataMap files; + Ranges ranges; + Match matches; + + // for client + ResponseHandler response_handler; + ContentReceiverWithProgress content_receiver; + Progress progress; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + const SSL *ssl = nullptr; +#endif + + bool has_header(const std::string &key) const; + std::string get_header_value(const std::string &key, size_t id = 0) const; + template + T get_header_value(const std::string &key, size_t id = 0) const; + size_t get_header_value_count(const std::string &key) const; + void set_header(const std::string &key, const std::string &val); + + bool has_param(const std::string &key) const; + std::string get_param_value(const std::string &key, size_t id = 0) const; + size_t get_param_value_count(const std::string &key) const; + + bool is_multipart_form_data() const; + + bool has_file(const std::string &key) const; + MultipartFormData get_file_value(const std::string &key) const; + + // private members... + size_t redirect_count_ = CPPHTTPLIB_REDIRECT_MAX_COUNT; + size_t content_length_ = 0; + ContentProvider content_provider_; + bool is_chunked_content_provider_ = false; + size_t authorization_count_ = 0; +}; + +struct Response { + std::string version; + int status = -1; + std::string reason; + Headers headers; + std::string body; + std::string location; // Redirect location + + bool has_header(const std::string &key) const; + std::string get_header_value(const std::string &key, size_t id = 0) const; + template + T get_header_value(const std::string &key, size_t id = 0) const; + size_t get_header_value_count(const std::string &key) const; + void set_header(const std::string &key, const std::string &val); + + void set_redirect(const std::string &url, int status = 302); + void set_content(const char *s, size_t n, const std::string &content_type); + void set_content(const std::string &s, const std::string &content_type); + + void set_content_provider( + size_t length, const std::string &content_type, ContentProvider provider, + ContentProviderResourceReleaser resource_releaser = nullptr); + + void set_content_provider( + const std::string &content_type, ContentProviderWithoutLength provider, + ContentProviderResourceReleaser resource_releaser = nullptr); + + void set_chunked_content_provider( + const std::string &content_type, ContentProviderWithoutLength provider, + ContentProviderResourceReleaser resource_releaser = nullptr); + + Response() = default; + Response(const Response &) = default; + Response &operator=(const Response &) = default; + Response(Response &&) = default; + Response &operator=(Response &&) = default; + ~Response() { + if (content_provider_resource_releaser_) { + content_provider_resource_releaser_(content_provider_success_); + } + } + + // private members... + size_t content_length_ = 0; + ContentProvider content_provider_; + ContentProviderResourceReleaser content_provider_resource_releaser_; + bool is_chunked_content_provider_ = false; + bool content_provider_success_ = false; +}; + +class Stream { +public: + virtual ~Stream() = default; + + virtual bool is_readable() const = 0; + virtual bool is_writable() const = 0; + + virtual ssize_t read(char *ptr, size_t size) = 0; + virtual ssize_t write(const char *ptr, size_t size) = 0; + virtual void get_remote_ip_and_port(std::string &ip, int &port) const = 0; + virtual socket_t socket() const = 0; + + template + ssize_t write_format(const char *fmt, const Args &...args); + ssize_t write(const char *ptr); + ssize_t write(const std::string &s); +}; + +class TaskQueue { +public: + TaskQueue() = default; + virtual ~TaskQueue() = default; + + virtual void enqueue(std::function fn) = 0; + virtual void shutdown() = 0; + + virtual void on_idle() {} +}; + +class ThreadPool : public TaskQueue { +public: + explicit ThreadPool(size_t n) : shutdown_(false) { + while (n) { + threads_.emplace_back(worker(*this)); + n--; + } + } + + ThreadPool(const ThreadPool &) = delete; + ~ThreadPool() override = default; + + void enqueue(std::function fn) override { + std::unique_lock lock(mutex_); + jobs_.push_back(std::move(fn)); + cond_.notify_one(); + } + + void shutdown() override { + // Stop all worker threads... + { + std::unique_lock lock(mutex_); + shutdown_ = true; + } + + cond_.notify_all(); + + // Join... + for (auto &t : threads_) { + t.join(); + } + } + +private: + struct worker { + explicit worker(ThreadPool &pool) : pool_(pool) {} + + void operator()() { + for (;;) { + std::function fn; + { + std::unique_lock lock(pool_.mutex_); + + pool_.cond_.wait( + lock, [&] { return !pool_.jobs_.empty() || pool_.shutdown_; }); + + if (pool_.shutdown_ && pool_.jobs_.empty()) { break; } + + fn = pool_.jobs_.front(); + pool_.jobs_.pop_front(); + } + + assert(true == static_cast(fn)); + fn(); + } + } + + ThreadPool &pool_; + }; + friend struct worker; + + std::vector threads_; + std::list> jobs_; + + bool shutdown_; + + std::condition_variable cond_; + std::mutex mutex_; +}; + +using Logger = std::function; + +using SocketOptions = std::function; + +void default_socket_options(socket_t sock); + +class Server { +public: + using Handler = std::function; + + using ExceptionHandler = + std::function; + + enum class HandlerResponse { + Handled, + Unhandled, + }; + using HandlerWithResponse = + std::function; + + using HandlerWithContentReader = std::function; + + using Expect100ContinueHandler = + std::function; + + Server(); + + virtual ~Server(); + + virtual bool is_valid() const; + + Server &Get(const std::string &pattern, Handler handler); + Server &Post(const std::string &pattern, Handler handler); + Server &Post(const std::string &pattern, HandlerWithContentReader handler); + Server &Put(const std::string &pattern, Handler handler); + Server &Put(const std::string &pattern, HandlerWithContentReader handler); + Server &Patch(const std::string &pattern, Handler handler); + Server &Patch(const std::string &pattern, HandlerWithContentReader handler); + Server &Delete(const std::string &pattern, Handler handler); + Server &Delete(const std::string &pattern, HandlerWithContentReader handler); + Server &Options(const std::string &pattern, Handler handler); + + bool set_base_dir(const std::string &dir, + const std::string &mount_point = std::string()); + bool set_mount_point(const std::string &mount_point, const std::string &dir, + Headers headers = Headers()); + bool remove_mount_point(const std::string &mount_point); + Server &set_file_extension_and_mimetype_mapping(const std::string &ext, + const std::string &mime); + Server &set_file_request_handler(Handler handler); + + Server &set_error_handler(HandlerWithResponse handler); + Server &set_error_handler(Handler handler); + Server &set_exception_handler(ExceptionHandler handler); + Server &set_pre_routing_handler(HandlerWithResponse handler); + Server &set_post_routing_handler(Handler handler); + + Server &set_expect_100_continue_handler(Expect100ContinueHandler handler); + Server &set_logger(Logger logger); + + Server &set_address_family(int family); + Server &set_tcp_nodelay(bool on); + Server &set_socket_options(SocketOptions socket_options); + + Server &set_default_headers(Headers headers); + + Server &set_keep_alive_max_count(size_t count); + Server &set_keep_alive_timeout(time_t sec); + + Server &set_read_timeout(time_t sec, time_t usec = 0); + template + Server &set_read_timeout(const std::chrono::duration &duration); + + Server &set_write_timeout(time_t sec, time_t usec = 0); + template + Server &set_write_timeout(const std::chrono::duration &duration); + + Server &set_idle_interval(time_t sec, time_t usec = 0); + template + Server &set_idle_interval(const std::chrono::duration &duration); + + Server &set_payload_max_length(size_t length); + + bool bind_to_port(const std::string &host, int port, int socket_flags = 0); + int bind_to_any_port(const std::string &host, int socket_flags = 0); + bool listen_after_bind(); + + bool listen(const std::string &host, int port, int socket_flags = 0); + + bool is_running() const; + void stop(); + + std::function new_task_queue; + +protected: + bool process_request(Stream &strm, bool close_connection, + bool &connection_closed, + const std::function &setup_request); + + std::atomic svr_sock_; + size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; + time_t keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND; + time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND; + time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND; + time_t write_timeout_sec_ = CPPHTTPLIB_WRITE_TIMEOUT_SECOND; + time_t write_timeout_usec_ = CPPHTTPLIB_WRITE_TIMEOUT_USECOND; + time_t idle_interval_sec_ = CPPHTTPLIB_IDLE_INTERVAL_SECOND; + time_t idle_interval_usec_ = CPPHTTPLIB_IDLE_INTERVAL_USECOND; + size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; + +private: + using Handlers = std::vector>; + using HandlersForContentReader = + std::vector>; + + socket_t create_server_socket(const std::string &host, int port, + int socket_flags, + SocketOptions socket_options) const; + int bind_internal(const std::string &host, int port, int socket_flags); + bool listen_internal(); + + bool routing(Request &req, Response &res, Stream &strm); + bool handle_file_request(const Request &req, Response &res, + bool head = false); + bool dispatch_request(Request &req, Response &res, const Handlers &handlers); + bool + dispatch_request_for_content_reader(Request &req, Response &res, + ContentReader content_reader, + const HandlersForContentReader &handlers); + + bool parse_request_line(const char *s, Request &req); + void apply_ranges(const Request &req, Response &res, + std::string &content_type, std::string &boundary); + bool write_response(Stream &strm, bool close_connection, const Request &req, + Response &res); + bool write_response_with_content(Stream &strm, bool close_connection, + const Request &req, Response &res); + bool write_response_core(Stream &strm, bool close_connection, + const Request &req, Response &res, + bool need_apply_ranges); + bool write_content_with_provider(Stream &strm, const Request &req, + Response &res, const std::string &boundary, + const std::string &content_type); + bool read_content(Stream &strm, Request &req, Response &res); + bool + read_content_with_content_receiver(Stream &strm, Request &req, Response &res, + ContentReceiver receiver, + MultipartContentHeader multipart_header, + ContentReceiver multipart_receiver); + bool read_content_core(Stream &strm, Request &req, Response &res, + ContentReceiver receiver, + MultipartContentHeader mulitpart_header, + ContentReceiver multipart_receiver); + + virtual bool process_and_close_socket(socket_t sock); + + struct MountPointEntry { + std::string mount_point; + std::string base_dir; + Headers headers; + }; + std::vector base_dirs_; + + std::atomic is_running_; + std::map file_extension_and_mimetype_map_; + Handler file_request_handler_; + Handlers get_handlers_; + Handlers post_handlers_; + HandlersForContentReader post_handlers_for_content_reader_; + Handlers put_handlers_; + HandlersForContentReader put_handlers_for_content_reader_; + Handlers patch_handlers_; + HandlersForContentReader patch_handlers_for_content_reader_; + Handlers delete_handlers_; + HandlersForContentReader delete_handlers_for_content_reader_; + Handlers options_handlers_; + HandlerWithResponse error_handler_; + ExceptionHandler exception_handler_; + HandlerWithResponse pre_routing_handler_; + Handler post_routing_handler_; + Logger logger_; + Expect100ContinueHandler expect_100_continue_handler_; + + int address_family_ = AF_UNSPEC; + bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; + SocketOptions socket_options_ = default_socket_options; + + Headers default_headers_; +}; + +enum class Error { + Success = 0, + Unknown, + Connection, + BindIPAddress, + Read, + Write, + ExceedRedirectCount, + Canceled, + SSLConnection, + SSLLoadingCerts, + SSLServerVerification, + UnsupportedMultipartBoundaryChars, + Compression, + ConnectionTimeout, +}; + +std::string to_string(const Error error); + +std::ostream &operator<<(std::ostream &os, const Error &obj); + +class Result { +public: + Result(std::unique_ptr &&res, Error err, + Headers &&request_headers = Headers{}) + : res_(std::move(res)), err_(err), + request_headers_(std::move(request_headers)) {} + // Response + operator bool() const { return res_ != nullptr; } + bool operator==(std::nullptr_t) const { return res_ == nullptr; } + bool operator!=(std::nullptr_t) const { return res_ != nullptr; } + const Response &value() const { return *res_; } + Response &value() { return *res_; } + const Response &operator*() const { return *res_; } + Response &operator*() { return *res_; } + const Response *operator->() const { return res_.get(); } + Response *operator->() { return res_.get(); } + + // Error + Error error() const { return err_; } + + // Request Headers + bool has_request_header(const std::string &key) const; + std::string get_request_header_value(const std::string &key, + size_t id = 0) const; + template + T get_request_header_value(const std::string &key, size_t id = 0) const; + size_t get_request_header_value_count(const std::string &key) const; + +private: + std::unique_ptr res_; + Error err_; + Headers request_headers_; +}; + +class ClientImpl { +public: + explicit ClientImpl(const std::string &host); + + explicit ClientImpl(const std::string &host, int port); + + explicit ClientImpl(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path); + + virtual ~ClientImpl(); + + virtual bool is_valid() const; + + Result Get(const std::string &path); + Result Get(const std::string &path, const Headers &headers); + Result Get(const std::string &path, Progress progress); + Result Get(const std::string &path, const Headers &headers, + Progress progress); + Result Get(const std::string &path, ContentReceiver content_receiver); + Result Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver); + Result Get(const std::string &path, ContentReceiver content_receiver, + Progress progress); + Result Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver, Progress progress); + Result Get(const std::string &path, ResponseHandler response_handler, + ContentReceiver content_receiver); + Result Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver); + Result Get(const std::string &path, ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress); + Result Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, ContentReceiver content_receiver, + Progress progress); + + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, ContentReceiver content_receiver, + Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress = nullptr); + + Result Head(const std::string &path); + Result Head(const std::string &path, const Headers &headers); + + Result Post(const std::string &path); + Result Post(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type); + Result Post(const std::string &path, const std::string &body, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type); + Result Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Params ¶ms); + Result Post(const std::string &path, const Headers &headers, + const Params ¶ms); + Result Post(const std::string &path, const MultipartFormDataItems &items); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, const std::string &boundary); + + Result Put(const std::string &path); + Result Put(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type); + Result Put(const std::string &path, const std::string &body, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Put(const std::string &path, size_t content_length, + ContentProvider content_provider, const std::string &content_type); + Result Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Params ¶ms); + Result Put(const std::string &path, const Headers &headers, + const Params ¶ms); + + Result Patch(const std::string &path); + Result Patch(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type); + Result Patch(const std::string &path, const std::string &body, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type); + Result Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + + Result Delete(const std::string &path); + Result Delete(const std::string &path, const Headers &headers); + Result Delete(const std::string &path, const char *body, + size_t content_length, const std::string &content_type); + Result Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type); + Result Delete(const std::string &path, const std::string &body, + const std::string &content_type); + Result Delete(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + + Result Options(const std::string &path); + Result Options(const std::string &path, const Headers &headers); + + bool send(Request &req, Response &res, Error &error); + Result send(const Request &req); + + size_t is_socket_open() const; + + socket_t socket() const; + + void stop(); + + void set_hostname_addr_map(std::map addr_map); + + void set_default_headers(Headers headers); + + void set_address_family(int family); + void set_tcp_nodelay(bool on); + void set_socket_options(SocketOptions socket_options); + + void set_connection_timeout(time_t sec, time_t usec = 0); + template + void + set_connection_timeout(const std::chrono::duration &duration); + + void set_read_timeout(time_t sec, time_t usec = 0); + template + void set_read_timeout(const std::chrono::duration &duration); + + void set_write_timeout(time_t sec, time_t usec = 0); + template + void set_write_timeout(const std::chrono::duration &duration); + + void set_basic_auth(const std::string &username, const std::string &password); + void set_bearer_token_auth(const std::string &token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_digest_auth(const std::string &username, + const std::string &password); +#endif + + void set_keep_alive(bool on); + void set_follow_location(bool on); + + void set_url_encode(bool on); + + void set_compress(bool on); + + void set_decompress(bool on); + + void set_interface(const std::string &intf); + + void set_proxy(const std::string &host, int port); + void set_proxy_basic_auth(const std::string &username, + const std::string &password); + void set_proxy_bearer_token_auth(const std::string &token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_proxy_digest_auth(const std::string &username, + const std::string &password); +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path = std::string()); + void set_ca_cert_store(X509_STORE *ca_cert_store); +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void enable_server_certificate_verification(bool enabled); +#endif + + void set_logger(Logger logger); + +protected: + struct Socket { + socket_t sock = INVALID_SOCKET; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + SSL *ssl = nullptr; +#endif + + bool is_open() const { return sock != INVALID_SOCKET; } + }; + + Result send_(Request &&req); + + virtual bool create_and_connect_socket(Socket &socket, Error &error); + + // All of: + // shutdown_ssl + // shutdown_socket + // close_socket + // should ONLY be called when socket_mutex_ is locked. + // Also, shutdown_ssl and close_socket should also NOT be called concurrently + // with a DIFFERENT thread sending requests using that socket. + virtual void shutdown_ssl(Socket &socket, bool shutdown_gracefully); + void shutdown_socket(Socket &socket); + void close_socket(Socket &socket); + + bool process_request(Stream &strm, Request &req, Response &res, + bool close_connection, Error &error); + + bool write_content_with_provider(Stream &strm, const Request &req, + Error &error); + + void copy_settings(const ClientImpl &rhs); + + // Socket endoint information + const std::string host_; + const int port_; + const std::string host_and_port_; + + // Current open socket + Socket socket_; + mutable std::mutex socket_mutex_; + std::recursive_mutex request_mutex_; + + // These are all protected under socket_mutex + size_t socket_requests_in_flight_ = 0; + std::thread::id socket_requests_are_from_thread_ = std::thread::id(); + bool socket_should_be_closed_when_request_is_done_ = false; + + // Hostname-IP map + std::map addr_map_; + + // Default headers + Headers default_headers_; + + // Settings + std::string client_cert_path_; + std::string client_key_path_; + + time_t connection_timeout_sec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND; + time_t connection_timeout_usec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND; + time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND; + time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND; + time_t write_timeout_sec_ = CPPHTTPLIB_WRITE_TIMEOUT_SECOND; + time_t write_timeout_usec_ = CPPHTTPLIB_WRITE_TIMEOUT_USECOND; + + std::string basic_auth_username_; + std::string basic_auth_password_; + std::string bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + std::string digest_auth_username_; + std::string digest_auth_password_; +#endif + + bool keep_alive_ = false; + bool follow_location_ = false; + + bool url_encode_ = true; + + int address_family_ = AF_UNSPEC; + bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; + SocketOptions socket_options_ = nullptr; + + bool compress_ = false; + bool decompress_ = true; + + std::string interface_; + + std::string proxy_host_; + int proxy_port_ = -1; + + std::string proxy_basic_auth_username_; + std::string proxy_basic_auth_password_; + std::string proxy_bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + std::string proxy_digest_auth_username_; + std::string proxy_digest_auth_password_; +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + std::string ca_cert_file_path_; + std::string ca_cert_dir_path_; + + X509_STORE *ca_cert_store_ = nullptr; +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + bool server_certificate_verification_ = true; +#endif + + Logger logger_; + +private: + socket_t create_client_socket(Error &error) const; + bool read_response_line(Stream &strm, const Request &req, Response &res); + bool write_request(Stream &strm, Request &req, bool close_connection, + Error &error); + bool redirect(Request &req, Response &res, Error &error); + bool handle_request(Stream &strm, Request &req, Response &res, + bool close_connection, Error &error); + std::unique_ptr send_with_content_provider( + Request &req, const char *body, size_t content_length, + ContentProvider content_provider, + ContentProviderWithoutLength content_provider_without_length, + const std::string &content_type, Error &error); + Result send_with_content_provider( + const std::string &method, const std::string &path, + const Headers &headers, const char *body, size_t content_length, + ContentProvider content_provider, + ContentProviderWithoutLength content_provider_without_length, + const std::string &content_type); + + std::string adjust_host_string(const std::string &host) const; + + virtual bool process_socket(const Socket &socket, + std::function callback); + virtual bool is_ssl() const; +}; + +class Client { +public: + // Universal interface + explicit Client(const std::string &scheme_host_port); + + explicit Client(const std::string &scheme_host_port, + const std::string &client_cert_path, + const std::string &client_key_path); + + // HTTP only interface + explicit Client(const std::string &host, int port); + + explicit Client(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path); + + Client(Client &&) = default; + + ~Client(); + + bool is_valid() const; + + Result Get(const std::string &path); + Result Get(const std::string &path, const Headers &headers); + Result Get(const std::string &path, Progress progress); + Result Get(const std::string &path, const Headers &headers, + Progress progress); + Result Get(const std::string &path, ContentReceiver content_receiver); + Result Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver); + Result Get(const std::string &path, ContentReceiver content_receiver, + Progress progress); + Result Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver, Progress progress); + Result Get(const std::string &path, ResponseHandler response_handler, + ContentReceiver content_receiver); + Result Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver); + Result Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, ContentReceiver content_receiver, + Progress progress); + Result Get(const std::string &path, ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress); + + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, ContentReceiver content_receiver, + Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress = nullptr); + + Result Head(const std::string &path); + Result Head(const std::string &path, const Headers &headers); + + Result Post(const std::string &path); + Result Post(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type); + Result Post(const std::string &path, const std::string &body, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type); + Result Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Params ¶ms); + Result Post(const std::string &path, const Headers &headers, + const Params ¶ms); + Result Post(const std::string &path, const MultipartFormDataItems &items); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, const std::string &boundary); + Result Put(const std::string &path); + Result Put(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type); + Result Put(const std::string &path, const std::string &body, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Put(const std::string &path, size_t content_length, + ContentProvider content_provider, const std::string &content_type); + Result Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Params ¶ms); + Result Put(const std::string &path, const Headers &headers, + const Params ¶ms); + Result Patch(const std::string &path); + Result Patch(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type); + Result Patch(const std::string &path, const std::string &body, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type); + Result Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + + Result Delete(const std::string &path); + Result Delete(const std::string &path, const Headers &headers); + Result Delete(const std::string &path, const char *body, + size_t content_length, const std::string &content_type); + Result Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type); + Result Delete(const std::string &path, const std::string &body, + const std::string &content_type); + Result Delete(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + + Result Options(const std::string &path); + Result Options(const std::string &path, const Headers &headers); + + bool send(Request &req, Response &res, Error &error); + Result send(const Request &req); + + size_t is_socket_open() const; + + socket_t socket() const; + + void stop(); + + void set_hostname_addr_map(std::map addr_map); + + void set_default_headers(Headers headers); + + void set_address_family(int family); + void set_tcp_nodelay(bool on); + void set_socket_options(SocketOptions socket_options); + + void set_connection_timeout(time_t sec, time_t usec = 0); + template + void + set_connection_timeout(const std::chrono::duration &duration); + + void set_read_timeout(time_t sec, time_t usec = 0); + template + void set_read_timeout(const std::chrono::duration &duration); + + void set_write_timeout(time_t sec, time_t usec = 0); + template + void set_write_timeout(const std::chrono::duration &duration); + + void set_basic_auth(const std::string &username, const std::string &password); + void set_bearer_token_auth(const std::string &token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_digest_auth(const std::string &username, + const std::string &password); +#endif + + void set_keep_alive(bool on); + void set_follow_location(bool on); + + void set_url_encode(bool on); + + void set_compress(bool on); + + void set_decompress(bool on); + + void set_interface(const std::string &intf); + + void set_proxy(const std::string &host, int port); + void set_proxy_basic_auth(const std::string &username, + const std::string &password); + void set_proxy_bearer_token_auth(const std::string &token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_proxy_digest_auth(const std::string &username, + const std::string &password); +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void enable_server_certificate_verification(bool enabled); +#endif + + void set_logger(Logger logger); + + // SSL +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path = std::string()); + + void set_ca_cert_store(X509_STORE *ca_cert_store); + + long get_openssl_verify_result() const; + + SSL_CTX *ssl_context() const; +#endif + +private: + std::unique_ptr cli_; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + bool is_ssl_ = false; +#endif +}; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +class SSLServer : public Server { +public: + SSLServer(const char *cert_path, const char *private_key_path, + const char *client_ca_cert_file_path = nullptr, + const char *client_ca_cert_dir_path = nullptr, + const char *private_key_password = nullptr); + + SSLServer(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store = nullptr); + + SSLServer( + const std::function &setup_ssl_ctx_callback); + + ~SSLServer() override; + + bool is_valid() const override; + + SSL_CTX *ssl_context() const; + +private: + bool process_and_close_socket(socket_t sock) override; + + SSL_CTX *ctx_; + std::mutex ctx_mutex_; +}; + +class SSLClient : public ClientImpl { +public: + explicit SSLClient(const std::string &host); + + explicit SSLClient(const std::string &host, int port); + + explicit SSLClient(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path); + + explicit SSLClient(const std::string &host, int port, X509 *client_cert, + EVP_PKEY *client_key); + + ~SSLClient() override; + + bool is_valid() const override; + + void set_ca_cert_store(X509_STORE *ca_cert_store); + + long get_openssl_verify_result() const; + + SSL_CTX *ssl_context() const; + +private: + bool create_and_connect_socket(Socket &socket, Error &error) override; + void shutdown_ssl(Socket &socket, bool shutdown_gracefully) override; + void shutdown_ssl_impl(Socket &socket, bool shutdown_socket); + + bool process_socket(const Socket &socket, + std::function callback) override; + bool is_ssl() const override; + + bool connect_with_proxy(Socket &sock, Response &res, bool &success, + Error &error); + bool initialize_ssl(Socket &socket, Error &error); + + bool load_certs(); + + bool verify_host(X509 *server_cert) const; + bool verify_host_with_subject_alt_name(X509 *server_cert) const; + bool verify_host_with_common_name(X509 *server_cert) const; + bool check_host_name(const char *pattern, size_t pattern_len) const; + + SSL_CTX *ctx_; + std::mutex ctx_mutex_; + std::once_flag initialize_cert_; + + std::vector host_components_; + + long verify_result_ = 0; + + friend class ClientImpl; +}; +#endif + +/* + * Implementation of template methods. + */ + +namespace detail { + +template +inline void duration_to_sec_and_usec(const T &duration, U callback) { + auto sec = std::chrono::duration_cast(duration).count(); + auto usec = std::chrono::duration_cast( + duration - std::chrono::seconds(sec)) + .count(); + callback(sec, usec); +} + +template +inline T get_header_value(const Headers & /*headers*/, + const std::string & /*key*/, size_t /*id*/ = 0, + uint64_t /*def*/ = 0) {} + +template <> +inline uint64_t get_header_value(const Headers &headers, + const std::string &key, size_t id, + uint64_t def) { + auto rng = headers.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { + return std::strtoull(it->second.data(), nullptr, 10); + } + return def; +} + +} // namespace detail + +template +inline T Request::get_header_value(const std::string &key, size_t id) const { + return detail::get_header_value(headers, key, id, 0); +} + +template +inline T Response::get_header_value(const std::string &key, size_t id) const { + return detail::get_header_value(headers, key, id, 0); +} + +template +inline ssize_t Stream::write_format(const char *fmt, const Args &...args) { + const auto bufsiz = 2048; + std::array buf{}; + + auto sn = snprintf(buf.data(), buf.size() - 1, fmt, args...); + if (sn <= 0) { return sn; } + + auto n = static_cast(sn); + + if (n >= buf.size() - 1) { + std::vector glowable_buf(buf.size()); + + while (n >= glowable_buf.size() - 1) { + glowable_buf.resize(glowable_buf.size() * 2); + n = static_cast( + snprintf(&glowable_buf[0], glowable_buf.size() - 1, fmt, args...)); + } + return write(&glowable_buf[0], n); + } else { + return write(buf.data(), n); + } +} + +inline void default_socket_options(socket_t sock) { + int yes = 1; +#ifdef _WIN32 + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), + sizeof(yes)); + setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, + reinterpret_cast(&yes), sizeof(yes)); +#else +#ifdef SO_REUSEPORT + setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&yes), + sizeof(yes)); +#else + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), + sizeof(yes)); +#endif +#endif +} + +template +inline Server & +Server::set_read_timeout(const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_read_timeout(sec, usec); }); + return *this; +} + +template +inline Server & +Server::set_write_timeout(const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_write_timeout(sec, usec); }); + return *this; +} + +template +inline Server & +Server::set_idle_interval(const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_idle_interval(sec, usec); }); + return *this; +} + +inline std::string to_string(const Error error) { + switch (error) { + case Error::Success: return "Success"; + case Error::Connection: return "Connection"; + case Error::BindIPAddress: return "BindIPAddress"; + case Error::Read: return "Read"; + case Error::Write: return "Write"; + case Error::ExceedRedirectCount: return "ExceedRedirectCount"; + case Error::Canceled: return "Canceled"; + case Error::SSLConnection: return "SSLConnection"; + case Error::SSLLoadingCerts: return "SSLLoadingCerts"; + case Error::SSLServerVerification: return "SSLServerVerification"; + case Error::UnsupportedMultipartBoundaryChars: + return "UnsupportedMultipartBoundaryChars"; + case Error::Compression: return "Compression"; + case Error::ConnectionTimeout: return "ConnectionTimeout"; + case Error::Unknown: return "Unknown"; + default: break; + } + + return "Invalid"; +} + +inline std::ostream &operator<<(std::ostream &os, const Error &obj) { + os << to_string(obj); + os << " (" << static_cast::type>(obj) << ')'; + return os; +} + +template +inline T Result::get_request_header_value(const std::string &key, + size_t id) const { + return detail::get_header_value(request_headers_, key, id, 0); +} + +template +inline void ClientImpl::set_connection_timeout( + const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec(duration, [&](time_t sec, time_t usec) { + set_connection_timeout(sec, usec); + }); +} + +template +inline void ClientImpl::set_read_timeout( + const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_read_timeout(sec, usec); }); +} + +template +inline void ClientImpl::set_write_timeout( + const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_write_timeout(sec, usec); }); +} + +template +inline void Client::set_connection_timeout( + const std::chrono::duration &duration) { + cli_->set_connection_timeout(duration); +} + +template +inline void +Client::set_read_timeout(const std::chrono::duration &duration) { + cli_->set_read_timeout(duration); +} + +template +inline void +Client::set_write_timeout(const std::chrono::duration &duration) { + cli_->set_write_timeout(duration); +} + +/* + * Forward declarations and types that will be part of the .h file if split into + * .h + .cc. + */ + +std::string hosted_at(const std::string &hostname); + +void hosted_at(const std::string &hostname, std::vector &addrs); + +std::string append_query_params(const std::string &path, const Params ¶ms); + +std::pair make_range_header(Ranges ranges); + +std::pair +make_basic_authentication_header(const std::string &username, + const std::string &password, + bool is_proxy = false); + +namespace detail { + +std::string encode_query_param(const std::string &value); + +std::string decode_url(const std::string &s, bool convert_plus_to_space); + +void read_file(const std::string &path, std::string &out); + +std::string trim_copy(const std::string &s); + +void split(const char *b, const char *e, char d, + std::function fn); + +bool process_client_socket(socket_t sock, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, + std::function callback); + +socket_t create_client_socket( + const std::string &host, const std::string &ip, int port, + int address_family, bool tcp_nodelay, SocketOptions socket_options, + time_t connection_timeout_sec, time_t connection_timeout_usec, + time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, const std::string &intf, Error &error); + +const char *get_header_value(const Headers &headers, const std::string &key, + size_t id = 0, const char *def = nullptr); + +std::string params_to_query_str(const Params ¶ms); + +void parse_query_text(const std::string &s, Params ¶ms); + +bool parse_range_header(const std::string &s, Ranges &ranges); + +int close_socket(socket_t sock); + +ssize_t send_socket(socket_t sock, const void *ptr, size_t size, int flags); + +ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags); + +enum class EncodingType { None = 0, Gzip, Brotli }; + +EncodingType encoding_type(const Request &req, const Response &res); + +class BufferStream : public Stream { +public: + BufferStream() = default; + ~BufferStream() override = default; + + bool is_readable() const override; + bool is_writable() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; + socket_t socket() const override; + + const std::string &get_buffer() const; + +private: + std::string buffer; + size_t position = 0; +}; + +class compressor { +public: + virtual ~compressor() = default; + + typedef std::function Callback; + virtual bool compress(const char *data, size_t data_length, bool last, + Callback callback) = 0; +}; + +class decompressor { +public: + virtual ~decompressor() = default; + + virtual bool is_valid() const = 0; + + typedef std::function Callback; + virtual bool decompress(const char *data, size_t data_length, + Callback callback) = 0; +}; + +class nocompressor : public compressor { +public: + virtual ~nocompressor() = default; + + bool compress(const char *data, size_t data_length, bool /*last*/, + Callback callback) override; +}; + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +class gzip_compressor : public compressor { +public: + gzip_compressor(); + ~gzip_compressor(); + + bool compress(const char *data, size_t data_length, bool last, + Callback callback) override; + +private: + bool is_valid_ = false; + z_stream strm_; +}; + +class gzip_decompressor : public decompressor { +public: + gzip_decompressor(); + ~gzip_decompressor(); + + bool is_valid() const override; + + bool decompress(const char *data, size_t data_length, + Callback callback) override; + +private: + bool is_valid_ = false; + z_stream strm_; +}; +#endif + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT +class brotli_compressor : public compressor { +public: + brotli_compressor(); + ~brotli_compressor(); + + bool compress(const char *data, size_t data_length, bool last, + Callback callback) override; + +private: + BrotliEncoderState *state_ = nullptr; +}; + +class brotli_decompressor : public decompressor { +public: + brotli_decompressor(); + ~brotli_decompressor(); + + bool is_valid() const override; + + bool decompress(const char *data, size_t data_length, + Callback callback) override; + +private: + BrotliDecoderResult decoder_r; + BrotliDecoderState *decoder_s = nullptr; +}; +#endif + +// NOTE: until the read size reaches `fixed_buffer_size`, use `fixed_buffer` +// to store data. The call can set memory on stack for performance. +class stream_line_reader { +public: + stream_line_reader(Stream &strm, char *fixed_buffer, + size_t fixed_buffer_size); + const char *ptr() const; + size_t size() const; + bool end_with_crlf() const; + bool getline(); + +private: + void append(char c); + + Stream &strm_; + char *fixed_buffer_; + const size_t fixed_buffer_size_; + size_t fixed_buffer_used_size_ = 0; + std::string glowable_buffer_; +}; + +} // namespace detail + +// ---------------------------------------------------------------------------- + +/* + * Implementation that will be part of the .cc file if split into .h + .cc. + */ + +namespace detail { + +inline bool is_hex(char c, int &v) { + if (0x20 <= c && isdigit(c)) { + v = c - '0'; + return true; + } else if ('A' <= c && c <= 'F') { + v = c - 'A' + 10; + return true; + } else if ('a' <= c && c <= 'f') { + v = c - 'a' + 10; + return true; + } + return false; +} + +inline bool from_hex_to_i(const std::string &s, size_t i, size_t cnt, + int &val) { + if (i >= s.size()) { return false; } + + val = 0; + for (; cnt; i++, cnt--) { + if (!s[i]) { return false; } + int v = 0; + if (is_hex(s[i], v)) { + val = val * 16 + v; + } else { + return false; + } + } + return true; +} + +inline std::string from_i_to_hex(size_t n) { + const char *charset = "0123456789abcdef"; + std::string ret; + do { + ret = charset[n & 15] + ret; + n >>= 4; + } while (n > 0); + return ret; +} + +inline size_t to_utf8(int code, char *buff) { + if (code < 0x0080) { + buff[0] = (code & 0x7F); + return 1; + } else if (code < 0x0800) { + buff[0] = static_cast(0xC0 | ((code >> 6) & 0x1F)); + buff[1] = static_cast(0x80 | (code & 0x3F)); + return 2; + } else if (code < 0xD800) { + buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); + buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[2] = static_cast(0x80 | (code & 0x3F)); + return 3; + } else if (code < 0xE000) { // D800 - DFFF is invalid... + return 0; + } else if (code < 0x10000) { + buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); + buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[2] = static_cast(0x80 | (code & 0x3F)); + return 3; + } else if (code < 0x110000) { + buff[0] = static_cast(0xF0 | ((code >> 18) & 0x7)); + buff[1] = static_cast(0x80 | ((code >> 12) & 0x3F)); + buff[2] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[3] = static_cast(0x80 | (code & 0x3F)); + return 4; + } + + // NOTREACHED + return 0; +} + +// NOTE: This code came up with the following stackoverflow post: +// https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c +inline std::string base64_encode(const std::string &in) { + static const auto lookup = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + std::string out; + out.reserve(in.size()); + + int val = 0; + int valb = -6; + + for (auto c : in) { + val = (val << 8) + static_cast(c); + valb += 8; + while (valb >= 0) { + out.push_back(lookup[(val >> valb) & 0x3F]); + valb -= 6; + } + } + + if (valb > -6) { out.push_back(lookup[((val << 8) >> (valb + 8)) & 0x3F]); } + + while (out.size() % 4) { + out.push_back('='); + } + + return out; +} + +inline bool is_file(const std::string &path) { +#ifdef _WIN32 + return _access_s(path.c_str(), 0) == 0; +#else + struct stat st; + return stat(path.c_str(), &st) >= 0 && S_ISREG(st.st_mode); +#endif +} + +inline bool is_dir(const std::string &path) { + struct stat st; + return stat(path.c_str(), &st) >= 0 && S_ISDIR(st.st_mode); +} + +inline bool is_valid_path(const std::string &path) { + size_t level = 0; + size_t i = 0; + + // Skip slash + while (i < path.size() && path[i] == '/') { + i++; + } + + while (i < path.size()) { + // Read component + auto beg = i; + while (i < path.size() && path[i] != '/') { + i++; + } + + auto len = i - beg; + assert(len > 0); + + if (!path.compare(beg, len, ".")) { + ; + } else if (!path.compare(beg, len, "..")) { + if (level == 0) { return false; } + level--; + } else { + level++; + } + + // Skip slash + while (i < path.size() && path[i] == '/') { + i++; + } + } + + return true; +} + +inline std::string encode_query_param(const std::string &value) { + std::ostringstream escaped; + escaped.fill('0'); + escaped << std::hex; + + for (auto c : value) { + if (std::isalnum(static_cast(c)) || c == '-' || c == '_' || + c == '.' || c == '!' || c == '~' || c == '*' || c == '\'' || c == '(' || + c == ')') { + escaped << c; + } else { + escaped << std::uppercase; + escaped << '%' << std::setw(2) + << static_cast(static_cast(c)); + escaped << std::nouppercase; + } + } + + return escaped.str(); +} + +inline std::string encode_url(const std::string &s) { + std::string result; + result.reserve(s.size()); + + for (size_t i = 0; s[i]; i++) { + switch (s[i]) { + case ' ': result += "%20"; break; + case '+': result += "%2B"; break; + case '\r': result += "%0D"; break; + case '\n': result += "%0A"; break; + case '\'': result += "%27"; break; + case ',': result += "%2C"; break; + // case ':': result += "%3A"; break; // ok? probably... + case ';': result += "%3B"; break; + default: + auto c = static_cast(s[i]); + if (c >= 0x80) { + result += '%'; + char hex[4]; + auto len = snprintf(hex, sizeof(hex) - 1, "%02X", c); + assert(len == 2); + result.append(hex, static_cast(len)); + } else { + result += s[i]; + } + break; + } + } + + return result; +} + +inline std::string decode_url(const std::string &s, + bool convert_plus_to_space) { + std::string result; + + for (size_t i = 0; i < s.size(); i++) { + if (s[i] == '%' && i + 1 < s.size()) { + if (s[i + 1] == 'u') { + int val = 0; + if (from_hex_to_i(s, i + 2, 4, val)) { + // 4 digits Unicode codes + char buff[4]; + size_t len = to_utf8(val, buff); + if (len > 0) { result.append(buff, len); } + i += 5; // 'u0000' + } else { + result += s[i]; + } + } else { + int val = 0; + if (from_hex_to_i(s, i + 1, 2, val)) { + // 2 digits hex codes + result += static_cast(val); + i += 2; // '00' + } else { + result += s[i]; + } + } + } else if (convert_plus_to_space && s[i] == '+') { + result += ' '; + } else { + result += s[i]; + } + } + + return result; +} + +inline void read_file(const std::string &path, std::string &out) { + std::ifstream fs(path, std::ios_base::binary); + fs.seekg(0, std::ios_base::end); + auto size = fs.tellg(); + fs.seekg(0); + out.resize(static_cast(size)); + fs.read(&out[0], static_cast(size)); +} + +inline std::string file_extension(const std::string &path) { + std::smatch m; + static auto re = std::regex("\\.([a-zA-Z0-9]+)$"); + if (std::regex_search(path, m, re)) { return m[1].str(); } + return std::string(); +} + +inline bool is_space_or_tab(char c) { return c == ' ' || c == '\t'; } + +inline std::pair trim(const char *b, const char *e, size_t left, + size_t right) { + while (b + left < e && is_space_or_tab(b[left])) { + left++; + } + while (right > 0 && is_space_or_tab(b[right - 1])) { + right--; + } + return std::make_pair(left, right); +} + +inline std::string trim_copy(const std::string &s) { + auto r = trim(s.data(), s.data() + s.size(), 0, s.size()); + return s.substr(r.first, r.second - r.first); +} + +inline void split(const char *b, const char *e, char d, + std::function fn) { + size_t i = 0; + size_t beg = 0; + + while (e ? (b + i < e) : (b[i] != '\0')) { + if (b[i] == d) { + auto r = trim(b, e, beg, i); + if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } + beg = i + 1; + } + i++; + } + + if (i) { + auto r = trim(b, e, beg, i); + if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } + } +} + +inline stream_line_reader::stream_line_reader(Stream &strm, char *fixed_buffer, + size_t fixed_buffer_size) + : strm_(strm), fixed_buffer_(fixed_buffer), + fixed_buffer_size_(fixed_buffer_size) {} + +inline const char *stream_line_reader::ptr() const { + if (glowable_buffer_.empty()) { + return fixed_buffer_; + } else { + return glowable_buffer_.data(); + } +} + +inline size_t stream_line_reader::size() const { + if (glowable_buffer_.empty()) { + return fixed_buffer_used_size_; + } else { + return glowable_buffer_.size(); + } +} + +inline bool stream_line_reader::end_with_crlf() const { + auto end = ptr() + size(); + return size() >= 2 && end[-2] == '\r' && end[-1] == '\n'; +} + +inline bool stream_line_reader::getline() { + fixed_buffer_used_size_ = 0; + glowable_buffer_.clear(); + + for (size_t i = 0;; i++) { + char byte; + auto n = strm_.read(&byte, 1); + + if (n < 0) { + return false; + } else if (n == 0) { + if (i == 0) { + return false; + } else { + break; + } + } + + append(byte); + + if (byte == '\n') { break; } + } + + return true; +} + +inline void stream_line_reader::append(char c) { + if (fixed_buffer_used_size_ < fixed_buffer_size_ - 1) { + fixed_buffer_[fixed_buffer_used_size_++] = c; + fixed_buffer_[fixed_buffer_used_size_] = '\0'; + } else { + if (glowable_buffer_.empty()) { + assert(fixed_buffer_[fixed_buffer_used_size_] == '\0'); + glowable_buffer_.assign(fixed_buffer_, fixed_buffer_used_size_); + } + glowable_buffer_ += c; + } +} + +inline int close_socket(socket_t sock) { +#ifdef _WIN32 + return closesocket(sock); +#else + return close(sock); +#endif +} + +template inline ssize_t handle_EINTR(T fn) { + ssize_t res = false; + while (true) { + res = fn(); + if (res < 0 && errno == EINTR) { continue; } + break; + } + return res; +} + +inline ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags) { + return handle_EINTR([&]() { + return recv(sock, +#ifdef _WIN32 + static_cast(ptr), static_cast(size), +#else + ptr, size, +#endif + flags); + }); +} + +inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size, + int flags) { + return handle_EINTR([&]() { + return send(sock, +#ifdef _WIN32 + static_cast(ptr), static_cast(size), +#else + ptr, size, +#endif + flags); + }); +} + +inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { +#ifdef CPPHTTPLIB_USE_POLL + struct pollfd pfd_read; + pfd_read.fd = sock; + pfd_read.events = POLLIN; + + auto timeout = static_cast(sec * 1000 + usec / 1000); + + return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); +#else +#ifndef _WIN32 + if (sock >= FD_SETSIZE) { return 1; } +#endif + + fd_set fds; + FD_ZERO(&fds); + FD_SET(sock, &fds); + + timeval tv; + tv.tv_sec = static_cast(sec); + tv.tv_usec = static_cast(usec); + + return handle_EINTR([&]() { + return select(static_cast(sock + 1), &fds, nullptr, nullptr, &tv); + }); +#endif +} + +inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) { +#ifdef CPPHTTPLIB_USE_POLL + struct pollfd pfd_read; + pfd_read.fd = sock; + pfd_read.events = POLLOUT; + + auto timeout = static_cast(sec * 1000 + usec / 1000); + + return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); +#else +#ifndef _WIN32 + if (sock >= FD_SETSIZE) { return 1; } +#endif + + fd_set fds; + FD_ZERO(&fds); + FD_SET(sock, &fds); + + timeval tv; + tv.tv_sec = static_cast(sec); + tv.tv_usec = static_cast(usec); + + return handle_EINTR([&]() { + return select(static_cast(sock + 1), nullptr, &fds, nullptr, &tv); + }); +#endif +} + +inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, + time_t usec) { +#ifdef CPPHTTPLIB_USE_POLL + struct pollfd pfd_read; + pfd_read.fd = sock; + pfd_read.events = POLLIN | POLLOUT; + + auto timeout = static_cast(sec * 1000 + usec / 1000); + + auto poll_res = handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); + + if (poll_res == 0) { return Error::ConnectionTimeout; } + + if (poll_res > 0 && pfd_read.revents & (POLLIN | POLLOUT)) { + int error = 0; + socklen_t len = sizeof(error); + auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, + reinterpret_cast(&error), &len); + auto successful = res >= 0 && !error; + return successful ? Error::Success : Error::Connection; + } + + return Error::Connection; +#else +#ifndef _WIN32 + if (sock >= FD_SETSIZE) { return Error::Connection; } +#endif + + fd_set fdsr; + FD_ZERO(&fdsr); + FD_SET(sock, &fdsr); + + auto fdsw = fdsr; + auto fdse = fdsr; + + timeval tv; + tv.tv_sec = static_cast(sec); + tv.tv_usec = static_cast(usec); + + auto ret = handle_EINTR([&]() { + return select(static_cast(sock + 1), &fdsr, &fdsw, &fdse, &tv); + }); + + if (ret == 0) { return Error::ConnectionTimeout; } + + if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { + int error = 0; + socklen_t len = sizeof(error); + auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, + reinterpret_cast(&error), &len); + auto successful = res >= 0 && !error; + return successful ? Error::Success : Error::Connection; + } + return Error::Connection; +#endif +} + +inline bool is_socket_alive(socket_t sock) { + const auto val = detail::select_read(sock, 0, 0); + if (val == 0) { + return true; + } else if (val < 0 && errno == EBADF) { + return false; + } + char buf[1]; + return detail::read_socket(sock, &buf[0], sizeof(buf), MSG_PEEK) > 0; +} + +class SocketStream : public Stream { +public: + SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec); + ~SocketStream() override; + + bool is_readable() const override; + bool is_writable() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; + socket_t socket() const override; + +private: + socket_t sock_; + time_t read_timeout_sec_; + time_t read_timeout_usec_; + time_t write_timeout_sec_; + time_t write_timeout_usec_; + + std::vector read_buff_; + size_t read_buff_off_ = 0; + size_t read_buff_content_size_ = 0; + + static const size_t read_buff_size_ = 1024 * 4; +}; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +class SSLSocketStream : public Stream { +public: + SSLSocketStream(socket_t sock, SSL *ssl, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec); + ~SSLSocketStream() override; + + bool is_readable() const override; + bool is_writable() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; + socket_t socket() const override; + +private: + socket_t sock_; + SSL *ssl_; + time_t read_timeout_sec_; + time_t read_timeout_usec_; + time_t write_timeout_sec_; + time_t write_timeout_usec_; +}; +#endif + +inline bool keep_alive(socket_t sock, time_t keep_alive_timeout_sec) { + using namespace std::chrono; + auto start = steady_clock::now(); + while (true) { + auto val = select_read(sock, 0, 10000); + if (val < 0) { + return false; + } else if (val == 0) { + auto current = steady_clock::now(); + auto duration = duration_cast(current - start); + auto timeout = keep_alive_timeout_sec * 1000; + if (duration.count() > timeout) { return false; } + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } else { + return true; + } + } +} + +template +inline bool +process_server_socket_core(const std::atomic &svr_sock, socket_t sock, + size_t keep_alive_max_count, + time_t keep_alive_timeout_sec, T callback) { + assert(keep_alive_max_count > 0); + auto ret = false; + auto count = keep_alive_max_count; + while (svr_sock != INVALID_SOCKET && count > 0 && + keep_alive(sock, keep_alive_timeout_sec)) { + auto close_connection = count == 1; + auto connection_closed = false; + ret = callback(close_connection, connection_closed); + if (!ret || connection_closed) { break; } + count--; + } + return ret; +} + +template +inline bool +process_server_socket(const std::atomic &svr_sock, socket_t sock, + size_t keep_alive_max_count, + time_t keep_alive_timeout_sec, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { + return process_server_socket_core( + svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, + [&](bool close_connection, bool &connection_closed) { + SocketStream strm(sock, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm, close_connection, connection_closed); + }); +} + +inline bool process_client_socket(socket_t sock, time_t read_timeout_sec, + time_t read_timeout_usec, + time_t write_timeout_sec, + time_t write_timeout_usec, + std::function callback) { + SocketStream strm(sock, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm); +} + +inline int shutdown_socket(socket_t sock) { +#ifdef _WIN32 + return shutdown(sock, SD_BOTH); +#else + return shutdown(sock, SHUT_RDWR); +#endif +} + +template +socket_t create_socket(const std::string &host, const std::string &ip, int port, + int address_family, int socket_flags, bool tcp_nodelay, + SocketOptions socket_options, + BindOrConnect bind_or_connect) { + // Get address info + const char *node = nullptr; + struct addrinfo hints; + struct addrinfo *result; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = 0; + + if (!ip.empty()) { + node = ip.c_str(); + // Ask getaddrinfo to convert IP in c-string to address + hints.ai_family = AF_UNSPEC; + hints.ai_flags = AI_NUMERICHOST; + } else { + if (!host.empty()) { node = host.c_str(); } + hints.ai_family = address_family; + hints.ai_flags = socket_flags; + } + +#ifndef _WIN32 + if (hints.ai_family == AF_UNIX) { + const auto addrlen = host.length(); + if (addrlen > sizeof(sockaddr_un::sun_path)) return INVALID_SOCKET; + + auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol); + if (sock != INVALID_SOCKET) { + sockaddr_un addr; + addr.sun_family = AF_UNIX; + std::copy(host.begin(), host.end(), addr.sun_path); + + hints.ai_addr = reinterpret_cast(&addr); + hints.ai_addrlen = static_cast( + sizeof(addr) - sizeof(addr.sun_path) + addrlen); + + if (!bind_or_connect(sock, hints)) { + close_socket(sock); + sock = INVALID_SOCKET; + } + } + return sock; + } +#endif + + auto service = std::to_string(port); + + if (getaddrinfo(node, service.c_str(), &hints, &result)) { +#if defined __linux__ && !defined __ANDROID__ + res_init(); +#endif + return INVALID_SOCKET; + } + + for (auto rp = result; rp; rp = rp->ai_next) { + // Create a socket +#ifdef _WIN32 + auto sock = + WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0, + WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED); + /** + * Since the WSA_FLAG_NO_HANDLE_INHERIT is only supported on Windows 7 SP1 + * and above the socket creation fails on older Windows Systems. + * + * Let's try to create a socket the old way in this case. + * + * Reference: + * https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa + * + * WSA_FLAG_NO_HANDLE_INHERIT: + * This flag is supported on Windows 7 with SP1, Windows Server 2008 R2 with + * SP1, and later + * + */ + if (sock == INVALID_SOCKET) { + sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + } +#else + auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); +#endif + if (sock == INVALID_SOCKET) { continue; } + +#ifndef _WIN32 + if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { continue; } +#endif + + if (tcp_nodelay) { + int yes = 1; + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&yes), + sizeof(yes)); + } + + if (socket_options) { socket_options(sock); } + + if (rp->ai_family == AF_INET6) { + int no = 0; + setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast(&no), + sizeof(no)); + } + + // bind or connect + if (bind_or_connect(sock, *rp)) { + freeaddrinfo(result); + return sock; + } + + close_socket(sock); + } + + freeaddrinfo(result); + return INVALID_SOCKET; +} + +inline void set_nonblocking(socket_t sock, bool nonblocking) { +#ifdef _WIN32 + auto flags = nonblocking ? 1UL : 0UL; + ioctlsocket(sock, FIONBIO, &flags); +#else + auto flags = fcntl(sock, F_GETFL, 0); + fcntl(sock, F_SETFL, + nonblocking ? (flags | O_NONBLOCK) : (flags & (~O_NONBLOCK))); +#endif +} + +inline bool is_connection_error() { +#ifdef _WIN32 + return WSAGetLastError() != WSAEWOULDBLOCK; +#else + return errno != EINPROGRESS; +#endif +} + +inline bool bind_ip_address(socket_t sock, const std::string &host) { + struct addrinfo hints; + struct addrinfo *result; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = 0; + + if (getaddrinfo(host.c_str(), "0", &hints, &result)) { return false; } + + auto ret = false; + for (auto rp = result; rp; rp = rp->ai_next) { + const auto &ai = *rp; + if (!::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { + ret = true; + break; + } + } + + freeaddrinfo(result); + return ret; +} + +#if !defined _WIN32 && !defined ANDROID +#define USE_IF2IP +#endif + +#ifdef USE_IF2IP +inline std::string if2ip(int address_family, const std::string &ifn) { + struct ifaddrs *ifap; + getifaddrs(&ifap); + std::string addr_candidate; + for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) { + if (ifa->ifa_addr && ifn == ifa->ifa_name && + (AF_UNSPEC == address_family || + ifa->ifa_addr->sa_family == address_family)) { + if (ifa->ifa_addr->sa_family == AF_INET) { + auto sa = reinterpret_cast(ifa->ifa_addr); + char buf[INET_ADDRSTRLEN]; + if (inet_ntop(AF_INET, &sa->sin_addr, buf, INET_ADDRSTRLEN)) { + freeifaddrs(ifap); + return std::string(buf, INET_ADDRSTRLEN); + } + } else if (ifa->ifa_addr->sa_family == AF_INET6) { + auto sa = reinterpret_cast(ifa->ifa_addr); + if (!IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr)) { + char buf[INET6_ADDRSTRLEN] = {}; + if (inet_ntop(AF_INET6, &sa->sin6_addr, buf, INET6_ADDRSTRLEN)) { + // equivalent to mac's IN6_IS_ADDR_UNIQUE_LOCAL + auto s6_addr_head = sa->sin6_addr.s6_addr[0]; + if (s6_addr_head == 0xfc || s6_addr_head == 0xfd) { + addr_candidate = std::string(buf, INET6_ADDRSTRLEN); + } else { + freeifaddrs(ifap); + return std::string(buf, INET6_ADDRSTRLEN); + } + } + } + } + } + } + freeifaddrs(ifap); + return addr_candidate; +} +#endif + +inline socket_t create_client_socket( + const std::string &host, const std::string &ip, int port, + int address_family, bool tcp_nodelay, SocketOptions socket_options, + time_t connection_timeout_sec, time_t connection_timeout_usec, + time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, const std::string &intf, Error &error) { + auto sock = create_socket( + host, ip, port, address_family, 0, tcp_nodelay, std::move(socket_options), + [&](socket_t sock2, struct addrinfo &ai) -> bool { + if (!intf.empty()) { +#ifdef USE_IF2IP + auto ip_from_if = if2ip(address_family, intf); + if (ip_from_if.empty()) { ip_from_if = intf; } + if (!bind_ip_address(sock2, ip_from_if.c_str())) { + error = Error::BindIPAddress; + return false; + } +#endif + } + + set_nonblocking(sock2, true); + + auto ret = + ::connect(sock2, ai.ai_addr, static_cast(ai.ai_addrlen)); + + if (ret < 0) { + if (is_connection_error()) { + error = Error::Connection; + return false; + } + error = wait_until_socket_is_ready(sock2, connection_timeout_sec, + connection_timeout_usec); + if (error != Error::Success) { return false; } + } + + set_nonblocking(sock2, false); + + { +#ifdef _WIN32 + auto timeout = static_cast(read_timeout_sec * 1000 + + read_timeout_usec / 1000); + setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, + sizeof(timeout)); +#else + timeval tv; + tv.tv_sec = static_cast(read_timeout_sec); + tv.tv_usec = static_cast(read_timeout_usec); + setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)); +#endif + } + { + +#ifdef _WIN32 + auto timeout = static_cast(write_timeout_sec * 1000 + + write_timeout_usec / 1000); + setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, + sizeof(timeout)); +#else + timeval tv; + tv.tv_sec = static_cast(write_timeout_sec); + tv.tv_usec = static_cast(write_timeout_usec); + setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)); +#endif + } + + error = Error::Success; + return true; + }); + + if (sock != INVALID_SOCKET) { + error = Error::Success; + } else { + if (error == Error::Success) { error = Error::Connection; } + } + + return sock; +} + +inline bool get_remote_ip_and_port(const struct sockaddr_storage &addr, + socklen_t addr_len, std::string &ip, + int &port) { + if (addr.ss_family == AF_INET) { + port = ntohs(reinterpret_cast(&addr)->sin_port); + } else if (addr.ss_family == AF_INET6) { + port = + ntohs(reinterpret_cast(&addr)->sin6_port); + } else { + return false; + } + + std::array ipstr{}; + if (getnameinfo(reinterpret_cast(&addr), addr_len, + ipstr.data(), static_cast(ipstr.size()), nullptr, + 0, NI_NUMERICHOST)) { + return false; + } + + ip = ipstr.data(); + return true; +} + +inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) { + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); + + if (!getpeername(sock, reinterpret_cast(&addr), + &addr_len)) { + get_remote_ip_and_port(addr, addr_len, ip, port); + } +} + +inline constexpr unsigned int str2tag_core(const char *s, size_t l, + unsigned int h) { + return (l == 0) ? h + : str2tag_core(s + 1, l - 1, + (h * 33) ^ static_cast(*s)); +} + +inline unsigned int str2tag(const std::string &s) { + return str2tag_core(s.data(), s.size(), 0); +} + +namespace udl { + +inline constexpr unsigned int operator"" _t(const char *s, size_t l) { + return str2tag_core(s, l, 0); +} + +} // namespace udl + +inline const char * +find_content_type(const std::string &path, + const std::map &user_data) { + auto ext = file_extension(path); + + auto it = user_data.find(ext); + if (it != user_data.end()) { return it->second.c_str(); } + + using udl::operator""_t; + + switch (str2tag(ext)) { + default: return nullptr; + case "css"_t: return "text/css"; + case "csv"_t: return "text/csv"; + case "htm"_t: + case "html"_t: return "text/html"; + case "js"_t: + case "mjs"_t: return "text/javascript"; + case "txt"_t: return "text/plain"; + case "vtt"_t: return "text/vtt"; + + case "apng"_t: return "image/apng"; + case "avif"_t: return "image/avif"; + case "bmp"_t: return "image/bmp"; + case "gif"_t: return "image/gif"; + case "png"_t: return "image/png"; + case "svg"_t: return "image/svg+xml"; + case "webp"_t: return "image/webp"; + case "ico"_t: return "image/x-icon"; + case "tif"_t: return "image/tiff"; + case "tiff"_t: return "image/tiff"; + case "jpg"_t: + case "jpeg"_t: return "image/jpeg"; + + case "mp4"_t: return "video/mp4"; + case "mpeg"_t: return "video/mpeg"; + case "webm"_t: return "video/webm"; + + case "mp3"_t: return "audio/mp3"; + case "mpga"_t: return "audio/mpeg"; + case "weba"_t: return "audio/webm"; + case "wav"_t: return "audio/wave"; + + case "otf"_t: return "font/otf"; + case "ttf"_t: return "font/ttf"; + case "woff"_t: return "font/woff"; + case "woff2"_t: return "font/woff2"; + + case "7z"_t: return "application/x-7z-compressed"; + case "atom"_t: return "application/atom+xml"; + case "pdf"_t: return "application/pdf"; + case "json"_t: return "application/json"; + case "rss"_t: return "application/rss+xml"; + case "tar"_t: return "application/x-tar"; + case "xht"_t: + case "xhtml"_t: return "application/xhtml+xml"; + case "xslt"_t: return "application/xslt+xml"; + case "xml"_t: return "application/xml"; + case "gz"_t: return "application/gzip"; + case "zip"_t: return "application/zip"; + case "wasm"_t: return "application/wasm"; + } +} + +inline const char *status_message(int status) { + switch (status) { + case 100: return "Continue"; + case 101: return "Switching Protocol"; + case 102: return "Processing"; + case 103: return "Early Hints"; + case 200: return "OK"; + case 201: return "Created"; + case 202: return "Accepted"; + case 203: return "Non-Authoritative Information"; + case 204: return "No Content"; + case 205: return "Reset Content"; + case 206: return "Partial Content"; + case 207: return "Multi-Status"; + case 208: return "Already Reported"; + case 226: return "IM Used"; + case 300: return "Multiple Choice"; + case 301: return "Moved Permanently"; + case 302: return "Found"; + case 303: return "See Other"; + case 304: return "Not Modified"; + case 305: return "Use Proxy"; + case 306: return "unused"; + case 307: return "Temporary Redirect"; + case 308: return "Permanent Redirect"; + case 400: return "Bad Request"; + case 401: return "Unauthorized"; + case 402: return "Payment Required"; + case 403: return "Forbidden"; + case 404: return "Not Found"; + case 405: return "Method Not Allowed"; + case 406: return "Not Acceptable"; + case 407: return "Proxy Authentication Required"; + case 408: return "Request Timeout"; + case 409: return "Conflict"; + case 410: return "Gone"; + case 411: return "Length Required"; + case 412: return "Precondition Failed"; + case 413: return "Payload Too Large"; + case 414: return "URI Too Long"; + case 415: return "Unsupported Media Type"; + case 416: return "Range Not Satisfiable"; + case 417: return "Expectation Failed"; + case 418: return "I'm a teapot"; + case 421: return "Misdirected Request"; + case 422: return "Unprocessable Entity"; + case 423: return "Locked"; + case 424: return "Failed Dependency"; + case 425: return "Too Early"; + case 426: return "Upgrade Required"; + case 428: return "Precondition Required"; + case 429: return "Too Many Requests"; + case 431: return "Request Header Fields Too Large"; + case 451: return "Unavailable For Legal Reasons"; + case 501: return "Not Implemented"; + case 502: return "Bad Gateway"; + case 503: return "Service Unavailable"; + case 504: return "Gateway Timeout"; + case 505: return "HTTP Version Not Supported"; + case 506: return "Variant Also Negotiates"; + case 507: return "Insufficient Storage"; + case 508: return "Loop Detected"; + case 510: return "Not Extended"; + case 511: return "Network Authentication Required"; + + default: + case 500: return "Internal Server Error"; + } +} + +inline bool can_compress_content_type(const std::string &content_type) { + using udl::operator""_t; + + auto tag = str2tag(content_type); + + switch (tag) { + case "image/svg+xml"_t: + case "application/javascript"_t: + case "application/json"_t: + case "application/xml"_t: + case "application/protobuf"_t: + case "application/xhtml+xml"_t: return true; + + default: + return !content_type.rfind("text/", 0) && tag != "text/event-stream"_t; + } +} + +inline EncodingType encoding_type(const Request &req, const Response &res) { + auto ret = + detail::can_compress_content_type(res.get_header_value("Content-Type")); + if (!ret) { return EncodingType::None; } + + const auto &s = req.get_header_value("Accept-Encoding"); + (void)(s); + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + // TODO: 'Accept-Encoding' has br, not br;q=0 + ret = s.find("br") != std::string::npos; + if (ret) { return EncodingType::Brotli; } +#endif + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + // TODO: 'Accept-Encoding' has gzip, not gzip;q=0 + ret = s.find("gzip") != std::string::npos; + if (ret) { return EncodingType::Gzip; } +#endif + + return EncodingType::None; +} + +inline bool nocompressor::compress(const char *data, size_t data_length, + bool /*last*/, Callback callback) { + if (!data_length) { return true; } + return callback(data, data_length); +} + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +inline gzip_compressor::gzip_compressor() { + std::memset(&strm_, 0, sizeof(strm_)); + strm_.zalloc = Z_NULL; + strm_.zfree = Z_NULL; + strm_.opaque = Z_NULL; + + is_valid_ = deflateInit2(&strm_, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8, + Z_DEFAULT_STRATEGY) == Z_OK; +} + +inline gzip_compressor::~gzip_compressor() { deflateEnd(&strm_); } + +inline bool gzip_compressor::compress(const char *data, size_t data_length, + bool last, Callback callback) { + assert(is_valid_); + + do { + constexpr size_t max_avail_in = + (std::numeric_limits::max)(); + + strm_.avail_in = static_cast( + (std::min)(data_length, max_avail_in)); + strm_.next_in = const_cast(reinterpret_cast(data)); + + data_length -= strm_.avail_in; + data += strm_.avail_in; + + auto flush = (last && data_length == 0) ? Z_FINISH : Z_NO_FLUSH; + int ret = Z_OK; + + std::array buff{}; + do { + strm_.avail_out = static_cast(buff.size()); + strm_.next_out = reinterpret_cast(buff.data()); + + ret = deflate(&strm_, flush); + if (ret == Z_STREAM_ERROR) { return false; } + + if (!callback(buff.data(), buff.size() - strm_.avail_out)) { + return false; + } + } while (strm_.avail_out == 0); + + assert((flush == Z_FINISH && ret == Z_STREAM_END) || + (flush == Z_NO_FLUSH && ret == Z_OK)); + assert(strm_.avail_in == 0); + } while (data_length > 0); + + return true; +} + +inline gzip_decompressor::gzip_decompressor() { + std::memset(&strm_, 0, sizeof(strm_)); + strm_.zalloc = Z_NULL; + strm_.zfree = Z_NULL; + strm_.opaque = Z_NULL; + + // 15 is the value of wbits, which should be at the maximum possible value + // to ensure that any gzip stream can be decoded. The offset of 32 specifies + // that the stream type should be automatically detected either gzip or + // deflate. + is_valid_ = inflateInit2(&strm_, 32 + 15) == Z_OK; +} + +inline gzip_decompressor::~gzip_decompressor() { inflateEnd(&strm_); } + +inline bool gzip_decompressor::is_valid() const { return is_valid_; } + +inline bool gzip_decompressor::decompress(const char *data, size_t data_length, + Callback callback) { + assert(is_valid_); + + int ret = Z_OK; + + do { + constexpr size_t max_avail_in = + (std::numeric_limits::max)(); + + strm_.avail_in = static_cast( + (std::min)(data_length, max_avail_in)); + strm_.next_in = const_cast(reinterpret_cast(data)); + + data_length -= strm_.avail_in; + data += strm_.avail_in; + + std::array buff{}; + while (strm_.avail_in > 0) { + strm_.avail_out = static_cast(buff.size()); + strm_.next_out = reinterpret_cast(buff.data()); + + auto prev_avail_in = strm_.avail_in; + + ret = inflate(&strm_, Z_NO_FLUSH); + + if (prev_avail_in - strm_.avail_in == 0) { return false; } + + assert(ret != Z_STREAM_ERROR); + switch (ret) { + case Z_NEED_DICT: + case Z_DATA_ERROR: + case Z_MEM_ERROR: inflateEnd(&strm_); return false; + } + + if (!callback(buff.data(), buff.size() - strm_.avail_out)) { + return false; + } + } + + if (ret != Z_OK && ret != Z_STREAM_END) return false; + + } while (data_length > 0); + + return true; +} +#endif + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT +inline brotli_compressor::brotli_compressor() { + state_ = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr); +} + +inline brotli_compressor::~brotli_compressor() { + BrotliEncoderDestroyInstance(state_); +} + +inline bool brotli_compressor::compress(const char *data, size_t data_length, + bool last, Callback callback) { + std::array buff{}; + + auto operation = last ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS; + auto available_in = data_length; + auto next_in = reinterpret_cast(data); + + for (;;) { + if (last) { + if (BrotliEncoderIsFinished(state_)) { break; } + } else { + if (!available_in) { break; } + } + + auto available_out = buff.size(); + auto next_out = buff.data(); + + if (!BrotliEncoderCompressStream(state_, operation, &available_in, &next_in, + &available_out, &next_out, nullptr)) { + return false; + } + + auto output_bytes = buff.size() - available_out; + if (output_bytes) { + callback(reinterpret_cast(buff.data()), output_bytes); + } + } + + return true; +} + +inline brotli_decompressor::brotli_decompressor() { + decoder_s = BrotliDecoderCreateInstance(0, 0, 0); + decoder_r = decoder_s ? BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT + : BROTLI_DECODER_RESULT_ERROR; +} + +inline brotli_decompressor::~brotli_decompressor() { + if (decoder_s) { BrotliDecoderDestroyInstance(decoder_s); } +} + +inline bool brotli_decompressor::is_valid() const { return decoder_s; } + +inline bool brotli_decompressor::decompress(const char *data, + size_t data_length, + Callback callback) { + if (decoder_r == BROTLI_DECODER_RESULT_SUCCESS || + decoder_r == BROTLI_DECODER_RESULT_ERROR) { + return 0; + } + + const uint8_t *next_in = (const uint8_t *)data; + size_t avail_in = data_length; + size_t total_out; + + decoder_r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; + + std::array buff{}; + while (decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) { + char *next_out = buff.data(); + size_t avail_out = buff.size(); + + decoder_r = BrotliDecoderDecompressStream( + decoder_s, &avail_in, &next_in, &avail_out, + reinterpret_cast(&next_out), &total_out); + + if (decoder_r == BROTLI_DECODER_RESULT_ERROR) { return false; } + + if (!callback(buff.data(), buff.size() - avail_out)) { return false; } + } + + return decoder_r == BROTLI_DECODER_RESULT_SUCCESS || + decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT; +} +#endif + +inline bool has_header(const Headers &headers, const std::string &key) { + return headers.find(key) != headers.end(); +} + +inline const char *get_header_value(const Headers &headers, + const std::string &key, size_t id, + const char *def) { + auto rng = headers.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second.c_str(); } + return def; +} + +template +inline bool parse_header(const char *beg, const char *end, T fn) { + // Skip trailing spaces and tabs. + while (beg < end && is_space_or_tab(end[-1])) { + end--; + } + + auto p = beg; + while (p < end && *p != ':') { + p++; + } + + if (p == end) { return false; } + + auto key_end = p; + + if (*p++ != ':') { return false; } + + while (p < end && is_space_or_tab(*p)) { + p++; + } + + if (p < end) { + fn(std::string(beg, key_end), decode_url(std::string(p, end), false)); + return true; + } + + return false; +} + +inline bool read_headers(Stream &strm, Headers &headers) { + const auto bufsiz = 2048; + char buf[bufsiz]; + stream_line_reader line_reader(strm, buf, bufsiz); + + for (;;) { + if (!line_reader.getline()) { return false; } + + // Check if the line ends with CRLF. + auto line_terminator_len = 2; + if (line_reader.end_with_crlf()) { + // Blank line indicates end of headers. + if (line_reader.size() == 2) { break; } +#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR + } else { + // Blank line indicates end of headers. + if (line_reader.size() == 1) { break; } + line_terminator_len = 1; + } +#else + } else { + continue; // Skip invalid line. + } +#endif + + if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + + // Exclude line terminator + auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; + + parse_header(line_reader.ptr(), end, + [&](std::string &&key, std::string &&val) { + headers.emplace(std::move(key), std::move(val)); + }); + } + + return true; +} + +inline bool read_content_with_length(Stream &strm, uint64_t len, + Progress progress, + ContentReceiverWithProgress out) { + char buf[CPPHTTPLIB_RECV_BUFSIZ]; + + uint64_t r = 0; + while (r < len) { + auto read_len = static_cast(len - r); + auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); + if (n <= 0) { return false; } + + if (!out(buf, static_cast(n), r, len)) { return false; } + r += static_cast(n); + + if (progress) { + if (!progress(r, len)) { return false; } + } + } + + return true; +} + +inline void skip_content_with_length(Stream &strm, uint64_t len) { + char buf[CPPHTTPLIB_RECV_BUFSIZ]; + uint64_t r = 0; + while (r < len) { + auto read_len = static_cast(len - r); + auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); + if (n <= 0) { return; } + r += static_cast(n); + } +} + +inline bool read_content_without_length(Stream &strm, + ContentReceiverWithProgress out) { + char buf[CPPHTTPLIB_RECV_BUFSIZ]; + uint64_t r = 0; + for (;;) { + auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ); + if (n < 0) { + return false; + } else if (n == 0) { + return true; + } + + if (!out(buf, static_cast(n), r, 0)) { return false; } + r += static_cast(n); + } + + return true; +} + +inline bool read_content_chunked(Stream &strm, + ContentReceiverWithProgress out) { + const auto bufsiz = 16; + char buf[bufsiz]; + + stream_line_reader line_reader(strm, buf, bufsiz); + + if (!line_reader.getline()) { return false; } + + unsigned long chunk_len; + while (true) { + char *end_ptr; + + chunk_len = std::strtoul(line_reader.ptr(), &end_ptr, 16); + + if (end_ptr == line_reader.ptr()) { return false; } + if (chunk_len == ULONG_MAX) { return false; } + + if (chunk_len == 0) { break; } + + if (!read_content_with_length(strm, chunk_len, nullptr, out)) { + return false; + } + + if (!line_reader.getline()) { return false; } + + if (strcmp(line_reader.ptr(), "\r\n")) { break; } + + if (!line_reader.getline()) { return false; } + } + + if (chunk_len == 0) { + // Reader terminator after chunks + if (!line_reader.getline() || strcmp(line_reader.ptr(), "\r\n")) + return false; + } + + return true; +} + +inline bool is_chunked_transfer_encoding(const Headers &headers) { + return !strcasecmp(get_header_value(headers, "Transfer-Encoding", 0, ""), + "chunked"); +} + +template +bool prepare_content_receiver(T &x, int &status, + ContentReceiverWithProgress receiver, + bool decompress, U callback) { + if (decompress) { + std::string encoding = x.get_header_value("Content-Encoding"); + std::unique_ptr decompressor; + + if (encoding == "gzip" || encoding == "deflate") { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + decompressor = detail::make_unique(); +#else + status = 415; + return false; +#endif + } else if (encoding.find("br") != std::string::npos) { +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + decompressor = detail::make_unique(); +#else + status = 415; + return false; +#endif + } + + if (decompressor) { + if (decompressor->is_valid()) { + ContentReceiverWithProgress out = [&](const char *buf, size_t n, + uint64_t off, uint64_t len) { + return decompressor->decompress(buf, n, + [&](const char *buf2, size_t n2) { + return receiver(buf2, n2, off, len); + }); + }; + return callback(std::move(out)); + } else { + status = 500; + return false; + } + } + } + + ContentReceiverWithProgress out = [&](const char *buf, size_t n, uint64_t off, + uint64_t len) { + return receiver(buf, n, off, len); + }; + return callback(std::move(out)); +} + +template +bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, + Progress progress, ContentReceiverWithProgress receiver, + bool decompress) { + return prepare_content_receiver( + x, status, std::move(receiver), decompress, + [&](const ContentReceiverWithProgress &out) { + auto ret = true; + auto exceed_payload_max_length = false; + + if (is_chunked_transfer_encoding(x.headers)) { + ret = read_content_chunked(strm, out); + } else if (!has_header(x.headers, "Content-Length")) { + ret = read_content_without_length(strm, out); + } else { + auto len = get_header_value(x.headers, "Content-Length"); + if (len > payload_max_length) { + exceed_payload_max_length = true; + skip_content_with_length(strm, len); + ret = false; + } else if (len > 0) { + ret = read_content_with_length(strm, len, std::move(progress), out); + } + } + + if (!ret) { status = exceed_payload_max_length ? 413 : 400; } + return ret; + }); +} // namespace detail + +inline ssize_t write_headers(Stream &strm, const Headers &headers) { + ssize_t write_len = 0; + for (const auto &x : headers) { + auto len = + strm.write_format("%s: %s\r\n", x.first.c_str(), x.second.c_str()); + if (len < 0) { return len; } + write_len += len; + } + auto len = strm.write("\r\n"); + if (len < 0) { return len; } + write_len += len; + return write_len; +} + +inline bool write_data(Stream &strm, const char *d, size_t l) { + size_t offset = 0; + while (offset < l) { + auto length = strm.write(d + offset, l - offset); + if (length < 0) { return false; } + offset += static_cast(length); + } + return true; +} + +template +inline bool write_content(Stream &strm, const ContentProvider &content_provider, + size_t offset, size_t length, T is_shutting_down, + Error &error) { + size_t end_offset = offset + length; + auto ok = true; + DataSink data_sink; + + data_sink.write = [&](const char *d, size_t l) -> bool { + if (ok) { + if (write_data(strm, d, l)) { + offset += l; + } else { + ok = false; + } + } + return ok; + }; + + data_sink.is_writable = [&](void) { return ok && strm.is_writable(); }; + + while (offset < end_offset && !is_shutting_down()) { + if (!content_provider(offset, end_offset - offset, data_sink)) { + error = Error::Canceled; + return false; + } + if (!ok) { + error = Error::Write; + return false; + } + } + + error = Error::Success; + return true; +} + +template +inline bool write_content(Stream &strm, const ContentProvider &content_provider, + size_t offset, size_t length, + const T &is_shutting_down) { + auto error = Error::Success; + return write_content(strm, content_provider, offset, length, is_shutting_down, + error); +} + +template +inline bool +write_content_without_length(Stream &strm, + const ContentProvider &content_provider, + const T &is_shutting_down) { + size_t offset = 0; + auto data_available = true; + auto ok = true; + DataSink data_sink; + + data_sink.write = [&](const char *d, size_t l) -> bool { + if (ok) { + offset += l; + if (!write_data(strm, d, l)) { ok = false; } + } + return ok; + }; + + data_sink.done = [&](void) { data_available = false; }; + + data_sink.is_writable = [&](void) { return ok && strm.is_writable(); }; + + while (data_available && !is_shutting_down()) { + if (!content_provider(offset, 0, data_sink)) { return false; } + if (!ok) { return false; } + } + return true; +} + +template +inline bool +write_content_chunked(Stream &strm, const ContentProvider &content_provider, + const T &is_shutting_down, U &compressor, Error &error) { + size_t offset = 0; + auto data_available = true; + auto ok = true; + DataSink data_sink; + + data_sink.write = [&](const char *d, size_t l) -> bool { + if (ok) { + data_available = l > 0; + offset += l; + + std::string payload; + if (compressor.compress(d, l, false, + [&](const char *data, size_t data_len) { + payload.append(data, data_len); + return true; + })) { + if (!payload.empty()) { + // Emit chunked response header and footer for each chunk + auto chunk = + from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; + if (!write_data(strm, chunk.data(), chunk.size())) { ok = false; } + } + } else { + ok = false; + } + } + return ok; + }; + + data_sink.done = [&](void) { + if (!ok) { return; } + + data_available = false; + + std::string payload; + if (!compressor.compress(nullptr, 0, true, + [&](const char *data, size_t data_len) { + payload.append(data, data_len); + return true; + })) { + ok = false; + return; + } + + if (!payload.empty()) { + // Emit chunked response header and footer for each chunk + auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; + if (!write_data(strm, chunk.data(), chunk.size())) { + ok = false; + return; + } + } + + static const std::string done_marker("0\r\n\r\n"); + if (!write_data(strm, done_marker.data(), done_marker.size())) { + ok = false; + } + }; + + data_sink.is_writable = [&](void) { return ok && strm.is_writable(); }; + + while (data_available && !is_shutting_down()) { + if (!content_provider(offset, 0, data_sink)) { + error = Error::Canceled; + return false; + } + if (!ok) { + error = Error::Write; + return false; + } + } + + error = Error::Success; + return true; +} + +template +inline bool write_content_chunked(Stream &strm, + const ContentProvider &content_provider, + const T &is_shutting_down, U &compressor) { + auto error = Error::Success; + return write_content_chunked(strm, content_provider, is_shutting_down, + compressor, error); +} + +template +inline bool redirect(T &cli, Request &req, Response &res, + const std::string &path, const std::string &location, + Error &error) { + Request new_req = req; + new_req.path = path; + new_req.redirect_count_ -= 1; + + if (res.status == 303 && (req.method != "GET" && req.method != "HEAD")) { + new_req.method = "GET"; + new_req.body.clear(); + new_req.headers.clear(); + } + + Response new_res; + + auto ret = cli.send(new_req, new_res, error); + if (ret) { + req = new_req; + res = new_res; + res.location = location; + } + return ret; +} + +inline std::string params_to_query_str(const Params ¶ms) { + std::string query; + + for (auto it = params.begin(); it != params.end(); ++it) { + if (it != params.begin()) { query += "&"; } + query += it->first; + query += "="; + query += encode_query_param(it->second); + } + return query; +} + +inline void parse_query_text(const std::string &s, Params ¶ms) { + std::set cache; + split(s.data(), s.data() + s.size(), '&', [&](const char *b, const char *e) { + std::string kv(b, e); + if (cache.find(kv) != cache.end()) { return; } + cache.insert(kv); + + std::string key; + std::string val; + split(b, e, '=', [&](const char *b2, const char *e2) { + if (key.empty()) { + key.assign(b2, e2); + } else { + val.assign(b2, e2); + } + }); + + if (!key.empty()) { + params.emplace(decode_url(key, true), decode_url(val, true)); + } + }); +} + +inline bool parse_multipart_boundary(const std::string &content_type, + std::string &boundary) { + auto pos = content_type.find("boundary="); + if (pos == std::string::npos) { return false; } + boundary = content_type.substr(pos + 9); + if (boundary.length() >= 2 && boundary.front() == '"' && + boundary.back() == '"') { + boundary = boundary.substr(1, boundary.size() - 2); + } + return !boundary.empty(); +} + +#ifdef CPPHTTPLIB_NO_EXCEPTIONS +inline bool parse_range_header(const std::string &s, Ranges &ranges) { +#else +inline bool parse_range_header(const std::string &s, Ranges &ranges) try { +#endif + static auto re_first_range = std::regex(R"(bytes=(\d*-\d*(?:,\s*\d*-\d*)*))"); + std::smatch m; + if (std::regex_match(s, m, re_first_range)) { + auto pos = static_cast(m.position(1)); + auto len = static_cast(m.length(1)); + bool all_valid_ranges = true; + split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) { + if (!all_valid_ranges) return; + static auto re_another_range = std::regex(R"(\s*(\d*)-(\d*))"); + std::cmatch cm; + if (std::regex_match(b, e, cm, re_another_range)) { + ssize_t first = -1; + if (!cm.str(1).empty()) { + first = static_cast(std::stoll(cm.str(1))); + } + + ssize_t last = -1; + if (!cm.str(2).empty()) { + last = static_cast(std::stoll(cm.str(2))); + } + + if (first != -1 && last != -1 && first > last) { + all_valid_ranges = false; + return; + } + ranges.emplace_back(std::make_pair(first, last)); + } + }); + return all_valid_ranges; + } + return false; +#ifdef CPPHTTPLIB_NO_EXCEPTIONS +} +#else +} catch (...) { return false; } +#endif + +class MultipartFormDataParser { +public: + MultipartFormDataParser() = default; + + void set_boundary(std::string &&boundary) { + boundary_ = boundary; + dash_boundary_crlf_ = dash_ + boundary_ + crlf_; + crlf_dash_boundary_ = crlf_ + dash_ + boundary_; + } + + bool is_valid() const { return is_valid_; } + + bool parse(const char *buf, size_t n, const ContentReceiver &content_callback, + const MultipartContentHeader &header_callback) { + + // TODO: support 'filename*' + static const std::regex re_content_disposition( + R"~(^Content-Disposition:\s*form-data;\s*name="(.*?)"(?:;\s*filename="(.*?)")?(?:;\s*filename\*=\S+)?\s*$)~", + std::regex_constants::icase); + + buf_append(buf, n); + + while (buf_size() > 0) { + switch (state_) { + case 0: { // Initial boundary + buf_erase(buf_find(dash_boundary_crlf_)); + if (dash_boundary_crlf_.size() > buf_size()) { return true; } + if (!buf_start_with(dash_boundary_crlf_)) { return false; } + buf_erase(dash_boundary_crlf_.size()); + state_ = 1; + break; + } + case 1: { // New entry + clear_file_info(); + state_ = 2; + break; + } + case 2: { // Headers + auto pos = buf_find(crlf_); + if (pos > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + while (pos < buf_size()) { + // Empty line + if (pos == 0) { + if (!header_callback(file_)) { + is_valid_ = false; + return false; + } + buf_erase(crlf_.size()); + state_ = 3; + break; + } + + static const std::string header_name = "content-type:"; + const auto header = buf_head(pos); + if (start_with_case_ignore(header, header_name)) { + file_.content_type = trim_copy(header.substr(header_name.size())); + } else { + std::smatch m; + if (std::regex_match(header, m, re_content_disposition)) { + file_.name = m[1]; + file_.filename = m[2]; + } + } + buf_erase(pos + crlf_.size()); + pos = buf_find(crlf_); + } + if (state_ != 3) { return true; } + break; + } + case 3: { // Body + if (crlf_dash_boundary_.size() > buf_size()) { return true; } + auto pos = buf_find(crlf_dash_boundary_); + if (pos < buf_size()) { + if (!content_callback(buf_data(), pos)) { + is_valid_ = false; + return false; + } + buf_erase(pos + crlf_dash_boundary_.size()); + state_ = 4; + } else { + auto len = buf_size() - crlf_dash_boundary_.size(); + if (len > 0) { + if (!content_callback(buf_data(), len)) { + is_valid_ = false; + return false; + } + buf_erase(len); + } + return true; + } + break; + } + case 4: { // Boundary + if (crlf_.size() > buf_size()) { return true; } + if (buf_start_with(crlf_)) { + buf_erase(crlf_.size()); + state_ = 1; + } else { + if (dash_crlf_.size() > buf_size()) { return true; } + if (buf_start_with(dash_crlf_)) { + buf_erase(dash_crlf_.size()); + is_valid_ = true; + buf_erase(buf_size()); // Remove epilogue + } else { + return true; + } + } + break; + } + } + } + + return true; + } + +private: + void clear_file_info() { + file_.name.clear(); + file_.filename.clear(); + file_.content_type.clear(); + } + + bool start_with_case_ignore(const std::string &a, + const std::string &b) const { + if (a.size() < b.size()) { return false; } + for (size_t i = 0; i < b.size(); i++) { + if (::tolower(a[i]) != ::tolower(b[i])) { return false; } + } + return true; + } + + const std::string dash_ = "--"; + const std::string crlf_ = "\r\n"; + const std::string dash_crlf_ = "--\r\n"; + std::string boundary_; + std::string dash_boundary_crlf_; + std::string crlf_dash_boundary_; + + size_t state_ = 0; + bool is_valid_ = false; + MultipartFormData file_; + + // Buffer + bool start_with(const std::string &a, size_t spos, size_t epos, + const std::string &b) const { + if (epos - spos < b.size()) { return false; } + for (size_t i = 0; i < b.size(); i++) { + if (a[i + spos] != b[i]) { return false; } + } + return true; + } + + size_t buf_size() const { return buf_epos_ - buf_spos_; } + + const char *buf_data() const { return &buf_[buf_spos_]; } + + std::string buf_head(size_t l) const { return buf_.substr(buf_spos_, l); } + + bool buf_start_with(const std::string &s) const { + return start_with(buf_, buf_spos_, buf_epos_, s); + } + + size_t buf_find(const std::string &s) const { + auto c = s.front(); + + size_t off = buf_spos_; + while (off < buf_epos_) { + auto pos = off; + while (true) { + if (pos == buf_epos_) { return buf_size(); } + if (buf_[pos] == c) { break; } + pos++; + } + + auto remaining_size = buf_epos_ - pos; + if (s.size() > remaining_size) { return buf_size(); } + + if (start_with(buf_, pos, buf_epos_, s)) { return pos - buf_spos_; } + + off = pos + 1; + } + + return buf_size(); + } + + void buf_append(const char *data, size_t n) { + auto remaining_size = buf_size(); + if (remaining_size > 0 && buf_spos_ > 0) { + for (size_t i = 0; i < remaining_size; i++) { + buf_[i] = buf_[buf_spos_ + i]; + } + } + buf_spos_ = 0; + buf_epos_ = remaining_size; + + if (remaining_size + n > buf_.size()) { buf_.resize(remaining_size + n); } + + for (size_t i = 0; i < n; i++) { + buf_[buf_epos_ + i] = data[i]; + } + buf_epos_ += n; + } + + void buf_erase(size_t size) { buf_spos_ += size; } + + std::string buf_; + size_t buf_spos_ = 0; + size_t buf_epos_ = 0; +}; + +inline std::string to_lower(const char *beg, const char *end) { + std::string out; + auto it = beg; + while (it != end) { + out += static_cast(::tolower(*it)); + it++; + } + return out; +} + +inline std::string make_multipart_data_boundary() { + static const char data[] = + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + // std::random_device might actually be deterministic on some + // platforms, but due to lack of support in the c++ standard library, + // doing better requires either some ugly hacks or breaking portability. + std::random_device seed_gen; + + // Request 128 bits of entropy for initialization + std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), seed_gen()}; + std::mt19937 engine(seed_sequence); + + std::string result = "--cpp-httplib-multipart-data-"; + + for (auto i = 0; i < 16; i++) { + result += data[engine() % (sizeof(data) - 1)]; + } + + return result; +} + +inline std::pair +get_range_offset_and_length(const Request &req, size_t content_length, + size_t index) { + auto r = req.ranges[index]; + + if (r.first == -1 && r.second == -1) { + return std::make_pair(0, content_length); + } + + auto slen = static_cast(content_length); + + if (r.first == -1) { + r.first = (std::max)(static_cast(0), slen - r.second); + r.second = slen - 1; + } + + if (r.second == -1) { r.second = slen - 1; } + return std::make_pair(r.first, static_cast(r.second - r.first) + 1); +} + +inline std::string make_content_range_header_field(size_t offset, size_t length, + size_t content_length) { + std::string field = "bytes "; + field += std::to_string(offset); + field += "-"; + field += std::to_string(offset + length - 1); + field += "/"; + field += std::to_string(content_length); + return field; +} + +template +bool process_multipart_ranges_data(const Request &req, Response &res, + const std::string &boundary, + const std::string &content_type, + SToken stoken, CToken ctoken, + Content content) { + for (size_t i = 0; i < req.ranges.size(); i++) { + ctoken("--"); + stoken(boundary); + ctoken("\r\n"); + if (!content_type.empty()) { + ctoken("Content-Type: "); + stoken(content_type); + ctoken("\r\n"); + } + + auto offsets = get_range_offset_and_length(req, res.body.size(), i); + auto offset = offsets.first; + auto length = offsets.second; + + ctoken("Content-Range: "); + stoken(make_content_range_header_field(offset, length, res.body.size())); + ctoken("\r\n"); + ctoken("\r\n"); + if (!content(offset, length)) { return false; } + ctoken("\r\n"); + } + + ctoken("--"); + stoken(boundary); + ctoken("--\r\n"); + + return true; +} + +inline bool make_multipart_ranges_data(const Request &req, Response &res, + const std::string &boundary, + const std::string &content_type, + std::string &data) { + return process_multipart_ranges_data( + req, res, boundary, content_type, + [&](const std::string &token) { data += token; }, + [&](const std::string &token) { data += token; }, + [&](size_t offset, size_t length) { + if (offset < res.body.size()) { + data += res.body.substr(offset, length); + return true; + } + return false; + }); +} + +inline size_t +get_multipart_ranges_data_length(const Request &req, Response &res, + const std::string &boundary, + const std::string &content_type) { + size_t data_length = 0; + + process_multipart_ranges_data( + req, res, boundary, content_type, + [&](const std::string &token) { data_length += token.size(); }, + [&](const std::string &token) { data_length += token.size(); }, + [&](size_t /*offset*/, size_t length) { + data_length += length; + return true; + }); + + return data_length; +} + +template +inline bool write_multipart_ranges_data(Stream &strm, const Request &req, + Response &res, + const std::string &boundary, + const std::string &content_type, + const T &is_shutting_down) { + return process_multipart_ranges_data( + req, res, boundary, content_type, + [&](const std::string &token) { strm.write(token); }, + [&](const std::string &token) { strm.write(token); }, + [&](size_t offset, size_t length) { + return write_content(strm, res.content_provider_, offset, length, + is_shutting_down); + }); +} + +inline std::pair +get_range_offset_and_length(const Request &req, const Response &res, + size_t index) { + auto r = req.ranges[index]; + + if (r.second == -1) { + r.second = static_cast(res.content_length_) - 1; + } + + return std::make_pair(r.first, r.second - r.first + 1); +} + +inline bool expect_content(const Request &req) { + if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" || + req.method == "PRI" || req.method == "DELETE") { + return true; + } + // TODO: check if Content-Length is set + return false; +} + +inline bool has_crlf(const std::string &s) { + auto p = s.c_str(); + while (*p) { + if (*p == '\r' || *p == '\n') { return true; } + p++; + } + return false; +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline std::string message_digest(const std::string &s, const EVP_MD *algo) { + auto context = std::unique_ptr( + EVP_MD_CTX_new(), EVP_MD_CTX_free); + + unsigned int hash_length = 0; + unsigned char hash[EVP_MAX_MD_SIZE]; + + EVP_DigestInit_ex(context.get(), algo, nullptr); + EVP_DigestUpdate(context.get(), s.c_str(), s.size()); + EVP_DigestFinal_ex(context.get(), hash, &hash_length); + + std::stringstream ss; + for (auto i = 0u; i < hash_length; ++i) { + ss << std::hex << std::setw(2) << std::setfill('0') + << (unsigned int)hash[i]; + } + + return ss.str(); +} + +inline std::string MD5(const std::string &s) { + return message_digest(s, EVP_md5()); +} + +inline std::string SHA_256(const std::string &s) { + return message_digest(s, EVP_sha256()); +} + +inline std::string SHA_512(const std::string &s) { + return message_digest(s, EVP_sha512()); +} +#endif + +#ifdef _WIN32 +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +// NOTE: This code came up with the following stackoverflow post: +// https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store +inline bool load_system_certs_on_windows(X509_STORE *store) { + auto hStore = CertOpenSystemStoreW((HCRYPTPROV_LEGACY)NULL, L"ROOT"); + + if (!hStore) { return false; } + + PCCERT_CONTEXT pContext = NULL; + while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != + nullptr) { + auto encoded_cert = + static_cast(pContext->pbCertEncoded); + + auto x509 = d2i_X509(NULL, &encoded_cert, pContext->cbCertEncoded); + if (x509) { + X509_STORE_add_cert(store, x509); + X509_free(x509); + } + } + + CertFreeCertificateContext(pContext); + CertCloseStore(hStore, 0); + + return true; +} +#endif + +class WSInit { +public: + WSInit() { + WSADATA wsaData; + if (WSAStartup(0x0002, &wsaData) == 0) is_valid_ = true; + } + + ~WSInit() { + if (is_valid_) WSACleanup(); + } + + bool is_valid_ = false; +}; + +static WSInit wsinit_; +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline std::pair make_digest_authentication_header( + const Request &req, const std::map &auth, + size_t cnonce_count, const std::string &cnonce, const std::string &username, + const std::string &password, bool is_proxy = false) { + std::string nc; + { + std::stringstream ss; + ss << std::setfill('0') << std::setw(8) << std::hex << cnonce_count; + nc = ss.str(); + } + + std::string qop; + if (auth.find("qop") != auth.end()) { + qop = auth.at("qop"); + if (qop.find("auth-int") != std::string::npos) { + qop = "auth-int"; + } else if (qop.find("auth") != std::string::npos) { + qop = "auth"; + } else { + qop.clear(); + } + } + + std::string algo = "MD5"; + if (auth.find("algorithm") != auth.end()) { algo = auth.at("algorithm"); } + + std::string response; + { + auto H = algo == "SHA-256" ? detail::SHA_256 + : algo == "SHA-512" ? detail::SHA_512 + : detail::MD5; + + auto A1 = username + ":" + auth.at("realm") + ":" + password; + + auto A2 = req.method + ":" + req.path; + if (qop == "auth-int") { A2 += ":" + H(req.body); } + + if (qop.empty()) { + response = H(H(A1) + ":" + auth.at("nonce") + ":" + H(A2)); + } else { + response = H(H(A1) + ":" + auth.at("nonce") + ":" + nc + ":" + cnonce + + ":" + qop + ":" + H(A2)); + } + } + + auto opaque = (auth.find("opaque") != auth.end()) ? auth.at("opaque") : ""; + + auto field = "Digest username=\"" + username + "\", realm=\"" + + auth.at("realm") + "\", nonce=\"" + auth.at("nonce") + + "\", uri=\"" + req.path + "\", algorithm=" + algo + + (qop.empty() ? ", response=\"" + : ", qop=" + qop + ", nc=" + nc + ", cnonce=\"" + + cnonce + "\", response=\"") + + response + "\"" + + (opaque.empty() ? "" : ", opaque=\"" + opaque + "\""); + + auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; + return std::make_pair(key, field); +} +#endif + +inline bool parse_www_authenticate(const Response &res, + std::map &auth, + bool is_proxy) { + auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate"; + if (res.has_header(auth_key)) { + static auto re = std::regex(R"~((?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~"); + auto s = res.get_header_value(auth_key); + auto pos = s.find(' '); + if (pos != std::string::npos) { + auto type = s.substr(0, pos); + if (type == "Basic") { + return false; + } else if (type == "Digest") { + s = s.substr(pos + 1); + auto beg = std::sregex_iterator(s.begin(), s.end(), re); + for (auto i = beg; i != std::sregex_iterator(); ++i) { + auto m = *i; + auto key = s.substr(static_cast(m.position(1)), + static_cast(m.length(1))); + auto val = m.length(2) > 0 + ? s.substr(static_cast(m.position(2)), + static_cast(m.length(2))) + : s.substr(static_cast(m.position(3)), + static_cast(m.length(3))); + auth[key] = val; + } + return true; + } + } + } + return false; +} + +// https://stackoverflow.com/questions/440133/how-do-i-create-a-random-alpha-numeric-string-in-c/440240#answer-440240 +inline std::string random_string(size_t length) { + auto randchar = []() -> char { + const char charset[] = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + const size_t max_index = (sizeof(charset) - 1); + return charset[static_cast(std::rand()) % max_index]; + }; + std::string str(length, 0); + std::generate_n(str.begin(), length, randchar); + return str; +} + +class ContentProviderAdapter { +public: + explicit ContentProviderAdapter( + ContentProviderWithoutLength &&content_provider) + : content_provider_(content_provider) {} + + bool operator()(size_t offset, size_t, DataSink &sink) { + return content_provider_(offset, sink); + } + +private: + ContentProviderWithoutLength content_provider_; +}; + +} // namespace detail + +inline std::string hosted_at(const std::string &hostname) { + std::vector addrs; + hosted_at(hostname, addrs); + if (addrs.empty()) { return std::string(); } + return addrs[0]; +} + +inline void hosted_at(const std::string &hostname, + std::vector &addrs) { + struct addrinfo hints; + struct addrinfo *result; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = 0; + + if (getaddrinfo(hostname.c_str(), nullptr, &hints, &result)) { +#if defined __linux__ && !defined __ANDROID__ + res_init(); +#endif + return; + } + + for (auto rp = result; rp; rp = rp->ai_next) { + const auto &addr = + *reinterpret_cast(rp->ai_addr); + std::string ip; + int dummy = -1; + if (detail::get_remote_ip_and_port(addr, sizeof(struct sockaddr_storage), + ip, dummy)) { + addrs.push_back(ip); + } + } + + freeaddrinfo(result); +} + +inline std::string append_query_params(const std::string &path, + const Params ¶ms) { + std::string path_with_query = path; + const static std::regex re("[^?]+\\?.*"); + auto delm = std::regex_match(path, re) ? '&' : '?'; + path_with_query += delm + detail::params_to_query_str(params); + return path_with_query; +} + +// Header utilities +inline std::pair make_range_header(Ranges ranges) { + std::string field = "bytes="; + auto i = 0; + for (auto r : ranges) { + if (i != 0) { field += ", "; } + if (r.first != -1) { field += std::to_string(r.first); } + field += '-'; + if (r.second != -1) { field += std::to_string(r.second); } + i++; + } + return std::make_pair("Range", std::move(field)); +} + +inline std::pair +make_basic_authentication_header(const std::string &username, + const std::string &password, bool is_proxy) { + auto field = "Basic " + detail::base64_encode(username + ":" + password); + auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; + return std::make_pair(key, std::move(field)); +} + +inline std::pair +make_bearer_token_authentication_header(const std::string &token, + bool is_proxy = false) { + auto field = "Bearer " + token; + auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; + return std::make_pair(key, std::move(field)); +} + +// Request implementation +inline bool Request::has_header(const std::string &key) const { + return detail::has_header(headers, key); +} + +inline std::string Request::get_header_value(const std::string &key, + size_t id) const { + return detail::get_header_value(headers, key, id, ""); +} + +inline size_t Request::get_header_value_count(const std::string &key) const { + auto r = headers.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +inline void Request::set_header(const std::string &key, + const std::string &val) { + if (!detail::has_crlf(key) && !detail::has_crlf(val)) { + headers.emplace(key, val); + } +} + +inline bool Request::has_param(const std::string &key) const { + return params.find(key) != params.end(); +} + +inline std::string Request::get_param_value(const std::string &key, + size_t id) const { + auto rng = params.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second; } + return std::string(); +} + +inline size_t Request::get_param_value_count(const std::string &key) const { + auto r = params.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +inline bool Request::is_multipart_form_data() const { + const auto &content_type = get_header_value("Content-Type"); + return !content_type.rfind("multipart/form-data", 0); +} + +inline bool Request::has_file(const std::string &key) const { + return files.find(key) != files.end(); +} + +inline MultipartFormData Request::get_file_value(const std::string &key) const { + auto it = files.find(key); + if (it != files.end()) { return it->second; } + return MultipartFormData(); +} + +// Response implementation +inline bool Response::has_header(const std::string &key) const { + return headers.find(key) != headers.end(); +} + +inline std::string Response::get_header_value(const std::string &key, + size_t id) const { + return detail::get_header_value(headers, key, id, ""); +} + +inline size_t Response::get_header_value_count(const std::string &key) const { + auto r = headers.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +inline void Response::set_header(const std::string &key, + const std::string &val) { + if (!detail::has_crlf(key) && !detail::has_crlf(val)) { + headers.emplace(key, val); + } +} + +inline void Response::set_redirect(const std::string &url, int stat) { + if (!detail::has_crlf(url)) { + set_header("Location", url); + if (300 <= stat && stat < 400) { + this->status = stat; + } else { + this->status = 302; + } + } +} + +inline void Response::set_content(const char *s, size_t n, + const std::string &content_type) { + body.assign(s, n); + + auto rng = headers.equal_range("Content-Type"); + headers.erase(rng.first, rng.second); + set_header("Content-Type", content_type); +} + +inline void Response::set_content(const std::string &s, + const std::string &content_type) { + set_content(s.data(), s.size(), content_type); +} + +inline void Response::set_content_provider( + size_t in_length, const std::string &content_type, ContentProvider provider, + ContentProviderResourceReleaser resource_releaser) { + assert(in_length > 0); + set_header("Content-Type", content_type); + content_length_ = in_length; + content_provider_ = std::move(provider); + content_provider_resource_releaser_ = resource_releaser; + is_chunked_content_provider_ = false; +} + +inline void Response::set_content_provider( + const std::string &content_type, ContentProviderWithoutLength provider, + ContentProviderResourceReleaser resource_releaser) { + set_header("Content-Type", content_type); + content_length_ = 0; + content_provider_ = detail::ContentProviderAdapter(std::move(provider)); + content_provider_resource_releaser_ = resource_releaser; + is_chunked_content_provider_ = false; +} + +inline void Response::set_chunked_content_provider( + const std::string &content_type, ContentProviderWithoutLength provider, + ContentProviderResourceReleaser resource_releaser) { + set_header("Content-Type", content_type); + content_length_ = 0; + content_provider_ = detail::ContentProviderAdapter(std::move(provider)); + content_provider_resource_releaser_ = resource_releaser; + is_chunked_content_provider_ = true; +} + +// Result implementation +inline bool Result::has_request_header(const std::string &key) const { + return request_headers_.find(key) != request_headers_.end(); +} + +inline std::string Result::get_request_header_value(const std::string &key, + size_t id) const { + return detail::get_header_value(request_headers_, key, id, ""); +} + +inline size_t +Result::get_request_header_value_count(const std::string &key) const { + auto r = request_headers_.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +// Stream implementation +inline ssize_t Stream::write(const char *ptr) { + return write(ptr, strlen(ptr)); +} + +inline ssize_t Stream::write(const std::string &s) { + return write(s.data(), s.size()); +} + +namespace detail { + +// Socket stream implementation +inline SocketStream::SocketStream(socket_t sock, time_t read_timeout_sec, + time_t read_timeout_usec, + time_t write_timeout_sec, + time_t write_timeout_usec) + : sock_(sock), read_timeout_sec_(read_timeout_sec), + read_timeout_usec_(read_timeout_usec), + write_timeout_sec_(write_timeout_sec), + write_timeout_usec_(write_timeout_usec), read_buff_(read_buff_size_, 0) {} + +inline SocketStream::~SocketStream() {} + +inline bool SocketStream::is_readable() const { + return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; +} + +inline bool SocketStream::is_writable() const { + return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0; +} + +inline ssize_t SocketStream::read(char *ptr, size_t size) { +#ifdef _WIN32 + size = + (std::min)(size, static_cast((std::numeric_limits::max)())); +#else + size = (std::min)(size, + static_cast((std::numeric_limits::max)())); +#endif + + if (read_buff_off_ < read_buff_content_size_) { + auto remaining_size = read_buff_content_size_ - read_buff_off_; + if (size <= remaining_size) { + memcpy(ptr, read_buff_.data() + read_buff_off_, size); + read_buff_off_ += size; + return static_cast(size); + } else { + memcpy(ptr, read_buff_.data() + read_buff_off_, remaining_size); + read_buff_off_ += remaining_size; + return static_cast(remaining_size); + } + } + + if (!is_readable()) { return -1; } + + read_buff_off_ = 0; + read_buff_content_size_ = 0; + + if (size < read_buff_size_) { + auto n = read_socket(sock_, read_buff_.data(), read_buff_size_, + CPPHTTPLIB_RECV_FLAGS); + if (n <= 0) { + return n; + } else if (n <= static_cast(size)) { + memcpy(ptr, read_buff_.data(), static_cast(n)); + return n; + } else { + memcpy(ptr, read_buff_.data(), size); + read_buff_off_ = size; + read_buff_content_size_ = static_cast(n); + return static_cast(size); + } + } else { + return read_socket(sock_, ptr, size, CPPHTTPLIB_RECV_FLAGS); + } +} + +inline ssize_t SocketStream::write(const char *ptr, size_t size) { + if (!is_writable()) { return -1; } + +#if defined(_WIN32) && !defined(_WIN64) + size = + (std::min)(size, static_cast((std::numeric_limits::max)())); +#endif + + return send_socket(sock_, ptr, size, CPPHTTPLIB_SEND_FLAGS); +} + +inline void SocketStream::get_remote_ip_and_port(std::string &ip, + int &port) const { + return detail::get_remote_ip_and_port(sock_, ip, port); +} + +inline socket_t SocketStream::socket() const { return sock_; } + +// Buffer stream implementation +inline bool BufferStream::is_readable() const { return true; } + +inline bool BufferStream::is_writable() const { return true; } + +inline ssize_t BufferStream::read(char *ptr, size_t size) { +#if defined(_MSC_VER) && _MSC_VER < 1910 + auto len_read = buffer._Copy_s(ptr, size, size, position); +#else + auto len_read = buffer.copy(ptr, size, position); +#endif + position += static_cast(len_read); + return static_cast(len_read); +} + +inline ssize_t BufferStream::write(const char *ptr, size_t size) { + buffer.append(ptr, size); + return static_cast(size); +} + +inline void BufferStream::get_remote_ip_and_port(std::string & /*ip*/, + int & /*port*/) const {} + +inline socket_t BufferStream::socket() const { return 0; } + +inline const std::string &BufferStream::get_buffer() const { return buffer; } + +} // namespace detail + +// HTTP server implementation +inline Server::Server() + : new_task_queue( + [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }), + svr_sock_(INVALID_SOCKET), is_running_(false) { +#ifndef _WIN32 + signal(SIGPIPE, SIG_IGN); +#endif +} + +inline Server::~Server() {} + +inline Server &Server::Get(const std::string &pattern, Handler handler) { + get_handlers_.push_back( + std::make_pair(std::regex(pattern), std::move(handler))); + return *this; +} + +inline Server &Server::Post(const std::string &pattern, Handler handler) { + post_handlers_.push_back( + std::make_pair(std::regex(pattern), std::move(handler))); + return *this; +} + +inline Server &Server::Post(const std::string &pattern, + HandlerWithContentReader handler) { + post_handlers_for_content_reader_.push_back( + std::make_pair(std::regex(pattern), std::move(handler))); + return *this; +} + +inline Server &Server::Put(const std::string &pattern, Handler handler) { + put_handlers_.push_back( + std::make_pair(std::regex(pattern), std::move(handler))); + return *this; +} + +inline Server &Server::Put(const std::string &pattern, + HandlerWithContentReader handler) { + put_handlers_for_content_reader_.push_back( + std::make_pair(std::regex(pattern), std::move(handler))); + return *this; +} + +inline Server &Server::Patch(const std::string &pattern, Handler handler) { + patch_handlers_.push_back( + std::make_pair(std::regex(pattern), std::move(handler))); + return *this; +} + +inline Server &Server::Patch(const std::string &pattern, + HandlerWithContentReader handler) { + patch_handlers_for_content_reader_.push_back( + std::make_pair(std::regex(pattern), std::move(handler))); + return *this; +} + +inline Server &Server::Delete(const std::string &pattern, Handler handler) { + delete_handlers_.push_back( + std::make_pair(std::regex(pattern), std::move(handler))); + return *this; +} + +inline Server &Server::Delete(const std::string &pattern, + HandlerWithContentReader handler) { + delete_handlers_for_content_reader_.push_back( + std::make_pair(std::regex(pattern), std::move(handler))); + return *this; +} + +inline Server &Server::Options(const std::string &pattern, Handler handler) { + options_handlers_.push_back( + std::make_pair(std::regex(pattern), std::move(handler))); + return *this; +} + +inline bool Server::set_base_dir(const std::string &dir, + const std::string &mount_point) { + return set_mount_point(mount_point, dir); +} + +inline bool Server::set_mount_point(const std::string &mount_point, + const std::string &dir, Headers headers) { + if (detail::is_dir(dir)) { + std::string mnt = !mount_point.empty() ? mount_point : "/"; + if (!mnt.empty() && mnt[0] == '/') { + base_dirs_.push_back({mnt, dir, std::move(headers)}); + return true; + } + } + return false; +} + +inline bool Server::remove_mount_point(const std::string &mount_point) { + for (auto it = base_dirs_.begin(); it != base_dirs_.end(); ++it) { + if (it->mount_point == mount_point) { + base_dirs_.erase(it); + return true; + } + } + return false; +} + +inline Server & +Server::set_file_extension_and_mimetype_mapping(const std::string &ext, + const std::string &mime) { + file_extension_and_mimetype_map_[ext] = mime; + return *this; +} + +inline Server &Server::set_file_request_handler(Handler handler) { + file_request_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_error_handler(HandlerWithResponse handler) { + error_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_error_handler(Handler handler) { + error_handler_ = [handler](const Request &req, Response &res) { + handler(req, res); + return HandlerResponse::Handled; + }; + return *this; +} + +inline Server &Server::set_exception_handler(ExceptionHandler handler) { + exception_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_pre_routing_handler(HandlerWithResponse handler) { + pre_routing_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_post_routing_handler(Handler handler) { + post_routing_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_logger(Logger logger) { + logger_ = std::move(logger); + return *this; +} + +inline Server & +Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) { + expect_100_continue_handler_ = std::move(handler); + + return *this; +} + +inline Server &Server::set_address_family(int family) { + address_family_ = family; + return *this; +} + +inline Server &Server::set_tcp_nodelay(bool on) { + tcp_nodelay_ = on; + return *this; +} + +inline Server &Server::set_socket_options(SocketOptions socket_options) { + socket_options_ = std::move(socket_options); + return *this; +} + +inline Server &Server::set_default_headers(Headers headers) { + default_headers_ = std::move(headers); + return *this; +} + +inline Server &Server::set_keep_alive_max_count(size_t count) { + keep_alive_max_count_ = count; + return *this; +} + +inline Server &Server::set_keep_alive_timeout(time_t sec) { + keep_alive_timeout_sec_ = sec; + return *this; +} + +inline Server &Server::set_read_timeout(time_t sec, time_t usec) { + read_timeout_sec_ = sec; + read_timeout_usec_ = usec; + return *this; +} + +inline Server &Server::set_write_timeout(time_t sec, time_t usec) { + write_timeout_sec_ = sec; + write_timeout_usec_ = usec; + return *this; +} + +inline Server &Server::set_idle_interval(time_t sec, time_t usec) { + idle_interval_sec_ = sec; + idle_interval_usec_ = usec; + return *this; +} + +inline Server &Server::set_payload_max_length(size_t length) { + payload_max_length_ = length; + return *this; +} + +inline bool Server::bind_to_port(const std::string &host, int port, + int socket_flags) { + if (bind_internal(host, port, socket_flags) < 0) return false; + return true; +} +inline int Server::bind_to_any_port(const std::string &host, int socket_flags) { + return bind_internal(host, 0, socket_flags); +} + +inline bool Server::listen_after_bind() { return listen_internal(); } + +inline bool Server::listen(const std::string &host, int port, + int socket_flags) { + return bind_to_port(host, port, socket_flags) && listen_internal(); +} + +inline bool Server::is_running() const { return is_running_; } + +inline void Server::stop() { + if (is_running_) { + assert(svr_sock_ != INVALID_SOCKET); + std::atomic sock(svr_sock_.exchange(INVALID_SOCKET)); + detail::shutdown_socket(sock); + detail::close_socket(sock); + } +} + +inline bool Server::parse_request_line(const char *s, Request &req) { + auto len = strlen(s); + if (len < 2 || s[len - 2] != '\r' || s[len - 1] != '\n') { return false; } + len -= 2; + + { + size_t count = 0; + + detail::split(s, s + len, ' ', [&](const char *b, const char *e) { + switch (count) { + case 0: req.method = std::string(b, e); break; + case 1: req.target = std::string(b, e); break; + case 2: req.version = std::string(b, e); break; + default: break; + } + count++; + }); + + if (count != 3) { return false; } + } + + static const std::set methods{ + "GET", "HEAD", "POST", "PUT", "DELETE", + "CONNECT", "OPTIONS", "TRACE", "PATCH", "PRI"}; + + if (methods.find(req.method) == methods.end()) { return false; } + + if (req.version != "HTTP/1.1" && req.version != "HTTP/1.0") { return false; } + + { + // Skip URL fragment + for (size_t i = 0; i < req.target.size(); i++) { + if (req.target[i] == '#') { + req.target.erase(i); + break; + } + } + + size_t count = 0; + + detail::split(req.target.data(), req.target.data() + req.target.size(), '?', + [&](const char *b, const char *e) { + switch (count) { + case 0: + req.path = detail::decode_url(std::string(b, e), false); + break; + case 1: { + if (e - b > 0) { + detail::parse_query_text(std::string(b, e), req.params); + } + break; + } + default: break; + } + count++; + }); + + if (count > 2) { return false; } + } + + return true; +} + +inline bool Server::write_response(Stream &strm, bool close_connection, + const Request &req, Response &res) { + return write_response_core(strm, close_connection, req, res, false); +} + +inline bool Server::write_response_with_content(Stream &strm, + bool close_connection, + const Request &req, + Response &res) { + return write_response_core(strm, close_connection, req, res, true); +} + +inline bool Server::write_response_core(Stream &strm, bool close_connection, + const Request &req, Response &res, + bool need_apply_ranges) { + assert(res.status != -1); + + if (400 <= res.status && error_handler_ && + error_handler_(req, res) == HandlerResponse::Handled) { + need_apply_ranges = true; + } + + std::string content_type; + std::string boundary; + if (need_apply_ranges) { apply_ranges(req, res, content_type, boundary); } + + // Prepare additional headers + if (close_connection || req.get_header_value("Connection") == "close") { + res.set_header("Connection", "close"); + } else { + std::stringstream ss; + ss << "timeout=" << keep_alive_timeout_sec_ + << ", max=" << keep_alive_max_count_; + res.set_header("Keep-Alive", ss.str()); + } + + if (!res.has_header("Content-Type") && + (!res.body.empty() || res.content_length_ > 0 || res.content_provider_)) { + res.set_header("Content-Type", "text/plain"); + } + + if (!res.has_header("Content-Length") && res.body.empty() && + !res.content_length_ && !res.content_provider_) { + res.set_header("Content-Length", "0"); + } + + if (!res.has_header("Accept-Ranges") && req.method == "HEAD") { + res.set_header("Accept-Ranges", "bytes"); + } + + if (post_routing_handler_) { post_routing_handler_(req, res); } + + // Response line and headers + { + detail::BufferStream bstrm; + + if (!bstrm.write_format("HTTP/1.1 %d %s\r\n", res.status, + detail::status_message(res.status))) { + return false; + } + + if (!detail::write_headers(bstrm, res.headers)) { return false; } + + // Flush buffer + auto &data = bstrm.get_buffer(); + detail::write_data(strm, data.data(), data.size()); + } + + // Body + auto ret = true; + if (req.method != "HEAD") { + if (!res.body.empty()) { + if (!detail::write_data(strm, res.body.data(), res.body.size())) { + ret = false; + } + } else if (res.content_provider_) { + if (write_content_with_provider(strm, req, res, boundary, content_type)) { + res.content_provider_success_ = true; + } else { + res.content_provider_success_ = false; + ret = false; + } + } + } + + // Log + if (logger_) { logger_(req, res); } + + return ret; +} + +inline bool +Server::write_content_with_provider(Stream &strm, const Request &req, + Response &res, const std::string &boundary, + const std::string &content_type) { + auto is_shutting_down = [this]() { + return this->svr_sock_ == INVALID_SOCKET; + }; + + if (res.content_length_ > 0) { + if (req.ranges.empty()) { + return detail::write_content(strm, res.content_provider_, 0, + res.content_length_, is_shutting_down); + } else if (req.ranges.size() == 1) { + auto offsets = + detail::get_range_offset_and_length(req, res.content_length_, 0); + auto offset = offsets.first; + auto length = offsets.second; + return detail::write_content(strm, res.content_provider_, offset, length, + is_shutting_down); + } else { + return detail::write_multipart_ranges_data( + strm, req, res, boundary, content_type, is_shutting_down); + } + } else { + if (res.is_chunked_content_provider_) { + auto type = detail::encoding_type(req, res); + + std::unique_ptr compressor; + if (type == detail::EncodingType::Gzip) { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + compressor = detail::make_unique(); +#endif + } else if (type == detail::EncodingType::Brotli) { +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + compressor = detail::make_unique(); +#endif + } else { + compressor = detail::make_unique(); + } + assert(compressor != nullptr); + + return detail::write_content_chunked(strm, res.content_provider_, + is_shutting_down, *compressor); + } else { + return detail::write_content_without_length(strm, res.content_provider_, + is_shutting_down); + } + } +} + +inline bool Server::read_content(Stream &strm, Request &req, Response &res) { + MultipartFormDataMap::iterator cur; + if (read_content_core( + strm, req, res, + // Regular + [&](const char *buf, size_t n) { + if (req.body.size() + n > req.body.max_size()) { return false; } + req.body.append(buf, n); + return true; + }, + // Multipart + [&](const MultipartFormData &file) { + cur = req.files.emplace(file.name, file); + return true; + }, + [&](const char *buf, size_t n) { + auto &content = cur->second.content; + if (content.size() + n > content.max_size()) { return false; } + content.append(buf, n); + return true; + })) { + const auto &content_type = req.get_header_value("Content-Type"); + if (!content_type.find("application/x-www-form-urlencoded")) { + if (req.body.size() > CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH) { + res.status = 413; // NOTE: should be 414? + return false; + } + detail::parse_query_text(req.body, req.params); + } + return true; + } + return false; +} + +inline bool Server::read_content_with_content_receiver( + Stream &strm, Request &req, Response &res, ContentReceiver receiver, + MultipartContentHeader multipart_header, + ContentReceiver multipart_receiver) { + return read_content_core(strm, req, res, std::move(receiver), + std::move(multipart_header), + std::move(multipart_receiver)); +} + +inline bool Server::read_content_core(Stream &strm, Request &req, Response &res, + ContentReceiver receiver, + MultipartContentHeader mulitpart_header, + ContentReceiver multipart_receiver) { + detail::MultipartFormDataParser multipart_form_data_parser; + ContentReceiverWithProgress out; + + if (req.is_multipart_form_data()) { + const auto &content_type = req.get_header_value("Content-Type"); + std::string boundary; + if (!detail::parse_multipart_boundary(content_type, boundary)) { + res.status = 400; + return false; + } + + multipart_form_data_parser.set_boundary(std::move(boundary)); + out = [&](const char *buf, size_t n, uint64_t /*off*/, uint64_t /*len*/) { + /* For debug + size_t pos = 0; + while (pos < n) { + auto read_size = (std::min)(1, n - pos); + auto ret = multipart_form_data_parser.parse( + buf + pos, read_size, multipart_receiver, mulitpart_header); + if (!ret) { return false; } + pos += read_size; + } + return true; + */ + return multipart_form_data_parser.parse(buf, n, multipart_receiver, + mulitpart_header); + }; + } else { + out = [receiver](const char *buf, size_t n, uint64_t /*off*/, + uint64_t /*len*/) { return receiver(buf, n); }; + } + + if (req.method == "DELETE" && !req.has_header("Content-Length")) { + return true; + } + + if (!detail::read_content(strm, req, payload_max_length_, res.status, nullptr, + out, true)) { + return false; + } + + if (req.is_multipart_form_data()) { + if (!multipart_form_data_parser.is_valid()) { + res.status = 400; + return false; + } + } + + return true; +} + +inline bool Server::handle_file_request(const Request &req, Response &res, + bool head) { + for (const auto &entry : base_dirs_) { + // Prefix match + if (!req.path.compare(0, entry.mount_point.size(), entry.mount_point)) { + std::string sub_path = "/" + req.path.substr(entry.mount_point.size()); + if (detail::is_valid_path(sub_path)) { + auto path = entry.base_dir + sub_path; + if (path.back() == '/') { path += "index.html"; } + + if (detail::is_file(path)) { + detail::read_file(path, res.body); + auto type = + detail::find_content_type(path, file_extension_and_mimetype_map_); + if (type) { res.set_header("Content-Type", type); } + for (const auto &kv : entry.headers) { + res.set_header(kv.first.c_str(), kv.second); + } + res.status = req.has_header("Range") ? 206 : 200; + if (!head && file_request_handler_) { + file_request_handler_(req, res); + } + return true; + } + } + } + } + return false; +} + +inline socket_t +Server::create_server_socket(const std::string &host, int port, + int socket_flags, + SocketOptions socket_options) const { + return detail::create_socket( + host, std::string(), port, address_family_, socket_flags, tcp_nodelay_, + std::move(socket_options), + [](socket_t sock, struct addrinfo &ai) -> bool { + if (::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { + return false; + } + if (::listen(sock, CPPHTTPLIB_LISTEN_BACKLOG)) { return false; } + return true; + }); +} + +inline int Server::bind_internal(const std::string &host, int port, + int socket_flags) { + if (!is_valid()) { return -1; } + + svr_sock_ = create_server_socket(host, port, socket_flags, socket_options_); + if (svr_sock_ == INVALID_SOCKET) { return -1; } + + if (port == 0) { + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); + if (getsockname(svr_sock_, reinterpret_cast(&addr), + &addr_len) == -1) { + return -1; + } + if (addr.ss_family == AF_INET) { + return ntohs(reinterpret_cast(&addr)->sin_port); + } else if (addr.ss_family == AF_INET6) { + return ntohs(reinterpret_cast(&addr)->sin6_port); + } else { + return -1; + } + } else { + return port; + } +} + +inline bool Server::listen_internal() { + auto ret = true; + is_running_ = true; + + { + std::unique_ptr task_queue(new_task_queue()); + + while (svr_sock_ != INVALID_SOCKET) { +#ifndef _WIN32 + if (idle_interval_sec_ > 0 || idle_interval_usec_ > 0) { +#endif + auto val = detail::select_read(svr_sock_, idle_interval_sec_, + idle_interval_usec_); + if (val == 0) { // Timeout + task_queue->on_idle(); + continue; + } +#ifndef _WIN32 + } +#endif + socket_t sock = accept(svr_sock_, nullptr, nullptr); + + if (sock == INVALID_SOCKET) { + if (errno == EMFILE) { + // The per-process limit of open file descriptors has been reached. + // Try to accept new connections after a short sleep. + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + continue; + } + if (svr_sock_ != INVALID_SOCKET) { + detail::close_socket(svr_sock_); + ret = false; + } else { + ; // The server socket was closed by user. + } + break; + } + + { +#ifdef _WIN32 + auto timeout = static_cast(read_timeout_sec_ * 1000 + + read_timeout_usec_ / 1000); + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, + sizeof(timeout)); +#else + timeval tv; + tv.tv_sec = static_cast(read_timeout_sec_); + tv.tv_usec = static_cast(read_timeout_usec_); + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)); +#endif + } + { + +#ifdef _WIN32 + auto timeout = static_cast(write_timeout_sec_ * 1000 + + write_timeout_usec_ / 1000); + setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, + sizeof(timeout)); +#else + timeval tv; + tv.tv_sec = static_cast(write_timeout_sec_); + tv.tv_usec = static_cast(write_timeout_usec_); + setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)); +#endif + } + +#if __cplusplus > 201703L + task_queue->enqueue([=, this]() { process_and_close_socket(sock); }); +#else + task_queue->enqueue([=]() { process_and_close_socket(sock); }); +#endif + } + + task_queue->shutdown(); + } + + is_running_ = false; + return ret; +} + +inline bool Server::routing(Request &req, Response &res, Stream &strm) { + if (pre_routing_handler_ && + pre_routing_handler_(req, res) == HandlerResponse::Handled) { + return true; + } + + // File handler + bool is_head_request = req.method == "HEAD"; + if ((req.method == "GET" || is_head_request) && + handle_file_request(req, res, is_head_request)) { + return true; + } + + if (detail::expect_content(req)) { + // Content reader handler + { + ContentReader reader( + [&](ContentReceiver receiver) { + return read_content_with_content_receiver( + strm, req, res, std::move(receiver), nullptr, nullptr); + }, + [&](MultipartContentHeader header, ContentReceiver receiver) { + return read_content_with_content_receiver(strm, req, res, nullptr, + std::move(header), + std::move(receiver)); + }); + + if (req.method == "POST") { + if (dispatch_request_for_content_reader( + req, res, std::move(reader), + post_handlers_for_content_reader_)) { + return true; + } + } else if (req.method == "PUT") { + if (dispatch_request_for_content_reader( + req, res, std::move(reader), + put_handlers_for_content_reader_)) { + return true; + } + } else if (req.method == "PATCH") { + if (dispatch_request_for_content_reader( + req, res, std::move(reader), + patch_handlers_for_content_reader_)) { + return true; + } + } else if (req.method == "DELETE") { + if (dispatch_request_for_content_reader( + req, res, std::move(reader), + delete_handlers_for_content_reader_)) { + return true; + } + } + } + + // Read content into `req.body` + if (!read_content(strm, req, res)) { return false; } + } + + // Regular handler + if (req.method == "GET" || req.method == "HEAD") { + return dispatch_request(req, res, get_handlers_); + } else if (req.method == "POST") { + return dispatch_request(req, res, post_handlers_); + } else if (req.method == "PUT") { + return dispatch_request(req, res, put_handlers_); + } else if (req.method == "DELETE") { + return dispatch_request(req, res, delete_handlers_); + } else if (req.method == "OPTIONS") { + return dispatch_request(req, res, options_handlers_); + } else if (req.method == "PATCH") { + return dispatch_request(req, res, patch_handlers_); + } + + res.status = 400; + return false; +} + +inline bool Server::dispatch_request(Request &req, Response &res, + const Handlers &handlers) { + for (const auto &x : handlers) { + const auto &pattern = x.first; + const auto &handler = x.second; + + if (std::regex_match(req.path, req.matches, pattern)) { + handler(req, res); + return true; + } + } + return false; +} + +inline void Server::apply_ranges(const Request &req, Response &res, + std::string &content_type, + std::string &boundary) { + if (req.ranges.size() > 1) { + boundary = detail::make_multipart_data_boundary(); + + auto it = res.headers.find("Content-Type"); + if (it != res.headers.end()) { + content_type = it->second; + res.headers.erase(it); + } + + res.headers.emplace("Content-Type", + "multipart/byteranges; boundary=" + boundary); + } + + auto type = detail::encoding_type(req, res); + + if (res.body.empty()) { + if (res.content_length_ > 0) { + size_t length = 0; + if (req.ranges.empty()) { + length = res.content_length_; + } else if (req.ranges.size() == 1) { + auto offsets = + detail::get_range_offset_and_length(req, res.content_length_, 0); + auto offset = offsets.first; + length = offsets.second; + auto content_range = detail::make_content_range_header_field( + offset, length, res.content_length_); + res.set_header("Content-Range", content_range); + } else { + length = detail::get_multipart_ranges_data_length(req, res, boundary, + content_type); + } + res.set_header("Content-Length", std::to_string(length)); + } else { + if (res.content_provider_) { + if (res.is_chunked_content_provider_) { + res.set_header("Transfer-Encoding", "chunked"); + if (type == detail::EncodingType::Gzip) { + res.set_header("Content-Encoding", "gzip"); + } else if (type == detail::EncodingType::Brotli) { + res.set_header("Content-Encoding", "br"); + } + } + } + } + } else { + if (req.ranges.empty()) { + ; + } else if (req.ranges.size() == 1) { + auto offsets = + detail::get_range_offset_and_length(req, res.body.size(), 0); + auto offset = offsets.first; + auto length = offsets.second; + auto content_range = detail::make_content_range_header_field( + offset, length, res.body.size()); + res.set_header("Content-Range", content_range); + if (offset < res.body.size()) { + res.body = res.body.substr(offset, length); + } else { + res.body.clear(); + res.status = 416; + } + } else { + std::string data; + if (detail::make_multipart_ranges_data(req, res, boundary, content_type, + data)) { + res.body.swap(data); + } else { + res.body.clear(); + res.status = 416; + } + } + + if (type != detail::EncodingType::None) { + std::unique_ptr compressor; + std::string content_encoding; + + if (type == detail::EncodingType::Gzip) { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + compressor = detail::make_unique(); + content_encoding = "gzip"; +#endif + } else if (type == detail::EncodingType::Brotli) { +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + compressor = detail::make_unique(); + content_encoding = "br"; +#endif + } + + if (compressor) { + std::string compressed; + if (compressor->compress(res.body.data(), res.body.size(), true, + [&](const char *data, size_t data_len) { + compressed.append(data, data_len); + return true; + })) { + res.body.swap(compressed); + res.set_header("Content-Encoding", content_encoding); + } + } + } + + auto length = std::to_string(res.body.size()); + res.set_header("Content-Length", length); + } +} + +inline bool Server::dispatch_request_for_content_reader( + Request &req, Response &res, ContentReader content_reader, + const HandlersForContentReader &handlers) { + for (const auto &x : handlers) { + const auto &pattern = x.first; + const auto &handler = x.second; + + if (std::regex_match(req.path, req.matches, pattern)) { + handler(req, res, content_reader); + return true; + } + } + return false; +} + +inline bool +Server::process_request(Stream &strm, bool close_connection, + bool &connection_closed, + const std::function &setup_request) { + std::array buf{}; + + detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); + + // Connection has been closed on client + if (!line_reader.getline()) { return false; } + + Request req; + Response res; + + res.version = "HTTP/1.1"; + + for (const auto &header : default_headers_) { + if (res.headers.find(header.first) == res.headers.end()) { + res.headers.insert(header); + } + } + +#ifdef _WIN32 + // TODO: Increase FD_SETSIZE statically (libzmq), dynamically (MySQL). +#else +#ifndef CPPHTTPLIB_USE_POLL + // Socket file descriptor exceeded FD_SETSIZE... + if (strm.socket() >= FD_SETSIZE) { + Headers dummy; + detail::read_headers(strm, dummy); + res.status = 500; + return write_response(strm, close_connection, req, res); + } +#endif +#endif + + // Check if the request URI doesn't exceed the limit + if (line_reader.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) { + Headers dummy; + detail::read_headers(strm, dummy); + res.status = 414; + return write_response(strm, close_connection, req, res); + } + + // Request line and headers + if (!parse_request_line(line_reader.ptr(), req) || + !detail::read_headers(strm, req.headers)) { + res.status = 400; + return write_response(strm, close_connection, req, res); + } + + if (req.get_header_value("Connection") == "close") { + connection_closed = true; + } + + if (req.version == "HTTP/1.0" && + req.get_header_value("Connection") != "Keep-Alive") { + connection_closed = true; + } + + strm.get_remote_ip_and_port(req.remote_addr, req.remote_port); + req.set_header("REMOTE_ADDR", req.remote_addr); + req.set_header("REMOTE_PORT", std::to_string(req.remote_port)); + + if (req.has_header("Range")) { + const auto &range_header_value = req.get_header_value("Range"); + if (!detail::parse_range_header(range_header_value, req.ranges)) { + res.status = 416; + return write_response(strm, close_connection, req, res); + } + } + + if (setup_request) { setup_request(req); } + + if (req.get_header_value("Expect") == "100-continue") { + auto status = 100; + if (expect_100_continue_handler_) { + status = expect_100_continue_handler_(req, res); + } + switch (status) { + case 100: + case 417: + strm.write_format("HTTP/1.1 %d %s\r\n\r\n", status, + detail::status_message(status)); + break; + default: return write_response(strm, close_connection, req, res); + } + } + + // Rounting + bool routed = false; +#ifdef CPPHTTPLIB_NO_EXCEPTIONS + routed = routing(req, res, strm); +#else + try { + routed = routing(req, res, strm); + } catch (std::exception &e) { + if (exception_handler_) { + auto ep = std::current_exception(); + exception_handler_(req, res, ep); + routed = true; + } else { + res.status = 500; + res.set_header("EXCEPTION_WHAT", e.what()); + } + } catch (...) { + if (exception_handler_) { + auto ep = std::current_exception(); + exception_handler_(req, res, ep); + routed = true; + } else { + res.status = 500; + res.set_header("EXCEPTION_WHAT", "UNKNOWN"); + } + } +#endif + + if (routed) { + if (res.status == -1) { res.status = req.ranges.empty() ? 200 : 206; } + return write_response_with_content(strm, close_connection, req, res); + } else { + if (res.status == -1) { res.status = 404; } + return write_response(strm, close_connection, req, res); + } +} + +inline bool Server::is_valid() const { return true; } + +inline bool Server::process_and_close_socket(socket_t sock) { + auto ret = detail::process_server_socket( + svr_sock_, sock, keep_alive_max_count_, keep_alive_timeout_sec_, + read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, + [this](Stream &strm, bool close_connection, bool &connection_closed) { + return process_request(strm, close_connection, connection_closed, + nullptr); + }); + + detail::shutdown_socket(sock); + detail::close_socket(sock); + return ret; +} + +// HTTP client implementation +inline ClientImpl::ClientImpl(const std::string &host) + : ClientImpl(host, 80, std::string(), std::string()) {} + +inline ClientImpl::ClientImpl(const std::string &host, int port) + : ClientImpl(host, port, std::string(), std::string()) {} + +inline ClientImpl::ClientImpl(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path) + : host_(host), port_(port), + host_and_port_(adjust_host_string(host) + ":" + std::to_string(port)), + client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} + +inline ClientImpl::~ClientImpl() { + std::lock_guard guard(socket_mutex_); + shutdown_socket(socket_); + close_socket(socket_); +} + +inline bool ClientImpl::is_valid() const { return true; } + +inline void ClientImpl::copy_settings(const ClientImpl &rhs) { + client_cert_path_ = rhs.client_cert_path_; + client_key_path_ = rhs.client_key_path_; + connection_timeout_sec_ = rhs.connection_timeout_sec_; + read_timeout_sec_ = rhs.read_timeout_sec_; + read_timeout_usec_ = rhs.read_timeout_usec_; + write_timeout_sec_ = rhs.write_timeout_sec_; + write_timeout_usec_ = rhs.write_timeout_usec_; + basic_auth_username_ = rhs.basic_auth_username_; + basic_auth_password_ = rhs.basic_auth_password_; + bearer_token_auth_token_ = rhs.bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + digest_auth_username_ = rhs.digest_auth_username_; + digest_auth_password_ = rhs.digest_auth_password_; +#endif + keep_alive_ = rhs.keep_alive_; + follow_location_ = rhs.follow_location_; + url_encode_ = rhs.url_encode_; + address_family_ = rhs.address_family_; + tcp_nodelay_ = rhs.tcp_nodelay_; + socket_options_ = rhs.socket_options_; + compress_ = rhs.compress_; + decompress_ = rhs.decompress_; + interface_ = rhs.interface_; + proxy_host_ = rhs.proxy_host_; + proxy_port_ = rhs.proxy_port_; + proxy_basic_auth_username_ = rhs.proxy_basic_auth_username_; + proxy_basic_auth_password_ = rhs.proxy_basic_auth_password_; + proxy_bearer_token_auth_token_ = rhs.proxy_bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + proxy_digest_auth_username_ = rhs.proxy_digest_auth_username_; + proxy_digest_auth_password_ = rhs.proxy_digest_auth_password_; +#endif +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + ca_cert_file_path_ = rhs.ca_cert_file_path_; + ca_cert_dir_path_ = rhs.ca_cert_dir_path_; + ca_cert_store_ = rhs.ca_cert_store_; +#endif +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + server_certificate_verification_ = rhs.server_certificate_verification_; +#endif + logger_ = rhs.logger_; +} + +inline socket_t ClientImpl::create_client_socket(Error &error) const { + if (!proxy_host_.empty() && proxy_port_ != -1) { + return detail::create_client_socket( + proxy_host_, std::string(), proxy_port_, address_family_, tcp_nodelay_, + socket_options_, connection_timeout_sec_, connection_timeout_usec_, + read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, interface_, error); + } + + // Check is custom IP specified for host_ + std::string ip; + auto it = addr_map_.find(host_); + if (it != addr_map_.end()) ip = it->second; + + return detail::create_client_socket( + host_, ip, port_, address_family_, tcp_nodelay_, socket_options_, + connection_timeout_sec_, connection_timeout_usec_, read_timeout_sec_, + read_timeout_usec_, write_timeout_sec_, write_timeout_usec_, interface_, + error); +} + +inline bool ClientImpl::create_and_connect_socket(Socket &socket, + Error &error) { + auto sock = create_client_socket(error); + if (sock == INVALID_SOCKET) { return false; } + socket.sock = sock; + return true; +} + +inline void ClientImpl::shutdown_ssl(Socket & /*socket*/, + bool /*shutdown_gracefully*/) { + // If there are any requests in flight from threads other than us, then it's + // a thread-unsafe race because individual ssl* objects are not thread-safe. + assert(socket_requests_in_flight_ == 0 || + socket_requests_are_from_thread_ == std::this_thread::get_id()); +} + +inline void ClientImpl::shutdown_socket(Socket &socket) { + if (socket.sock == INVALID_SOCKET) { return; } + detail::shutdown_socket(socket.sock); +} + +inline void ClientImpl::close_socket(Socket &socket) { + // If there are requests in flight in another thread, usually closing + // the socket will be fine and they will simply receive an error when + // using the closed socket, but it is still a bug since rarely the OS + // may reassign the socket id to be used for a new socket, and then + // suddenly they will be operating on a live socket that is different + // than the one they intended! + assert(socket_requests_in_flight_ == 0 || + socket_requests_are_from_thread_ == std::this_thread::get_id()); + + // It is also a bug if this happens while SSL is still active +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + assert(socket.ssl == nullptr); +#endif + if (socket.sock == INVALID_SOCKET) { return; } + detail::close_socket(socket.sock); + socket.sock = INVALID_SOCKET; +} + +inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, + Response &res) { + std::array buf{}; + + detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); + + if (!line_reader.getline()) { return false; } + +#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR + const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n"); +#else + const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r?\n"); +#endif + + std::cmatch m; + if (!std::regex_match(line_reader.ptr(), m, re)) { + return req.method == "CONNECT"; + } + res.version = std::string(m[1]); + res.status = std::stoi(std::string(m[2])); + res.reason = std::string(m[3]); + + // Ignore '100 Continue' + while (res.status == 100) { + if (!line_reader.getline()) { return false; } // CRLF + if (!line_reader.getline()) { return false; } // next response line + + if (!std::regex_match(line_reader.ptr(), m, re)) { return false; } + res.version = std::string(m[1]); + res.status = std::stoi(std::string(m[2])); + res.reason = std::string(m[3]); + } + + return true; +} + +inline bool ClientImpl::send(Request &req, Response &res, Error &error) { + std::lock_guard request_mutex_guard(request_mutex_); + + { + std::lock_guard guard(socket_mutex_); + + // Set this to false immediately - if it ever gets set to true by the end of + // the request, we know another thread instructed us to close the socket. + socket_should_be_closed_when_request_is_done_ = false; + + auto is_alive = false; + if (socket_.is_open()) { + is_alive = detail::is_socket_alive(socket_.sock); + if (!is_alive) { + // Attempt to avoid sigpipe by shutting down nongracefully if it seems + // like the other side has already closed the connection Also, there + // cannot be any requests in flight from other threads since we locked + // request_mutex_, so safe to close everything immediately + const bool shutdown_gracefully = false; + shutdown_ssl(socket_, shutdown_gracefully); + shutdown_socket(socket_); + close_socket(socket_); + } + } + + if (!is_alive) { + if (!create_and_connect_socket(socket_, error)) { return false; } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + // TODO: refactoring + if (is_ssl()) { + auto &scli = static_cast(*this); + if (!proxy_host_.empty() && proxy_port_ != -1) { + bool success = false; + if (!scli.connect_with_proxy(socket_, res, success, error)) { + return success; + } + } + + if (!scli.initialize_ssl(socket_, error)) { return false; } + } +#endif + } + + // Mark the current socket as being in use so that it cannot be closed by + // anyone else while this request is ongoing, even though we will be + // releasing the mutex. + if (socket_requests_in_flight_ > 1) { + assert(socket_requests_are_from_thread_ == std::this_thread::get_id()); + } + socket_requests_in_flight_ += 1; + socket_requests_are_from_thread_ = std::this_thread::get_id(); + } + + for (const auto &header : default_headers_) { + if (req.headers.find(header.first) == req.headers.end()) { + req.headers.insert(header); + } + } + + auto close_connection = !keep_alive_; + auto ret = process_socket(socket_, [&](Stream &strm) { + return handle_request(strm, req, res, close_connection, error); + }); + + // Briefly lock mutex in order to mark that a request is no longer ongoing + { + std::lock_guard guard(socket_mutex_); + socket_requests_in_flight_ -= 1; + if (socket_requests_in_flight_ <= 0) { + assert(socket_requests_in_flight_ == 0); + socket_requests_are_from_thread_ = std::thread::id(); + } + + if (socket_should_be_closed_when_request_is_done_ || close_connection || + !ret) { + shutdown_ssl(socket_, true); + shutdown_socket(socket_); + close_socket(socket_); + } + } + + if (!ret) { + if (error == Error::Success) { error = Error::Unknown; } + } + + return ret; +} + +inline Result ClientImpl::send(const Request &req) { + auto req2 = req; + return send_(std::move(req2)); +} + +inline Result ClientImpl::send_(Request &&req) { + auto res = detail::make_unique(); + auto error = Error::Success; + auto ret = send(req, *res, error); + return Result{ret ? std::move(res) : nullptr, error, std::move(req.headers)}; +} + +inline bool ClientImpl::handle_request(Stream &strm, Request &req, + Response &res, bool close_connection, + Error &error) { + if (req.path.empty()) { + error = Error::Connection; + return false; + } + + auto req_save = req; + + bool ret; + + if (!is_ssl() && !proxy_host_.empty() && proxy_port_ != -1) { + auto req2 = req; + req2.path = "http://" + host_and_port_ + req.path; + ret = process_request(strm, req2, res, close_connection, error); + req = req2; + req.path = req_save.path; + } else { + ret = process_request(strm, req, res, close_connection, error); + } + + if (!ret) { return false; } + + if (300 < res.status && res.status < 400 && follow_location_) { + req = req_save; + ret = redirect(req, res, error); + } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if ((res.status == 401 || res.status == 407) && + req.authorization_count_ < 5) { + auto is_proxy = res.status == 407; + const auto &username = + is_proxy ? proxy_digest_auth_username_ : digest_auth_username_; + const auto &password = + is_proxy ? proxy_digest_auth_password_ : digest_auth_password_; + + if (!username.empty() && !password.empty()) { + std::map auth; + if (detail::parse_www_authenticate(res, auth, is_proxy)) { + Request new_req = req; + new_req.authorization_count_ += 1; + new_req.headers.erase(is_proxy ? "Proxy-Authorization" + : "Authorization"); + new_req.headers.insert(detail::make_digest_authentication_header( + req, auth, new_req.authorization_count_, detail::random_string(10), + username, password, is_proxy)); + + Response new_res; + + ret = send(new_req, new_res, error); + if (ret) { res = new_res; } + } + } + } +#endif + + return ret; +} + +inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { + if (req.redirect_count_ == 0) { + error = Error::ExceedRedirectCount; + return false; + } + + auto location = detail::decode_url(res.get_header_value("location"), true); + if (location.empty()) { return false; } + + const static std::regex re( + R"((?:(https?):)?(?://(?:\[([\d:]+)\]|([^:/?#]+))(?::(\d+))?)?([^?#]*(?:\?[^#]*)?)(?:#.*)?)"); + + std::smatch m; + if (!std::regex_match(location, m, re)) { return false; } + + auto scheme = is_ssl() ? "https" : "http"; + + auto next_scheme = m[1].str(); + auto next_host = m[2].str(); + if (next_host.empty()) { next_host = m[3].str(); } + auto port_str = m[4].str(); + auto next_path = m[5].str(); + + auto next_port = port_; + if (!port_str.empty()) { + next_port = std::stoi(port_str); + } else if (!next_scheme.empty()) { + next_port = next_scheme == "https" ? 443 : 80; + } + + if (next_scheme.empty()) { next_scheme = scheme; } + if (next_host.empty()) { next_host = host_; } + if (next_path.empty()) { next_path = "/"; } + + if (next_scheme == scheme && next_host == host_ && next_port == port_) { + return detail::redirect(*this, req, res, next_path, location, error); + } else { + if (next_scheme == "https") { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + SSLClient cli(next_host.c_str(), next_port); + cli.copy_settings(*this); + if (ca_cert_store_) { cli.set_ca_cert_store(ca_cert_store_); } + return detail::redirect(cli, req, res, next_path, location, error); +#else + return false; +#endif + } else { + ClientImpl cli(next_host.c_str(), next_port); + cli.copy_settings(*this); + return detail::redirect(cli, req, res, next_path, location, error); + } + } +} + +inline bool ClientImpl::write_content_with_provider(Stream &strm, + const Request &req, + Error &error) { + auto is_shutting_down = []() { return false; }; + + if (req.is_chunked_content_provider_) { + // TODO: Brotli suport + std::unique_ptr compressor; +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + if (compress_) { + compressor = detail::make_unique(); + } else +#endif + { + compressor = detail::make_unique(); + } + + return detail::write_content_chunked(strm, req.content_provider_, + is_shutting_down, *compressor, error); + } else { + return detail::write_content(strm, req.content_provider_, 0, + req.content_length_, is_shutting_down, error); + } +} // namespace httplib + +inline bool ClientImpl::write_request(Stream &strm, Request &req, + bool close_connection, Error &error) { + // Prepare additional headers + if (close_connection) { + if (!req.has_header("Connection")) { + req.headers.emplace("Connection", "close"); + } + } + + if (!req.has_header("Host")) { + if (is_ssl()) { + if (port_ == 443) { + req.headers.emplace("Host", host_); + } else { + req.headers.emplace("Host", host_and_port_); + } + } else { + if (port_ == 80) { + req.headers.emplace("Host", host_); + } else { + req.headers.emplace("Host", host_and_port_); + } + } + } + + if (!req.has_header("Accept")) { req.headers.emplace("Accept", "*/*"); } + +#ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT + if (!req.has_header("User-Agent")) { + auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION; + req.headers.emplace("User-Agent", agent); + } +#endif + + if (req.body.empty()) { + if (req.content_provider_) { + if (!req.is_chunked_content_provider_) { + if (!req.has_header("Content-Length")) { + auto length = std::to_string(req.content_length_); + req.headers.emplace("Content-Length", length); + } + } + } else { + if (req.method == "POST" || req.method == "PUT" || + req.method == "PATCH") { + req.headers.emplace("Content-Length", "0"); + } + } + } else { + if (!req.has_header("Content-Type")) { + req.headers.emplace("Content-Type", "text/plain"); + } + + if (!req.has_header("Content-Length")) { + auto length = std::to_string(req.body.size()); + req.headers.emplace("Content-Length", length); + } + } + + if (!basic_auth_password_.empty() || !basic_auth_username_.empty()) { + if (!req.has_header("Authorization")) { + req.headers.insert(make_basic_authentication_header( + basic_auth_username_, basic_auth_password_, false)); + } + } + + if (!proxy_basic_auth_username_.empty() && + !proxy_basic_auth_password_.empty()) { + if (!req.has_header("Proxy-Authorization")) { + req.headers.insert(make_basic_authentication_header( + proxy_basic_auth_username_, proxy_basic_auth_password_, true)); + } + } + + if (!bearer_token_auth_token_.empty()) { + if (!req.has_header("Authorization")) { + req.headers.insert(make_bearer_token_authentication_header( + bearer_token_auth_token_, false)); + } + } + + if (!proxy_bearer_token_auth_token_.empty()) { + if (!req.has_header("Proxy-Authorization")) { + req.headers.insert(make_bearer_token_authentication_header( + proxy_bearer_token_auth_token_, true)); + } + } + + // Request line and headers + { + detail::BufferStream bstrm; + + const auto &path = url_encode_ ? detail::encode_url(req.path) : req.path; + bstrm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), path.c_str()); + + detail::write_headers(bstrm, req.headers); + + // Flush buffer + auto &data = bstrm.get_buffer(); + if (!detail::write_data(strm, data.data(), data.size())) { + error = Error::Write; + return false; + } + } + + // Body + if (req.body.empty()) { + return write_content_with_provider(strm, req, error); + } + + if (!detail::write_data(strm, req.body.data(), req.body.size())) { + error = Error::Write; + return false; + } + + return true; +} + +inline std::unique_ptr ClientImpl::send_with_content_provider( + Request &req, const char *body, size_t content_length, + ContentProvider content_provider, + ContentProviderWithoutLength content_provider_without_length, + const std::string &content_type, Error &error) { + if (!content_type.empty()) { + req.headers.emplace("Content-Type", content_type); + } + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + if (compress_) { req.headers.emplace("Content-Encoding", "gzip"); } +#endif + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + if (compress_ && !content_provider_without_length) { + // TODO: Brotli support + detail::gzip_compressor compressor; + + if (content_provider) { + auto ok = true; + size_t offset = 0; + DataSink data_sink; + + data_sink.write = [&](const char *data, size_t data_len) -> bool { + if (ok) { + auto last = offset + data_len == content_length; + + auto ret = compressor.compress( + data, data_len, last, + [&](const char *compressed_data, size_t compressed_data_len) { + req.body.append(compressed_data, compressed_data_len); + return true; + }); + + if (ret) { + offset += data_len; + } else { + ok = false; + } + } + return ok; + }; + + data_sink.is_writable = [&](void) { return ok && true; }; + + while (ok && offset < content_length) { + if (!content_provider(offset, content_length - offset, data_sink)) { + error = Error::Canceled; + return nullptr; + } + } + } else { + if (!compressor.compress(body, content_length, true, + [&](const char *data, size_t data_len) { + req.body.append(data, data_len); + return true; + })) { + error = Error::Compression; + return nullptr; + } + } + } else +#endif + { + if (content_provider) { + req.content_length_ = content_length; + req.content_provider_ = std::move(content_provider); + req.is_chunked_content_provider_ = false; + } else if (content_provider_without_length) { + req.content_length_ = 0; + req.content_provider_ = detail::ContentProviderAdapter( + std::move(content_provider_without_length)); + req.is_chunked_content_provider_ = true; + req.headers.emplace("Transfer-Encoding", "chunked"); + } else { + req.body.assign(body, content_length); + ; + } + } + + auto res = detail::make_unique(); + return send(req, *res, error) ? std::move(res) : nullptr; +} + +inline Result ClientImpl::send_with_content_provider( + const std::string &method, const std::string &path, const Headers &headers, + const char *body, size_t content_length, ContentProvider content_provider, + ContentProviderWithoutLength content_provider_without_length, + const std::string &content_type) { + Request req; + req.method = method; + req.headers = headers; + req.path = path; + + auto error = Error::Success; + + auto res = send_with_content_provider( + req, body, content_length, std::move(content_provider), + std::move(content_provider_without_length), content_type, error); + + return Result{std::move(res), error, std::move(req.headers)}; +} + +inline std::string +ClientImpl::adjust_host_string(const std::string &host) const { + if (host.find(':') != std::string::npos) { return "[" + host + "]"; } + return host; +} + +inline bool ClientImpl::process_request(Stream &strm, Request &req, + Response &res, bool close_connection, + Error &error) { + // Send request + if (!write_request(strm, req, close_connection, error)) { return false; } + + // Receive response and headers + if (!read_response_line(strm, req, res) || + !detail::read_headers(strm, res.headers)) { + error = Error::Read; + return false; + } + + // Body + if ((res.status != 204) && req.method != "HEAD" && req.method != "CONNECT") { + auto redirect = 300 < res.status && res.status < 400 && follow_location_; + + if (req.response_handler && !redirect) { + if (!req.response_handler(res)) { + error = Error::Canceled; + return false; + } + } + + auto out = + req.content_receiver + ? static_cast( + [&](const char *buf, size_t n, uint64_t off, uint64_t len) { + if (redirect) { return true; } + auto ret = req.content_receiver(buf, n, off, len); + if (!ret) { error = Error::Canceled; } + return ret; + }) + : static_cast( + [&](const char *buf, size_t n, uint64_t /*off*/, + uint64_t /*len*/) { + if (res.body.size() + n > res.body.max_size()) { + return false; + } + res.body.append(buf, n); + return true; + }); + + auto progress = [&](uint64_t current, uint64_t total) { + if (!req.progress || redirect) { return true; } + auto ret = req.progress(current, total); + if (!ret) { error = Error::Canceled; } + return ret; + }; + + int dummy_status; + if (!detail::read_content(strm, res, (std::numeric_limits::max)(), + dummy_status, std::move(progress), std::move(out), + decompress_)) { + if (error != Error::Canceled) { error = Error::Read; } + return false; + } + } + + if (res.get_header_value("Connection") == "close" || + (res.version == "HTTP/1.0" && res.reason != "Connection established")) { + // TODO this requires a not-entirely-obvious chain of calls to be correct + // for this to be safe. Maybe a code refactor (such as moving this out to + // the send function and getting rid of the recursiveness of the mutex) + // could make this more obvious. + + // This is safe to call because process_request is only called by + // handle_request which is only called by send, which locks the request + // mutex during the process. It would be a bug to call it from a different + // thread since it's a thread-safety issue to do these things to the socket + // if another thread is using the socket. + std::lock_guard guard(socket_mutex_); + shutdown_ssl(socket_, true); + shutdown_socket(socket_); + close_socket(socket_); + } + + // Log + if (logger_) { logger_(req, res); } + + return true; +} + +inline bool +ClientImpl::process_socket(const Socket &socket, + std::function callback) { + return detail::process_client_socket( + socket.sock, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, std::move(callback)); +} + +inline bool ClientImpl::is_ssl() const { return false; } + +inline Result ClientImpl::Get(const std::string &path) { + return Get(path, Headers(), Progress()); +} + +inline Result ClientImpl::Get(const std::string &path, Progress progress) { + return Get(path, Headers(), std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers) { + return Get(path, headers, Progress()); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + Progress progress) { + Request req; + req.method = "GET"; + req.path = path; + req.headers = headers; + req.progress = std::move(progress); + + return send_(std::move(req)); +} + +inline Result ClientImpl::Get(const std::string &path, + ContentReceiver content_receiver) { + return Get(path, Headers(), nullptr, std::move(content_receiver), nullptr); +} + +inline Result ClientImpl::Get(const std::string &path, + ContentReceiver content_receiver, + Progress progress) { + return Get(path, Headers(), nullptr, std::move(content_receiver), + std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver) { + return Get(path, headers, nullptr, std::move(content_receiver), nullptr); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver, + Progress progress) { + return Get(path, headers, nullptr, std::move(content_receiver), + std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, + ResponseHandler response_handler, + ContentReceiver content_receiver) { + return Get(path, Headers(), std::move(response_handler), + std::move(content_receiver), nullptr); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver) { + return Get(path, headers, std::move(response_handler), + std::move(content_receiver), nullptr); +} + +inline Result ClientImpl::Get(const std::string &path, + ResponseHandler response_handler, + ContentReceiver content_receiver, + Progress progress) { + return Get(path, Headers(), std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, + Progress progress) { + Request req; + req.method = "GET"; + req.path = path; + req.headers = headers; + req.response_handler = std::move(response_handler); + req.content_receiver = + [content_receiver](const char *data, size_t data_length, + uint64_t /*offset*/, uint64_t /*total_length*/) { + return content_receiver(data, data_length); + }; + req.progress = std::move(progress); + + return send_(std::move(req)); +} + +inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, + const Headers &headers, Progress progress) { + if (params.empty()) { return Get(path, headers); } + + std::string path_with_query = append_query_params(path, params); + return Get(path_with_query.c_str(), headers, progress); +} + +inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ContentReceiver content_receiver, + Progress progress) { + return Get(path, params, headers, nullptr, content_receiver, progress); +} + +inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, + Progress progress) { + if (params.empty()) { + return Get(path, headers, response_handler, content_receiver, progress); + } + + std::string path_with_query = append_query_params(path, params); + return Get(path_with_query.c_str(), headers, response_handler, + content_receiver, progress); +} + +inline Result ClientImpl::Head(const std::string &path) { + return Head(path, Headers()); +} + +inline Result ClientImpl::Head(const std::string &path, + const Headers &headers) { + Request req; + req.method = "HEAD"; + req.headers = headers; + req.path = path; + + return send_(std::move(req)); +} + +inline Result ClientImpl::Post(const std::string &path) { + return Post(path, std::string(), std::string()); +} + +inline Result ClientImpl::Post(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return Post(path, Headers(), body, content_length, content_type); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return send_with_content_provider("POST", path, headers, body, content_length, + nullptr, nullptr, content_type); +} + +inline Result ClientImpl::Post(const std::string &path, const std::string &body, + const std::string &content_type) { + return Post(path, Headers(), body, content_type); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return send_with_content_provider("POST", path, headers, body.data(), + body.size(), nullptr, nullptr, + content_type); +} + +inline Result ClientImpl::Post(const std::string &path, const Params ¶ms) { + return Post(path, Headers(), params); +} + +inline Result ClientImpl::Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return Post(path, Headers(), content_length, std::move(content_provider), + content_type); +} + +inline Result ClientImpl::Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return Post(path, Headers(), std::move(content_provider), content_type); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return send_with_content_provider("POST", path, headers, nullptr, + content_length, std::move(content_provider), + nullptr, content_type); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return send_with_content_provider("POST", path, headers, nullptr, 0, nullptr, + std::move(content_provider), content_type); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const Params ¶ms) { + auto query = detail::params_to_query_str(params); + return Post(path, headers, query, "application/x-www-form-urlencoded"); +} + +inline Result ClientImpl::Post(const std::string &path, + const MultipartFormDataItems &items) { + return Post(path, Headers(), items); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items) { + return Post(path, headers, items, detail::make_multipart_data_boundary()); +} +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const std::string &boundary) { + for (size_t i = 0; i < boundary.size(); i++) { + char c = boundary[i]; + if (!std::isalnum(c) && c != '-' && c != '_') { + return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; + } + } + + std::string body; + + for (const auto &item : items) { + body += "--" + boundary + "\r\n"; + body += "Content-Disposition: form-data; name=\"" + item.name + "\""; + if (!item.filename.empty()) { + body += "; filename=\"" + item.filename + "\""; + } + body += "\r\n"; + if (!item.content_type.empty()) { + body += "Content-Type: " + item.content_type + "\r\n"; + } + body += "\r\n"; + body += item.content + "\r\n"; + } + + body += "--" + boundary + "--\r\n"; + + std::string content_type = "multipart/form-data; boundary=" + boundary; + return Post(path, headers, body, content_type.c_str()); +} + +inline Result ClientImpl::Put(const std::string &path) { + return Put(path, std::string(), std::string()); +} + +inline Result ClientImpl::Put(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return Put(path, Headers(), body, content_length, content_type); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return send_with_content_provider("PUT", path, headers, body, content_length, + nullptr, nullptr, content_type); +} + +inline Result ClientImpl::Put(const std::string &path, const std::string &body, + const std::string &content_type) { + return Put(path, Headers(), body, content_type); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return send_with_content_provider("PUT", path, headers, body.data(), + body.size(), nullptr, nullptr, + content_type); +} + +inline Result ClientImpl::Put(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return Put(path, Headers(), content_length, std::move(content_provider), + content_type); +} + +inline Result ClientImpl::Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return Put(path, Headers(), std::move(content_provider), content_type); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return send_with_content_provider("PUT", path, headers, nullptr, + content_length, std::move(content_provider), + nullptr, content_type); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return send_with_content_provider("PUT", path, headers, nullptr, 0, nullptr, + std::move(content_provider), content_type); +} + +inline Result ClientImpl::Put(const std::string &path, const Params ¶ms) { + return Put(path, Headers(), params); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const Params ¶ms) { + auto query = detail::params_to_query_str(params); + return Put(path, headers, query, "application/x-www-form-urlencoded"); +} + +inline Result ClientImpl::Patch(const std::string &path) { + return Patch(path, std::string(), std::string()); +} + +inline Result ClientImpl::Patch(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return Patch(path, Headers(), body, content_length, content_type); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return send_with_content_provider("PATCH", path, headers, body, + content_length, nullptr, nullptr, + content_type); +} + +inline Result ClientImpl::Patch(const std::string &path, + const std::string &body, + const std::string &content_type) { + return Patch(path, Headers(), body, content_type); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return send_with_content_provider("PATCH", path, headers, body.data(), + body.size(), nullptr, nullptr, + content_type); +} + +inline Result ClientImpl::Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return Patch(path, Headers(), content_length, std::move(content_provider), + content_type); +} + +inline Result ClientImpl::Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return Patch(path, Headers(), std::move(content_provider), content_type); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return send_with_content_provider("PATCH", path, headers, nullptr, + content_length, std::move(content_provider), + nullptr, content_type); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return send_with_content_provider("PATCH", path, headers, nullptr, 0, nullptr, + std::move(content_provider), content_type); +} + +inline Result ClientImpl::Delete(const std::string &path) { + return Delete(path, Headers(), std::string(), std::string()); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers) { + return Delete(path, headers, std::string(), std::string()); +} + +inline Result ClientImpl::Delete(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return Delete(path, Headers(), body, content_length, content_type); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, const char *body, + size_t content_length, + const std::string &content_type) { + Request req; + req.method = "DELETE"; + req.headers = headers; + req.path = path; + + if (!content_type.empty()) { + req.headers.emplace("Content-Type", content_type); + } + req.body.assign(body, content_length); + + return send_(std::move(req)); +} + +inline Result ClientImpl::Delete(const std::string &path, + const std::string &body, + const std::string &content_type) { + return Delete(path, Headers(), body.data(), body.size(), content_type); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, + const std::string &body, + const std::string &content_type) { + return Delete(path, headers, body.data(), body.size(), content_type); +} + +inline Result ClientImpl::Options(const std::string &path) { + return Options(path, Headers()); +} + +inline Result ClientImpl::Options(const std::string &path, + const Headers &headers) { + Request req; + req.method = "OPTIONS"; + req.headers = headers; + req.path = path; + + return send_(std::move(req)); +} + +inline size_t ClientImpl::is_socket_open() const { + std::lock_guard guard(socket_mutex_); + return socket_.is_open(); +} + +inline socket_t ClientImpl::socket() const { + return socket_.sock; +} + +inline void ClientImpl::stop() { + std::lock_guard guard(socket_mutex_); + + // If there is anything ongoing right now, the ONLY thread-safe thing we can + // do is to shutdown_socket, so that threads using this socket suddenly + // discover they can't read/write any more and error out. Everything else + // (closing the socket, shutting ssl down) is unsafe because these actions are + // not thread-safe. + if (socket_requests_in_flight_ > 0) { + shutdown_socket(socket_); + + // Aside from that, we set a flag for the socket to be closed when we're + // done. + socket_should_be_closed_when_request_is_done_ = true; + return; + } + + // Otherwise, sitll holding the mutex, we can shut everything down ourselves + shutdown_ssl(socket_, true); + shutdown_socket(socket_); + close_socket(socket_); +} + +inline void ClientImpl::set_connection_timeout(time_t sec, time_t usec) { + connection_timeout_sec_ = sec; + connection_timeout_usec_ = usec; +} + +inline void ClientImpl::set_read_timeout(time_t sec, time_t usec) { + read_timeout_sec_ = sec; + read_timeout_usec_ = usec; +} + +inline void ClientImpl::set_write_timeout(time_t sec, time_t usec) { + write_timeout_sec_ = sec; + write_timeout_usec_ = usec; +} + +inline void ClientImpl::set_basic_auth(const std::string &username, + const std::string &password) { + basic_auth_username_ = username; + basic_auth_password_ = password; +} + +inline void ClientImpl::set_bearer_token_auth(const std::string &token) { + bearer_token_auth_token_ = token; +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void ClientImpl::set_digest_auth(const std::string &username, + const std::string &password) { + digest_auth_username_ = username; + digest_auth_password_ = password; +} +#endif + +inline void ClientImpl::set_keep_alive(bool on) { keep_alive_ = on; } + +inline void ClientImpl::set_follow_location(bool on) { follow_location_ = on; } + +inline void ClientImpl::set_url_encode(bool on) { url_encode_ = on; } + +inline void +ClientImpl::set_hostname_addr_map(std::map addr_map) { + addr_map_ = std::move(addr_map); +} + +inline void ClientImpl::set_default_headers(Headers headers) { + default_headers_ = std::move(headers); +} + +inline void ClientImpl::set_address_family(int family) { + address_family_ = family; +} + +inline void ClientImpl::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; } + +inline void ClientImpl::set_socket_options(SocketOptions socket_options) { + socket_options_ = std::move(socket_options); +} + +inline void ClientImpl::set_compress(bool on) { compress_ = on; } + +inline void ClientImpl::set_decompress(bool on) { decompress_ = on; } + +inline void ClientImpl::set_interface(const std::string &intf) { + interface_ = intf; +} + +inline void ClientImpl::set_proxy(const std::string &host, int port) { + proxy_host_ = host; + proxy_port_ = port; +} + +inline void ClientImpl::set_proxy_basic_auth(const std::string &username, + const std::string &password) { + proxy_basic_auth_username_ = username; + proxy_basic_auth_password_ = password; +} + +inline void ClientImpl::set_proxy_bearer_token_auth(const std::string &token) { + proxy_bearer_token_auth_token_ = token; +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void ClientImpl::set_proxy_digest_auth(const std::string &username, + const std::string &password) { + proxy_digest_auth_username_ = username; + proxy_digest_auth_password_ = password; +} +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void ClientImpl::set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path) { + ca_cert_file_path_ = ca_cert_file_path; + ca_cert_dir_path_ = ca_cert_dir_path; +} + +inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) { + if (ca_cert_store && ca_cert_store != ca_cert_store_) { + ca_cert_store_ = ca_cert_store; + } +} +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void ClientImpl::enable_server_certificate_verification(bool enabled) { + server_certificate_verification_ = enabled; +} +#endif + +inline void ClientImpl::set_logger(Logger logger) { + logger_ = std::move(logger); +} + +/* + * SSL Implementation + */ +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +namespace detail { + +template +inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex, + U SSL_connect_or_accept, V setup) { + SSL *ssl = nullptr; + { + std::lock_guard guard(ctx_mutex); + ssl = SSL_new(ctx); + } + + if (ssl) { + set_nonblocking(sock, true); + auto bio = BIO_new_socket(static_cast(sock), BIO_NOCLOSE); + BIO_set_nbio(bio, 1); + SSL_set_bio(ssl, bio, bio); + + if (!setup(ssl) || SSL_connect_or_accept(ssl) != 1) { + SSL_shutdown(ssl); + { + std::lock_guard guard(ctx_mutex); + SSL_free(ssl); + } + set_nonblocking(sock, false); + return nullptr; + } + BIO_set_nbio(bio, 0); + set_nonblocking(sock, false); + } + + return ssl; +} + +inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, + bool shutdown_gracefully) { + // sometimes we may want to skip this to try to avoid SIGPIPE if we know + // the remote has closed the network connection + // Note that it is not always possible to avoid SIGPIPE, this is merely a + // best-efforts. + if (shutdown_gracefully) { SSL_shutdown(ssl); } + + std::lock_guard guard(ctx_mutex); + SSL_free(ssl); +} + +template +bool ssl_connect_or_accept_nonblocking(socket_t sock, SSL *ssl, + U ssl_connect_or_accept, + time_t timeout_sec, + time_t timeout_usec) { + int res = 0; + while ((res = ssl_connect_or_accept(ssl)) != 1) { + auto err = SSL_get_error(ssl, res); + switch (err) { + case SSL_ERROR_WANT_READ: + if (select_read(sock, timeout_sec, timeout_usec) > 0) { continue; } + break; + case SSL_ERROR_WANT_WRITE: + if (select_write(sock, timeout_sec, timeout_usec) > 0) { continue; } + break; + default: break; + } + return false; + } + return true; +} + +template +inline bool process_server_socket_ssl( + const std::atomic &svr_sock, SSL *ssl, socket_t sock, + size_t keep_alive_max_count, time_t keep_alive_timeout_sec, + time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { + return process_server_socket_core( + svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, + [&](bool close_connection, bool &connection_closed) { + SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm, close_connection, connection_closed); + }); +} + +template +inline bool +process_client_socket_ssl(SSL *ssl, socket_t sock, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { + SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm); +} + +#if OPENSSL_VERSION_NUMBER < 0x10100000L +static std::shared_ptr> openSSL_locks_; + +class SSLThreadLocks { +public: + SSLThreadLocks() { + openSSL_locks_ = + std::make_shared>(CRYPTO_num_locks()); + CRYPTO_set_locking_callback(locking_callback); + } + + ~SSLThreadLocks() { CRYPTO_set_locking_callback(nullptr); } + +private: + static void locking_callback(int mode, int type, const char * /*file*/, + int /*line*/) { + auto &lk = (*openSSL_locks_)[static_cast(type)]; + if (mode & CRYPTO_LOCK) { + lk.lock(); + } else { + lk.unlock(); + } + } +}; + +#endif + +class SSLInit { +public: + SSLInit() { +#if OPENSSL_VERSION_NUMBER < 0x1010001fL + SSL_load_error_strings(); + SSL_library_init(); +#else + OPENSSL_init_ssl( + OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); +#endif + } + + ~SSLInit() { +#if OPENSSL_VERSION_NUMBER < 0x1010001fL + ERR_free_strings(); +#endif + } + +private: +#if OPENSSL_VERSION_NUMBER < 0x10100000L + SSLThreadLocks thread_init_; +#endif +}; + +// SSL socket stream implementation +inline SSLSocketStream::SSLSocketStream(socket_t sock, SSL *ssl, + time_t read_timeout_sec, + time_t read_timeout_usec, + time_t write_timeout_sec, + time_t write_timeout_usec) + : sock_(sock), ssl_(ssl), read_timeout_sec_(read_timeout_sec), + read_timeout_usec_(read_timeout_usec), + write_timeout_sec_(write_timeout_sec), + write_timeout_usec_(write_timeout_usec) { + SSL_clear_mode(ssl, SSL_MODE_AUTO_RETRY); +} + +inline SSLSocketStream::~SSLSocketStream() {} + +inline bool SSLSocketStream::is_readable() const { + return detail::select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; +} + +inline bool SSLSocketStream::is_writable() const { + return detail::select_write(sock_, write_timeout_sec_, write_timeout_usec_) > + 0; +} + +inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { + if (SSL_pending(ssl_) > 0) { + return SSL_read(ssl_, ptr, static_cast(size)); + } else if (is_readable()) { + auto ret = SSL_read(ssl_, ptr, static_cast(size)); + if (ret < 0) { + auto err = SSL_get_error(ssl_, ret); + int n = 1000; +#ifdef _WIN32 + while (--n >= 0 && (err == SSL_ERROR_WANT_READ || + (err == SSL_ERROR_SYSCALL && + WSAGetLastError() == WSAETIMEDOUT))) { +#else + while (--n >= 0 && err == SSL_ERROR_WANT_READ) { +#endif + if (SSL_pending(ssl_) > 0) { + return SSL_read(ssl_, ptr, static_cast(size)); + } else if (is_readable()) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + ret = SSL_read(ssl_, ptr, static_cast(size)); + if (ret >= 0) { return ret; } + err = SSL_get_error(ssl_, ret); + } else { + return -1; + } + } + } + return ret; + } + return -1; +} + +inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { + if (is_writable()) { + auto handle_size = static_cast( + std::min(size, (std::numeric_limits::max)())); + + auto ret = SSL_write(ssl_, ptr, static_cast(handle_size)); + if (ret < 0) { + auto err = SSL_get_error(ssl_, ret); + int n = 1000; +#ifdef _WIN32 + while (--n >= 0 && (err == SSL_ERROR_WANT_WRITE || + (err == SSL_ERROR_SYSCALL && + WSAGetLastError() == WSAETIMEDOUT))) { +#else + while (--n >= 0 && err == SSL_ERROR_WANT_WRITE) { +#endif + if (is_writable()) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + ret = SSL_write(ssl_, ptr, static_cast(handle_size)); + if (ret >= 0) { return ret; } + err = SSL_get_error(ssl_, ret); + } else { + return -1; + } + } + } + return ret; + } + return -1; +} + +inline void SSLSocketStream::get_remote_ip_and_port(std::string &ip, + int &port) const { + detail::get_remote_ip_and_port(sock_, ip, port); +} + +inline socket_t SSLSocketStream::socket() const { return sock_; } + +static SSLInit sslinit_; + +} // namespace detail + +// SSL HTTP server implementation +inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, + const char *client_ca_cert_file_path, + const char *client_ca_cert_dir_path, + const char *private_key_password) { + ctx_ = SSL_CTX_new(TLS_server_method()); + + if (ctx_) { + SSL_CTX_set_options(ctx_, + SSL_OP_NO_COMPRESSION | + SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); + + SSL_CTX_set_min_proto_version(ctx_, TLS1_1_VERSION); + + // add default password callback before opening encrypted private key + if (private_key_password != nullptr && (private_key_password[0] != '\0')) { + SSL_CTX_set_default_passwd_cb_userdata(ctx_, + (char *)private_key_password); + } + + if (SSL_CTX_use_certificate_chain_file(ctx_, cert_path) != 1 || + SSL_CTX_use_PrivateKey_file(ctx_, private_key_path, SSL_FILETYPE_PEM) != + 1) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } else if (client_ca_cert_file_path || client_ca_cert_dir_path) { + SSL_CTX_load_verify_locations(ctx_, client_ca_cert_file_path, + client_ca_cert_dir_path); + + SSL_CTX_set_verify( + ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); + } + } +} + +inline SSLServer::SSLServer(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store) { + ctx_ = SSL_CTX_new(TLS_server_method()); + + if (ctx_) { + SSL_CTX_set_options(ctx_, + SSL_OP_NO_COMPRESSION | + SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); + + SSL_CTX_set_min_proto_version(ctx_, TLS1_1_VERSION); + + if (SSL_CTX_use_certificate(ctx_, cert) != 1 || + SSL_CTX_use_PrivateKey(ctx_, private_key) != 1) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } else if (client_ca_cert_store) { + SSL_CTX_set_cert_store(ctx_, client_ca_cert_store); + + SSL_CTX_set_verify( + ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); + } + } +} + +inline SSLServer::SSLServer( + const std::function &setup_ssl_ctx_callback) { + ctx_ = SSL_CTX_new(TLS_method()); + if (ctx_) { + if (!setup_ssl_ctx_callback(*ctx_)) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } + } +} + +inline SSLServer::~SSLServer() { + if (ctx_) { SSL_CTX_free(ctx_); } +} + +inline bool SSLServer::is_valid() const { return ctx_; } + +inline SSL_CTX *SSLServer::ssl_context() const { return ctx_; } + +inline bool SSLServer::process_and_close_socket(socket_t sock) { + auto ssl = detail::ssl_new( + sock, ctx_, ctx_mutex_, + [&](SSL *ssl2) { + return detail::ssl_connect_or_accept_nonblocking( + sock, ssl2, SSL_accept, read_timeout_sec_, read_timeout_usec_); + }, + [](SSL * /*ssl2*/) { return true; }); + + bool ret = false; + if (ssl) { + ret = detail::process_server_socket_ssl( + svr_sock_, ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_, + read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, + [this, ssl](Stream &strm, bool close_connection, + bool &connection_closed) { + return process_request(strm, close_connection, connection_closed, + [&](Request &req) { req.ssl = ssl; }); + }); + + // Shutdown gracefully if the result seemed successful, non-gracefully if + // the connection appeared to be closed. + const bool shutdown_gracefully = ret; + detail::ssl_delete(ctx_mutex_, ssl, shutdown_gracefully); + } + + detail::shutdown_socket(sock); + detail::close_socket(sock); + return ret; +} + +// SSL HTTP client implementation +inline SSLClient::SSLClient(const std::string &host) + : SSLClient(host, 443, std::string(), std::string()) {} + +inline SSLClient::SSLClient(const std::string &host, int port) + : SSLClient(host, port, std::string(), std::string()) {} + +inline SSLClient::SSLClient(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path) + : ClientImpl(host, port, client_cert_path, client_key_path) { + ctx_ = SSL_CTX_new(TLS_client_method()); + + detail::split(&host_[0], &host_[host_.size()], '.', + [&](const char *b, const char *e) { + host_components_.emplace_back(std::string(b, e)); + }); + + if (!client_cert_path.empty() && !client_key_path.empty()) { + if (SSL_CTX_use_certificate_file(ctx_, client_cert_path.c_str(), + SSL_FILETYPE_PEM) != 1 || + SSL_CTX_use_PrivateKey_file(ctx_, client_key_path.c_str(), + SSL_FILETYPE_PEM) != 1) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } + } +} + +inline SSLClient::SSLClient(const std::string &host, int port, + X509 *client_cert, EVP_PKEY *client_key) + : ClientImpl(host, port) { + ctx_ = SSL_CTX_new(TLS_client_method()); + + detail::split(&host_[0], &host_[host_.size()], '.', + [&](const char *b, const char *e) { + host_components_.emplace_back(std::string(b, e)); + }); + + if (client_cert != nullptr && client_key != nullptr) { + if (SSL_CTX_use_certificate(ctx_, client_cert) != 1 || + SSL_CTX_use_PrivateKey(ctx_, client_key) != 1) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } + } +} + +inline SSLClient::~SSLClient() { + if (ctx_) { SSL_CTX_free(ctx_); } + // Make sure to shut down SSL since shutdown_ssl will resolve to the + // base function rather than the derived function once we get to the + // base class destructor, and won't free the SSL (causing a leak). + shutdown_ssl_impl(socket_, true); +} + +inline bool SSLClient::is_valid() const { return ctx_; } + +inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) { + if (ca_cert_store) { + if (ctx_) { + if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) { + // Free memory allocated for old cert and use new store `ca_cert_store` + SSL_CTX_set_cert_store(ctx_, ca_cert_store); + } + } else { + X509_STORE_free(ca_cert_store); + } + } +} + +inline long SSLClient::get_openssl_verify_result() const { + return verify_result_; +} + +inline SSL_CTX *SSLClient::ssl_context() const { return ctx_; } + +inline bool SSLClient::create_and_connect_socket(Socket &socket, Error &error) { + return is_valid() && ClientImpl::create_and_connect_socket(socket, error); +} + +// Assumes that socket_mutex_ is locked and that there are no requests in flight +inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, + bool &success, Error &error) { + success = true; + Response res2; + if (!detail::process_client_socket( + socket.sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) { + Request req2; + req2.method = "CONNECT"; + req2.path = host_and_port_; + return process_request(strm, req2, res2, false, error); + })) { + // Thread-safe to close everything because we are assuming there are no + // requests in flight + shutdown_ssl(socket, true); + shutdown_socket(socket); + close_socket(socket); + success = false; + return false; + } + + if (res2.status == 407) { + if (!proxy_digest_auth_username_.empty() && + !proxy_digest_auth_password_.empty()) { + std::map auth; + if (detail::parse_www_authenticate(res2, auth, true)) { + Response res3; + if (!detail::process_client_socket( + socket.sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) { + Request req3; + req3.method = "CONNECT"; + req3.path = host_and_port_; + req3.headers.insert(detail::make_digest_authentication_header( + req3, auth, 1, detail::random_string(10), + proxy_digest_auth_username_, proxy_digest_auth_password_, + true)); + return process_request(strm, req3, res3, false, error); + })) { + // Thread-safe to close everything because we are assuming there are + // no requests in flight + shutdown_ssl(socket, true); + shutdown_socket(socket); + close_socket(socket); + success = false; + return false; + } + } + } else { + res = res2; + return false; + } + } + + return true; +} + +inline bool SSLClient::load_certs() { + bool ret = true; + + std::call_once(initialize_cert_, [&]() { + std::lock_guard guard(ctx_mutex_); + if (!ca_cert_file_path_.empty()) { + if (!SSL_CTX_load_verify_locations(ctx_, ca_cert_file_path_.c_str(), + nullptr)) { + ret = false; + } + } else if (!ca_cert_dir_path_.empty()) { + if (!SSL_CTX_load_verify_locations(ctx_, nullptr, + ca_cert_dir_path_.c_str())) { + ret = false; + } + } else { +#ifdef _WIN32 + detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); +#else + SSL_CTX_set_default_verify_paths(ctx_); +#endif + } + }); + + return ret; +} + +inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { + auto ssl = detail::ssl_new( + socket.sock, ctx_, ctx_mutex_, + [&](SSL *ssl2) { + if (server_certificate_verification_) { + if (!load_certs()) { + error = Error::SSLLoadingCerts; + return false; + } + SSL_set_verify(ssl2, SSL_VERIFY_NONE, nullptr); + } + + if (!detail::ssl_connect_or_accept_nonblocking( + socket.sock, ssl2, SSL_connect, connection_timeout_sec_, + connection_timeout_usec_)) { + error = Error::SSLConnection; + return false; + } + + if (server_certificate_verification_) { + verify_result_ = SSL_get_verify_result(ssl2); + + if (verify_result_ != X509_V_OK) { + error = Error::SSLServerVerification; + return false; + } + + auto server_cert = SSL_get_peer_certificate(ssl2); + + if (server_cert == nullptr) { + error = Error::SSLServerVerification; + return false; + } + + if (!verify_host(server_cert)) { + X509_free(server_cert); + error = Error::SSLServerVerification; + return false; + } + X509_free(server_cert); + } + + return true; + }, + [&](SSL *ssl2) { + SSL_set_tlsext_host_name(ssl2, host_.c_str()); + return true; + }); + + if (ssl) { + socket.ssl = ssl; + return true; + } + + shutdown_socket(socket); + close_socket(socket); + return false; +} + +inline void SSLClient::shutdown_ssl(Socket &socket, bool shutdown_gracefully) { + shutdown_ssl_impl(socket, shutdown_gracefully); +} + +inline void SSLClient::shutdown_ssl_impl(Socket &socket, + bool shutdown_gracefully) { + if (socket.sock == INVALID_SOCKET) { + assert(socket.ssl == nullptr); + return; + } + if (socket.ssl) { + detail::ssl_delete(ctx_mutex_, socket.ssl, shutdown_gracefully); + socket.ssl = nullptr; + } + assert(socket.ssl == nullptr); +} + +inline bool +SSLClient::process_socket(const Socket &socket, + std::function callback) { + assert(socket.ssl); + return detail::process_client_socket_ssl( + socket.ssl, socket.sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, std::move(callback)); +} + +inline bool SSLClient::is_ssl() const { return true; } + +inline bool SSLClient::verify_host(X509 *server_cert) const { + /* Quote from RFC2818 section 3.1 "Server Identity" + + If a subjectAltName extension of type dNSName is present, that MUST + be used as the identity. Otherwise, the (most specific) Common Name + field in the Subject field of the certificate MUST be used. Although + the use of the Common Name is existing practice, it is deprecated and + Certification Authorities are encouraged to use the dNSName instead. + + Matching is performed using the matching rules specified by + [RFC2459]. If more than one identity of a given type is present in + the certificate (e.g., more than one dNSName name, a match in any one + of the set is considered acceptable.) Names may contain the wildcard + character * which is considered to match any single domain name + component or component fragment. E.g., *.a.com matches foo.a.com but + not bar.foo.a.com. f*.com matches foo.com but not bar.com. + + In some cases, the URI is specified as an IP address rather than a + hostname. In this case, the iPAddress subjectAltName must be present + in the certificate and must exactly match the IP in the URI. + + */ + return verify_host_with_subject_alt_name(server_cert) || + verify_host_with_common_name(server_cert); +} + +inline bool +SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { + auto ret = false; + + auto type = GEN_DNS; + + struct in6_addr addr6; + struct in_addr addr; + size_t addr_len = 0; + +#ifndef __MINGW32__ + if (inet_pton(AF_INET6, host_.c_str(), &addr6)) { + type = GEN_IPADD; + addr_len = sizeof(struct in6_addr); + } else if (inet_pton(AF_INET, host_.c_str(), &addr)) { + type = GEN_IPADD; + addr_len = sizeof(struct in_addr); + } +#endif + + auto alt_names = static_cast( + X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr)); + + if (alt_names) { + auto dsn_matched = false; + auto ip_mached = false; + + auto count = sk_GENERAL_NAME_num(alt_names); + + for (decltype(count) i = 0; i < count && !dsn_matched; i++) { + auto val = sk_GENERAL_NAME_value(alt_names, i); + if (val->type == type) { + auto name = (const char *)ASN1_STRING_get0_data(val->d.ia5); + auto name_len = (size_t)ASN1_STRING_length(val->d.ia5); + + switch (type) { + case GEN_DNS: dsn_matched = check_host_name(name, name_len); break; + + case GEN_IPADD: + if (!memcmp(&addr6, name, addr_len) || + !memcmp(&addr, name, addr_len)) { + ip_mached = true; + } + break; + } + } + } + + if (dsn_matched || ip_mached) { ret = true; } + } + + GENERAL_NAMES_free((STACK_OF(GENERAL_NAME) *)alt_names); + return ret; +} + +inline bool SSLClient::verify_host_with_common_name(X509 *server_cert) const { + const auto subject_name = X509_get_subject_name(server_cert); + + if (subject_name != nullptr) { + char name[BUFSIZ]; + auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName, + name, sizeof(name)); + + if (name_len != -1) { + return check_host_name(name, static_cast(name_len)); + } + } + + return false; +} + +inline bool SSLClient::check_host_name(const char *pattern, + size_t pattern_len) const { + if (host_.size() == pattern_len && host_ == pattern) { return true; } + + // Wildcard match + // https://bugs.launchpad.net/ubuntu/+source/firefox-3.0/+bug/376484 + std::vector pattern_components; + detail::split(&pattern[0], &pattern[pattern_len], '.', + [&](const char *b, const char *e) { + pattern_components.emplace_back(std::string(b, e)); + }); + + if (host_components_.size() != pattern_components.size()) { return false; } + + auto itr = pattern_components.begin(); + for (const auto &h : host_components_) { + auto &p = *itr; + if (p != h && p != "*") { + auto partial_match = (p.size() > 0 && p[p.size() - 1] == '*' && + !p.compare(0, p.size() - 1, h)); + if (!partial_match) { return false; } + } + ++itr; + } + + return true; +} +#endif + +// Universal client implementation +inline Client::Client(const std::string &scheme_host_port) + : Client(scheme_host_port, std::string(), std::string()) {} + +inline Client::Client(const std::string &scheme_host_port, + const std::string &client_cert_path, + const std::string &client_key_path) { + const static std::regex re( + R"((?:([a-z]+):\/\/)?(?:\[([\d:]+)\]|([^:/?#]+))(?::(\d+))?)"); + + std::smatch m; + if (std::regex_match(scheme_host_port, m, re)) { + auto scheme = m[1].str(); + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (!scheme.empty() && (scheme != "http" && scheme != "https")) { +#else + if (!scheme.empty() && scheme != "http") { +#endif +#ifndef CPPHTTPLIB_NO_EXCEPTIONS + std::string msg = "'" + scheme + "' scheme is not supported."; + throw std::invalid_argument(msg); +#endif + return; + } + + auto is_ssl = scheme == "https"; + + auto host = m[2].str(); + if (host.empty()) { host = m[3].str(); } + + auto port_str = m[4].str(); + auto port = !port_str.empty() ? std::stoi(port_str) : (is_ssl ? 443 : 80); + + if (is_ssl) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + cli_ = detail::make_unique(host, port, + client_cert_path, client_key_path); + is_ssl_ = is_ssl; +#endif + } else { + cli_ = detail::make_unique(host, port, + client_cert_path, client_key_path); + } + } else { + cli_ = detail::make_unique(scheme_host_port, 80, + client_cert_path, client_key_path); + } +} + +inline Client::Client(const std::string &host, int port) + : cli_(detail::make_unique(host, port)) {} + +inline Client::Client(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path) + : cli_(detail::make_unique(host, port, client_cert_path, + client_key_path)) {} + +inline Client::~Client() {} + +inline bool Client::is_valid() const { + return cli_ != nullptr && cli_->is_valid(); +} + +inline Result Client::Get(const std::string &path) { return cli_->Get(path); } +inline Result Client::Get(const std::string &path, const Headers &headers) { + return cli_->Get(path, headers); +} +inline Result Client::Get(const std::string &path, Progress progress) { + return cli_->Get(path, std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + Progress progress) { + return cli_->Get(path, headers, std::move(progress)); +} +inline Result Client::Get(const std::string &path, + ContentReceiver content_receiver) { + return cli_->Get(path, std::move(content_receiver)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver) { + return cli_->Get(path, headers, std::move(content_receiver)); +} +inline Result Client::Get(const std::string &path, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, std::move(content_receiver), std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, headers, std::move(content_receiver), + std::move(progress)); +} +inline Result Client::Get(const std::string &path, + ResponseHandler response_handler, + ContentReceiver content_receiver) { + return cli_->Get(path, std::move(response_handler), + std::move(content_receiver)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver) { + return cli_->Get(path, headers, std::move(response_handler), + std::move(content_receiver)); +} +inline Result Client::Get(const std::string &path, + ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Params ¶ms, + const Headers &headers, Progress progress) { + return cli_->Get(path, params, headers, progress); +} +inline Result Client::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, params, headers, content_receiver, progress); +} +inline Result Client::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, params, headers, response_handler, content_receiver, + progress); +} + +inline Result Client::Head(const std::string &path) { return cli_->Head(path); } +inline Result Client::Head(const std::string &path, const Headers &headers) { + return cli_->Head(path, headers); +} + +inline Result Client::Post(const std::string &path) { return cli_->Post(path); } +inline Result Client::Post(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return cli_->Post(path, body, content_length, content_type); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return cli_->Post(path, headers, body, content_length, content_type); +} +inline Result Client::Post(const std::string &path, const std::string &body, + const std::string &content_type) { + return cli_->Post(path, body, content_type); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return cli_->Post(path, headers, body, content_type); +} +inline Result Client::Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Post(path, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Post(path, std::move(content_provider), content_type); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Post(path, headers, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Post(path, headers, std::move(content_provider), content_type); +} +inline Result Client::Post(const std::string &path, const Params ¶ms) { + return cli_->Post(path, params); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const Params ¶ms) { + return cli_->Post(path, headers, params); +} +inline Result Client::Post(const std::string &path, + const MultipartFormDataItems &items) { + return cli_->Post(path, items); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items) { + return cli_->Post(path, headers, items); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const std::string &boundary) { + return cli_->Post(path, headers, items, boundary); +} +inline Result Client::Put(const std::string &path) { return cli_->Put(path); } +inline Result Client::Put(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return cli_->Put(path, body, content_length, content_type); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return cli_->Put(path, headers, body, content_length, content_type); +} +inline Result Client::Put(const std::string &path, const std::string &body, + const std::string &content_type) { + return cli_->Put(path, body, content_type); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return cli_->Put(path, headers, body, content_type); +} +inline Result Client::Put(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Put(path, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Put(path, std::move(content_provider), content_type); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Put(path, headers, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Put(path, headers, std::move(content_provider), content_type); +} +inline Result Client::Put(const std::string &path, const Params ¶ms) { + return cli_->Put(path, params); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const Params ¶ms) { + return cli_->Put(path, headers, params); +} +inline Result Client::Patch(const std::string &path) { + return cli_->Patch(path); +} +inline Result Client::Patch(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return cli_->Patch(path, body, content_length, content_type); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return cli_->Patch(path, headers, body, content_length, content_type); +} +inline Result Client::Patch(const std::string &path, const std::string &body, + const std::string &content_type) { + return cli_->Patch(path, body, content_type); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return cli_->Patch(path, headers, body, content_type); +} +inline Result Client::Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Patch(path, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Patch(path, std::move(content_provider), content_type); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Patch(path, headers, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Patch(path, headers, std::move(content_provider), content_type); +} +inline Result Client::Delete(const std::string &path) { + return cli_->Delete(path); +} +inline Result Client::Delete(const std::string &path, const Headers &headers) { + return cli_->Delete(path, headers); +} +inline Result Client::Delete(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return cli_->Delete(path, body, content_length, content_type); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return cli_->Delete(path, headers, body, content_length, content_type); +} +inline Result Client::Delete(const std::string &path, const std::string &body, + const std::string &content_type) { + return cli_->Delete(path, body, content_type); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return cli_->Delete(path, headers, body, content_type); +} +inline Result Client::Options(const std::string &path) { + return cli_->Options(path); +} +inline Result Client::Options(const std::string &path, const Headers &headers) { + return cli_->Options(path, headers); +} + +inline bool Client::send(Request &req, Response &res, Error &error) { + return cli_->send(req, res, error); +} + +inline Result Client::send(const Request &req) { return cli_->send(req); } + +inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); } + +inline socket_t Client::socket() const { return cli_->socket(); } + +inline void Client::stop() { cli_->stop(); } + +inline void +Client::set_hostname_addr_map(std::map addr_map) { + cli_->set_hostname_addr_map(std::move(addr_map)); +} + +inline void Client::set_default_headers(Headers headers) { + cli_->set_default_headers(std::move(headers)); +} + +inline void Client::set_address_family(int family) { + cli_->set_address_family(family); +} + +inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); } + +inline void Client::set_socket_options(SocketOptions socket_options) { + cli_->set_socket_options(std::move(socket_options)); +} + +inline void Client::set_connection_timeout(time_t sec, time_t usec) { + cli_->set_connection_timeout(sec, usec); +} + +inline void Client::set_read_timeout(time_t sec, time_t usec) { + cli_->set_read_timeout(sec, usec); +} + +inline void Client::set_write_timeout(time_t sec, time_t usec) { + cli_->set_write_timeout(sec, usec); +} + +inline void Client::set_basic_auth(const std::string &username, + const std::string &password) { + cli_->set_basic_auth(username, password); +} +inline void Client::set_bearer_token_auth(const std::string &token) { + cli_->set_bearer_token_auth(token); +} +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_digest_auth(const std::string &username, + const std::string &password) { + cli_->set_digest_auth(username, password); +} +#endif + +inline void Client::set_keep_alive(bool on) { cli_->set_keep_alive(on); } +inline void Client::set_follow_location(bool on) { + cli_->set_follow_location(on); +} + +inline void Client::set_url_encode(bool on) { cli_->set_url_encode(on); } + +inline void Client::set_compress(bool on) { cli_->set_compress(on); } + +inline void Client::set_decompress(bool on) { cli_->set_decompress(on); } + +inline void Client::set_interface(const std::string &intf) { + cli_->set_interface(intf); +} + +inline void Client::set_proxy(const std::string &host, int port) { + cli_->set_proxy(host, port); +} +inline void Client::set_proxy_basic_auth(const std::string &username, + const std::string &password) { + cli_->set_proxy_basic_auth(username, password); +} +inline void Client::set_proxy_bearer_token_auth(const std::string &token) { + cli_->set_proxy_bearer_token_auth(token); +} +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_proxy_digest_auth(const std::string &username, + const std::string &password) { + cli_->set_proxy_digest_auth(username, password); +} +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::enable_server_certificate_verification(bool enabled) { + cli_->enable_server_certificate_verification(enabled); +} +#endif + +inline void Client::set_logger(Logger logger) { cli_->set_logger(logger); } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path) { + cli_->set_ca_cert_path(ca_cert_file_path, ca_cert_dir_path); +} + +inline void Client::set_ca_cert_store(X509_STORE *ca_cert_store) { + if (is_ssl_) { + static_cast(*cli_).set_ca_cert_store(ca_cert_store); + } else { + cli_->set_ca_cert_store(ca_cert_store); + } +} + +inline long Client::get_openssl_verify_result() const { + if (is_ssl_) { + return static_cast(*cli_).get_openssl_verify_result(); + } + return -1; // NOTE: -1 doesn't match any of X509_V_ERR_??? +} + +inline SSL_CTX *Client::ssl_context() const { + if (is_ssl_) { return static_cast(*cli_).ssl_context(); } + return nullptr; +} +#endif + +// ---------------------------------------------------------------------------- + +} // namespace httplib + +#endif // CPPHTTPLIB_HTTPLIB_H diff --git a/tjp/core/http/remote-run b/tjp/core/http/remote-run new file mode 100755 index 0000000..238f178 --- /dev/null +++ b/tjp/core/http/remote-run @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +set -euo pipefail + +usage() { + cat <<'EOF' +Usage: + ./remote-run + +Environment variables: + REMOTE_HOST Default: host.docker.internal + REMOTE_PORT Default: 6666 + REMOTE_TOKEN Optional shared token +EOF +} + +if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then + usage + exit 0 +fi + +if [[ $# -lt 1 ]]; then + usage + exit 2 +fi + +cmd="$1" +host="${REMOTE_HOST:-host.docker.internal}" +port="${REMOTE_PORT:-6666}" +token="${REMOTE_TOKEN:-}" + +url="http://${host}:${port}/run/${cmd}" + +curl_args=(--silent --show-error --no-buffer "$url") +if [[ -n "$token" ]]; then + curl_args=(-H "X-Remote-Token: ${token}" "${curl_args[@]}") +fi + +exec curl "${curl_args[@]}" diff --git a/tjp/core/http/require_header.hpp b/tjp/core/http/require_header.hpp new file mode 100755 index 0000000..ca2d95f --- /dev/null +++ b/tjp/core/http/require_header.hpp @@ -0,0 +1,18 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Types.hpp" + +namespace tjp::core::http { + +inline +auto require_header(const Request& req, const String &key) +{ + if (auto header = req.header(key)) + return String(*header); + + throw Exception { "Missing header" }; +} + +} // namespace diff --git a/tjp/core/http/temp/compression/_delete/zlib.cpp b/tjp/core/http/temp/compression/_delete/zlib.cpp new file mode 100644 index 0000000..c4afa03 --- /dev/null +++ b/tjp/core/http/temp/compression/_delete/zlib.cpp @@ -0,0 +1,588 @@ +// TJP COPYRIGHT HEADER + +#include "zlib.hpp" + +#include +#include +#include + +#include + +namespace tjp::core::compression::zlib { + +struct CompressContexts { + Mutex mutex; + Map> contextsByLevel; + + ~CompressContexts() + { + for (auto &[l, contexts] : contextsByLevel) + for (auto *c : contexts) + free((z_streamp)c); + } + + void *getContext(int level) + { + { + auto lock = lock_of(mutex); + + auto &contexts = contextsByLevel[level]; + if (!contexts.empty()) + { + auto *v = contexts.back(); + contexts.pop_back(); + return v; + } + } + + auto z = (z_streamp)calloc(1, sizeof(z_stream)); + deflateInit(z, level); + return z; + } + + void putContext(int level, void *v) + { + auto lock = lock_of(mutex); + contextsByLevel[level].push_back(v); + } +} ; + +struct DecompressContexts +{ + Mutex mutex; + Vector contexts; + + ~DecompressContexts() + { + for (auto *c : contexts) + free((z_streamp)c); + } + + void *getContext() + { + { + auto lock = lock_of(mutex); + + if (!contexts.empty()) + { + auto *v = contexts.back(); + contexts.pop_back(); + return v; + } + } + + auto z = (z_streamp)calloc(1, sizeof(z_stream)); + inflateInit(z); + return z; + } + + void putContext(void *v) + { + auto lock = lock_of(mutex); + contexts.push_back(v); + } +} ; + +CompressContexts compressContexts; +DecompressContexts decompressContexts; + +// -------------- + +bool clearStream(void *p, bool inflate) +{ + auto z = (z_streamp)p; + + if (inflate) + return (inflateReset(z) == Z_OK); + else + return (deflateReset(z) == Z_OK); +} + + +bool setDict(void *p, bool inflate, char *dict, size_t size) +{ + auto z = (z_streamp)p; + + int error = 0; + if (inflate) + error = inflateSetDictionary(z, (const Bytef *)dict, (uInt)size); + else + error = deflateSetDictionary(z, (const Bytef *)dict, (uInt)size); + + return error == Z_OK; +} + +bool getDict(void *p, bool inflate, char *dict, size_t size) +{ + auto z = (z_streamp)p; + + auto size_ = (uInt)size; + + char store[32768]; + + int error = 0; + if (inflate) + error = inflateGetDictionary(z, (Bytef *)&store[0], &size_); + else + error = deflateGetDictionary(z, (Bytef *)&store[0], &size_); + + // zlib seems to store from end to front + auto offset = + size_ > size ? + size_ - size : + 0; + + mem_copy(dict, &store[offset], size); + + return error == Z_OK; +} + + +// -------------- + +CompressUnordered::CompressUnordered(int level_) : + level(level_) +{ +} + +size_t CompressUnordered::execute(char *dest_, size_t destLen_, const char *source_, size_t sourceLen_) +{ + const Bytef *source = (const Bytef *)source_; + uLongf sourceLen = sourceLen_; + Bytef *dest = (Bytef *)dest_; + uLongf destLen = destLen_; + + auto context = (z_streamp)compressContexts.getContext(level); + + context->next_in = (Bytef *)source; + context->avail_in = (uInt)sourceLen; + + context->next_out = dest; + context->avail_out = (uInt)destLen; + + auto result = deflate(context, Z_FINISH); + auto toSize = context->total_out; + + compressContexts.putContext(level, context); + + if (result != Z_STREAM_END) + return 0; + + return toSize; +} + +size_t UncompressUnordered::execute(char *dest_, size_t destLen_, const char *source_, size_t sourceLen_) +{ + const Bytef *source = (const Bytef *)source_; + uLongf sourceLen = sourceLen_; + Bytef *dest = (Bytef *)dest_; + uLongf destLen = destLen_; + + auto context = (z_streamp)decompressContexts.getContext(); + + context->next_in = (Bytef *)source; + context->avail_in = (uInt)sourceLen; + + context->next_out = dest; + context->avail_out = (uInt)destLen; + + auto result = inflate(context, Z_FINISH); + auto toSize = context->total_out; + + decompressContexts.putContext(context); + + if (result != Z_STREAM_END) + return 0; + + return toSize; +} + + +// -------- + + + +CompressOrdered::CompressOrdered(int level_) : + level(level_) +{ + memset(dictionary, 0, dictionarySize); +} + +CompressOrdered::~CompressOrdered() +{ +} + +size_t CompressOrdered::execute(char *dest_, size_t destLen_, const char *source_, size_t sourceLen_) +{ + const Bytef *source = (const Bytef *)source_; + uLongf sourceLen = sourceLen_; + Bytef *dest = (Bytef *)dest_; + uLongf destLen = destLen_; + + auto context = (z_streamp)compressContexts.getContext(level); + + if (!clearStream(context, false)) + return 0; + + if (!setDict(context, false, dictionary, dictionarySize)) + return 0; + + context->next_in = (Bytef *)source; + context->avail_in = (uInt)sourceLen; + + context->next_out = dest; + context->avail_out = (uInt)destLen; + + auto result = deflate(context, Z_FINISH); + auto toSize = context->total_out; + + if (result == Z_STREAM_END) + getDict(context, false, dictionary, dictionarySize); + + compressContexts.putContext(level, context); + + if (result != Z_STREAM_END) + return 0; + + return toSize; +} + +size_t CompressOrdered::execute(Vector &dest, const containers::MemorySegment &source) +{ + auto context = (z_streamp)compressContexts.getContext(level); + + if (!clearStream(context, false)) + return 0; + + if (!setDict(context, false, dictionary, dictionarySize)) + return 0; + + context->next_in = (Bytef *)source.data(); + context->avail_in = (uInt)source.size(); + + auto sizeOffset = dest.size(); + dest.resize(dest.size() + sizeof(u32)); + auto bos = dest.size(); + + int result = 0; + + while(1) + { + auto newSize = std::max(bos + context->total_out + 2048, (size_t)16384); + dest.resize(newSize); + + auto eos = bos + context->total_out; + context->next_out = (Bytef *)dest.data() + eos; + context->avail_out = (uInt)dest.size() - eos; + + result = deflate(context, Z_FINISH); + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + break; + } + + getDict(context, false, dictionary, dictionarySize); + + u32 toSize = context->total_out; + mem_copy(dest.data() + sizeOffset, (char *)&toSize, sizeof(u32)); + + auto eos = bos + context->total_out; + dest.resize(eos); + + compressContexts.putContext(level, context); + + if (result != Z_STREAM_END) + return 0; + + return toSize; +} + +UncompressOrdered::UncompressOrdered() +{ + memset(dictionary, 0, dictionarySize); +} + +UncompressOrdered::~UncompressOrdered() +{ +} + + +size_t UncompressOrdered::execute(char *dest_, size_t destLen_, const char *source_, size_t sourceLen_) +{ + const Bytef *source = (const Bytef *)source_; + uLongf sourceLen = sourceLen_; + Bytef *dest = (Bytef *)dest_; + uLongf destLen = destLen_; + + auto context = (z_streamp)decompressContexts.getContext(); + + if (!clearStream(context, true)) + return 0; + + context->next_in = (Bytef *)source; + context->avail_in = (uInt)sourceLen; + + context->next_out = dest; + context->avail_out = (uInt)destLen; + + auto result = inflate(context, Z_FINISH); + + if (result == Z_NEED_DICT) + { + if (!setDict(context, true, dictionary, dictionarySize)) + return 0; + + result = inflate(context, Z_FINISH); + } + + auto toSize = context->total_out; + + if (result == Z_STREAM_END) + getDict(context, true, dictionary, dictionarySize); + + decompressContexts.putContext(context); + + if (result != Z_STREAM_END) + return 0; + + return toSize; +} + +size_t UncompressOrdered::execute(const containers::MemorySegment &dest, const containers::MemorySegment &source) +{ + u32 size = 0; + mem_copy((char *)&size, source.data(), sizeof(u32)); + auto dataOffset = sizeof(u32); + + auto context = (z_streamp)decompressContexts.getContext(); + + if (!clearStream(context, true)) + return 0; + + context->next_in = (Bytef *)source.data() + dataOffset; + context->avail_in = size; + + int result = 0; + + while(1) + { + context->next_out = (Bytef *)dest.data() + context->total_out; + context->avail_out = (uInt)dest.size() - context->total_out; + + result = inflate(context, Z_FINISH); + + if (result == Z_NEED_DICT) + { + if (!setDict(context, true, dictionary, dictionarySize)) + return 0; + + result = inflate(context, Z_FINISH); + } + + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + break; + } + + auto toSize = context->total_out; + debug_assert(toSize == dest.size()); + + getDict(context, true, dictionary, dictionarySize); + + decompressContexts.putContext(context); + + if (result != Z_STREAM_END) + return 0; + + return size + sizeof(32); +} + +size_t UncompressOrdered::execute(Vector &dest, const containers::MemorySegment &source) +{ + u32 size = 0; + mem_copy((char *)&size, source.data(), sizeof(u32)); + auto dataOffset = sizeof(u32); + + auto context = (z_streamp)decompressContexts.getContext(); + + if (!clearStream(context, true)) + return 0; + + context->next_in = (Bytef *)source.data() + dataOffset; + context->avail_in = size; + + auto offset = 0; + int result = 0; + + while(1) + { + auto newSize = std::max(offset + context->total_out + 2048, (size_t)16384); + dest.resize(newSize); + + context->next_out = (Bytef *)dest.data() + offset + context->total_out; + context->avail_out = (uInt)dest.size() - offset - context->total_out; + + result = inflate(context, Z_FINISH); + + if (result == Z_NEED_DICT) + { + if (!setDict(context, true, dictionary, DictionarySize)) + return 0; + + result = inflate(context, Z_FINISH); + } + + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + break; + } + + auto toSize = context->total_out; + dest.resize(toSize); + + getDict(context, true, dictionary, dictionarySize); + + decompressContexts.putContext(context); + + if (result != Z_STREAM_END) + return 0; + + return size + sizeof(32); +} + +// --------- + +CompressStream::CompressStream() +{ + memset(dictionary, 0, dictionarySize); +} + +size_t CompressStream::with(Vector &v, Function &&f) +{ + auto context = (z_streamp)decompressContexts.getContext(); + + if (!clearStream(context, true)) + return 0; + + if (!setDict(context, true, dictionary, dictionarySize)) + return 0; + + i = (I *)context; + v_ = &v; + p = v.size(); + + auto initialSize = v.size(); + f(*this); + + v.resize(p); + auto finalSize = v.size(); + + decompressContexts.putContext(context); + + return finalSize - initialSize; +} + +size_t CompressStream::write(const containers::MemorySegment &s) +{ + auto &v = *v_; + + auto context = (z_streamp)i; + context->next_in = (Bytef *)s.data(); + context->avail_in = (uInt)s.size(); + + // beginning of stream + auto bos = p; + + int result = 0; + + while(1) + { + auto newSize = std::max(bos + context->total_out + 2048, (size_t)16384); + v.resize(newSize); + + context->next_out = (Bytef *)v.data() + bos + context->total_out; + context->avail_out = (uInt)v.size() - bos - context->total_out; + + result = deflate(context, Z_FINISH); + + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + break; + } + + auto toSize = context->total_out; + p += toSize; + + return toSize; +} + +DecompressStream::DecompressStream() +{ + memset(dictionary, 0, dictionarySize); +} + +size_t DecompressStream::with(const containers::MemorySegment &v, Function &&f) +{ + auto context = (z_streamp)decompressContexts.getContext(); + + if (!clearStream(context, true)) + return 0; + + if (!setDict(context, true, dictionary, dictionarySize)) + return 0; + + i = (I *)context; + + context->next_in = (Bytef *)v.data(); + context->avail_in = (uInt)v.size(); + + f(*this); + + auto sizeRead = context->total_in; + decompressContexts.putContext(context); + + return sizeRead; +} + +size_t DecompressStream::read(const containers::MemorySegment &s) +{ + // beginning of stream + int result = 0; + + auto context = (z_streamp)i; + context->next_out = (Bytef *)s.data(); + context->avail_out = (uInt)s.size(); + + while(1) + { + result = inflate(context, Z_FINISH); + + if (result == Z_NEED_DICT) + { + if (!setDict(context, true, dictionary, DictionarySize)) + return 0; + + result = inflate(context, Z_FINISH); + } + + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + break; + } + + auto toSize = context->total_out; + + return toSize; +} + +} // namespace diff --git a/tjp/core/http/temp/compression/_delete/zlib.hpp b/tjp/core/http/temp/compression/_delete/zlib.hpp new file mode 100644 index 0000000..7649975 --- /dev/null +++ b/tjp/core/http/temp/compression/_delete/zlib.hpp @@ -0,0 +1,88 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "zlib.h" +#include +#include +#include + +namespace tjp::core::compression::zlib { + +constexpr size_t DictionarySize = 256; +using Dictionary = char[DictionarySize]; + +struct CompressUnordered +{ + static constexpr auto dictionarySize = DictionarySize; + + CompressUnordered(int level); + int level; + + size_t execute(char *dest, size_t destLen, const char *source, size_t sourceLen); +}; + +struct UncompressUnordered +{ + size_t execute(char *dest, size_t destLen, const char *source, size_t sourceLen); +} ; + +struct CompressOrdered { + + static constexpr auto dictionarySize = DictionarySize; + + CompressOrdered(int level); + ~CompressOrdered(); + + int level; + Dictionary dictionary; + + size_t execute(char *dest, size_t destLen, const char *source, size_t sourceLen); + size_t execute(Vector &, const containers::MemorySegment &); +}; + +struct UncompressOrdered +{ + static constexpr auto dictionarySize = DictionarySize; + + UncompressOrdered(); + ~UncompressOrdered(); + + Dictionary dictionary; + + size_t execute(char *dest, size_t destLen, const char *source, size_t sourceLen); + size_t execute(const containers::MemorySegment &, const containers::MemorySegment &); + size_t execute(Vector &, const containers::MemorySegment &); +} ; + +struct CompressStream +{ + static constexpr auto dictionarySize = DictionarySize; + Dictionary dictionary; + + using I = void; + I *i; + Vector *v_; + size_t p; + + CompressStream(); + + size_t with(Vector &, Function &&f); + size_t write(const containers::MemorySegment &); +} ; + +struct DecompressStream +{ + static constexpr auto dictionarySize = DictionarySize; + Dictionary dictionary; + + using I = void; + I *i; + + DecompressStream(); + + size_t with(const containers::MemorySegment &, Function &&f); + size_t read(const containers::MemorySegment &); +} ; + +} // namespace diff --git a/tjp/core/http/temp/compression/_tests/zlib.cpp b/tjp/core/http/temp/compression/_tests/zlib.cpp new file mode 100644 index 0000000..776ef8d --- /dev/null +++ b/tjp/core/http/temp/compression/_tests/zlib.cpp @@ -0,0 +1,282 @@ +// TJP COPYRIGHT HEADER + +#include +#include + +#include +#include + +namespace tjp::core::compression::zlib { +namespace { + +SCENARIO("tjp::core::compression::zlib") +{ + GIVEN("read and write") + { + CompressStream out(9); + Vector packet; + + struct Values { + s16 a; + Vector b; + u32 c; + Vector d; + }; + + Values expected { + .a = 42, + .b = { 1, 2, 3, 4, 5 }, + .c = 13 + }; + + auto largeNumber = 235324; + expected.d.assign(largeNumber, 12345); + + out.with(packet, [&](auto &s) { + s.value(expected.a); + s.vector(expected.b); + s.value(expected.c); + s.vector(expected.d); + }); + + Values expectedB { + .a = 124, + .b = { 5, 2, 3, 4, 5 }, + .c = 23 + }; + expectedB.d.assign(largeNumber, 6666); + + out.with(packet, [&](auto &s) { + s.value(expectedB.a); + s.vector(expectedB.b); + s.value(expectedB.c); + s.vector(expectedB.d); + }); + + Values real; + real.b.resize(5); + real.d.resize(largeNumber); + + DecompressStream in; + + auto segment = containers::memory_segment_vector(packet); + + in.with(segment, [&](auto &s) { + s.value(real.a); + s.vector(real.b); + s.value(real.c); + s.vector(real.d); + }); + + REQUIRE(real.a == expected.a); + REQUIRE(real.b == expected.b); + REQUIRE(real.c == expected.c); + + auto d_hash = hash_of(real.d); + auto d_expectedHash = hash_of(expected.d); + REQUIRE(d_hash == d_expectedHash); + + + Values realB; + realB.b.resize(5); + realB.d.resize(largeNumber); + + in.with(segment, [&](auto &s) { + s.value(realB.a); + s.vector(realB.b); + s.value(realB.c); + s.vector(realB.d); + }); + + REQUIRE(realB.a == expectedB.a); + REQUIRE(realB.b == expectedB.b); + REQUIRE(realB.c == expectedB.c); + + auto bd_hash = hash_of(realB.d); + auto bd_expectedHash = hash_of(expectedB.d); + REQUIRE(bd_hash == bd_expectedHash); + } + + GIVEN("back-to-back streams") + { + CompressStream out(9); + Vector packet; + + Vector expectedA; + Vector expectedB; + expectedA.assign(32768, 1111); + expectedB.assign(32768, 2222); + + auto firstCompressedSize = out.with(packet, [&](auto &s) { + s.vector(expectedA); + }); + auto secondCompressedSize = out.with(packet, [&](auto &s) { + s.vector(expectedB); + }); + + REQUIRE(firstCompressedSize > 0); + REQUIRE(secondCompressedSize > 0); + + DecompressStream in; + auto segment = containers::memory_segment_vector(packet); + + Vector realA; + realA.resize(expectedA.size()); + in.with(segment, [&](auto &s) { + s.vector(realA); + }); + + auto a_hash = hash_of(realA); + auto a_expectedHash = hash_of(expectedA); + REQUIRE(a_hash == a_expectedHash); + REQUIRE(segment.size() == secondCompressedSize); + + Vector realB; + realB.resize(expectedB.size()); + in.with(segment, [&](auto &s) { + s.vector(realB); + }); + + auto b_hash = hash_of(realB); + auto b_expectedHash = hash_of(expectedB); + REQUIRE(b_hash == b_expectedHash); + REQUIRE(segment.size() == 0); + } + + GIVEN("gzip read and write") + { + CompressStream out(9, Format::Gzip); + Vector packet; + + struct Values { + s16 a; + Vector b; + u32 c; + Vector d; + }; + + Values expected { + .a = 17, + .b = { 9, 8, 7, 6, 5 }, + .c = 99 + }; + + auto largeNumber = 32768; + expected.d.assign(largeNumber, 4321); + + Values expectedB { + .a = 18, + .b = { 1, 3, 5, 7, 9 }, + .c = 100 + }; + expectedB.d.assign(largeNumber, 8765); + + out.with(packet, [&](auto &s) { + s.value(expected.a); + s.vector(expected.b); + s.value(expected.c); + s.vector(expected.d); + }); + + out.with(packet, [&](auto &s) { + s.value(expectedB.a); + s.vector(expectedB.b); + s.value(expectedB.c); + s.vector(expectedB.d); + }); + + REQUIRE(packet.size() >= 2); + REQUIRE((unsigned char)packet[0] == 0x1f); + REQUIRE((unsigned char)packet[1] == 0x8b); + + Values real; + real.b.resize(expected.b.size()); + real.d.resize(largeNumber); + + DecompressStream in; + auto segment = containers::memory_segment_vector(packet); + in.with(segment, [&](auto &s) { + s.value(real.a); + s.vector(real.b); + s.value(real.c); + s.vector(real.d); + }); + + REQUIRE(real.a == expected.a); + REQUIRE(real.b == expected.b); + REQUIRE(real.c == expected.c); + auto d_hash = hash_of(real.d); + auto d_expectedHash = hash_of(expected.d); + REQUIRE(d_hash == d_expectedHash); + + Values realB; + realB.b.resize(expectedB.b.size()); + realB.d.resize(largeNumber); + in.with(segment, [&](auto &s) { + s.value(realB.a); + s.vector(realB.b); + s.value(realB.c); + s.vector(realB.d); + }); + + REQUIRE(realB.a == expectedB.a); + REQUIRE(realB.b == expectedB.b); + REQUIRE(realB.c == expectedB.c); + auto bd_hash = hash_of(realB.d); + auto bd_expectedHash = hash_of(expectedB.d); + REQUIRE(bd_hash == bd_expectedHash); + REQUIRE(segment.size() == 0); + } + + GIVEN("gzip back-to-back streams") + { + CompressStream out(9, Format::Gzip); + Vector packet; + + Vector expectedA; + Vector expectedB; + expectedA.assign(32768, 1111); + expectedB.assign(32768, 2222); + + auto firstCompressedSize = out.with(packet, [&](auto &s) { + s.vector(expectedA); + }); + auto secondCompressedSize = out.with(packet, [&](auto &s) { + s.vector(expectedB); + }); + + REQUIRE(firstCompressedSize > 0); + REQUIRE(secondCompressedSize > 0); + REQUIRE(packet.size() >= 2); + REQUIRE((unsigned char)packet[0] == 0x1f); + REQUIRE((unsigned char)packet[1] == 0x8b); + + DecompressStream in; + auto segment = containers::memory_segment_vector(packet); + + Vector realA; + realA.resize(expectedA.size()); + in.with(segment, [&](auto &s) { + s.vector(realA); + }); + + auto a_hash = hash_of(realA); + auto a_expectedHash = hash_of(expectedA); + REQUIRE(a_hash == a_expectedHash); + REQUIRE(segment.size() == secondCompressedSize); + + Vector realB; + realB.resize(expectedB.size()); + in.with(segment, [&](auto &s) { + s.vector(realB); + }); + + auto b_hash = hash_of(realB); + auto b_expectedHash = hash_of(expectedB); + REQUIRE(b_hash == b_expectedHash); + REQUIRE(segment.size() == 0); + } +} + +} // namespace +} // namespace diff --git a/tjp/core/http/temp/compression/_tests/zstd.cpp b/tjp/core/http/temp/compression/_tests/zstd.cpp new file mode 100644 index 0000000..ec5032e --- /dev/null +++ b/tjp/core/http/temp/compression/_tests/zstd.cpp @@ -0,0 +1,148 @@ +// TJP COPYRIGHT HEADER + +#include +#include + +#include +#include + +namespace tjp::core::compression::zstd { +namespace { + +SCENARIO("tjp::core::compression::zstd") +{ + GIVEN("read and write") + { + CompressStream out(9); + Vector packet; + + struct Values { + s16 a; + Vector b; + u32 c; + Vector d; + }; + + Values expected { + .a = 42, + .b = { 1, 2, 3, 4, 5 }, + .c = 13 + }; + + auto largeNumber = 235324; + expected.d.assign(largeNumber, 12345); + + out.with(packet, [&](auto &s) { + s.value(expected.a); + s.vector(expected.b); + s.value(expected.c); + s.vector(expected.d); + }); + + Values expectedB { + .a = 124, + .b = { 5, 2, 3, 4, 5 }, + .c = 23 + }; + expectedB.d.assign(largeNumber, 6666); + + out.with(packet, [&](auto &s) { + s.value(expectedB.a); + s.vector(expectedB.b); + s.value(expectedB.c); + s.vector(expectedB.d); + }); + + Values real; + real.b.resize(5); + real.d.resize(largeNumber); + + DecompressStream in; + + auto segment = containers::memory_segment_vector(packet); + + in.with(segment, [&](auto &s) { + s.value(real.a); + s.vector(real.b); + s.value(real.c); + s.vector(real.d); + }); + + REQUIRE(real.a == expected.a); + REQUIRE(real.b == expected.b); + REQUIRE(real.c == expected.c); + + auto d_hash = hash_of(real.d); + auto d_expectedHash = hash_of(expected.d); + REQUIRE(d_hash == d_expectedHash); + + + Values realB; + realB.b.resize(5); + realB.d.resize(largeNumber); + + in.with(segment, [&](auto &s) { + s.value(realB.a); + s.vector(realB.b); + s.value(realB.c); + s.vector(realB.d); + }); + + REQUIRE(realB.a == expectedB.a); + REQUIRE(realB.b == expectedB.b); + REQUIRE(realB.c == expectedB.c); + + auto bd_hash = hash_of(realB.d); + auto bd_expectedHash = hash_of(expectedB.d); + REQUIRE(bd_hash == bd_expectedHash); + } + + GIVEN("back-to-back streams") + { + CompressStream out(9); + Vector packet; + + Vector expectedA; + Vector expectedB; + expectedA.assign(32768, 1111); + expectedB.assign(32768, 2222); + + auto firstCompressedSize = out.with(packet, [&](auto &s) { + s.vector(expectedA); + }); + auto secondCompressedSize = out.with(packet, [&](auto &s) { + s.vector(expectedB); + }); + + REQUIRE(firstCompressedSize > 0); + REQUIRE(secondCompressedSize > 0); + + DecompressStream in; + auto segment = containers::memory_segment_vector(packet); + + Vector realA; + realA.resize(expectedA.size()); + in.with(segment, [&](auto &s) { + s.vector(realA); + }); + + auto a_hash = hash_of(realA); + auto a_expectedHash = hash_of(expectedA); + REQUIRE(a_hash == a_expectedHash); + REQUIRE(segment.size() == secondCompressedSize); + + Vector realB; + realB.resize(expectedB.size()); + in.with(segment, [&](auto &s) { + s.vector(realB); + }); + + auto b_hash = hash_of(realB); + auto b_expectedHash = hash_of(expectedB); + REQUIRE(b_hash == b_expectedHash); + REQUIRE(segment.size() == 0); + } +} + +} // namespace +} // namespace diff --git a/tjp/core/http/temp/compression/remote-run b/tjp/core/http/temp/compression/remote-run new file mode 100755 index 0000000..238f178 --- /dev/null +++ b/tjp/core/http/temp/compression/remote-run @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +set -euo pipefail + +usage() { + cat <<'EOF' +Usage: + ./remote-run + +Environment variables: + REMOTE_HOST Default: host.docker.internal + REMOTE_PORT Default: 6666 + REMOTE_TOKEN Optional shared token +EOF +} + +if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then + usage + exit 0 +fi + +if [[ $# -lt 1 ]]; then + usage + exit 2 +fi + +cmd="$1" +host="${REMOTE_HOST:-host.docker.internal}" +port="${REMOTE_PORT:-6666}" +token="${REMOTE_TOKEN:-}" + +url="http://${host}:${port}/run/${cmd}" + +curl_args=(--silent --show-error --no-buffer "$url") +if [[ -n "$token" ]]; then + curl_args=(-H "X-Remote-Token: ${token}" "${curl_args[@]}") +fi + +exec curl "${curl_args[@]}" diff --git a/tjp/core/http/temp/compression/zlib.cpp b/tjp/core/http/temp/compression/zlib.cpp new file mode 100644 index 0000000..30fc5fe --- /dev/null +++ b/tjp/core/http/temp/compression/zlib.cpp @@ -0,0 +1,525 @@ +// TJP COPYRIGHT HEADER + +#include "zlib.hpp" + +#include +#include +#include +#include + +#include + +namespace tjp::core::compression::zlib { + +enum class ContextType { + Compress, + Decompress +} ; + +namespace { + +constexpr int ZlibWindowBits = 15; +constexpr int GzipWindowBits = ZlibWindowBits + 16; +constexpr int InflateAutoWindowBits = ZlibWindowBits + 32; + +int window_bits_for(Format format) +{ + switch (format) + { + case Format::Zlib: + return ZlibWindowBits; + case Format::Gzip: + return GzipWindowBits; + default: + return ZlibWindowBits; + } +} + +size_t compress_key(int level, Format format) +{ + return (size_t(level) << 2) | size_t(format); +} + +} // namespace + +struct CompressContexts { + Mutex mutex; + Map> contextsByKey; + + ~CompressContexts() + { + for (auto &[k, contexts] : contextsByKey) + for (auto *c : contexts) + free((z_streamp)c); + } + + void *getContext(int level, Format format) + { + auto key = compress_key(level, format); + { + auto lock = lock_of(mutex); + + auto &contexts = contextsByKey[key]; + if (!contexts.empty()) + { + auto *v = contexts.back(); + contexts.pop_back(); + return v; + } + } + + auto z = (z_streamp)calloc(1, sizeof(z_stream)); + auto result = deflateInit2( + z, + level, + Z_DEFLATED, + window_bits_for(format), + 8, + Z_DEFAULT_STRATEGY + ); + if (result != Z_OK) + { + free((void *)z); + return nullptr; + } + return z; + } + + void putContext(int level, Format format, void *v) + { + auto key = compress_key(level, format); + auto lock = lock_of(mutex); + contextsByKey[key].push_back(v); + } +} ; + +struct DecompressContexts +{ + Mutex mutex; + Vector contexts; + + ~DecompressContexts() + { + for (auto *c : contexts) + free((z_streamp)c); + } + + void *getContext() + { + { + auto lock = lock_of(mutex); + + if (!contexts.empty()) + { + auto *v = contexts.back(); + contexts.pop_back(); + return v; + } + } + + auto z = (z_streamp)calloc(1, sizeof(z_stream)); + auto result = inflateInit2(z, InflateAutoWindowBits); + if (result != Z_OK) + { + free((void *)z); + return nullptr; + } + return z; + } + + void putContext(void *v) + { + auto lock = lock_of(mutex); + contexts.push_back(v); + } +} ; + +CompressContexts compressContexts; +DecompressContexts decompressContexts; + +#ifdef TIMPREPSCIUS_ZLIB_USE_DICTIONARY + +Dictionary::Dictionary() +{ + memset(block, 0, size); +} + +bool setDict(void *p, ContextType type, const Dictionary &d) +{ + auto z = (z_streamp)p; + + int error = 0; + if (type == ContextType::Decompress) + error = inflateSetDictionary(z, (const Bytef *)d.block, (uInt)d.size); + else + error = deflateSetDictionary(z, (const Bytef *)d.block, (uInt)d.size); + + return error == Z_OK; +} + +bool getDict(void *p, ContextType type, Dictionary &d) +{ + auto z = (z_streamp)p; + + constexpr auto DictStoreSize = 32768; + char store[DictStoreSize]; + memset(store, 0, DictStoreSize); + auto size_ = (uInt)DictStoreSize; + + int error = 0; + if (type == ContextType::Decompress) + error = inflateGetDictionary(z, (Bytef *)&store[0], &size_); + else + error = deflateGetDictionary(z, (Bytef *)&store[0], &size_); + + // zlib seems to store from end to front + auto offset = + size_ > d.size ? + size_ - d.size : + 0; + + int firstIndex = -1, lastIndex = -1; + for (auto i=0; inext_out = (Bytef *)v.data() + p; + context->avail_out = uInt(newSize - p); + + auto result = deflate(context, Z_FINISH); + p = size_t(context->next_out - (Bytef *)v.data()); + + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + { + error_ = result; + break; + } + } + + getDict(context, ContextType::Compress, dictionary); + + p = size_t(context->next_out - (Bytef *)v.data()); + v.resize(p); + + return p - p_; +} + +size_t Compressor::write(const containers::MemorySegment &s) +{ + auto &v = *v_; + + auto context = (z_streamp)i; + + context->next_in = (Bytef *)s.data(); + context->avail_in = (uInt)s.size(); + + // beginning of stream + int result = 0; + size_t written = 0; + + while(1) + { + auto newSize = round_up_to_multiple(p + 1, sizeMultiples); + v.resize(newSize); + + context->next_out = (Bytef *)v.data() + p; + context->avail_out = uInt(newSize - p); + + result = deflate(context, Z_NO_FLUSH); + p = size_t(context->next_out - (Bytef *)v.data()); + written = s.size() - (size_t)context->avail_in; + + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + { + error_ = result; + break; + } + + if (context->avail_in == 0) + break; + } + + + return written; +} + +void Compressor::flush() +{ + auto &v = *v_; + + auto context = (z_streamp)i; + + context->next_in = nullptr; + context->avail_in = 0; + + // beginning of stream + int result = 0; + + while(1) + { + auto newSize = round_up_to_multiple(p + 1, sizeMultiples); + v.resize(newSize); + + context->next_out = (Bytef *)v.data() + p; + context->avail_out = uInt(newSize - p); + + result = deflate(context, Z_PARTIAL_FLUSH); + p = size_t(context->next_out - (Bytef *)v.data()); + + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + { + error_ = result; + break; + } + + if (context->avail_out > 0) + break; + } +} + +size_t Compressor::size() const +{ + return p; +} + +bool Compressor::error () const +{ + return error_; +} + +CompressStream::CompressStream(int level, Format format) : + compressor(level, format) +{ +} + +size_t CompressStream::with(V &v, F &&f) +{ + return compressor.with(v, std::move(f)); +} + +CompressPacket::CompressPacket(int level_, Format format_) : + level(level_), + format(format_) +{ + +} + +size_t CompressPacket::with(V &v, F &&f) +{ + Compressor compressor(level, format); + return compressor.with(v, std::move(f)); +} + +Decompressor::Decompressor() +{ +} + +size_t Decompressor::with(V &v, F &&f) +{ + error_ = 0; + + auto context = (z_streamp)decompressContexts.getContext(); + if (!context) + return 0; + + i = (I *)context; + v_ = &v; + + auto _ = ExecuteOnDestruct([&]() { + decompressContexts.putContext(context); + i = nullptr; + v_ = nullptr; + }); + + if (!clearStream(context, ContextType::Decompress)) + return 0; + + context->next_in = (Bytef *)v.data(); + context->avail_in = uInt(v.size()); + p = 0; + + f(*this); + + getDict(context, ContextType::Decompress, dictionary); + + auto written = context->total_out; + auto consumed = size_t(context->next_in - (Bytef *)v.data()); + + v.data_ = (char *)context->next_in; + if (consumed > v.size_) + v.size_ = 0; + else + v.size_ -= consumed; + return written; +} + +size_t Decompressor::read(const containers::MemorySegment &s) +{ + auto &v = *v_; + auto context = (z_streamp)i; + + context->next_out = (Bytef *)s.data(); + context->avail_out = (uInt)s.size(); + + int result = 0; + size_t written = 0; + + while(1) + { + result = inflate(context, Z_SYNC_FLUSH); + written = size_t(context->next_out - (Bytef *)s.data()); + p = size_t(context->next_in - (Bytef *)v.data()); + + if (result == Z_NEED_DICT) + { + if (!setDict(context, ContextType::Decompress, dictionary)) + return 0; + } + + if (result == Z_STREAM_END) + break; + + if (result <= Z_ERRNO) + { + error_ = result; + break; + } + + if (written == s.size()) + break; + } + + return written; +} + +bool Decompressor::eos() const +{ + return error() || p >= v_->size(); +} + +bool Decompressor::error () const +{ + return error_; +} + +DecompressStream::DecompressStream() +{ + +} + +size_t DecompressStream::with(V &v, F &&f) +{ + return decompressor.with(v, std::move(f)); +} + +DecompressPacket::DecompressPacket() +{ + +} + +size_t DecompressPacket::with(V &v, F &&f) +{ + Decompressor decompressor; + + return decompressor.with(v, std::move(f)); +} + + + +} // namespace diff --git a/tjp/core/http/temp/compression/zlib.h b/tjp/core/http/temp/compression/zlib.h new file mode 100644 index 0000000..660c159 --- /dev/null +++ b/tjp/core/http/temp/compression/zlib.h @@ -0,0 +1,11 @@ +// TJP COPYRIGHT HEADER + +#pragma once + + +namespace tjp::core::compression::zlib { + +struct CompressStream; +struct DecompressStream; + +} // namespace diff --git a/tjp/core/http/temp/compression/zlib.hpp b/tjp/core/http/temp/compression/zlib.hpp new file mode 100644 index 0000000..3dd12d9 --- /dev/null +++ b/tjp/core/http/temp/compression/zlib.hpp @@ -0,0 +1,158 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "zlib.h" +#include +#include +#include + +namespace tjp::core::compression::zlib { + +enum class Format +{ + Zlib, + Gzip +} ; + +//#define TIMPREPSCIUS_ZLIB_USE_DICTIONARY +#ifdef TIMPREPSCIUS_ZLIB_USE_DICTIONARY +struct Dictionary +{ + static constexpr auto size = 256; + char block[size]; + + Dictionary(); +} ; +#else +struct Dictionary {}; +#endif + + + +struct Compressor +{ + [[no_unique_address]] + Dictionary dictionary; + + static constexpr auto sizeMultiples = 512; + using V = Vector; + using F = Function; + + int level; + Format format; + using I = void; + I *i = nullptr; + V *v_ = nullptr; + size_t p = 0; + int error_ = 0; + + Compressor(int level, Format format = Format::Zlib); + + size_t size() const; + + size_t with(V &, F &&f); + size_t write(const containers::MemorySegment &); + void flush(); + + template + auto value(T &t) { + auto segment = containers::memory_segment_value(t); + return write(segment) == segment.size(); + } + + template + auto vector(T &t) { + auto segment = containers::memory_segment_vector(t); + return write(segment) == segment.size(); + } + + bool error() const; +} ; + +struct CompressStream +{ + using V = Compressor::V; + using F = Compressor::F; + + Compressor compressor; + + CompressStream(int level, Format format = Format::Zlib); + + size_t with(V &, F &&f); +} ; + +struct CompressPacket +{ + using V = Compressor::V; + using F = Compressor::F; + + int level; + Format format; + + CompressPacket(int level, Format format = Format::Zlib); + + size_t with(V &, F &&f); +} ; + + +struct Decompressor +{ + [[no_unique_address]] + Dictionary dictionary; + + using V = containers::MemorySegment; + using F = Function; + + using I = void; + I *i = nullptr; + V *v_ = nullptr; + size_t p = 0; + int error_ = 0; + + Decompressor(); + + bool eos() const; + + size_t with(V &, F &&f); + size_t read(const containers::MemorySegment &); + + template + auto value(T &t) { + auto segment = containers::memory_segment_value(t); + return read(segment) == segment.size(); + } + + template + auto vector(T &t) { + auto segment = containers::memory_segment_vector(t); + return read(segment) == segment.size(); + } + + bool error () const; +} ; + +struct DecompressStream +{ + using V = Decompressor::V; + using F = Decompressor::F; + + Decompressor decompressor; + + DecompressStream(); + + size_t with(V &, F &&f); +} ; + +struct DecompressPacket +{ + using V = Decompressor::V; + using F = Decompressor::F; + + DecompressPacket(); + + size_t with(V &, F &&f); +} ; + + +} // namespace diff --git a/tjp/core/http/temp/compression/zstd.cpp b/tjp/core/http/temp/compression/zstd.cpp new file mode 100644 index 0000000..fed6e4d --- /dev/null +++ b/tjp/core/http/temp/compression/zstd.cpp @@ -0,0 +1,356 @@ +// TJP COPYRIGHT HEADER + +#include "zstd.hpp" + +#include +#include +#include +#include + +#include + +namespace tjp::core::compression::zstd { + +enum class ContextType { + Compress, + Decompress +} ; + +struct CompressContexts { + Mutex mutex; + Map> contextsByLevel; + + ~CompressContexts() + { + for (auto &[l, contexts] : contextsByLevel) + for (auto *c : contexts) + ZSTD_freeCStream((ZSTD_CStream *)c); + } + + void *getContext(int level) + { + { + auto lock = lock_of(mutex); + + auto &contexts = contextsByLevel[level]; + if (!contexts.empty()) + { + auto *v = contexts.back(); + contexts.pop_back(); + return v; + } + } + + auto *v = ZSTD_createCStream(); + ZSTD_initCStream(v, level); + return v; + } + + void putContext(int level, void *v) + { + auto lock = lock_of(mutex); + contextsByLevel[level].push_back(v); + } +} ; + +struct DecompressContexts +{ + Mutex mutex; + Vector contexts; + + ~DecompressContexts() + { + for (auto *c : contexts) + ZSTD_freeDStream((ZSTD_DStream *)c); + } + + void *getContext() + { + { + auto lock = lock_of(mutex); + + if (!contexts.empty()) + { + auto *v = contexts.back(); + contexts.pop_back(); + return v; + } + } + + void *v = ZSTD_createDStream(); + return v; + } + + void putContext(void *v) + { + auto lock = lock_of(mutex); + contexts.push_back(v); + } +} ; + +CompressContexts compressContexts; +DecompressContexts decompressContexts; + + +// -------------- + +bool clearStream(void *p, ContextType type) +{ + return true; +} + + +// -------------- + +static size_t round_up_to_multiple(size_t number, size_t multiple) { + if (multiple == 0) { + return number; // Avoid division by zero + } + auto remainder = number % multiple; + if (remainder == 0) { + return number; // Already a multiple + } + return number + multiple - remainder; +} + +Compressor::Compressor(int level_) : + level(level_) +{ +} + +size_t Compressor::with(V &v, F &&f) +{ + auto lock = lock_of(mutex); + auto context = (ZSTD_CStream *)compressContexts.getContext(level); + + auto _ = ExecuteOnDestruct([&]() { + compressContexts.putContext(level, context); + i = nullptr; + v_ = nullptr; + }); + + if (!clearStream(context, ContextType::Compress)) + return 0; + + i = (I *)context; + v_ = &v; + p = v.size(); + + auto p_ = p; + + f(*this); + + // finish the stream + while(1) + { + auto newSize = round_up_to_multiple(p + 1, sizeMultiples); + v.resize(newSize); + + ZSTD_outBuffer out; + out.dst = v.data(); + out.pos = p; + out.size = v.size(); + + auto result = ZSTD_endStream(context, &out); + p = out.pos; + + if (ZSTD_isError(result)) + { + error_ = result; + break; + } + + if (result == 0) + break; + } + + v.resize(p); + + return p - p_; +} + +size_t Compressor::write(const containers::MemorySegment &s) +{ + auto &v = *v_; + + auto context = (ZSTD_CStream *)i; + + ZSTD_inBuffer in; + in.size = s.size(); + in.src = s.data(); + in.pos = 0; + + // beginning of stream + size_t result = 0; + + while(1) + { + auto newSize = round_up_to_multiple(p + 1, sizeMultiples); + v.resize(newSize); + + ZSTD_outBuffer out; + out.dst = v.data(); + out.pos = p; + out.size = v.size(); + + result = ZSTD_compressStream(context, &out, &in); + p = out.pos; + + if (ZSTD_isError(result)) + { + error_ = result; + break; + } + + if (in.pos == in.size) + break; + } + + return in.pos; +} + +void Compressor::flush() +{ + auto &v = *v_; + + auto context = (ZSTD_CStream *)i; + + // beginning of stream + size_t result = 0; + + while(1) + { + auto newSize = round_up_to_multiple(p + 1, sizeMultiples); + v.resize(newSize); + + ZSTD_outBuffer out; + out.dst = v.data(); + out.pos = p; + out.size = v.size(); + + result = ZSTD_flushStream(context, &out); + p = out.pos; + + if (ZSTD_isError(result)) + { + error_ = result; + break; + } + + if (out.pos < out.size) + break; + } +} + +size_t Compressor::size() const +{ + return p; +} + +bool Compressor::error () const +{ + return error_; +} + +CompressPacket::CompressPacket(int level_) : + level(level_) +{ +} + +size_t CompressPacket::with(V &v, F &&f) +{ + Compressor compressor(level); + return compressor.with(v, std::move(f)); +} + + + +Decompressor::Decompressor() +{ +} + +size_t Decompressor::with(V &v, F &&f) +{ + auto lock = lock_of(mutex); + auto context = (ZSTD_DCtx *)decompressContexts.getContext(); + + i = (I *)context; + v_ = &v; + + auto _ = ExecuteOnDestruct([&]() { + decompressContexts.putContext(context); + i = nullptr; + v_ = nullptr; + }); + + if (!clearStream(context, ContextType::Decompress)) + return 0; + + p = 0; + f(*this); + + v.data_ += p; + v.size_ -= p; + + return p; +} + +size_t Decompressor::read(const containers::MemorySegment &s) +{ + auto &v = *v_; + auto context = (ZSTD_DCtx *)i; + + ZSTD_inBuffer in; + in.size = v.size(); + in.src = v.data(); + in.pos = p; + + ZSTD_outBuffer out; + out.dst = (void *)s.data(); + out.pos = 0; + out.size = s.size(); + + size_t result = 0; + + while(1) + { + result = ZSTD_decompressStream(context, &out, &in); + p = in.pos; + + if (ZSTD_isError(result)) + { + error_ = result; + break; + } + + if (in.pos == in.size) + break; + + if (out.pos == out.size) + break; + } + + return out.pos; +} + +bool Decompressor::eos() const +{ + return error() || p >= v_->size(); +} + +bool Decompressor::error () const +{ + return error_; +} + +DecompressPacket::DecompressPacket() +{ +} + +size_t DecompressPacket::with(V &v, F &&f) +{ + Decompressor decompressor; + return decompressor.with(v, std::move(f)); +} + +} // namespace diff --git a/tjp/core/http/temp/compression/zstd.h b/tjp/core/http/temp/compression/zstd.h new file mode 100644 index 0000000..86b8041 --- /dev/null +++ b/tjp/core/http/temp/compression/zstd.h @@ -0,0 +1,14 @@ +// TJP COPYRIGHT HEADER + +#pragma once + + +namespace tjp::core::compression::zstd { + +struct CompressPacket; +struct DecompressPacket; + +using CompressStream = CompressPacket; +using DecompressStream = DecompressPacket; + +} // namespace diff --git a/tjp/core/http/temp/compression/zstd.hpp b/tjp/core/http/temp/compression/zstd.hpp new file mode 100644 index 0000000..531c33c --- /dev/null +++ b/tjp/core/http/temp/compression/zstd.hpp @@ -0,0 +1,111 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "zstd.h" +#include +#include +#include +#include + +namespace tjp::core::compression::zstd { + +struct Compressor +{ + core::TestMutex mutex; + + static constexpr auto sizeMultiples = 512; + using V = Vector; + using I = void; + using F = Function; + + int level; + I *i = nullptr; + V *v_ = nullptr; + size_t p = 0; + size_t error_ = 0; + + Compressor(int level); + + size_t with(V &, F &&f); + + size_t size() const; + + size_t write(const containers::MemorySegment &); + void flush(); + + template + auto value(const T &t) { + auto segment = containers::memory_segment_value(t); + return write(segment) == segment.size(); + } + + template + auto vector(const T &t) { + auto segment = containers::memory_segment_vector(t); + return write(segment) == segment.size(); + } + + bool error () const; +} ; + +struct CompressPacket +{ + using V = Compressor::V; + using F = Compressor::F; + + int level; + + CompressPacket(int level); + + size_t with(V &, F &&f); +} ; + +struct Decompressor +{ + core::TestMutex mutex; + + using V = containers::MemorySegment; + using I = void; + using F = Function; + + I *i = nullptr; + V *v_ = nullptr; + size_t p = 0; + size_t error_ = 0; + + Decompressor(); + + bool eos() const; + + size_t with(V &, F &&f); + + size_t read(const containers::MemorySegment &); + + template + auto value(T &t) { + auto segment = containers::memory_segment_value(t); + return read(segment) == segment.size(); + } + + template + auto vector(T &t) { + auto segment = containers::memory_segment_vector(t); + return read(segment) == segment.size(); + } + + bool error () const; +}; + +struct DecompressPacket +{ + using V = Decompressor::V; + using F = Decompressor::F; + + DecompressPacket(); + + size_t with(V &, F &&); +} ; + + +} // namespace diff --git a/tjp/core/http/transform.hpp b/tjp/core/http/transform.hpp new file mode 100755 index 0000000..ab50a99 --- /dev/null +++ b/tjp/core/http/transform.hpp @@ -0,0 +1,55 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Types.hpp" +#include "future_response.hpp" +#include + +namespace tjp::core::http { + + +template +auto transform(F &&f) +{ + return + [f=std::move(f)](const Request& req) + { + return future_response(f, req); + }; +} + +template +auto transform(F &&f) +{ + return + transform( + [f=std::move(f)](const Request& req) { + auto future = future_response(f, req); + return future.then( + [](auto &&f) { + return Serializer{}(f.get()); + }); + } + ); +} + + + +//template +//auto transform(F &&f) +//{ +// return +// [f=std::move(f)](const Request& req, Response& res) +// { +// auto future = future_response(f, req); +// return future.then( +// [](auto &&f) { +// auto [body, type] = Serializer{}(result); +// res.set_content(body, type); +// }); +// }; +//} + + +} diff --git a/tjp/core/http/transform_base64_text.hpp b/tjp/core/http/transform_base64_text.hpp new file mode 100755 index 0000000..5f5c26a --- /dev/null +++ b/tjp/core/http/transform_base64_text.hpp @@ -0,0 +1,29 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Types.hpp" +#include "../bases/Base64.h" +#include "transform.hpp" + +namespace tjp::core::http { + +struct ToBase64Text +{ + template + BodyType operator()(const T &value) const + { + return { + toBase64(value), + "application/base64" + }; + } +} ; + +template +auto transform_base64_text(F &&f) +{ + return transform(std::forward(f)); +} + +} diff --git a/tjp/core/http/transform_binary.hpp b/tjp/core/http/transform_binary.hpp new file mode 100755 index 0000000..fe8df9c --- /dev/null +++ b/tjp/core/http/transform_binary.hpp @@ -0,0 +1,46 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Types.hpp" +#include +#include +#include "transform.hpp" + +namespace tjp::core::http { + +struct ToBinary +{ + template + BodyType operator()(T &&value) const + { + return { + core::as_string(std::forward(value)), + "application/binary" + }; + } +} ; + +template +auto transform_binary(F &&f) +{ + return + [f=std::move(f)](const Request& req) + { + auto future = future_with(f(req)); + return future.then([](auto &&f) { + return BodyType { + Serializer{}(f.get()), + "application/binary" + }; + }); + }; +} + +template +auto transform_binary_text(F &&f) +{ + return transform(std::forward(f)); +} + +} diff --git a/tjp/core/http/transform_json.hpp b/tjp/core/http/transform_json.hpp new file mode 100755 index 0000000..b37797e --- /dev/null +++ b/tjp/core/http/transform_json.hpp @@ -0,0 +1,46 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Types.hpp" +#include +#include +#include "transform.hpp" + +namespace tjp::core::http { + +struct ToJson +{ + template + BodyType operator()(const T &value) const + { + return { + core::as_string(core::io::json::serialize(value)), + "application/json" + }; + } +} ; + +template +auto transform_json(F &&f) +{ + return + [f=std::move(f)](const Request& req) + { + auto future = future_with(f(req)); + return future.then([](auto &&f) { + return BodyType { + Serializer{}(f.get()), + "application/json" + }; + }); + }; +} + +template +auto transform_json(F &&f) +{ + return transform(std::forward(f)); +} + +} diff --git a/tjp/core/http/transform_json_text.hpp b/tjp/core/http/transform_json_text.hpp new file mode 100755 index 0000000..8a74af3 --- /dev/null +++ b/tjp/core/http/transform_json_text.hpp @@ -0,0 +1,29 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Types.hpp" +#include +#include "transform.hpp" + +namespace tjp::core::http { + +struct ToJsonText +{ + template + BodyType operator()(const T &value) const + { + return { + core::as_string(value), + "application/json" + }; + } +} ; + +template +auto transform_json_text(F &&f) +{ + return transform(std::forward(f)); +} + +} diff --git a/tjp/core/http/transform_ok.hpp b/tjp/core/http/transform_ok.hpp new file mode 100755 index 0000000..413ec99 --- /dev/null +++ b/tjp/core/http/transform_ok.hpp @@ -0,0 +1,30 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Types.hpp" +#include +#include +#include "transform.hpp" + +namespace tjp::core::http { + +template +auto transform_ok(F &&f) +{ + return + [f=std::move(f)](const Request& req) + { + auto future = future_with(f, req); + return future.then([](auto &&f) { + f.get(); + + return BodyType { + "OK", + "text/plain" + }; + }); + }; +} + +} diff --git a/tjp/core/http/transform_plaintext.hpp b/tjp/core/http/transform_plaintext.hpp new file mode 100755 index 0000000..89bdd54 --- /dev/null +++ b/tjp/core/http/transform_plaintext.hpp @@ -0,0 +1,46 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Types.hpp" +#include +#include +#include "transform.hpp" + +namespace tjp::core::http { + +struct ToPlainText +{ + template + BodyType operator()(T &&value) const + { + return { + core::to_string(std::forward(value)), + "text/plain" + }; + } +} ; + +template +auto transform_plaintext(F &&f) +{ + return + [f=std::move(f)](const Request& req) + { + auto future = future_with(f(req)); + return future.then([](auto &&f) { + return BodyType { + Serializer{}(f.get()), + "text/plain" + }; + }); + }; +} + +template +auto transform_plaintext(F &&f) +{ + return transform(std::forward(f)); +} + +} diff --git a/tjp/core/in_place/InPlace.h b/tjp/core/in_place/InPlace.h new file mode 100755 index 0000000..cdc5f76 --- /dev/null +++ b/tjp/core/in_place/InPlace.h @@ -0,0 +1,46 @@ +// TJP COPYRIGHT HEADER + +// draft + +#pragma once + +#include + +namespace tjp { +namespace core { + +template +struct InPlace +{ + struct Value { + char memory [size]; + } __attribute__ ((aligned (alignment))); + + Value v; + + T *operator ->() + { + return (T *)&v.memory; + } + + T *operator ->() const + { + return (const T *)&v.memory; + } + + template + InPlace(Args&& ... args) + { + (*this)->T(std::forward(args)...); + static_assert(sizeof(T) == size); + } + + ~InPlace () + { + (*this)->~T(); + } +} ; + +} // namespace +} // namespace + diff --git a/tjp/core/initializer/Initializer.cpp b/tjp/core/initializer/Initializer.cpp new file mode 100644 index 0000000..3a8a8dc --- /dev/null +++ b/tjp/core/initializer/Initializer.cpp @@ -0,0 +1,51 @@ +// TJP COPYRIGHT HEADER + +#include "Initializer.hpp" + +namespace tjp { +namespace core { +namespace initializer { + +//void deallocate(Initializer *v) +//{ +// delete v; +//} + +Initializer::Initializer(Initializers &&initializers_) : + initializers(initializers_) +{ +} + +StrongPtr Catalog::get(const std::string &id) +{ + auto i = initializerById.find(id); + if (i != initializerById.end()) + return i->second; + + return nullptr; +} + +void Catalog::put(const std::string &id, const StrongPtr &initializer) +{ + initializerById[id] = initializer; + initializer->catalog = weak_this(this); + initializer->id = id; +} + + +StrongPtr Initializer::getInitializer(const std::string &id) +{ + if (id.empty()) + return strong_this(this); + + if (auto catalog_ = strong(catalog)) + { + return catalog_->get(id); + } + + return nullptr; +} + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/initializer/Initializer.h b/tjp/core/initializer/Initializer.h new file mode 100644 index 0000000..f35a78f --- /dev/null +++ b/tjp/core/initializer/Initializer.h @@ -0,0 +1,17 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +namespace tjp { +namespace core { +namespace initializer { + +struct Token; +struct Initializer; +struct Globals; +struct InitializerChain; +struct Catalog; + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/initializer/Initializer.hpp b/tjp/core/initializer/Initializer.hpp new file mode 100644 index 0000000..fbca6fd --- /dev/null +++ b/tjp/core/initializer/Initializer.hpp @@ -0,0 +1,238 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Initializer.h" +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace tjp { +namespace core { +namespace initializer { + +typedef std::string ID; + +typedef std::function InitializerFunction; +typedef std::pair KeyedInitializerFunction; +typedef std::map> InitializersMap; + +struct Initializers +{ + InitializersMap map; + + Initializers(InitializersMap &&map_) : + map(map_) {} + + Initializers merge(const Initializers &base) + { + auto result = Initializers(InitializersMap(map)); + + for (auto &[fromI, fromV] : base.map) + { + auto &toV = result.map[fromI]; + + for (auto &fromKF: fromV) + { + auto contains = false; + + auto &fromK = fromKF.first; + for (auto &toKF : toV) + { + auto &toK = toKF.first; + + if (fromK == toK) + { + contains = true; + break; + } + } + + if (!contains) + toV.push_back(fromKF); + } + } + + return result; + } + + Initializers replace(const Initializers &base) + { + auto result = Initializers(InitializersMap(map)); + + for (auto &[fromI, fromV] : base.map) + { + auto &toV = result.map[fromI]; + + for (auto &fromKF: fromV) + { + auto &fromK = fromKF.first; + vector_erase_if_value(toV, [&](auto &v) { return v.first == fromK; }); + toV.push_back(fromKF); + } + } + + return result; + } +} ; + +struct Token {}; + +struct Initializer; +struct InitializerChain; + +// SFINAE test +template +class has_setInitializer +{ +private: + typedef char true_type; + typedef long false_type; + + template static false_type& test(...); + template static true_type& test( decltype(&C::setInitializer) ) ; + + +public: + enum { value = sizeof(test(0)) == sizeof(true_type) }; +}; + +template::value, T>::type* = nullptr> +void chain_initializer(Initializer *initializer, T *t); + +template::value, T>::type* = nullptr> +void chain_initializer(Initializer *initializer, T *t) { } + +struct Initializer; + +//void deallocate(Initializer *); + +struct Catalog : StrongThis +{ + std::map> initializerById; + + StrongPtr get(const ID &id); + + void put(const ID &id, const StrongPtr &initializer); + + template + StrongPtr construct_with(const U &id, Args&& ...args); +} ; + + +struct Initializer : StrongThis +{ + WeakPtr catalog; + std::string id; + Initializers initializers; + + Initializer(Initializers &&initializers_); + + StrongPtr getInitializer(const ID &id); + + template + void initialize(R *r) + { + chain_initializer(this, r); + + auto type = type_id(); + + auto i = initializers.map.find(type); + if (i != initializers.map.end()) + { + for (auto &[key, initializer]: i->second) + { + initializer(r); + } + } + } + + template + void initialize_reflect(R *r) + { + r->initialize_with(this); + } + + template + StrongPtr construct(Args&& ...args) + { + auto p = strong(Token{}, std::forward(args)...); + initialize_reflect(ptr_of(p)); + return p; + } + + template + StrongPtr construct_with(const U &id, Args&& ...args) + { + auto initializer = getInitializer(ID(id)); + debug_assert(initializer); + return initializer->construct(std::forward(args)...); + } +} ; + +struct InitializerChain +{ + StrongPtr initializer; + + template + void initialize(T *t) + { + debug_assert(initializer); + initializer->initialize_reflect(t); + } + + template + void initialize(const StrongPtr &t) + { + debug_assert(initializer); + initializer->initialize_reflect(ptr_of(t)); + } + + void setInitializer(const StrongPtr &initializer_) + { + initializer = initializer_; + } + + template + StrongPtr construct(Args&& ...args) + { + return initializer->construct(std::forward(args)...); + } + + template + StrongPtr construct_with(const U &id, Args&& ...args) + { + return initializer->construct_with(id, std::forward(args)...); + } + + std::string &initializer_id() + { + return initializer->id; + } +} ; + +template +StrongPtr Catalog::construct_with(const U &id, Args&& ...args) +{ + auto initializer = get(ID(id)); + debug_assert(initializer); + return initializer->construct(std::forward(args)...); +} + +template::value, T>::type*> +void chain_initializer(Initializer *initializer, T *t) +{ + t->setInitializer(strong_this(initializer)); +} + + +} // namespace +} // namespace +} // namespace + diff --git a/tjp/core/internet/Data.hpp b/tjp/core/internet/Data.hpp new file mode 100755 index 0000000..a570eb6 --- /dev/null +++ b/tjp/core/internet/Data.hpp @@ -0,0 +1,75 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include + +namespace tjp::core::internet { + +using DataBlock = Vector; +using DataView = StringView; + +struct Data { + mutable Optional block; + mutable Optional string; + + Data() + {} + + Data(DataBlock &&block_) : + block(std::move(block_)) + {} + + Data(String &&string_) : + string(std::move(string_)) + {} + + operator String &() const + { + if (!string.has_value()) + { + if (block.has_value()) + string = String(block->data(), block->size()); + else + string = String(); + } + + return *string; + } + + operator DataView () const + { + if (block.has_value()) + return StringView(block->data(), block->size()); + + if (string.has_value()) + return *string; + + return {}; + } ; + + const char *data() const + { + DataView view = *this; + return view.data(); + } + + const char *c_str() const + { + String &view = *this; + return view.c_str(); + } + + const auto size() const + { + DataView view = *this; + return view.size(); + } + + +}; + +} // namespace + diff --git a/tjp/core/internet/Headers.cpp b/tjp/core/internet/Headers.cpp new file mode 100755 index 0000000..ff25d5e --- /dev/null +++ b/tjp/core/internet/Headers.cpp @@ -0,0 +1,260 @@ +// TJP COPYRIGHT HEADER + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif +#include + +#include "Headers.hpp" + +#include +#include +#include + +#include + +#include +#include +#include +#include + +namespace tjp::core::internet { + +TJP_CORE_HEADER_ONLY_INLINE +const String *getHeader(const Headers &headers, StringView name) +{ + for (auto &[key, value]: headers) + if (core::cmp_case(key, name) == 0) + return &value; + + return nullptr; +} + +TJP_CORE_HEADER_ONLY_INLINE +Optional parseDate(const String &s) +{ + std::tm tm = {}; + core::io::imemstream ss(s.data(), s.size()); + + // Example format: "Fri, 02 Jun 2023 18:24:37 GMT" + ss >> std::get_time(&tm, "%a, %d %b %Y %H:%M:%S GMT"); + if (ss.fail()) { + return {}; + } + + // Convert to time_t in UTC +#if defined(_WIN32) + time_t t = _mkgmtime(&tm); +#else + time_t t = timegm(&tm); +#endif + + return static_cast(t); +} + +TJP_CORE_HEADER_ONLY_INLINE +Optional getLastModified(const Headers &headers) +{ + if (auto value = getHeader(headers, "Last-Modified")) + return parseDate(*value); + + return {}; +} + +TJP_CORE_HEADER_ONLY_INLINE +Optional getDate(const Headers &headers) +{ + if (auto value = getHeader(headers, "Date")) + return parseDate(*value); + + return {}; +} + +TJP_CORE_HEADER_ONLY_INLINE +Optional getType(const Headers &headers) +{ + if (auto value = getHeader(headers, "Content-Type")) + return *value; + + return {}; +} + +TJP_CORE_HEADER_ONLY_INLINE +Optional getCacheControl(const Headers &headers) +{ + if (auto value = getHeader(headers, "Cache-Control")) + return *value; + + return {}; +} + +TJP_CORE_HEADER_ONLY_INLINE +Optional getContentLocation(const Headers &headers) +{ + if (auto value = getHeader(headers, "Content-Location")) + return *value; + + return {}; +} + +TJP_CORE_HEADER_ONLY_INLINE +Optional getLocation(const Headers &headers) +{ + if (auto value = getHeader(headers, "Location")) + return *value; + + return {}; +} + +TJP_CORE_HEADER_ONLY_INLINE +Optional getCookie(const Headers &headers) +{ + if (auto value = getHeader(headers, "Set-Cookie")) + return *value; + + return {}; +} + +TJP_CORE_HEADER_ONLY_INLINE +Optional parseStatusCode(const String &s) +{ + if (s.rfind("HTTP/", 0) == 0) + { + int code = 0; + if (sscanf((char *)s.data(), "HTTP/%*s %d", &code) != 1) + return {}; + + return code; + } + + return {}; +} + + +TJP_CORE_HEADER_ONLY_INLINE +Optional parseRange(const String &s) +{ + long long int start = -1, end = -1; + + int matched = std::sscanf(s.data(), "bytes=%lld-%lld", &start, &end); + if (matched != 2) + return {}; + + return Range { start, end }; +} + +TJP_CORE_HEADER_ONLY_INLINE +String makeRange(Range range) +{ + ToString string; + + auto [begin, end] = range; + string << "bytes=" << begin << '-' << end; + + return string; +} + +TJP_CORE_HEADER_ONLY_INLINE +Optional parseContentRange(const String &s) +{ + char unit[16]; + char totalStr[32]; + long long int start = -1, end = -1, total = -1; + + int matched = std::sscanf(s.data(), "%15s %lld-%lld/%31s", unit, &start, &end, totalStr); + if (matched != 4 || std::strcmp(unit, "bytes") != 0) + return {}; + + if (std::strcmp(totalStr, "*") == 0) + total = -1; // unknown total + else + total = std::atoll(totalStr); + + return ContentRange { start, end, total }; +} + +TJP_CORE_HEADER_ONLY_INLINE +Optional getCacheControlMaxAge(const String &s_) +{ + StringView s(s_); + const StringView prefix = "max-age="; + auto maxAgeP = s.find(prefix); + if (maxAgeP == s.npos) + return {}; + + auto maxAgeB = maxAgeP + prefix.size(); + auto semi = s.find(';', maxAgeB); + if (semi == s.npos) + semi = s.size(); + + auto ageS = s.substr(maxAgeB, semi - maxAgeB); + return core::from_string(ageS); +} + + +TJP_CORE_HEADER_ONLY_INLINE +String makeContentRange(ContentRange range) +{ + ToString string; + + auto [begin, end, of] = range; + string << "bytes " << begin << '-' << end << '/'; + + if (of >= 0) + string << of; + else + string << '*'; + + return string; +} + +TJP_CORE_HEADER_ONLY_INLINE +Optional getContentRange(const Headers &headers) +{ + if (auto value = getHeader(headers, "Content-Range")) + return parseContentRange(*value); + + return {}; +} + + +TJP_CORE_HEADER_ONLY_INLINE +Optional parseContentSize(const String &s) +{ + long long int size = 0; + if (sscanf(s.c_str(), "%lld", &size) != 1) + return {}; + + return size; +} + +TJP_CORE_HEADER_ONLY_INLINE +Optional getContentSize(const Headers &headers) +{ + if (auto value = getHeader(headers, "Content-Length")) + return parseContentSize(*value); + + return {}; +} + +TJP_CORE_HEADER_ONLY_INLINE +String makeDateHeader(std::time_t t) +{ + std::tm* gm = std::gmtime(&t); // convert to UTC + if (!gm) return {}; + + char buffer[128]; + // Format: "Fri, 02 Jun 2023 18:24:37 GMT" + if (std::strftime(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S GMT", gm) == 0) + return {}; + + return String(buffer); +} + +TJP_CORE_HEADER_ONLY_INLINE +String makeLastModifiedHeader(std::time_t t) +{ + return makeDateHeader(t); +} + +} // namespace diff --git a/tjp/core/internet/Headers.hpp b/tjp/core/internet/Headers.hpp new file mode 100755 index 0000000..d67a47d --- /dev/null +++ b/tjp/core/internet/Headers.hpp @@ -0,0 +1,53 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace tjp::core::internet { + +using Header = Tuple; +using Headers = Vector
; + +const String *getHeader(const Headers &, StringView name); + +using ContentRange = Tuple; +using Range = Tuple; + +Optional parseStatusCode(const String &); +Optional parseDate(const String &); +Optional parseContentSize(const String &); +Optional parseContentRange(const String &); +Optional parseRange(const String &); + +Optional getType(const Headers &); +Optional getCacheControl(const Headers &); +Optional getLocation(const Headers &); +Optional getContentLocation(const Headers &); +Optional getCookie(const Headers &); +Optional getDate(const Headers &); +Optional getLastModified(const Headers &); +String makeDateHeader(std::time_t); +String makeLastModifiedheader(std::time_t); + +Optional getContentSize(const Headers &); +Optional getContentRange(const Headers &); +String makeContentRange(ContentRange); +String makeRange(Range); + +Optional getCacheControlMaxAge(const String &); + + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "Headers.cpp" +#endif diff --git a/tjp/core/internet/Internet+Internal.cpp b/tjp/core/internet/Internet+Internal.cpp new file mode 100755 index 0000000..d44af7e --- /dev/null +++ b/tjp/core/internet/Internet+Internal.cpp @@ -0,0 +1,147 @@ +// TJP COPYRIGHT HEADER + + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif +#include + +#include "Internet+Internal.hpp" +#include + +#include +#include + +#include + +namespace tjp::core::internet::internal { + +TJP_CORE_HEADER_ONLY_INLINE +Request::~Request () +{ + xDebugLine(); +} + +TJP_CORE_HEADER_ONLY_INLINE +size_t receiveContentsCallback(void *ptr, size_t size, void *userdata) +{ + sLogDebug("core::internet::receive", logVar(size)); + + auto* progressiveData = static_cast(userdata); + if (auto request = strong(progressiveData->request)) + { + auto &data = progressiveData->receive; + auto &options = progressiveData->options; + + auto realsize = size; + + char *ptr_ = (char *)ptr; + + if (options.onData) + options.onData(ptr_, realsize); + + data.data.data.insert(data.data.data.end(), ptr_, ptr_+realsize); + return realsize; + } + else + { + progressiveData->interrupted = true; + } + + sLogDebug("core::internet::receive", "lost ptr"); + + return 0; +} + +TJP_CORE_HEADER_ONLY_INLINE +size_t sendContentsCallback(char *ptr, size_t size, void *userdata) +{ + sLogDebug("core::internet::send", logVar(size)); + + auto* progressiveData = static_cast(userdata); + if (auto request = strong(progressiveData->request)) + { + auto &send = progressiveData->send; + auto &data = send.data; + auto &offset = send.offset; + + size_t buffer_size = size; + + size_t to_copy = std::min(data.size() - offset, buffer_size); + + std::memcpy(ptr, data.data() + offset, to_copy); + offset += to_copy; + + return to_copy; + } + else + { + progressiveData->interrupted = true; + } + + sLogDebug("core::internet::send", "lost ptr"); + + return 0; +} + +TJP_CORE_HEADER_ONLY_INLINE +size_t receiveHeadersCallback(char *buffer, size_t size, void *userdata) +{ + sLogDebug("core::internet::headers", logVar(buffer) << logVar(size)); + + auto *progressiveData = static_cast(userdata); + auto &options = progressiveData->options; + + if (auto request = strong(progressiveData->request)) + { + auto &receive = progressiveData->receive; + + StringView headerLine(buffer, size); + if (headerLine.rfind("HTTP/", 0) == 0) + { + int code = 0; + String s(buffer, size); + sscanf(s.c_str(), "HTTP/%*s %d", &code); + receive.data.statusCode = code; + } + else + if (headerLine == "\r\n") + { + if (options.onHeaders) + { + options.onHeaders(receive.data.headers, receive.data.statusCode); + options.onHeaders = {}; + } + } + else + { + auto colon = headerLine.find(':'); + if (colon != headerLine.npos) + { + auto key = headerLine.substr(0, colon); + auto value = headerLine.substr(colon+1); + + while (!value.empty() && std::isspace(value.front())) + value.remove_prefix(1); + + while (!value.empty() && std::isspace(value.back())) + value.remove_suffix(1); + + receive.data.headers.push_back({ String(key), String(value)}); + } + } + + return size; + } + else + { + progressiveData->interrupted = true; + } + + sLogDebug("core::internet::headers", "lost ptr"); + + return 0; +} + +} // namespace + diff --git a/tjp/core/internet/Internet+Internal.hpp b/tjp/core/internet/Internet+Internal.hpp new file mode 100755 index 0000000..ec02d37 --- /dev/null +++ b/tjp/core/internet/Internet+Internal.hpp @@ -0,0 +1,61 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Internet.hpp" +#include + +namespace tjp::core::internet { + +namespace internal { + +enum class RequestType { + GET, + POST, + PUT, + HEAD +} ; + +struct Request { + RequestType type; + + String url; + HttpOptions options; + Optional data; + + Promise promise; + + ~Request(); +} ; + + +struct SendData +{ +}; + +struct ProgressiveData { + WeakPtr request; + HttpOptions options; + bool interrupted = false; + + struct { + FileContents data; + } receive; + + struct { + Data data; + size_t offset = 0; + } send; +} ; + +size_t receiveContentsCallback(void *ptr, size_t size, void *receiveData); +size_t receiveHeadersCallback(char *buffer, size_t size, void *userdata); +size_t sendContentsCallback(char *ptr, size_t size, void *sendData); + +} // namespace + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "Internet+Internal.cpp" +#endif diff --git a/tjp/core/internet/Internet.h b/tjp/core/internet/Internet.h new file mode 100755 index 0000000..c990911 --- /dev/null +++ b/tjp/core/internet/Internet.h @@ -0,0 +1,19 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include + +namespace tjp::core::internet { + +using Header = Tuple; +using Headers = Vector
; + +struct HttpOptions; +struct FileContents; +struct Exception; + +} // namespace + diff --git a/tjp/core/internet/Internet.hpp b/tjp/core/internet/Internet.hpp new file mode 100755 index 0000000..877b6c0 --- /dev/null +++ b/tjp/core/internet/Internet.hpp @@ -0,0 +1,111 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Internet.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "Data.hpp" +#include "Headers.hpp" + +#include + +namespace tjp::core::internet { + +constexpr size_t GeneralFailure = 0xFFFF; + +using Header = Tuple; +using Headers = Vector
; + +using StatusCode = s64; + +using HeadersCallback = Function; +using ProgressiveCallback = Function; +using CompleteCallback = Function; + +struct HttpOptions { + Headers headers; + time_t lastModifiedTime = 0; + time_t timeout = 0; + bool throwOnFailure = true; + size_t maxRedirects = 0; + + Optional httpVersion; + Optional verbose; + + HeadersCallback onHeaders; + ProgressiveCallback onData; + CompleteCallback onComplete; +} ; + +namespace HttpStatusCodes { + const size_t NotModified = 304; + const size_t OK = 200; +} ; + +inline +HttpOptions makeEmptyOptions() { + return {}; +} + +struct FileContents +{ + Headers headers; + DataBlock data; + + StatusCode statusCode = GeneralFailure; + time_t lastModifiedTime = 0; + + bool ok() const + { + return statusCode < 400; + } +} ; + +struct Exception : core::Exception { + using Super = core::Exception; + + Optional contents; + + Exception ( + const String &what_, + Optional &&contents_ + ) : + Super(what_), + contents(std::move(contents_)) + { + } + + const Optional &getContents() const { + return contents; + } +} ; + +Future get(const std::string &url, const HttpOptions &o = makeEmptyOptions()); +Future post(const std::string &url, Data &&, const HttpOptions &o = makeEmptyOptions()); +Future put(const std::string &url, Data &&, const HttpOptions &o = makeEmptyOptions()); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #if defined(SYS_IOS) + #include "ios/Internet.hpp" + #else + #include "curl/Internet.hpp" + #endif +#endif diff --git a/tjp/core/internet/Queue.h b/tjp/core/internet/Queue.h new file mode 100755 index 0000000..05f0203 --- /dev/null +++ b/tjp/core/internet/Queue.h @@ -0,0 +1,15 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +namespace tjp::core::internet { + +struct Queue; + +struct MainQueue; + +template +struct Queues; + +} // namespace + diff --git a/tjp/core/internet/Queue.hpp b/tjp/core/internet/Queue.hpp new file mode 100755 index 0000000..9a3b3a9 --- /dev/null +++ b/tjp/core/internet/Queue.hpp @@ -0,0 +1,89 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Internet.hpp" +#include "Internet+Internal.hpp" +#include +#include +#include +#include +#include + +#include + +namespace tjp::core::internet { + +struct Queue +{ + struct Internal; + StrongPtr i; + + Queue(); + ~Queue(); + + void prepare(); + + Future execute(internal::RequestType, const String &url, Data &&data, const HttpOptions &o = makeEmptyOptions()); + + Future get(const String &url, const HttpOptions &o = makeEmptyOptions()) + { + return execute(internal::RequestType::GET, url, {}, o); + } + + Future post(const std::string &url, Data &&data, const HttpOptions &o = makeEmptyOptions()) + { + return execute(internal::RequestType::POST, url, std::move(data), o); + } + + Future put(const std::string &url, Data &&data, const HttpOptions &o = makeEmptyOptions()) + { + return execute(internal::RequestType::PUT, url, std::move(data), o); + } + + Future head(const std::string &url, const HttpOptions &o = makeEmptyOptions()) + { + return execute(internal::RequestType::HEAD, url, {}, o); + } +} ; + +struct MainQueue {}; + +template +struct Queues { + static Queue queue; + + static Future get(const String &url, const HttpOptions &o = makeEmptyOptions()) + { + return queue.execute(internal::RequestType::GET, url, {}, o); + } + + static Future post(const std::string &url, Data &&data, const HttpOptions &o = makeEmptyOptions()) + { + return queue.execute(internal::RequestType::POST, url, std::move(data), o); + } + + static Future put(const std::string &url, Data &&data, const HttpOptions &o = makeEmptyOptions()) + { + return queue.execute(internal::RequestType::PUT, url, std::move(data), o); + } + + static Future head(const std::string &url, const HttpOptions &o = makeEmptyOptions()) + { + return queue.execute(internal::RequestType::HEAD, url, {}, o); + } +} ; + +template +Queue Queues::queue; + +} // namespace + + +#ifdef TJP_CORE_HEADER_ONLY + #if defined(SYS_IOS) + #include "ios/Queue.hpp" + #else + #include "curl/Queue.hpp" + #endif +#endif diff --git a/tjp/core/internet/URL.cpp b/tjp/core/internet/URL.cpp new file mode 100755 index 0000000..5fa558e --- /dev/null +++ b/tjp/core/internet/URL.cpp @@ -0,0 +1,207 @@ +// TJP COPYRIGHT HEADER + +// TJP 4/09/01 relative URLs have not been checked yet. don't expect them +// to work the first time you use this class! +// +// TJP 4/11/01 getAsLocalPath needs to be modified to work with all sorts +// of protocols and port numbers... right now, probably not + +#include "URL.h" + +#include "../file/Path.h" +#include +#include + +namespace tjp { +namespace core { +namespace internet { + +const URL Empty; + +const char FS = '/'; +//const char BS = '\\'; +const char *DD = ".."; + +bool URL::isAbsoluteURL (const std::string &url) +{ + return url.find("://") != -1; +} + +void URL::parseAbsoluteURL (const std::string &url) +{ + auto protocolBegin = 0; + auto protocolEnd = url.find("://"); + + if (protocolEnd==-1) + { + throw Exception ("Could not find protocol"); + } + + auto serverBegin = protocolEnd + std::string("://").length(); + auto serverEnd = url.find (FS, serverBegin); + + if (serverEnd==-1) + { + throw Exception ("Could not find server"); + } + + auto directoryBegin = serverEnd + 1; + auto directoryEnd = url.find_last_of (FS) + 1; + + auto fileBegin = directoryEnd; + auto fileEnd = url.length(); + + protocol = url.substr (protocolBegin, protocolEnd-protocolBegin); + + server = url.substr (serverBegin, serverEnd-serverBegin); + directory = url.substr (directoryBegin, directoryEnd-directoryBegin); + file = url.substr (fileBegin, fileEnd-fileBegin); +} + +void URL::parseRelativeURL (const std::string &url) +{ + auto directoryBegin = 0; + auto directoryEnd = url.find_last_of (FS) + 1; + + auto fileBegin = directoryEnd; + auto fileEnd = url.length(); + + std::string relativeDirectory = url.substr (directoryBegin, directoryEnd-directoryBegin); + + if (relativeDirectory.substr (0,2) == "./") + { + relativeDirectory = relativeDirectory.substr(2); + } + + std::string relativeFile = url.substr (fileBegin, fileEnd-fileBegin); + + if ( (relativeDirectory.length()>0 && directory.length()>0) && + ((directory[directory.length()-1]==FS) && relativeDirectory[0]==FS) ) + { + directory += relativeDirectory.substr(1); + } + else + { + directory += relativeDirectory; + } + + file = url.substr (fileBegin, fileEnd-fileBegin); +} + +URL::URL () +{ +} + +URL::URL (const std::string &url) +{ + parseAbsoluteURL (url); +} + +URL::URL (const URL &parent, const std::string &url) +{ + if (isAbsoluteURL (url)) + { + parseAbsoluteURL (url); + } + else + { + protocol = parent.getProtocol(); + server = parent.getServer(); + directory = parent.getDirectory(); + + parseRelativeURL (url); + } +} + +URL::URL (const URL &url) +{ + protocol = url.protocol; + server = url.server; + directory = url.directory; + file = url.file; +} + +URL::~URL () +{ +} + +const std::string &URL::getProtocol () const +{ + return protocol; +} + +const std::string &URL::getServer() const +{ + return server; +} + +std::string URL::getPath () const +{ + return directory + file; +} + +const std::string &URL::getDirectory () const +{ + return directory; +} + +const std::string &URL::getFile () const +{ + return file; +} + +URL URL::join(const URL &url, const std::string &append) +{ + if (append.empty()) + return url; + + return URL((std::string)url + FS + append); +} + +URL::operator std::string() const +{ + return getProtocol() + "://" + getServer() + FS + getPath(); +} + +bool URL::operator <(const URL &right) const +{ + // this implementation should be redone probably + + auto p = cmp_case(protocol, right.protocol); + if (p < 0) return true; + if (p > 0) return false; + + auto s = cmp_case(server, right.server); + if (s < 0) return true; + if (s > 0) return false; + + auto d = cmp_case(directory, right.directory); + if (d < 0) return true; + if (d > 0) return false; + + return less_case(file, right.file); +} + +bool URL::operator ==(const URL &right) const +{ + return ( + equal_case(protocol, right.protocol) && + equal_case(server, right.server) && + equal_case(directory, right.directory) && + equal_case(file, right.file) + ); +} + +bool URL::operator !=(const URL &right) const +{ + return (!(*this == right)); +} + +std::ostream &operator <<(std::ostream &out, const URL &url) +{ + return out << (std::string)url; +} + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/internet/URL.h b/tjp/core/internet/URL.h new file mode 100755 index 0000000..8f81044 --- /dev/null +++ b/tjp/core/internet/URL.h @@ -0,0 +1,130 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +namespace tjp { +namespace core { +namespace internet { + +/** + * a URL is defined as a protocal specifying string + a server string + a directory + * + a (optional) file. Such as ftp://myserver/mydirectory/myfile. + * + * For the purposes of this application a URL may be created using a parent URL, in + * which case, if the protocal is NOT specified, the URL is assumed to be a RELATIVE + * URL. In this case, only the directory and file are used.. So, the URL: + * "/directory/myfile" created with the parent URL - "ftp://myserver/mydirectory" + * on construction would expand to: "ftp://myserver/mydirectory/directory/myfile" + * + * I am not sure whether this is exactly the standard or not, perhaps it should + * be modified later. + */ +class URL +{ + public: + /** + * The single exception thrown by the url class on errors + */ + DECLARE_EXCEPTION (Exception); + + protected: + std::string protocol; + std::string server; + std::string directory; + std::string file; + + bool isAbsoluteURL (const std::string &); + void parseAbsoluteURL (const std::string &); + void parseRelativeURL (const std::string &); + + public: + /** + * default constructor + */ + URL (); + + /** + * throws an exception if the protocal is unknown, or if + * the path somehow is invalid, such as "ftp://me.com/../SystemData" + * because that dot.dot my cause the resource to point to our files. + */ + URL (const std::string &); + + /** + * when given a parent URL, if the child URL does not have a + * protocal or server it is interpreted as a relative URL + * + * relative URL's are resolved on construction, pointers are not + * kept hanging around + */ + URL (const URL &parent, const std::string &); + + /** + * copy constructor + */ + URL (const URL &); + + /** + * destructor + */ + virtual ~URL (); + + /** + * returns the string name of the protocal in string form + */ + const std::string &getProtocol () const; + + /** + * returns the server segment on the string (server:port) + */ + const std::string &getServer() const; + + /** + * returns the file segment of the string (file_path) + */ + std::string getPath () const; + + /** + * returns the directory portion of the path + */ + const std::string &getDirectory () const; + + /** + * returns the file portion of the path + */ + const std::string &getFile () const; + + /** + * returns the file portion of the path + */ + static URL join(const URL &, const std::string &); + + /** + * converts the URL into a string + */ + operator std::string() const; + + /** + * comparison operators for container classes + */ + bool operator <(const URL &right) const; + bool operator ==(const URL &right) const; + bool operator !=(const URL &right) const; + + //========================================================== + + /** + * for comparisons + */ + static const URL Empty; +} ; + +std::ostream &operator <<(std::ostream &, const URL &); + +} // namespace utilities +} // namespace utilities +} // namespace + diff --git a/tjp/core/internet/_tests/Internet+common.cpp b/tjp/core/internet/_tests/Internet+common.cpp new file mode 100755 index 0000000..7088f88 --- /dev/null +++ b/tjp/core/internet/_tests/Internet+common.cpp @@ -0,0 +1,21 @@ +// TJP COPYRIGHT HEADER + +#include "../join.hpp" +#include + +namespace tjp::core::internet::test { + +SCENARIO("core::internet") +{ + GIVEN("join") + { + REQUIRE(join("a/", "/b") == "a/b"); + REQUIRE(join("a", "b") == "a/b"); + REQUIRE(join("a/", "b") == "a/b"); + REQUIRE(join("a", "/b") == "a/b"); + REQUIRE(join("a", "") == "a"); + REQUIRE(join("", "b") == "b"); + } +} + +} // namespace diff --git a/tjp/core/internet/curl/Internet.cpp b/tjp/core/internet/curl/Internet.cpp new file mode 100755 index 0000000..b2c9b19 --- /dev/null +++ b/tjp/core/internet/curl/Internet.cpp @@ -0,0 +1,289 @@ +// TJP COPYRIGHT HEADER + +#include + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#include "Internet.hpp" + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + + +//=============================================================================== + +namespace tjp { +namespace core { +namespace internet { + +TJP_CORE_HEADER_ONLY_INLINE +size_t receiveContentsCallback(void *ptr, size_t size, size_t nmemb, void *userdata) +{ + return internal::receiveContentsCallback(ptr, size*nmemb, userdata); +} + +TJP_CORE_HEADER_ONLY_INLINE +size_t sendContentsCallback(char *ptr, size_t size, size_t nmemb, void *userdata) +{ + return internal::sendContentsCallback(ptr, size*nmemb, userdata); +} + +TJP_CORE_HEADER_ONLY_INLINE +size_t receiveHeadersCallback(char *ptr, size_t size, size_t nitems, void *userdata) +{ + return internal::receiveHeadersCallback(ptr, size*nitems, userdata); + +} + +TJP_CORE_HEADER_ONLY_INLINE +Future get (const std::string &url, const HttpOptions &options) +{ + auto *connection = curl_easy_init(); + + ExecuteOnDestruct cleanup = [&]() { + curl_easy_cleanup(connection); + }; + + auto request = strong(); + request->type = internal::RequestType::GET; + request->url = url; + request->options = options; + + perform(request, connection); + + return request->promise.get_future(); +} + +TJP_CORE_HEADER_ONLY_INLINE +Future post (const std::string &url, Data &&data, const HttpOptions &options) +{ + auto *connection = curl_easy_init(); + + ExecuteOnDestruct cleanup = [&]() { + curl_easy_cleanup(connection); + }; + + auto request = strong(); + request->type = internal::RequestType::POST; + request->url = url; + request->options = options; + request->data = std::move(data); + + perform(request, connection); + + return request->promise.get_future(); +} + +TJP_CORE_HEADER_ONLY_INLINE +void perform (const WeakPtr &request_, CURL *connection) +{ + sLogDebug("core::internet", "perform"); + + curl_easy_reset(connection); + + auto getFile = [](const WeakPtr &request_, CURL *connection) { + + internal::ProgressiveData data; + data.request = request_; + String url; + + struct curl_slist* headers = NULL; + + if (auto request = strong(request_)) + { + url = request->url; + data.options = std::move(request->options); + if (request->data) + data.send.data = std::move(*request->data); + + sLogDebug("core::internet", "starting " << logVar(url)); + + FileContents contents; + + if (!connection) + return contents; + + CURLU *u = curl_url(); + char *user_ = nullptr; + char *password_ = nullptr; + Optional user, password; + + if (curl_url_set(u, CURLUPART_URL, url.c_str(), 0) == CURLUE_OK) + { + if (curl_url_get(u, CURLUPART_USER, &user_, 0) == CURLUE_OK) + user = user_; + + if (curl_url_get(u, CURLUPART_PASSWORD, &password_, 0) == CURLUE_OK) + password = password_; + + } + curl_url_cleanup(u); + + for (auto &header: data.options.headers) + { + auto &[key, value] = header; + sLogDebug("core::internet::headers", "send header " << logVar(key) << logVar(value)); + + headers = curl_slist_append(headers, (key + ": " + value).c_str()); + } + + curl_easy_setopt (connection, CURLOPT_HTTPHEADER, headers); + + curl_easy_setopt(connection, CURLOPT_SSL_VERIFYPEER, false); + + // if (CA_BUNDLE_PATH) + // curl_easy_setopt(connection, CURLOPT_CAINFO, CA_BUNDLE_PATH); + + curl_easy_setopt (connection, CURLOPT_USERAGENT, "User-Agent: Curl Client"); +// curl_easy_setopt (connection, CURLOPT_ACCEPT_ENCODING, "gzip, deflate"); +// curl_easy_setopt (connection, CURLOPT_ACCEPT_ENCODING, ""); + curl_easy_setopt (connection, CURLOPT_URL, url.c_str()); + curl_easy_setopt (connection, CURLOPT_WRITEFUNCTION, receiveContentsCallback); + curl_easy_setopt (connection, CURLOPT_WRITEDATA, (void *)&data); + curl_easy_setopt (connection, CURLOPT_HEADERFUNCTION, receiveHeadersCallback); + curl_easy_setopt (connection, CURLOPT_HEADERDATA, (void *)&data); + + if (user) + curl_easy_setopt(connection, CURLOPT_USERNAME, user->c_str()); + if (password) + curl_easy_setopt(connection, CURLOPT_PASSWORD, password->c_str()); + + curl_easy_setopt(connection, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + + if (data.options.httpVersion == "2") + curl_easy_setopt(connection, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); + + if (data.options.maxRedirects) + { + curl_easy_setopt(connection, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(connection, CURLOPT_MAXREDIRS, data.options.maxRedirects); + curl_easy_setopt(connection, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL); + } + + if (request->type == internal::RequestType::GET) + { + curl_easy_setopt(connection, CURLOPT_HTTPGET, 1L); + } + else + if (request->type == internal::RequestType::HEAD) + { + curl_easy_setopt(connection, CURLOPT_NOBODY, 1); + } + else + if (request->type == internal::RequestType::POST) + { + DataView block = data.send.data; + curl_easy_setopt(connection, CURLOPT_POST, 1); + curl_easy_setopt(connection, CURLOPT_POSTFIELDS, block.data()); + curl_easy_setopt(connection, CURLOPT_POSTFIELDSIZE_LARGE, block.size()); + } + else + if (request->type == internal::RequestType::PUT) + { + curl_easy_setopt(connection, CURLOPT_UPLOAD, 1); + curl_easy_setopt(connection, CURLOPT_READDATA, &data); + curl_easy_setopt(connection, CURLOPT_READFUNCTION, sendContentsCallback); + curl_easy_setopt(connection, CURLOPT_INFILESIZE_LARGE, (curl_off_t)data.send.data.size()); + } + + if (data.options.timeout) + curl_easy_setopt(connection, CURLOPT_TIMEOUT, data.options.timeout); + + // curl_easy_setopt (connection, CURLOPT_DNS_CACHE_TIMEOUT, 0L); + // curl_easy_setopt (connection, CURLOPT_NOSIGNAL, 1L); + // curl_easy_setopt (connection, CURLOPT_DNS_USE_GLOBAL_CACHE, 0L); + curl_easy_setopt (connection, CURLOPT_FILETIME, 1L); + + if (data.options.verbose) + curl_easy_setopt(connection, CURLOPT_VERBOSE, 1L); + + } + else + { + throw Exception ( + "HTTPConnection canceled", + {} + ); + } + + StatusCode code; + CURLcode res = curl_easy_perform(connection); + curl_slist_free_all(headers); + + if (res != CURLE_OK) + { + data.receive.data.statusCode = GeneralFailure; + } + else + { + curl_easy_getinfo (connection, CURLINFO_RESPONSE_CODE, &code); + data.receive.data.statusCode = code; + xLogDebug (logVar(code)); + } + + StatusCode lastModifiedTime; + curl_easy_getinfo(connection, CURLINFO_FILETIME, &lastModifiedTime); + data.receive.data.lastModifiedTime = lastModifiedTime; + + if (data.receive.data.statusCode < 0) + data.receive.data.statusCode = GeneralFailure-1; + + if (data.interrupted) + data.receive.data.statusCode = GeneralFailure-2; + + sLogDebugIf(res, "debug", "finishing " << logVar(url) << logVar(res) << logVar(data.receive.data.statusCode)); + + if (data.options.onHeaders) + data.options.onHeaders(data.receive.data.headers, (int)data.receive.data.statusCode); + + if (data.options.onComplete) + data.options.onComplete(data.receive.data.statusCode); + + sLogDebugIf(res, "debug", "finished " << logVar(url) << logVar(res) << logVar(data.receive.data.statusCode)); + + if (res > 0) + { + sLogRelease("errors", "get http failed " << logVar(url) << logVar(res)); + throw Exception ( + "HTTPConnection getFile failed " + std::to_string(res), + std::move(data.receive.data) + ); + } + if (data.options.throwOnFailure && !data.receive.data.ok()) + { + sLogRelease("errors", "get http failed " << logVar(url) << logVar(data.receive.data.statusCode)); + throw Exception ( + "HTTPConnection getFile failed code " + std::to_string(data.receive.data.statusCode), + std::move(data.receive.data) + ); + } + + return data.receive.data; + }; + + Promise promise; + if (auto request = strong(request_)) + { + sLogDebug("core::internet", "perform will consume"); + promise = std::move(request->promise); + } + + promise.consume(getFile, request_, connection); +} + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/internet/curl/Internet.hpp b/tjp/core/internet/curl/Internet.hpp new file mode 100755 index 0000000..d7fa0e9 --- /dev/null +++ b/tjp/core/internet/curl/Internet.hpp @@ -0,0 +1,17 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "../Internet.hpp" +#include "../Internet+Internal.hpp" + +namespace tjp::core::internet { + +typedef void CURL; +void perform (const WeakPtr &request, CURL *connection); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "Internet.cpp" +#endif diff --git a/tjp/core/internet/curl/Queue.cpp b/tjp/core/internet/curl/Queue.cpp new file mode 100755 index 0000000..e08f7c6 --- /dev/null +++ b/tjp/core/internet/curl/Queue.cpp @@ -0,0 +1,126 @@ +// TJP COPYRIGHT HEADER + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#include "../Queue.hpp" +#include "Internet.hpp" +#include + +#include +#include +#include + +#include +#include + +#include + +//=============================================================================== + +namespace tjp::core::internet { + +struct Queue::Internal +{ + std::thread thread; + bool finished = false; + + std::queue> queue; + Event event; + Event::Mutex mutex; + + CURL *curl; + + void run (); + void runOne (const WeakPtr &i); + + Internal(); + ~Internal(); +} ; + +TJP_CORE_HEADER_ONLY_INLINE +Queue::Queue() +{ +} + +TJP_CORE_HEADER_ONLY_INLINE +Queue::~Queue () +{ +} + +TJP_CORE_HEADER_ONLY_INLINE +Queue::Internal::Internal() +{ + curl = curl_easy_init(); +} + +TJP_CORE_HEADER_ONLY_INLINE +Queue::Internal::~Internal() +{ + curl_easy_cleanup(curl); +} + +TJP_CORE_HEADER_ONLY_INLINE +void Queue::Internal::runOne (const WeakPtr &i) +{ + internet::perform(i, curl); +} + +TJP_CORE_HEADER_ONLY_INLINE +void Queue::Internal::run () +{ + threads::process_queue( + finished, + event, + mutex, + queue, + [this](auto &i) { return runOne(i); } + ); +} + +TJP_CORE_HEADER_ONLY_INLINE +void Queue::prepare() +{ + if (i) + return; + + static Mutex mutex; + auto lock = lock_of(mutex); + if (i) + return; + + i = strong(); + i->thread = std::thread([s=i](){ s->run(); }); +} + +TJP_CORE_HEADER_ONLY_INLINE +Future Queue::execute(internal::RequestType type, const std::string &url, Data &&data, const HttpOptions &options) +{ + prepare(); + + auto item = strong_of( + internal::Request { + .type = type, + .url = url, + .options = options, + .data = std::move(data) + } + ); + + { + auto lock = lock_of(i->mutex); + i->queue.push(weak(item)); + } + + i->event.notify_all(); + + return item->promise.get_future().then( + [item](auto &&f) { + return f.get(); + } + ); + +} + +} // namespace diff --git a/tjp/core/internet/curl/Queue.hpp b/tjp/core/internet/curl/Queue.hpp new file mode 100755 index 0000000..5fe3b78 --- /dev/null +++ b/tjp/core/internet/curl/Queue.hpp @@ -0,0 +1,7 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#ifdef TJP_CORE_HEADER_ONLY + #include "Queue.cpp" +#endif diff --git a/tjp/core/internet/form_encode.hpp b/tjp/core/internet/form_encode.hpp new file mode 100755 index 0000000..371f6d5 --- /dev/null +++ b/tjp/core/internet/form_encode.hpp @@ -0,0 +1,49 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include +#include "url_param_encode.hpp" + + +namespace tjp::core::internet { + + +template +void form_encode_(String &result, T &&t) +{ + bool isKey = (result.empty() || result.back() != '='); + + if (isKey && !result.empty()) + result += '&'; + + result += url_param_encode(std::forward(t)); + + if (isKey) + result += '='; +} + +template +String form_encode(Args && ...args) +{ + String result; + (form_encode_(result, std::forward(args)), ...); + + return result; +} + +inline +String form_encode_map(const Map &args) +{ + String result; + for (auto &[key, value]: args) + { + form_encode_(result, key); + form_encode_(result, value); + } + + return result; +} +} // namespace diff --git a/tjp/core/internet/future_json_to_result.hpp b/tjp/core/internet/future_json_to_result.hpp new file mode 100755 index 0000000..f48bf85 --- /dev/null +++ b/tjp/core/internet/future_json_to_result.hpp @@ -0,0 +1,18 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp::core::internet { + +template +auto future_json_to_result() +{ + return [](auto &&f) { + auto f_ = f.get(); + return core::io::json::deserialize(f_.data); + }; +} + +} // namespace diff --git a/tjp/core/internet/get_embedded_value.hpp b/tjp/core/internet/get_embedded_value.hpp new file mode 100755 index 0000000..697add9 --- /dev/null +++ b/tjp/core/internet/get_embedded_value.hpp @@ -0,0 +1,27 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Internet.hpp" +#include + +namespace tjp::core::internet { + +inline +Optional get_embedded_value(const FileContents &value, const StringView &prefix, const StringView &postfix) +{ + auto s = as_string_view(value.data); + + auto p = s.find(prefix); + if (p == -1) + return {}; + + s = s.substr(p + prefix.size()); + auto pe = s.find(postfix); + if (pe == -1) + return {}; + + return StringView(s.substr(0, pe)); +} + +} // namespace diff --git a/tjp/core/internet/get_redirect.hpp b/tjp/core/internet/get_redirect.hpp new file mode 100755 index 0000000..dc3dd3e --- /dev/null +++ b/tjp/core/internet/get_redirect.hpp @@ -0,0 +1,25 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Internet.hpp" +#include + +namespace tjp::core::internet { + +inline +Optional get_redirect(const FileContents &value) +{ + for (auto &header: value.headers) + { + auto &[key, value] = header; + if (cmp_case(key, "location") == 0) + { + return value; + } + } + + return {}; +} + +} // namespace diff --git a/tjp/core/internet/hirose/Internet.cpp b/tjp/core/internet/hirose/Internet.cpp new file mode 100755 index 0000000..c0b47dd --- /dev/null +++ b/tjp/core/internet/hirose/Internet.cpp @@ -0,0 +1,141 @@ +// TJP COPYRIGHT HEADER + +#include + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +// Internet.cpp : Defines the entry point for the DLL application. +// +#include "Internet.hpp" +#include "../../http/hirose/httplib.h" + +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include +#include + +//=============================================================================== + +namespace tjp { +namespace core { +namespace internet { + +TJP_CORE_HEADER_ONLY_INLINE +Future post (const std::string &url, Data &&data, const HttpOptions &options) +{ + Promise promise; + + post(url, std::move(data), options, promise); + + return promise.get_future(); +} + +TJP_CORE_HEADER_ONLY_INLINE +void perform (const std::string &url, const std::string &method, Data &&data, const HttpOptions &options, Promise &promise) +{ + xLogDebug (logVar(url)); + + auto getFile = [url, options, method, data=std::move(data)]() { + sLogDebug("core::internet", "starting " << logVar(url)); + + FileContents contents; + + auto firstSlashSlash = url.find("//"); + auto firstSlash = url.find("/", firstSlashSlash+2); + httplib::Client cli(url.substr(0, firstSlash)); + if (options.timeout) + cli.set_read_timeout(options.timeout); + + httplib::Headers headers; + for (auto &[k,v]: options.headers) + headers.insert({k, v}); + + decltype(cli.Get(url.substr(firstSlash), headers)) result; + if (method == "GET") + result = cli.Get(url.substr(firstSlash), headers); + else + if (method == "POST") + result = cli.Post(url.substr(firstSlash), headers, data, "text/plain"); + else + if (method == "PUT") + result = cli.Put(url.substr(firstSlash), headers, data, "application/binary"); + + if (options.onHeaders) + options.onHeaders(contents.headers, contents.statusCode); + + if (options.onData) + options.onData(contents.data.data(), contents.data.size()); + + if (options.onComplete) + options.onComplete(contents.statusCode); + + if (result) + { + contents.statusCode = result->status; + contents.data = as_vector(result->body); + for (auto &header: result->headers) + contents.headers.push_back(header); + contents.lastModifiedTime = 0; + } + else + { + sLogRelease("errors", "get http failed " << logVar(url) << logVar(result.error())); + throw Exception ( + "HTTPConnection getFile failed " + std::to_string((int)result.error()), + std::move(contents) + ); + } + if (options.throwOnFailure && !contents.ok()) + { + sLogRelease("errors", "get http failed " << logVar(url) << logVar(contents.statusCode)); + throw Exception ( + "HTTPConnection getFile failed code " + std::to_string(contents.statusCode), + std::move(contents) + ); + } + + return contents; + }; + + promise_of(promise, getFile); +} + +TJP_CORE_HEADER_ONLY_INLINE +void get (const std::string &url,const HttpOptions &options, Promise &promise) +{ + return perform(url, "GET", Data {}, options, promise); +} + + +TJP_CORE_HEADER_ONLY_INLINE +void post (const std::string &url, Data &&data, const HttpOptions &options, Promise &promise) +{ + return perform(url, "POST", std::move(data), options, promise); +} + +TJP_CORE_HEADER_ONLY_INLINE +void put (const std::string &url, Data &&data, const HttpOptions &options, Promise &promise) +{ + return perform(url, "PUT", std::move(data), options, promise); +} + +TJP_CORE_HEADER_ONLY_INLINE +void head (const std::string &url, Data &&data, const HttpOptions &options, Promise &promise) +{ + return perform(url, "HEAD", std::move(data), options, promise); +} + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/internet/hirose/Internet.hpp b/tjp/core/internet/hirose/Internet.hpp new file mode 100755 index 0000000..7aea4ce --- /dev/null +++ b/tjp/core/internet/hirose/Internet.hpp @@ -0,0 +1,16 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "../Internet.hpp" + +namespace tjp::core::internet { + +void get (const std::string &url, const HttpOptions &options, Promise &promise); +void post (const std::string &url, Data &&, const HttpOptions &options, Promise &promise); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY + #include "Internet.cpp" +#endif diff --git a/tjp/core/internet/html_decode.hpp b/tjp/core/internet/html_decode.hpp new file mode 100755 index 0000000..d4e2e5a --- /dev/null +++ b/tjp/core/internet/html_decode.hpp @@ -0,0 +1,66 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +#include +#include +#include +#include + +namespace tjp::core::internet { + +inline +String html_decode(const StringView &value) +{ + std::ostringstream decoded; + imemstream input(value.data(), value.size()); + char c; + + // Map of common HTML entities + static const std::unordered_map html_entities = { + {"quot", '"'}, {"amp", '&'}, {"lt", '<'}, {"gt", '>'}, {"nbsp", ' '}, + {"apos", '\''} + }; + + while (input.get(c)) { + if (c == '%') { // URL decoding + char hex[3] = {0}; + if (input.get(hex[0]) && input.get(hex[1])) { + decoded << static_cast(std::stoi(hex, nullptr, 16)); + } + } else if (c == '+') { + decoded << ' '; + } else if (c == '&') { // HTML entity decoding + std::string entity; + char entity_char; + while (input.get(entity_char) && entity_char != ';' && entity.size() < 10) { + entity += entity_char; + } + + if (html_entities.count(entity)) { + decoded << html_entities.at(entity); + } else if (!entity.empty() && entity[0] == '#') { + try { + if (entity[1] == 'x' || entity[1] == 'X') { + decoded << static_cast(std::stoi(entity.substr(2), nullptr, 16)); + } else { + decoded << static_cast(std::stoi(entity.substr(1), nullptr, 10)); + } + } catch (...) { + decoded << "&" << entity << ";"; // Preserve invalid entity + } + } else { + decoded << "&" << entity << ";"; // Preserve unknown entity + } + } else { + decoded << c; + } + } + + return decoded.str(); +} + +} // namespace diff --git a/tjp/core/internet/ios/Internet.hpp b/tjp/core/internet/ios/Internet.hpp new file mode 100755 index 0000000..31e8ab9 --- /dev/null +++ b/tjp/core/internet/ios/Internet.hpp @@ -0,0 +1,18 @@ +// TJP COPYRIGHT HEADER + +#include + +#include "../Internet.hpp" +#include "../Internet+Internal.hpp" + +namespace tjp::core::internet { + +void perform (const WeakPtr &request); + +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY +#ifdef __OBJC__ + #include "Internet.mm" +#endif +#endif diff --git a/tjp/core/internet/ios/Internet.mm b/tjp/core/internet/ios/Internet.mm new file mode 100755 index 0000000..7642b07 --- /dev/null +++ b/tjp/core/internet/ios/Internet.mm @@ -0,0 +1,614 @@ +#include + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#pragma clang diagnostic ignored "-Wnullability-completeness" + +#import + +#include "Internet.hpp" +#include +#include +#include + +#include +#include +#include + +#include + +// http://iphoneincubator.com/blog/server-communication/how-to-download-a-file-only-if-it-has-been-updated/comment-page-1 + +//=============================================================================== + +typedef bool (*PartialCallback)(const void *bytes, NSUInteger length, void * _Nonnull context); +typedef void (*CompleteCallback)(int statusCode, const char * _Nullable errorMessage, void * _Nonnull context); +typedef bool (*HeaderLineCallback)(const void *line, NSUInteger length, void * _Nonnull context); + +@interface ProgressiveDownloader : NSObject +{ + PartialCallback partialCallback; + CompleteCallback completeCallback; + HeaderLineCallback headerLineCallback; + NSString *method; + + size_t maxRedirects; + void *callbackContext; +} + +@property (nonatomic, strong) NSURLSession *session; +@property (nonatomic, strong) NSMutableData *receivedData; + +- (instancetype)initWithPartial:(PartialCallback)partial + complete:(CompleteCallback)complete + headers:(HeaderLineCallback)headers + context:(void *)context + maxRedirects:(size_t)maxRedirects + method:(NSString *)method; + +- (void)startDownload:(NSMutableURLRequest *)url; + +- (void)URLSession:(NSURLSession *)session + dataTask:(NSURLSessionDataTask *)dataTask + didReceiveData:(NSData *)data; + +@end + +@implementation ProgressiveDownloader + +- (instancetype)initWithPartial:(PartialCallback)partial + complete:(CompleteCallback)complete + headers:(HeaderLineCallback)headers + context:(void *)context + maxRedirects:(size_t)maxRedirects_ + method:(NSString *)method_ +{ + self = [super init]; + if (self) + { + partialCallback = partial; + completeCallback = complete; + headerLineCallback = headers; + callbackContext = context; + maxRedirects = maxRedirects_; + method = method_; + + NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; + config.HTTPCookieStorage = nil; + + NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[[NSOperationQueue alloc] init]]; + self.session = session; + +// NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; +// self.session = [NSURLSession sessionWithConfiguration:config +// delegate:self +// delegateQueue:[NSOperationQueue mainQueue]]; + + self.receivedData = [NSMutableData data]; + } + return self; +} + +- (void)startDownload:(NSMutableURLRequest *)request +{ + NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request]; + [task resume]; +} + +// Response received +- (void)URLSession:(NSURLSession *)session + dataTask:(NSURLSessionDataTask *)dataTask +didReceiveResponse:(NSURLResponse *)response + completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler +{ + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; + + if (httpResponse.statusCode/100 == 3) + if (maxRedirects) + { + sLogDebug("core::internet::headers", "getting headers while redirecting - ignoring"); + + return completionHandler(NSURLSessionResponseAllow); + } + + auto continue_task = true; + if (headerLineCallback) + { + NSString *headerLine = [NSString stringWithFormat:@"HTTP/1.1 %d\r\n", httpResponse.statusCode]; + headerLineCallback(headerLine.UTF8String, [headerLine length], callbackContext); + + [httpResponse.allHeaderFields enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) { + NSString *k = [key description]; + NSString *v = [value description]; + NSString *headerLine = [NSString stringWithFormat:@"%@: %@\r\n", k, v]; + headerLineCallback(headerLine.UTF8String, [headerLine length], callbackContext); + }]; + + continue_task = headerLineCallback("\r\n", 2, callbackContext); + } + + if (!continue_task) + [dataTask cancel]; + + completionHandler(NSURLSessionResponseAllow); +} + +// Data arrives progressively +- (void)URLSession:(NSURLSession *)session + dataTask:(NSURLSessionDataTask *)dataTask + didReceiveData:(NSData *)data +{ + auto continue_task = true; + if (partialCallback) + continue_task = partialCallback(data.bytes, data.length, callbackContext); + + [self.receivedData appendData:data]; + + if (!continue_task) + [dataTask cancel]; +} + +// Completion or error +- (void)URLSession:(NSURLSession * _Nonnull)session + task:(NSURLSessionTask * _Nonnull)task +didCompleteWithError:(NSError * _Nullable)error +{ + NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response; + + if (completeCallback) + { + if (error) + completeCallback(response.statusCode, error.localizedDescription.UTF8String, callbackContext); + else + completeCallback(response.statusCode, NULL, callbackContext); + } +} + +// Optionally handle redirect logic if needed +- (void)URLSession:(NSURLSession * _Nonnull)session + task:(NSURLSessionTask * _Nonnull)task + willPerformHTTPRedirection:(NSHTTPURLResponse *)response_ + newRequest:(NSURLRequest *)request + completionHandler:(void (^)(NSURLRequest * _Nullable))completionHandler +{ +// NSHTTPURLResponse *response = (NSHTTPURLResponse *)response_; + + sLogDebug("core::internet", "redirect requested " << + logVar(maxRedirects) + logVar(request.URL.absoluteString.UTF8String) << + logVar(request.HTTPMethod.UTF8String) + ); + + if (maxRedirects) + { + maxRedirects--; + + if (![request.HTTPMethod isEqualToString:method]) + { + sLogDebug("core::internet", "modifying method to match request"); + + NSMutableURLRequest *request_ = request.mutableCopy; + [request_ setHTTPMethod:method]; + request = request_; + } + + completionHandler(request); + } + else + { + completionHandler(nil); + } +} + +@end + + +namespace tjp { +namespace core { +namespace internet { + +struct ProgressiveContext { + internal::ProgressiveData data; + dispatch_semaphore_t semaphor; +} ; + +bool onPartial(const void * _Nullable bytes, NSUInteger length, void * _Nonnull context_) +{ + return internal::receiveContentsCallback((char *)bytes, length, context_) == length; +} + +bool onHeaders(const void * _Nullable bytes, NSUInteger length, void * _Nonnull context_) +{ + return internal::receiveHeadersCallback((char *)bytes, length, context_) == length; +} + +void onComplete(int statusCode, const char * _Nullable errorMessage, void * _Nonnull context_) +{ + auto *context = static_cast(context_); + context->data.receive.data.statusCode = statusCode; + + dispatch_semaphore_signal(context->semaphor); +} + +Future get (const std::string &url, const HttpOptions &options) +{ + auto request = strong(); + request->type = internal::RequestType::GET; + request->url = url; + request->options = options; + + perform(request); + + return request->promise.get_future(); +} + +Future post (const std::string &url, Data &&data, const HttpOptions &options) +{ + auto request = strong(); + request->type = internal::RequestType::POST; + request->url = url; + request->data = std::move(data); + request->options = options; + + perform(request); + + return request->promise.get_future(); +} + + +void perform_progressive(const WeakPtr &request_) +{ + auto getFile = [](const WeakPtr &request_) + { + ProgressiveContext context; + auto &data = context.data; + data.request = request_; + context.semaphor = dispatch_semaphore_create(0); + + String url; + ProgressiveDownloader *downloader; + NSString *method; + + if (auto request__ = strong(request_)) + { + url = request__->url; + + data.options = std::move(request__->options); + if (request__->data) + data.send.data = std::move(*request__->data); + + NSString *stringURL = [NSString stringWithUTF8String:url.c_str()]; + + // without this, iOS <= 16 doesn't work for all urls + NSCharacterSet *allowed = [NSCharacterSet URLFragmentAllowedCharacterSet]; + NSString *stringURLEncoded = [stringURL stringByAddingPercentEncodingWithAllowedCharacters:allowed]; + + NSURL *url_ = [NSURL URLWithString:stringURLEncoded]; + + sLogDebug("core::internet", "starting " << logVar(url) << logVar(stringURLEncoded.UTF8String)); + + // Prepare HTTP request + NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url_]; + [request setCachePolicy:NSURLRequestReloadIgnoringCacheData]; + // [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"]; + + if (data.options.timeout) + request.timeoutInterval = data.options.timeout; + + auto &requestType = request__->type; + if (requestType == internal::RequestType::POST) + { + method = @"POST"; + + sLogDebug("core::internet", "starting " << "POST"); + [request setHTTPMethod:method]; + NSData *data_ = [NSData dataWithBytes:data.send.data.c_str() length:data.send.data.size()]; + [request setHTTPBody:data_]; + } + else + if (requestType == internal::RequestType::PUT) + { + method = @"PUT"; + + sLogDebug("core::internet", "starting " << "PUT"); + [request setHTTPMethod:method]; + NSData *data_ = [NSData dataWithBytes:data.send.data.data() length:data.send.data.size()]; + [request setHTTPBody:data_]; + } + else + if (requestType == internal::RequestType::GET) + { + method = @"GET"; + + sLogDebug("core::internet", "starting " << "GET"); + [request setHTTPMethod:method]; + } + else + if (requestType == internal::RequestType::HEAD) + { + method = @"HEAD"; + + sLogDebug("core::internet", "starting " << "HEAD"); + [request setHTTPMethod:method]; + } + + for (auto &header : data.options.headers) + { + auto &[key, value] = header; + sLogDebug("core::internet::headers", "send header " << logVar(key) << logVar(value)); + + NSString *key_ = [NSString stringWithUTF8String:key.c_str()]; + NSString *value_ = [NSString stringWithUTF8String:value.c_str()]; + [request setValue:value_ forHTTPHeaderField:key_]; + } + + if (data.options.lastModifiedTime) + { + NSDate *date = [NSDate dateWithTimeIntervalSince1970:data.options.lastModifiedTime]; + NSDateFormatter *df = [[NSDateFormatter alloc] init]; + df.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss Z"; + df.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; + df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; + NSString *dateString = [df stringFromDate:date]; + [request setValue:dateString forHTTPHeaderField:@"If-Modified-Since"]; + } + + if (url_.user) + { + NSString *user = url_.user; + NSString *pass = url_.password ?: @""; + NSString *auth = [NSString stringWithFormat:@"%@:%@", user, pass]; + + NSData *authData = [auth dataUsingEncoding:NSUTF8StringEncoding]; + NSString *authValue = [NSString stringWithFormat:@"Basic %@", + [authData base64EncodedStringWithOptions:0]]; + + [request setValue:authValue forHTTPHeaderField:@"Authorization"]; + } + + [request setValue:@"close" forHTTPHeaderField:@"Connection"]; + + // --- Define progressive callbacks --- + + downloader = + [[ProgressiveDownloader alloc] + initWithPartial:onPartial + complete:onComplete + headers:onHeaders + context:&context + maxRedirects:data.options.maxRedirects + method:method + ]; + + [downloader startDownload:request]; + } + else + { + throw Exception ( + "HTTPConnection canceled", + {} + ); + } + + dispatch_semaphore_wait(context.semaphor, DISPATCH_TIME_FOREVER); + + sLogDebug("core::internet", "finishing 0"); + + if (data.receive.data.statusCode < 0) + data.receive.data.statusCode = internet::GeneralFailure; + + if (data.interrupted) + data.receive.data.statusCode = internet::GeneralFailure; + + if (data.options.onHeaders) + data.options.onHeaders(data.receive.data.headers, data.receive.data.statusCode); + + if (data.options.onComplete) + data.options.onComplete(data.receive.data.statusCode); + + sLogDebug("core::internet", "finishing 1"); + + auto got_error = data.receive.data.statusCode >= 400; + + sLogReleaseIf(got_error, "debug", "finished " << logVar(url) << logVar(got_error)); + + if (got_error) + { + throw Exception("HTTPConnection getFile failed", std::move(data.receive.data)); + } + + if (data.options.throwOnFailure && !data.receive.data.ok()) + { + sLogRelease("errors", "get http failed: " << logVar(url) << logVar(data.receive.data.statusCode)); + throw Exception( + "HTTPConnection getFile failed code " + std::to_string(data.receive.data.statusCode), + std::move(data.receive.data)); + } + + return data.receive.data; + }; + + Promise promise; + if (auto request = strong(request_)) + promise = std::move(request->promise); + + promise.consume(getFile, request_); +} + +#if REMOVE_CODE_2025_11_11 +# code is superseded by perform + +void perform_completion (const std::string &url, const std::string &method, Data &&data, const HttpOptions &options, Promise &promise) +{ + xLogDebug (logVar(url)); + + auto getFile = [&]() { + __block FileContents contents; + + sLogDebug("core::internet", "starting " << url); + + NSString *stringURL = [NSString stringWithUTF8String:url.c_str()]; + NSURL *url_ = [NSURL URLWithString:stringURL]; + +// NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration]; // No cookie storage + NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; // No cookie storage +// config.HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicyNever; + config.HTTPCookieStorage = nil; + + NSURLSession *session = [NSURLSession sessionWithConfiguration:config]; +// NSURLSession *session = [NSURLSession sharedSession]; + NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url_]; + + if (options.timeout) + request.timeoutInterval = options.timeout; + + [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"]; + [request setCachePolicy:NSURLRequestReloadIgnoringCacheData]; + + if (method == "POST") + { + [request setHTTPMethod:@"POST"]; + + NSString *str = [NSString stringWithUTF8String:data.c_str()]; + NSData *data_ = [str dataUsingEncoding:NSUTF8StringEncoding]; + + [request setHTTPBody:data_]; + } + else + if (method == "GET") + { + [request setHTTPMethod:@"GET"]; + } + else + if (method == "HEAD") + { + [request setHTTPMethod:@"HEAD"]; + } + else + if (method == "PUT") + { + [request setHTTPMethod:@"PUT"]; + + NSData *data_ = [NSData dataWithBytes:data.data() length:data.size()]; + [request setHTTPBody:data_]; + } + + for (auto &header : options.headers) + { + auto &[key, value] = header; + NSString *key_ = [NSString stringWithUTF8String:key.c_str()]; + NSString *value_ = [NSString stringWithUTF8String:value.c_str()]; + + [request setValue:value_ forHTTPHeaderField:key_]; + } + + if (options.lastModifiedTime) + { + NSString *key_ = @"If-Modified-Since"; + NSDate *date = [NSDate dateWithTimeIntervalSince1970:options.lastModifiedTime]; + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // maybe there exist a new-method now + dateFormatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss Z"; //RFC2822-Format + dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US_POSIX"]; + dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; + NSString *dateString = [dateFormatter stringFromDate:date]; + + [request setValue:dateString forHTTPHeaderField:key_]; + } + + dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); + __block bool got_error = false; + + NSURLSessionDataTask *downloadTask = + [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response_, NSError *error) { + if (!error) + { + NSHTTPURLResponse *response = (NSHTTPURLResponse *)response_; + NSString *lastModifiedString = [[response allHeaderFields] objectForKey:@"Last-Modified"]; + + if (lastModifiedString) + { + NSDate *lastModifiedServer = nil; + @try { + NSDateFormatter *df = [[NSDateFormatter alloc] init]; + df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'"; + df.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; + df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; + lastModifiedServer = [df dateFromString:lastModifiedString]; + } + @catch (NSException * e) { + NSLog(@"Error parsing last modified date: %@ - %@", lastModifiedString, [e description]); + } + + contents.lastModifiedTime = [lastModifiedServer timeIntervalSince1970]; + } + + NSDictionary *headers = [response allHeaderFields]; + NSArray *keys = [headers allKeys]; + + for (NSString* key in keys) + { + NSString *value = [headers objectForKey:key]; + contents.headers.push_back({ key.UTF8String, value.UTF8String }); + } + + contents.data.resize(data.length); + contents.statusCode = response.statusCode; + memcpy(contents.data.data(), data.bytes, contents.data.size()); + } + else + { + got_error = true; + } + + dispatch_semaphore_signal(semaphore); + }]; + + [downloadTask resume]; + + dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); + + if (options.onHeaders) + options.onHeaders(contents.headers, contents.statusCode); + + if (options.onData) + options.onData(contents.data.data(), contents.data.size()); + + if (options.onComplete) + options.onComplete(contents.statusCode); + + sLogReleaseIf(got_error, "debug", "finished " << logVar(url) << logVar(got_error)); + + if (got_error) + throw Exception ( + "HTTPConnection getFile failed", + std::move(contents) + ); + + if (options.throwOnFailure && !contents.ok()) + { + sLogRelease("errors", "get http failed: " << logVar(url) << logVar(contents.statusCode)); + throw Exception ( + "HTTPConnection getFile failed code " + std::to_string(contents.statusCode), + std::move(contents) + ); + } + + return contents; + }; + + promise_of(promise, getFile); +} + +#endif + +void perform(const WeakPtr &request_) +{ + perform_progressive(request_); +} + + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/internet/ios/Queue.cpp b/tjp/core/internet/ios/Queue.cpp new file mode 100755 index 0000000..95d3be1 --- /dev/null +++ b/tjp/core/internet/ios/Queue.cpp @@ -0,0 +1,106 @@ +// TJP COPYRIGHT HEADER + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#include "../Queue.hpp" +#include "Internet.hpp" + +#include +#include + +#include +#include + +#include + +//=============================================================================== + +namespace tjp::core::internet { + +struct Queue::Internal +{ + std::thread thread; + bool finished = false; + + std::queue> queue; + Event event; + Event::Mutex mutex; + + void run (); + void runOne (const WeakPtr &i); +} ; + +TJP_CORE_HEADER_ONLY_INLINE +Queue::Queue() +{ +} + +TJP_CORE_HEADER_ONLY_INLINE +Queue::~Queue () +{ +} + +TJP_CORE_HEADER_ONLY_INLINE +void Queue::Internal::runOne (const WeakPtr &i_) +{ + perform(i_); +} + +TJP_CORE_HEADER_ONLY_INLINE +void Queue::Internal::run () +{ + threads::process_queue( + finished, + event, + mutex, + queue, + [this](auto &i) { return runOne(i); } + ); +} + +TJP_CORE_HEADER_ONLY_INLINE +void Queue::prepare() +{ + if (i) + return; + + static Mutex mutex; + auto lock = lock_of(mutex); + if (i) + return; + + i = strong(); + i->thread = std::thread([s=i](){ s->run(); }); +} + +TJP_CORE_HEADER_ONLY_INLINE +Future Queue::execute(internal::RequestType type, const std::string &url, Data &&data, const HttpOptions &options) +{ + prepare(); + + auto item = strong_of_value( + internal::Request { + .type = type, + .url = url, + .options = options, + .data = std::move(data) + } + ); + + { + auto lock = lock_of(i->mutex); + i->queue.push(weak(item)); + } + + i->event.notify_all(); + + return item->promise.get_future().then( + [item](auto &&f) { + return f.get(); + } + ); +} + +} // namespace diff --git a/tjp/core/internet/ios/Queue.hpp b/tjp/core/internet/ios/Queue.hpp new file mode 100755 index 0000000..5fe3b78 --- /dev/null +++ b/tjp/core/internet/ios/Queue.hpp @@ -0,0 +1,7 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#ifdef TJP_CORE_HEADER_ONLY + #include "Queue.cpp" +#endif diff --git a/tjp/core/internet/join.hpp b/tjp/core/internet/join.hpp new file mode 100755 index 0000000..421baab --- /dev/null +++ b/tjp/core/internet/join.hpp @@ -0,0 +1,39 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +namespace tjp::core::internet { + +inline +void join_(String &base, const StringView &url) +{ + if (base.empty()) + { + concat_(base, url); + } + else + if (!url.empty()) + { + if (base.back() == '/' && url.front() == '/') + concat_(base, url.substr(1)); + else + if (base.back() != '/' && url.front() != '/') + concat_(base, "/", url); + else + concat_(base, url); + } +} + +template +String join(Args&& ...args) +{ + String result; + (join_(result, std::forward(args)), ...); + + return result; +} + +} // namespace diff --git a/tjp/core/internet/url_param_decode.hpp b/tjp/core/internet/url_param_decode.hpp new file mode 100755 index 0000000..cdd4abb --- /dev/null +++ b/tjp/core/internet/url_param_decode.hpp @@ -0,0 +1,39 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include + +#include +#include +#include +#include + +namespace tjp::core::internet { + +inline +String url_param_decode(const StringView &value) +{ + std::ostringstream decoded; + io::imemstream input(value.data(), value.size()); + char c; + + while (input.get(c)) { + if (c == '%') { + char hex[3] = {0}; + if (input.get(hex[0]) && input.get(hex[1])) { + decoded << static_cast(std::stoi(hex, nullptr, 16)); + } + } else if (c == '+') { + decoded << ' '; + } else { + decoded << c; + } + } + + return decoded.str(); +} + +} // namespace diff --git a/tjp/core/internet/url_param_encode.hpp b/tjp/core/internet/url_param_encode.hpp new file mode 100755 index 0000000..ed22157 --- /dev/null +++ b/tjp/core/internet/url_param_encode.hpp @@ -0,0 +1,40 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +#include +#include +#include +#include + +namespace tjp::core::internet { + +inline +String url_param_encode(const StringView &value) +{ + std::ostringstream escaped; + escaped.fill('0'); + escaped << std::hex; + + for (auto i = value.begin(), n = value.end(); i != n; ++i) { + auto c = (*i); + + // Keep alphanumeric and other accepted characters intact + if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') { + escaped << c; + continue; + } + + // Any other characters are percent-encoded + escaped << std::uppercase; + escaped << '%' << std::setw(2) << int((unsigned char) c); + escaped << std::nouppercase; + } + + return escaped.str(); +} + +} // namespace diff --git a/tjp/core/internet/url_path_decode.hpp b/tjp/core/internet/url_path_decode.hpp new file mode 100755 index 0000000..a704b7a --- /dev/null +++ b/tjp/core/internet/url_path_decode.hpp @@ -0,0 +1,15 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "url_param_decode.hpp" + +namespace tjp::core::internet { + +inline +String url_path_decode(const StringView &value) +{ + return url_param_decode(value); +} + +} // namespace diff --git a/tjp/core/internet/url_path_encode.hpp b/tjp/core/internet/url_path_encode.hpp new file mode 100755 index 0000000..2d92ed6 --- /dev/null +++ b/tjp/core/internet/url_path_encode.hpp @@ -0,0 +1,40 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +#include +#include +#include +#include + +namespace tjp::core::internet { + +inline +String url_path_encode(const StringView &value) +{ + std::ostringstream escaped; + escaped.fill('0'); + escaped << std::hex; + + for (auto i = value.begin(), n = value.end(); i != n; ++i) { + auto c = (*i); + + // Keep alphanumeric and other accepted characters intact + if (isalnum(c) || c == ':' || c == '/' || c == '-' || c == '_' || c == '.' || c == '~') { + escaped << c; + continue; + } + + // Any other characters are percent-encoded + escaped << std::uppercase; + escaped << '%' << std::setw(2) << int((unsigned char) c); + escaped << std::nouppercase; + } + + return escaped.str(); +} + +} // namespace diff --git a/tjp/core/internet/url_with_params.hpp b/tjp/core/internet/url_with_params.hpp new file mode 100755 index 0000000..5f6ede3 --- /dev/null +++ b/tjp/core/internet/url_with_params.hpp @@ -0,0 +1,16 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "form_encode.hpp" +#include "url_path_encode.hpp" + +namespace tjp::core::internet { + +template +String url_with_params(const StringView &url, Args && ...args) +{ + return url_path_encode(url) + "?" + form_encode(std::forward(args)...); +} + +} // namespace diff --git a/tjp/core/mysql/MysqlConnection.h b/tjp/core/mysql/MysqlConnection.h new file mode 100755 index 0000000..85f6862 --- /dev/null +++ b/tjp/core/mysql/MysqlConnection.h @@ -0,0 +1,11 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +#ifdef SYS_LINUX + #include +#else + #include +#endif diff --git a/tjp/core/mysql/MysqlQueries.h b/tjp/core/mysql/MysqlQueries.h new file mode 100755 index 0000000..06ef744 --- /dev/null +++ b/tjp/core/mysql/MysqlQueries.h @@ -0,0 +1,19 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +#ifdef SYS_LINUX + #include + #include + #include + #include + #include +#else + #include + #include + #include + #include + #include +#endif diff --git a/tjp/core/pipe/Pipe.h b/tjp/core/pipe/Pipe.h new file mode 100755 index 0000000..7674cf4 --- /dev/null +++ b/tjp/core/pipe/Pipe.h @@ -0,0 +1,85 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include +#include + +namespace tjp { +namespace core { + +template +struct Pipe +{ + mutable Mutex mutex; + + Pipe(); + ~Pipe(); + + typedef std::list PacketList; + PacketList pipe; + size_t size_; + bool open; + bool flush_; + + size_t sizeReadRequested = 0; + size_t sizeRead = 0; + void clearSizeRead(); + + size_t write (Packet &&); + size_t write_no_lock (Packet &&); + + Packet read (); + Packet read_no_lock (); + + bool empty() const; + bool empty_no_lock() const; + + void close(); + bool isOpen() const; + bool eos() const; + bool finishing() const; + bool flushing() const; + + void flush() { flush_ = true; } + bool flushed(); + + size_t offset; + + template + void readInto (PipeOut &rhs) + { + while (!empty()) + { + rhs.write(read()); + } + + if (flushed()) + rhs.flush(); + } + + size_t read (char *buffer, size_t size_t); + size_t read_no_lock (char *buffer, size_t size_t); + + size_t write (const char *buffer, size_t size_t); + size_t peek (char *buffer, size_t size_t) const; + size_t peek_no_lock (char *buffer, size_t size_t) const; + + size_t skip(size_t size); + size_t skip_no_lock(size_t size); + + size_t resize (size_t maximumSize); + + size_t size () const; + const Packet *peek() const; + + void clear(); +} ; + +} // namespace +} // namespace + +#include "Pipe.inl" + diff --git a/tjp/core/pipe/Pipe.inl b/tjp/core/pipe/Pipe.inl new file mode 100755 index 0000000..ee02639 --- /dev/null +++ b/tjp/core/pipe/Pipe.inl @@ -0,0 +1,294 @@ +// TJP COPYRIGHT HEADER + +#include "Pipe.h" +#include + +namespace tjp { +namespace core { + +template +Pipe::Pipe () : + offset(0), + size_(0), + open(true) +{ +} + +template +Pipe::~Pipe () +{ +} + +template +void Pipe::close() +{ + auto l = lock_of(mutex); + + open = false; +} + +template +bool Pipe::isOpen() const +{ + auto l = lock_of(mutex); + return open; +} + +template +size_t Pipe::write_no_lock (Packet &&packet) +{ + debug_assert(mutex.locked_by_caller()); + + if (packet.size() == 0) + return size_; + + size_ += packet.size(); + pipe.emplace_back(std::move(packet)); + + return size_; +} + +template +size_t Pipe::write (Packet &&packet) +{ + auto l = lock_of(mutex); + + return write_no_lock(std::move(packet)); +} + +template +Packet Pipe::read_no_lock () +{ + debug_assert(!pipe.empty()); + debug_assert(offset == 0); + debug_assert(mutex.locked_by_caller()); + + auto p = std::move(pipe.front()); + pipe.pop_front(); + + debug_assert(size_ >= p.size()); + size_ -= p.size(); + offset = 0; + return p; +} + +template +Packet Pipe::read () +{ + auto l = lock_of(mutex); + + return read_no_lock(); +} + +template +bool Pipe::empty_no_lock() const +{ + return pipe.empty(); +} + +template +bool Pipe::empty() const +{ + auto l = lock_of(mutex); + + return empty_no_lock(); +} + +// --------------------- + +template +size_t Pipe::peek_no_lock (char *buffer, size_t size) const +{ + debug_assert(mutex.locked_by_caller()); + + size_t ptr = 0; + size_t offset = this->offset; + + auto &buffers = pipe; + + typename PacketList::const_iterator i=buffers.begin(); + while (i!=buffers.end() && (size > 0)) + { + auto chunksize = i->size() - offset; + size_t newoffset = 0; + + if (chunksize > size) + { + newoffset = offset + size; + chunksize = size; + } + + if (buffer) + memcpy (buffer + ptr, i->data() + offset, chunksize); + + ptr += chunksize; + size -= chunksize; + offset = newoffset; + + i++; + } + + return ptr; +} + +template +size_t Pipe::peek (char *buffer, size_t size) const +{ + auto l = lock_of(mutex); + + return peek_no_lock(buffer, size); +} + +template +size_t Pipe::read_no_lock (char *buffer, size_t requestedSize) +{ + auto size = requestedSize; + size_t ptr = 0; + + auto &buffers = pipe; + + while (!buffers.empty() && (size > 0)) + { + auto chunksize = buffers.front().size() - offset; + size_t newoffset = 0; + + if (chunksize > size) + { + newoffset = offset + size; + chunksize = size; + } + + if (buffer) + { + debug_assert(ptr + chunksize <= requestedSize); + memcpy (buffer + ptr, buffers.front().data() + offset, chunksize); + } + + ptr += chunksize; + size -= chunksize; + offset = newoffset; + + if (offset == 0) + { + size_ -= buffers.front().size(); + buffers.pop_front(); + } + } + + sizeReadRequested += requestedSize; + sizeRead += ptr; + + return ptr; +} ; + +template +void Pipe::clearSizeRead () +{ + sizeReadRequested = sizeRead = 0; +} + +template +size_t Pipe::read (char *buffer, size_t requestedSize) +{ + auto l = lock_of(mutex); + return read_no_lock(buffer, requestedSize); +} + +template +size_t Pipe::write (const char *buffer, size_t size) +{ + if (size == 0) + return size; + + write(Packet(buffer, buffer+size)); + return size; +} + +template +size_t Pipe::skip_no_lock(size_t size) +{ + return read_no_lock(nullptr, size); +} + +template +size_t Pipe::skip(size_t size) +{ + auto l = lock_of(mutex); + + return skip_no_lock(size); +} + +template +size_t Pipe::size () const +{ + auto l = lock_of(mutex); + return size_ - offset; +} + +template +void Pipe::clear () +{ + auto l = lock_of(mutex); + + pipe.clear(); + size_ = offset = 0; + flush_ = false; +} + +template +size_t Pipe::resize (size_t maximumSize) +{ + auto l = lock_of(mutex); + if (size_ > maximumSize) + { + skip_no_lock(size_ - maximumSize); + } + + return size_; +} + +template +const Packet *Pipe::peek() const +{ + auto l = lock_of(mutex); + + if (pipe.empty()) + return nullptr; + + return &pipe.front(); +} + +template +bool Pipe::eos() const +{ + return !open && empty(); +} + +template +bool Pipe::finishing() const +{ + return !open && !empty(); +} + +template +bool Pipe::flushing() const +{ + return flush_ && !empty(); +} + +template +bool Pipe::flushed() +{ + if (!flush_) + return false; + + if (!empty()) + return false; + + flush_ = false; + return true; +} + +} // namespace +} // namespace + diff --git a/tjp/core/pipe/_tests/Pipe.cpp b/tjp/core/pipe/_tests/Pipe.cpp new file mode 100755 index 0000000..4139734 --- /dev/null +++ b/tjp/core/pipe/_tests/Pipe.cpp @@ -0,0 +1,83 @@ +// TJP COPYRIGHT HEADER + +#include + +#include + +#include + +namespace tjp { +namespace core { +namespace test { + +SCENARIO("audio pipe") +{ + typedef std::vector Packet; + + Packet a({'a', 'a', 'a', 'a', 'a'}); + Packet b({'b', 'b', 'b' }); + + GIVEN("a pipe") + { + Pipe p; + REQUIRE(p.empty()); + + WHEN("write 1 packet") + { + p.write(core::copy_of(a)); + + REQUIRE(p.size() == a.size()); + REQUIRE(!p.empty()); + } + + WHEN("write 2 packets") + { + p.write(core::copy_of(a)); + p.write(core::copy_of(b)); + + REQUIRE(p.size() == a.size() + b.size()); + } + + WHEN("write 2 packets and read 1") + { + p.write(core::copy_of(a)); + p.write(core::copy_of(b)); + p.read(); + + REQUIRE(p.size() == b.size()); + } + + WHEN("write 2 packets and read 2") + { + p.write(core::copy_of(a)); + p.write(core::copy_of(b)); + p.read(); + p.read(); + + REQUIRE(p.size() == 0); + REQUIRE(p.empty()); + } + + WHEN("write 2 packets and read some") + { + p.write(core::copy_of(a)); + p.write(core::copy_of(b)); + Packet u, v; + u.resize(7); + + v = a; + v.insert(v.end(), b.begin(), b.end()); + v.resize(u.size()); + + p.read(u.data(), u.size()); + + REQUIRE(u == v); + REQUIRE(p.size() == (a.size() + b.size() - u.size())); + } + } +} + +} // namespace +} // namespace +} // namespace + diff --git a/tjp/core/pipe/obsolete/Pipe.cpp b/tjp/core/pipe/obsolete/Pipe.cpp new file mode 100755 index 0000000..1285c67 --- /dev/null +++ b/tjp/core/pipe/obsolete/Pipe.cpp @@ -0,0 +1,173 @@ +// TJP COPYRIGHT HEADER + +#include "Pipe.h" + +namespace tjp { +namespace core { + +Pipe::~Pipe () +{ +} + +PipeStream::PipeStream () +{ + writing = reading = true; + offset = 0; + size = 0; +} + +PipeStream::~PipeStream () +{ + while (!buffers.empty()) + { + delete[] buffers.front().first; + buffers.pop_front(); + } +} + +bool PipeStream::isReading () const +{ + return reading; +} + +bool PipeStream::isWriting () const +{ + return writing; +} + +void PipeStream::write (const char *buffer, size_t size) +{ + char *copy = new char[size]; + memcpy (copy, buffer, size); + + buffers.push_back (std::pair(copy, size)); + this->size += size; +} + +void PipeStream::unread (const char *buffer, size_t size) +{ + auto amountToWriteToExisting = std::min(offset, size); + memcpy (buffers.front().first + (offset - amountToWriteToExisting), buffer + (size - amountToWriteToExisting), amountToWriteToExisting); + + + auto amountToNewBuffer = size - amountToWriteToExisting; + char *copy = new char[amountToNewBuffer]; + memcpy (copy, buffer, amountToNewBuffer); + + buffers.push_front (std::pair(copy, amountToNewBuffer)); + this->size += size; +} + +char *PipeStream::write (size_t size) +{ + char *copy = new char[size]; + buffers.push_back (std::pair(copy, size)); + this->size += size; + + return copy; +} + +void PipeStream::closeWrite () +{ + writing = false; + + if (!reading) + delete this; +} + +void PipeStream::closeRead () +{ + reading = false; + + if (!writing) + delete this; +} + +Pipe::size_t PipeStream::peek (char *buffer, size_t size) const +{ + size_t ptr = 0; + size_t offset = this->offset; + + std::list< std::pair >::const_iterator i=buffers.begin(); + while (i!=buffers.end() && (size > 0)) + { + auto chunksize = i->second - offset; + size_t newoffset = 0; + + if (chunksize > size) + { + newoffset = offset + size; + chunksize = size; + } + + if (buffer) + memcpy (buffer + ptr, i->first + offset, chunksize); + + ptr += chunksize; + size -= chunksize; + offset = newoffset; + + i++; + } + + return ptr; +} + +PipeStream::size_t PipeStream::read (char *buffer, size_t size) +{ + size_t ptr = 0; + + while (!buffers.empty() && (size > 0)) + { + auto chunksize = buffers.front().second - offset; + size_t newoffset = 0; + + if (chunksize > size) + { + newoffset = offset + size; + chunksize = size; + } + + if (buffer) + memcpy (buffer + ptr, buffers.front().first + offset, chunksize); + + ptr += chunksize; + size -= chunksize; + offset = newoffset; + + if (offset == 0) + { + delete[] buffers.front().first; + buffers.pop_front(); + } + } + + this->size -= ptr; + + return ptr; +} ; + +PipeStream::size_t PipeStream::getSize () const +{ + return size; +} + +bool PipeStream::eos () const +{ + return getSize() == 0 && !isWriting(); +} + +void PipeStream::clear () +{ + while (!buffers.empty()) + { + delete[] buffers.front().first; + buffers.pop_front(); + } + + size = 0; + offset = 0; +} + +} // namespace +} // namespace diff --git a/tjp/core/pipe/obsolete/Pipe.h b/tjp/core/pipe/obsolete/Pipe.h new file mode 100755 index 0000000..1c5c75d --- /dev/null +++ b/tjp/core/pipe/obsolete/Pipe.h @@ -0,0 +1,102 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp { +namespace core { + +class Pipe +{ + public: + typedef ::size_t size_t; + + virtual ~Pipe (); + + virtual bool isReading () const = 0; + virtual bool isWriting () const = 0; + + virtual void write (const char *buffer, size_t size_t) = 0; + virtual void closeWrite () = 0; + + virtual size_t peek (char *buffer, size_t size_t) const = 0; + virtual size_t read (char *buffer, size_t size_t) = 0; + + int readChar () + { + unsigned char c; + auto size_t = read((char *)&c, 1); + if (size_t != 1) + return -1; + + return c; + } + + virtual void closeRead () = 0; + + virtual size_t getSize () const = 0; + virtual bool eos () const = 0; + + virtual void clear () = 0; +} ; + +class PipeStream : public Pipe +{ + protected: + bool reading, writing; + std::list< std::pair > buffers; + size_t offset, size; + + public: + PipeStream (); + virtual ~PipeStream (); + + virtual bool isReading () const; + virtual bool isWriting () const; + + virtual void write (const char *buffer, size_t size_t); + virtual char *write(size_t size_t); + virtual void closeWrite (); + + virtual size_t peek (char *buffer, size_t size_t) const; + + void unread (const char *buffer, size_t size_t); + virtual size_t read (char *buffer, size_t size_t); + virtual void closeRead (); + + virtual size_t getSize () const; + virtual bool eos () const; + + virtual void clear (); +} ; + +class PacketStream +{ + protected: + bool reading, writing; + std::list< std::vector > buffers; + + public: + PacketStream (); + virtual ~PacketStream (); + + virtual void read (const char *buffer, size_t size_t); + virtual char *write(size_t size_t); + virtual void closeWrite (); + + virtual size_t peek (char *buffer, size_t size_t) const; + + void unread (const char *buffer, size_t size_t); + virtual size_t read (char *buffer, size_t size_t); + virtual void closeRead (); + + virtual size_t getSize () const; + virtual bool eos () const; + + virtual void clear (); +} ; + +} // namespace core +} // namespace tjp + diff --git a/tjp/core/profile/PROFILE_SCOPE.cpp b/tjp/core/profile/PROFILE_SCOPE.cpp new file mode 100644 index 0000000..e657c9d --- /dev/null +++ b/tjp/core/profile/PROFILE_SCOPE.cpp @@ -0,0 +1,150 @@ +// TJP COPYRIGHT HEADER + +#include "PROFILE_SCOPE.h" + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include + +namespace tjp { +namespace core { +namespace profile_scope { + +void ProfileScopeManager::append (ProfileToken *token) +{ + auto lock = lock_of(mutex); + tokens.push_back(token); +} + + +std::ostream &ProfileScopeManager::print (std::ostream &s) +{ + auto l = lock_of(mutex); + return print_no_lock(s); +} + +std::ostream &ProfileScopeManager::print_no_lock (std::ostream &s) +{ + Timepoint now = Clock::now(); + Timepoint then = now - std::chrono::milliseconds(1000); + + s << std::setprecision(10) << std::fixed << std::showpoint; + + struct TokenStats { + ProfileToken *token; + Stats stats; + } ; + std::vector sortedStats; + + for (auto &token : tokens) + { + Stats stats = token->getStats(then); + if (stats.count > 0) + { + sortedStats.push_back({ token, stats }); + } + } + + + std::sort(sortedStats.begin(), sortedStats.end(), [](auto &l, auto &r) { return l.stats.totalDuration < r.stats.totalDuration; }); + + s << "\nPROFILE ---------------------"; + + for (auto &[token, stats] : sortedStats) + { + s << "\n"; + s + << "PROFILE\t" << token->name + << ":\t" << stats.count + << "\t" << std::chrono::duration_cast(stats.averageDuration).count() / (float)1e+9 + << "s\t" << std::chrono::duration_cast(stats.totalDuration).count() / (float)1e+9 + << "s"; + } + + return s; +} + +std::ostream &ProfileScopeManager::print_clear (std::ostream &s) +{ + auto l = lock_of(mutex); + auto &r = print_no_lock(s); + + return r; +} + +void profile_log () +{ + std::ostringstream s; + ProfileScopeManagerSingleton::getInstance()->print(s); + + sLogRelease("profile", s.str()); +} + +// ------------------------------------------------------ + +ProfileToken::ProfileToken(const char *prettyFunction) +{ + name = prettyFunction; + ProfileScopeManagerSingleton::getInstance()->append(this); +} + +void ProfileToken::onRecord(ProfileRecord &&record) +{ + auto l = lock_of(mutex); + records.emplace_back(std::move(record)); +} + +Stats ProfileToken::getStats (const Timepoint &then) +{ + auto l = lock_of(mutex); + Duration duration = Duration::zero(); + size_t count = 0; + + for (auto &record : records) + { + if (record.end > then) + { + duration += record.duration; + count++; + } + } + + if (count) + { + return Stats { + .count = count, + .totalDuration = duration, + .averageDuration = duration / count + }; + } + + return Stats { .count = 0 }; +} + +static bool profile_enabled_ = false; + +void profile_enable() +{ + xLogActivateStory("profile"); + profile_enabled_ = true; +} + +bool profile_enabled() +{ + return profile_enabled_; +} + +} // namespace +} // namespace +} // namespace + diff --git a/tjp/core/profile/PROFILE_SCOPE.h b/tjp/core/profile/PROFILE_SCOPE.h new file mode 100644 index 0000000..8961e7c --- /dev/null +++ b/tjp/core/profile/PROFILE_SCOPE.h @@ -0,0 +1,147 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace tjp { +namespace core { +namespace profile_scope { + +typedef std::chrono::high_resolution_clock Clock; +typedef std::chrono::time_point Timepoint; +typedef Clock::duration Duration; + +struct ProfileToken; + +struct ProfileScopeManager +{ + Mutex mutex; + + std::vector tokens; + +public: + void append (ProfileToken *token); + + std::ostream &print (std::ostream &s); + std::ostream &print_no_lock (std::ostream &s); + std::ostream &print_clear (std::ostream &s); +} ; + + +template +struct ProfileScopeManagerSingleton_ +{ + static ProfileScopeManager *singleton; + + static ProfileScopeManager *getInstance () + { + if (!singleton) + singleton = new ProfileScopeManager(); + + return singleton; + } +} ; + +template +ProfileScopeManager *ProfileScopeManagerSingleton_::singleton = nullptr; + +using ProfileScopeManagerSingleton = ProfileScopeManagerSingleton_; + +struct ProfileRecord +{ + Timepoint begin, end; + Duration duration; + const void *other; +} ; + +struct Stats +{ + size_t count; + Duration totalDuration, averageDuration; +} ; + +struct ProfileToken +{ + std::string name; + + Mutex mutex; + std::vector records; + + ProfileToken(const char *prettyFunction); + + void onRecord(ProfileRecord &&record); + Stats getStats (const Timepoint &then); +} ; + +void profile_enable(); +bool profile_enabled(); + +struct ProfileScope +{ + bool enabled; + ProfileToken *token; + ProfileRecord record; + + ProfileScope(ProfileToken *token_, const void *other = nullptr) : + token(token_), + enabled(profile_enabled()) + { + if (enabled) + { + record.other = other; + record.begin = Clock::now(); + } + } + + ~ProfileScope() + { + if (enabled) + { + record.end = Clock::now(); + record.duration = record.end - record.begin; + token->onRecord(std::move(record)); + } + } +} ; + +void profile_log(); + + + +} // namespace +} // namespace +} // namespace + +#ifdef _DEBUG + //#define USE_PROFILE +#endif + +#ifdef USE_PROFILE + +#define PROFILE_SCOPE(...) \ + static tjp::core::profile_scope::ProfileToken __profile_token__(__PRETTY_FUNCTION__); \ + tjp::core::profile_scope::ProfileScope __profile_scope__(&__profile_token__, __VA_ARGS__) + +#define PROFILE_FUNCTION(...) PROFILE_SCOPE(__VA_ARGS__) + +#define PROFILE_LOG() tjp::core::profile_scope::profile_log() + +#define PROFILE_ENABLE() tjp::core::profile_scope::profile_enable() + +#else + +#define PROFILE_SCOPE(...) +#define PROFILE_FUNCTION(...) +#define PROFILE_LOG() +#define PROFILE_ENABLE() + +#endif diff --git a/tjp/core/profile/PROFILE_SIGNAL.cpp b/tjp/core/profile/PROFILE_SIGNAL.cpp new file mode 100644 index 0000000..5e71f7e --- /dev/null +++ b/tjp/core/profile/PROFILE_SIGNAL.cpp @@ -0,0 +1,106 @@ +// TJP COPYRIGHT HEADER + +#define TESTING +#include "PROFILE_SIGNAL.h" + +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include + + +namespace tjp { +namespace core { + +void ProfileSignal::clear () +{ + auto l = lock_of(mutex); + clear_no_lock(); +} + +void ProfileSignal::clear_no_lock () +{ + timers.clear(); + times.clear(); + cleared = true; +} + +std::ostream &ProfileSignal::print (std::ostream &s) +{ + auto l = lock_of(mutex); + return print_no_lock(s); +} + +std::ostream &ProfileSignal::print_no_lock (std::ostream &s) +{ + for (auto &[e, ts] : times) + { + s << "\t" << e; + for (auto &t : ts) + { + s << "\t" << t; + } + + s << std::endl; + } + + return s; +} + +std::ostream &ProfileSignal::print_clear (std::ostream &s) +{ + auto l = lock_of(mutex); + auto &r = print_no_lock(s); + clear_no_lock(); + + return r; +} + + +void ProfileSignal::begin (Event &&e) +{ + auto l = lock_of(mutex); + + auto time = timeSource.get(); + auto i = timers.find(e); + if (i != timers.end()) + return; + + timers[e] = time; + + signal.notify_all(); +} + +void ProfileSignal::end (Event &&e) +{ + auto l = lock_of(mutex); + auto i = timers.find(e); + if (i != timers.end()) + { + auto then = i->second; + auto now = timeSource.get(); + auto dt = now-then; + times[e].push_back(dt); + + std::ostringstream s; + sLogDebug("profile_signal", e << "\t" << dt << ((dt > time::Deciseconds(1)) ? " dt-large" : "")); + std::cout << s.str(); + + timers.erase(i); + } + + signal.notify_all(); +} + + +} // namespace +} // namespace + diff --git a/tjp/core/profile/PROFILE_SIGNAL.h b/tjp/core/profile/PROFILE_SIGNAL.h new file mode 100644 index 0000000..463e8bf --- /dev/null +++ b/tjp/core/profile/PROFILE_SIGNAL.h @@ -0,0 +1,144 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#ifdef TESTING + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace tjp { +namespace core { + +struct ProfileSignal +{ + typedef std::string Event; + + core::Event::Mutex mutex; + core::Event signal; + bool cleared = false; + + typedef time::Time Time; + typedef time::Duration Duration; + time::SourceStandard timeSource; + + std::map timers; + std::map> times; + + typedef std::list Events; + Events events; + +public: + void append (Event &&e); + + const std::string STAR = "*"; + + void begin(Event &&e); + void end(Event &&e); + + void clear(); + void clear_no_lock(); + + std::ostream &print (std::ostream &s); + std::ostream &print_no_lock (std::ostream &s); + std::ostream &print_clear (std::ostream &s); +} ; + + +template +struct ProfileSignalSingleton +{ + static ProfileSignal *singleton; + + static ProfileSignal *getInstance () + { + if (!singleton) + singleton = new ProfileSignal(); + + return singleton; + } +} ; + +template +ProfileSignal *ProfileSignalSingleton::singleton = nullptr; + + +// ------- + + +template +void profile_test_event_string_args(S &s, T &t) +{ + s << t; +} + +template +void profile_test_event_string_args(S &s, T &&t, X&&...x) +{ + s << t << ","; + profile_test_event_string_args(s, x...); +} + +template +std::string profile_test_event_string(T&& ...t) +{ + std::ostringstream s; + profile_test_event_string_args(s, t...); + return s.str(); +} + + +template +void profile_time_clear() +{ + ProfileSignalSingleton::getInstance()->clear (); +} + +template +S &profile_time_print(S &stream) +{ + return ProfileSignalSingleton::getInstance()->print (stream); +} + +template +S &process_time_print_clear(S &stream) +{ + return ProfileSignalSingleton::getInstance()->print_clear (stream); +} + + +template +void profile_time_begin(T&&...t) +{ + ProfileSignalSingleton::getInstance()->begin (ProfileSignal::Event{ process_test_event_string(t...) }); +} + +template +void profile_time_end(T&&...t) +{ + return ProfileSignalSingleton::getInstance()->end(ProfileSignal::Event{ process_test_event_string(t...) }); +} + +} // namespace +} // namespace + +#define TEST_TIME_CLEAR() tjp::core::profile_time_clear() +#define TEST_TIME_PRINT(stream) tjp::core::profile_time_print(stream) +#define TEST_TIME_PRINT_CLEAR(stream) tjp::core::profile_time_print_clear(stream) +#define TEST_TIME_BEGIN(v,...) tjp::core::profile_time_begin(v,__VA_ARGS__) +#define TEST_TIME_END(v,...) tjp::core::profile_time_end(v,__VA_ARGS__) + +#else + +#define TEST_TIME_BEGIN(...) +#define TEST_TIME_END(...) + +#endif diff --git a/tjp/core/random/Random.cpp b/tjp/core/random/Random.cpp new file mode 100755 index 0000000..a83d1eb --- /dev/null +++ b/tjp/core/random/Random.cpp @@ -0,0 +1,18 @@ +// TJP COPYRIGHT HEADER + +#ifdef TJP_CORE_HEADER_ONLY + #pragma once +#endif + +#include + +#include "Random.h" + +namespace tjp::core { + +StandardRandomAdapter StandardRandom; + +RealRandom StandardRealRandom; + +} // namespace + diff --git a/tjp/core/random/Random.h b/tjp/core/random/Random.h new file mode 100755 index 0000000..6d61000 --- /dev/null +++ b/tjp/core/random/Random.h @@ -0,0 +1,236 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include +#include + +#define HAS_CPP_11 +#ifdef HAS_CPP_11 + +#include + +#else + +#include +namespace std { + +class default_random_engine { + +public: + char operator ()() { + return random(); + } + + void seed(int seed=0) {} +} ; + +typedef default_random_engine random_device; + +} + +#endif + +namespace tjp { +namespace core { + +namespace random_detail { + +template +T abs_(T t) { return std::abs(t); } + +template<> +inline +s128 abs_(s128 t) { return t > 0 ? t : -t; } + +template<> +inline +constexpr u64 abs_(u64 t) { return t; } + +template<> +inline +constexpr u32 abs_(u32 t) { return t; } + +template<> +inline +constexpr u16 abs_(u16 t) { return t; } + +template<> +inline +constexpr u8 abs_(u8 t) { return t; } + +#if defined(SYS_APPLE) || defined(SYS_IOS) +template<> +inline +constexpr size_t abs_(size_t t) { return t; } +#endif + + +} // + +template +class Random { + +public: + using Generator = G; + Generator generator; + + template + T next () { + T t; + char *begin = (char *)&t; + char *end = begin + sizeof(T); + + fill(begin, end); + return t; + } + +public: + Random() {}; + + Random(Generator &&generator_) : + generator(generator_) + { + } + + auto generate() + { + return generator(); + } + + char nextChar () + { + return next(); + } + + template + T nextIndex (T r) + { + return nextInt() % r; + } + + template + T nextInt () + { + return random_detail::abs_(next()); + } + + template + T nextInt (T l, T r) + { + auto d = r - l + 1; + return (random_detail::abs_(next()) % d) + l; + } + + template + T nextReal (T min=0, T max=1) + { + auto d = max - min; + + constexpr auto random_max = numeric_limits_ext::random_max; + auto l = T(nextInt(0, random_max)); + auto r = T(random_max); + auto v = l/r * d + min; + + return v; + } + + template + T nextPositiveBits (size_t numBits) + { + auto v = next(); + auto zeroBits = sizeof(v) * 8 - numBits; + v = (v << zeroBits) >> zeroBits; + return random_detail::abs_(v); + } + + template + T nextBits (size_t numBits) + { + auto v = next(); + auto zeroBits = sizeof(v) * 8 - numBits; + v = (v << zeroBits) >> zeroBits; + return v; + } + + void fill (char *begin, char *end) + { + if (begin == end) + return; + + auto *i = begin; + while (true) + { + auto v = generate(); + for (auto j=0; j> 8; + + if (i == end) + return; + } + } + } +}; + +template +class RandomGeneratorSeeded +{ +public: + using Generator = T; + Generator random; + +public: + RandomGeneratorSeeded () : + random(std::random_device()()) + { + } + + RandomGeneratorSeeded(int seed) : + random(seed) + { + + } + + auto operator()() + { + return random(); + } +} ; + +using ReliableRandom = Random>; +using RealRandom = ReliableRandom; + +using FastRandom = Random>; + +using StandardRandomAdapter = FastRandom; + +extern StandardRandomAdapter StandardRandom; +extern RealRandom StandardRealRandom; + +#if 1 + template + void random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last) + { + std::random_device rd; + std::mt19937 g(rd()); + + std::shuffle(__first, __last, g); + } +#else + template + void random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last) + { + std::random_shuffle(__first, __last); + } +#endif + +} // namespace +} // namespace + +#ifdef TJP_CORE_HEADER_ONLY_SINGLE_FILE + #include "Random.cpp" +#endif diff --git a/tjp/core/random/bell_curve.hpp b/tjp/core/random/bell_curve.hpp new file mode 100755 index 0000000..0565930 --- /dev/null +++ b/tjp/core/random/bell_curve.hpp @@ -0,0 +1,46 @@ +// TJP COPYRIGHT HEADER + + +#pragma once + +#include +#include "Random.h" +#include + +namespace tjp::core::bell_curve { + +template +T sigmoid(T x) { + constexpr T one(1); + return one / (one + std::exp(-x)); +} + +template +T random(T z, T k) +{ + return sigmoid(k * z); +} + +template +T random(T z) +{ + return random(z, T(3)); +} + +template +T random() +{ + std::normal_distribution<> dist(0.0, 1.0); + auto z = (T)dist(StandardRandom.generator.random); + return random(z); +} + +size_t random_index(size_t s) +{ + auto r = random(); + auto i = size_t(r * s); + return math::clamp(i, 0, s-1); +} + +} // namespace + diff --git a/tjp/core/random/file_name.hpp b/tjp/core/random/file_name.hpp new file mode 100755 index 0000000..7870ace --- /dev/null +++ b/tjp/core/random/file_name.hpp @@ -0,0 +1,18 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Random.h" +#include +#include + +namespace tjp::core::random { + +inline +String file_name () +{ + return to_string(StandardRandom.next()); +} + +} // namespace + diff --git a/tjp/core/random/temporary_path.hpp b/tjp/core/random/temporary_path.hpp new file mode 100755 index 0000000..f434afb --- /dev/null +++ b/tjp/core/random/temporary_path.hpp @@ -0,0 +1,18 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Random.h" +#include +#include + +namespace tjp::core::random { + +inline +String temporary_path (const StringView &prefix) +{ + return ToString() << "/tmp/" << prefix << '-' << to_string(StandardRandom.next()) << '/'; +} + +} // namespace + diff --git a/tjp/core/resource_version/GlobalResourceVersion+IO.cpp b/tjp/core/resource_version/GlobalResourceVersion+IO.cpp new file mode 100644 index 0000000..80210c7 --- /dev/null +++ b/tjp/core/resource_version/GlobalResourceVersion+IO.cpp @@ -0,0 +1,16 @@ +// TJP COPYRIGHT HEADER + +#include "GlobalResourceVersion+IO.inl" +#include + +namespace tjp { +namespace core { + +std::ostream &operator <<(std::ostream &o, const GlobalResourceVersion &v) +{ + return o << v.value; +} + + +} // namespace +} // namespace diff --git a/tjp/core/resource_version/GlobalResourceVersion+IO.inl b/tjp/core/resource_version/GlobalResourceVersion+IO.inl new file mode 100644 index 0000000..ddb6a0e --- /dev/null +++ b/tjp/core/resource_version/GlobalResourceVersion+IO.inl @@ -0,0 +1,21 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "GlobalResourceVersion.hpp" +#include + +namespace tjp { +namespace core { + +template +inline void io_(IO &io, GlobalResourceVersion &v) +{ + io.any(v.value); +} + +std::ostream &operator <<(std::ostream &o, const GlobalResourceVersion &v); + + +} // namespace +} // namespace diff --git a/tjp/core/resource_version/GlobalResourceVersion.cpp b/tjp/core/resource_version/GlobalResourceVersion.cpp new file mode 100644 index 0000000..d48befd --- /dev/null +++ b/tjp/core/resource_version/GlobalResourceVersion.cpp @@ -0,0 +1,21 @@ +// TJP COPYRIGHT HEADER + +#include "GlobalResourceVersion.hpp" +#include "../random/Random.h" + +#include + +namespace tjp { +namespace core { + +const GlobalResourceVersion GlobalResourceVersion::Null; + +std::atomic GlobalResourceVersion::nextValue = 0; + +void GlobalResourceVersion::increment () +{ + value = ++nextValue; +} + +} // namespace +} // namespace diff --git a/tjp/core/resource_version/GlobalResourceVersion.h b/tjp/core/resource_version/GlobalResourceVersion.h new file mode 100644 index 0000000..e43de83 --- /dev/null +++ b/tjp/core/resource_version/GlobalResourceVersion.h @@ -0,0 +1,15 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +namespace tjp::core { + +struct GlobalResourceVersion; + +bool operator ==(const GlobalResourceVersion &lhs, const GlobalResourceVersion &rhs); +bool operator !=(const GlobalResourceVersion &lhs, const GlobalResourceVersion &rhs); +bool operator <(const GlobalResourceVersion &lhs, const GlobalResourceVersion &rhs); +bool operator >=(const GlobalResourceVersion &lhs, const GlobalResourceVersion &rhs); +bool operator >(const GlobalResourceVersion &lhs, const GlobalResourceVersion &rhs); + +} // namespace diff --git a/tjp/core/resource_version/GlobalResourceVersion.hpp b/tjp/core/resource_version/GlobalResourceVersion.hpp new file mode 100644 index 0000000..0af76e3 --- /dev/null +++ b/tjp/core/resource_version/GlobalResourceVersion.hpp @@ -0,0 +1,162 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "GlobalResourceVersion.h" + +#include +#include +#include + +#include "../algorithm/for_each_argument.hpp" +#include + +namespace tjp::core { + +struct GlobalResourceVersion +{ + using Type = u64; + using Self = GlobalResourceVersion; + + static std::atomic nextValue; + static const Self Null; + + Type value = 0; + + void increment (); + + void clear() + { + value = 0; + } + + bool empty() const + { + return value == 0; + } + + Self &operator ++() + { + increment(); + return *this; + } + + // post increment + Self operator ++(int) + { + Self result { value }; + increment(); + return result; + } + + static Self next () + { + Self result; + ++result; + + return result; + } + + static bool shouldUpdate (Type &value, Type rhs) + { + return value < rhs; + } + + bool shouldUpdate (const Self &rhs) + { + return value < rhs.value; + } + + static bool update (Type &value, Type rhs) + { + auto updated = shouldUpdate(value, rhs); + if (updated) + value = rhs; + + return updated; + } + + template + auto as_tuple (Args ... args) + { + return std::tuple_cat(std::forward(args)...); + } + + template + auto updates (Args&& ... args) + { + auto result = std::make_tuple(shouldUpdate(args)...); +// for_each_argument(update, args...); + bool did[] = { update(args)... }; + (void)did; + + return result; + } + + template + auto updatesAny (Args&& ... args) + { + auto result = updates(std::forward(args)...); + + return tuple_has_value(result, true); + } + + bool update (Type rhs) + { + return update(value, rhs); + } + + bool update (const Self &rhs) + { + return update(rhs.value); + } + + bool changed (Type rhs) + { + return value != rhs; + } + + bool changed (const Self &rhs) + { + return changed(rhs.value); + } +} ; + +inline +bool operator ==(const GlobalResourceVersion &lhs, const GlobalResourceVersion &rhs) +{ + return lhs.value == rhs.value; +} + +inline +bool operator !=(const GlobalResourceVersion &lhs, const GlobalResourceVersion &rhs) +{ + return lhs.value != rhs.value; +} + +inline +bool operator <(const GlobalResourceVersion &lhs, const GlobalResourceVersion &rhs) +{ + return lhs.value < rhs.value; +} + +inline +bool operator >=(const GlobalResourceVersion &lhs, const GlobalResourceVersion &rhs) +{ + return lhs.value >= rhs.value; +} + +inline +bool operator <=(const GlobalResourceVersion &lhs, const GlobalResourceVersion &rhs) +{ + return lhs.value <= rhs.value; +} + +inline +bool operator >(const GlobalResourceVersion &lhs, const GlobalResourceVersion &rhs) +{ + return lhs.value > rhs.value; +} + + +} // namespace diff --git a/tjp/core/resource_version/LocalResourceVersion+IO.inl b/tjp/core/resource_version/LocalResourceVersion+IO.inl new file mode 100644 index 0000000..df38f16 --- /dev/null +++ b/tjp/core/resource_version/LocalResourceVersion+IO.inl @@ -0,0 +1,25 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "LocalResourceVersion.hpp" +#include + +namespace tjp { +namespace core { + +template +inline void io_(IO &io, LocalResourceVersion &v) +{ + io.any(v.value); +} + +inline +std::ostream &operator <<(std::ostream &o, const LocalResourceVersion &v) +{ + return o << v.value; +} + + +} // namespace +} // namespace diff --git a/tjp/core/resource_version/LocalResourceVersion.hpp b/tjp/core/resource_version/LocalResourceVersion.hpp new file mode 100644 index 0000000..db5ce08 --- /dev/null +++ b/tjp/core/resource_version/LocalResourceVersion.hpp @@ -0,0 +1,114 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +namespace tjp { +namespace core { + +struct LocalResourceVersion +{ + using Type = u64; + using Self = LocalResourceVersion; + + Type value = 0; + + void clear() + { + value = 0; + } + + bool empty() const + { + return value == 0; + } + + Self &operator ++() + { + increment(); + return *this; + } + + // post increment + Self operator ++(int) + { + Self result { value }; + increment(); + return result; + } + + void increment () + { + value++; + } + + static bool update (Type &value, Type rhs) + { + auto updated = value < rhs; + if (updated) + value = rhs; + + return updated; + } + + bool update (Type rhs) + { + return update(value, rhs); + } + + bool update (const Self &rhs) + { + return update(rhs.value); + } + + bool changed (Type rhs) + { + return value != rhs; + } + + bool changed (const Self &rhs) + { + return changed(rhs.value); + } +} ; + +inline +bool operator !=(const LocalResourceVersion &lhs, const LocalResourceVersion &rhs) +{ + return lhs.value != rhs.value; +} + +inline +bool operator ==(const LocalResourceVersion &lhs, const LocalResourceVersion &rhs) +{ + return lhs.value == rhs.value; +} + +inline +bool operator <(const LocalResourceVersion &lhs, const LocalResourceVersion &rhs) +{ + return lhs.value < rhs.value; +} + +inline +bool operator >(const LocalResourceVersion &lhs, const LocalResourceVersion &rhs) +{ + return lhs.value > rhs.value; +} + +inline +bool operator <=(const LocalResourceVersion &lhs, const LocalResourceVersion &rhs) +{ + return lhs.value <= rhs.value; +} + +inline +bool operator >=(const LocalResourceVersion &lhs, const LocalResourceVersion &rhs) +{ + return lhs.value >= rhs.value; +} + +} // namespace +} // namespace diff --git a/tjp/core/resource_version/_tests/GlobalResource.cpp b/tjp/core/resource_version/_tests/GlobalResource.cpp new file mode 100755 index 0000000..5392a23 --- /dev/null +++ b/tjp/core/resource_version/_tests/GlobalResource.cpp @@ -0,0 +1,71 @@ +// TJP COPYRIGHT HEADER + +#include + +#include + +namespace tjp { +namespace core { +namespace test { + +SCENARIO("core::resource_version::GlobalResourceVersion") +{ + GIVEN("a resource") + { + GlobalResourceVersion a; + + auto e = a; + REQUIRE(a.empty()); + + WHEN("pre increment") + { + auto b = ++a; + REQUIRE(b == a); + REQUIRE(b > e); + REQUIRE(a > e); + REQUIRE(!a.empty()); + REQUIRE(!b.empty()); + + auto c = ++a; + REQUIRE(c == a); + REQUIRE(c > b); + REQUIRE(b > e); + REQUIRE(!c.empty()); + REQUIRE(!a.empty()); + REQUIRE(!b.empty()); + } + + WHEN("post increment") + { + auto b = a++; + REQUIRE(b != a); + REQUIRE(a > b); + REQUIRE(a > e); + REQUIRE(b == e); + REQUIRE(!a.empty()); + } + } + + GIVEN("a multiple resources") + { + GlobalResourceVersion x, a, b, c, d; + + a++; + b++; + x++; + c++; + d++; + + auto [ua, ub, uc, ud] = x.updates(a, b, c, d); + REQUIRE(!ua); + REQUIRE(!ub); + REQUIRE(uc); + REQUIRE(ud); + } + +} + +} // namespace +} // namespace +} // namespace + diff --git a/tjp/core/runnable/Runnable.h b/tjp/core/runnable/Runnable.h new file mode 100644 index 0000000..8d71a9b --- /dev/null +++ b/tjp/core/runnable/Runnable.h @@ -0,0 +1,9 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +namespace tjp::core { + +struct Endable; + +} // namespace diff --git a/tjp/core/runnable/Runnable.hpp b/tjp/core/runnable/Runnable.hpp new file mode 100644 index 0000000..e30976f --- /dev/null +++ b/tjp/core/runnable/Runnable.hpp @@ -0,0 +1,78 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Runnable.h" + +#include +#include +#include + +#include + +namespace tjp::core { + +struct Endable; + +struct Runnable : StrongThis +{ + using End = Function; + + Runnable(End &&end) : + end_(std::move(end)) + {} + + Function end_; + + bool end = false; + Promise ended; + + Endable getEndable(); +} ; + +struct Endable +{ + StrongPtr runnable; + bool endOnDestruct_ = false; + + ~Endable() + { + if (endOnDestruct_) + { + end(); + get_future().wait(); + } + } + + void endOnDestruct() + { + endOnDestruct_ = true; + } + + void end() + { + runnable->end = true; + + if (runnable->end_) + { + runnable->end_(*runnable); + runnable->end_ = {}; + } + } + + Future get_future() + { + return runnable->ended.get_future(); + } +} ; + +inline +Endable Runnable::getEndable() +{ + return Endable { + strong_this(this) + }; +} + + +} // namespace diff --git a/tjp/core/singleton/Singleton.cpp b/tjp/core/singleton/Singleton.cpp new file mode 100755 index 0000000..4020157 --- /dev/null +++ b/tjp/core/singleton/Singleton.cpp @@ -0,0 +1,43 @@ +// TJP COPYRIGHT HEADER + +/** + * legal header + * + * ============================================================================ + * TJP 10/11/01 Got rid of the constructed singleton, it was essentially + * a pointer! + * + * @author Timothy Prepscius + * @created 3/../01 + */ + +#include "Singleton.hpp" + +namespace timprepscius { +namespace core { + +Singletons::Singletons_ &Singletons::singletons() +{ + static Singletons_ *v = new Singletons_(); + return *v; +} + +void Singletons::clearAll () +{ + for (auto *singleton : singletons()) + singleton->clear(); +} + +Singleton_::Singleton_ () +{ + Singletons::singletons().insert(this); +} + +Singleton_::~Singleton_ () +{ + Singletons::singletons().erase(this); +} + +} // namespace +} // namespace + diff --git a/tjp/core/singleton/Singleton.h b/tjp/core/singleton/Singleton.h new file mode 100755 index 0000000..3362800 --- /dev/null +++ b/tjp/core/singleton/Singleton.h @@ -0,0 +1,36 @@ +// TJP COPYRIGHT HEADER + +/** + * legal header + * + * ============================================================================ + * TJP 10/11/01 Got rid of the constructed singleton, it was essentially + * a pointer! + * + * @author Timothy Prepscius + * @created 3/../01 + */ + +#pragma once + +namespace timprepscius { +namespace core { + +/** + * a Singleton container, whose contained instance is constructed on its + * first request + * + * T defines the class pointer type + * X defines the allocation class type + */ +class Singletons; + + +class Singleton_; + +template +class Singleton; + +} // namespace +} // namespace + diff --git a/tjp/core/singleton/Singleton.hpp b/tjp/core/singleton/Singleton.hpp new file mode 100755 index 0000000..0122fac --- /dev/null +++ b/tjp/core/singleton/Singleton.hpp @@ -0,0 +1,125 @@ +// TJP COPYRIGHT HEADER + +/** + * legal header + * + * ============================================================================ + * TJP 10/11/01 Got rid of the constructed singleton, it was essentially + * a pointer! + * + * @author Timothy Prepscius + * @created 3/../01 + */ + +#pragma once + +#include "Singleton.h" + +#include "../containers/FilledArray.hpp" + +namespace timprepscius { +namespace core { + +/** + * a Singleton container, whose contained instance is constructed on its + * first request + * + * T defines the class pointer type + * X defines the allocation class type + */ +class Singletons +{ +protected: + friend class Singleton_; + typedef FilledArray Singletons_; + static Singletons_ &singletons(); + +public: + static void clearAll (); +} ; + +class Singleton_ +{ +protected: + friend class Singletons; + virtual void clear () {} + +public: + Singleton_ (); + virtual ~Singleton_ (); +} ; + +template +class Singleton : Singleton_ +{ + private: + T *instance; + + void clear () override + { + deleteInstance(); + } + + public: + Singleton() + { + instance = nullptr; + } + + virtual ~Singleton() + { + deleteInstance(); + } + + void setInstance (T *instance) + { + if (this->instance) + delete instance; + + this->instance = instance; + } + + void verifyInstance() + { + if (instance==nullptr) + { + instance = new X(); + } + } + + void deleteInstance() + { + delete instance; + instance = nullptr; + } + + T *p() + { + verifyInstance(); + return instance; + } + + /********************************************************************** + * operator overloads to simplify use + **********************************************************************/ + + template + operator U *() + { + return p(); + } + + T *operator ->() + { + return p(); + } + + T &operator *() + { + return *p(); + } +} ; + +} // namespace +} // namespace + diff --git a/tjp/core/singleton/ThreadSafeSingleton.h b/tjp/core/singleton/ThreadSafeSingleton.h new file mode 100755 index 0000000..7a7571b --- /dev/null +++ b/tjp/core/singleton/ThreadSafeSingleton.h @@ -0,0 +1,27 @@ +// TJP COPYRIGHT HEADER + +/** + * legal header + * + * ============================================================================ + * TJP 10/11/01 Got rid of the constructed singleton, it was essentially + * a pointer! + * + * @author Timothy Prepscius + * @created 3/../01 + */ + +#pragma once + +namespace timprepscius { +namespace core { + +template +class ThreadSafeSingletonNoConstruct; + +template +class ThreadSafeSingleton; + +} // namespace utilities +} // namespace + diff --git a/tjp/core/singleton/ThreadSafeSingleton.hpp b/tjp/core/singleton/ThreadSafeSingleton.hpp new file mode 100755 index 0000000..e5acf96 --- /dev/null +++ b/tjp/core/singleton/ThreadSafeSingleton.hpp @@ -0,0 +1,112 @@ +// TJP COPYRIGHT HEADER + +/** + * legal header + * + * ============================================================================ + * TJP 10/11/01 Got rid of the constructed singleton, it was essentially + * a pointer! + * + * @author Timothy Prepscius + * @created 3/../01 + */ + +#pragma once + +#include "ThreadSafeSingleton.h" +#include "../threads/Monitor.h" + +namespace timprepscius { +namespace core { + +/** + * a Singleton container, whose contained instance is constructed on its + * first request + * + * T defines the class pointer type + * X defines the allocation class type + */ + +template +class ThreadSafeSingletonNoConstruct : public Monitor +{ + public: + typedef Monitor Super; + typedef typename Super::Writer Accessor; + + protected: + // only called by reader/writer after lock + virtual T *access() const + { + return this->object; + } + + public: + ThreadSafeSingletonNoConstruct () : + Super(NULL) + { + } + + virtual ~ThreadSafeSingletonNoConstruct() + { + deleteInstance(); + } + + virtual void setInstance (T *instance) + { + Accessor w(*this); + { + this->object = instance; + } + } + + void deleteInstance() + { + Accessor w(*this); + + if (this->object) + delete this->object; + + this->object = NULL; + } +} ; + +template +class ThreadSafeSingleton : public ThreadSafeSingletonNoConstruct +{ + public: + typedef ThreadSafeSingletonNoConstruct Super; + typedef typename Super::Writer Accessor; + + protected: + // only called by reader/writer after lock + virtual T *access() const + { + if (!this->object) + { + const_cast *>(this)->setInstance(new T()); + } + + return this->object; + } + + public: + virtual ~ThreadSafeSingleton() + { + Super::deleteInstance(); + } + + virtual void setInstance (T *instance) + { + Accessor a(*this); + + if (this->object) + delete this->object; + + this->object = instance; + } +} ; + +} // namespace utilities +} // namespace + diff --git a/tjp/core/singleton/ThreadSpecificSingleton.h b/tjp/core/singleton/ThreadSpecificSingleton.h new file mode 100755 index 0000000..29c4575 --- /dev/null +++ b/tjp/core/singleton/ThreadSpecificSingleton.h @@ -0,0 +1,41 @@ +// TJP COPYRIGHT HEADER + +/** + * legal header + * + * ============================================================================ + * TJP 10/11/01 Got rid of the constructed singleton, it was essentially + * a pointer! + * + * @author Timothy Prepscius + * @created 3/../01 + */ + +#pragma once + +namespace timprepscius { +namespace core { + +/** + * a Singleton container, whose contained instance is constructed on its + * first request + * + * T defines the class pointer type + * X defines the allocation class type + */ +template +class ThreadSpecificSingletonNoConstruct; + +/** + * a Singleton container, whose contained instance is constructed on its + * first request + * + * T defines the class pointer type + * X defines the allocation class type + */ +template +class ThreadSpecificSingleton; + +} // namespace utilities +} // namespace + diff --git a/tjp/core/singleton/ThreadSpecificSingleton.hpp b/tjp/core/singleton/ThreadSpecificSingleton.hpp new file mode 100755 index 0000000..e1286e3 --- /dev/null +++ b/tjp/core/singleton/ThreadSpecificSingleton.hpp @@ -0,0 +1,132 @@ +// TJP COPYRIGHT HEADER + +/** + * legal header + * + * ============================================================================ + * TJP 10/11/01 Got rid of the constructed singleton, it was essentially + * a pointer! + * + * @author Timothy Prepscius + * @created 3/../01 + */ + +#pragma once + +#include "ThreadSpecificSingleton.h" + +#include "../system/DisableWarningsPush.h" +#include +#include "../system/DisableWarningsPop.h" + +namespace timprepscius { +namespace core { + +/** + * a Singleton container, whose contained instance is constructed on its + * first request + * + * T defines the class pointer type + * X defines the allocation class type + */ +template +class ThreadSpecificSingletonNoConstruct +{ + private: + boost::thread_specific_ptr instance; + + static void cleanup_function (T *p) { } + + public: + ThreadSpecificSingletonNoConstruct () : + instance(&cleanup_function) { } + + virtual ~ThreadSpecificSingletonNoConstruct () {} + + virtual void setInstance (T *instance) + { + this->instance.release(); + this->instance.reset(instance); + } + + void deleteInstance() + { + delete this->instance.release(); + } + + virtual T *p() + { + return instance.get(); + } + + /********************************************************************** + * operator overloads to simplify use + **********************************************************************/ + + operator T *() + { + return p(); + } + + T *operator ->() + { + return p(); + } + + T &operator *() + { + return *p(); + } +} ; + +/** + * a Singleton container, whose contained instance is constructed on its + * first request + * + * T defines the class pointer type + * X defines the allocation class type + */ +template +class ThreadSpecificSingleton : public ThreadSpecificSingletonNoConstruct +{ + protected: + typedef ThreadSpecificSingletonNoConstruct Super; + public: + virtual void setInstance (T *instance) + { + Super::deleteInstance(); + Super::setInstance(instance); + } + + T *p() + { + T *p = Super::p(); + if (!p) + Super::setInstance(p = new X()); + + return p; + } + + /********************************************************************** + * operator overloads to simplify use + **********************************************************************/ + + operator T *() + { + return p(); + } + + T *operator ->() + { + return p(); + } + + T &operator *() + { + return *p(); + } +} ; + +} // namespace utilities +} // namespace + diff --git a/tjp/core/string/to_cstring.hpp b/tjp/core/string/to_cstring.hpp new file mode 100644 index 0000000..a8edf57 --- /dev/null +++ b/tjp/core/string/to_cstring.hpp @@ -0,0 +1,43 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include + +namespace tjp::core { + +template +String to_cstring(const T &v) +{ + std::ostringstream out_; + for (auto &c_ : v) + { + auto c = static_cast(c_); + + out_ << "\\x" << std::hex << std::setw(2) << std::setfill('0') << (static_cast(c) & 0xff); + } + + return out_.str(); +} + +template +String to_printable_with_dots(const T &v) +{ + std::ostringstream out_; + for (auto &c_ : v) + { + if (isprint(static_cast(c_) && c_ != '\n')) + { + out_ << c_; + } + else + { + out_ << "."; + } + } + + return out_.str(); +} + +} // namespace diff --git a/tjp/core/string_lookup/StringLookup.cpp b/tjp/core/string_lookup/StringLookup.cpp new file mode 100755 index 0000000..6ac4348 --- /dev/null +++ b/tjp/core/string_lookup/StringLookup.cpp @@ -0,0 +1,166 @@ +// TJP COPYRIGHT HEADER + +#include "StringLookup.h" +#include +#include +#include +#include + +#include +#include + +#include "../file/Path.h" + +#include +#include + +namespace tjp { +namespace core { +namespace string_lookup { + +void SKeySource::clear() +{ + kv.clear(); +} + +void SKeySource::load(const String &path_) +{ + path = path_; + + std::ifstream f(path); + if (!f) + return ; + + while (!f.eof()) + { + String line; + std::getline(f, line); + line = core::trim_copy(line); + if (line.empty()) + continue; + + static String comment = "#"; + if (starts_with(line, comment)) + continue; + + auto colon = line.find(':'); + if (colon == String::npos) + continue; + + auto key = line.substr(0, colon); + auto value = line.substr(colon+1); + + if (value == "$") + { + value = ""; + + line.clear(); + int notFirst = 0; + while (!f.eof()) + { + std::getline(f, line); + line = core::trim_copy(line); + if (line == "$") + break; + + if (notFirst++) + value += "\n"; + + value += line; + } + } + + kv[key] = value; + } + + version++; + +#ifdef DEBUG + empty = "XXXXXXX"; +#endif + +} + +const SKeyValue &SKeySource::get(const StringView &s) const +{ + auto v = map_value(kv, s); + if (v) + return *v; + +#ifdef DEBUG + sLogDebug("debug", file::fileName(path) << " missing key: " << s); +#endif + + return empty; +} + + +const SKeyValue &SKeySource::get(const KeySet &keys) const +{ + std::ostringstream oss; + + auto notFirst = 0; + for (auto key: keys) + { + if (notFirst++) + oss << ","; + oss << key; + } + + auto s = oss.str(); + return get(s); +} + +SKey::SKey (SKeyKey key_, SKey *parent_) : + key(key_), + parent(parent_), + source(nullptr) +{ +} + +SKey::SKey (SKeyKey key_, SKeySource *source_) : + key(key_), + source(source_), + parent(nullptr) +{ +} + +KeySet SKey::getKeySet() const +{ + if (parent) + { + auto result = parent->getKeySet(); + result.push_back(key); + return result; + } + + return { key }; +} + +SKeySource *SKey::getSource() const +{ + if (source) + return source; + + if (parent) + return parent->getSource(); + + return nullptr; +} + +const SKeyValue &SKey::get_ () const +{ + auto &cacheTo = const_cast(*this); + auto source = getSource(); + + cacheTo.source = source; + cacheTo.cache = source->get(getKeySet()); + cacheTo.version.update(source->version); + + return cacheTo.cache; +} + +} // namespace +} // namespace +} // namespace + diff --git a/tjp/core/string_lookup/StringLookup.h b/tjp/core/string_lookup/StringLookup.h new file mode 100755 index 0000000..b1abdcf --- /dev/null +++ b/tjp/core/string_lookup/StringLookup.h @@ -0,0 +1,98 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include +#include +#include +#include +#include "../resource_version/GlobalResourceVersion.hpp" +#include + +#include + +namespace tjp { +namespace core { +namespace string_lookup { + +using SKeyValue = String; +using SKeyKey = const char *; +using Version = GlobalResourceVersion; + +using KeySet = StackArray; + +struct SKeySource +{ + Map kv; + Version version; + String path; + + SKeyValue empty; + + void clear(); + void load(const String &path); + + const SKeyValue &get(const StringView &v) const; + const SKeyValue &get(const KeySet &keys) const; +} ; + +struct SKey { + SKeyKey key; + SKey *parent; + SKeySource *source; + + Version version; + SKeyValue cache; + + SKey (SKeyKey key_, SKey *parent_); + SKey (SKeyKey key_, SKeySource *source_); + + KeySet getKeySet() const; + SKeySource *getSource() const; + + const SKeyValue &get_ () const; + const SKeyValue &get() const + { + if (source && + version == source->version) + return cache; + + return get_(); + } + + operator const String &() const + { + return get(); + } + + operator StringView() const + { + return get(); + } + + const char *c_str() const + { + return get().c_str(); + } + + bool empty() const + { + return get().empty(); + } +}; + +template +String SL(const SKeySource &source, T&& ... args) +{ + return source.get(KeySet { (args.key)... }); +} + +#define IMPLEMENT_STRING_LOOKUP_EXP(s, y, x) tjp::core::string_lookup::SKey x(y, &s) + +#define IMPLEMENT_STRING_LOOKUP(s, x) IMPLEMENT_STRING_LOOKUP_EXP(s, #x, x) + + +} // namespace +} // namespace +} // namespace + diff --git a/tjp/core/system_usage/CpuUsage.h b/tjp/core/system_usage/CpuUsage.h new file mode 100644 index 0000000..a0e0138 --- /dev/null +++ b/tjp/core/system_usage/CpuUsage.h @@ -0,0 +1,28 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp { +namespace core { + +struct CpuUsage { + size_t total; + size_t idle; +} ; + +CpuUsage getCpuUsage (); + +inline +CpuUsage operator-(const CpuUsage &lhs, const CpuUsage &rhs) +{ + return CpuUsage { + .total = lhs.total - rhs.total, + .idle = lhs.idle - rhs.idle + }; +} + +} // namespace +} // namespace + diff --git a/tjp/core/system_usage/NetworkUsage.h b/tjp/core/system_usage/NetworkUsage.h new file mode 100644 index 0000000..33e4d99 --- /dev/null +++ b/tjp/core/system_usage/NetworkUsage.h @@ -0,0 +1,28 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp { +namespace core { + +struct NetworkUsage { + size_t total; + size_t used; +} ; + +NetworkUsage getNetworkUsage (); + +inline +NetworkUsage operator-(const NetworkUsage &lhs, const NetworkUsage &rhs) +{ + return NetworkUsage { + .total = lhs.total - rhs.total, + .used = lhs.used - rhs.used + }; +} + +} // namespace +} // namespace + diff --git a/tjp/core/system_usage/ProcessUsage.cpp b/tjp/core/system_usage/ProcessUsage.cpp new file mode 100644 index 0000000..d6e772b --- /dev/null +++ b/tjp/core/system_usage/ProcessUsage.cpp @@ -0,0 +1,60 @@ +// TJP COPYRIGHT HEADER + +#include "ProcessUsage.h" + +#include +#include + +#include + +#include + +namespace tjp { +namespace core { + +template +T difference_wrapped(T l, T r) +{ + auto max = std::numeric_limits::max(); + + return + (l >= r) ? + l - r : + max - r + l + 1; +} + + +ProcessUsage operator-(const ProcessUsage &lhs, const ProcessUsage &rhs) +{ + auto ticks = difference_wrapped((clock_t)lhs.ticks, (clock_t)rhs.ticks); + auto millis = difference_wrapped(lhs.total, rhs.total); + + static_assert((u64)60 * 1000 * CLOCKS_PER_SEC < std::numeric_limits::max()); + + static_assert(std::numeric_limits::max() >= std::numeric_limits::max()); + + return ProcessUsage { + .ticks = (size_t)ticks, + .total = millis * CLOCKS_PER_SEC / 1000 + } ; +} ; + +ProcessUsage getProcessUsage () +{ + static auto start = std::chrono::high_resolution_clock::now(); + auto now = std::chrono::high_resolution_clock::now(); + + auto totalTime = std::chrono::duration_cast(now - start).count(); + + auto ticks = clock(); + + return ProcessUsage { + .ticks = (size_t)ticks, + .total = (size_t)totalTime + } ; + +} + +} // namespace +} // namespace + diff --git a/tjp/core/system_usage/ProcessUsage.h b/tjp/core/system_usage/ProcessUsage.h new file mode 100644 index 0000000..398fc0d --- /dev/null +++ b/tjp/core/system_usage/ProcessUsage.h @@ -0,0 +1,22 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp { +namespace core { + +struct ProcessUsage { + size_t ticks; + size_t total; +} ; + +ProcessUsage getProcessUsage (); + +ProcessUsage operator-(const ProcessUsage &lhs, const ProcessUsage &rhs); + + +} // namespace +} // namespace + diff --git a/tjp/core/system_usage/SystemUsage.cpp b/tjp/core/system_usage/SystemUsage.cpp new file mode 100644 index 0000000..008de61 --- /dev/null +++ b/tjp/core/system_usage/SystemUsage.cpp @@ -0,0 +1,52 @@ +// TJP COPYRIGHT HEADER + +#include "SystemUsage.hpp" + +#include +#include +#include +#include + +namespace tjp { +namespace core { + +SystemUsage::SystemUsage () +{ + poll(); +} + +void SystemUsage::poll () +{ + Poll poll { + .time = time::now(), + .cpu = getCpuUsage(), + .network = getNetworkUsage(), + .process = getProcessUsage() + }; + + if (previous.time != time::Time::Zero) + { + usage.cpu = poll.cpu - previous.cpu; + usage.network = poll.network - previous.network; + usage.duration = poll.time - previous.time; + usage.process = poll.process - previous.process; + } + + previous = poll; +} + +void SystemUsage::sleepAndPoll(time::Duration duration) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(duration.v)); + poll(); +} + +SystemUsage::Poll SystemUsage::getUsage () const +{ + return usage; +} + + +} // namespace +} // namespace + diff --git a/tjp/core/system_usage/SystemUsage.h b/tjp/core/system_usage/SystemUsage.h new file mode 100644 index 0000000..e3b2ccc --- /dev/null +++ b/tjp/core/system_usage/SystemUsage.h @@ -0,0 +1,12 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +namespace tjp { +namespace core { + +struct SystemUsage; + +} // namespace +} // namespace + diff --git a/tjp/core/system_usage/SystemUsage.hpp b/tjp/core/system_usage/SystemUsage.hpp new file mode 100644 index 0000000..b7b8530 --- /dev/null +++ b/tjp/core/system_usage/SystemUsage.hpp @@ -0,0 +1,35 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "SystemUsage.h" +#include +#include "CpuUsage.h" +#include "NetworkUsage.h" +#include "ProcessUsage.h" + +namespace tjp { +namespace core { + +struct SystemUsage +{ + struct Poll { + time::Time time; + CpuUsage cpu; + NetworkUsage network; + ProcessUsage process; + time::Duration duration; + }; + + Poll usage, previous; + void poll(); + void sleepAndPoll(time::Duration duration = time::Milliseconds(10)); + + SystemUsage(); + + Poll getUsage () const; +} ; + +} // namespace +} // namespace + diff --git a/tjp/core/system_usage/apple/CpuUsage.cpp b/tjp/core/system_usage/apple/CpuUsage.cpp new file mode 100644 index 0000000..5143dfa --- /dev/null +++ b/tjp/core/system_usage/apple/CpuUsage.cpp @@ -0,0 +1,36 @@ +// TJP COPYRIGHT HEADER + +#include "../CpuUsage.h" + +#include +#include +#include +#include + +namespace tjp { +namespace core { + +CpuUsage getCpuUsage () +{ + host_cpu_load_info_data_t cpuinfo; + mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT; + if (host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&cpuinfo, &count) == KERN_SUCCESS) + { + unsigned long long totalTicks = 0; + for(int i=0; i +#include + +#include +#include + +#include +#include + +namespace tjp { +namespace core { + +CpuUsage getCpuUsage () +{ + std::ifstream in("/proc/stat"); + + std::string line; + + std::getline(in, line); + std::istringstream iss(line); + + std::string label; + iss >> label; + + std::vector values; + while (iss) + { + size_t value; + iss >> value; + + if (!iss.fail()) + values.push_back(value); + } + + sLogDebug("core::system_usage", "[" << line << "]" << logVar(values)); + + auto &idle = values[3]; + size_t total = 0; + for (auto &v : values) + total += v; + + return { + .total = total, + .idle = idle + }; +} + +} // namespace +} // namespace + diff --git a/tjp/core/system_usage/linux/NetworkUsage.cpp b/tjp/core/system_usage/linux/NetworkUsage.cpp new file mode 100644 index 0000000..7236455 --- /dev/null +++ b/tjp/core/system_usage/linux/NetworkUsage.cpp @@ -0,0 +1,15 @@ +// TJP COPYRIGHT HEADER + +#include "../NetworkUsage.h" + +namespace tjp { +namespace core { + +NetworkUsage getNetworkUsage () +{ + return { 0, 0 }; +} + +} // namespace +} // namespace + diff --git a/tjp/core/thread_pool/AutoThreadPool.h b/tjp/core/thread_pool/AutoThreadPool.h new file mode 100755 index 0000000..c6ea14e --- /dev/null +++ b/tjp/core/thread_pool/AutoThreadPool.h @@ -0,0 +1,15 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp::core::thread_pool { + +template +struct AutoThreadPool; + +template +struct AutoThreadPoolSized; + +} // namespace diff --git a/tjp/core/thread_pool/AutoThreadPool.hpp b/tjp/core/thread_pool/AutoThreadPool.hpp new file mode 100755 index 0000000..9733c5f --- /dev/null +++ b/tjp/core/thread_pool/AutoThreadPool.hpp @@ -0,0 +1,151 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "AutoThreadPool.h" + +#include +#include +#include + +#include +#include +#include + +namespace tjp::core::thread_pool { + +template +struct AutoThreadPool +{ + using Pool = P; + + Pool pool; + size_t minThreads, maxThreads; + + std::chrono::time_point from; + float averagingDuration; + float averageDesiredThreads = 0; + bool ending = false; + + AutoThreadPool(size_t minThreads_, size_t maxThreads_, float averagingDuration_=1.0) : + minThreads(minThreads_), + maxThreads(maxThreads_), + averagingDuration(averagingDuration_) + { + from = std::chrono::system_clock::now(); + averageDesiredThreads = minThreads; + + pool.timeout = [&]() { onTimeOut(); }; + pool.resize(minThreads); + } + + ~AutoThreadPool() + { + stop(true); + } + + void stop(bool wait) + { + ending = true; + + pool.stop(wait); + } + + template + auto push(T && ...t) + { + return pool.with_lock([&]() { + auto result = pool.push_noLock(std::forward(t)...); + onTimeOut(); + return result; + }); + } + + void onTimeOut() + { + if (ending) + return; + + auto to = std::chrono::system_clock::now(); + std::chrono::duration duration_ = to - from; + auto duration = duration_.count(); + + auto numRunning = pool.num_running(); + auto numIdle = pool.num_idle(); + auto numProcessing = numRunning - numIdle; + auto numWaiting = pool.queue_size(); + auto numSlots = numProcessing + numWaiting; + + if (numSlots > averageDesiredThreads) + { + averageDesiredThreads = numSlots; + } + else + { + averageDesiredThreads = (averagingDuration * averageDesiredThreads + duration * numSlots) / (averagingDuration + duration); + debug_assert(averageDesiredThreads > 0); + from = to; + } + + sLogDebug("core::autothreadpool", logOfThis(this) << logVar(averageDesiredThreads) << logVar(numProcessing) << logVar(numWaiting) << logVar(duration) << logVar(numRunning) << logVar(numIdle)); + + resize_no_lock(); + } + + void resize () + { + pool.with_lock([&]() { + resize_no_lock(); + }); + } + + void resize_no_lock () + { + if (ending) + return; + + auto weight = 0.2; + auto numDesiredThreadsIncrease = (int)std::round(averageDesiredThreads); + auto numDesiredThreadsDecrease = (int)std::round(averageDesiredThreads + weight); + if (!increase(numDesiredThreadsIncrease)) + cull(numDesiredThreadsDecrease); + } + + bool increase (size_t numDesiredThreads) + { + numDesiredThreads = std::min(maxThreads, numDesiredThreads); + + auto numThreads = pool.num_requested(); + if (numThreads >= numDesiredThreads) + return false; + + sLogDebug("core::autothreadpool", logOfThis(this) << logVar(numThreads) << logVar(numDesiredThreads) << logVar(pool.num_running())); + + pool.resize_no_lock(numDesiredThreads); + + return true; + } + + bool cull (size_t numDesiredThreads) + { + numDesiredThreads = std::max(minThreads, numDesiredThreads); + + auto numThreads = pool.num_requested(); + if (numThreads <= numDesiredThreads) + return false; + + sLogDebug("core::autothreadpool", logOfThis(this) << logVar(numThreads) << logVar(numDesiredThreads) << logVar(pool.num_running())); + + pool.resize_no_lock(numDesiredThreads); + + return true; + } +} ; + +template +struct AutoThreadPoolSized : AutoThreadPool

+{ + AutoThreadPoolSized () : AutoThreadPool

(minThreads_, maxThreads_) {} +} ; + +} // namespace diff --git a/tjp/core/thread_pool/Base.h b/tjp/core/thread_pool/Base.h new file mode 100755 index 0000000..1841817 --- /dev/null +++ b/tjp/core/thread_pool/Base.h @@ -0,0 +1,287 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +#include +#include +#include +#include +#include + +#include + + +namespace tjp::core::thread_pool { + +using Mutex = core::Mutex; +using ThreadID = u64; +constexpr ThreadID ThreadID_Zero = 0; + +template +class Base { + +protected: + using value_type = T; + using self_type = P; + using Size = int; + + Atomic numRequested = 0; + Atomic numRunning = 0; + + Atomic nextID = 1; + + Map> threads; + Atomic numIdle = 0; // how many threads are waiting + + Event::Mutex mutex; + Event event; + +protected: + std::queue q; + +public: + + Base() + { + } + + Base(int nThreads) + { + resize(nThreads); + } + + ~Base() + { + stop(true); + } + + template + auto with_lock(F &&f) + { + auto lock = lock_of(mutex); + return f(); + } + + // get the number of running threads in the pool + int num_requested() + { + return numRequested; + } + + // number of idle threads + int num_idle() + { + return numIdle; + } + + int num_running() + { + return numRunning; + } + + void resize(size_t nThreads) + { + auto lock = lock_of(mutex); + resize_no_lock((Size)nThreads); + } + + void resize_no_lock(size_t nThreads) + { + numRequested = (Size)nThreads; + on_resize(); + } + + auto size() + { + return threads.size(); + } + + auto queue_size() + { + return q.size(); + } + + void on_resize() + { + while (numRequested > numRunning) + { + this->newThread(nextID++); + } + + if (numRequested < numRunning) + { + event.notify_all(); + } + } + + void clear_queue() + { + q.clear(); + } + + void stop(bool isWait = false) + { + resize(0); + + if (isWait) + { + auto lock = lock_of(mutex); + while (numRunning > 0) + { + event.wait(lock); + } + } + } + + auto push_noLock(T &&t) + { + q.emplace(std::move(t)); + event.notify_one(); + + return q.size(); + } + + auto push(T &&t) + { + auto lock = lock_of(mutex); + return push_noLock(std::move(t)); + } + + auto pop_noLock(Optional &t) + { + debug_assert(!t.has_value()); + + if (q.empty()) + return false; + + t = std::move(q.front()); + q.pop(); + + return true; + } + + auto pop(Optional &t) + { + auto lock = lock_of(mutex); + pop_noLock(t); + } + +public: + Function timeout; + +private: + void onTimeOut() + { + if (timeout) + timeout(); + } + + // deleted + Base(const Base &);// = delete; + Base(Base &&);// = delete; + Base & operator=(const Base &);// = delete; + Base & operator=(Base &&);// = delete; + + void onExit(ThreadID id) + { + sLogRelease("core::autothreadpool::delta", logOfThis(this) << " exit thread " << logVar(numRunning)); + + debug_assert(mutex.locked_by_caller()); + + auto thread_ = threads.find(id); + debug_assert(thread_ != threads.end()); + + if (thread_ != threads.end()) + { + auto &thread = thread_->second; + thread->detach(); + threads.erase(thread_); + } + } + + auto shouldExitThread () + { + auto currentlyRunning = (Size)numRunning; + while (currentlyRunning > numRequested) + { + auto oneLessRunning = currentlyRunning - 1; + if (numRunning.compare_exchange_strong(currentlyRunning, oneLessRunning)) + { + event.notify_all(); + return true; + } + + currentlyRunning = (Size)numRunning; + } + + return false; + }; + + void newThread(ThreadID id) + { + ++numRunning; + + sLogRelease("core::autothreadpool::delta", logOfThis(this) << " new thread " << logVar(numRunning)); + + auto f = [this, id]() + { + // this is only the duration to call timeout and possibly exit + auto duration = std::chrono::milliseconds(100); + + Optional t; + + auto lock = lock_of(mutex); + pop_noLock(t); + + while (true) + { + // process the next item + while(t) + { + onTimeOut(); + + { + auto unlock = unlock_of(mutex); + static_cast(this)->process(id, std::move(*t)); + t.reset(); + } + + if (shouldExitThread()) + return onExit(id); + + pop_noLock(t); + } + + + // the queue is empty here, wait for the next command + ++numIdle; + + // wait for an item + // spurious wake ups are possible + do + { + onTimeOut(); + + if (shouldExitThread()) + { + --numIdle; + return onExit(id); + } + + event.wait_for(lock, duration); + pop_noLock(t); + } + while (!t); + + --numIdle; + } + }; + + threads.emplace(id, new std::thread(f)); // compiler may not support std::make_unique() + } +}; + +} + diff --git a/tjp/core/thread_pool/FunctionalProcessor.hpp b/tjp/core/thread_pool/FunctionalProcessor.hpp new file mode 100755 index 0000000..d5dbeca --- /dev/null +++ b/tjp/core/thread_pool/FunctionalProcessor.hpp @@ -0,0 +1,86 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Base.h" + +#include + +namespace tjp::core::thread_pool { + +class FunctionalProcessor : public Base>, FunctionalProcessor> { + +public: + + using Super = Base>, FunctionalProcessor>; + + template + FunctionalProcessor(T && ...t) : + Super(std::forward(t)...) + { + } + + ~FunctionalProcessor() + { + stop(true); + } + + void process(ThreadID id, value_type &&f) + { + (*f)(id); + } + + template + auto push_noLock(F && f, Rest&&... rest) ->std::future + { + auto pck = std::make_shared< + std::packaged_task + >( + std::bind(std::forward(f), std::placeholders::_1, std::forward(rest)...) + ); + + auto _f = new std::function([pck](ThreadID id) { + (*pck)(id); + }); + + Super::push_noLock(value_type(_f)); + + return pck->get_future(); + } + + template + auto push(F && f, Rest&&... rest) ->std::future + { + auto lock = lock_of(mutex); + + return push_noLock(std::forward(f), std::forward(rest)...); + } + + template + auto push_noLock(F && f) ->std::future + { + auto pck = std::make_shared< + std::packaged_task + >(std::forward(f)); + + auto _f = new std::function([pck](ThreadID id) { + (*pck)(id); + }); + + Super::push_noLock(value_type(_f)); + + return pck->get_future(); + } + + template + auto push(F && f) ->std::future + { + auto lock = lock_of(mutex); + + return push_noLock(std::forward(f)); + } + +}; + +} + diff --git a/tjp/core/thread_pool/ObjectConsumer.hpp b/tjp/core/thread_pool/ObjectConsumer.hpp new file mode 100755 index 0000000..207de22 --- /dev/null +++ b/tjp/core/thread_pool/ObjectConsumer.hpp @@ -0,0 +1,37 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Base.h" + +namespace tjp::core::thread_pool { + +template +class ObjectConsumer : public Base> { + +public: + using Super = Base>; + + Function f; + +public: + + template + ObjectConsumer(U && ...u) : + Super(std::forward(u)...) + { + } + + ~ObjectConsumer() + { + this->stop(true); + } + + void process(ThreadID id, typename Super::value_type &&t) + { + f(std::move(t)); + } +}; + +} + diff --git a/tjp/core/thread_pool/_delete/AutoThreadPool.h b/tjp/core/thread_pool/_delete/AutoThreadPool.h new file mode 100755 index 0000000..c6ea14e --- /dev/null +++ b/tjp/core/thread_pool/_delete/AutoThreadPool.h @@ -0,0 +1,15 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +namespace tjp::core::thread_pool { + +template +struct AutoThreadPool; + +template +struct AutoThreadPoolSized; + +} // namespace diff --git a/tjp/core/thread_pool/_delete/AutoThreadPool.hpp b/tjp/core/thread_pool/_delete/AutoThreadPool.hpp new file mode 100755 index 0000000..e971de9 --- /dev/null +++ b/tjp/core/thread_pool/_delete/AutoThreadPool.hpp @@ -0,0 +1,50 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "AutoThreadPool.h" +#include "ctpl_stl.h" + +namespace tjp::core::thread_pool { + +template +struct AutoThreadPool +{ + using Pool = P; + Pool pool; + size_t minThreads, maxThreads; + + AutoThreadPool(size_t minThreads_, size_t maxThreads_) : + pool((int)minThreads_), + minThreads(minThreads_), + maxThreads(maxThreads_) + { + } + + template + auto push(T &&...t) + { + adjustBeforePush(); + pool.push(t...); + } + + void adjustBeforePush () + { + auto numThreads = pool.size(); + auto numIdle = pool.n_idle(); + + if (numIdle < 1 && numThreads < maxThreads) + pool.resize(numThreads + 1); + + if (numIdle > 2 && numThreads > minThreads) + pool.resize(numThreads - 1); + } +} ; + +template +struct AutoThreadPoolSized : AutoThreadPool

+{ + AutoThreadPoolSized () : AutoThreadPool(minThreads_, maxThreads_) {} +} ; + +} // namespace diff --git a/tjp/core/thread_pool/_delete/ctpl_stl.h b/tjp/core/thread_pool/_delete/ctpl_stl.h new file mode 100755 index 0000000..fee695d --- /dev/null +++ b/tjp/core/thread_pool/_delete/ctpl_stl.h @@ -0,0 +1,253 @@ +// TJP COPYRIGHT HEADER + +/********************************************************* +* +* Copyright (C) 2014 by Vitaliy Vitsentiy +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +*********************************************************/ + + +#ifndef __ctpl_stl_thread_pool_H__ +#define __ctpl_stl_thread_pool_H__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +// thread pool to run user's functors with signature +// ret func(int id, other_params) +// where id is the index of the thread that runs the functor +// ret is some return type + + +namespace ctpl { + + namespace detail { + template + class Queue { + public: + bool push(T const & value) { + std::unique_lock lock(this->mutex); + this->q.push(value); + return true; + } + // deletes the retrieved element, do not use for non integral types + bool pop(T & v) { + std::unique_lock lock(this->mutex); + if (this->q.empty()) + return false; + v = this->q.front(); + this->q.pop(); + return true; + } + bool empty() { + std::unique_lock lock(this->mutex); + return this->q.empty(); + } + private: + std::queue q; + std::mutex mutex; + }; + } + + class thread_pool { + + public: + + thread_pool() { this->init(); } + thread_pool(int nThreads) { this->init(); this->resize(nThreads); } + + // the destructor waits for all the functions in the queue to be finished + ~thread_pool() { + this->stop(true); + } + + // get the number of running threads in the pool + int size() { return static_cast(this->threads.size()); } + + // number of idle threads + int n_idle() { return this->nWaiting; } + std::thread & get_thread(int i) { return *this->threads[i]; } + + // change the number of threads in the pool + // should be called from one thread, otherwise be careful to not interleave, also with this->stop() + // nThreads must be >= 0 + void resize(int nThreads) { + if (!this->isStop && !this->isDone) { + int oldNThreads = static_cast(this->threads.size()); + if (oldNThreads <= nThreads) { // if the number of threads is increased + this->threads.resize(nThreads); + this->flags.resize(nThreads); + + for (int i = oldNThreads; i < nThreads; ++i) { + this->flags[i] = std::make_shared>(false); + this->set_thread(i); + } + } + else { // the number of threads is decreased + for (int i = oldNThreads - 1; i >= nThreads; --i) { + *this->flags[i] = true; // this thread will finish + this->threads[i]->detach(); + } + { + // stop the detached threads that were waiting + std::unique_lock lock(this->mutex); + this->cv.notify_all(); + } + this->threads.resize(nThreads); // safe to delete because the threads are detached + this->flags.resize(nThreads); // safe to delete because the threads have copies of shared_ptr of the flags, not originals + } + } + } + + // empty the queue + void clear_queue() { + std::function * _f; + while (this->q.pop(_f)) + delete _f; // empty the queue + } + + // pops a functional wrapper to the original function + std::function pop() { + std::function * _f = nullptr; + this->q.pop(_f); + std::unique_ptr> func(_f); // at return, delete the function even if an exception occurred + std::function f; + if (_f) + f = *_f; + return f; + } + + // wait for all computing threads to finish and stop all threads + // may be called asynchronously to not pause the calling thread while waiting + // if isWait == true, all the functions in the queue are run, otherwise the queue is cleared without running the functions + void stop(bool isWait = false) { + if (!isWait) { + if (this->isStop) + return; + this->isStop = true; + for (int i = 0, n = this->size(); i < n; ++i) { + *this->flags[i] = true; // command the threads to stop + } + this->clear_queue(); // empty the queue + } + else { + if (this->isDone || this->isStop) + return; + this->isDone = true; // give the waiting threads a command to finish + } + { + std::unique_lock lock(this->mutex); + this->cv.notify_all(); // stop all waiting threads + } + for (int i = 0; i < static_cast(this->threads.size()); ++i) { // wait for the computing threads to finish + if (this->threads[i]->joinable()) + this->threads[i]->join(); + } + // if there were no threads in the pool but some functors in the queue, the functors are not deleted by the threads + // therefore delete them here + this->clear_queue(); + this->threads.clear(); + this->flags.clear(); + } + + template + auto push(F && f, Rest&&... rest) ->std::future { + auto pck = std::make_shared>( + std::bind(std::forward(f), std::placeholders::_1, std::forward(rest)...) + ); + auto _f = new std::function([pck](int id) { + (*pck)(id); + }); + this->q.push(_f); + std::unique_lock lock(this->mutex); + this->cv.notify_one(); + return pck->get_future(); + } + + // run the user's function that excepts argument int - id of the running thread. returned value is templatized + // operator returns std::future, where the user can get the result and rethrow the catched exceptins + template + auto push(F && f) ->std::future { + auto pck = std::make_shared>(std::forward(f)); + auto _f = new std::function([pck](int id) { + (*pck)(id); + }); + this->q.push(_f); + std::unique_lock lock(this->mutex); + this->cv.notify_one(); + return pck->get_future(); + } + + + private: + + // deleted + thread_pool(const thread_pool &);// = delete; + thread_pool(thread_pool &&);// = delete; + thread_pool & operator=(const thread_pool &);// = delete; + thread_pool & operator=(thread_pool &&);// = delete; + + void set_thread(int i) { + std::shared_ptr> flag(this->flags[i]); // a copy of the shared ptr to the flag + auto f = [this, i, flag/* a copy of the shared ptr to the flag */]() { + std::atomic & _flag = *flag; + std::function * _f; + bool isPop = this->q.pop(_f); + while (true) { + while (isPop) { // if there is anything in the queue + std::unique_ptr> func(_f); // at return, delete the function even if an exception occurred + (*_f)(i); + if (_flag) + return; // the thread is wanted to stop, return even if the queue is not empty yet + else + isPop = this->q.pop(_f); + } + // the queue is empty here, wait for the next command + std::unique_lock lock(this->mutex); + ++this->nWaiting; + this->cv.wait(lock, [this, &_f, &isPop, &_flag](){ isPop = this->q.pop(_f); return isPop || this->isDone || _flag; }); + --this->nWaiting; + if (!isPop) + return; // if the queue is empty and this->isDone == true or *flag then return + } + }; + this->threads[i].reset(new std::thread(f)); // compiler may not support std::make_unique() + } + + void init() { this->nWaiting = 0; this->isStop = false; this->isDone = false; } + + std::vector> threads; + std::vector>> flags; + detail::Queue *> q; + std::atomic isDone; + std::atomic isStop; + std::atomic nWaiting; // how many threads are waiting + + std::mutex mutex; + std::condition_variable cv; + }; + +} + +#endif // __ctpl_stl_thread_pool_H__ diff --git a/tjp/core/thread_pool/_tests/AutoThreadPool.cpp b/tjp/core/thread_pool/_tests/AutoThreadPool.cpp new file mode 100755 index 0000000..8c574ef --- /dev/null +++ b/tjp/core/thread_pool/_tests/AutoThreadPool.cpp @@ -0,0 +1,122 @@ +// TJP COPYRIGHT HEADER + +#include +#include + +#include +#include +#include +#include +#include + +namespace tjp::core::thread_pool { +namespace { + +SCENARIO("AutoThreadPool") +{ + xLogActivateStory("core::autothreadpool"); + + GIVEN("thread pool [0,4]") + { + AutoThreadPool pool(0,4); + + auto jobTime = std::chrono::seconds(1); + WHEN("to the max jobs") + { + for (auto i : range(pool.maxThreads)) + { + unused(i); + + pool.push([=](ThreadID id) { + std::this_thread::sleep_for(jobTime); + }); + } + + REQUIRE(pool.pool.size() == pool.maxThreads); + } + + WHEN("to the max jobs and let cull") + { + for (auto i : range(pool.maxThreads)) + { + unused(i); + + pool.push([=](auto id) { + std::this_thread::sleep_for(jobTime); + }); + } + + REQUIRE(pool.pool.size() == pool.maxThreads); + + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + REQUIRE(pool.pool.size() == pool.maxThreads); + + std::this_thread::sleep_for(jobTime); + for (auto i : range(60)) + { + unused(i); + + sLogDebug("testing", logVar(pool.averageDesiredThreads) << logVar(pool.pool.size()) << logVar(pool.pool.num_requested())); + + if (pool.pool.size() == pool.minThreads) + break; + + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + } + + REQUIRE(pool.pool.size() == pool.minThreads); + } + + WHEN("to the max jobs, cull to half and then up") + { + for (auto i : range(pool.maxThreads)) + { + unused(i); + + pool.push([=](auto id) { + std::this_thread::sleep_for(jobTime); + }); + } + + REQUIRE(pool.pool.size() == pool.maxThreads); + + std::this_thread::sleep_for(jobTime); + + while(pool.pool.size() > pool.maxThreads/2) + { + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + sLogDebug("testing", logVar(pool.averageDesiredThreads) << logVar(pool.pool.size()) << logVar(pool.pool.num_requested())); + } + + for (auto i : range(pool.maxThreads)) + { + unused(i); + + pool.push([=](auto id) { + std::this_thread::sleep_for(jobTime); + }); + } + + REQUIRE(pool.pool.size() == pool.maxThreads); + + for (auto i : range(60)) + { + unused(i); + + sLogDebug("testing", logVar(pool.averageDesiredThreads) << logVar(pool.pool.size()) << logVar(pool.pool.num_requested())); + + if (pool.pool.size() == pool.minThreads) + break; + + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + } + + REQUIRE(pool.pool.size() == pool.minThreads); + } + } +} + +} // namespace +} // namespace + diff --git a/tjp/core/threads/Parallel.cpp b/tjp/core/threads/Parallel.cpp new file mode 100755 index 0000000..e51ad62 --- /dev/null +++ b/tjp/core/threads/Parallel.cpp @@ -0,0 +1,109 @@ +// TJP COPYRIGHT HEADER + +#include "Parallel.hpp" +#include + +namespace tjp::core::threads { + + +Parallel::Parallel(int max, int free) +{ + start(max, free); +} + +Parallel::~Parallel() +{ + stop(); +} + +void Parallel::start(int max, int free) +{ + auto lock = lock_of(mutex); + if (running) + return; + + running = true; + auto n_ = + std::max( + 1, + std::min( + max, + (int)std::thread::hardware_concurrency() - free + ) + ); + + n = n_; + debug_assert(n > 0 && n <= max); + + threads = new std::thread[n]; + + for (auto m=0; m::max(); + event.notify_all(); + + auto n = std::thread::hardware_concurrency(); + for (auto m=0; m + +#include +#include +#include +#include + +namespace tjp::core::threads { + +struct Parallel +{ + size_t n; + std::thread *threads; + + using F = Function; + F f; + + [[no_unique_address]] + TestMutex oneUser; + + Event event; + Event::Mutex mutex; + bool running = false; + + using Signal = u128; + static constexpr Signal SignalBit = 1; + Signal signal = 0; + + Parallel(int max, int free); + ~Parallel(); + + void start(int max, int free); + void execute(size_t n, size_t m); + void run(F &&f_); + void stop(); + +} ; + +} // namespace diff --git a/tjp/core/threads/PinnedFutures.hpp b/tjp/core/threads/PinnedFutures.hpp new file mode 100755 index 0000000..27677dd --- /dev/null +++ b/tjp/core/threads/PinnedFutures.hpp @@ -0,0 +1,140 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include + +#include +#include +#include +#include +#include + +#include + +namespace tjp::core::threads { + + +// DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT +// DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT + +struct PinnedFutures +{ + std::thread thread; + Event::Mutex mutex; + Event event; + std::queue> events; + bool running = false; + + bool is_on_thread() + { + return thread.joinable() && + (std::this_thread::get_id() == thread.get_id()); + } + + PinnedFutures(bool start_=true) + { + if (start_) + start(); + } + + ~PinnedFutures() + { + stop(); + } + + void run() + { + auto lock = lock_of(mutex); + while (running) + { + if (events.empty()) + { + event.wait(lock); + continue; + } + + auto fn = std::move(events.front()); + events.pop(); + + { + auto unlock = unlock_of(mutex); + fn(); + } + } + } + + void start() + { + if (running) + return; + + running = true; + thread = std::thread([this]() { run(); }); + } + + void stop() + { + { + auto lock = lock_of(mutex); + running = false; + } + event.notify_all(); + if (thread.joinable()) + thread.join(); + } + + template + Future on_(Future &f0) + { + Promise p; + + auto swizzle = f0.then([this, p](auto &&f) { + auto lock = lock_of(mutex); + events.push([p, f=std::move(f)]() { + auto p_ = p; + auto f_ = f; + p_.consume([&](auto &&f) { return f.get(); }, f_); + }); + + event.notify_one(); + }); + + return p.get_future().then([swizzle](auto &&f1) { + return f1.get(); + }); + } + + template + auto run_(F &&f) + { + using R = decltype(f()); + Promise p; + + { + auto lock = lock_of(mutex); + events.push([&]() { p.consume(f); }); + event.notify_one(); + } + + return p.get_future().get(); + } + + template + Future on(Future &f) + { + return on_(f); + } + + template + auto run(F &&f) + { + if (is_on_thread()) + return f(); + + return run_(std::forward(f)); + } + +} ; + +} // namespace diff --git a/tjp/core/time-obsolete/Notes.txt b/tjp/core/time-obsolete/Notes.txt new file mode 100644 index 0000000..c73e32e --- /dev/null +++ b/tjp/core/time-obsolete/Notes.txt @@ -0,0 +1,2 @@ +The scheduler needs to be redone. + diff --git a/tjp/core/time-obsolete/Scheduler+Retry.hpp b/tjp/core/time-obsolete/Scheduler+Retry.hpp new file mode 100644 index 0000000..b6de2f2 --- /dev/null +++ b/tjp/core/time-obsolete/Scheduler+Retry.hpp @@ -0,0 +1,161 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#pragma once + +#include "Scheduler.hpp" +#include + +namespace timprepscius { +namespace core { +namespace time { + +struct RetryImmediateFailure : Exception +{ + RetryImmediateFailure(Exception &&e) : + exception(std::move(e)) + {} + + Exception exception; +} ; + +struct RetryOverrideDelayFailure : Exception +{ + RetryOverrideDelayFailure(Exception &&e, float delay_) : + exception(std::move(e)), + delay(delay_) + {} + + Exception exception; + float delay; +} ; + +template +void retry_( + const typename PromiseOfFutures::Instance &promise, + const WeakSchedulerPtr &scheduler_, + int remaining, float m, + G &&delayF, + F &&f, + int retries +) +{ + promise->add( + f().then([promise, scheduler_, remaining, m, delayF=std::move(delayF), f=std::move(f), retries](auto &&future) { + sLogDebug("core::time::retry", "time::retry " << logVar(remaining) << logVar(retries) << logVar(m)); + + if (remaining == 0) + { + promise->consume(future); + } + else + { + auto executeRetry = [&](int retries, float delay) + { + if (auto scheduler = strong(scheduler_)) + { + sLogDebug("core::time::retry", "time::retry scheduling try " << logVar(remaining) << logVar(retries) << logVar(m) << logVar(delay)); + + promise->add( + scheduler->schedule( + delay, + [promise, scheduler_, remaining, m, delayF=std::move(delayF), f=std::move(f), retries]() { + retry_( + promise, scheduler_, remaining-1, m, + std::move(delayF), + std::move(f), + retries + ); + } + ) + ); + } + else + { + promise->consume(future_of_exception(NullPointerException{})); + } + } ; + + try + { + promise->consume_throw_exception(future); + sLogDebug("core::time::retry", "time::retry consume_throw_exception succeeded " << logVar(remaining) << logVar(retries) << logVar(m)); + } + catch (RetryImmediateFailure &e) + { + sLogDebug("core::time::retry", "time::retry failed RetryImmediateFailure " << logVar(e.what())); + + promise->consume(future_of_exception(std::move(e.exception))); + } + catch (RetryOverrideDelayFailure &e) + { + sLogDebug("core::time::retry", "time::retry failed RetryOverrideDelayFailure " << logVar(e.what())); + + executeRetry( + retries+1, + e.delay + ); + } + catch (Exception &e) + { + sLogDebug("core::time::retry", "time::retry failed " << logVar(e.what())); + + executeRetry( + retries+1, + delayF(retries+1, m) + ); + } + } + }) + ); +} + +template +Future retry_function(const SchedulerPtr &scheduler, int count, float delay, C &&calc, F &&f) +{ + auto promise = PromiseOfFutures::generate(); + retry_(promise, weak(scheduler), count, delay, std::forward(calc), std::forward(f), 0); + return promise->get_future(); +} + +template +Future retry_exponential(const SchedulerPtr &scheduler, int count, float delay, F &&f) +{ + auto expontial = [](int count, float delay) { return (count*count) * delay; }; + + return retry_function(scheduler, count, delay, expontial, std::forward(f)); +} + +template +Future retry_linear(const SchedulerPtr &scheduler, int count, float delay, F &&f) +{ + auto linear = [](int count, float delay) { return count * delay; }; + + return retry_function(scheduler, count, delay, linear, std::forward(f)); +} + +template +Future retry_constant(const SchedulerPtr &scheduler, int count, float delay, F &&f) +{ + auto constant = [](int count, float delay) { return delay; }; + + return retry_function(scheduler, count, delay, constant, std::forward(f)); +} + +template +Future retry(const SchedulerPtr &scheduler, int count, float delay, F &&f) +{ + return retry_linear(scheduler, count, delay, std::forward(f)); +} + +} // namespace +} // namespace +} // namespace + diff --git a/tjp/core/time-obsolete/Scheduler.cpp b/tjp/core/time-obsolete/Scheduler.cpp new file mode 100644 index 0000000..6cb6db9 --- /dev/null +++ b/tjp/core/time-obsolete/Scheduler.cpp @@ -0,0 +1,34 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#include "Scheduler.hpp" + +namespace timprepscius::core::time { + +Scheduler::~Scheduler () +{ +} + +Future Scheduler::after(Time period) +{ + Promise v; + auto event = schedule( + period, + false, + [v]() { + auto r = v; + r.set_value(); + } + ); + + return v.get_future().then([event](auto &&f) { return f.get(); }); +} + +} // namespace diff --git a/tjp/core/time-obsolete/Scheduler.h b/tjp/core/time-obsolete/Scheduler.h new file mode 100644 index 0000000..f2b6aa5 --- /dev/null +++ b/tjp/core/time-obsolete/Scheduler.h @@ -0,0 +1,24 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#pragma once + +#include + +namespace timprepscius::core::time { + +struct Scheduler; +struct SchedulerEvent; +using SchedulerEventPtr = StrongPtr; + +using SchedulerPtr = StrongPtr; +using WeakSchedulerPtr = WeakPtr; + +} // namespace diff --git a/tjp/core/time-obsolete/Scheduler.hpp b/tjp/core/time-obsolete/Scheduler.hpp new file mode 100644 index 0000000..e544b9c --- /dev/null +++ b/tjp/core/time-obsolete/Scheduler.hpp @@ -0,0 +1,52 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#pragma once + +#include "Scheduler.h" + +#include + +#include +#include "Time.h" + +namespace timprepscius::core::time { + +struct SchedulerEvent { + virtual ~SchedulerEvent () {} + virtual void cancel () = 0; +} ; + +struct Scheduler +{ + virtual SchedulerEventPtr schedule(Duration period, bool repeat, std::function &&f) = 0; + virtual ~Scheduler(); + + Future after(Duration period); + + template + Future schedule(Duration period, F &&f); + + template + Future schedule_future(Duration period, F &&f); + + template + Future schedule(F &&f); + + template + Future schedule_future(F &&f); + + template + void run(F &&f); +}; + +} // namespace + +#include "Scheduler.inl" diff --git a/tjp/core/time-obsolete/Scheduler.inl b/tjp/core/time-obsolete/Scheduler.inl new file mode 100644 index 0000000..9bc6284 --- /dev/null +++ b/tjp/core/time-obsolete/Scheduler.inl @@ -0,0 +1,97 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#pragma once + +#include "Scheduler.h" + +#include +#include + +#include +#include "Time.h" + +namespace timprepscius::core::time { + +template +Future Scheduler::schedule(Duration period, F &&f) +{ + Promise v; + auto event = schedule( + period, + false, + [v, f=std::move(f)]() { + auto r = v; + r.consume(f); + } + ); + + return v.get_future().then([event](auto &&f) { return f.get(); }); +} + + +// this should be rewritten to use the above "after", should be much simpler + +template +Future Scheduler::schedule_future(Duration period, F &&f_) +{ + auto v = PromiseOfFutures::generate(); + + auto execute = Promise(); + v->add( + execute.get_future().then( + [v_=weak(v), f=std::move(f_)](auto &&) { + if (auto v = strong(v_)) + { + v->add( + f().then( + [v_](auto &&r) { + if (auto v = strong(v_)) + v->consume(r); + } + ) + ); + } + } + ) + ); + + auto event = schedule( + period, + false, + [execute]() { + auto execute_ = execute; + execute_.set_value(); + } + ); + + return v->get_future().then([event](auto &&f) { return f.get(); }); +} + + +template +Future Scheduler::schedule(F &&f) +{ + return schedule(Immediate, std::forward(f)); +} + +template +Future Scheduler::schedule_future(F &&f) +{ + return schedule_future(Immediate, std::forward(f)); +} + +template +void Scheduler::run(F &&f) +{ + return schedule(std::forward(f)).wait(); +} + +} // namespace diff --git a/tjp/core/time-obsolete/SchedulerManual.cpp b/tjp/core/time-obsolete/SchedulerManual.cpp new file mode 100644 index 0000000..86c84fc --- /dev/null +++ b/tjp/core/time-obsolete/SchedulerManual.cpp @@ -0,0 +1,229 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#include "SchedulerManual.hpp" + +#include + +namespace timprepscius { +namespace core { +namespace time { + +SchedulerManual::SchedulerManual(const StrongPtr &timeSource_) : + timeSource (timeSource_), + running(false), + stopped(false), + nextIndex(1) +{ +} + +SchedulerManual::~SchedulerManual() +{ + debug_assert(!running); +} + +SchedulerManual::EventImp::EventImp( + SchedulerManual *parent_, + Time period_, + bool repeat_, + std::function &&f_ +) : + parent(parent_), + period(period_), + repeat(repeat_), + f(f_) +{ +} + +SchedulerEventPtr SchedulerManual::schedule_no_lock (const StrongPtr &eventImp, Time now) +{ + debug_assert(mutex.locked_by_caller()); + + eventImp->at = { now + eventImp->period, nextIndex++ }; + + sLogDebug ("core::time::SchedulerManual", logVar(ptr_of(eventImp)) << logVar(now) << logVar(eventImp->at.time) << logVar(eventImp->period) << logVar(eventImp->repeat)); + + auto insertion = events.emplace(eventImp->at, weak(eventImp)); + debug_assert(insertion.second); + debug_assert(eventImp->position == events.end()); + + eventImp->position = insertion.first; + changed.notify_all(); + return eventImp; +} + +SchedulerEventPtr SchedulerManual::schedule (const StrongPtr &eventImp, Time now) +{ + auto l = lock_of(mutex); + return schedule_no_lock(eventImp, now); +} + +SchedulerEventPtr SchedulerManual::schedule (Time period, bool repeat, std::function &&f) +{ + auto eventImp = strong( + this, + period, + repeat, + std::move(f) + ); + + eventImp->position = events.end(); + + schedule(eventImp, timeSource->getTime()); + return eventImp; +} + +void SchedulerManual::EventImp::cancel () +{ + auto lock = lock_of(mutex); + parent->cancel(this); +} + +void SchedulerManual::cancel(EventImp *event) +{ + sLogDebug("core::time::scheduler", logVar(event)); + + if (event) + { + auto l = lock_of(mutex); + + if (event->position != events.end()) + events.erase(event->position); + + event->position = events.end(); + changed.notify_all(); + } +} + +Duration SchedulerManual::durationUntilNextEvent() +{ + debug_assert(mutex.locked_by_caller()); + + if (events.empty()) + return time::FarFuture; + + return events.begin()->first.time - timeSource->getTime(); +} + +void SchedulerManual::update_has_lock () +{ + auto currentTime = EventTime { timeSource->getTime(), nextIndex }; + + while (!events.empty()) + { + if (stopped) + return; + + auto position = events.begin(); + auto &event_ = position->second; + + if (position->first < currentTime) + { + auto event = strong(event_); + + debug_assert(mutex.locked_by_caller()); + events.erase(position); + + if (event) + { + event->position = events.end(); + + if (event->repeat) + { + schedule_no_lock(event, currentTime.time); + } + } + + mutex.unlock(); + + if (event) + { + auto lock = lock_of(event->mutex); + + try + { + event->f(); + } + catch (Exception &e) + { + sLogError("core::time::SchedulerManual", "there was a problem houston " << e.what()); + } + } + } + else + { + break; + } + + mutex.lock(); + } +}; + +void SchedulerManual::run() +{ + running = true; + stopped = false; + + mutex.lock(); + while (!stopped) + { + auto duration = std::chrono::duration(toReal(durationUntilNextEvent())); + auto now = std::chrono::system_clock::now(); + auto then = now + duration; + changed.wait_until(mutex, then); + + update_has_lock(); + } + + running = false; + stopped_.notify_all(); + + mutex.unlock(); +} + +void SchedulerManual::cycle() +{ + mutex.lock(); + running = true; + + if (!events.empty()) + { + update_has_lock(); + } + + running = false; + mutex.unlock(); +} + +void SchedulerManual::stop () +{ + auto lock = lock_of(mutex); + + stopped = true; + changed.notify_all(); +} + +void SchedulerManual::clear () +{ + auto l = lock_of(mutex); + for (auto &[id, event_]: events) + { + if (auto event = strong(event_)) + { + event->position = events.end(); + } + } + + events.clear(); +} + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/time-obsolete/SchedulerManual.h b/tjp/core/time-obsolete/SchedulerManual.h new file mode 100644 index 0000000..2cfd33c --- /dev/null +++ b/tjp/core/time-obsolete/SchedulerManual.h @@ -0,0 +1,21 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#pragma once + +namespace timprepscius { +namespace core { +namespace time { + +struct SchedulerManual; + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/time-obsolete/SchedulerManual.hpp b/tjp/core/time-obsolete/SchedulerManual.hpp new file mode 100644 index 0000000..c6db176 --- /dev/null +++ b/tjp/core/time-obsolete/SchedulerManual.hpp @@ -0,0 +1,121 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#pragma once + +#include "Scheduler.hpp" + +#include +#include +#include +#include +#include +#include + +#include "TimeSource.hpp" + +#include +#include +#include + +namespace timprepscius { +namespace core { +namespace time { + +struct SchedulerManual : Scheduler +{ + struct EventImp; + + struct EventTime + { + Time time; + u64 index; + + bool operator<(const EventTime &rhs) const + { + if (time == rhs.time) + return index < rhs.index; + + return time < rhs.time; + } + + bool operator==(const EventTime &rhs) const + { + return time == rhs.time && index == rhs.index; + } + + } ; + + typedef WeakPtr WeakEventPtr; + + typedef core::MapAllocator Allocator; + typedef Map,Allocator> Events; + +// typedef Map Events; + + struct EventImp : SchedulerEvent + { + SchedulerManual *parent; + + Time period; + bool repeat; + std::function f; + EventTime at; + + RecursiveMutex mutex; + Events::iterator position; + + EventImp( + SchedulerManual *parent, + Time period, + bool repeat, + std::function &&f + ); + + bool operator<(const EventImp &rhs) const + { + return at < rhs.at; + } + + void cancel () override; + } ; + + + Atomic nextIndex; + StrongPtr timeSource; + Mutex mutex; + Events events; + + SchedulerManual(const StrongPtr &timeSource_); + ~SchedulerManual(); + + SchedulerEventPtr schedule (Time period, bool repeat, std::function &&f) override; + SchedulerEventPtr schedule (const StrongPtr &event, Time now); + SchedulerEventPtr schedule_no_lock (const StrongPtr &event, Time now); + + void cancel(EventImp *event); + Time durationUntilNextEvent(); + + Event changed; + void update_has_lock(); + + bool running, stopped; + Event stopped_; + + void run(); + void cycle(); + void stop(); + + void clear(); +}; + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/time-obsolete/SchedulerPool.cpp b/tjp/core/time-obsolete/SchedulerPool.cpp new file mode 100644 index 0000000..94ff3e6 --- /dev/null +++ b/tjp/core/time-obsolete/SchedulerPool.cpp @@ -0,0 +1,101 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#include "SchedulerPool.hpp" + +namespace timprepscius { +namespace core { +namespace time { + +SchedulerPool::SchedulerPool () : + nextIndex(0) +{ +} + +SchedulerPool::~SchedulerPool () +{ + clear(); +} + +SchedulerEventPtr SchedulerPool::schedule(const SchedulerPtr &scheduler, Time period, std::function &&f) +{ + auto event = scheduler->schedule(period, true, std::move(f)); + + auto lock = lock_of(mutex); + events.insert(event); + + return event; +} + +SchedulerEventPtr SchedulerPool::once(const SchedulerPtr &scheduler, Time period, std::function &&f) +{ + auto index = nextIndex++; + + auto weak = weak_this(this); + + // I'm locking before the schedule because, very rarely, the scheduler runs the event + // before I can put it in the map, which causes it to stay in the map forever. + // If this causes some locking problem in the future, I could also + // allocate it in the map, then schedule, and then finally, if the map still + // has the entry for it, set it - if not discard. + auto lock = lock_of(mutex); + + auto event = scheduler->schedule( + period, + false, + [this, weak, f=std::move(f), index]() { + if (auto that = strong(weak)) + { + remove_once(index); + f(); + } + } + ); + + onces.emplace(index, event); + + sLogDebug("core::time::SchedulerPool", logOfThis(this) << logVar(index) << logVar(onces.size()) << logVar(period) << logVar(event)); + + return event; +} + +void SchedulerPool::remove_once_no_lock(size_t index) +{ + debug_assert(mutex.locked_by_caller()); + + auto i = onces.find(index); + if (i != onces.end()) + { + sLogDebug("core::time::SchedulerPool", logOfThis(this) << logVar(index) << logVar(onces.size()) << logVar(i->second)); + + onces.erase(i); + } + else + { + sLogDebug("core::time::SchedulerPool", logOfThis(this) << logVar(index) << logVar(onces.size()) << logLabel("failed")); + } +} + +void SchedulerPool::remove_once(size_t index) +{ + auto lock = lock_of(mutex); + remove_once_no_lock(index); +} + +void SchedulerPool::clear () +{ + auto lock = lock_of(mutex); + onces.clear(); + events.clear(); +} + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/time-obsolete/SchedulerPool.h b/tjp/core/time-obsolete/SchedulerPool.h new file mode 100644 index 0000000..3c5481c --- /dev/null +++ b/tjp/core/time-obsolete/SchedulerPool.h @@ -0,0 +1,21 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#pragma once + +namespace timprepscius { +namespace core { +namespace time { + +struct SchedulerPool; + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/time-obsolete/SchedulerPool.hpp b/tjp/core/time-obsolete/SchedulerPool.hpp new file mode 100644 index 0000000..ee71555 --- /dev/null +++ b/tjp/core/time-obsolete/SchedulerPool.hpp @@ -0,0 +1,47 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#pragma once + +#include "SchedulerPool.h" +#include "Scheduler.hpp" +#include +#include +#include +#include + +namespace timprepscius { +namespace core { +namespace time { + +struct SchedulerPool : StrongThis +{ + QMutex mutex; + std::set events; + + std::atomic nextIndex; + std::map onces; + SchedulerPtr scheduler; + + SchedulerPool(); + virtual ~SchedulerPool (); + + SchedulerEventPtr schedule(const SchedulerPtr &scheduler, Time period, std::function &&f); + + SchedulerEventPtr once(const SchedulerPtr &scheduler, Time offset, std::function &&f); + void remove_once(size_t index); + void remove_once_no_lock(size_t index); + + void clear(); +} ; + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/time-obsolete/Time+Test.cpp b/tjp/core/time-obsolete/Time+Test.cpp new file mode 100644 index 0000000..88cc1fa --- /dev/null +++ b/tjp/core/time-obsolete/Time+Test.cpp @@ -0,0 +1,50 @@ +// TJP COPYRIGHT HEADER + +/* + * Author: Timothy Prepscius + * Copyright: See copyright located at COPYRIGHTLOCATION + */ + +#include "Time.h" + +#include +#include + +#include + +namespace timprepscius::core::time { + +SCENARIO("time::Time") +{ + GIVEN("a time source") + { + auto t0 = v1::now(); + + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + auto t1 = v1::now(); + + auto dt = t1 - t0; + auto mL = 15 * v1::Millisecond; + auto mR = 25 * v1::Millisecond; + + auto between = mL < dt && dt < mR; + REQUIRE(between); + } + + GIVEN("a time source") + { + auto t0 = v2::now(); + + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + auto t1 = v2::now(); + + auto dt = t1 - t0; + auto mL = 15 * v2::Millisecond; + auto mR = 25 * v2::Millisecond; + + auto between = mL < dt && dt < mR; + REQUIRE(between); + } +} + +} // namespace diff --git a/tjp/core/time-obsolete/Time.h b/tjp/core/time-obsolete/Time.h new file mode 100644 index 0000000..d08008b --- /dev/null +++ b/tjp/core/time-obsolete/Time.h @@ -0,0 +1,20 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#pragma once + +#include "Time_v1.h" +#include "Time_v2.h" + +namespace timprepscius::core::time { + +using namespace v1; + +} diff --git a/tjp/core/time-obsolete/TimeSource+Test.cpp b/tjp/core/time-obsolete/TimeSource+Test.cpp new file mode 100644 index 0000000..2b64708 --- /dev/null +++ b/tjp/core/time-obsolete/TimeSource+Test.cpp @@ -0,0 +1,39 @@ +// TJP COPYRIGHT HEADER + +/* + * Author: Timothy Prepscius + * Copyright: See copyright located at COPYRIGHTLOCATION + */ + +#include "TimeSource.hpp" +#include "Time.h" + +#include +#include + +#include + +namespace timprepscius::core::time { + +SCENARIO("time::TimeSource") +{ + GIVEN("a time source") + { + auto ts = new TimeSourceStandard(); + + auto t0 = ts->getTime(); + + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + auto t1 = ts->getTime(); + + auto dt = t1 - t0; + auto mL = 15 * Millisecond; + auto mR = 25 * Millisecond; + + auto between = mL < dt && dt < mR; + REQUIRE(between); + } + +} + +} // namespace diff --git a/tjp/core/time-obsolete/TimeSource.h b/tjp/core/time-obsolete/TimeSource.h new file mode 100644 index 0000000..3e876f5 --- /dev/null +++ b/tjp/core/time-obsolete/TimeSource.h @@ -0,0 +1,32 @@ +// TJP COPYRIGHT HEADER + +// +// Server.hpp +// TimeSync +// +// Created by Timothy Prepscius on 1/27/17. +// Copyright © 2017 Timothy Prepscius. All rights reserved. +// + +#pragma once + +namespace timprepscius { +namespace core { +namespace time { + +class TimeSource; + +template +class TimeSourceResolution; + +template +class TimeSourceResolutionStandard; + +class ManualTimeSource; + +class TimeSourceStandard; + +} // namespace +} // namespace +} // namespace + diff --git a/tjp/core/time-obsolete/TimeSource.hpp b/tjp/core/time-obsolete/TimeSource.hpp new file mode 100644 index 0000000..cc03e49 --- /dev/null +++ b/tjp/core/time-obsolete/TimeSource.hpp @@ -0,0 +1,129 @@ +// TJP COPYRIGHT HEADER + +// +// Server.hpp +// TimeSync +// +// Created by Timothy Prepscius on 1/27/17. +// Copyright © 2017 Timothy Prepscius. All rights reserved. +// + +#pragma once + +#include "TimeSource.h" +#include "Time.h" +#include + +namespace timprepscius { +namespace core { +namespace time { + +class TimeSource +{ +public: + virtual ~TimeSource () {} + + virtual Time getTime () = 0; + virtual size_t getTimeTicks () = 0; + virtual Time getTimeAt (size_t ticks) = 0; +} ; + +template +class TimeSourceResolution : public TimeSource +{ +public: + typedef Resolution resolution; + + Time getTime () override + { + return getTimeAt(getTimeTicks()); + } + + Time getTimeAt (size_t ticks) override + { + auto denominator = Resolution::period::den; + if (time::Denominator == 1) + return toTime((time::V)ticks / denominator); + + const auto d0 = time::Denominator; + const auto d1 = denominator; + + if (d0 > d1) + { + V r = d0 / d1; + return toTime( (time::V)ticks * r ); + } + else + { + V r = d1 / d0; + return toTime( (time::V)ticks / r ); + } + } +} ; + +template +class TimeSourceResolutionStandard : public TimeSourceResolution +{ +public: + typedef clock_type_t clock_type; + +protected: + clock_type clock; + typename clock_type::time_point startTime; + +public: + TimeSourceResolutionStandard () + { + startTime = clock.now(); + } + + size_t getTimeTicks () override + { + return std::chrono::duration_cast(clock.now() - startTime).count() + 0; + } +} ; + +class ManualTimeSource : public TimeSource +{ +protected: + TimeSource *source; + size_t ticks; + +public: + ManualTimeSource(TimeSource *source_) : + source(source_) + { + ticks = source_->getTimeTicks(); + } + + size_t getTimeTicks () override + { + return ticks; + } + + Time getTime () override + { + return source->getTimeAt(ticks); + } + + Time getTimeAt (size_t ticks) override + { + return source->getTimeAt(ticks); + } + + void update () + { + ticks = source->getTimeTicks(); + } +} ; + +class TimeSourceStandard : public TimeSourceResolutionStandard +{ +public: + TimeSourceStandard() {} +} ; + +} // namespace +} // namespace +} // namespace + diff --git a/tjp/core/time-obsolete/Time_v1.cpp b/tjp/core/time-obsolete/Time_v1.cpp new file mode 100644 index 0000000..d28e728 --- /dev/null +++ b/tjp/core/time-obsolete/Time_v1.cpp @@ -0,0 +1,140 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#include "Time_v1.h" +#include +#include +#include +#include +#include +#include + +namespace timprepscius::core::time::v1 { + +Time now() +{ + return getSecondsSince1970(); +} + +Real toReal(Time v) +{ + return v; +} + +Time toTime(Real v) +{ + return v; +} + +size_t getMillisecondsSince1970_count() +{ + auto t = std::chrono::duration_cast< std::chrono::milliseconds > (std::chrono::system_clock::now().time_since_epoch()); + return t.count(); +} + +size_t getSecondsSince1970_count() +{ + auto t = std::chrono::duration_cast< std::chrono::seconds > (std::chrono::system_clock::now().time_since_epoch()); + return t.count(); +} + + +double getSecondsSince1970 () +{ + auto t = std::chrono::duration_cast< std::chrono::milliseconds > ( + std::chrono::system_clock::now().time_since_epoch() + ); + + return t.count() / 1000.0; +} + +double getSecondsSince1970_UTC () +{ + return getSecondsSince1970(); + /* + std::chrono::system_clock clock; + auto now = clock.now(); + auto timenow = clock.to_time_t(now); + + + time_t t = ::time(NULL); + + std::tm tm; + struct timeval now; + struct timezone zone; + + std::memset(&tm, 0, sizeof(tm)); + std::memset(&now, 0, sizeof(now)); + std::memset(&zone, 0, sizeof(zone)); + + ::gmtime_r(&t, &tm); + gettimeofday(&now, &zone); + + auto secondsAtNow = ::mktime(&tm); + auto partialSeconds = ((double)now.tv_usec)/(1000 * 1000); + + return (double)secondsAtNow + partialSeconds; + */ +} + +double getUTCFromLocal(double time) +{ + // https://stackoverflow.com/questions/38686405/convert-time-t-from-localtime-zone-to-utc + + std::time_t lt = time; + + std::tm tm; + std::memset(&tm, 0, sizeof(tm)); + + ::gmtime_r(<, &tm); + tm.tm_isdst = -1; + auto utc = std::mktime(&tm); + + double iptr; + + auto result = utc + modf(time, &iptr); + + return result; +} + + +// https://arstechnica.com/civis/viewtopic.php?t=136625 +size_t getLocalFromUTCOffset () +{ + time_t currtime; + + struct tm * timeinfo; + + ::time ( &currtime ); + + timeinfo = gmtime ( &currtime ); + + time_t utc = mktime( timeinfo ); + + timeinfo = localtime ( &currtime ); + + time_t local = mktime( timeinfo ); + + // Get offset in hours from UTC + size_t HOUR_IN_SECONDS = 60 *60; + + double offsetFromUTC = difftime(utc, local); + + // Adjust for DST + + if (timeinfo->tm_isdst) + { + offsetFromUTC -= 1 * HOUR_IN_SECONDS; + } + + return offsetFromUTC; +} + +} // namespace diff --git a/tjp/core/time-obsolete/Time_v1.h b/tjp/core/time-obsolete/Time_v1.h new file mode 100644 index 0000000..7f781e3 --- /dev/null +++ b/tjp/core/time-obsolete/Time_v1.h @@ -0,0 +1,45 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#pragma once + +#include + +namespace timprepscius::core::time::v1 { + +using V = double; +using Time = V; +using Duration = Time; + +const V Denominator = 1.0; +using Real = double; + +const Time Zero = 0; +const Duration FarFuture = 10000000000.0; +const Duration Immediate = 0; + +const Duration Second = 1.0; +const Duration Decisecond = Second / 10; +const Duration Centisecond = Second / 100; +const Duration Millisecond = Second / 1000; + +size_t getMillisecondsSince1970_count(); +size_t getSecondsSince1970_count(); +double getSecondsSince1970 (); +double getSecondsSince1970_UTC (); +double getUTCFromLocal(double time); + +size_t getLocalFromUTCOffset (); + +Time now(); +Real toReal(Time t); +Time toTime(V v); + +} // namespace diff --git a/tjp/core/time-obsolete/Time_v2.cpp b/tjp/core/time-obsolete/Time_v2.cpp new file mode 100644 index 0000000..b9bee21 --- /dev/null +++ b/tjp/core/time-obsolete/Time_v2.cpp @@ -0,0 +1,51 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#include "Time_v2.h" + +#include +#include +#include + +namespace timprepscius::core::time::v2 { + +const Duration Millisecond = { 1000 }; +const Duration Centisecond = { 10 * Millisecond }; +const Duration Decisecond = { 100 * Millisecond }; +const Duration Second = { 1000 * Millisecond }; +const V Denominator = Second.v; + +const Time FarFuture = { std::numeric_limits::max() }; + +const Duration Immediate = { 0 }; +const Time Zero = { 0 }; + +std::ostream &operator <<(std::ostream &o, const Duration &v) +{ + return o << v.v; +} + +Time now() +{ + auto t = std::chrono::duration_cast< std::chrono::milliseconds > (std::chrono::system_clock::now().time_since_epoch()); + return (size_t)t.count() * Millisecond; +} + +double toReal(Time time) +{ + return (double)time.v / Second.v; +} + +Time toTime(V v) +{ + return { v }; +} + +} // namespace diff --git a/tjp/core/time-obsolete/Time_v2.h b/tjp/core/time-obsolete/Time_v2.h new file mode 100644 index 0000000..1bd9d67 --- /dev/null +++ b/tjp/core/time-obsolete/Time_v2.h @@ -0,0 +1,61 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#pragma once + +#include +#include + +namespace timprepscius::core::time::v2 { + +using V = s64; + +struct Duration { + V v; +} ; + +using Time = Duration; +extern const V Denominator; + +extern const Duration Second; +extern const Duration Decisecond; +extern const Duration Centisecond; +extern const Duration Millisecond; + +extern const Time FarFuture; +extern const Duration Immediate; +extern const Time Zero; + +Duration operator *(const Duration &lhs, const Duration &rhs); +Duration operator /(const Duration &lhs, const Duration &rhs); +Duration operator +(const Duration &lhs, const Duration &rhs); +Duration operator -(const Duration &lhs, const Duration &rhs); + +bool operator >(const Duration &lhs, const Duration &rhs); +bool operator <(const Duration &lhs, const Duration &rhs); +bool operator ==(const Duration &lhs, const Duration &rhs); +bool operator !=(const Duration &lhs, const Duration &rhs); + +Duration operator *(int, const Duration &rhs); +Duration operator *(size_t, const Duration &rhs); +Duration operator *(double, const Duration &rhs); +Duration operator /(const Duration &rhs, double); +Duration operator /(const Duration &rhs, int); +Duration operator /(const Duration &rhs, size_t); + +std::ostream &operator <<(std::ostream &o, const Duration &v); + +Time now(); +double toReal(Time); +Time toTime(V v); + +} // namespace + +#include "Time_v2.inl" diff --git a/tjp/core/time-obsolete/Time_v2.inl b/tjp/core/time-obsolete/Time_v2.inl new file mode 100644 index 0000000..d4ea7ff --- /dev/null +++ b/tjp/core/time-obsolete/Time_v2.inl @@ -0,0 +1,114 @@ +// TJP COPYRIGHT HEADER + +// +// Timer.h +// algorithm +// +// Created by Timothy Prepscius on 1/19/18. +// Copyright © 2018 Timothy Prepscius. All rights reserved. +// + +#pragma once + +#include "Time_v2.h" + +namespace timprepscius::core::time::v2 { + +inline +Duration operator *(const Duration &lhs, const Duration &rhs) +{ + return { lhs.v * rhs.v }; +} + +inline +Duration operator /(const Duration &lhs, const Duration &rhs) +{ + return { lhs.v / rhs.v }; +} + +inline +Duration operator +(const Duration &lhs, const Duration &rhs) +{ + return { lhs.v + rhs.v }; +} + +inline +Duration operator -(const Duration &lhs, const Duration &rhs) +{ + return { lhs.v - rhs.v }; +} + +inline +bool operator >(const Duration &lhs, const Duration &rhs) +{ + return lhs.v > rhs.v; +} + +inline +bool operator <(const Duration &lhs, const Duration &rhs) +{ + return lhs.v < rhs.v; +} + +inline +bool operator ==(const Duration &lhs, const Duration &rhs) +{ + return lhs.v == rhs.v; +} + +inline +bool operator !=(const Duration &lhs, const Duration &rhs) +{ + return lhs.v != rhs.v; +} + +template +Duration mul(T lhs, const Duration &rhs) +{ + return { (V)(lhs * rhs.v) }; +} + +template +Duration dev(const Duration &lhs, T t) +{ + return { (V)(lhs.v / t) }; +} + +inline +Duration operator *(int lhs, const Duration &rhs) +{ + return mul(lhs, rhs); +} + +inline +Duration operator *(size_t lhs, const Duration &rhs) +{ + return mul(lhs, rhs); +} + +inline +Duration operator *(double lhs, const Duration &rhs) +{ + return mul(lhs, rhs); +} + +inline +Duration operator /(const Duration &lhs, double rhs) +{ + return dev(lhs, rhs); +} + +inline +Duration operator /(const Duration &lhs, int rhs) +{ + return dev(lhs, rhs); +} + +inline +Duration operator /(const Duration &lhs, size_t rhs) +{ + return dev(lhs, rhs); +} + + +} // namespace diff --git a/tjp/core/url/URL.cpp b/tjp/core/url/URL.cpp new file mode 100755 index 0000000..92eca6f --- /dev/null +++ b/tjp/core/url/URL.cpp @@ -0,0 +1,155 @@ +// TJP COPYRIGHT HEADER + +#include "URL.h" +#include +#include +#include +#include +#include + +namespace tjp::core::url { + +const char S = '/'; +const char D = '#'; +const char P = ':'; + +URL make(const Domain &domain) +{ + debug_assert(domain.find(D)==-1); + debug_assert(domain.find(S)==-1); + + return domain + D; +} + +IP getIP(const URL &url) +{ + auto p = url.find(P); + if (p == -1) + return {}; + + return url.substr(0,p); +} + +Address getAddress(const URL &url) +{ + auto d = url.find(D); + if (d == -1) + return {}; + + return url.substr(0,d); +} + +Domain getDomain(const URL &url) +{ + auto d = url.find(D); + return url.substr(0,d+1); +} + +Path getPath(const URL &url) +{ + return url.substr(url.find(D) + 1); +} + +URL push_back(const URL &lhs, const URL &rhs) +{ + debug_assert(rhs.find(D)==-1); + + if (rhs.empty()) + return lhs; + + auto domain = getDomain(lhs); + auto path0 = getPath(lhs); + auto path1 = getPath(rhs); + + auto s0 = path0.empty() ? "" : (path1.empty() ? "" : "/"); + + return domain + path0 + s0 + path1; +} + +URL pop_back_(const URL &url) +{ + int i; + + for (i=(int)url.size()-1; i>0; --i) + if (is_in(url[i], D, S)) + break; + + if (i < 0) + return {}; + + if (url[i] == D) + return url.substr(0,i+1); + + return url.substr(0,i); +} + +URL pop_front_(const URL &url) +{ + int i; + + for (i=0; i= url.size()) + return {}; + + return url.substr(i+1); +} + +URL pop_back(const URL &url_, size_t num) +{ + auto url = url_; + for (auto i : range(num)) + { + unused(i); + url = pop_back_(url); + } + + return url; +} + +URL pop_front(const URL &url_, size_t num) +{ + auto url = url_; + for (auto i : range(num)) + { + unused(i); + url = pop_front_(url); + } + + return url; +} + +URL back(const URL &url) +{ + int i; + + for (i=(int)url.size()-1; i>=0; --i) + { + if (is_in(url[i], D, S)) + break; + } + + return url.substr(i+1); +} + +URL front(const URL &url) +{ + auto path = getPath(url); + + int i; + for (i=0; i + +namespace tjp { +namespace core { + +namespace url { + +using URL = std::string; +using Domain = std::string; +using Path = std::string; +using Part = std::string; +using Address = std::string; +using IP = std::string; + +URL make(const Domain &domain); + +template +URL make(const Domain &domain, Args&& ...args); + +IP getIP(const URL &url); +Address getAddress(const URL &url); +Domain getDomain(const URL &url); +Path getPath(const URL &url); + +URL pop_front(const URL &, size_t num=1); +URL pop_back(const URL &url, size_t num=1); + +URL push_front(const URL &lhs, const URL &rhs); +URL push_back(const URL &url, const URL &rhs); + +template +URL push_back(const URL &url, const URL &rhs, Args&& ...args); + +template +URL push_front(const URL &url, const URL &rhs, Args&& ...args); + +URL back(const URL &url); +URL front(const URL &url); + +bool hasPath(const URL &url); + +} // namespace + +using URL = url::URL; + +} // namespace +} // namespace + +#include "URL.inl" diff --git a/tjp/core/url/URL.inl b/tjp/core/url/URL.inl new file mode 100755 index 0000000..15676f5 --- /dev/null +++ b/tjp/core/url/URL.inl @@ -0,0 +1,32 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "URL.h" + +namespace tjp { +namespace core { + +namespace url { + +template +URL make(const Domain &domain, Args&& ...args) +{ + return push_back( + make(domain), + std::forward(args)... + ); +} + +template +URL push_back(const URL &url, const URL &rhs, Args&& ...args) +{ + return push_back( + push_back(url, rhs), + std::forward(args)... + ); +} + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/url/_tests/URL.cpp b/tjp/core/url/_tests/URL.cpp new file mode 100755 index 0000000..8868c38 --- /dev/null +++ b/tjp/core/url/_tests/URL.cpp @@ -0,0 +1,110 @@ +// TJP COPYRIGHT HEADER + +#include +#include + +namespace tjp { +namespace core { +namespace url { + +SCENARIO("url tests" ) +{ + GIVEN("url with just a domain") + { + Address address = "address"; + Path empty; + URL domain = url::make(address); + auto url0 = domain; + + REQUIRE(getAddress(url0) == address); + REQUIRE(getDomain(url0) == domain); + + WHEN("adding a path") + { + Path path1 = "path1"; + URL url1 = push_back(url0, path1); + REQUIRE(getPath(url1) == path1); + REQUIRE(getDomain(url1) == domain); + REQUIRE(getAddress(url1) == address); + + Path path2 = "path2"; + URL url2 = push_back(url1, path2); + REQUIRE(getPath(url2) == push_back(path1, path2)); + REQUIRE(getDomain(url2) == domain); + REQUIRE(getAddress(url2) == address); + } + + WHEN("back a path") + { + auto path = back(url0); + REQUIRE(path == empty); + } + } + + GIVEN("url") + { + Address address = "address"; + URL domain = make(address); + Path path1 = "path1"; + auto url0 = domain; + URL url1 = make(address, path1); + + REQUIRE(getDomain(domain) == domain); + REQUIRE(getPath(url1) == path1); + + WHEN("adding a path") + { + Path path2 = "path2"; + URL url2 = push_back(url1, path2); + REQUIRE(getPath(url2) == push_back(path1, path2)); + REQUIRE(getDomain(url2) == domain); + REQUIRE(getAddress(url2) == address); + + Path path3 = "path3"; + URL url3 = push_back(url2, path3); + REQUIRE(getPath(url3) == push_back(path1, push_back(path2, path3))); + REQUIRE(getDomain(url3) == domain); + REQUIRE(getAddress(url3) == address); + } + + WHEN("back a path") + { + auto path1_ = back(url1); + REQUIRE(path1_ == path1); + } + } + + GIVEN("url") + { + Address address = "address"; + Domain domain = make(address); + Path path1 = "path1"; + Path path2 = "path2"; + auto url0 = domain; + auto url2 = push_back(push_back(url0, path1), path2); + + REQUIRE(getDomain(url2) == domain); + REQUIRE(getPath(url2) == push_back + (path1, path2)); + + WHEN("popping a path") + { + auto url1 = pop_back(url2); + REQUIRE(url1 == make(address, path1)); + + auto url0 = pop_back(url1); + REQUIRE(url0 == make(address)); + } + + WHEN("back a path") + { + auto path2_ = back(url2); + REQUIRE(path2 == path2_); + } + + } +} + +} // namespace +} // namespace +} // namespace diff --git a/tjp/core/zip/Compress+zlib.cpp b/tjp/core/zip/Compress+zlib.cpp new file mode 100755 index 0000000..b72c6b1 --- /dev/null +++ b/tjp/core/zip/Compress+zlib.cpp @@ -0,0 +1,425 @@ +// TJP COPYRIGHT HEADER + + +#include +#include +#include +#include +#include +#include +#include + +#ifndef WIN32 +# include +# include +# include +# include +#else +# include +# include +#endif + +#include +#include "minizip/zip.h" + +#ifdef WIN32 +#include "minizip/iowin32.h" +#endif + +#include +#include +#include +#include "Compress.h" +#include "../file/File.h" +#include "../file/Path.h" + +#include "../endian/Endian.h" +#include "MemoryBuffer.hpp" + +#include +#include + +#define WRITEBUFFERSIZE (16384) +#define MAXFILENAME (256) + +using namespace tjp; +using namespace core::zip; +using namespace core; + + +#ifdef WIN32 +uLong filetime(const std::string &f, tm_zip *tmzip, uLong *dt) +{ + int ret = -1; + { + FILETIME ftLocal; + HANDLE hFind; + WIN32_FIND_DATA ff32; + + hFind = FindFirstFile(f.c_str(),&ff32); + if (hFind != INVALID_HANDLE_VALUE) + { + FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal); + FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0); + FindClose(hFind); + ret = 0; + } + } + return ret; +} +#else +uLong filetime(const char *f, tm_zip *tmzip, uLong *dt) +{ + struct stat s; /* results of stat() */ + struct tm* filedate; + time_t tm_t=0; + + if (strcmp(f,"-")!=0) + { + char name[MAXFILENAME+1]; + auto len = strlen(f); + if (len > MAXFILENAME) + len = MAXFILENAME; + + strncpy(name, f,MAXFILENAME-1); + /* strncpy doesnt append the trailing NULL, of the string is too long. */ + name[ MAXFILENAME ] = '\0'; + + if (name[len - 1] == '/') + name[len - 1] = '\0'; + /* not all systems allow stat'ing a file with / appended */ + if (stat(name,&s)==0) + { + tm_t = s.st_mtime; + } + else + return 1; + } + filedate = localtime(&tm_t); + + tmzip->tm_sec = filedate->tm_sec; + tmzip->tm_min = filedate->tm_min; + tmzip->tm_hour = filedate->tm_hour; + tmzip->tm_mday = filedate->tm_mday; + tmzip->tm_mon = filedate->tm_mon ; + tmzip->tm_year = filedate->tm_year; + + return 0; +} +#endif + + +/* calculate the CRC32 of a file, + because to encrypt a file, we need known the CRC32 of the file before */ +int getFileCrc(const std::string &filenameinzip,unsigned long &result_crc) +{ + const int size_buf = 16384; + char *buf = new char[size_buf]; + + uLong calculate_crc=0; + int err=ZIP_OK; + FILE * fin = fopen(filenameinzip.c_str(),"rb"); + uInt size_read = 0; + unsigned long total_read = 0; + if (fin==NULL) + { + err = ZIP_ERRNO; + } + + if (err == ZIP_OK) + do + { + err = ZIP_OK; + size_read = (int)fread(buf,1,size_buf,fin); + if (size_read < size_buf) + if (feof(fin)==0) + { + err = ZIP_ERRNO; + } + + if (size_read>0) + calculate_crc = crc32(calculate_crc,(const Bytef *)buf,size_read); + total_read += size_read; + + } while ((err == ZIP_OK) && (size_read>0)); + + if (fin) + fclose(fin); + + result_crc=calculate_crc; + return err; +} + +void compressFile( + zipFile zf, + const std::string &path, + const std::string &filename, + const char *password, + int opt_compress_level=Z_DEFAULT_COMPRESSION +) +{ + std::string filenameinzip = path; + zip_fileinfo zi; + unsigned long crcFile=0; + + zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = + zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0; + zi.dosDate = 0; + zi.internal_fa = 0; + zi.external_fa = 0; + if (filetime(filenameinzip.c_str(),&zi.tmz_date,&zi.dosDate) != ZIP_OK) + throw zip::Exception ("filetime failed " + filenameinzip); + + if (password != NULL) + { + if (getFileCrc(filenameinzip,crcFile) != ZIP_OK) + throw zip::Exception ("getFileCrc"); + } + + std::string unixFileName = file::convertDelimiter(filename, file::LocalDirectoryDelimiter, file::ServerDirectoryDelimiter); + + if (zipOpenNewFileInZip3(zf,unixFileName.c_str(),&zi, + NULL,0,NULL,0,NULL /* comment*/, + (opt_compress_level != 0) ? Z_DEFLATED : 0, + opt_compress_level,0, + /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */ + -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, + password,crcFile) + != ZIP_OK) + throw zip::Exception ("zipOpenNewFileInZip"); + + const int buffer_size = 16384; + char *buffer = new char[buffer_size]; + + try + { + std::ifstream fin (filenameinzip.c_str(), std::ios::binary); + + if (!fin) + throw zip::Exception ("error opening file to compress"); + + int size_read; + + do + { + fin.read (buffer, buffer_size); + size_read = (int)fin.gcount(); + + if (size_read>0) + { + if (zipWriteInFileInZip (zf,buffer,size_read) < 0) + throw zip::Exception ("zipWriteInFileInZip"); + + } + + } while (fin); + + } + catch (zip::Exception &e) + { + delete[] buffer; + + zipCloseFileInZip(zf); + throw e; + } + + if (zipCloseFileInZip(zf) != ZIP_OK) + throw zip::Exception ("zipCloseFileInZip"); +} + +void zip::compress (const std::string &file, const std::string &path, const std::list &files) +{ + zipFile zf; + +#ifdef WIN32 + zlib_filefunc_def ffunc; + fill_win32_filefunc(&ffunc); + zf = zipOpen2(file.c_str(),0,NULL,&ffunc); +#else + zf = zipOpen(file.c_str(),0); +#endif + + if (!zf) + throw zip::Exception ("zipOpen"); + + std::list::const_iterator i; + for (i=files.begin(); i!=files.end(); ++i) + { + compressFile (zf, path + *i, *i, NULL); + } + + if (zipClose(zf,NULL) != ZIP_OK) + throw zip::Exception ("zipClose"); +} + +void zip::compress (const std::string &file, const std::map &zipFileNameToDiskFileName) +{ + zipFile zf; + +#ifdef WIN32 + zlib_filefunc_def ffunc; + fill_win32_filefunc(&ffunc); + zf = zipOpen2(file.c_str(),0,NULL,&ffunc); +#else + zf = zipOpen(file.c_str(),0); +#endif + + if (!zf) + throw zip::Exception ("zipOpen"); + + std::map::const_iterator i; + for (i=zipFileNameToDiskFileName.begin(); i!=zipFileNameToDiskFileName.end(); ++i) + { + compressFile (zf, i->second, i->first, NULL); + } + + if (zipClose(zf,NULL) != ZIP_OK) + throw zip::Exception ("zipClose"); +} + +void zip::compress (const std::string &from, const std::string &to) +{ + std::ifstream infile (from.c_str(), std::ios::binary); + + int size_buf = 16384; + char *buf = new char[16384]; + + try + { + file::ensurePath (to); + gzFile gz = gzopen (to.c_str(), "wb"); + + if (gz == NULL) + throw zip::Exception ("gzopen"); + + int read = 0; + while (infile) + { + infile.read (buf, size_buf); + read = (int)infile.gcount(); + + if (gzwrite (gz, buf, read)) + throw zip::Exception ("gzwrite"); + } + } + catch (zip::Exception &e) + { + delete[] buf; + file::deleteFile (to); + throw e; + } + +} + +typedef unsigned int Header; + +bool zip::compress (const char *in, int insize, char *&out, int &outsize, int level) +{ + uLongf ulInSize = insize; + uLongf ulOutSize = insize - sizeof(Header); + + char *block = new char[insize]; + Header *header = (Header *)block; + char *data = block + sizeof (Header); + + int error = ::compress2((Bytef*)data, &ulOutSize, (const Bytef*)in, ulInSize, level); + + if ((error!=Z_OK)) + { + delete[] block; + return false; + } + + *header = (Header)ulInSize; + endian::swap (*header); + + out = block; + outsize = int(ulOutSize + sizeof(Header)); + + //----------------------------------------------------- + /* + char *temp; + int tempsize; + decompress (out, outsize, temp, tempsize); + + assert (tempsize == insize); + assert (memcmp (in, temp, tempsize)==0); + + delete[] temp; + */ + //----------------------------------------------------- + + return true; +} + +bool zip::compress (const char *in, int insize, std::vector &out, int level) +{ + uLongf ulInSize = insize; + uLongf ulOutSize = insize - sizeof(Header); + + out.resize(insize); + char *block = out.data(); + Header *header = (Header *)block; + char *data = block + sizeof (Header); + + int error = ::compress2((Bytef*)data, &ulOutSize, (const Bytef*)in, ulInSize, level); + + if ((error!=Z_OK)) + { + delete[] block; + return false; + } + + *header = (Header)ulInSize; + endian::swap (*header); + + out.resize(int(ulOutSize + sizeof(Header))); + + //----------------------------------------------------- + /* + char *temp; + int tempsize; + decompress (out, outsize, temp, tempsize); + + assert (tempsize == insize); + assert (memcmp (in, temp, tempsize)==0); + + delete[] temp; + */ + //----------------------------------------------------- + + return true; +} + +std::vector zip::compress(const char *in, int insize, int level) +{ + std::vector result; + zip::compress(in, insize, result, level); + return result; +} + +Vector zip::compress (const std::string &path, const std::list &files) +{ + MemoryBuffer buffer({}); + + zipFile zf = zipOpen2("dummy.zip", APPEND_STATUS_CREATE, nullptr, &buffer.funcs); + if (!zf) + throw zip::Exception ("zipOpen"); + + std::list::const_iterator i; + for (i=files.begin(); i!=files.end(); ++i) + { + auto file = path + *i; + sLogDebug("debug", logVar(file)); + compressFile (zf, file, *i, NULL); + } + + if (zipClose(zf,NULL) != ZIP_OK) + throw zip::Exception ("zipClose"); + + return buffer.buffer; +} + + + + diff --git a/tjp/core/zip/Compress+zstd.cpp b/tjp/core/zip/Compress+zstd.cpp new file mode 100755 index 0000000..eb481b3 --- /dev/null +++ b/tjp/core/zip/Compress+zstd.cpp @@ -0,0 +1,56 @@ +// TJP COPYRIGHT HEADER + + +#include "Compress.h" +#include "Decompress.h" + +#include + +using namespace tjp; +using namespace core::zstd; +using namespace core; + +std::vector zstd::compress(const char *in, int insize, int level) +{ + // Get maximum compressed size + size_t maxCompressedSize = ZSTD_compressBound(insize); + + // Allocate output buffer + std::vector result(maxCompressedSize); + + // Compress the input + size_t compressedSize = ZSTD_compress(result.data(), maxCompressedSize, in, insize, level); + + // Check for compression error + if (ZSTD_isError(compressedSize)) { + result.clear(); + return result; + } + + // Resize result to actual compressed size + result.resize(compressedSize); + return result; +} + +std::vector zstd::decompress(const char *in, int insize) +{ + // Get decompressed size from the frame header + unsigned long long decompressedSize = ZSTD_getFrameContentSize(in, insize); + + if (decompressedSize == ZSTD_CONTENTSIZE_ERROR || decompressedSize == ZSTD_CONTENTSIZE_UNKNOWN) { + return {}; + } + + // Allocate output buffer + std::vector result(decompressedSize); + + // Decompress + size_t actualSize = ZSTD_decompress(result.data(), decompressedSize, in, insize); + + // Check for decompression error + if (ZSTD_isError(actualSize)) { + return {}; + } + + return result; +} diff --git a/tjp/core/zip/Compress.h b/tjp/core/zip/Compress.h new file mode 100755 index 0000000..0f48602 --- /dev/null +++ b/tjp/core/zip/Compress.h @@ -0,0 +1,40 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "Exception.h" +#include +#include +#include +#include + +namespace tjp { +namespace core { +namespace zip { + +/** + * decompresses the given file to the given path, sets the files to the set of files + * found + */ +void compress (const std::string &file, const std::string &path, const std::list &files); +void compress (const std::string &file, const std::map &zipFileNameToDiskFileName); +void compress (const std::string &from, const std::string &to); + +Vector compress (const std::string &path, const std::list &files); + +bool compress (const char *from, int fromSize, char *&to, int &toSize, int level=9); + +bool compress (const char *from, int fromSize, std::vector &to, int level=9); + +std::vector compress (const char *from, int fromSize, int level=9); + +} // namespace + +namespace zstd { + +std::vector compress (const char *from, int fromSize, int level=9); + +} + +} // +} // diff --git a/tjp/core/zip/Decompress.cpp b/tjp/core/zip/Decompress.cpp new file mode 100755 index 0000000..421a2ff --- /dev/null +++ b/tjp/core/zip/Decompress.cpp @@ -0,0 +1,276 @@ +// TJP COPYRIGHT HEADER + + +#include "Decompress.h" +#include +#include +#include "../endian/Endian.h" +#include "../file/File.h" +#include + +#include +#include "../file/Path.h" + +#include +#include + +#include "minizip/ioapi.h" +#ifdef WIN32 +#include "minizip/iowin32.h" +#endif + +using namespace tjp; +using namespace core; +using namespace core::zip; + + +#include "minizip/unzip.h" + +void extractFile( + unzFile uf, + const char *password, + const std::string &basedirectory, + std::string &relativefilename +) +{ + unz_file_info file_info; + char filename_inzip[256]; + + int err; + + err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); + if (err!=UNZ_OK) + { + throw zip::Exception ("unzGetCurrentFileInfo"); + } + + relativefilename = filename_inzip; + + std::string filename = basedirectory + file::convertDelimiter (relativefilename); + file::ensurePath (filename); + + const int size_buf = 16384; + char *buf = new char [size_buf]; + + try + { + std::ofstream outfile (filename.c_str(), std::ios::binary); + + if (unzOpenCurrentFilePassword(uf,password)!=UNZ_OK) + { + throw zip::Exception ("unzOpenCurrentFilePassword"); + } + + int len; + do + { + len = unzReadCurrentFile(uf,buf,size_buf); + if (len<0) + { + throw zip::Exception("unzReadCurrentFile"); + } + else + if (len>0) + { + if (!outfile.write (buf, len)) + { + throw zip::Exception("error in writing extracted file\n"); + } + } + } + while (len>0); + + } + catch (zip::Exception &e) + { + file::deleteFile(filename); + unzCloseCurrentFile (uf); + delete[] buf; + throw e; + } + + delete[] buf; + +// change_file_date(filename,file_info.dosDate, +// file_info.tmu_date); + + err = unzCloseCurrentFile (uf); + if (err!=UNZ_OK) + { + throw zip::Exception("error %d with zipfile in unzCloseCurrentFile"); + } +} + +void extractZip( + unzFile uf, + const char* password, + const std::string &basedirectory, + std::list &list + ) +{ + uLong i; + unz_global_info gi; + int err; + + err = unzGetGlobalInfo (uf,&gi); + if (err!=UNZ_OK) + throw zip::Exception("unzGetGlobalInfo"); + + for (i=0;i &files) +{ +#ifdef WIN32 + zlib_filefunc_def ffunc; +#endif + + char filename_try[256+1]; + strncpy(filename_try, file.c_str(),256-1); + /* strncpy doesnt append the trailing NULL, if the string is too long. */ + filename_try[256] = '\0'; + +#ifdef WIN32 + fill_win32_filefunc(&ffunc); + unzFile uf = unzOpen2(filename_try,&ffunc); +#else + unzFile uf = unzOpen(filename_try); +#endif + + if (uf==NULL) + { + throw zip::Exception ("unzOpen"); + } + + try + { + extractZip (uf,NULL, path, files); + } + catch (zip::Exception &) + { + unzCloseCurrentFile(uf); + throw; + } + unzCloseCurrentFile(uf); +} + +void zip::decompress (const std::string &from, const std::string &to) +{ + gzFile gz = gzopen (from.c_str(), "rb"); + + if (gz == NULL) + throw zip::Exception ("gzopen"); + + int size_buf = 16384; + char *buf = new char[16384]; + + try + { + file::ensurePath (to); + std::ofstream outfile (to.c_str(), std::ios::binary); + + int read = 0; + while ( (read = gzread (gz, buf, size_buf) > 0) && outfile) + { + outfile.write (buf, read); + } + + if (read < 0 && !outfile) + { + throw zip::Exception ("gzread"); + } + } + catch (zip::Exception &e) + { + delete[] buf; + file::deleteFile (to); + throw e; + } +} + +typedef unsigned int Header; + +bool zip::decompress (const char *in, int insize, char *&out, int &outsize) +{ + const char *block = in; + const Header *header = (const Header *)block; + const char *data = block + sizeof (Header); + + endian::swap (*header); + + uLongf ulInSize = insize - sizeof (Header); + uLongf ulOutSize = *header; + + char *uncompressed = new char[ulOutSize]; + int error = ::uncompress((Bytef *)uncompressed, &ulOutSize, (const Bytef*)data, ulInSize); + + if (error!=Z_OK) + { + delete[] uncompressed; + return false; + } + + out = uncompressed; + outsize = (int)ulOutSize; + + return true; +} + +bool zip::decompress (const char *in, int insize, std::vector &out) +{ + const char *block = in; + const Header *header = (const Header *)block; + const char *data = block + sizeof (Header); + + endian::swap (*header); + + uLongf ulInSize = insize - sizeof (Header); + uLongf ulOutSize = *header; + + out.resize(ulOutSize); + char *uncompressed = out.data(); + int error = ::uncompress((Bytef *)uncompressed, &ulOutSize, (const Bytef*)data, ulInSize); + + if (error!=Z_OK) + { + delete[] uncompressed; + return false; + } + + return true; +} + +std::vector zip::decompress (const char *in, int insize) +{ + std::vector out; + zip::decompress(in, insize, out); + + return out; +} diff --git a/tjp/core/zip/Decompress.h b/tjp/core/zip/Decompress.h new file mode 100755 index 0000000..08ef33c --- /dev/null +++ b/tjp/core/zip/Decompress.h @@ -0,0 +1,38 @@ +// TJP COPYRIGHT HEADER + + +#pragma once + +#include "Exception.h" +#include +#include + +namespace tjp { +namespace core { +namespace zip { + +/** + * decompresses the given file to the given path, sets the files to the set of files + * found + */ +void decompress (const std::string &file, const std::string &path, std::list &files); + +void decompress (const std::string &from, const std::string &to); + +bool decompress (const char *from, int fromSize, char *&to, int &toSize); + +bool decompress (const char *from, int fromSize, std::vector &to); + +std::vector decompress (const char *from, int fromSize); + +} // namespace + +namespace zstd { + +std::vector decompress (const char *from, int fromSize); + +} + +} // namespace +} // namespace + diff --git a/tjp/core/zip/Exception.h b/tjp/core/zip/Exception.h new file mode 100755 index 0000000..b9f302d --- /dev/null +++ b/tjp/core/zip/Exception.h @@ -0,0 +1,13 @@ +// TJP COPYRIGHT HEADER + + +#pragma once + +#include + +namespace tjp::core::zip { + +DECLARE_EXCEPTION (Exception); + +} // namespace Zip + diff --git a/tjp/core/zip/MemoryBuffer.cpp b/tjp/core/zip/MemoryBuffer.cpp new file mode 100755 index 0000000..613bd24 --- /dev/null +++ b/tjp/core/zip/MemoryBuffer.cpp @@ -0,0 +1,101 @@ +// TJP COPYRIGHT HEADER + +#include "MemoryBuffer.hpp" + +#include + +namespace tjp::core::zip { + +// Custom read function for zlib +static voidpf ZCALLBACK mem_open(voidpf opaque, const char* filename, int mode) { + + return opaque; // Opaque is our MemoryBuffer +} + +int ZCALLBACK mem_close(voidpf opaque, voidpf stream) { + // Nothing to do here since std::vector manages memory automatically + return ZIP_OK; +} + +static uLong ZCALLBACK mem_read(voidpf opaque, voidpf stream, void* buf, uLong size) { + MemoryBuffer* mem = static_cast(opaque); + size_t remaining = mem->buffer.size() - mem->position; + + size_t toRead = (remaining < size) ? remaining : size; + if (toRead > 0) { + std::memcpy(buf, mem->buffer.data() + mem->position, toRead); + mem->position += toRead; + } + return static_cast(toRead); +} + +static long ZCALLBACK mem_tell(voidpf opaque, voidpf stream) { + MemoryBuffer* mem = static_cast(opaque); + return static_cast(mem->position); +} + +static long ZCALLBACK mem_seek(voidpf opaque, voidpf stream, uLong offset, int origin) { + MemoryBuffer* mem = static_cast(opaque); + size_t new_pos = mem->position; + + if (origin == ZLIB_FILEFUNC_SEEK_CUR) { + new_pos += offset; + } else if (origin == ZLIB_FILEFUNC_SEEK_END) { + new_pos = mem->buffer.size() + offset; + } else if (origin == ZLIB_FILEFUNC_SEEK_SET) { + new_pos = offset; + } + + if (new_pos > mem->buffer.size()) { + return -1; // Seeking beyond the buffer + } + + mem->position = new_pos; + return 0; // Success +} + +uLong ZCALLBACK mem_write(voidpf opaque, voidpf stream, const void* buf, uLong size) +{ + MemoryBuffer* mem = static_cast(opaque); + const char *data = (const char *)buf; + + if (mem->buffer.size() < mem->position) + return -1; + + auto remaining = mem->buffer.size() - mem->position; + auto wm = std::min(remaining, size); + + if (wm > 0) + std::memcpy( + mem->buffer.data() + mem->position, + data, + wm + ); + + if (size > wm) + mem->buffer.insert( + mem->buffer.end(), + data + wm, + data + (size - wm) + ); + + mem->position += size; + + return size; +} + +MemoryBuffer::MemoryBuffer(Vector &&contents) : + buffer(std::move(contents)), + funcs{0} +{ + funcs.zopen_file = mem_open; + funcs.zwrite_file = mem_write; + funcs.ztell_file = mem_tell; + funcs.zclose_file = mem_close; + funcs.zread_file = mem_read; +// funcs.zerror_file = mem_error; + funcs.zseek_file = mem_seek; + funcs.opaque = this; +} // + +} // namespace diff --git a/tjp/core/zip/MemoryBuffer.h b/tjp/core/zip/MemoryBuffer.h new file mode 100755 index 0000000..d15b457 --- /dev/null +++ b/tjp/core/zip/MemoryBuffer.h @@ -0,0 +1,11 @@ +// TJP COPYRIGHT HEADER + + +#pragma once + +namespace tjp::core::zip { + +// Custom memory structure +struct MemoryBuffer; + +} // diff --git a/tjp/core/zip/MemoryBuffer.hpp b/tjp/core/zip/MemoryBuffer.hpp new file mode 100755 index 0000000..02827b0 --- /dev/null +++ b/tjp/core/zip/MemoryBuffer.hpp @@ -0,0 +1,25 @@ +// TJP COPYRIGHT HEADER + + +#pragma once + +#include "Exception.h" +#include + +#include +#include "minizip/zip.h" + +namespace tjp::core::zip { + +// Custom memory structure +struct MemoryBuffer +{ + Vector buffer; + size_t position = 0; + + zlib_filefunc_def funcs; + + MemoryBuffer(Vector &&); +}; + +} // diff --git a/tjp/core/zip/VirtualFileSystem.cpp b/tjp/core/zip/VirtualFileSystem.cpp new file mode 100644 index 0000000..d6b2603 --- /dev/null +++ b/tjp/core/zip/VirtualFileSystem.cpp @@ -0,0 +1,138 @@ +// TJP COPYRIGHT HEADER + +#include "VirtualFileSystem.hpp" + +#include + +#include "minizip/ioapi.h" +#ifdef WIN32 +#include "minizip/iowin32.h" +#endif + +#include "minizip/unzip.h" + +#include "../zip/MemoryBuffer.hpp" + +#include + +#include + +namespace tjp::core::zip { + +using Contents = file::Contents; +using ContentsView = file::ContentsView; + +struct VirtualFileSystem::I +{ + zip::MemoryBuffer memoryBuffer; + unzFile uf = nullptr; + + I(Contents &&contents); + ~I(); + + Contents get(const StringView &fileName); + void put(const StringView &fileName, ContentsView); + bool has(const StringView &fileName); +} ; + +VirtualFileSystem::I::I(Contents &&contents) : + memoryBuffer(std::move(contents)) +{ + uf = unzOpen2("dummy.zip", &memoryBuffer.funcs); +} + +VirtualFileSystem::I::~I() +{ + if (uf) + unzClose(uf); + + uf = nullptr; +} + +Contents VirtualFileSystem::I::get(const StringView &fileName) +{ +// debug_break(); + + if (unzLocateFile(uf,fileName.data(),false)!=UNZ_OK) + throw Exception { "File does not exist" }; + + unz_file_info file_info; + char filename_inzip[256]; + + int err; + + err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); + if (err!=UNZ_OK) + { + throw Exception ("unzGetCurrentFileInfo"); + } + + Contents contents; + contents.resize(file_info.uncompressed_size); + + try + { + if (unzOpenCurrentFile(uf) != UNZ_OK) + throw Exception { "Open failure" }; + + auto len = unzReadCurrentFile(uf, contents.data(), contents.size()); + if (len != contents.size()) + throw Exception { "Read error" }; + + unzCloseCurrentFile (uf); + + return contents; + } + catch (Exception &e) + { + unzCloseCurrentFile (uf); + throw e; + } +} + +bool VirtualFileSystem::I::has(const StringView &fileName) +{ + return unzLocateFile(uf,fileName.data(),false) == UNZ_OK; +} + +void VirtualFileSystem::I::put(const StringView &fileName, ContentsView) +{ + throw Exception { "Not implemented" }; +} + +VirtualFileSystem::VirtualFileSystem(Contents &&contents) : + i(new I(std::move(contents))) +{ +} + +Contents VirtualFileSystem::get(const StringView &path) const +{ + return i->get(path); +} + +Optional VirtualFileSystem::get_if(const StringView &path) const +{ + try + { + return i->get(path); + } + catch(...) + { + + } + + return {}; +} + +void VirtualFileSystem::put(const StringView &path, ContentsView contents) +{ + return i->put(path, contents); +} + +bool VirtualFileSystem::has(const StringView &path) const +{ + return i->has(path); +} + + +} // namespace diff --git a/tjp/core/zip/VirtualFileSystem.h b/tjp/core/zip/VirtualFileSystem.h new file mode 100644 index 0000000..4bd92ae --- /dev/null +++ b/tjp/core/zip/VirtualFileSystem.h @@ -0,0 +1,9 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +namespace tjp::core::zip { + +struct VirtualFileSystem; + +} // namespace diff --git a/tjp/core/zip/VirtualFileSystem.hpp b/tjp/core/zip/VirtualFileSystem.hpp new file mode 100644 index 0000000..3743768 --- /dev/null +++ b/tjp/core/zip/VirtualFileSystem.hpp @@ -0,0 +1,32 @@ +// TJP COPYRIGHT HEADER + +#pragma once + +#include "../file/VirtualFileSystem.hpp" +#include + +namespace tjp::core::zip { + +struct VirtualFileSystem : file::VirtualFileSystem +{ + using Super = file::VirtualFileSystem; + + struct I; + mutable I *i; + + VirtualFileSystem(file::Contents &&contents); + + inline + String pathFor(const StringView &path) const override + { + throw Exception { "No Implemented" }; + } + + file::Contents get(const StringView &path) const override; + Optional get_if(const StringView &path) const override; + void put(const StringView &path, file::ContentsView contents) override; + bool has(const StringView &fileName) const override; +} ; + + +} // namespace diff --git a/tjp/core/zip/_delete/Compression.cpp b/tjp/core/zip/_delete/Compression.cpp new file mode 100755 index 0000000..fc9d28d --- /dev/null +++ b/tjp/core/zip/_delete/Compression.cpp @@ -0,0 +1,232 @@ +// TJP COPYRIGHT HEADER + + +#include "Compression.hpp" +#include + +#include +#include +#include + +namespace tjp::core::zip { + +// ---------------------- + +CompressorOneShot::CompressorOneShot(int level_) : + level(level_) +{ + +} + +bool CompressorOneShot::operate(const Data *from, Size fromSize, Data *&to, Size &toSize) +{ + const Bytef *source = (const Bytef *)from; + uLongf sourceLen = (uLongf)fromSize; + Bytef *dest = (Bytef *)to; + uLongf destLen = (uLongf)toSize; + + auto result = compress2(dest, &destLen, source, sourceLen, level) == Z_OK; + + toSize = destLen; + + return result; +} + +bool DecompressorOneShot::operate(const Data *from, Size fromSize, Data *&to, Size &toSize) +{ + const Bytef *source = (const Bytef *)from; + uLongf sourceLen = fromSize; + Bytef *dest = (Bytef *)to; + uLongf destLen = toSize; + + auto result = ::uncompress(dest, &destLen, source, sourceLen) == Z_OK; + + toSize = destLen; + + return result; +} + +// --------------------- + +Compressor::Compressor(int level_) : + level(level_), + i(nullptr) +{ +} + +void *Compressor::allocateStream() +{ + auto z = (z_streamp)calloc(1, sizeof(z_stream)); + deflateInit(z, level); + return z; +} + +void Compressor::deallocateStream(void *z) +{ + free(z); +} + +bool Compressor::operate(const Data *from, Size fromSize, Data *&to, Size &toSize) +{ + auto strm = (z_streamp)i; + + strm->next_in = (Bytef*)from; + strm->avail_in = (uInt)fromSize; + + strm->next_out = (Bytef*)to; + strm->avail_out = (uInt)toSize; + + auto result = deflate(strm, Z_FINISH); + toSize = strm->total_out; + + return result == Z_STREAM_END; +} + +// ------------ + +Decompressor::Decompressor() : + i(nullptr) +{ +} + + +void *Decompressor::allocateStream() +{ + auto z = (z_streamp)calloc(1, sizeof(z_stream)); + inflateInit(z); + return z; +} + + +void Decompressor::deallocateStream(void *z) +{ + free(z); +} + +bool Decompressor::operate(const Data *from, Size fromSize, Data *&to, Size &toSize) +{ + auto strm = (z_streamp)i; + + strm->next_in = (Bytef*)from; + strm->avail_in = (uInt)fromSize; + + strm->next_out = (Bytef*)to; + strm->avail_out = (uInt)toSize; + + auto result = inflate(strm, Z_FINISH); + toSize = strm->total_out; + + return result == Z_STREAM_END; +} + +// ------------ + +struct Pool +{ + Mutex mutex; + Vector pooled; + + z_stream *allocate(); + void release(z_stream *); +} ; + +z_stream * Pool::allocate() +{ + auto lock = lock_of(mutex); + + if (pooled.empty()) + return nullptr; + + auto result = pooled.back(); + pooled.pop_back(); + + return result; +} + +void Pool::release(z_stream *z) +{ + auto lock = lock_of(mutex); + + pooled.push_back(z); +} + +static Pool deflators[10]; +static Pool inflators; + +void *getPoolStream(int level) +{ + if (level == -1) + return (void *)inflators.allocate(); + + if (level > 9) + level = 9; + + return (void *)deflators[level].allocate(); +} + +void putPoolStream(int level, void *s) +{ + if (level == -1) + return inflators.release((z_streamp)s); + + if (level > 9) + level = 9; + + return deflators[level].release((z_streamp)s); + +} + +// -------------- + +bool setDict(void *p, int level, char *dict, size_t size) +{ + auto z = (z_streamp)p; + + int error = 0; + if (level < 0) + error = inflateSetDictionary(z, (const Bytef *)dict, (uInt)size); + else + error = deflateSetDictionary(z, (const Bytef *)dict, (uInt)size); + + return error == Z_OK; +} + +bool getDict(void *p, int level, char *dict, size_t size) +{ + auto z = (z_streamp)p; + + auto size_ = (uInt)size; + + char store[32768]; + + int error = 0; + if (level < 0) + error = inflateGetDictionary(z, (Bytef *)&store[0], &size_); + else + error = deflateGetDictionary(z, (Bytef *)&store[0], &size_); + + // zlib seems to store from end to front + auto offset = + size_ > size ? + size_ - size : + 0; + + mem_copy(dict, &store[offset], size); + + return error == Z_OK; +} + +// ---------------- + +bool clearStream(void *p, int level) +{ + auto z = (z_streamp)p; + + if (level < 0) + return (inflateReset(z) == Z_OK); + else + return (deflateReset(z) == Z_OK); +} + + +} // namespace diff --git a/tjp/core/zip/_delete/Compression.h b/tjp/core/zip/_delete/Compression.h new file mode 100755 index 0000000..81ab4f3 --- /dev/null +++ b/tjp/core/zip/_delete/Compression.h @@ -0,0 +1,18 @@ +// TJP COPYRIGHT HEADER + + +#pragma once + +namespace tjp { +namespace core { +namespace zip { + +/** + * decompresses the given file to the given path, sets the files to the set of files + * found + */ +struct Compressor; + +} // namespace +} // +} // diff --git a/tjp/core/zip/_delete/Compression.hpp b/tjp/core/zip/_delete/Compression.hpp new file mode 100755 index 0000000..e54dc63 --- /dev/null +++ b/tjp/core/zip/_delete/Compression.hpp @@ -0,0 +1,267 @@ +// TJP COPYRIGHT HEADER + + +#pragma once + +#include +#include +#include + +namespace tjp::core::zip { + +using Data = char; +using Size = size_t; + +/** + * decompresses the given file to the given path, sets the files to the set of files + * found + */ +struct Operator +{ + virtual ~Operator() {} + + virtual char getID() = 0; + virtual bool operate(const Data *from, Size fromSize, Data *&to, Size &toSize) = 0; +} ; + + +template +struct VirtualOperator : Operator +{ + using Operator = T; + Operator op; + + template + VirtualOperator(Args&& ... args) : + op(std::forward(args)...) + {} + + char getID() override + { + return op.getID(); + } + + bool operate(const Data *from, Size fromSize, Data *&to, Size &toSize) override + { + return op.operate(from, fromSize, to, toSize); + } +} ; + +struct CompressorOneShot +{ + CompressorOneShot(int level_); + + int level; + bool enabled(); + char getID(); + + bool operate(const Data *from, Size fromSize, Data *&to, Size &toSize); +} ; + +struct DecompressorOneShot +{ + char getID(); + bool operate(const Data *from, Size fromSize, Data *&to, Size &toSize); +} ; + +struct Compressor +{ + Compressor(int level_); + + void setExecutor(void *); + void *getExecutor(); + + void *i; + int level; + + int getLevel(); + bool enabled(); + char getID(); + + bool operate(const Data *from, Size fromSize, Data *&to, Size &toSize); + + void *allocateStream(); + void deallocateStream(void *); +} ; + +struct Decompressor +{ + Decompressor(); + + void *i; + + void setExecutor(void *); + void *getExecutor(); + + char getID(); + int getLevel(); + + bool operate(const Data *from, Size fromSize, Data *&to, Size &toSize); + + void *allocateStream(); + void deallocateStream(void *); +} ; + +template +struct Chain +{ + using Next = T; + Next next; + + template + Chain(Args&& ... args) : + next(std::forward(args)...) + {} + + auto setExecutor(void *i) + { + next.setExecutor(i); + } + + auto getExecutor() + { + return next.getExecutor(); + } + + auto enabled() + { + return next.enabled(); + } + + auto getLevel() + { + return next.getLevel(); + } + + auto getID() + { + return next.getID(); + } + + auto operate(const Data *from, Size fromSize, Data *&to, Size &toSize) + { + return next.operate(from, fromSize, to, toSize); + } + + void *allocateStream() + { + return next.allocateStream(); + } + + void deallocateStream(void *s) + { + next.deallocateStream(s); + } + +} ; + +template +struct RetainMemory : Chain +{ + using Super = Chain; + + template + RetainMemory(Args&& ... args) : + Super(std::forward(args)...) + { + Super::setExecutor(Super::allocateStream()); + } + + ~RetainMemory() + { + Super::deallocateStream(Super::getExecutor()); + } +} ; + +bool clearStream(void *p, int level); + +template +struct Clear : Chain +{ + using Super = Chain; + + template + Clear(Args&& ... args) : + Super(std::forward(args)...) + { + } + + bool operate(const Data *from, Size fromSize, Data *&to, Size &toSize) + { + if (!clearStream(Super::getExecutor(), Super::getLevel())) + return false; + + return Super::operate(from, fromSize, to, toSize); + } +} ; + +void *getPoolStream(int level); +void putPoolStream(int level, void *); + +template +struct MemoryPool : Chain +{ + using Super = Chain; + + template + MemoryPool(Args&& ... args) : + Super(std::forward(args)...) + { + } + + bool operate(const Data *from, Size fromSize, Data *&to, Size &toSize) + { + auto l = Super::getLevel(); + auto s = getPoolStream(l); + if (!s) + s = Super::allocateStream(); + + Super::setExecutor(s); + auto result = Super::operate(from, fromSize, to, toSize); + + Super::setExecutor(nullptr); + + putPoolStream(l, s); + return result; + } +} ; + +bool setDict(void *p, int level, char *dict, size_t size); +bool getDict(void *p, int level, char *dict, size_t size); + +template +struct MiniDict : Chain +{ + using Super = Chain; + + static constexpr size_t DictSize = 256; + using Dict = char[DictSize]; + Dict dict; + + template + MiniDict(Args&& ... args) : + Super(std::forward(args)...) + { + memset(dict, 0, sizeof(dict)); + } + + bool operate(const Data *from, Size fromSize, Data *&to, Size &toSize) + { + if (!setDict(Super::getExecutor(), Super::getLevel(), dict, DictSize)) + return false; + + auto result = Super::operate(from, fromSize, to, toSize); + + if (!getDict(Super::getExecutor(), Super::getLevel(), dict, DictSize)) + return false; + + return result; + } +} ; + +using StandardCompressor = MemoryPool>; +using StandardDecompressor = MemoryPool>; + +} // namespace + +#include "Compression.inl" diff --git a/tjp/core/zip/_delete/Compression.inl b/tjp/core/zip/_delete/Compression.inl new file mode 100755 index 0000000..0a98ae1 --- /dev/null +++ b/tjp/core/zip/_delete/Compression.inl @@ -0,0 +1,82 @@ +// TJP COPYRIGHT HEADER + + +#pragma once + +namespace tjp::core::zip { + +inline +bool CompressorOneShot::enabled() +{ + return level > 0; +} + +inline +bool Compressor::enabled() +{ + return level > 0; +} + +inline +char CompressorOneShot::getID() +{ + return 1; +} + +inline +char DecompressorOneShot::getID() +{ + return 1; +} + +inline +char Compressor::getID() +{ + return 2; +} + +inline +int Compressor::getLevel() +{ + return level; +} + +inline +void Compressor::setExecutor(void *i_) +{ + i = i_; +} + +inline +void *Compressor::getExecutor() +{ + return i; +} + + +inline +char Decompressor::getID() +{ + return 2; +} + +inline +void Decompressor::setExecutor(void *i_) +{ + i = i_; +} + +inline +void *Decompressor::getExecutor() +{ + return i; +} + +inline +int Decompressor::getLevel() +{ + return -1; +} + + +} // namespace diff --git a/tjp/core/zip/_delete/_tests/Compression.cpp b/tjp/core/zip/_delete/_tests/Compression.cpp new file mode 100644 index 0000000..21242a2 --- /dev/null +++ b/tjp/core/zip/_delete/_tests/Compression.cpp @@ -0,0 +1,165 @@ +// TJP COPYRIGHT HEADER + +#include + +#include +#include + +#include +#include + +#include +#include + +namespace tjp::core::zip { + +SCENARIO("zip::compression") +{ + GIVEN("CompressorOneShot") + { + Vector source, compressed, uncompressed; + source.resize(90); + compressed.resize(100); + uncompressed.resize(100,1); + + CompressorOneShot compressor(5); + DecompressorOneShot decompressor; + + auto compressedSize = compressed.size(); + auto compressedData = compressed.data(); + auto didCompress = compressor.operate( + source.data(), source.size(), + compressedData, compressedSize + ); + REQUIRE(didCompress); + + auto uncompressedSize = uncompressed.size(); + auto uncompressedData = uncompressed.data(); + auto didDecompress = decompressor.operate( + compressed.data(), compressedSize, + uncompressedData, uncompressedSize + ); + REQUIRE(didDecompress); + uncompressed.resize(uncompressedSize); + + auto same = source == uncompressed; + REQUIRE(uncompressedSize == source.size()); + REQUIRE(same); + } + + GIVEN("RetainMemory") + { + Vector source, compressed, uncompressed; + source.resize(90); + compressed.resize(100); + uncompressed.resize(100,1); + + RetainMemory compressor(5); + RetainMemory decompressor; + + auto compressedSize = compressed.size(); + auto compressedData = compressed.data(); + auto didCompress = compressor.operate( + source.data(), source.size(), + compressedData, compressedSize + ); + REQUIRE(didCompress); + + auto uncompressedSize = uncompressed.size(); + auto uncompressedData = uncompressed.data(); + auto didDecompress = decompressor.operate( + compressed.data(), compressedSize, + uncompressedData, uncompressedSize + ); + REQUIRE(didDecompress); + uncompressed.resize(uncompressedSize); + + auto same = source == uncompressed; + REQUIRE(uncompressedSize == source.size()); + REQUIRE(same); + } + + GIVEN("MemoryPool") + { + Vector source, compressed, uncompressed; + source.resize(90); + compressed.resize(100); + + for (auto i=0; i<16; ++i) + { + uncompressed.clear(); + uncompressed.resize(100,1); + + MemoryPool> compressor((i % 4) + 1); + MemoryPool> decompressor; + + auto compressedSize = compressed.size(); + auto compressedData = compressed.data(); + auto didCompress = compressor.operate( + source.data(), source.size(), + compressedData, compressedSize + ); + REQUIRE(didCompress); + + auto uncompressedSize = uncompressed.size(); + auto uncompressedData = uncompressed.data(); + auto didDecompress = decompressor.operate( + compressed.data(), compressedSize, + uncompressedData, uncompressedSize + ); + REQUIRE(didDecompress); + uncompressed.resize(uncompressedSize); + + auto same = source == uncompressed; + REQUIRE(uncompressedSize == source.size()); + REQUIRE(same); + } + } + + NO_GIVEN("MiniDict") + { + Vector source, compressed, uncompressed; + source.resize(90); + compressed.resize(100); + + for (auto i=0; i<90; ++i) + source[i] = i%13; + + MemoryPool>> compressor(5); + MemoryPool>> decompressor; + int lastCompression = 1000000; + + for (auto i=0; i<16; ++i) + { + uncompressed.clear(); + uncompressed.resize(100,1); + + auto compressedSize = compressed.size(); + auto compressedData = compressed.data(); + auto didCompress = compressor.operate( + source.data(), source.size(), + compressedData, compressedSize + ); + REQUIRE(didCompress); + sLogTest("testing", logVar(compressedSize) << logVar(lastCompression)); + + REQUIRE(compressedSize < lastCompression); + lastCompression = compressedSize; + + auto uncompressedSize = uncompressed.size(); + auto uncompressedData = uncompressed.data(); + auto didDecompress = decompressor.operate( + compressed.data(), compressedSize, + uncompressedData, uncompressedSize + ); + REQUIRE(didDecompress); + uncompressed.resize(uncompressedSize); + + auto same = source == uncompressed; + REQUIRE(uncompressedSize == source.size()); + REQUIRE(same); + } + } +} + +} // namespace