url.hpp
1 /*
2  * Copyright (c) 2025, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <optional>
19 #include <string>
20 
21 #include <curl/curl.h>
22 
23 namespace kvikio::detail {
32  private:
33  CURLU* _handle{nullptr};
34 
35  public:
43 
47  ~CurlUrlHandle() noexcept;
48 
49  CurlUrlHandle(CurlUrlHandle const&) = delete;
50  CurlUrlHandle& operator=(CurlUrlHandle const&) = delete;
51 
52  CurlUrlHandle(CurlUrlHandle&& other) noexcept;
53  CurlUrlHandle& operator=(CurlUrlHandle&& other) noexcept;
54 
61  CURLU* get() const;
62 };
63 
84 class UrlParser {
85  public:
89  struct UrlComponents {
94  std::optional<std::string> scheme;
95 
100  std::optional<std::string> host;
101 
107  std::optional<std::string> port;
108 
113  std::optional<std::string> path;
114 
118  std::optional<std::string> query;
119 
123  std::optional<std::string> fragment;
124  };
125 
161  static UrlComponents parse(std::string const& url,
162  std::optional<unsigned int> bitmask_url_flags = std::nullopt,
163  std::optional<unsigned int> bitmask_component_flags = std::nullopt);
164 
175  static std::optional<std::string> extract_component(
176  CurlUrlHandle const& handle,
177  CURLUPart part,
178  std::optional<unsigned int> bitmask_component_flags = std::nullopt,
179  std::optional<CURLUcode> allowed_err_code = std::nullopt);
180 
192  static std::optional<std::string> extract_component(
193  std::string const& url,
194  CURLUPart part,
195  std::optional<unsigned int> bitmask_url_flags = std::nullopt,
196  std::optional<unsigned int> bitmask_component_flags = std::nullopt,
197  std::optional<CURLUcode> allowed_err_code = std::nullopt);
198 };
199 } // namespace kvikio::detail
RAII wrapper for libcurl's URL handle (CURLU)
Definition: url.hpp:31
~CurlUrlHandle() noexcept
Clean up the underlying URL handle.
CURLU * get() const
Get the underlying libcurl URL handle.
CurlUrlHandle()
Create a new libcurl URL handle.
URL parsing utility using libcurl's URL API.
Definition: url.hpp:84
static std::optional< std::string > extract_component(std::string const &url, CURLUPart part, std::optional< unsigned int > bitmask_url_flags=std::nullopt, std::optional< unsigned int > bitmask_component_flags=std::nullopt, std::optional< CURLUcode > allowed_err_code=std::nullopt)
Extract a specific component from a URL string.
static UrlComponents parse(std::string const &url, std::optional< unsigned int > bitmask_url_flags=std::nullopt, std::optional< unsigned int > bitmask_component_flags=std::nullopt)
Parses the given URL according to RFC 3986 plus and extracts its components.
static std::optional< std::string > extract_component(CurlUrlHandle const &handle, CURLUPart part, std::optional< unsigned int > bitmask_component_flags=std::nullopt, std::optional< CURLUcode > allowed_err_code=std::nullopt)
Extract a specific component from a CurlUrlHandle.
Container for parsed URL components.
Definition: url.hpp:89
std::optional< std::string > host
The hostname or IP address. May be empty for URLs without an authority component (e....
Definition: url.hpp:100
std::optional< std::string > path
The path component of the URL. Libcurl ensures that the path component is always present,...
Definition: url.hpp:113
std::optional< std::string > scheme
The URL scheme (e.g., "http", "https", "ftp"). May be empty for scheme-relative URLs or paths.
Definition: url.hpp:94
std::optional< std::string > query
The query string (without the leading "?"). Empty if no query parameters are present.
Definition: url.hpp:118
std::optional< std::string > port
The port number as a string. Will be empty if no explicit port is specified in the URL.
Definition: url.hpp:107
std::optional< std::string > fragment
The fragment identifier (without the leading "#"). Empty if no fragment is present.
Definition: url.hpp:123