File: vtkURI.h

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (366 lines) | stat: -rw-r--r-- 12,169 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#ifndef vtkURI_h
#define vtkURI_h

#include "vtkIOCoreModule.h" // For export macro
#include "vtkObject.h"
#include "vtkSmartPointer.h"  // For vtkSmartPointer
#include "vtkWrappingHints.h" // For VTK_WRAPEXCLUDE

#include <cstdlib> // for std::size_t
#include <memory>  // for std::unique_ptr
#include <string>  // for std::string

VTK_ABI_NAMESPACE_BEGIN

/**
 * @brief Represent an URI component
 *
 * An URI can have an empty but defined component.
 * This applies to authority, path, query and fragment.
 * This is mainly used for strong string reconstruction, example:
 * `vtkURI::Parse("file://?#")->ToString()` must return `"file://?#"`,
 * all components are empty, but defined, so they must be restored
 * when recomposition the URI string representation.
 * URI path is always defined for a valid URI.
 */
class VTKIOCORE_EXPORT vtkURIComponent
{
public:
  struct UndefinedTag
  {
  };
  static constexpr UndefinedTag Undefined{};

  /**
   * @brief Default constructor. Constructs an undefined component.
   */
  vtkURIComponent() = default;

  /**
   * @brief Default constructor. Constructs a defined component.
   * @param str The component value, may be empty.
   */
  vtkURIComponent(std::string str)
    : Value{ std::move(str) }
    , Defined{ true }
  {
  }

  /**
   * @brief Default constructor. Constructs a defined component.
   * @param str The component value, may be empty, but must not be nullptr.
   */
  vtkURIComponent(const char* str)
    : Value{ str }
    , Defined{ true }
  {
  }

  /**
   * @brief Constructs an undefined component. Use vtkURIComponent::Undefined
   */
  vtkURIComponent(UndefinedTag) {}

  ~vtkURIComponent() = default;
  vtkURIComponent(const vtkURIComponent&) = default;
  vtkURIComponent& operator=(const vtkURIComponent&) = default;
  vtkURIComponent(vtkURIComponent&&) = default;
  vtkURIComponent& operator=(vtkURIComponent&&) = default;

  /**
   * @return Return component value. Is empty if this is undefined.
   */
  const std::string& GetValue() const noexcept { return this->Value; }

  /**
   * @return `true` if this is defined, `false` otherwise
   */
  bool IsDefined() const noexcept { return this->Defined; }

  /**
   * @return `true` if this is defined, `false` otherwise
   */
  explicit operator bool() const noexcept { return this->Defined; }

  ///@{
  /**
   * @return Equality compararison of URI components.
   * Two components are equal if they are both defined and have the same value, or if they are both
   * undefined.
   */
  bool operator==(const vtkURIComponent& other) const noexcept
  {
    return this->Value == other.Value && this->Defined == other.Defined;
  }

  bool operator!=(const vtkURIComponent& other) const noexcept { return !(*this == other); }
  ///@}

private:
  std::string Value;
  bool Defined = false;
};

/**
 * @brief URI representation
 *
 * This class is final and immutable.
 * - Use `vtkURI::Parse` to create an URI from its string representation.
 * - Use `ToString` to get the string representation from an URI.
 * - Use `vtkURI::Make` to create an URI from components directly.
 * - Use `vtkURI::Resolve` to merge two URIs.
 * - Use `vtkURI::Clone` or member `Clone` if you need to copy an URI.
 *
 * Other functions are mainly getters for URI components or URI type identification.
 *
 * Known limitations:
 * - No [normalized comparison support](https://datatracker.ietf.org/doc/html/rfc3986#section-6.1)
 */
class VTKIOCORE_EXPORT vtkURI final : public vtkObject
{
public:
  vtkTypeMacro(vtkURI, vtkObject);
  void PrintSelf(ostream& os, vtkIndent indent) override;

  /**
   * @brief Construct a new vtkURI
   *
   * Default URI as a defined but empty path. Other components are undefined.
   *
   * @return New URI instance
   */
  static vtkURI* New();

  /**
   *  Calls `PercentEncode(str.data(), str.size())`
   *
   * @param str Input string to encode, may be empty.
   * @return Encodes string from `str`
   */
  static std::string PercentEncode(const std::string& str)
  {
    return PercentEncode(str.data(), str.size());
  }

  /**
   * @brief Encode a string into an URI compatible, percent-encoded, string
   *
   * Transform all bytes in `str` that are **not** part of
   * ["reserved"](https://datatracker.ietf.org/doc/html/rfc3986#section-2.2)
   * or ["unreserved"](https://datatracker.ietf.org/doc/html/rfc3986#section-2.3)
   * character sets into
   * [percent-encoded values](https://datatracker.ietf.org/doc/html/rfc3986#section-2.1).
   *
   * Note:
   * - All '%' in `str` will be replaced with "%25",
   *   even if they already represent a percent-encoded value
   *
   * @param str Input string to encode, may be null if size is 0.
   * @param size Input string size, may be 0.
   * @return Encoded string from `str`
   */
  static std::string PercentEncode(const char* str, std::size_t size);

  /**
   *  Calls `PercentDecode(str.data(), str.size())`
   *
   * @param str Input string to decode, may be empty.
   * @return Decoded string from `str`
   */
  static std::string PercentDecode(const std::string& str)
  {
    return PercentDecode(str.data(), str.size());
  }

  /**
   * @brief Decode percent-encoded values from given string
   *
   * [Percent-encoded values](https://datatracker.ietf.org/doc/html/rfc3986#section-2.1)
   * are used to store reserved characters in URIs.
   *
   * This function decode `str`, replacing `%HH` values with their real value.
   *
   * @param str Input string to decode, may be null if size is 0.
   * @param size Input string size, may be 0.
   * @return Decoded string from `str`
   */
  static std::string PercentDecode(const char* str, std::size_t size);

  /**
   * @brief Create a new vtkURI with specified components
   *
   * Syntax of components is checked in order to ensure that they respect
   * [RFC3986](https://datatracker.ietf.org/doc/html/rfc3986#section-3).
   *
   * If scheme is "data" (case-insensitive), the path is only checked
   * until the beginning of the data. This is done to prevent massive overhead when constructing
   * a big data URI. Data validation has to be performed by the decoding algorithm.
   * vtkURI::PercentDecode does the required checks for raw data URIs.
   *
   * Percent-encoded character are not decoded. Use `vtkURI::PercentEncode` if necessary.
   *
   * Tip: Parameters may be moved-in to prevent copy of big strings.
   * This function is not wrapped. If you need to construct an URI from a wrapper, use `Parse(str)`.
   *
   * @param scheme URI scheme, must not be empty if defined.
   * @param authority URI authority, may be defined, but empty.
   * @param path URI path, must be defined, but can be empty.
   * @param query URI query, may be defined, but empty.
   * @param fragment URI fragment, may be defined, but empty.
   * @return nullptr if URI syntax checks do not pass, otherwise a new vtkURI.
   */
  VTK_WRAPEXCLUDE static vtkSmartPointer<vtkURI> Make(
    vtkURIComponent scheme = vtkURIComponent::Undefined,
    vtkURIComponent authority = vtkURIComponent::Undefined, vtkURIComponent path = "",
    vtkURIComponent query = vtkURIComponent::Undefined,
    vtkURIComponent fragment = vtkURIComponent::Undefined);

  /**
   * @brief Clone a vtkURI
   *
   * @param other vtkURI to clone
   * @return if `other == nullptr` returns nullptr, otherwise returns a new vtkURI
   * with the exact same components as `other`
   */
  static vtkSmartPointer<vtkURI> Clone(const vtkURI* other);

  /**
   * @brief Create a new URI from a string.
   *
   * Perform as if by calling `vtkURI::Parse(uri.data(), uri.size())`.
   *
   * @param uri the URI string representation, may be empty.
   * @return nullptr if URI syntax checks do not pass, otherwise a new vtkURI.
   */
  static vtkSmartPointer<vtkURI> Parse(const std::string& uri)
  {
    return Parse(uri.data(), uri.size());
  }

  /**
   * @brief Create a new URI from a string.
   *
   * @param uri the URI string representation, must not be nullptr if `size > 0`
   * @param size the URI string representation size, may be `0`
   * @return nullptr if URI syntax checks do not pass, otherwise a new vtkURI.
   */
  static vtkSmartPointer<vtkURI> Parse(const char* uri, std::size_t size);

  /**
   * @brief Resolve an URI from a base URI
   *
   * This implements [RFC3986](https://datatracker.ietf.org/doc/html/rfc3986#section-5).
   * Base URI are used to compose absolute URIs from relative reference.
   *
   * @param baseURI the base URI, if nullptr, this function only checks if `uri` is a complete URI
   * @param uri relative reference that needs to be resolved from `baseURI`
   * @return nullptr if URI syntax checks do not pass, otherwise a new vtkURI.
   */
  static vtkSmartPointer<vtkURI> Resolve(const vtkURI* baseURI, const vtkURI* uri);

  /**
   * @brief URI scheme.
   */
  const vtkURIComponent& GetScheme() const { return this->Scheme; }

  /**
   * @brief URI authority.
   */
  const vtkURIComponent& GetAuthority() const { return this->Authority; }

  /**
   * @brief URI path.
   */
  const vtkURIComponent& GetPath() const { return this->Path; }

  /**
   * @brief URI query.
   */
  const vtkURIComponent& GetQuery() const { return this->Query; }

  /**
   * @brief URI fragment.
   */
  const vtkURIComponent& GetFragment() const { return this->Fragment; }

  ///@{
  /**
   * @brief URI types determination
   *
   * URI can be either:
   * - A full [URI](https://datatracker.ietf.org/doc/html/rfc3986#section-3):
   *   It has a scheme.
   * - an [URI reference](https://datatracker.ietf.org/doc/html/rfc3986#section-4.1):
   *   an URI that is either a relative reference or a full URI.
   * - a [relative reference](https://datatracker.ietf.org/doc/html/rfc3986#section-4.2),
   *   an URI that refers to data that has to be resolved from a base URI prior to loading.
   *   It does not define a scheme but defines at least one other component.
   * - an [absolute URI](https://datatracker.ietf.org/doc/html/rfc3986#section-4.3),
   *   an URI that can be used as a base URI.
   *   It defines a scheme and no fragment. It may define other components.
   * - a [same-document reference](https://datatracker.ietf.org/doc/html/rfc3986#section-4.4):
   *   an URI that defines only a fragment.
   * - An empty URI
   */
  bool IsReference() const { return this->IsRelative() || this->IsFull(); }

  bool IsRelative() const { return !this->Scheme; }

  bool IsAbsolute() const { return this->Scheme && !this->Fragment; }

  bool IsFull() const { return this->Scheme.IsDefined(); }

  bool IsSameDocRef() const
  {
    return !this->Scheme && !this->Authority && this->Path.GetValue().empty() && !this->Query &&
      this->Fragment;
  }

  bool IsEmpty() const
  {
    return !this->Scheme && !this->Authority && this->Path.GetValue().empty() && !this->Query &&
      !this->Fragment;
  }
  ///@}

  /**
   * @return `vtkURI::Clone(this)`
   */
  vtkSmartPointer<vtkURI> Clone() const { return vtkURI::Clone(this); }

  /**
   * @brief Construct the string representation of the URI
   *
   * @return a string representing the URI
   */
  std::string ToString() const;

private:
  /**
   * @brief Private version of vtkURI::Make but does not perform syntax check
   */
  static vtkSmartPointer<vtkURI> MakeUnchecked(vtkURIComponent scheme, vtkURIComponent authority,
    vtkURIComponent path, vtkURIComponent query, vtkURIComponent fragment);

  // These functions are factories and need write access to this class
  friend vtkSmartPointer<vtkURI> MakeUnchecked(vtkURIComponent scheme, vtkURIComponent authority,
    vtkURIComponent path, vtkURIComponent query, vtkURIComponent fragment);
  friend vtkSmartPointer<vtkURI> Clone(const vtkURI* other);

  vtkURI() = default;
  ~vtkURI() override = default;
  vtkURI(const vtkURI&) = delete;
  vtkURI& operator=(const vtkURI&) = delete;

  vtkURIComponent Scheme{};
  vtkURIComponent Authority{};
  vtkURIComponent Path{ "" }; // path is defined but empty by default
  vtkURIComponent Query{};
  vtkURIComponent Fragment{};
};

VTK_ABI_NAMESPACE_END

#endif