fix windows upload
This commit is contained in:
		@@ -39,8 +39,8 @@ struct http_put_file final : http_request_base {
 | 
				
			|||||||
  std::shared_ptr<utils::encryption::encrypting_reader> reader;
 | 
					  std::shared_ptr<utils::encryption::encrypting_reader> reader;
 | 
				
			||||||
  std::string source_path;
 | 
					  std::string source_path;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  [[nodiscard]] auto
 | 
					  [[nodiscard]] auto set_method(CURL *curl, stop_type &stop_requested) const
 | 
				
			||||||
  set_method(CURL *curl, stop_type &stop_requested) const -> bool override;
 | 
					      -> bool override;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
  mutable std::shared_ptr<read_file_info> read_info{};
 | 
					  mutable std::shared_ptr<read_file_info> read_info{};
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,9 +26,7 @@
 | 
				
			|||||||
#include "utils/file.hpp"
 | 
					#include "utils/file.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace repertory::curl::requests {
 | 
					namespace repertory::curl::requests {
 | 
				
			||||||
using read_callback = size_t (*)(char *, size_t, size_t, void *);
 | 
					using curl_response_callback =
 | 
				
			||||||
 | 
					 | 
				
			||||||
using response_callback =
 | 
					 | 
				
			||||||
    std::function<void(const data_buffer &data, long response_code)>;
 | 
					    std::function<void(const data_buffer &data, long response_code)>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct read_file_info final {
 | 
					struct read_file_info final {
 | 
				
			||||||
@@ -37,19 +35,8 @@ struct read_file_info final {
 | 
				
			|||||||
  std::uint64_t offset{};
 | 
					  std::uint64_t offset{};
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
inline const auto read_file_data = static_cast<read_callback>(
 | 
					[[nodiscard]] auto curl_file_reader(char *buffer, size_t size, size_t nitems,
 | 
				
			||||||
    [](char *buffer, size_t size, size_t nitems, void *instream) -> size_t {
 | 
					                                    void *instream) -> size_t;
 | 
				
			||||||
      auto *read_info = reinterpret_cast<read_file_info *>(instream);
 | 
					 | 
				
			||||||
      std::size_t bytes_read{};
 | 
					 | 
				
			||||||
      auto ret =
 | 
					 | 
				
			||||||
          read_info->file->read(reinterpret_cast<unsigned char *>(buffer),
 | 
					 | 
				
			||||||
                                size * nitems, read_info->offset, &bytes_read);
 | 
					 | 
				
			||||||
      if (ret) {
 | 
					 | 
				
			||||||
        read_info->offset += bytes_read;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      return ret && not read_info->stop_requested ? bytes_read
 | 
					 | 
				
			||||||
                                                  : CURL_READFUNC_ABORT;
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct http_request_base {
 | 
					struct http_request_base {
 | 
				
			||||||
  http_request_base() = default;
 | 
					  http_request_base() = default;
 | 
				
			||||||
@@ -68,14 +55,15 @@ struct http_request_base {
 | 
				
			|||||||
  std::string path{};
 | 
					  std::string path{};
 | 
				
			||||||
  http_query_parameters query{};
 | 
					  http_query_parameters query{};
 | 
				
			||||||
  std::optional<http_range> range{};
 | 
					  std::optional<http_range> range{};
 | 
				
			||||||
  std::optional<response_callback> response_handler;
 | 
					  std::optional<curl_response_callback> response_handler;
 | 
				
			||||||
  std::optional<http_headers> response_headers;
 | 
					  std::optional<http_headers> response_headers;
 | 
				
			||||||
  std::optional<std::uint64_t> total_size{};
 | 
					  std::optional<std::uint64_t> total_size{};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  [[nodiscard]] virtual auto get_path() const -> std::string { return path; }
 | 
					  [[nodiscard]] virtual auto get_path() const -> std::string { return path; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  [[nodiscard]] virtual auto
 | 
					  [[nodiscard]] virtual auto set_method(CURL *curl,
 | 
				
			||||||
  set_method(CURL *curl, stop_type &stop_requested) const -> bool = 0;
 | 
					                                        stop_type &stop_requested) const
 | 
				
			||||||
 | 
					      -> bool = 0;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
} // namespace repertory::curl::requests
 | 
					} // namespace repertory::curl::requests
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,21 +21,23 @@
 | 
				
			|||||||
*/
 | 
					*/
 | 
				
			||||||
#include "comm/curl/requests/http_put_file.hpp"
 | 
					#include "comm/curl/requests/http_put_file.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "utils/error_utils.hpp"
 | 
				
			||||||
#include "utils/string.hpp"
 | 
					#include "utils/string.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace repertory::curl::requests {
 | 
					namespace repertory::curl::requests {
 | 
				
			||||||
auto http_put_file::set_method(CURL *curl, stop_type &stop_requested) const
 | 
					auto http_put_file::set_method(CURL *curl, stop_type &stop_requested) const
 | 
				
			||||||
    -> bool {
 | 
					    -> bool {
 | 
				
			||||||
  curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
 | 
					  REPERTORY_USES_FUNCTION_NAME();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
 | 
				
			||||||
 | 
					  curl_easy_setopt(curl, CURLOPT_INFILE, nullptr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (source_path.empty()) {
 | 
					  if (source_path.empty()) {
 | 
				
			||||||
    curl_easy_setopt(curl, CURLOPT_INFILE, nullptr);
 | 
					 | 
				
			||||||
    curl_easy_setopt(curl, CURLOPT_INFILESIZE, 0L);
 | 
					    curl_easy_setopt(curl, CURLOPT_INFILESIZE, 0L);
 | 
				
			||||||
    return true;
 | 
					    return true;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (reader) {
 | 
					  if (reader) {
 | 
				
			||||||
    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
 | 
					 | 
				
			||||||
    curl_easy_setopt(curl, CURLOPT_READDATA, reader.get());
 | 
					    curl_easy_setopt(curl, CURLOPT_READDATA, reader.get());
 | 
				
			||||||
    curl_easy_setopt(
 | 
					    curl_easy_setopt(
 | 
				
			||||||
        curl, CURLOPT_READFUNCTION,
 | 
					        curl, CURLOPT_READFUNCTION,
 | 
				
			||||||
@@ -51,19 +53,19 @@ auto http_put_file::set_method(CURL *curl, stop_type &stop_requested) const
 | 
				
			|||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (not*read_info->file) {
 | 
					  if (not*read_info->file) {
 | 
				
			||||||
 | 
					    utils::error::raise_url_error(function_name, get_path(), source_path,
 | 
				
			||||||
 | 
					                                  std::runtime_error("failed to open file"));
 | 
				
			||||||
    return false;
 | 
					    return false;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  auto file_size = read_info->file->size();
 | 
					  auto file_size = read_info->file->size().value_or(0U);
 | 
				
			||||||
  if (file_size == 0U) {
 | 
					  if (file_size == 0U) {
 | 
				
			||||||
    curl_easy_setopt(curl, CURLOPT_INFILE, nullptr);
 | 
					 | 
				
			||||||
    curl_easy_setopt(curl, CURLOPT_INFILESIZE, 0L);
 | 
					    curl_easy_setopt(curl, CURLOPT_INFILESIZE, 0L);
 | 
				
			||||||
    return true;
 | 
					    return true;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
 | 
					 | 
				
			||||||
  curl_easy_setopt(curl, CURLOPT_READDATA, read_info.get());
 | 
					  curl_easy_setopt(curl, CURLOPT_READDATA, read_info.get());
 | 
				
			||||||
  curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_file_data);
 | 
					  curl_easy_setopt(curl, CURLOPT_READFUNCTION, curl_file_reader);
 | 
				
			||||||
  curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, file_size);
 | 
					  curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, file_size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return true;
 | 
					  return true;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -0,0 +1,43 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					  Copyright <2018-2024> <scott.e.graves@protonmail.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 "comm/curl/requests/http_request_base.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "utils/file.hpp"
 | 
				
			||||||
 | 
					#include "utils/string.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace repertory::curl::requests {
 | 
				
			||||||
 | 
					auto curl_file_reader(char *buffer, size_t size, size_t nitems, void *instream)
 | 
				
			||||||
 | 
					    -> size_t {
 | 
				
			||||||
 | 
					  auto *read_info = reinterpret_cast<read_file_info *>(instream);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  std::size_t bytes_read{};
 | 
				
			||||||
 | 
					  auto ret =
 | 
				
			||||||
 | 
					      read_info->file->read(reinterpret_cast<unsigned char *>(buffer),
 | 
				
			||||||
 | 
					                            size * nitems, read_info->offset, &bytes_read);
 | 
				
			||||||
 | 
					  if (ret) {
 | 
				
			||||||
 | 
					    read_info->offset += bytes_read;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return ret && not read_info->stop_requested ? bytes_read
 | 
				
			||||||
 | 
					                                              : CURL_READFUNC_ABORT;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					} // namespace repertory::curl::requests
 | 
				
			||||||
		Reference in New Issue
	
	Block a user