Grok 20.3.2
HTTPFetcher.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2026 Grok Image Compression Inc.
3 *
4 * This source code is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License, version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This source code is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
12 *
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18#pragma once
19
20#include "grk_config_private.h"
21#include "CurlFetcher.h"
22#include "FetchPathParser.h"
23#include <ctime>
24
25#ifdef GRK_ENABLE_LIBCURL
26
27namespace grk
28{
29
30class HTTPFetcher : public CurlFetcher
31{
32protected:
33 void parse(const std::string& path) override
34 {
35 // Create a non-const copy of path for FetchPathParser
36 std::string mutable_path = path;
37 if(mutable_path.starts_with("/vsicurl/"))
38 {
39 // Content after /vsicurl/ is a complete URL — use it directly
40 url_ = mutable_path.substr(9); // strlen("/vsicurl/") == 9
41 if(!url_.starts_with("http://") && !url_.starts_with("https://"))
42 {
43 grklog.error("Invalid /vsicurl/ URL: must start with http:// or https://: %s",
44 url_.c_str());
45 throw std::runtime_error("Invalid /vsicurl/ URL: must start with http:// or https://");
46 }
47 grklog.debug("Parsed /vsicurl/ URL: %s", url_.c_str());
48 }
49 else if(mutable_path.starts_with("http://") || mutable_path.starts_with("https://"))
50 {
51 url_ = mutable_path; // Use the URL as-is
52 grklog.debug("Parsed HTTP/HTTPS URL: %s", url_.c_str());
53 }
54 else
55 {
57 "Unsupported URL format for HTTPFetcher; must be http://, https://, or /vsicurl/: %s",
58 path.c_str());
59 throw std::runtime_error(
60 "Unsupported URL format for HTTPFetcher; must be http://, https://, or /vsicurl/");
61 }
62 }
63
64 void auth(CURL* curl) override
65 {
66 // Apply parent auth settings (e.g., SSL verification)
67 CurlFetcher::auth(curl);
68
69 // Check environment variables for username and password
70 std::string username = auth_.username_;
71 std::string password = auth_.password_;
72 if(username.empty() && password.empty())
73 {
74 if(const char* userpwd = std::getenv("GRK_HTTP_USERPWD"))
75 {
76 std::string userpwd_str(userpwd);
77 size_t colon_pos = userpwd_str.find(':');
78 if(colon_pos != std::string::npos)
79 {
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",
83 username.c_str());
84 }
85 }
86 }
87
88 // Apply username and password if available
89 if(!username.empty() && !password.empty())
90 {
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());
94 }
95
96 // Custom headers via GRK_HTTP_HEADER_FILE
97 if(const char* header_file = std::getenv("GRK_HTTP_HEADER_FILE"))
98 {
99 // Note: Simplified; actual implementation would read headers from the file
100 grklog.debug("GRK_HTTP_HEADER_FILE set to %s (not fully implemented)", header_file);
101 }
102
103 // Log custom headers and bearer token usage
104 for(const auto& hdr : auth_.custom_headers_)
105 {
106 grklog.debug("Using custom header: %s", hdr.c_str());
107 }
108 if(!auth_.bearer_token_.empty())
109 {
110 grklog.debug("Using bearer token: %s", auth_.bearer_token_.c_str());
111 }
112 }
113
114 curl_slist* prepareAuthHeaders(curl_slist* headers) override
115 {
116 // Add custom headers from FetchAuth if provided
117 for(const auto& hdr : auth_.custom_headers_)
118 {
119 headers = curl_slist_append(headers, hdr.c_str());
120 }
121
122 // Add Authorization header for bearer token if provided
123 if(!auth_.bearer_token_.empty())
124 {
125 std::string auth_header = "Authorization: Bearer " + auth_.bearer_token_;
126 headers = curl_slist_append(headers, auth_header.c_str());
127 }
128
129 return headers;
130 }
131};
132
133} // namespace grk
134
135#endif
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