LibreOffice
LibreOffice 24.2 SDK C/C++ API Reference
uri.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 /*
21  * This file is part of LibreOffice published API.
22  */
23 
24 #ifndef INCLUDED_RTL_URI_HXX
25 #define INCLUDED_RTL_URI_HXX
26 
28 #include "rtl/uri.h"
29 #include "rtl/textenc.h"
30 #include "rtl/ustring.hxx"
31 #include "sal/types.h"
32 
33 #if defined LIBO_INTERNAL_ONLY
34 #include <array>
35 #include <cassert>
36 #include <cstddef>
37 #include <string_view>
38 #include "config_global.h"
39 #endif
40 
41 namespace rtl {
42 
45 class Uri
46 {
47 public:
51  static inline rtl::OUString encode(rtl::OUString const & rText,
52  sal_Bool const * pCharClass,
53  rtl_UriEncodeMechanism eMechanism,
54  rtl_TextEncoding eCharset);
55 
59  static inline rtl::OUString encode(rtl::OUString const & rText,
60  rtl_UriCharClass eCharClass,
61  rtl_UriEncodeMechanism eMechanism,
62  rtl_TextEncoding eCharset);
63 
66  static inline rtl::OUString decode(rtl::OUString const & rText,
67  rtl_UriDecodeMechanism eMechanism,
68  rtl_TextEncoding eCharset);
69 
76  static inline rtl::OUString convertRelToAbs(
77  rtl::OUString const & rBaseUriRef, rtl::OUString const & rRelUriRef);
78 
79 private:
81 
83 
85 
86  void operator =(Uri) SAL_DELETED_FUNCTION;
87 };
88 
90  sal_Bool const * pCharClass,
91  rtl_UriEncodeMechanism eMechanism,
92  rtl_TextEncoding eCharset)
93 {
94  rtl::OUString aResult;
95  rtl_uriEncode(rText.pData,
96  pCharClass,
97  eMechanism,
98  eCharset,
99  &aResult.pData);
100  return aResult;
101 }
102 
104  rtl_UriCharClass eCharClass,
105  rtl_UriEncodeMechanism eMechanism,
106  rtl_TextEncoding eCharset)
107 {
108  rtl::OUString aResult;
109  rtl_uriEncode(rText.pData,
110  rtl_getUriCharClass(eCharClass),
111  eMechanism,
112  eCharset,
113  &aResult.pData);
114  return aResult;
115 }
116 
118  rtl_UriDecodeMechanism eMechanism,
119  rtl_TextEncoding eCharset)
120 {
121  rtl::OUString aResult;
122  rtl_uriDecode(rText.pData,
123  eMechanism,
124  eCharset,
125  &aResult.pData);
126  return aResult;
127 }
128 
130  rtl::OUString const & rRelUriRef)
131 {
132  rtl::OUString aResult;
133  rtl::OUString aException;
135  rBaseUriRef.pData,
136  rRelUriRef.pData, &aResult.pData,
137  &aException.pData))
138  throw MalformedUriException(aException);
139  return aResult;
140 }
141 
142 #if defined LIBO_INTERNAL_ONLY
143 
144 constexpr std::size_t UriCharClassSize = 128;
145 
146 // Create a char class (for use with rtl_uriEncode and rtl::Uri::encode), represented as a
147 // compile-time std::array, from an UTF-8 string literal.
148 //
149 // The given `unencoded` lists each ASCII character once that shall not be encoded. (It uses an
150 // UTF-8 string type to emphasize that its characters' values are always interpreted as ASCII
151 // values.)
152 #if HAVE_CPP_CONSTEVAL
153 consteval
154 #else
155 constexpr
156 #endif
157 auto createUriCharClass(std::u8string_view unencoded)
158 {
159  std::array<sal_Bool, UriCharClassSize> a = {};
160  for (auto c: unencoded) {
161  assert(!a[c]); // would presumably indicate a typo in the `unencoded` argument
162  a[c] = true;
163  }
164  return a;
165 }
166 
167 #endif
168 
169 }
170 
171 #endif // INCLUDED_RTL_URI_HXX
172 
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static rtl::OUString encode(rtl::OUString const &rText, sal_Bool const *pCharClass, rtl_UriEncodeMechanism eMechanism, rtl_TextEncoding eCharset)
A wrapper around rtl_uriEncode() from <rtl/uri.h> (see there), using an array of 128 booleans as char...
Definition: uri.hxx:89
rtl_UriCharClass
Various predefined URI &#39;char classes.
Definition: uri.h:49
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:378
static rtl::OUString decode(rtl::OUString const &rText, rtl_UriDecodeMechanism eMechanism, rtl_TextEncoding eCharset)
A wrapper around rtl_uriDecode() from <rtl/uri.h> (see there).
Definition: uri.hxx:117
SAL_DLLPUBLIC void rtl_uriEncode(rtl_uString *pText, sal_Bool const *pCharClass, rtl_UriEncodeMechanism eMechanism, rtl_TextEncoding eCharset, rtl_uString **pResult) SAL_THROW_EXTERN_C()
Encode a text as (part of) a URI.
unsigned char sal_Bool
Definition: types.h:38
rtl_UriDecodeMechanism
The mechanism describing how rtl_uriDecode() translates (part of) a URI into a Unicode string...
Definition: uri.h:193
A wrapper around the C functions from <rtl/uri.h>.
Definition: uri.hxx:45
sal_uInt16 rtl_TextEncoding
The various supported text encodings.
Definition: textenc.h:37
Definition: bootstrap.hxx:33
static rtl::OUString convertRelToAbs(rtl::OUString const &rBaseUriRef, rtl::OUString const &rRelUriRef)
A wrapper around rtl_uriConvertRelToAbs() from <rtl/uri.h> (see there).
Definition: uri.hxx:129
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:170
An exception indicating a malformed URI.
Definition: malformeduriexception.hxx:35
SAL_DLLPUBLIC void rtl_uriDecode(rtl_uString *pText, rtl_UriDecodeMechanism eMechanism, rtl_TextEncoding eCharset, rtl_uString **pResult) SAL_THROW_EXTERN_C()
Decode (a part of) a URI.
SAL_DLLPUBLIC sal_Bool rtl_uriConvertRelToAbs(rtl_uString *pBaseUriRef, rtl_uString *pRelUriRef, rtl_uString **pResult, rtl_uString **pException) SAL_THROW_EXTERN_C()
Convert a relative URI reference into an absolute URI.
SAL_DLLPUBLIC sal_Bool const * rtl_getUriCharClass(rtl_UriCharClass eCharClass) SAL_THROW_EXTERN_C()
Map a predefined rtl_UriCharClass to a form usable by rtl_uriEncode().
rtl_UriEncodeMechanism
The mechanism describing how escape sequences in the input of rtl_uriEncode() are handled...
Definition: uri.h:134