20#include "grk_config_private.h"
25#ifdef GRK_ENABLE_LIBCURL
30class HTTPFetcher :
public CurlFetcher
33 void parse(
const std::string& path)
override
36 std::string mutable_path = path;
37 if(mutable_path.starts_with(
"/vsicurl/"))
40 url_ = mutable_path.substr(9);
41 if(!url_.starts_with(
"http://") && !url_.starts_with(
"https://"))
43 grklog.
error(
"Invalid /vsicurl/ URL: must start with http:// or https://: %s",
45 throw std::runtime_error(
"Invalid /vsicurl/ URL: must start with http:// or https://");
47 grklog.
debug(
"Parsed /vsicurl/ URL: %s", url_.c_str());
49 else if(mutable_path.starts_with(
"http://") || mutable_path.starts_with(
"https://"))
52 grklog.
debug(
"Parsed HTTP/HTTPS URL: %s", url_.c_str());
57 "Unsupported URL format for HTTPFetcher; must be http://, https://, or /vsicurl/: %s",
59 throw std::runtime_error(
60 "Unsupported URL format for HTTPFetcher; must be http://, https://, or /vsicurl/");
64 void auth(CURL* curl)
override
67 CurlFetcher::auth(curl);
70 std::string username = auth_.username_;
71 std::string password = auth_.password_;
72 if(username.empty() && password.empty())
74 if(
const char* userpwd = std::getenv(
"GRK_HTTP_USERPWD"))
76 std::string userpwd_str(userpwd);
77 size_t colon_pos = userpwd_str.find(
':');
78 if(colon_pos != std::string::npos)
80 username = userpwd_str.substr(0, colon_pos);
81 password = userpwd_str.substr(colon_pos + 1);
82 grklog.
debug(
"Set HTTP username = %s and password from GRK_HTTP_USERPWD",
89 if(!username.empty() && !password.empty())
91 curl_easy_setopt(curl, CURLOPT_USERNAME, username.c_str());
92 curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
93 grklog.
debug(
"Applied HTTP basic authentication for username: %s", username.c_str());
97 if(
const char* header_file = std::getenv(
"GRK_HTTP_HEADER_FILE"))
100 grklog.
debug(
"GRK_HTTP_HEADER_FILE set to %s (not fully implemented)", header_file);
104 for(
const auto& hdr : auth_.custom_headers_)
106 grklog.
debug(
"Using custom header: %s", hdr.c_str());
108 if(!auth_.bearer_token_.empty())
110 grklog.
debug(
"Using bearer token: %s", auth_.bearer_token_.c_str());
114 curl_slist* prepareAuthHeaders(curl_slist* headers)
override
117 for(
const auto& hdr : auth_.custom_headers_)
119 headers = curl_slist_append(headers, hdr.c_str());
123 if(!auth_.bearer_token_.empty())
125 std::string auth_header =
"Authorization: Bearer " + auth_.bearer_token_;
126 headers = curl_slist_append(headers, auth_header.c_str());
ResWindow.
Definition CompressedChunkCache.h:36
ILogger & grklog
Definition Logger.cpp:24
virtual void error(const char *fmt,...)=0
virtual void debug(const char *fmt,...)=0