Grok 20.3.2
FetchPathParser.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 <string>
21#include <stdexcept>
22
23namespace grk
24{
25
27{
28public:
29 static void parseVsiPath(std::string& url, ParsedFetchPath& parsed, const std::string& prefix)
30 {
31 std::string full_prefix = "/" + prefix + "/";
32 grklog.debug("Processing VSI path with prefix '%s': %s", prefix.c_str(), url.c_str());
33 if(!url.starts_with(full_prefix))
34 {
35 grklog.error("Invalid VSI path, does not start with %s: %s", full_prefix.c_str(),
36 url.c_str());
37 throw std::runtime_error("Invalid VSI path: does not start with " + full_prefix);
38 }
39 url = url.substr(full_prefix.length());
40 grklog.debug("Stripped %s prefix, remaining: %s", full_prefix.c_str(), url.c_str());
41
42 parseBucketKey(url, parsed, prefix.c_str());
43 }
44
45 // Parse HTTPS path (generic host/port and bucket/key extraction)
46 static void parseHttpsPath(std::string& url, ParsedFetchPath& parsed, int default_port = 443)
47 {
48 grklog.debug("Processing HTTPS path: %s", url.c_str());
49 if(!url.starts_with("https://"))
50 {
51 grklog.error("Invalid HTTPS URL, does not start with https://: %s", url.c_str());
52 throw std::runtime_error("Invalid HTTPS URL: does not start with https://");
53 }
54 url = url.substr(8);
55 grklog.debug("Stripped https:// prefix, remaining: %s", url.c_str());
56
57 // Parse host and port
58 parseHostPort(url, parsed, default_port);
59
60 // Extract path and parse bucket/key
61 std::string path = url.substr(url.find('/') + 1);
62 grklog.debug("Extracted path: %s", path.c_str());
63 parseBucketKey(path, parsed, "HTTPS");
64 }
65
66private:
67 // Parse host and port from URL
68 static void parseHostPort(const std::string& url, ParsedFetchPath& parsed, int default_port)
69 {
70 size_t host_end = url.find(':');
71 std::string port_str;
72 if(host_end == std::string::npos)
73 {
74 host_end = url.find('/');
75 if(host_end == std::string::npos)
76 {
77 grklog.error("Invalid HTTPS URL: no bucket/key separator in: %s", url.c_str());
78 throw std::runtime_error("Invalid HTTPS URL: no bucket/key separator");
79 }
80 parsed.host = url.substr(0, host_end);
81 parsed.port = default_port;
82 grklog.debug("No port specified, using host=%s, port=%d", parsed.host.c_str(), parsed.port);
83 }
84 else
85 {
86 parsed.host = url.substr(0, host_end);
87 port_str = url.substr(host_end + 1, url.find('/') - host_end - 1);
88 grklog.debug("Port specified: host=%s, port_str=%s", parsed.host.c_str(), port_str.c_str());
89 try
90 {
91 parsed.port = port_str.empty() ? default_port : std::stoi(port_str);
92 grklog.debug("Parsed port: %d", parsed.port);
93 }
94 catch(const std::exception& e)
95 {
96 grklog.error("Invalid port in HTTPS URL: %s, using default %d", port_str.c_str(),
97 default_port);
98 parsed.port = default_port;
99 }
100 }
101 }
102
103 // Parse bucket and key from path
104 static void parseBucketKey(const std::string& path, ParsedFetchPath& parsed,
105 const char* log_context)
106 {
107 size_t bucket_end = path.find('/');
108 if(bucket_end == std::string::npos)
109 {
110 grklog.error("Invalid %s URL: no key after bucket in path: %s", log_context, path.c_str());
111 throw std::runtime_error(std::string("Invalid ") + log_context + " URL: no key after bucket");
112 }
113 parsed.bucket = path.substr(0, bucket_end);
114 parsed.key = path.substr(bucket_end + 1);
115 grklog.debug("%s parsed: bucket=%s, key=%s", log_context, parsed.bucket.c_str(),
116 parsed.key.c_str());
117 }
118};
119
120} // namespace grk
Definition FetchPathParser.h:27
static void parseHostPort(const std::string &url, ParsedFetchPath &parsed, int default_port)
Definition FetchPathParser.h:68
static void parseBucketKey(const std::string &path, ParsedFetchPath &parsed, const char *log_context)
Definition FetchPathParser.h:104
static void parseVsiPath(std::string &url, ParsedFetchPath &parsed, const std::string &prefix)
Definition FetchPathParser.h:29
static void parseHttpsPath(std::string &url, ParsedFetchPath &parsed, int default_port=443)
Definition FetchPathParser.h:46
ResWindow.
Definition CompressedChunkCache.h:36
ILogger & grklog
Definition Logger.cpp:24
Definition FetchCommon.h:34
int port
Definition FetchCommon.h:38
std::string bucket
Definition FetchCommon.h:36
std::string key
Definition FetchCommon.h:37
std::string host
Definition FetchCommon.h:35