LibreOffice Module ucb (master) 1
CurlUri.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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#include "CurlUri.hxx"
21
22#include <sal/log.hxx>
23#include <rtl/uri.hxx>
24#include <rtl/ustrbuf.hxx>
25
26#include <optional>
27
28namespace http_dav_ucp
29{
30const auto DEFAULT_HTTP_PORT = 80;
31const auto DEFAULT_HTTPS_PORT = 443;
32
33static ::std::optional<OUString> GetURLComponent(CURLU& rURI, CURLUPart const what,
34 CURLUcode const expected,
35 unsigned int const flags = 0)
36{
37 char* pPart(nullptr);
38 auto uc = curl_url_get(&rURI, what, &pPart, flags);
39 if (expected != CURLUE_OK && uc == expected)
40 {
41 return ::std::optional<OUString>();
42 }
43 if (uc != CURLUE_OK)
44 {
45 SAL_WARN("ucb.ucp.webdav.curl", "curl_url_get failed: " << what << " " << uc);
47 }
48 assert(pPart);
49 CurlUniquePtr<char> pPart2(pPart);
50 return ::rtl::OStringToOUString(pPart, RTL_TEXTENCODING_UTF8);
51}
52
54{
55 // looks like the result should be the same as the old calculateURI()
56 auto const oURI(GetURLComponent(*m_pUrl, CURLUPART_URL, CURLUE_OK, CURLU_NO_DEFAULT_PORT));
57 assert(oURI);
58 m_URI = *oURI;
59
60 auto const oScheme(GetURLComponent(*m_pUrl, CURLUPART_SCHEME, CURLUE_NO_SCHEME));
61 if (oScheme)
62 {
63 m_Scheme = *oScheme;
64 }
65 auto const oUser(GetURLComponent(*m_pUrl, CURLUPART_USER, CURLUE_NO_USER));
66 if (oUser)
67 {
68 m_User = *oUser;
69 }
70 auto const oPassWord(GetURLComponent(*m_pUrl, CURLUPART_PASSWORD, CURLUE_NO_PASSWORD));
71 if (oPassWord)
72 {
73 m_Password = *oPassWord;
74 }
75 auto const oHost(GetURLComponent(*m_pUrl, CURLUPART_HOST, CURLUE_NO_HOST));
76 if (oHost)
77 {
78 m_Host = *oHost;
79 }
80 // DAV schemes always have port but Content::transfer() is called with
81 // arbitrary URLs so use CURLUE_NO_PORT
82 auto const oPort(GetURLComponent(*m_pUrl, CURLUPART_PORT, CURLUE_NO_PORT, CURLU_DEFAULT_PORT));
83 if (oPort)
84 {
85 m_nPort = oPort->toInt32();
86 }
87
88 auto const oPath(GetURLComponent(*m_pUrl, CURLUPART_PATH, CURLUE_OK));
89 assert(oPath);
90 m_Path = *oPath;
91
92 // note: this used to be added to m_Path because before 2007, ne_uri path contained query/fragment as well :-/
93 auto const oQuery(GetURLComponent(*m_pUrl, CURLUPART_QUERY, CURLUE_NO_QUERY));
94 if (oQuery)
95 {
96 m_QueryAndFragment += "?" + *oQuery;
97 }
98 auto const oFragment(GetURLComponent(*m_pUrl, CURLUPART_FRAGMENT, CURLUE_NO_FRAGMENT));
99 if (oFragment)
100 {
101 m_QueryAndFragment += "#" + *oFragment;
102 }
103}
104
105CurlUri::CurlUri(::std::u16string_view const rURI)
106{
107 // note: in the old implementation, the rURI would be URI-encoded again
108 // here, apparently because it could actually be an IRI (RFC 3987) and
109 // neon didn't support that - not clear if this is a good idea
110
111 m_pUrl.reset(curl_url());
112 if (!m_pUrl)
113 {
114 throw ::std::bad_alloc();
115 }
116
117 // use curl to parse the URI, to get a consistent interpretation
118 if (rURI.find(u'\0') != std::u16string_view::npos)
119 {
121 }
122 OString const utf8URI(OUStringToOString(rURI, RTL_TEXTENCODING_UTF8));
123 auto uc = curl_url_set(m_pUrl.get(), CURLUPART_URL, utf8URI.getStr(), 0);
124 if (uc != CURLUE_OK)
125 {
126 SAL_WARN("ucb.ucp.webdav.curl", "curl_url_set failed: " << uc);
128 }
129
130 Init();
131}
132
133CurlUri::CurlUri(CURLU /*const*/& rUrl)
134 : m_pUrl(curl_url_dup(&rUrl))
135{
136 if (!m_pUrl)
137 {
138 throw ::std::bad_alloc();
139 }
140
141 Init();
142}
143
145 : m_pUrl(curl_url_dup(rOther.m_pUrl.get()))
146 , m_URI(rOther.m_URI)
147 , m_Scheme(rOther.m_Scheme)
148 , m_User(rOther.m_User)
149 , m_Password(rOther.m_Password)
150 , m_Host(rOther.m_Host)
151 , m_nPort(rOther.m_nPort)
152 , m_Path(rOther.m_Path)
153 , m_QueryAndFragment(rOther.m_QueryAndFragment)
154{
155 assert(rOther.m_pUrl);
156 if (!m_pUrl)
157 {
158 throw ::std::bad_alloc();
159 }
160}
161
162void CurlUri::operator=(CurlUri const& rOther)
163{
164 assert(rOther.m_pUrl);
165 m_pUrl.reset(curl_url_dup(rOther.m_pUrl.get()));
166 if (!m_pUrl)
167 {
168 throw ::std::bad_alloc();
169 }
170 m_URI = rOther.m_URI;
171 m_Scheme = rOther.m_Scheme;
172 m_User = rOther.m_User;
173 m_Password = rOther.m_Password;
174 m_Host = rOther.m_Host;
175 m_nPort = rOther.m_nPort;
176 m_Path = rOther.m_Path;
178}
179
180bool CurlUri::operator==(CurlUri const& rOther) const { return m_URI == rOther.m_URI; }
181
183{
184 sal_Int32 nPos = m_Path.lastIndexOf('/');
185 sal_Int32 nTrail = 0;
186 if (nPos == m_Path.getLength() - 1)
187 {
188 // Trailing slash found. Skip.
189 nTrail = 1;
190 nPos = m_Path.lastIndexOf('/', nPos);
191 }
192 if (nPos == -1)
193 {
194 return "/";
195 }
196 return m_Path.copy(nPos + 1, m_Path.getLength() - nPos - 1 - nTrail);
197}
198
200
201void CurlUri::SetScheme(::std::u16string_view const rScheme)
202{
203 OString const utf8URI(OUStringToOString(rScheme, RTL_TEXTENCODING_UTF8));
204 auto uc = curl_url_set(m_pUrl.get(), CURLUPART_SCHEME, utf8URI.getStr(), 0);
205 if (uc != CURLUE_OK)
206 {
207 SAL_WARN("ucb.ucp.webdav.curl", "curl_url_set failed: " << uc);
209 }
210 auto const oURI(GetURLComponent(*m_pUrl, CURLUPART_URL, CURLUE_OK, CURLU_NO_DEFAULT_PORT));
211 assert(oURI);
212 m_URI = *oURI;
213 auto const oScheme(GetURLComponent(*m_pUrl, CURLUPART_SCHEME, CURLUE_NO_SCHEME));
214 if (oScheme)
215 {
216 m_Scheme = *oScheme;
217 }
218}
219
220void CurlUri::AppendPath(::std::u16string_view const rPath)
221{
222 OUStringBuffer path(m_Path);
223 if (path.lastIndexOf('/') != path.getLength() - 1)
224 {
225 path.append("/");
226 }
227 path.append(rPath);
228 OString const utf8Path(OUStringToOString(path, RTL_TEXTENCODING_UTF8));
229 auto uc = curl_url_set(m_pUrl.get(), CURLUPART_PATH, utf8Path.getStr(), 0);
230 if (uc != CURLUE_OK)
231 {
232 SAL_WARN("ucb.ucp.webdav.curl", "curl_url_set failed: " << uc);
234 }
235 auto const oURI(GetURLComponent(*m_pUrl, CURLUPART_URL, CURLUE_OK, CURLU_NO_DEFAULT_PORT));
236 assert(oURI);
237 m_URI = *oURI;
238 auto const oPath(GetURLComponent(*m_pUrl, CURLUPART_PATH, CURLUE_OK));
239 assert(oPath);
240 m_Path = *oPath;
241}
242
243CurlUri CurlUri::CloneWithRelativeRefPathAbsolute(std::u16string_view rRelativeRef) const
244{
245 ::std::unique_ptr<CURLU, deleter_from_fn<CURLU, curl_url_cleanup>> pUrl(
246 curl_url_dup(m_pUrl.get()));
247 size_t indexEnd(rRelativeRef.size());
248 auto const indexQuery(rRelativeRef.find('?'));
249 auto const indexFragment(rRelativeRef.find('#'));
250 CURLUcode uc;
251 if (indexFragment != std::u16string_view::npos)
252 {
253 std::u16string_view const fragment(rRelativeRef.substr(indexFragment + 1));
254 indexEnd = indexFragment;
255 OString const utf8Fragment(OUStringToOString(fragment, RTL_TEXTENCODING_UTF8));
256 uc = curl_url_set(pUrl.get(), CURLUPART_FRAGMENT, utf8Fragment.getStr(), 0);
257 }
258 else
259 {
260 uc = curl_url_set(pUrl.get(), CURLUPART_FRAGMENT, nullptr, 0);
261 }
262 if (uc != CURLUE_OK)
263 {
264 SAL_WARN("ucb.ucp.webdav.curl", "curl_url_set failed: " << uc);
266 }
267 if (indexQuery != std::u16string_view::npos
268 && (indexFragment == std::u16string_view::npos || indexQuery < indexFragment))
269 {
270 std::u16string_view const query(
271 rRelativeRef.substr(indexQuery + 1, indexEnd - indexQuery - 1));
272 indexEnd = indexQuery;
273 OString const utf8Query(OUStringToOString(query, RTL_TEXTENCODING_UTF8));
274 uc = curl_url_set(pUrl.get(), CURLUPART_QUERY, utf8Query.getStr(), 0);
275 }
276 else
277 {
278 uc = curl_url_set(pUrl.get(), CURLUPART_QUERY, nullptr, 0);
279 }
280 if (uc != CURLUE_OK)
281 {
282 SAL_WARN("ucb.ucp.webdav.curl", "curl_url_set failed: " << uc);
284 }
285 std::u16string_view const path(rRelativeRef.substr(0, indexEnd));
286 OString const utf8Path(OUStringToOString(path, RTL_TEXTENCODING_UTF8));
287 uc = curl_url_set(pUrl.get(), CURLUPART_PATH, utf8Path.getStr(), 0);
288 if (uc != CURLUE_OK)
289 {
290 SAL_WARN("ucb.ucp.webdav.curl", "curl_url_set failed: " << uc);
292 }
293 return CurlUri(*pUrl.release());
294}
295
296OUString EncodeSegment(OUString const& rSegment)
297{
298 return rtl::Uri::encode(rSegment, rtl_UriCharClassPchar, rtl_UriEncodeIgnoreEscapes,
299 RTL_TEXTENCODING_UTF8);
300}
301
302OUString DecodeURI(OUString const& rURI)
303{
304 return rtl::Uri::decode(rURI, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8);
305}
306
307OUString ConnectionEndPointString(std::u16string_view rHostName, sal_uInt16 const nPort)
308{
309 OUStringBuffer aBuf;
310
311 // Is host a numeric IPv6 address?
312 if ((rHostName.find(':') != std::u16string_view::npos) && (rHostName[0] != '['))
313 {
314 aBuf.append(OUString::Concat("[") + rHostName + "]");
315 }
316 else
317 {
318 aBuf.append(rHostName);
319 }
320
321 if ((nPort != DEFAULT_HTTP_PORT) && (nPort != DEFAULT_HTTPS_PORT))
322 {
323 aBuf.append(":" + OUString::number(sal_Int32(nPort)));
324 }
325 return aBuf.makeStringAndClear();
326}
327
328} // namespace http_dav_ucp
329
330/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
CurlUri CloneWithRelativeRefPathAbsolute(std::u16string_view rRelativeRef) const
Definition: CurlUri.cxx:243
OUString GetPathBaseName() const
Definition: CurlUri.cxx:182
void SetScheme(::std::u16string_view rScheme)
Definition: CurlUri.cxx:201
CurlUri(CurlUri const &rUri)
Definition: CurlUri.cxx:144
::std::unique_ptr< CURLU, deleter_from_fn< CURLU, curl_url_cleanup > > m_pUrl
native curl representation of parsed URI
Definition: CurlUri.hxx:42
OUString m_QueryAndFragment
Definition: CurlUri.hxx:51
OUString m_URI
duplicate state for quick access to some components
Definition: CurlUri.hxx:44
bool operator==(CurlUri const &rOther) const
Definition: CurlUri.cxx:180
void operator=(CurlUri const &rOther)
Definition: CurlUri.cxx:162
OUString GetPathBaseNameUnescaped() const
Definition: CurlUri.cxx:199
sal_uInt16 m_nPort
Definition: CurlUri.hxx:49
void AppendPath(::std::u16string_view rPath)
Definition: CurlUri.cxx:220
float u
sal_uInt16 nPos
#define SAL_WARN(area, stream)
aBuf
OUString DecodeURI(OUString const &rURI)
Definition: CurlUri.cxx:302
const auto DEFAULT_HTTP_PORT
Definition: CurlUri.cxx:30
static ::std::optional< OUString > GetURLComponent(CURLU &rURI, CURLUPart const what, CURLUcode const expected, unsigned int const flags=0)
Definition: CurlUri.cxx:33
::std::unique_ptr< T, deleter_from_fn< T, curl_free > > CurlUniquePtr
Definition: CurlUri.hxx:36
OUString EncodeSegment(OUString const &rSegment)
Definition: CurlUri.cxx:296
OUString ConnectionEndPointString(std::u16string_view rHostName, sal_uInt16 const nPort)
Definition: CurlUri.cxx:307
const auto DEFAULT_HTTPS_PORT
Definition: CurlUri.cxx:31
int fragment
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
store_handle_type *SAL_CALL query(OStoreObject *pHandle, store_handle_type *)
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)