| 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 |
|
| 10 |
#include "CurlSession.hxx" |
| 11 |
|
| 12 |
#include "SerfLockStore.hxx" |
| 13 |
#include "DAVProperties.hxx" |
| 14 |
#include "UCBDeadPropertyValue.hxx" |
| 15 |
#include "webdavresponseparser.hxx" |
| 16 |
|
| 17 |
#include <cppuhelper/implbase.hxx> |
| 18 |
#include <comphelper/processfactory.hxx> |
| 19 |
#include <comphelper/attributelist.hxx> |
| 20 |
#include <comphelper/scopeguard.hxx> |
| 21 |
#include <comphelper/string.hxx> |
| 22 |
#include <cppuhelper/queryinterface.hxx> |
| 23 |
#include <cppuhelper/supportsservice.hxx> |
| 24 |
|
| 25 |
#include <o3tl/safeint.hxx> |
| 26 |
#include <o3tl/string_view.hxx> |
| 27 |
|
| 28 |
#include <officecfg/Inet.hxx> |
| 29 |
#include <officecfg/Office/Security.hxx> |
| 30 |
|
| 31 |
#include <com/sun/star/beans/NamedValue.hpp> |
| 32 |
#include <com/sun/star/io/Pipe.hpp> |
| 33 |
#include <com/sun/star/lang/XServiceInfo.hpp> |
| 34 |
#include <com/sun/star/io/SequenceInputStream.hpp> |
| 35 |
#include <com/sun/star/io/SequenceOutputStream.hpp> |
| 36 |
#include <com/sun/star/xml/sax/Writer.hpp> |
| 37 |
|
| 38 |
#include <osl/time.h> |
| 39 |
#include <sal/log.hxx> |
| 40 |
#include <rtl/uri.hxx> |
| 41 |
#include <rtl/strbuf.hxx> |
| 42 |
#include <rtl/ustrbuf.hxx> |
| 43 |
#include <systools/curlinit.hxx> |
| 44 |
#include <tools/hostfilter.hxx> |
| 45 |
#include <config_version.h> |
| 46 |
|
| 47 |
#include <map> |
| 48 |
#include <optional> |
| 49 |
#include <tuple> |
| 50 |
#include <utility> |
| 51 |
|
| 52 |
#ifndef _WIN32 |
| 53 |
#include <arpa/inet.h> |
| 54 |
#include <netinet/in.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
using namespace ::com::sun::star; |
| 58 |
|
| 59 |
namespace |
| 60 |
{ |
| 61 |
void lock_cb(CURL*, curl_lock_data, curl_lock_access, void*); |
| 62 |
void unlock_cb(CURL*, curl_lock_data, void*); |
| 63 |
|
| 64 |
/// globals container |
| 65 |
struct Init |
| 66 |
{ |
| 67 |
/// note: LockStore has its own mutex and calls CurlSession from its thread |
| 68 |
/// so don't call LockStore with m_Mutex held to prevent deadlock. |
| 69 |
::http_dav_ucp::SerfLockStore LockStore; |
| 70 |
|
| 71 |
/// libcurl shared data - to store cookies beyond one connection |
| 72 |
::std::mutex ShareLock[CURL_LOCK_DATA_LAST]; |
| 73 |
::std::unique_ptr<CURLSH, http_dav_ucp::deleter_from_fn<CURLSH, curl_share_cleanup>> pShare; |
| 74 |
|
| 75 |
Init() |
| 76 |
{ |
| 77 |
if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) |
| 78 |
{ |
| 79 |
assert(!"curl_global_init failed"); |
| 80 |
::std::abort(); // can't handle error here |
| 81 |
} |
| 82 |
pShare.reset(curl_share_init()); |
| 83 |
if (!pShare) |
| 84 |
{ |
| 85 |
assert(!"curl_share_init failed"); |
| 86 |
::std::abort(); // can't handle error here |
| 87 |
} |
| 88 |
CURLSHcode sh = curl_share_setopt(pShare.get(), CURLSHOPT_LOCKFUNC, lock_cb); |
| 89 |
if (sh != CURLSHE_OK) |
| 90 |
{ |
| 91 |
assert(!"curl_share_setopt failed"); |
| 92 |
::std::abort(); // can't handle error here |
| 93 |
} |
| 94 |
sh = curl_share_setopt(pShare.get(), CURLSHOPT_UNLOCKFUNC, unlock_cb); |
| 95 |
if (sh != CURLSHE_OK) |
| 96 |
{ |
| 97 |
assert(!"curl_share_setopt failed"); |
| 98 |
::std::abort(); // can't handle error here |
| 99 |
} |
| 100 |
sh = curl_share_setopt(pShare.get(), CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE); |
| 101 |
if (sh != CURLSHE_OK) |
| 102 |
{ |
| 103 |
assert(!"curl_share_setopt failed"); |
| 104 |
::std::abort(); // can't handle error here |
| 105 |
} |
| 106 |
sh = curl_share_setopt(pShare.get(), CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS); |
| 107 |
// might fail but this is just a perf improvement |
| 108 |
SAL_WARN_IF(sh != CURLSHE_OK, "ucb.ucp.webdav.curl", "curl_share_setopt failed"); |
| 109 |
sh = curl_share_setopt(pShare.get(), CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION); |
| 110 |
// might fail but this is just a perf improvement |
| 111 |
SAL_WARN_IF(sh != CURLSHE_OK, "ucb.ucp.webdav.curl", "curl_share_setopt failed"); |
| 112 |
// note: CURL_LOCK_DATA_CONNECT isn't safe in a multi threaded program. |
| 113 |
} |
| 114 |
// do not call curl_global_cleanup() - this is not the only client of curl |
| 115 |
}; |
| 116 |
Init g_Init; |
| 117 |
|
| 118 |
// global callbacks |
| 119 |
|
| 120 |
void lock_cb(CURL* /*handle*/, curl_lock_data const data, curl_lock_access /*access*/, |
| 121 |
void* /*userptr*/) |
| 122 |
{ |
| 123 |
assert(0 <= data && data < CURL_LOCK_DATA_LAST); |
| 124 |
try |
| 125 |
{ |
| 126 |
g_Init.ShareLock[data].lock(); |
| 127 |
} |
| 128 |
catch (std::exception const&) |
| 129 |
{ |
| 130 |
::std::abort(); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
void unlock_cb(CURL* /*handle*/, curl_lock_data const data, void* /*userptr*/) |
| 135 |
{ |
| 136 |
assert(0 <= data && data < CURL_LOCK_DATA_LAST); |
| 137 |
g_Init.ShareLock[data].unlock(); |
| 138 |
} |
| 139 |
|
| 140 |
struct ResponseHeaders |
| 141 |
{ |
| 142 |
::std::vector<::std::pair<::std::vector<OString>, ::std::optional<long>>> HeaderFields; |
| 143 |
CURL* pCurl; |
| 144 |
ResponseHeaders(CURL* const i_pCurl) |
| 145 |
: pCurl(i_pCurl) |
| 146 |
{ |
| 147 |
} |
| 148 |
}; |
| 149 |
|
| 150 |
auto GetResponseCode(ResponseHeaders const& rHeaders) -> ::std::optional<long> |
| 151 |
{ |
| 152 |
return (rHeaders.HeaderFields.empty()) ? ::std::optional<long>{} |
| 153 |
: rHeaders.HeaderFields.back().second; |
| 154 |
} |
| 155 |
|
| 156 |
struct DownloadTarget |
| 157 |
{ |
| 158 |
uno::Reference<io::XOutputStream> xOutStream; |
| 159 |
ResponseHeaders const& rHeaders; |
| 160 |
DownloadTarget(uno::Reference<io::XOutputStream> i_xOutStream, |
| 161 |
ResponseHeaders const& i_rHeaders) |
| 162 |
: xOutStream(std::move(i_xOutStream)) |
| 163 |
, rHeaders(i_rHeaders) |
| 164 |
{ |
| 165 |
} |
| 166 |
}; |
| 167 |
|
| 168 |
struct UploadSource |
| 169 |
{ |
| 170 |
uno::Sequence<sal_Int8> const& rInData; |
| 171 |
size_t nPosition; |
| 172 |
UploadSource(uno::Sequence<sal_Int8> const& i_rInData) |
| 173 |
: rInData(i_rInData) |
| 174 |
, nPosition(0) |
| 175 |
{ |
| 176 |
} |
| 177 |
}; |
| 178 |
|
| 179 |
auto GetErrorString(CURLcode const rc, char const* const pErrorBuffer = nullptr) -> OString |
| 180 |
{ |
| 181 |
char const* const pMessage( // static fallback |
| 182 |
(pErrorBuffer && pErrorBuffer[0] != '\0') ? pErrorBuffer : curl_easy_strerror(rc)); |
| 183 |
return OString::Concat("(") + OString::number(sal_Int32(rc)) + ") " + pMessage; |
| 184 |
} |
| 185 |
|
| 186 |
auto GetErrorStringMulti(CURLMcode const mc) -> OString |
| 187 |
{ |
| 188 |
return OString::Concat("(") + OString::number(sal_Int32(mc)) + ") " + curl_multi_strerror(mc); |
| 189 |
} |
| 190 |
|
| 191 |
/// represent an option to be passed to curl_easy_setopt() |
| 192 |
struct CurlOption |
| 193 |
{ |
| 194 |
CURLoption const Option; |
| 195 |
enum class Type |
| 196 |
{ |
| 197 |
Pointer, |
| 198 |
Long, |
| 199 |
CurlOffT |
| 200 |
}; |
| 201 |
Type const Tag; |
| 202 |
union { |
| 203 |
void const* const pValue; |
| 204 |
long /*const*/ lValue; |
| 205 |
curl_off_t /*const*/ cValue; |
| 206 |
}; |
| 207 |
char const* const pExceptionString; |
| 208 |
|
| 209 |
CurlOption(CURLoption const i_Option, void const* const i_Value, |
| 210 |
char const* const i_pExceptionString) |
| 211 |
: Option(i_Option) |
| 212 |
, Tag(Type::Pointer) |
| 213 |
, pValue(i_Value) |
| 214 |
, pExceptionString(i_pExceptionString) |
| 215 |
{ |
| 216 |
} |
| 217 |
// Depending on platform, curl_off_t may be "long" or a larger type |
| 218 |
// so cannot use overloading to distinguish these cases. |
| 219 |
CurlOption(CURLoption const i_Option, curl_off_t const i_Value, |
| 220 |
char const* const i_pExceptionString, Type const type = Type::Long) |
| 221 |
: Option(i_Option) |
| 222 |
, Tag(type) |
| 223 |
, pExceptionString(i_pExceptionString) |
| 224 |
{ |
| 225 |
static_assert(sizeof(long) <= sizeof(curl_off_t)); |
| 226 |
switch (type) |
| 227 |
{ |
| 228 |
case Type::Long: |
| 229 |
lValue = i_Value; |
| 230 |
break; |
| 231 |
case Type::CurlOffT: |
| 232 |
cValue = i_Value; |
| 233 |
break; |
| 234 |
default: |
| 235 |
assert(false); |
| 236 |
} |
| 237 |
} |
| 238 |
}; |
| 239 |
|
| 240 |
/// combined guard class to ensure things are released in correct order, |
| 241 |
/// particularly in ProcessRequest() error handling |
| 242 |
class Guard |
| 243 |
{ |
| 244 |
private: |
| 245 |
/// mutex *first* because m_oGuard requires it |
| 246 |
::std::unique_lock<::std::mutex> m_Lock; |
| 247 |
::std::vector<CurlOption> const m_Options; |
| 248 |
::http_dav_ucp::CurlUri const& m_rURI; |
| 249 |
CURL* const m_pCurl; |
| 250 |
|
| 251 |
public: |
| 252 |
explicit Guard(::std::mutex& rMutex, ::std::vector<CurlOption> aOptions, |
| 253 |
::http_dav_ucp::CurlUri const& rURI, CURL* const pCurl) |
| 254 |
: m_Lock(rMutex, ::std::defer_lock) |
| 255 |
, m_Options(std::move(aOptions)) |
| 256 |
, m_rURI(rURI) |
| 257 |
, m_pCurl(pCurl) |
| 258 |
{ |
| 259 |
Acquire(); |
| 260 |
} |
| 261 |
~Guard() |
| 262 |
{ |
| 263 |
if (m_Lock.owns_lock()) |
| 264 |
{ |
| 265 |
Release(); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
void Acquire() |
| 270 |
{ |
| 271 |
assert(!m_Lock.owns_lock()); |
| 272 |
m_Lock.lock(); |
| 273 |
for (auto const& it : m_Options) |
| 274 |
{ |
| 275 |
CURLcode rc(CURL_LAST); // warning C4701 |
| 276 |
if (it.Tag == CurlOption::Type::Pointer) |
| 277 |
{ |
| 278 |
rc = curl_easy_setopt(m_pCurl, it.Option, it.pValue); |
| 279 |
} |
| 280 |
else if (it.Tag == CurlOption::Type::Long) |
| 281 |
{ |
| 282 |
rc = curl_easy_setopt(m_pCurl, it.Option, it.lValue); |
| 283 |
} |
| 284 |
else if (it.Tag == CurlOption::Type::CurlOffT) |
| 285 |
{ |
| 286 |
rc = curl_easy_setopt(m_pCurl, it.Option, it.cValue); |
| 287 |
} |
| 288 |
else |
| 289 |
{ |
| 290 |
assert(false); |
| 291 |
} |
| 292 |
if (it.pExceptionString != nullptr) |
| 293 |
{ |
| 294 |
if (rc != CURLE_OK) |
| 295 |
{ |
| 296 |
SAL_WARN("ucb.ucp.webdav.curl", |
| 297 |
"set " << it.pExceptionString << " failed: " << GetErrorString(rc)); |
| 298 |
throw ::http_dav_ucp::DAVException( |
| 299 |
::http_dav_ucp::DAVException::DAV_SESSION_CREATE, |
| 300 |
::http_dav_ucp::ConnectionEndPointString(m_rURI.GetHost(), |
| 301 |
m_rURI.GetPort())); |
| 302 |
} |
| 303 |
} |
| 304 |
else // many of the options cannot fail |
| 305 |
{ |
| 306 |
assert(rc == CURLE_OK); |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
void Release() |
| 311 |
{ |
| 312 |
assert(m_Lock.owns_lock()); |
| 313 |
for (auto const& it : m_Options) |
| 314 |
{ |
| 315 |
CURLcode rc(CURL_LAST); // warning C4701 |
| 316 |
if (it.Tag == CurlOption::Type::Pointer) |
| 317 |
{ |
| 318 |
rc = curl_easy_setopt(m_pCurl, it.Option, nullptr); |
| 319 |
} |
| 320 |
else if (it.Tag == CurlOption::Type::Long) |
| 321 |
{ |
| 322 |
rc = curl_easy_setopt(m_pCurl, it.Option, 0L); |
| 323 |
} |
| 324 |
else if (it.Tag == CurlOption::Type::CurlOffT) |
| 325 |
{ |
| 326 |
rc = curl_easy_setopt(m_pCurl, it.Option, curl_off_t(-1)); |
| 327 |
} |
| 328 |
else |
| 329 |
{ |
| 330 |
assert(false); |
| 331 |
} |
| 332 |
assert(rc == CURLE_OK); |
| 333 |
(void)rc; |
| 334 |
} |
| 335 |
m_Lock.unlock(); |
| 336 |
} |
| 337 |
}; |
| 338 |
|
| 339 |
} // namespace |
| 340 |
|
| 341 |
namespace http_dav_ucp |
| 342 |
{ |
| 343 |
// libcurl callbacks: |
| 344 |
|
| 345 |
static int debug_callback(CURL* handle, curl_infotype type, char* data, size_t size, |
| 346 |
void* /*userdata*/) |
| 347 |
{ |
| 348 |
char const* pType(nullptr); |
| 349 |
switch (type) |
| 350 |
{ |
| 351 |
case CURLINFO_TEXT: |
| 352 |
SAL_INFO("ucb.ucp.webdav.curl", "debug log: " << handle << ": " << data); |
| 353 |
return 0; |
| 354 |
case CURLINFO_HEADER_IN: |
| 355 |
SAL_INFO("ucb.ucp.webdav.curl", |
| 356 |
"CURLINFO_HEADER_IN: " << handle << ": " << OString(data, size)); |
| 357 |
return 0; |
| 358 |
case CURLINFO_HEADER_OUT: |
| 359 |
{ |
| 360 |
// unlike IN, this is all headers in one call |
| 361 |
OString tmp(data, size); |
| 362 |
sal_Int32 const start(tmp.indexOf("Authorization: ")); |
| 363 |
if (start != -1) |
| 364 |
{ |
| 365 |
sal_Int32 const end(tmp.indexOf("\r\n", start)); |
| 366 |
assert(end != -1); |
| 367 |
sal_Int32 const len(SAL_N_ELEMENTS("Authorization: ") - 1); |
| 368 |
tmp = tmp.replaceAt( |
| 369 |
start + len, end - start - len, |
| 370 |
Concat2View(OString::number(end - start - len) + " bytes redacted")); |
| 371 |
} |
| 372 |
SAL_INFO("ucb.ucp.webdav.curl", "CURLINFO_HEADER_OUT: " << handle << ": " << tmp); |
| 373 |
return 0; |
| 374 |
} |
| 375 |
case CURLINFO_DATA_IN: |
| 376 |
pType = "CURLINFO_DATA_IN"; |
| 377 |
break; |
| 378 |
case CURLINFO_DATA_OUT: |
| 379 |
pType = "CURLINFO_DATA_OUT"; |
| 380 |
break; |
| 381 |
case CURLINFO_SSL_DATA_IN: |
| 382 |
pType = "CURLINFO_SSL_DATA_IN"; |
| 383 |
break; |
| 384 |
case CURLINFO_SSL_DATA_OUT: |
| 385 |
pType = "CURLINFO_SSL_DATA_OUT"; |
| 386 |
break; |
| 387 |
default: |
| 388 |
SAL_WARN("ucb.ucp.webdav.curl", "unexpected debug log type"); |
| 389 |
return 0; |
| 390 |
} |
| 391 |
SAL_INFO("ucb.ucp.webdav.curl", "debug log: " << handle << ": " << pType << " " << size); |
| 392 |
return 0; |
| 393 |
} |
| 394 |
|
| 395 |
static size_t write_callback(char* const ptr, size_t const size, size_t const nmemb, |
| 396 |
void* const userdata) |
| 397 |
{ |
| 398 |
auto* const pTarget(static_cast<DownloadTarget*>(userdata)); |
| 399 |
if (!pTarget) // looks like ~every request may have a response body |
| 400 |
{ |
| 401 |
return nmemb; |
| 402 |
} |
| 403 |
assert(size == 1); // says the man page |
| 404 |
(void)size; |
| 405 |
assert(pTarget->xOutStream.is()); |
| 406 |
auto const oResponseCode(GetResponseCode(pTarget->rHeaders)); |
| 407 |
if (!oResponseCode) |
| 408 |
{ |
| 409 |
return 0; // that is an error |
| 410 |
} |
| 411 |
// always write, for exception handler in ProcessRequest() |
| 412 |
// if (200 <= *oResponseCode && *oResponseCode < 300) |
| 413 |
{ |
| 414 |
try |
| 415 |
{ |
| 416 |
uno::Sequence<sal_Int8> const data(reinterpret_cast<sal_Int8*>(ptr), nmemb); |
| 417 |
pTarget->xOutStream->writeBytes(data); |
| 418 |
} |
| 419 |
catch (...) |
| 420 |
{ |
| 421 |
SAL_WARN("ucb.ucp.webdav.curl", "exception in write_callback"); |
| 422 |
return 0; // error |
| 423 |
} |
| 424 |
} |
| 425 |
// else: ignore the body? CurlSession will check the status eventually |
| 426 |
return nmemb; |
| 427 |
} |
| 428 |
|
| 429 |
static size_t read_callback(char* const buffer, size_t const size, size_t const nitems, |
| 430 |
void* const userdata) |
| 431 |
{ |
| 432 |
auto* const pSource(static_cast<UploadSource*>(userdata)); |
| 433 |
assert(pSource); |
| 434 |
size_t const nBytes(size * nitems); |
| 435 |
size_t nRet(0); |
| 436 |
try |
| 437 |
{ |
| 438 |
assert(pSource->nPosition <= o3tl::make_unsigned(pSource->rInData.getLength())); |
| 439 |
nRet = ::std::min<size_t>(pSource->rInData.getLength() - pSource->nPosition, nBytes); |
| 440 |
::std::memcpy(buffer, pSource->rInData.getConstArray() + pSource->nPosition, nRet); |
| 441 |
pSource->nPosition += nRet; |
| 442 |
} |
| 443 |
catch (...) |
| 444 |
{ |
| 445 |
SAL_WARN("ucb.ucp.webdav.curl", "exception in read_callback"); |
| 446 |
return CURL_READFUNC_ABORT; // error |
| 447 |
} |
| 448 |
return nRet; |
| 449 |
} |
| 450 |
|
| 451 |
static size_t header_callback(char* const buffer, size_t const size, size_t const nitems, |
| 452 |
void* const userdata) |
| 453 |
{ |
| 454 |
auto* const pHeaders(static_cast<ResponseHeaders*>(userdata)); |
| 455 |
assert(pHeaders); |
| 456 |
#if 0 |
| 457 |
if (!pHeaders) // TODO maybe not needed in every request? not sure |
| 458 |
{ |
| 459 |
return nitems; |
| 460 |
} |
| 461 |
#endif |
| 462 |
assert(size == 1); // says the man page |
| 463 |
(void)size; |
| 464 |
try |
| 465 |
{ |
| 466 |
if (nitems <= 2) |
| 467 |
{ |
| 468 |
// end of header, body follows... |
| 469 |
if (pHeaders->HeaderFields.empty()) |
| 470 |
{ |
| 471 |
SAL_WARN("ucb.ucp.webdav.curl", "header_callback: empty header?"); |
| 472 |
return 0; // error |
| 473 |
} |
| 474 |
// unfortunately there's no separate callback when the body begins, |
| 475 |
// so have to manually retrieve the status code here |
| 476 |
long statusCode(SC_NONE); |
| 477 |
auto rc = curl_easy_getinfo(pHeaders->pCurl, CURLINFO_RESPONSE_CODE, &statusCode); |
| 478 |
assert(rc == CURLE_OK); |
| 479 |
(void)rc; |
| 480 |
// always put the current response code here - wasn't necessarily in this header |
| 481 |
pHeaders->HeaderFields.back().second.emplace(statusCode); |
| 482 |
} |
| 483 |
else if (buffer[0] == ' ' || buffer[0] == '\t') // folded header field? |
| 484 |
{ |
| 485 |
size_t i(0); |
| 486 |
do |
| 487 |
{ |
| 488 |
++i; |
| 489 |
} while (i == ' ' || i == '\t'); |
| 490 |
if (pHeaders->HeaderFields.empty() || pHeaders->HeaderFields.back().second |
| 491 |
|| pHeaders->HeaderFields.back().first.empty()) |
| 492 |
{ |
| 493 |
SAL_WARN("ucb.ucp.webdav.curl", |
| 494 |
"header_callback: folded header field without start"); |
| 495 |
return 0; // error |
| 496 |
} |
| 497 |
pHeaders->HeaderFields.back().first.back() |
| 498 |
+= OString::Concat(" ") + ::std::string_view(&buffer[i], nitems - i); |
| 499 |
} |
| 500 |
else |
| 501 |
{ |
| 502 |
if (pHeaders->HeaderFields.empty() || pHeaders->HeaderFields.back().second) |
| 503 |
{ |
| 504 |
pHeaders->HeaderFields.emplace_back(); |
| 505 |
} |
| 506 |
pHeaders->HeaderFields.back().first.emplace_back(OString(buffer, nitems)); |
| 507 |
} |
| 508 |
} |
| 509 |
catch (...) |
| 510 |
{ |
| 511 |
SAL_WARN("ucb.ucp.webdav.curl", "exception in header_callback"); |
| 512 |
return 0; // error |
| 513 |
} |
| 514 |
return nitems; |
| 515 |
} |
| 516 |
|
| 517 |
static auto ProcessHeaders(::std::vector<OString> const& rHeaders) -> ::std::map<OUString, OUString> |
| 518 |
{ |
| 519 |
::std::map<OUString, OUString> ret; |
| 520 |
for (OString const& rLine : rHeaders) |
| 521 |
{ |
| 522 |
std::string_view line; |
| 523 |
if (!rLine.endsWith("\r\n", &line)) |
| 524 |
{ |
| 525 |
SAL_WARN("ucb.ucp.webdav.curl", "invalid header field (no CRLF)"); |
| 526 |
continue; |
| 527 |
} |
| 528 |
if (o3tl::starts_with(line, "HTTP/") // first line |
| 529 |
|| line.empty()) // last line |
| 530 |
{ |
| 531 |
continue; |
| 532 |
} |
| 533 |
const std::string_view::size_type nColon(line.find(':')); |
| 534 |
if (nColon == std::string_view::npos) |
| 535 |
{ |
| 536 |
{ |
| 537 |
SAL_WARN("ucb.ucp.webdav.curl", "invalid header field (no :)"); |
| 538 |
} |
| 539 |
continue; |
| 540 |
} |
| 541 |
if (nColon == 0) |
| 542 |
{ |
| 543 |
SAL_WARN("ucb.ucp.webdav.curl", "invalid header field (empty name)"); |
| 544 |
continue; |
| 545 |
} |
| 546 |
assert(nColon != std::string_view::npos); |
| 547 |
// case insensitive; must be ASCII |
| 548 |
auto const name(::rtl::OStringToOUString(OString(line.substr(0, nColon)).toAsciiLowerCase(), |
| 549 |
RTL_TEXTENCODING_ASCII_US)); |
| 550 |
std::string_view::size_type nStart(nColon + 1); |
| 551 |
while (nStart < line.size() && (line[nStart] == ' ' || line[nStart] == '\t')) |
| 552 |
{ |
| 553 |
++nStart; |
| 554 |
} |
| 555 |
std::string_view::size_type nEnd(line.size()); |
| 556 |
while (nStart < nEnd && (line[nEnd - 1] == ' ' || line[nEnd - 1] == '\t')) |
| 557 |
{ |
| 558 |
--nEnd; |
| 559 |
} |
| 560 |
// RFC 7230 says that only ASCII works reliably anyway (neon also did this) |
| 561 |
auto const value(::rtl::OStringToOUString(line.substr(nStart, nEnd - nStart), |
| 562 |
RTL_TEXTENCODING_ASCII_US)); |
| 563 |
auto const it(ret.find(name)); |
| 564 |
if (it != ret.end()) |
| 565 |
{ |
| 566 |
it->second = it->second + "," + value; |
| 567 |
} |
| 568 |
else |
| 569 |
{ |
| 570 |
ret[name] = value; |
| 571 |
} |
| 572 |
} |
| 573 |
return ret; |
| 574 |
} |
| 575 |
|
| 576 |
static auto ExtractRequestedHeaders( |
| 577 |
ResponseHeaders const& rHeaders, |
| 578 |
::std::pair<::std::vector<OUString> const&, DAVResource&> const* const pRequestedHeaders) |
| 579 |
-> void |
| 580 |
{ |
| 581 |
::std::map<OUString, OUString> const headerMap( |
| 582 |
ProcessHeaders(rHeaders.HeaderFields.back().first)); |
| 583 |
if (pRequestedHeaders) |
| 584 |
{ |
| 585 |
for (OUString const& rHeader : pRequestedHeaders->first) |
| 586 |
{ |
| 587 |
auto const it(headerMap.find(rHeader.toAsciiLowerCase())); |
| 588 |
if (it != headerMap.end()) |
| 589 |
{ |
| 590 |
DAVPropertyValue value; |
| 591 |
value.IsCaseSensitive = false; |
| 592 |
value.Name = it->first; |
| 593 |
value.Value <<= it->second; |
| 594 |
pRequestedHeaders->second.properties.push_back(value); |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
// this appears to be the only way to get the "realm" from libcurl |
| 601 |
static auto ExtractRealm(ResponseHeaders const& rHeaders, char const* const pAuthHeaderName) |
| 602 |
-> ::std::optional<OUString> |
| 603 |
{ |
| 604 |
::std::map<OUString, OUString> const headerMap( |
| 605 |
ProcessHeaders(rHeaders.HeaderFields.back().first)); |
| 606 |
auto const it(headerMap.find(OUString::createFromAscii(pAuthHeaderName).toAsciiLowerCase())); |
| 607 |
if (it == headerMap.end()) |
| 608 |
{ |
| 609 |
SAL_WARN("ucb.ucp.webdav.curl", "cannot find auth header"); |
| 610 |
return {}; |
| 611 |
} |
| 612 |
// It may be possible that the header contains multiple methods each with |
| 613 |
// a different realm - extract only the first one bc the downstream API |
| 614 |
// only supports one anyway. |
| 615 |
// case insensitive! |
| 616 |
auto i(it->second.toAsciiLowerCase().indexOf("realm=")); |
| 617 |
// is optional |
| 618 |
if (i == -1) |
| 619 |
{ |
| 620 |
SAL_INFO("ucb.ucp.webdav.curl", "auth header has no realm"); |
| 621 |
return {}; |
| 622 |
} |
| 623 |
// no whitespace allowed before or after = |
| 624 |
i += ::std::strlen("realm="); |
| 625 |
if (it->second.getLength() < i + 2 || it->second[i] != '\"') |
| 626 |
{ |
| 627 |
SAL_WARN("ucb.ucp.webdav.curl", "no realm value"); |
| 628 |
return {}; |
| 629 |
} |
| 630 |
++i; |
| 631 |
OUStringBuffer buf; |
| 632 |
while (i < it->second.getLength() && it->second[i] != '\"') |
| 633 |
{ |
| 634 |
if (it->second[i] == '\\') // quoted-pair escape |
| 635 |
{ |
| 636 |
++i; |
| 637 |
if (it->second.getLength() <= i) |
| 638 |
{ |
| 639 |
SAL_WARN("ucb.ucp.webdav.curl", "unterminated quoted-pair"); |
| 640 |
return {}; |
| 641 |
} |
| 642 |
} |
| 643 |
buf.append(it->second[i]); |
| 644 |
++i; |
| 645 |
} |
| 646 |
if (it->second.getLength() <= i) |
| 647 |
{ |
| 648 |
SAL_WARN("ucb.ucp.webdav.curl", "unterminated realm"); |
| 649 |
return {}; |
| 650 |
} |
| 651 |
return buf.makeStringAndClear(); |
| 652 |
} |
| 653 |
|
| 654 |
#ifndef _WIN32 |
| 655 |
|
| 656 |
static std::string makeIPAddress(const sockaddr& ai_addr) |
| 657 |
{ |
| 658 |
char addrstr[INET6_ADDRSTRLEN]; |
| 659 |
|
| 660 |
static_assert(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN, "ipv6 addresses are longer than ipv4"); |
| 661 |
const void* inAddr = nullptr; |
| 662 |
switch (ai_addr.sa_family) |
| 663 |
{ |
| 664 |
case AF_INET: |
| 665 |
{ |
| 666 |
auto ipv4 = reinterpret_cast<const sockaddr_in*>(&ai_addr); |
| 667 |
inAddr = &(ipv4->sin_addr); |
| 668 |
break; |
| 669 |
} |
| 670 |
case AF_INET6: |
| 671 |
{ |
| 672 |
auto ipv6 = reinterpret_cast<const sockaddr_in6*>(&ai_addr); |
| 673 |
inAddr = &(ipv6->sin6_addr); |
| 674 |
break; |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
if (!inAddr) |
| 679 |
{ |
| 680 |
SAL_WARN("ucb.ucp.webdav.curl", "Unknown sa_family: " << ai_addr.sa_family); |
| 681 |
return std::string(); |
| 682 |
} |
| 683 |
|
| 684 |
const char* result = inet_ntop(ai_addr.sa_family, inAddr, addrstr, sizeof(addrstr)); |
| 685 |
if (!result) |
| 686 |
{ |
| 687 |
SAL_WARN("ucb.ucp.webdav.curl", "inet_ntop failure"); |
| 688 |
return std::string(); |
| 689 |
} |
| 690 |
return std::string(result); |
| 691 |
} |
| 692 |
|
| 693 |
// filter out connections to instance metadata |
| 694 |
static curl_socket_t opensocket_callback(void* /*clientp*/, curlsocktype purpose, |
| 695 |
struct curl_sockaddr* address) |
| 696 |
{ |
| 697 |
if (purpose == CURLSOCKTYPE_IPCXN) |
| 698 |
{ |
| 699 |
if (address->family == AF_INET && makeIPAddress(address->addr) == "169.254.169.254") |
| 700 |
{ |
| 701 |
SAL_WARN("ucb.ucp.webdav.curl", "ignoring instance metadata ip"); |
| 702 |
return CURL_SOCKET_BAD; |
| 703 |
} |
| 704 |
else if (address->family == AF_INET6 && makeIPAddress(address->addr) == "fd00:ec2::254") |
| 705 |
{ |
| 706 |
SAL_WARN("ucb.ucp.webdav.curl", "ignoring instance metadata ip"); |
| 707 |
return CURL_SOCKET_BAD; |
| 708 |
} |
| 709 |
} |
| 710 |
return socket(address->family, address->socktype, address->protocol); |
| 711 |
} |
| 712 |
|
| 713 |
#endif |
| 714 |
|
| 715 |
CurlSession::CurlSession(uno::Reference<uno::XComponentContext> xContext, |
| 716 |
::rtl::Reference<DAVSessionFactory> const& rpFactory, OUString const& rURI, |
| 717 |
uno::Sequence<beans::NamedValue> const& rFlags, |
| 718 |
::ucbhelper::InternetProxyDecider const& rProxyDecider) |
| 719 |
: DAVSession(rpFactory) |
| 720 |
, m_xContext(std::move(xContext)) |
| 721 |
, m_Flags(rFlags) |
| 722 |
, m_URI(rURI) |
| 723 |
, m_Proxy(rProxyDecider.getProxy(m_URI.GetScheme(), m_URI.GetHost(), m_URI.GetPort())) |
| 724 |
{ |
| 725 |
assert(m_URI.GetScheme() == "http" || m_URI.GetScheme() == "https"); |
| 726 |
m_pCurlMulti.reset(curl_multi_init()); |
| 727 |
if (!m_pCurlMulti) |
| 728 |
{ |
| 729 |
SAL_WARN("ucb.ucp.webdav.curl", "curl_multi_init failed"); |
| 730 |
throw DAVException(DAVException::DAV_SESSION_CREATE, |
| 731 |
ConnectionEndPointString(m_URI.GetHost(), m_URI.GetPort())); |
| 732 |
} |
| 733 |
m_pCurl.reset(curl_easy_init()); |
| 734 |
if (!m_pCurl) |
| 735 |
{ |
| 736 |
SAL_WARN("ucb.ucp.webdav.curl", "curl_easy_init failed"); |
| 737 |
throw DAVException(DAVException::DAV_SESSION_CREATE, |
| 738 |
ConnectionEndPointString(m_URI.GetHost(), m_URI.GetPort())); |
| 739 |
} |
| 740 |
m_ErrorBuffer[0] = '\0'; |
| 741 |
// this supposedly gives the highest quality error reporting |
| 742 |
auto rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_ERRORBUFFER, m_ErrorBuffer); |
| 743 |
assert(rc == CURLE_OK); |
| 744 |
#if 1 |
| 745 |
// just for debugging... |
| 746 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_DEBUGFUNCTION, debug_callback); |
| 747 |
assert(rc == CURLE_OK); |
| 748 |
#endif |
| 749 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_VERBOSE, 1L); |
| 750 |
assert(rc == CURLE_OK); |
| 751 |
// accept any encoding supported by libcurl |
| 752 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_ACCEPT_ENCODING, ""); |
| 753 |
if (rc != CURLE_OK) |
| 754 |
{ |
| 755 |
SAL_WARN("ucb.ucp.webdav.curl", "CURLOPT_ACCEPT_ENCODING failed: " << GetErrorString(rc)); |
| 756 |
throw DAVException(DAVException::DAV_SESSION_CREATE, |
| 757 |
ConnectionEndPointString(m_URI.GetHost(), m_URI.GetPort())); |
| 758 |
} |
| 759 |
auto const connectTimeout(officecfg::Inet::Settings::ConnectTimeout::get()); |
| 760 |
// default is 300s |
| 761 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_CONNECTTIMEOUT, |
| 762 |
::std::max<long>(2L, ::std::min<long>(connectTimeout, 180L))); |
| 763 |
if (rc != CURLE_OK) |
| 764 |
{ |
| 765 |
SAL_WARN("ucb.ucp.webdav.curl", "CURLOPT_CONNECTTIMEOUT failed: " << GetErrorString(rc)); |
| 766 |
throw DAVException(DAVException::DAV_SESSION_CREATE, |
| 767 |
ConnectionEndPointString(m_URI.GetHost(), m_URI.GetPort())); |
| 768 |
} |
| 769 |
auto const readTimeout(officecfg::Inet::Settings::ReadTimeout::get()); |
| 770 |
m_nReadTimeout = ::std::max<int>(20, ::std::min<long>(readTimeout, 180)) * 1000; |
| 771 |
// default is infinite |
| 772 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_TIMEOUT, 300L); |
| 773 |
if (rc != CURLE_OK) |
| 774 |
{ |
| 775 |
SAL_WARN("ucb.ucp.webdav.curl", "CURLOPT_TIMEOUT failed: " << GetErrorString(rc)); |
| 776 |
throw DAVException(DAVException::DAV_SESSION_CREATE, |
| 777 |
ConnectionEndPointString(m_URI.GetHost(), m_URI.GetPort())); |
| 778 |
} |
| 779 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_WRITEFUNCTION, &write_callback); |
| 780 |
assert(rc == CURLE_OK); |
| 781 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_READFUNCTION, &read_callback); |
| 782 |
assert(rc == CURLE_OK); |
| 783 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_HEADERFUNCTION, &header_callback); |
| 784 |
assert(rc == CURLE_OK); |
| 785 |
::InitCurl_easy(m_pCurl.get()); |
| 786 |
if (officecfg::Office::Security::Net::AllowInsecureProtocols::get()) |
| 787 |
{ |
| 788 |
// tdf#149921 by default, with schannel (WNT) connection fails if revocation |
| 789 |
// lists cannot be checked; try to limit the checking to when revocation |
| 790 |
// lists can actually be retrieved (usually not the case for self-signed CA) |
| 791 |
#if CURL_AT_LEAST_VERSION(7, 70, 0) |
| 792 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_SSL_OPTIONS, CURLSSLOPT_REVOKE_BEST_EFFORT); |
| 793 |
assert(rc == CURLE_OK); |
| 794 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_PROXY_SSL_OPTIONS, |
| 795 |
CURLSSLOPT_REVOKE_BEST_EFFORT); |
| 796 |
assert(rc == CURLE_OK); |
| 797 |
#endif |
| 798 |
} |
| 799 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_SHARE, g_Init.pShare.get()); |
| 800 |
assert(rc == CURLE_OK); |
| 801 |
// set this initially, may be overwritten during authentication |
| 802 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_HTTPAUTH, CURLAUTH_ANY); |
| 803 |
assert(rc == CURLE_OK); // ANY is always available |
| 804 |
// always set CURLOPT_PROXY to suppress proxy detection in libcurl |
| 805 |
OString const utf8Proxy(OUStringToOString(m_Proxy, RTL_TEXTENCODING_UTF8)); |
| 806 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_PROXY, utf8Proxy.getStr()); |
| 807 |
if (rc != CURLE_OK) |
| 808 |
{ |
| 809 |
SAL_WARN("ucb.ucp.webdav.curl", "CURLOPT_PROXY failed: " << GetErrorString(rc)); |
| 810 |
throw DAVException(DAVException::DAV_SESSION_CREATE, m_Proxy); |
| 811 |
} |
| 812 |
if (!m_Proxy.isEmpty()) |
| 813 |
{ |
| 814 |
// set this initially, may be overwritten during authentication |
| 815 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_PROXYAUTH, CURLAUTH_ANY); |
| 816 |
assert(rc == CURLE_OK); // ANY is always available |
| 817 |
} |
| 818 |
auto const it(::std::find_if(m_Flags.begin(), m_Flags.end(), |
| 819 |
[](auto const& rFlag) { return rFlag.Name == "KeepAlive"; })); |
| 820 |
if (it != m_Flags.end() && it->Value.get<bool>()) |
| 821 |
{ |
| 822 |
// neon would close the connection from ne_end_request(), this seems |
| 823 |
// to be the equivalent and not CURLOPT_TCP_KEEPALIVE |
| 824 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_FORBID_REUSE, 1L); |
| 825 |
assert(rc == CURLE_OK); |
| 826 |
} |
| 827 |
if (HostFilter::isExemptVerifyHost(m_URI.GetHost())) |
| 828 |
{ |
| 829 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_SSL_VERIFYPEER, 0L); |
| 830 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_SSL_VERIFYHOST, 0L); |
| 831 |
assert(rc == CURLE_OK); |
| 832 |
} |
| 833 |
|
| 834 |
#ifndef _WIN32 |
| 835 |
if (comphelper::LibreOfficeKit::isActive()) |
| 836 |
{ |
| 837 |
//See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html |
| 838 |
rc = curl_easy_setopt(m_pCurl.get(), CURLOPT_OPENSOCKETFUNCTION, opensocket_callback); |
| 839 |
assert(rc == CURLE_OK); |
| 840 |
} |
| 841 |
#endif |
| 842 |
} |
| 843 |
|
| 844 |
CurlSession::~CurlSession() {} |
| 845 |
|
| 846 |
auto CurlSession::CanUse(OUString const& rURI, uno::Sequence<beans::NamedValue> const& rFlags) |
| 847 |
-> bool |
| 848 |
{ |
| 849 |
try |
| 850 |
{ |
| 851 |
CurlUri const uri(rURI); |
| 852 |
|
| 853 |
return m_URI.GetScheme() == uri.GetScheme() && m_URI.GetHost() == uri.GetHost() |
| 854 |
&& m_URI.GetPort() == uri.GetPort() && m_Flags == rFlags; |
| 855 |
} |
| 856 |
catch (DAVException const&) |
| 857 |
{ |
| 858 |
return false; |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
auto CurlSession::UsesProxy() -> bool |
| 863 |
{ |
| 864 |
assert(m_URI.GetScheme() == "http" || m_URI.GetScheme() == "https"); |
| 865 |
return !m_Proxy.isEmpty(); |
| 866 |
} |
| 867 |
|
| 868 |
auto CurlSession::abort() -> void |
| 869 |
{ |
| 870 |
// note: abort() was a no-op since OOo 3.2 and before that it crashed. |
| 871 |
bool expected(false); |
| 872 |
// it would be pointless to lock m_Mutex here as the other thread holds it |
| 873 |
if (m_AbortFlag.compare_exchange_strong(expected, true)) |
| 874 |
{ |
| 875 |
// This function looks safe to call without m_Mutex as long as the |
| 876 |
// m_pCurlMulti handle is not destroyed, and the caller must own a ref |
| 877 |
// to this object which keeps it alive; it should cause poll to return. |
| 878 |
curl_multi_wakeup(m_pCurlMulti.get()); |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
/// this is just a bunch of static member functions called from CurlSession |
| 883 |
struct CurlProcessor |
| 884 |
{ |
| 885 |
static auto URIReferenceToURI(CurlSession& rSession, std::u16string_view rURIReference) |
| 886 |
-> CurlUri; |
| 887 |
|
| 888 |
static auto ProcessRequestImpl( |
| 889 |
CurlSession& rSession, CurlUri const& rURI, OUString const& rMethod, |
| 890 |
curl_slist* pRequestHeaderList, uno::Reference<io::XOutputStream> const* pxOutStream, |
| 891 |
uno::Sequence<sal_Int8> const* pInData, |
| 892 |
::std::pair<::std::vector<OUString> const&, DAVResource&> const* pRequestedHeaders, |
| 893 |
ResponseHeaders& rHeaders) -> void; |
| 894 |
|
| 895 |
static auto ProcessRequest( |
| 896 |
CurlSession& rSession, CurlUri const& rURI, OUString const& rMethod, |
| 897 |
::std::vector<CurlOption> const& rOptions, DAVRequestEnvironment const* pEnv, |
| 898 |
::std::unique_ptr<curl_slist, deleter_from_fn<curl_slist, curl_slist_free_all>> |
| 899 |
pRequestHeaderList, |
| 900 |
uno::Reference<io::XOutputStream> const* pxOutStream, |
| 901 |
uno::Reference<io::XInputStream> const* pxInStream, |
| 902 |
::std::pair<::std::vector<OUString> const&, DAVResource&> const* pRequestedHeaders) -> void; |
| 903 |
|
| 904 |
static auto |
| 905 |
PropFind(CurlSession& rSession, CurlUri const& rURI, Depth depth, |
| 906 |
::std::tuple<::std::vector<OUString> const&, ::std::vector<DAVResource>* const, |
| 907 |
::std::vector<ucb::Lock>* const> const* o_pRequestedProperties, |
| 908 |
::std::vector<DAVResourceInfo>* const o_pResourceInfos, |
| 909 |
DAVRequestEnvironment const& rEnv) -> void; |
| 910 |
|
| 911 |
static auto MoveOrCopy(CurlSession& rSession, std::u16string_view rSourceURIReference, |
| 912 |
::std::u16string_view rDestinationURI, DAVRequestEnvironment const& rEnv, |
| 913 |
bool isOverwrite, char const* pMethod) -> void; |
| 914 |
|
| 915 |
static auto Lock(CurlSession& rSession, CurlUri const& rURI, DAVRequestEnvironment const* pEnv, |
| 916 |
::std::unique_ptr<curl_slist, deleter_from_fn<curl_slist, curl_slist_free_all>> |
| 917 |
pRequestHeaderList, |
| 918 |
uno::Reference<io::XInputStream> const* pxInStream) |
| 919 |
-> ::std::vector<::std::pair<ucb::Lock, sal_Int32>>; |
| 920 |
|
| 921 |
static auto Unlock(CurlSession& rSession, CurlUri const& rURI, |
| 922 |
DAVRequestEnvironment const* pEnv) -> void; |
| 923 |
}; |
| 924 |
|
| 925 |
auto CurlProcessor::URIReferenceToURI(CurlSession& rSession, std::u16string_view rURIReference) |
| 926 |
-> CurlUri |
| 927 |
{ |
| 928 |
// No need to acquire rSession.m_Mutex because accessed members are const. |
| 929 |
if (rSession.UsesProxy()) |
| 930 |
// very odd, but see DAVResourceAccess::getRequestURI() :-/ |
| 931 |
{ |
| 932 |
assert(o3tl::starts_with(rURIReference, u"http://") |
| 933 |
|| o3tl::starts_with(rURIReference, u"https://")); |
| 934 |
return CurlUri(rURIReference); |
| 935 |
} |
| 936 |
else |
| 937 |
{ |
| 938 |
assert(o3tl::starts_with(rURIReference, u"/")); |
| 939 |
return rSession.m_URI.CloneWithRelativeRefPathAbsolute(rURIReference); |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
/// main function to initiate libcurl requests |
| 944 |
auto CurlProcessor::ProcessRequestImpl( |
| 945 |
CurlSession& rSession, CurlUri const& rURI, OUString const& rMethod, |
| 946 |
curl_slist* const pRequestHeaderList, |
| 947 |
uno::Reference<io::XOutputStream> const* const pxOutStream, |
| 948 |
uno::Sequence<sal_Int8> const* const pInData, |
| 949 |
::std::pair<::std::vector<OUString> const&, DAVResource&> const* const pRequestedHeaders, |
| 950 |
ResponseHeaders& rHeaders) -> void |
| 951 |
{ |
| 952 |
::comphelper::ScopeGuard const g([&]() { |
| 953 |
auto rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_HEADERDATA, nullptr); |
| 954 |
assert(rc == CURLE_OK); |
| 955 |
(void)rc; |
| 956 |
if (pxOutStream) |
| 957 |
{ |
| 958 |
rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_WRITEDATA, nullptr); |
| 959 |
assert(rc == CURLE_OK); |
| 960 |
} |
| 961 |
if (pInData) |
| 962 |
{ |
| 963 |
rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_READDATA, nullptr); |
| 964 |
assert(rc == CURLE_OK); |
| 965 |
rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_UPLOAD, 0L); |
| 966 |
assert(rc == CURLE_OK); |
| 967 |
} |
| 968 |
if (pRequestHeaderList) |
| 969 |
{ |
| 970 |
rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_HTTPHEADER, nullptr); |
| 971 |
assert(rc == CURLE_OK); |
| 972 |
} |
| 973 |
}); |
| 974 |
|
| 975 |
if (pRequestHeaderList) |
| 976 |
{ |
| 977 |
auto rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_HTTPHEADER, pRequestHeaderList); |
| 978 |
assert(rc == CURLE_OK); |
| 979 |
(void)rc; |
| 980 |
} |
| 981 |
|
| 982 |
auto rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_CURLU, rURI.GetCURLU()); |
| 983 |
assert(rc == CURLE_OK); // can't fail since 7.63.0 |
| 984 |
|
| 985 |
rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_HEADERDATA, &rHeaders); |
| 986 |
assert(rc == CURLE_OK); |
| 987 |
::std::optional<DownloadTarget> oDownloadTarget; |
| 988 |
if (pxOutStream) |
| 989 |
{ |
| 990 |
oDownloadTarget.emplace(*pxOutStream, rHeaders); |
| 991 |
rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_WRITEDATA, &*oDownloadTarget); |
| 992 |
assert(rc == CURLE_OK); |
| 993 |
} |
| 994 |
::std::optional<UploadSource> oUploadSource; |
| 995 |
if (pInData) |
| 996 |
{ |
| 997 |
oUploadSource.emplace(*pInData); |
| 998 |
rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_READDATA, &*oUploadSource); |
| 999 |
assert(rc == CURLE_OK); |
| 1000 |
} |
| 1001 |
rSession.m_ErrorBuffer[0] = '\0'; |
| 1002 |
|
| 1003 |
// note: easy handle must be added for *every* transfer! |
| 1004 |
// otherwise it gets stuck in MSTATE_MSGSENT forever after 1st transfer |
| 1005 |
auto mc = curl_multi_add_handle(rSession.m_pCurlMulti.get(), rSession.m_pCurl.get()); |
| 1006 |
if (mc != CURLM_OK) |
| 1007 |
{ |
| 1008 |
SAL_WARN("ucb.ucp.webdav.curl", |
| 1009 |
"curl_multi_add_handle failed: " << GetErrorStringMulti(mc)); |
| 1010 |
throw DAVException( |
| 1011 |
DAVException::DAV_SESSION_CREATE, |
| 1012 |
ConnectionEndPointString(rSession.m_URI.GetHost(), rSession.m_URI.GetPort())); |
| 1013 |
} |
| 1014 |
::comphelper::ScopeGuard const gg([&]() { |
| 1015 |
mc = curl_multi_remove_handle(rSession.m_pCurlMulti.get(), rSession.m_pCurl.get()); |
| 1016 |
if (mc != CURLM_OK) |
| 1017 |
{ |
| 1018 |
SAL_WARN("ucb.ucp.webdav.curl", |
| 1019 |
"curl_multi_remove_handle failed: " << GetErrorStringMulti(mc)); |
| 1020 |
} |
| 1021 |
}); |
| 1022 |
|
| 1023 |
// this is where libcurl actually does something |
| 1024 |
rc = CURL_LAST; // clear current value |
| 1025 |
int nRunning; |
| 1026 |
do |
| 1027 |
{ |
| 1028 |
mc = curl_multi_perform(rSession.m_pCurlMulti.get(), &nRunning); |
| 1029 |
if (mc != CURLM_OK) |
| 1030 |
{ |
| 1031 |
SAL_WARN("ucb.ucp.webdav.curl", |
| 1032 |
"curl_multi_perform failed: " << GetErrorStringMulti(mc)); |
| 1033 |
throw DAVException( |
| 1034 |
DAVException::DAV_HTTP_CONNECT, |
| 1035 |
ConnectionEndPointString(rSession.m_URI.GetHost(), rSession.m_URI.GetPort())); |
| 1036 |
} |
| 1037 |
if (nRunning == 0) |
| 1038 |
{ // short request like HEAD on loopback could be done in first call |
| 1039 |
break; |
| 1040 |
} |
| 1041 |
int nFDs; |
| 1042 |
mc = curl_multi_poll(rSession.m_pCurlMulti.get(), nullptr, 0, rSession.m_nReadTimeout, |
| 1043 |
&nFDs); |
| 1044 |
if (mc != CURLM_OK) |
| 1045 |
{ |
| 1046 |
SAL_WARN("ucb.ucp.webdav.curl", "curl_multi_poll failed: " << GetErrorStringMulti(mc)); |
| 1047 |
throw DAVException( |
| 1048 |
DAVException::DAV_HTTP_CONNECT, |
| 1049 |
ConnectionEndPointString(rSession.m_URI.GetHost(), rSession.m_URI.GetPort())); |
| 1050 |
} |
| 1051 |
if (rSession.m_AbortFlag.load()) |
| 1052 |
{ // flag was set by abort() -> not sure what exception to throw? |
| 1053 |
throw DAVException(DAVException::DAV_HTTP_ERROR, u"abort() was called"_ustr, 0); |
| 1054 |
} |
| 1055 |
} while (nRunning != 0); |
| 1056 |
// there should be exactly 1 CURLMsg now, but the interface is |
| 1057 |
// extensible so future libcurl versions could yield additional things |
| 1058 |
do |
| 1059 |
{ |
| 1060 |
CURLMsg const* const pMsg = curl_multi_info_read(rSession.m_pCurlMulti.get(), &nRunning); |
| 1061 |
if (pMsg && pMsg->msg == CURLMSG_DONE) |
| 1062 |
{ |
| 1063 |
assert(pMsg->easy_handle == rSession.m_pCurl.get()); |
| 1064 |
rc = pMsg->data.result; |
| 1065 |
} |
| 1066 |
else |
| 1067 |
{ |
| 1068 |
SAL_WARN("ucb.ucp.webdav.curl", "curl_multi_info_read unexpected result"); |
| 1069 |
} |
| 1070 |
} while (nRunning != 0); |
| 1071 |
|
| 1072 |
// error handling part 1: libcurl errors |
| 1073 |
if (rc != CURLE_OK) |
| 1074 |
{ |
| 1075 |
// TODO: is there any value in extracting CURLINFO_OS_ERRNO |
| 1076 |
auto const errorString(GetErrorString(rc, rSession.m_ErrorBuffer)); |
| 1077 |
SAL_WARN("ucb.ucp.webdav.curl", "curl_easy_perform failed: " << errorString); |
| 1078 |
switch (rc) |
| 1079 |
{ |
| 1080 |
case CURLE_UNSUPPORTED_PROTOCOL: |
| 1081 |
throw DAVException(DAVException::DAV_UNSUPPORTED, u""_ustr, |
| 1082 |
rtl::OStringToOUString(errorString, RTL_TEXTENCODING_UTF8)); |
| 1083 |
case CURLE_COULDNT_RESOLVE_PROXY: |
| 1084 |
throw DAVException(DAVException::DAV_HTTP_LOOKUP, rSession.m_Proxy, |
| 1085 |
rtl::OStringToOUString(errorString, RTL_TEXTENCODING_UTF8)); |
| 1086 |
case CURLE_COULDNT_RESOLVE_HOST: |
| 1087 |
throw DAVException( |
| 1088 |
DAVException::DAV_HTTP_LOOKUP, |
| 1089 |
ConnectionEndPointString(rSession.m_URI.GetHost(), rSession.m_URI.GetPort()), |
| 1090 |
rtl::OStringToOUString(errorString, RTL_TEXTENCODING_UTF8)); |
| 1091 |
case CURLE_COULDNT_CONNECT: |
| 1092 |
case CURLE_SSL_CONNECT_ERROR: |
| 1093 |
case CURLE_SSL_CERTPROBLEM: |
| 1094 |
case CURLE_SSL_CIPHER: |
| 1095 |
case CURLE_PEER_FAILED_VERIFICATION: |
| 1096 |
case CURLE_SSL_ISSUER_ERROR: |
| 1097 |
case CURLE_SSL_PINNEDPUBKEYNOTMATCH: |
| 1098 |
case CURLE_SSL_INVALIDCERTSTATUS: |
| 1099 |
case CURLE_FAILED_INIT: |
| 1100 |
#if CURL_AT_LEAST_VERSION(7, 69, 0) |
| 1101 |
case CURLE_QUIC_CONNECT_ERROR: |
| 1102 |
#endif |
| 1103 |
throw DAVException( |
| 1104 |
DAVException::DAV_HTTP_CONNECT, |
| 1105 |
ConnectionEndPointString(rSession.m_URI.GetHost(), rSession.m_URI.GetPort()), |
| 1106 |
rtl::OStringToOUString(errorString, RTL_TEXTENCODING_UTF8)); |
| 1107 |
case CURLE_REMOTE_ACCESS_DENIED: |
| 1108 |
case CURLE_LOGIN_DENIED: |
| 1109 |
case CURLE_AUTH_ERROR: |
| 1110 |
throw DAVException( |
| 1111 |
DAVException::DAV_HTTP_AUTH, // probably? |
| 1112 |
ConnectionEndPointString(rSession.m_URI.GetHost(), rSession.m_URI.GetPort()), |
| 1113 |
rtl::OStringToOUString(errorString, RTL_TEXTENCODING_UTF8)); |
| 1114 |
case CURLE_WRITE_ERROR: |
| 1115 |
case CURLE_READ_ERROR: // error returned from our callbacks |
| 1116 |
case CURLE_OUT_OF_MEMORY: |
| 1117 |
case CURLE_ABORTED_BY_CALLBACK: |
| 1118 |
case CURLE_BAD_FUNCTION_ARGUMENT: |
| 1119 |
case CURLE_SEND_ERROR: |
| 1120 |
case CURLE_RECV_ERROR: |
| 1121 |
case CURLE_SSL_CACERT_BADFILE: |
| 1122 |
case CURLE_SSL_CRL_BADFILE: |
| 1123 |
case CURLE_RECURSIVE_API_CALL: |
| 1124 |
throw DAVException( |
| 1125 |
DAVException::DAV_HTTP_FAILED, |
| 1126 |
ConnectionEndPointString(rSession.m_URI.GetHost(), rSession.m_URI.GetPort()), |
| 1127 |
rtl::OStringToOUString(errorString, RTL_TEXTENCODING_UTF8)); |
| 1128 |
case CURLE_OPERATION_TIMEDOUT: |
| 1129 |
throw DAVException( |
| 1130 |
DAVException::DAV_HTTP_TIMEOUT, |
| 1131 |
ConnectionEndPointString(rSession.m_URI.GetHost(), rSession.m_URI.GetPort()), |
| 1132 |
rtl::OStringToOUString(errorString, RTL_TEXTENCODING_UTF8)); |
| 1133 |
default: // lots of generic errors |
| 1134 |
throw DAVException(DAVException::DAV_HTTP_ERROR, u""_ustr, |
| 1135 |
rtl::OStringToOUString(errorString, RTL_TEXTENCODING_UTF8)); |
| 1136 |
} |
| 1137 |
} |
| 1138 |
// error handling part 2: HTTP status codes |
| 1139 |
long statusCode(SC_NONE); |
| 1140 |
rc = curl_easy_getinfo(rSession.m_pCurl.get(), CURLINFO_RESPONSE_CODE, &statusCode); |
| 1141 |
assert(rc == CURLE_OK); |
| 1142 |
assert(statusCode != SC_NONE); // ??? should be error returned from perform? |
| 1143 |
SAL_INFO("ucb.ucp.webdav.curl", "HTTP status code: " << statusCode); |
| 1144 |
if (statusCode < 300) |
| 1145 |
{ |
| 1146 |
// neon did this regardless of status or even error, which seems odd |
| 1147 |
ExtractRequestedHeaders(rHeaders, pRequestedHeaders); |
| 1148 |
} |
| 1149 |
else |
| 1150 |
{ |
| 1151 |
// create message containing the HTTP method and response status line |
| 1152 |
OUString statusLine("\n" + rMethod + "\n=>\n"); |
| 1153 |
if (!rHeaders.HeaderFields.empty() && !rHeaders.HeaderFields.back().first.empty() |
| 1154 |
&& rHeaders.HeaderFields.back().first.front().startsWith("HTTP")) |
| 1155 |
{ |
| 1156 |
statusLine += ::rtl::OStringToOUString( |
| 1157 |
::o3tl::trim(rHeaders.HeaderFields.back().first.front()), |
| 1158 |
RTL_TEXTENCODING_ASCII_US); |
| 1159 |
} |
| 1160 |
switch (statusCode) |
| 1161 |
{ |
| 1162 |
case SC_REQUEST_TIMEOUT: |
| 1163 |
{ |
| 1164 |
throw DAVException( |
| 1165 |
DAVException::DAV_HTTP_TIMEOUT, |
| 1166 |
ConnectionEndPointString(rSession.m_URI.GetHost(), rSession.m_URI.GetPort())); |
| 1167 |
break; |
| 1168 |
} |
| 1169 |
case SC_MOVED_PERMANENTLY: |
| 1170 |
case SC_MOVED_TEMPORARILY: |
| 1171 |
case SC_SEE_OTHER: |
| 1172 |
case SC_TEMPORARY_REDIRECT: |
| 1173 |
{ |
| 1174 |
// could also use CURLOPT_FOLLOWLOCATION but apparently the |
| 1175 |
// upper layer wants to know about redirects? |
| 1176 |
char* pRedirectURL(nullptr); |
| 1177 |
rc = curl_easy_getinfo(rSession.m_pCurl.get(), CURLINFO_REDIRECT_URL, |
| 1178 |
&pRedirectURL); |
| 1179 |
assert(rc == CURLE_OK); |
| 1180 |
if (pRedirectURL) |
| 1181 |
{ |
| 1182 |
// Sharepoint 2016 workaround: contains unencoded U+0020 |
| 1183 |
OUString const redirectURL(::rtl::Uri::encode( |
| 1184 |
OUString(pRedirectURL, strlen(pRedirectURL), RTL_TEXTENCODING_UTF8), |
| 1185 |
rtl_UriCharClassUric, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8)); |
| 1186 |
|
| 1187 |
throw DAVException(DAVException::DAV_HTTP_REDIRECT, redirectURL); |
| 1188 |
} |
| 1189 |
[[fallthrough]]; |
| 1190 |
} |
| 1191 |
default: |
| 1192 |
throw DAVException(DAVException::DAV_HTTP_ERROR, statusLine, statusCode); |
| 1193 |
} |
| 1194 |
} |
| 1195 |
|
| 1196 |
if (pxOutStream) |
| 1197 |
{ |
| 1198 |
(*pxOutStream)->closeOutput(); // signal EOF |
| 1199 |
} |
| 1200 |
} |
| 1201 |
|
| 1202 |
static auto TryRemoveExpiredLockToken(CurlSession& rSession, CurlUri const& rURI, |
| 1203 |
DAVRequestEnvironment const* const pEnv) -> bool |
| 1204 |
{ |
| 1205 |
if (!pEnv) |
| 1206 |
{ |
| 1207 |
// caller was a NonInteractive_*LOCK function anyway, its caller is LockStore |
| 1208 |
return false; |
| 1209 |
} |
| 1210 |
OUString const* const pToken(g_Init.LockStore.getLockTokenForURI(rURI.GetURI(), nullptr)); |
| 1211 |
if (!pToken) |
| 1212 |
{ |
| 1213 |
return false; |
| 1214 |
} |
| 1215 |
try |
| 1216 |
{ |
| 1217 |
// determine validity of existing lock via lockdiscovery request |
| 1218 |
::std::vector<OUString> const propertyNames{ DAVProperties::LOCKDISCOVERY }; |
| 1219 |
::std::vector<ucb::Lock> locks; |
| 1220 |
::std::tuple<::std::vector<OUString> const&, ::std::vector<DAVResource>* const, |
| 1221 |
::std::vector<ucb::Lock>* const> const args(propertyNames, nullptr, &locks); |
| 1222 |
|
| 1223 |
CurlProcessor::PropFind(rSession, rURI, DAVZERO, &args, nullptr, *pEnv); |
| 1224 |
|
| 1225 |
// https://datatracker.ietf.org/doc/html/rfc4918#section-15.8 |
| 1226 |
// The response MAY not contain tokens, but hopefully it |
| 1227 |
// will if client is properly authenticated. |
| 1228 |
if (::std::any_of(locks.begin(), locks.end(), [pToken](ucb::Lock const& rLock) { |
| 1229 |
return ::std::any_of( |
| 1230 |
rLock.LockTokens.begin(), rLock.LockTokens.end(), |
| 1231 |
[pToken](OUString const& rToken) { return *pToken == rToken; }); |
| 1232 |
})) |
| 1233 |
{ |
| 1234 |
return false; // still have the lock |
| 1235 |
} |
| 1236 |
|
| 1237 |
SAL_INFO("ucb.ucp.webdav.curl", |
| 1238 |
"lock token expired, removing: " << rURI.GetURI() << " " << *pToken); |
| 1239 |
g_Init.LockStore.removeLock(rURI.GetURI()); |
| 1240 |
return true; |
| 1241 |
} |
| 1242 |
catch (DAVException const&) |
| 1243 |
{ |
| 1244 |
return false; // ignore, the caller already has a better exception |
| 1245 |
} |
| 1246 |
} |
| 1247 |
|
| 1248 |
auto CurlProcessor::ProcessRequest( |
| 1249 |
CurlSession& rSession, CurlUri const& rURI, OUString const& rMethod, |
| 1250 |
::std::vector<CurlOption> const& rOptions, DAVRequestEnvironment const* const pEnv, |
| 1251 |
::std::unique_ptr<curl_slist, deleter_from_fn<curl_slist, curl_slist_free_all>> |
| 1252 |
pRequestHeaderList, |
| 1253 |
uno::Reference<io::XOutputStream> const* const pxOutStream, |
| 1254 |
uno::Reference<io::XInputStream> const* const pxInStream, |
| 1255 |
::std::pair<::std::vector<OUString> const&, DAVResource&> const* const pRequestedHeaders) |
| 1256 |
-> void |
| 1257 |
{ |
| 1258 |
if (HostFilter::isForbidden(rURI.GetHost())) |
| 1259 |
{ |
| 1260 |
SAL_WARN("ucb.ucp.webdav.curl", "Access denied to host: " << rURI.GetHost()); |
| 1261 |
throw uno::RuntimeException(u"access to host denied"_ustr); |
| 1262 |
} |
| 1263 |
|
| 1264 |
if (pEnv) |
| 1265 |
{ // add custom request headers passed by caller |
| 1266 |
for (auto const& rHeader : pEnv->m_aRequestHeaders) |
| 1267 |
{ |
| 1268 |
OString const utf8Header( |
| 1269 |
OUStringToOString(rHeader.first, RTL_TEXTENCODING_ASCII_US) + ": " |
| 1270 |
+ OUStringToOString(rHeader.second, RTL_TEXTENCODING_ASCII_US)); |
| 1271 |
pRequestHeaderList.reset( |
| 1272 |
curl_slist_append(pRequestHeaderList.release(), utf8Header.getStr())); |
| 1273 |
if (!pRequestHeaderList) |
| 1274 |
{ |
| 1275 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 1276 |
} |
| 1277 |
} |
| 1278 |
} |
| 1279 |
|
| 1280 |
uno::Sequence<sal_Int8> data; |
| 1281 |
if (pxInStream) |
| 1282 |
{ |
| 1283 |
uno::Reference<io::XSeekable> const xSeekable(*pxInStream, uno::UNO_QUERY); |
| 1284 |
if (xSeekable.is()) |
| 1285 |
{ |
| 1286 |
auto const len(xSeekable->getLength() - xSeekable->getPosition()); |
| 1287 |
if ((**pxInStream).readBytes(data, len) != len) |
| 1288 |
{ |
| 1289 |
throw uno::RuntimeException(u"short readBytes"_ustr); |
| 1290 |
} |
| 1291 |
} |
| 1292 |
else |
| 1293 |
{ |
| 1294 |
::std::vector<uno::Sequence<sal_Int8>> bufs; |
| 1295 |
bool isDone(false); |
| 1296 |
do |
| 1297 |
{ |
| 1298 |
bufs.emplace_back(); |
| 1299 |
isDone = (**pxInStream).readSomeBytes(bufs.back(), 65536) == 0; |
| 1300 |
} while (!isDone); |
| 1301 |
sal_Int32 nSize(0); |
| 1302 |
for (auto const& rBuf : bufs) |
| 1303 |
{ |
| 1304 |
if (o3tl::checked_add(nSize, rBuf.getLength(), nSize)) |
| 1305 |
{ |
| 1306 |
throw std::bad_alloc(); // too large for Sequence |
| 1307 |
} |
| 1308 |
} |
| 1309 |
data.realloc(nSize); |
| 1310 |
size_t nCopied(0); |
| 1311 |
for (auto const& rBuf : bufs) |
| 1312 |
{ |
| 1313 |
::std::memcpy(data.getArray() + nCopied, rBuf.getConstArray(), rBuf.getLength()); |
| 1314 |
nCopied += rBuf.getLength(); // can't overflow |
| 1315 |
} |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
// Clear flag before transfer starts; only a transfer started before |
| 1320 |
// calling abort() will be aborted, not one started later. |
| 1321 |
rSession.m_AbortFlag.store(false); |
| 1322 |
|
| 1323 |
Guard guard(rSession.m_Mutex, rOptions, rURI, rSession.m_pCurl.get()); |
| 1324 |
|
| 1325 |
// authentication data may be in the URI, or requested via XInteractionHandler |
| 1326 |
struct Auth |
| 1327 |
{ |
| 1328 |
OUString UserName; |
| 1329 |
OUString PassWord; |
| 1330 |
decltype(CURLAUTH_ANY) AuthMask; ///< allowed auth methods |
| 1331 |
Auth(OUString aUserName, OUString aPassword, decltype(CURLAUTH_ANY) aAuthMask) |
| 1332 |
: UserName(std::move(aUserName)) |
| 1333 |
, PassWord(std::move(aPassword)) |
| 1334 |
, AuthMask(aAuthMask) |
| 1335 |
{ |
| 1336 |
} |
| 1337 |
}; |
| 1338 |
::std::optional<Auth> oAuth; |
| 1339 |
::std::optional<Auth> oAuthProxy; |
| 1340 |
if (pEnv && !rSession.m_isAuthenticatedProxy && !rSession.m_Proxy.isEmpty()) |
| 1341 |
{ |
| 1342 |
try |
| 1343 |
{ |
| 1344 |
// the hope is that this must be a URI |
| 1345 |
CurlUri const uri(rSession.m_Proxy); |
| 1346 |
if (!uri.GetUser().isEmpty() || !uri.GetPassword().isEmpty()) |
| 1347 |
{ |
| 1348 |
oAuthProxy.emplace(uri.GetUser(), uri.GetPassword(), CURLAUTH_ANY); |
| 1349 |
} |
| 1350 |
} |
| 1351 |
catch (DAVException&) |
| 1352 |
{ |
| 1353 |
// ignore any parsing failure here |
| 1354 |
} |
| 1355 |
} |
| 1356 |
decltype(CURLAUTH_ANY) const authSystem(CURLAUTH_NEGOTIATE | CURLAUTH_NTLM | CURLAUTH_NTLM_WB); |
| 1357 |
if (pRequestedHeaders || (pEnv && !rSession.m_isAuthenticated)) |
| 1358 |
{ |
| 1359 |
// m_aRequestURI *may* be a path or *may* be URI - wtf |
| 1360 |
// TODO: why is there this m_aRequestURI and also rURIReference argument? |
| 1361 |
// ... only caller is DAVResourceAccess - always identical except MOVE/COPY |
| 1362 |
// which doesn't work if it's just a URI reference so let's just use |
| 1363 |
// rURIReference via rURI instead |
| 1364 |
#if 0 |
| 1365 |
CurlUri const uri(pEnv->m_aRequestURI); |
| 1366 |
#endif |
| 1367 |
// note: due to parsing bug pwd didn't work in previous webdav ucps |
| 1368 |
if (pEnv && !rSession.m_isAuthenticated |
| 1369 |
&& (!rURI.GetUser().isEmpty() || !rURI.GetPassword().isEmpty())) |
| 1370 |
{ |
| 1371 |
oAuth.emplace(rURI.GetUser(), rURI.GetPassword(), CURLAUTH_ANY); |
| 1372 |
} |
| 1373 |
if (pRequestedHeaders) |
| 1374 |
{ |
| 1375 |
// note: Previously this would be the rURIReference directly but |
| 1376 |
// that ends up in CurlUri anyway and curl is unhappy. |
| 1377 |
// But it looks like all consumers of this .uri are interested |
| 1378 |
// only in the path, so it shouldn't make a difference to give |
| 1379 |
// the entire URI when the caller extracts the path anyway. |
| 1380 |
pRequestedHeaders->second.uri = rURI.GetURI(); |
| 1381 |
pRequestedHeaders->second.properties.clear(); |
| 1382 |
} |
| 1383 |
} |
| 1384 |
bool isRetry(false); |
| 1385 |
bool isFallbackHTTP10(false); |
| 1386 |
int nAuthRequests(0); |
| 1387 |
int nAuthRequestsProxy(0); |
| 1388 |
|
| 1389 |
// libcurl does not have an authentication callback so handle auth |
| 1390 |
// related status codes and requesting credentials via this loop |
| 1391 |
do |
| 1392 |
{ |
| 1393 |
isRetry = false; |
| 1394 |
|
| 1395 |
// re-check m_isAuthenticated flags every time, could have been set |
| 1396 |
// by re-entrant call |
| 1397 |
if (oAuth && !rSession.m_isAuthenticated) |
| 1398 |
{ |
| 1399 |
OString const utf8UserName(OUStringToOString(oAuth->UserName, RTL_TEXTENCODING_UTF8)); |
| 1400 |
auto rc |
| 1401 |
= curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_USERNAME, utf8UserName.getStr()); |
| 1402 |
if (rc != CURLE_OK) |
| 1403 |
{ |
| 1404 |
SAL_WARN("ucb.ucp.webdav.curl", "CURLOPT_USERNAME failed: " << GetErrorString(rc)); |
| 1405 |
throw DAVException(DAVException::DAV_INVALID_ARG); |
| 1406 |
} |
| 1407 |
OString const utf8PassWord(OUStringToOString(oAuth->PassWord, RTL_TEXTENCODING_UTF8)); |
| 1408 |
rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_PASSWORD, utf8PassWord.getStr()); |
| 1409 |
if (rc != CURLE_OK) |
| 1410 |
{ |
| 1411 |
SAL_WARN("ucb.ucp.webdav.curl", "CURLOPT_PASSWORD failed: " << GetErrorString(rc)); |
| 1412 |
throw DAVException(DAVException::DAV_INVALID_ARG); |
| 1413 |
} |
| 1414 |
rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_HTTPAUTH, oAuth->AuthMask); |
| 1415 |
if (rc != CURLE_OK) |
| 1416 |
{ // NEGOTIATE typically disabled on Linux, NTLM is optional too |
| 1417 |
assert(rc == CURLE_NOT_BUILT_IN); |
| 1418 |
SAL_INFO("ucb.ucp.webdav.curl", "no auth method available"); |
| 1419 |
throw DAVException( |
| 1420 |
DAVException::DAV_HTTP_NOAUTH, |
| 1421 |
ConnectionEndPointString(rSession.m_URI.GetHost(), rSession.m_URI.GetPort())); |
| 1422 |
} |
| 1423 |
} |
| 1424 |
|
| 1425 |
if (oAuthProxy && !rSession.m_isAuthenticatedProxy) |
| 1426 |
{ |
| 1427 |
OString const utf8UserName( |
| 1428 |
OUStringToOString(oAuthProxy->UserName, RTL_TEXTENCODING_UTF8)); |
| 1429 |
auto rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_PROXYUSERNAME, |
| 1430 |
utf8UserName.getStr()); |
| 1431 |
if (rc != CURLE_OK) |
| 1432 |
{ |
| 1433 |
SAL_WARN("ucb.ucp.webdav.curl", |
| 1434 |
"CURLOPT_PROXYUSERNAME failed: " << GetErrorString(rc)); |
| 1435 |
throw DAVException(DAVException::DAV_INVALID_ARG); |
| 1436 |
} |
| 1437 |
OString const utf8PassWord( |
| 1438 |
OUStringToOString(oAuthProxy->PassWord, RTL_TEXTENCODING_UTF8)); |
| 1439 |
rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_PROXYPASSWORD, |
| 1440 |
utf8PassWord.getStr()); |
| 1441 |
if (rc != CURLE_OK) |
| 1442 |
{ |
| 1443 |
SAL_WARN("ucb.ucp.webdav.curl", |
| 1444 |
"CURLOPT_PROXYPASSWORD failed: " << GetErrorString(rc)); |
| 1445 |
throw DAVException(DAVException::DAV_INVALID_ARG); |
| 1446 |
} |
| 1447 |
rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_PROXYAUTH, oAuthProxy->AuthMask); |
| 1448 |
if (rc != CURLE_OK) |
| 1449 |
{ // NEGOTIATE typically disabled on Linux, NTLM is optional too |
| 1450 |
assert(rc == CURLE_NOT_BUILT_IN); |
| 1451 |
SAL_INFO("ucb.ucp.webdav.curl", "no auth method available"); |
| 1452 |
throw DAVException( |
| 1453 |
DAVException::DAV_HTTP_NOAUTH, |
| 1454 |
ConnectionEndPointString(rSession.m_URI.GetHost(), rSession.m_URI.GetPort())); |
| 1455 |
} |
| 1456 |
} |
| 1457 |
|
| 1458 |
ResponseHeaders headers(rSession.m_pCurl.get()); |
| 1459 |
// always pass a stream for debug logging, buffer the result body |
| 1460 |
uno::Reference<io::XSequenceOutputStream> const xSeqOutStream( |
| 1461 |
io::SequenceOutputStream::create(rSession.m_xContext)); |
| 1462 |
uno::Reference<io::XOutputStream> const xTempOutStream(xSeqOutStream); |
| 1463 |
assert(xTempOutStream.is()); |
| 1464 |
|
| 1465 |
try |
| 1466 |
{ |
| 1467 |
ProcessRequestImpl(rSession, rURI, rMethod, pRequestHeaderList.get(), &xTempOutStream, |
| 1468 |
pxInStream ? &data : nullptr, pRequestedHeaders, headers); |
| 1469 |
if (pxOutStream) |
| 1470 |
{ // only copy to result stream if transfer was successful |
| 1471 |
(*pxOutStream)->writeBytes(xSeqOutStream->getWrittenBytes()); |
| 1472 |
(*pxOutStream)->closeOutput(); // signal EOF |
| 1473 |
} |
| 1474 |
} |
| 1475 |
catch (DAVException const& rException) |
| 1476 |
{ |
| 1477 |
// log start of request body if there was any |
| 1478 |
auto const bytes(xSeqOutStream->getWrittenBytes()); |
| 1479 |
auto const len(::std::min<sal_Int32>(bytes.getLength(), 10000)); |
| 1480 |
SAL_INFO("ucb.ucp.webdav.curl", |
| 1481 |
"DAVException; (first) " << len << " bytes of data received:"); |
| 1482 |
if (0 < len) |
| 1483 |
{ |
| 1484 |
OStringBuffer buf(len); |
| 1485 |
for (sal_Int32 i = 0; i < len; ++i) |
| 1486 |
{ |
| 1487 |
if (bytes[i] < 0x20) // also if negative |
| 1488 |
{ |
| 1489 |
static char const hexDigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7', |
| 1490 |
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
| 1491 |
buf.append(OString::Concat("\\x") |
| 1492 |
+ OStringChar(hexDigit[static_cast<sal_uInt8>(bytes[i]) >> 4]) |
| 1493 |
+ OStringChar(hexDigit[bytes[i] & 0x0F])); |
| 1494 |
} |
| 1495 |
else |
| 1496 |
{ |
| 1497 |
buf.append(static_cast<char>(bytes[i])); |
| 1498 |
} |
| 1499 |
} |
| 1500 |
SAL_INFO("ucb.ucp.webdav.curl", buf.makeStringAndClear()); |
| 1501 |
} |
| 1502 |
|
| 1503 |
// error handling part 3: special HTTP status codes |
| 1504 |
// that require unlocking m_Mutex to handle |
| 1505 |
if (rException.getError() == DAVException::DAV_HTTP_ERROR) |
| 1506 |
{ |
| 1507 |
auto const statusCode(rException.getStatus()); |
| 1508 |
switch (statusCode) |
| 1509 |
{ |
| 1510 |
case SC_LOCKED: |
| 1511 |
{ |
| 1512 |
guard.Release(); // release m_Mutex before accessing LockStore |
| 1513 |
if (g_Init.LockStore.getLockTokenForURI(rURI.GetURI(), nullptr)) |
| 1514 |
{ |
| 1515 |
throw DAVException(DAVException::DAV_LOCKED_SELF); |
| 1516 |
} |
| 1517 |
else // locked by third party |
| 1518 |
{ |
| 1519 |
throw DAVException(DAVException::DAV_LOCKED); |
| 1520 |
} |
| 1521 |
break; |
| 1522 |
} |
| 1523 |
case SC_PRECONDITION_FAILED: |
| 1524 |
case SC_BAD_REQUEST: |
| 1525 |
{ |
| 1526 |
guard.Release(); // release m_Mutex before accessing LockStore |
| 1527 |
// Not obvious but apparently these codes may indicate |
| 1528 |
// the expiration of a lock. |
| 1529 |
// Initiate a new request *outside* ProcessRequestImpl |
| 1530 |
// *after* rGuard.unlock() to avoid messing up m_pCurl state. |
| 1531 |
if (TryRemoveExpiredLockToken(rSession, rURI, pEnv)) |
| 1532 |
{ |
| 1533 |
throw DAVException(DAVException::DAV_LOCK_EXPIRED); |
| 1534 |
} |
| 1535 |
break; |
| 1536 |
} |
| 1537 |
case SC_FORBIDDEN: |
| 1538 |
{ |
| 1539 |
::std::map<OUString, OUString> const headerMap( |
| 1540 |
ProcessHeaders(headers.HeaderFields.back().first)); |
| 1541 |
// X-MSDAVEXT_Error see [MS-WEBDAVE] 2.2.3.1.9 |
| 1542 |
auto const it(headerMap.find(u"x-msdavext_error"_ustr)); |
| 1543 |
if (it == headerMap.end() || !it->second.startsWith("917656;")) |
| 1544 |
{ |
| 1545 |
break; |
| 1546 |
} |
| 1547 |
// fallback needs cookie engine enabled |
| 1548 |
CURLcode rc |
| 1549 |
= curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_COOKIEFILE, ""); |
| 1550 |
assert(rc == CURLE_OK); |
| 1551 |
(void)rc; |
| 1552 |
SAL_INFO("ucb.ucp.webdav.curl", "403 fallback authentication"); |
| 1553 |
// disable 302 redirect |
| 1554 |
pRequestHeaderList.reset(curl_slist_append( |
| 1555 |
pRequestHeaderList.release(), "X-FORMS_BASED_AUTH_ACCEPTED: f")); |
| 1556 |
if (!pRequestHeaderList) |
| 1557 |
{ |
| 1558 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 1559 |
} |
| 1560 |
} |
| 1561 |
[[fallthrough]]; // SP, no cookie, or cookie failed: try NTLM/Negotiate |
| 1562 |
case SC_UNAUTHORIZED: |
| 1563 |
case SC_PROXY_AUTHENTICATION_REQUIRED: |
| 1564 |
{ |
| 1565 |
(statusCode != SC_PROXY_AUTHENTICATION_REQUIRED |
| 1566 |
? rSession.m_isAuthenticated |
| 1567 |
: rSession.m_isAuthenticatedProxy) |
| 1568 |
= false; // any auth data in m_pCurl is invalid |
| 1569 |
auto& rnAuthRequests(statusCode != SC_PROXY_AUTHENTICATION_REQUIRED |
| 1570 |
? nAuthRequests |
| 1571 |
: nAuthRequestsProxy); |
| 1572 |
if (rnAuthRequests == 10) |
| 1573 |
{ |
| 1574 |
SAL_INFO("ucb.ucp.webdav.curl", "aborting authentication after " |
| 1575 |
<< rnAuthRequests << " attempts"); |
| 1576 |
} |
| 1577 |
else if (pEnv && pEnv->m_xAuthListener) |
| 1578 |
{ |
| 1579 |
::std::optional<OUString> const oRealm( |
| 1580 |
ExtractRealm(headers, statusCode != SC_PROXY_AUTHENTICATION_REQUIRED |
| 1581 |
? "WWW-Authenticate" |
| 1582 |
: "Proxy-Authenticate")); |
| 1583 |
|
| 1584 |
::std::optional<Auth>& roAuth( |
| 1585 |
statusCode != SC_PROXY_AUTHENTICATION_REQUIRED ? oAuth |
| 1586 |
: oAuthProxy); |
| 1587 |
OUString userName(roAuth ? roAuth->UserName : OUString()); |
| 1588 |
OUString passWord(roAuth ? roAuth->PassWord : OUString()); |
| 1589 |
long authAvail(0); |
| 1590 |
auto rc |
| 1591 |
= curl_easy_getinfo(rSession.m_pCurl.get(), |
| 1592 |
statusCode != SC_PROXY_AUTHENTICATION_REQUIRED |
| 1593 |
? CURLINFO_HTTPAUTH_AVAIL |
| 1594 |
: CURLINFO_PROXYAUTH_AVAIL, |
| 1595 |
&authAvail); |
| 1596 |
assert(rc == CURLE_OK); |
| 1597 |
if (statusCode == SC_FORBIDDEN) |
| 1598 |
{ // SharePoint fallback: try Negotiate auth |
| 1599 |
assert(authAvail == 0); |
| 1600 |
// note: this must be a single value! |
| 1601 |
// would need 2 iterations to try CURLAUTH_NTLM too |
| 1602 |
rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_HTTPAUTH, |
| 1603 |
CURLAUTH_NEGOTIATE); |
| 1604 |
if (rc == CURLE_OK) |
| 1605 |
{ |
| 1606 |
authAvail = CURLAUTH_NEGOTIATE; |
| 1607 |
} |
| 1608 |
else |
| 1609 |
{ |
| 1610 |
rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_HTTPAUTH, |
| 1611 |
CURLAUTH_NTLM); |
| 1612 |
if (rc == CURLE_OK) |
| 1613 |
{ |
| 1614 |
authAvail = CURLAUTH_NTLM; |
| 1615 |
} |
| 1616 |
else |
| 1617 |
{ // can't work |
| 1618 |
SAL_INFO("ucb.ucp.webdav.curl", |
| 1619 |
"no SP fallback auth method available"); |
| 1620 |
throw DAVException( |
| 1621 |
DAVException::DAV_HTTP_NOAUTH, |
| 1622 |
ConnectionEndPointString(rSession.m_URI.GetHost(), |
| 1623 |
rSession.m_URI.GetPort())); |
| 1624 |
} |
| 1625 |
} |
| 1626 |
} |
| 1627 |
// only allow SystemCredentials once - the |
| 1628 |
// PasswordContainer may have stored it in the |
| 1629 |
// Config (TrySystemCredentialsFirst or |
| 1630 |
// AuthenticateUsingSystemCredentials) and then it |
| 1631 |
// will always force its use no matter how hopeless |
| 1632 |
bool const isSystemCredSupported((authAvail & authSystem) != 0 |
| 1633 |
&& rnAuthRequests == 0); |
| 1634 |
++rnAuthRequests; |
| 1635 |
|
| 1636 |
// Ask user via XInteractionHandler. |
| 1637 |
// Warning: This likely runs an event loop which may |
| 1638 |
// end up calling back into this instance, so all |
| 1639 |
// changes to m_pCurl must be undone now and |
| 1640 |
// restored after return. |
| 1641 |
guard.Release(); |
| 1642 |
|
| 1643 |
auto const ret = pEnv->m_xAuthListener->authenticate( |
| 1644 |
oRealm ? *oRealm : u""_ustr, |
| 1645 |
statusCode != SC_PROXY_AUTHENTICATION_REQUIRED |
| 1646 |
? rSession.m_URI.GetHost() |
| 1647 |
: rSession.m_Proxy, |
| 1648 |
userName, passWord, isSystemCredSupported); |
| 1649 |
|
| 1650 |
if (ret == 0) |
| 1651 |
{ |
| 1652 |
// NTLM may either use a password requested |
| 1653 |
// from the user, or from the system via SSPI |
| 1654 |
// so i guess it should not be disabled here |
| 1655 |
// regardless of the state of the system auth |
| 1656 |
// checkbox, particularly since SSPI is only |
| 1657 |
// available on WNT. |
| 1658 |
// Additionally, "Negotiate" has a "legacy" |
| 1659 |
// mode that is actually just NTLM according to |
| 1660 |
// https://curl.se/rfc/ntlm.html#ntlmHttpAuthentication |
| 1661 |
// so there's nothing in authSystem that can be |
| 1662 |
// disabled here. |
| 1663 |
roAuth.emplace(userName, passWord, |
| 1664 |
((userName.isEmpty() && passWord.isEmpty()) |
| 1665 |
? (authAvail & authSystem) |
| 1666 |
: authAvail)); |
| 1667 |
isRetry = true; |
| 1668 |
// Acquire is only necessary in case of success. |
| 1669 |
guard.Acquire(); |
| 1670 |
break; // break out of switch |
| 1671 |
} |
| 1672 |
// else: throw |
| 1673 |
} |
| 1674 |
SAL_INFO("ucb.ucp.webdav.curl", "no auth credentials provided"); |
| 1675 |
throw DAVException(DAVException::DAV_HTTP_NOAUTH, |
| 1676 |
ConnectionEndPointString(rSession.m_URI.GetHost(), |
| 1677 |
rSession.m_URI.GetPort())); |
| 1678 |
break; |
| 1679 |
} |
| 1680 |
} |
| 1681 |
} |
| 1682 |
else if (rException.getError() == DAVException::DAV_UNSUPPORTED) |
| 1683 |
{ |
| 1684 |
// tdf#152493 libcurl can't handle "Transfer-Encoding: chunked" |
| 1685 |
// in HTTP/1.1 100 Continue response. |
| 1686 |
// workaround: if HTTP/1.1 didn't work, try HTTP/1.0 |
| 1687 |
// (but fallback only once - to prevent infinite loop) |
| 1688 |
if (isFallbackHTTP10) |
| 1689 |
{ |
| 1690 |
throw DAVException(DAVException::DAV_HTTP_ERROR); |
| 1691 |
} |
| 1692 |
isFallbackHTTP10 = true; |
| 1693 |
// note: this is not reset - future requests to this URI use it! |
| 1694 |
auto rc = curl_easy_setopt(rSession.m_pCurl.get(), CURLOPT_HTTP_VERSION, |
| 1695 |
CURL_HTTP_VERSION_1_0); |
| 1696 |
if (rc != CURLE_OK) |
| 1697 |
{ |
| 1698 |
throw DAVException(DAVException::DAV_HTTP_ERROR); |
| 1699 |
} |
| 1700 |
SAL_INFO("ucb.ucp.webdav.curl", "attempting fallback to HTTP/1.0"); |
| 1701 |
isRetry = true; |
| 1702 |
} |
| 1703 |
if (!isRetry) |
| 1704 |
{ |
| 1705 |
throw; // everything else: re-throw |
| 1706 |
} |
| 1707 |
} |
| 1708 |
} while (isRetry); |
| 1709 |
|
| 1710 |
if (oAuth) |
| 1711 |
{ |
| 1712 |
// assume this worked, leave auth data as stored in m_pCurl |
| 1713 |
rSession.m_isAuthenticated = true; |
| 1714 |
} |
| 1715 |
if (oAuthProxy) |
| 1716 |
{ |
| 1717 |
// assume this worked, leave auth data as stored in m_pCurl |
| 1718 |
rSession.m_isAuthenticatedProxy = true; |
| 1719 |
} |
| 1720 |
} |
| 1721 |
|
| 1722 |
auto CurlSession::OPTIONS(OUString const& rURIReference, |
| 1723 |
|
| 1724 |
DAVOptions& rOptions, DAVRequestEnvironment const& rEnv) -> void |
| 1725 |
{ |
| 1726 |
SAL_INFO("ucb.ucp.webdav.curl", "OPTIONS: " << rURIReference); |
| 1727 |
|
| 1728 |
rOptions.init(); |
| 1729 |
|
| 1730 |
CurlUri const uri(CurlProcessor::URIReferenceToURI(*this, rURIReference)); |
| 1731 |
|
| 1732 |
::std::vector<OUString> const headerNames{ u"allow"_ustr, u"dav"_ustr }; |
| 1733 |
DAVResource result; |
| 1734 |
::std::pair<::std::vector<OUString> const&, DAVResource&> const headers(headerNames, result); |
| 1735 |
|
| 1736 |
::std::vector<CurlOption> const options{ { CURLOPT_CUSTOMREQUEST, "OPTIONS", |
| 1737 |
"CURLOPT_CUSTOMREQUEST" } }; |
| 1738 |
|
| 1739 |
CurlProcessor::ProcessRequest(*this, uri, u"OPTIONS"_ustr, options, &rEnv, nullptr, nullptr, |
| 1740 |
nullptr, &headers); |
| 1741 |
|
| 1742 |
for (auto const& it : result.properties) |
| 1743 |
{ |
| 1744 |
OUString value; |
| 1745 |
it.Value >>= value; |
| 1746 |
SAL_INFO("ucb.ucp.webdav.curl", "OPTIONS: header: " << it.Name << ": " << value); |
| 1747 |
if (it.Name.equalsIgnoreAsciiCase("allow")) |
| 1748 |
{ |
| 1749 |
rOptions.setAllowedMethods(value); |
| 1750 |
} |
| 1751 |
else if (it.Name.equalsIgnoreAsciiCase("dav")) |
| 1752 |
{ |
| 1753 |
// see <http://tools.ietf.org/html/rfc4918#section-10.1>, |
| 1754 |
// <http://tools.ietf.org/html/rfc4918#section-18>, |
| 1755 |
// and <http://tools.ietf.org/html/rfc7230#section-3.2> |
| 1756 |
// we detect the class (1, 2 and 3), other elements (token, URL) |
| 1757 |
// are not used for now |
| 1758 |
auto const list(::comphelper::string::convertCommaSeparated(value)); |
| 1759 |
for (OUString const& v : list) |
| 1760 |
{ |
| 1761 |
if (v == "1") |
| 1762 |
{ |
| 1763 |
rOptions.setClass1(); |
| 1764 |
} |
| 1765 |
else if (v == "2") |
| 1766 |
{ |
| 1767 |
rOptions.setClass2(); |
| 1768 |
} |
| 1769 |
else if (v == "3") |
| 1770 |
{ |
| 1771 |
rOptions.setClass3(); |
| 1772 |
} |
| 1773 |
} |
| 1774 |
} |
| 1775 |
} |
| 1776 |
if (rOptions.isClass2() || rOptions.isClass3()) |
| 1777 |
{ |
| 1778 |
if (g_Init.LockStore.getLockTokenForURI(uri.GetURI(), nullptr)) |
| 1779 |
{ |
| 1780 |
rOptions.setLocked(); |
| 1781 |
} |
| 1782 |
} |
| 1783 |
} |
| 1784 |
|
| 1785 |
auto CurlProcessor::PropFind( |
| 1786 |
CurlSession& rSession, CurlUri const& rURI, Depth const nDepth, |
| 1787 |
::std::tuple<::std::vector<OUString> const&, ::std::vector<DAVResource>* const, |
| 1788 |
::std::vector<ucb::Lock>* const> const* const o_pRequestedProperties, |
| 1789 |
::std::vector<DAVResourceInfo>* const o_pResourceInfos, DAVRequestEnvironment const& rEnv) |
| 1790 |
-> void |
| 1791 |
{ |
| 1792 |
assert((o_pRequestedProperties != nullptr) != (o_pResourceInfos != nullptr)); |
| 1793 |
assert((o_pRequestedProperties == nullptr) |
| 1794 |
|| (::std::get<1>(*o_pRequestedProperties) != nullptr) |
| 1795 |
!= (::std::get<2>(*o_pRequestedProperties) != nullptr)); |
| 1796 |
|
| 1797 |
::std::unique_ptr<curl_slist, deleter_from_fn<curl_slist, curl_slist_free_all>> pList; |
| 1798 |
pList.reset(curl_slist_append(pList.release(), "Content-Type: application/xml")); |
| 1799 |
if (!pList) |
| 1800 |
{ |
| 1801 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 1802 |
} |
| 1803 |
OString depth; |
| 1804 |
switch (nDepth) |
| 1805 |
{ |
| 1806 |
case DAVZERO: |
| 1807 |
depth = "Depth: 0"_ostr; |
| 1808 |
break; |
| 1809 |
case DAVONE: |
| 1810 |
depth = "Depth: 1"_ostr; |
| 1811 |
break; |
| 1812 |
case DAVINFINITY: |
| 1813 |
depth = "Depth: infinity"_ostr; |
| 1814 |
break; |
| 1815 |
default: |
| 1816 |
assert(false); |
| 1817 |
} |
| 1818 |
pList.reset(curl_slist_append(pList.release(), depth.getStr())); |
| 1819 |
if (!pList) |
| 1820 |
{ |
| 1821 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 1822 |
} |
| 1823 |
|
| 1824 |
uno::Reference<io::XSequenceOutputStream> const xSeqOutStream( |
| 1825 |
io::SequenceOutputStream::create(rSession.m_xContext)); |
| 1826 |
uno::Reference<io::XOutputStream> const xRequestOutStream(xSeqOutStream); |
| 1827 |
assert(xRequestOutStream.is()); |
| 1828 |
|
| 1829 |
uno::Reference<xml::sax::XWriter> const xWriter(xml::sax::Writer::create(rSession.m_xContext)); |
| 1830 |
xWriter->setOutputStream(xRequestOutStream); |
| 1831 |
xWriter->startDocument(); |
| 1832 |
rtl::Reference<::comphelper::AttributeList> const pAttrList(new ::comphelper::AttributeList); |
| 1833 |
pAttrList->AddAttribute(u"xmlns"_ustr, u"DAV:"_ustr); |
| 1834 |
xWriter->startElement(u"propfind"_ustr, pAttrList); |
| 1835 |
if (o_pResourceInfos) |
| 1836 |
{ |
| 1837 |
xWriter->startElement(u"propname"_ustr, nullptr); |
| 1838 |
xWriter->endElement(u"propname"_ustr); |
| 1839 |
} |
| 1840 |
else |
| 1841 |
{ |
| 1842 |
if (::std::get<0>(*o_pRequestedProperties).empty()) |
| 1843 |
{ |
| 1844 |
xWriter->startElement(u"allprop"_ustr, nullptr); |
| 1845 |
xWriter->endElement(u"allprop"_ustr); |
| 1846 |
} |
| 1847 |
else |
| 1848 |
{ |
| 1849 |
xWriter->startElement(u"prop"_ustr, nullptr); |
| 1850 |
for (OUString const& rName : ::std::get<0>(*o_pRequestedProperties)) |
| 1851 |
{ |
| 1852 |
SerfPropName name; |
| 1853 |
DAVProperties::createSerfPropName(rName, name); |
| 1854 |
pAttrList->Clear(); |
| 1855 |
pAttrList->AddAttribute(u"xmlns"_ustr, OUString::createFromAscii(name.nspace)); |
| 1856 |
xWriter->startElement(OUString::createFromAscii(name.name), pAttrList); |
| 1857 |
xWriter->endElement(OUString::createFromAscii(name.name)); |
| 1858 |
} |
| 1859 |
xWriter->endElement(u"prop"_ustr); |
| 1860 |
} |
| 1861 |
} |
| 1862 |
xWriter->endElement(u"propfind"_ustr); |
| 1863 |
xWriter->endDocument(); |
| 1864 |
|
| 1865 |
uno::Reference<io::XInputStream> const xRequestInStream( |
| 1866 |
io::SequenceInputStream::createStreamFromSequence(rSession.m_xContext, |
| 1867 |
xSeqOutStream->getWrittenBytes())); |
| 1868 |
assert(xRequestInStream.is()); |
| 1869 |
|
| 1870 |
curl_off_t const len(xSeqOutStream->getWrittenBytes().getLength()); |
| 1871 |
|
| 1872 |
::std::vector<CurlOption> const options{ |
| 1873 |
{ CURLOPT_UPLOAD, 1L, nullptr }, |
| 1874 |
{ CURLOPT_CUSTOMREQUEST, "PROPFIND", "CURLOPT_CUSTOMREQUEST" }, |
| 1875 |
// note: Sharepoint cannot handle "Transfer-Encoding: chunked" |
| 1876 |
{ CURLOPT_INFILESIZE_LARGE, len, nullptr, CurlOption::Type::CurlOffT } |
| 1877 |
}; |
| 1878 |
|
| 1879 |
// stream for response |
| 1880 |
uno::Reference<io::XInputStream> const xResponseInStream(io::Pipe::create(rSession.m_xContext)); |
| 1881 |
uno::Reference<io::XOutputStream> const xResponseOutStream(xResponseInStream, uno::UNO_QUERY); |
| 1882 |
assert(xResponseInStream.is()); |
| 1883 |
assert(xResponseOutStream.is()); |
| 1884 |
|
| 1885 |
CurlProcessor::ProcessRequest(rSession, rURI, u"PROPFIND"_ustr, options, &rEnv, |
| 1886 |
::std::move(pList), &xResponseOutStream, &xRequestInStream, |
| 1887 |
nullptr); |
| 1888 |
|
| 1889 |
if (o_pResourceInfos) |
| 1890 |
{ |
| 1891 |
*o_pResourceInfos = parseWebDAVPropNameResponse(xResponseInStream); |
| 1892 |
} |
| 1893 |
else |
| 1894 |
{ |
| 1895 |
if (::std::get<1>(*o_pRequestedProperties) != nullptr) |
| 1896 |
{ |
| 1897 |
*::std::get<1>(*o_pRequestedProperties) |
| 1898 |
= parseWebDAVPropFindResponse(xResponseInStream); |
| 1899 |
for (DAVResource& it : *::std::get<1>(*o_pRequestedProperties)) |
| 1900 |
{ |
| 1901 |
// caller will give these uris to CurlUri so can't be relative |
| 1902 |
if (it.uri.startsWith("/")) |
| 1903 |
{ |
| 1904 |
try |
| 1905 |
{ |
| 1906 |
it.uri = rSession.m_URI.CloneWithRelativeRefPathAbsolute(it.uri).GetURI(); |
| 1907 |
} |
| 1908 |
catch (DAVException const&) |
| 1909 |
{ |
| 1910 |
SAL_INFO("ucb.ucp.webdav.curl", |
| 1911 |
"PROPFIND: exception parsing uri " << it.uri); |
| 1912 |
} |
| 1913 |
} |
| 1914 |
} |
| 1915 |
} |
| 1916 |
else |
| 1917 |
{ |
| 1918 |
*::std::get<2>(*o_pRequestedProperties) = parseWebDAVLockResponse(xResponseInStream); |
| 1919 |
} |
| 1920 |
} |
| 1921 |
} |
| 1922 |
|
| 1923 |
// DAV methods |
| 1924 |
auto CurlSession::PROPFIND(OUString const& rURIReference, Depth const depth, |
| 1925 |
::std::vector<OUString> const& rPropertyNames, |
| 1926 |
::std::vector<DAVResource>& o_rResources, |
| 1927 |
DAVRequestEnvironment const& rEnv) -> void |
| 1928 |
{ |
| 1929 |
SAL_INFO("ucb.ucp.webdav.curl", "PROPFIND: " << rURIReference << " " << depth); |
| 1930 |
|
| 1931 |
CurlUri const uri(CurlProcessor::URIReferenceToURI(*this, rURIReference)); |
| 1932 |
|
| 1933 |
::std::tuple<::std::vector<OUString> const&, ::std::vector<DAVResource>* const, |
| 1934 |
::std::vector<ucb::Lock>* const> const args(rPropertyNames, &o_rResources, |
| 1935 |
nullptr); |
| 1936 |
return CurlProcessor::PropFind(*this, uri, depth, &args, nullptr, rEnv); |
| 1937 |
} |
| 1938 |
|
| 1939 |
auto CurlSession::PROPFIND(OUString const& rURIReference, Depth const depth, |
| 1940 |
::std::vector<DAVResourceInfo>& o_rResourceInfos, |
| 1941 |
DAVRequestEnvironment const& rEnv) -> void |
| 1942 |
{ |
| 1943 |
SAL_INFO("ucb.ucp.webdav.curl", "PROPFIND: " << rURIReference << " " << depth); |
| 1944 |
|
| 1945 |
CurlUri const uri(CurlProcessor::URIReferenceToURI(*this, rURIReference)); |
| 1946 |
|
| 1947 |
return CurlProcessor::PropFind(*this, uri, depth, nullptr, &o_rResourceInfos, rEnv); |
| 1948 |
} |
| 1949 |
|
| 1950 |
auto CurlSession::PROPPATCH(OUString const& rURIReference, |
| 1951 |
::std::vector<ProppatchValue> const& rValues, |
| 1952 |
DAVRequestEnvironment const& rEnv) -> void |
| 1953 |
{ |
| 1954 |
SAL_INFO("ucb.ucp.webdav.curl", "PROPPATCH: " << rURIReference); |
| 1955 |
|
| 1956 |
CurlUri const uri(CurlProcessor::URIReferenceToURI(*this, rURIReference)); |
| 1957 |
|
| 1958 |
// TODO: either set CURLOPT_INFILESIZE_LARGE or chunked? |
| 1959 |
::std::unique_ptr<curl_slist, deleter_from_fn<curl_slist, curl_slist_free_all>> pList; |
| 1960 |
pList.reset(curl_slist_append(pList.release(), "Content-Type: application/xml")); |
| 1961 |
if (!pList) |
| 1962 |
{ |
| 1963 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 1964 |
} |
| 1965 |
|
| 1966 |
// generate XML document for PROPPATCH |
| 1967 |
uno::Reference<io::XSequenceOutputStream> const xSeqOutStream( |
| 1968 |
io::SequenceOutputStream::create(m_xContext)); |
| 1969 |
uno::Reference<io::XOutputStream> const xRequestOutStream(xSeqOutStream); |
| 1970 |
assert(xRequestOutStream.is()); |
| 1971 |
uno::Reference<xml::sax::XWriter> const xWriter(xml::sax::Writer::create(m_xContext)); |
| 1972 |
xWriter->setOutputStream(xRequestOutStream); |
| 1973 |
xWriter->startDocument(); |
| 1974 |
rtl::Reference<::comphelper::AttributeList> const pAttrList(new ::comphelper::AttributeList); |
| 1975 |
pAttrList->AddAttribute(u"xmlns"_ustr, u"DAV:"_ustr); |
| 1976 |
xWriter->startElement(u"propertyupdate"_ustr, pAttrList); |
| 1977 |
for (ProppatchValue const& rPropValue : rValues) |
| 1978 |
{ |
| 1979 |
assert(rPropValue.operation == PROPSET || rPropValue.operation == PROPREMOVE); |
| 1980 |
OUString const operation((rPropValue.operation == PROPSET) ? u"set"_ustr : u"remove"_ustr); |
| 1981 |
xWriter->startElement(operation, nullptr); |
| 1982 |
xWriter->startElement(u"prop"_ustr, nullptr); |
| 1983 |
SerfPropName name; |
| 1984 |
DAVProperties::createSerfPropName(rPropValue.name, name); |
| 1985 |
pAttrList->Clear(); |
| 1986 |
pAttrList->AddAttribute(u"xmlns"_ustr, OUString::createFromAscii(name.nspace)); |
| 1987 |
xWriter->startElement(OUString::createFromAscii(name.name), pAttrList); |
| 1988 |
if (rPropValue.operation == PROPSET) |
| 1989 |
{ |
| 1990 |
if (DAVProperties::isUCBDeadProperty(name)) |
| 1991 |
{ |
| 1992 |
::std::optional<::std::pair<OUString, OUString>> const oProp( |
| 1993 |
UCBDeadPropertyValue::toXML(rPropValue.value)); |
| 1994 |
if (oProp) |
| 1995 |
{ |
| 1996 |
xWriter->startElement(u"ucbprop"_ustr, nullptr); |
| 1997 |
xWriter->startElement(u"type"_ustr, nullptr); |
| 1998 |
xWriter->characters(oProp->first); |
| 1999 |
xWriter->endElement(u"type"_ustr); |
| 2000 |
xWriter->startElement(u"value"_ustr, nullptr); |
| 2001 |
xWriter->characters(oProp->second); |
| 2002 |
xWriter->endElement(u"value"_ustr); |
| 2003 |
xWriter->endElement(u"ucbprop"_ustr); |
| 2004 |
} |
| 2005 |
} |
| 2006 |
else |
| 2007 |
{ |
| 2008 |
OUString value; |
| 2009 |
rPropValue.value >>= value; |
| 2010 |
xWriter->characters(value); |
| 2011 |
} |
| 2012 |
} |
| 2013 |
xWriter->endElement(OUString::createFromAscii(name.name)); |
| 2014 |
xWriter->endElement(u"prop"_ustr); |
| 2015 |
xWriter->endElement(operation); |
| 2016 |
} |
| 2017 |
xWriter->endElement(u"propertyupdate"_ustr); |
| 2018 |
xWriter->endDocument(); |
| 2019 |
|
| 2020 |
uno::Reference<io::XInputStream> const xRequestInStream( |
| 2021 |
io::SequenceInputStream::createStreamFromSequence(m_xContext, |
| 2022 |
xSeqOutStream->getWrittenBytes())); |
| 2023 |
assert(xRequestInStream.is()); |
| 2024 |
|
| 2025 |
curl_off_t const len(xSeqOutStream->getWrittenBytes().getLength()); |
| 2026 |
|
| 2027 |
::std::vector<CurlOption> const options{ |
| 2028 |
{ CURLOPT_UPLOAD, 1L, nullptr }, |
| 2029 |
{ CURLOPT_CUSTOMREQUEST, "PROPPATCH", "CURLOPT_CUSTOMREQUEST" }, |
| 2030 |
// note: Sharepoint cannot handle "Transfer-Encoding: chunked" |
| 2031 |
{ CURLOPT_INFILESIZE_LARGE, len, nullptr, CurlOption::Type::CurlOffT } |
| 2032 |
}; |
| 2033 |
|
| 2034 |
CurlProcessor::ProcessRequest(*this, uri, u"PROPPATCH"_ustr, options, &rEnv, ::std::move(pList), |
| 2035 |
nullptr, &xRequestInStream, nullptr); |
| 2036 |
} |
| 2037 |
|
| 2038 |
auto CurlSession::HEAD(OUString const& rURIReference, ::std::vector<OUString> const& rHeaderNames, |
| 2039 |
DAVResource& io_rResource, DAVRequestEnvironment const& rEnv) -> void |
| 2040 |
{ |
| 2041 |
SAL_INFO("ucb.ucp.webdav.curl", "HEAD: " << rURIReference); |
| 2042 |
|
| 2043 |
CurlUri const uri(CurlProcessor::URIReferenceToURI(*this, rURIReference)); |
| 2044 |
|
| 2045 |
::std::vector<CurlOption> const options{ |
| 2046 |
// NOBODY will prevent logging the response body in ProcessRequest() |
| 2047 |
// exception, but omitting it here results in a long timeout until the |
| 2048 |
// server closes the connection, which is worse |
| 2049 |
{ CURLOPT_NOBODY, 1L, nullptr }, |
| 2050 |
{ CURLOPT_CUSTOMREQUEST, "HEAD", "CURLOPT_CUSTOMREQUEST" } |
| 2051 |
}; |
| 2052 |
|
| 2053 |
::std::pair<::std::vector<OUString> const&, DAVResource&> const headers(rHeaderNames, |
| 2054 |
io_rResource); |
| 2055 |
|
| 2056 |
CurlProcessor::ProcessRequest(*this, uri, u"HEAD"_ustr, options, &rEnv, nullptr, nullptr, |
| 2057 |
nullptr, &headers); |
| 2058 |
} |
| 2059 |
|
| 2060 |
auto CurlSession::GET(OUString const& rURIReference, DAVRequestEnvironment const& rEnv) |
| 2061 |
-> uno::Reference<io::XInputStream> |
| 2062 |
{ |
| 2063 |
SAL_INFO("ucb.ucp.webdav.curl", "GET: " << rURIReference); |
| 2064 |
|
| 2065 |
CurlUri const uri(CurlProcessor::URIReferenceToURI(*this, rURIReference)); |
| 2066 |
|
| 2067 |
// could use either com.sun.star.io.Pipe or com.sun.star.io.SequenceInputStream? |
| 2068 |
// Pipe can just write into its XOuputStream, which is simpler. |
| 2069 |
// Both resize exponentially, so performance should be fine. |
| 2070 |
// However, Pipe doesn't implement XSeekable, which is required by filters. |
| 2071 |
|
| 2072 |
uno::Reference<io::XSequenceOutputStream> const xSeqOutStream( |
| 2073 |
io::SequenceOutputStream::create(m_xContext)); |
| 2074 |
uno::Reference<io::XOutputStream> const xResponseOutStream(xSeqOutStream); |
| 2075 |
assert(xResponseOutStream.is()); |
| 2076 |
|
| 2077 |
::std::vector<CurlOption> const options{ { CURLOPT_HTTPGET, 1L, nullptr } }; |
| 2078 |
|
| 2079 |
CurlProcessor::ProcessRequest(*this, uri, u"GET"_ustr, options, &rEnv, nullptr, |
| 2080 |
&xResponseOutStream, nullptr, nullptr); |
| 2081 |
|
| 2082 |
uno::Reference<io::XInputStream> const xResponseInStream( |
| 2083 |
io::SequenceInputStream::createStreamFromSequence(m_xContext, |
| 2084 |
xSeqOutStream->getWrittenBytes())); |
| 2085 |
assert(xResponseInStream.is()); |
| 2086 |
|
| 2087 |
return xResponseInStream; |
| 2088 |
} |
| 2089 |
|
| 2090 |
auto CurlSession::GET(OUString const& rURIReference, uno::Reference<io::XOutputStream>& rxOutStream, |
| 2091 |
DAVRequestEnvironment const& rEnv) -> void |
| 2092 |
{ |
| 2093 |
SAL_INFO("ucb.ucp.webdav.curl", "GET: " << rURIReference); |
| 2094 |
|
| 2095 |
CurlUri const uri(CurlProcessor::URIReferenceToURI(*this, rURIReference)); |
| 2096 |
|
| 2097 |
::std::vector<CurlOption> const options{ { CURLOPT_HTTPGET, 1L, nullptr } }; |
| 2098 |
|
| 2099 |
CurlProcessor::ProcessRequest(*this, uri, u"GET"_ustr, options, &rEnv, nullptr, &rxOutStream, |
| 2100 |
nullptr, nullptr); |
| 2101 |
} |
| 2102 |
|
| 2103 |
auto CurlSession::GET(OUString const& rURIReference, ::std::vector<OUString> const& rHeaderNames, |
| 2104 |
DAVResource& io_rResource, DAVRequestEnvironment const& rEnv) |
| 2105 |
-> uno::Reference<io::XInputStream> |
| 2106 |
{ |
| 2107 |
SAL_INFO("ucb.ucp.webdav.curl", "GET: " << rURIReference); |
| 2108 |
|
| 2109 |
CurlUri const uri(CurlProcessor::URIReferenceToURI(*this, rURIReference)); |
| 2110 |
|
| 2111 |
::std::vector<CurlOption> const options{ { CURLOPT_HTTPGET, 1L, nullptr } }; |
| 2112 |
|
| 2113 |
uno::Reference<io::XSequenceOutputStream> const xSeqOutStream( |
| 2114 |
io::SequenceOutputStream::create(m_xContext)); |
| 2115 |
uno::Reference<io::XOutputStream> const xResponseOutStream(xSeqOutStream); |
| 2116 |
assert(xResponseOutStream.is()); |
| 2117 |
|
| 2118 |
::std::pair<::std::vector<OUString> const&, DAVResource&> const headers(rHeaderNames, |
| 2119 |
io_rResource); |
| 2120 |
|
| 2121 |
CurlProcessor::ProcessRequest(*this, uri, u"GET"_ustr, options, &rEnv, nullptr, |
| 2122 |
&xResponseOutStream, nullptr, &headers); |
| 2123 |
|
| 2124 |
uno::Reference<io::XInputStream> const xResponseInStream( |
| 2125 |
io::SequenceInputStream::createStreamFromSequence(m_xContext, |
| 2126 |
xSeqOutStream->getWrittenBytes())); |
| 2127 |
assert(xResponseInStream.is()); |
| 2128 |
|
| 2129 |
return xResponseInStream; |
| 2130 |
} |
| 2131 |
|
| 2132 |
auto CurlSession::GET(OUString const& rURIReference, uno::Reference<io::XOutputStream>& rxOutStream, |
| 2133 |
::std::vector<OUString> const& rHeaderNames, DAVResource& io_rResource, |
| 2134 |
DAVRequestEnvironment const& rEnv) -> void |
| 2135 |
{ |
| 2136 |
SAL_INFO("ucb.ucp.webdav.curl", "GET: " << rURIReference); |
| 2137 |
|
| 2138 |
CurlUri const uri(CurlProcessor::URIReferenceToURI(*this, rURIReference)); |
| 2139 |
|
| 2140 |
::std::vector<CurlOption> const options{ { CURLOPT_HTTPGET, 1L, nullptr } }; |
| 2141 |
|
| 2142 |
::std::pair<::std::vector<OUString> const&, DAVResource&> const headers(rHeaderNames, |
| 2143 |
io_rResource); |
| 2144 |
|
| 2145 |
CurlProcessor::ProcessRequest(*this, uri, u"GET"_ustr, options, &rEnv, nullptr, &rxOutStream, |
| 2146 |
nullptr, &headers); |
| 2147 |
} |
| 2148 |
|
| 2149 |
auto CurlSession::PUT(OUString const& rURIReference, |
| 2150 |
uno::Reference<io::XInputStream> const& rxInStream, |
| 2151 |
DAVRequestEnvironment const& rEnv) -> void |
| 2152 |
{ |
| 2153 |
SAL_INFO("ucb.ucp.webdav.curl", "PUT: " << rURIReference); |
| 2154 |
|
| 2155 |
CurlUri const uri(CurlProcessor::URIReferenceToURI(*this, rURIReference)); |
| 2156 |
|
| 2157 |
// NextCloud silently fails with chunked encoding |
| 2158 |
uno::Reference<io::XSeekable> const xSeekable(rxInStream, uno::UNO_QUERY); |
| 2159 |
if (!xSeekable.is()) |
| 2160 |
{ |
| 2161 |
throw uno::RuntimeException(u"TODO: not seekable"_ustr); |
| 2162 |
} |
| 2163 |
curl_off_t const len(xSeekable->getLength() - xSeekable->getPosition()); |
| 2164 |
|
| 2165 |
::std::unique_ptr<curl_slist, deleter_from_fn<curl_slist, curl_slist_free_all>> pList; |
| 2166 |
OUString const* const pToken(g_Init.LockStore.getLockTokenForURI(uri.GetURI(), nullptr)); |
| 2167 |
if (pToken) |
| 2168 |
{ |
| 2169 |
OString const utf8If("If: " |
| 2170 |
// disabled as Sharepoint 2013 workaround, it accepts only |
| 2171 |
// "No-Tag-List", see fed2984281a85a5a2f308841ec810f218c75f2ab |
| 2172 |
#if 0 |
| 2173 |
"<" + OUStringToOString(rURIReference, RTL_TEXTENCODING_ASCII_US) |
| 2174 |
+ "> " |
| 2175 |
#endif |
| 2176 |
"(<" |
| 2177 |
+ OUStringToOString(*pToken, RTL_TEXTENCODING_ASCII_US) + ">)"); |
| 2178 |
pList.reset(curl_slist_append(pList.release(), utf8If.getStr())); |
| 2179 |
if (!pList) |
| 2180 |
{ |
| 2181 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 2182 |
} |
| 2183 |
} |
| 2184 |
|
| 2185 |
// lock m_Mutex after accessing global LockStore to avoid deadlock |
| 2186 |
|
| 2187 |
// note: Nextcloud 20 cannot handle "Transfer-Encoding: chunked" |
| 2188 |
::std::vector<CurlOption> const options{ |
| 2189 |
{ CURLOPT_UPLOAD, 1L, nullptr }, // libcurl won't upload without setting this |
| 2190 |
{ CURLOPT_INFILESIZE_LARGE, len, nullptr, CurlOption::Type::CurlOffT } |
| 2191 |
}; |
| 2192 |
|
| 2193 |
CurlProcessor::ProcessRequest(*this, uri, u"PUT"_ustr, options, &rEnv, ::std::move(pList), |
| 2194 |
nullptr, &rxInStream, nullptr); |
| 2195 |
} |
| 2196 |
|
| 2197 |
auto CurlSession::POST(OUString const& rURIReference, OUString const& rContentType, |
| 2198 |
OUString const& rReferer, uno::Reference<io::XInputStream> const& rxInStream, |
| 2199 |
DAVRequestEnvironment const& rEnv) -> uno::Reference<io::XInputStream> |
| 2200 |
{ |
| 2201 |
SAL_INFO("ucb.ucp.webdav.curl", "POST: " << rURIReference); |
| 2202 |
|
| 2203 |
CurlUri const uri(CurlProcessor::URIReferenceToURI(*this, rURIReference)); |
| 2204 |
|
| 2205 |
// TODO: either set CURLOPT_POSTFIELDSIZE_LARGE or chunked? |
| 2206 |
::std::unique_ptr<curl_slist, deleter_from_fn<curl_slist, curl_slist_free_all>> pList( |
| 2207 |
curl_slist_append(nullptr, "Transfer-Encoding: chunked")); |
| 2208 |
if (!pList) |
| 2209 |
{ |
| 2210 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 2211 |
} |
| 2212 |
OString const utf8ContentType("Content-Type: " |
| 2213 |
+ OUStringToOString(rContentType, RTL_TEXTENCODING_ASCII_US)); |
| 2214 |
pList.reset(curl_slist_append(pList.release(), utf8ContentType.getStr())); |
| 2215 |
if (!pList) |
| 2216 |
{ |
| 2217 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 2218 |
} |
| 2219 |
OString const utf8Referer("Referer: " + OUStringToOString(rReferer, RTL_TEXTENCODING_ASCII_US)); |
| 2220 |
pList.reset(curl_slist_append(pList.release(), utf8Referer.getStr())); |
| 2221 |
if (!pList) |
| 2222 |
{ |
| 2223 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 2224 |
} |
| 2225 |
|
| 2226 |
::std::vector<CurlOption> const options{ { CURLOPT_POST, 1L, nullptr } }; |
| 2227 |
|
| 2228 |
uno::Reference<io::XSequenceOutputStream> const xSeqOutStream( |
| 2229 |
io::SequenceOutputStream::create(m_xContext)); |
| 2230 |
uno::Reference<io::XOutputStream> const xResponseOutStream(xSeqOutStream); |
| 2231 |
assert(xResponseOutStream.is()); |
| 2232 |
|
| 2233 |
CurlProcessor::ProcessRequest(*this, uri, u"POST"_ustr, options, &rEnv, ::std::move(pList), |
| 2234 |
&xResponseOutStream, &rxInStream, nullptr); |
| 2235 |
|
| 2236 |
uno::Reference<io::XInputStream> const xResponseInStream( |
| 2237 |
io::SequenceInputStream::createStreamFromSequence(m_xContext, |
| 2238 |
xSeqOutStream->getWrittenBytes())); |
| 2239 |
assert(xResponseInStream.is()); |
| 2240 |
|
| 2241 |
return xResponseInStream; |
| 2242 |
} |
| 2243 |
|
| 2244 |
auto CurlSession::POST(OUString const& rURIReference, OUString const& rContentType, |
| 2245 |
OUString const& rReferer, uno::Reference<io::XInputStream> const& rxInStream, |
| 2246 |
uno::Reference<io::XOutputStream>& rxOutStream, |
| 2247 |
DAVRequestEnvironment const& rEnv) -> void |
| 2248 |
{ |
| 2249 |
SAL_INFO("ucb.ucp.webdav.curl", "POST: " << rURIReference); |
| 2250 |
|
| 2251 |
CurlUri const uri(CurlProcessor::URIReferenceToURI(*this, rURIReference)); |
| 2252 |
|
| 2253 |
// TODO: either set CURLOPT_POSTFIELDSIZE_LARGE or chunked? |
| 2254 |
::std::unique_ptr<curl_slist, deleter_from_fn<curl_slist, curl_slist_free_all>> pList( |
| 2255 |
curl_slist_append(nullptr, "Transfer-Encoding: chunked")); |
| 2256 |
if (!pList) |
| 2257 |
{ |
| 2258 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 2259 |
} |
| 2260 |
OString const utf8ContentType("Content-Type: " |
| 2261 |
+ OUStringToOString(rContentType, RTL_TEXTENCODING_ASCII_US)); |
| 2262 |
pList.reset(curl_slist_append(pList.release(), utf8ContentType.getStr())); |
| 2263 |
if (!pList) |
| 2264 |
{ |
| 2265 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 2266 |
} |
| 2267 |
OString const utf8Referer("Referer: " + OUStringToOString(rReferer, RTL_TEXTENCODING_ASCII_US)); |
| 2268 |
pList.reset(curl_slist_append(pList.release(), utf8Referer.getStr())); |
| 2269 |
if (!pList) |
| 2270 |
{ |
| 2271 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 2272 |
} |
| 2273 |
|
| 2274 |
::std::vector<CurlOption> const options{ { CURLOPT_POST, 1L, nullptr } }; |
| 2275 |
|
| 2276 |
CurlProcessor::ProcessRequest(*this, uri, u"POST"_ustr, options, &rEnv, ::std::move(pList), |
| 2277 |
&rxOutStream, &rxInStream, nullptr); |
| 2278 |
} |
| 2279 |
|
| 2280 |
auto CurlSession::MKCOL(OUString const& rURIReference, DAVRequestEnvironment const& rEnv) -> void |
| 2281 |
{ |
| 2282 |
SAL_INFO("ucb.ucp.webdav.curl", "MKCOL: " << rURIReference); |
| 2283 |
|
| 2284 |
CurlUri const uri(CurlProcessor::URIReferenceToURI(*this, rURIReference)); |
| 2285 |
|
| 2286 |
::std::vector<CurlOption> const options{ { CURLOPT_CUSTOMREQUEST, "MKCOL", |
| 2287 |
"CURLOPT_CUSTOMREQUEST" } }; |
| 2288 |
|
| 2289 |
CurlProcessor::ProcessRequest(*this, uri, u"MKCOL"_ustr, options, &rEnv, nullptr, nullptr, |
| 2290 |
nullptr, nullptr); |
| 2291 |
} |
| 2292 |
|
| 2293 |
auto CurlProcessor::MoveOrCopy(CurlSession& rSession, std::u16string_view rSourceURIReference, |
| 2294 |
::std::u16string_view const rDestinationURI, |
| 2295 |
DAVRequestEnvironment const& rEnv, bool const isOverwrite, |
| 2296 |
char const* const pMethod) -> void |
| 2297 |
{ |
| 2298 |
CurlUri const uriSource(CurlProcessor::URIReferenceToURI(rSession, rSourceURIReference)); |
| 2299 |
|
| 2300 |
OString const utf8Destination("Destination: " |
| 2301 |
+ OUStringToOString(rDestinationURI, RTL_TEXTENCODING_ASCII_US)); |
| 2302 |
::std::unique_ptr<curl_slist, deleter_from_fn<curl_slist, curl_slist_free_all>> pList( |
| 2303 |
curl_slist_append(nullptr, utf8Destination.getStr())); |
| 2304 |
if (!pList) |
| 2305 |
{ |
| 2306 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 2307 |
} |
| 2308 |
OString const utf8Overwrite(OString::Concat("Overwrite: ") + (isOverwrite ? "T" : "F")); |
| 2309 |
pList.reset(curl_slist_append(pList.release(), utf8Overwrite.getStr())); |
| 2310 |
if (!pList) |
| 2311 |
{ |
| 2312 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 2313 |
} |
| 2314 |
|
| 2315 |
::std::vector<CurlOption> const options{ { CURLOPT_CUSTOMREQUEST, pMethod, |
| 2316 |
"CURLOPT_CUSTOMREQUEST" } }; |
| 2317 |
|
| 2318 |
CurlProcessor::ProcessRequest(rSession, uriSource, OUString::createFromAscii(pMethod), options, |
| 2319 |
&rEnv, ::std::move(pList), nullptr, nullptr, nullptr); |
| 2320 |
} |
| 2321 |
|
| 2322 |
auto CurlSession::COPY(OUString const& rSourceURIReference, OUString const& rDestinationURI, |
| 2323 |
DAVRequestEnvironment const& rEnv, bool const isOverwrite) -> void |
| 2324 |
{ |
| 2325 |
SAL_INFO("ucb.ucp.webdav.curl", "COPY: " << rSourceURIReference); |
| 2326 |
|
| 2327 |
return CurlProcessor::MoveOrCopy(*this, rSourceURIReference, rDestinationURI, rEnv, isOverwrite, |
| 2328 |
"COPY"); |
| 2329 |
} |
| 2330 |
|
| 2331 |
auto CurlSession::MOVE(OUString const& rSourceURIReference, OUString const& rDestinationURI, |
| 2332 |
DAVRequestEnvironment const& rEnv, bool const isOverwrite) -> void |
| 2333 |
{ |
| 2334 |
SAL_INFO("ucb.ucp.webdav.curl", "MOVE: " << rSourceURIReference); |
| 2335 |
|
| 2336 |
return CurlProcessor::MoveOrCopy(*this, rSourceURIReference, rDestinationURI, rEnv, isOverwrite, |
| 2337 |
"MOVE"); |
| 2338 |
} |
| 2339 |
|
| 2340 |
auto CurlSession::DESTROY(OUString const& rURIReference, DAVRequestEnvironment const& rEnv) -> void |
| 2341 |
{ |
| 2342 |
SAL_INFO("ucb.ucp.webdav.curl", "DESTROY: " << rURIReference); |
| 2343 |
|
| 2344 |
CurlUri const uri(CurlProcessor::URIReferenceToURI(*this, rURIReference)); |
| 2345 |
|
| 2346 |
::std::vector<CurlOption> const options{ { CURLOPT_CUSTOMREQUEST, "DELETE", |
| 2347 |
"CURLOPT_CUSTOMREQUEST" } }; |
| 2348 |
|
| 2349 |
CurlProcessor::ProcessRequest(*this, uri, u"DESTROY"_ustr, options, &rEnv, nullptr, nullptr, |
| 2350 |
nullptr, nullptr); |
| 2351 |
} |
| 2352 |
|
| 2353 |
auto CurlProcessor::Lock( |
| 2354 |
CurlSession& rSession, CurlUri const& rURI, DAVRequestEnvironment const* const pEnv, |
| 2355 |
::std::unique_ptr<curl_slist, deleter_from_fn<curl_slist, curl_slist_free_all>> |
| 2356 |
pRequestHeaderList, |
| 2357 |
uno::Reference<io::XInputStream> const* const pxRequestInStream) |
| 2358 |
-> ::std::vector<::std::pair<ucb::Lock, sal_Int32>> |
| 2359 |
{ |
| 2360 |
curl_off_t len(0); |
| 2361 |
if (pxRequestInStream) |
| 2362 |
{ |
| 2363 |
uno::Reference<io::XSeekable> const xSeekable(*pxRequestInStream, uno::UNO_QUERY); |
| 2364 |
assert(xSeekable.is()); |
| 2365 |
len = xSeekable->getLength(); |
| 2366 |
} |
| 2367 |
|
| 2368 |
::std::vector<CurlOption> const options{ |
| 2369 |
{ CURLOPT_UPLOAD, 1L, nullptr }, |
| 2370 |
{ CURLOPT_CUSTOMREQUEST, "LOCK", "CURLOPT_CUSTOMREQUEST" }, |
| 2371 |
// note: Sharepoint cannot handle "Transfer-Encoding: chunked" |
| 2372 |
{ CURLOPT_INFILESIZE_LARGE, len, nullptr, CurlOption::Type::CurlOffT } |
| 2373 |
}; |
| 2374 |
|
| 2375 |
// stream for response |
| 2376 |
uno::Reference<io::XInputStream> const xResponseInStream(io::Pipe::create(rSession.m_xContext)); |
| 2377 |
uno::Reference<io::XOutputStream> const xResponseOutStream(xResponseInStream, uno::UNO_QUERY); |
| 2378 |
assert(xResponseInStream.is()); |
| 2379 |
assert(xResponseOutStream.is()); |
| 2380 |
|
| 2381 |
TimeValue startTime; |
| 2382 |
osl_getSystemTime(&startTime); |
| 2383 |
|
| 2384 |
CurlProcessor::ProcessRequest(rSession, rURI, u"LOCK"_ustr, options, pEnv, |
| 2385 |
::std::move(pRequestHeaderList), &xResponseOutStream, |
| 2386 |
pxRequestInStream, nullptr); |
| 2387 |
|
| 2388 |
::std::vector<ucb::Lock> const acquiredLocks(parseWebDAVLockResponse(xResponseInStream)); |
| 2389 |
SAL_WARN_IF(acquiredLocks.empty(), "ucb.ucp.webdav.curl", |
| 2390 |
"could not get LOCK for " << rURI.GetURI()); |
| 2391 |
|
| 2392 |
TimeValue endTime; |
| 2393 |
osl_getSystemTime(&endTime); |
| 2394 |
auto const elapsedSeconds(endTime.Seconds - startTime.Seconds); |
| 2395 |
|
| 2396 |
// determine expiration time (seconds from endTime) for each acquired lock |
| 2397 |
::std::vector<::std::pair<ucb::Lock, sal_Int32>> ret; |
| 2398 |
ret.reserve(acquiredLocks.size()); |
| 2399 |
for (auto const& rLock : acquiredLocks) |
| 2400 |
{ |
| 2401 |
sal_Int32 lockExpirationTimeSeconds; |
| 2402 |
if (rLock.Timeout == -1) |
| 2403 |
{ |
| 2404 |
lockExpirationTimeSeconds = -1; |
| 2405 |
} |
| 2406 |
else if (rLock.Timeout <= elapsedSeconds) |
| 2407 |
{ |
| 2408 |
SAL_WARN("ucb.ucp.webdav.curl", |
| 2409 |
"LOCK timeout already expired when receiving LOCK response for " |
| 2410 |
<< rURI.GetURI()); |
| 2411 |
lockExpirationTimeSeconds = 0; |
| 2412 |
} |
| 2413 |
else |
| 2414 |
{ |
| 2415 |
lockExpirationTimeSeconds = startTime.Seconds + rLock.Timeout; |
| 2416 |
} |
| 2417 |
ret.emplace_back(rLock, lockExpirationTimeSeconds); |
| 2418 |
} |
| 2419 |
|
| 2420 |
return ret; |
| 2421 |
} |
| 2422 |
|
| 2423 |
auto CurlSession::LOCK(OUString const& rURIReference, ucb::Lock /*const*/& rLock, |
| 2424 |
DAVRequestEnvironment const& rEnv) -> void |
| 2425 |
{ |
| 2426 |
SAL_INFO("ucb.ucp.webdav.curl", "LOCK: " << rURIReference); |
| 2427 |
|
| 2428 |
CurlUri const uri(CurlProcessor::URIReferenceToURI(*this, rURIReference)); |
| 2429 |
|
| 2430 |
if (g_Init.LockStore.getLockTokenForURI(uri.GetURI(), &rLock)) |
| 2431 |
{ |
| 2432 |
// already have a lock that covers the requirement |
| 2433 |
// TODO: maybe use DAV:lockdiscovery to ensure it's valid |
| 2434 |
return; |
| 2435 |
} |
| 2436 |
|
| 2437 |
// note: no m_Mutex lock needed here, only in CurlProcessor::Lock() |
| 2438 |
|
| 2439 |
// generate XML document for acquiring new LOCK |
| 2440 |
uno::Reference<io::XSequenceOutputStream> const xSeqOutStream( |
| 2441 |
io::SequenceOutputStream::create(m_xContext)); |
| 2442 |
uno::Reference<io::XOutputStream> const xRequestOutStream(xSeqOutStream); |
| 2443 |
assert(xRequestOutStream.is()); |
| 2444 |
uno::Reference<xml::sax::XWriter> const xWriter(xml::sax::Writer::create(m_xContext)); |
| 2445 |
xWriter->setOutputStream(xRequestOutStream); |
| 2446 |
xWriter->startDocument(); |
| 2447 |
rtl::Reference<::comphelper::AttributeList> const pAttrList(new ::comphelper::AttributeList); |
| 2448 |
pAttrList->AddAttribute(u"xmlns"_ustr, u"DAV:"_ustr); |
| 2449 |
xWriter->startElement(u"lockinfo"_ustr, pAttrList); |
| 2450 |
xWriter->startElement(u"lockscope"_ustr, nullptr); |
| 2451 |
switch (rLock.Scope) |
| 2452 |
{ |
| 2453 |
case ucb::LockScope_EXCLUSIVE: |
| 2454 |
xWriter->startElement(u"exclusive"_ustr, nullptr); |
| 2455 |
xWriter->endElement(u"exclusive"_ustr); |
| 2456 |
break; |
| 2457 |
case ucb::LockScope_SHARED: |
| 2458 |
xWriter->startElement(u"shared"_ustr, nullptr); |
| 2459 |
xWriter->endElement(u"shared"_ustr); |
| 2460 |
break; |
| 2461 |
default: |
| 2462 |
assert(false); |
| 2463 |
} |
| 2464 |
xWriter->endElement(u"lockscope"_ustr); |
| 2465 |
xWriter->startElement(u"locktype"_ustr, nullptr); |
| 2466 |
xWriter->startElement(u"write"_ustr, nullptr); |
| 2467 |
xWriter->endElement(u"write"_ustr); |
| 2468 |
xWriter->endElement(u"locktype"_ustr); |
| 2469 |
OUString owner; |
| 2470 |
if ((rLock.Owner >>= owner) && !owner.isEmpty()) |
| 2471 |
{ |
| 2472 |
xWriter->startElement(u"owner"_ustr, nullptr); |
| 2473 |
xWriter->characters(owner); |
| 2474 |
xWriter->endElement(u"owner"_ustr); |
| 2475 |
} |
| 2476 |
xWriter->endElement(u"lockinfo"_ustr); |
| 2477 |
xWriter->endDocument(); |
| 2478 |
|
| 2479 |
uno::Reference<io::XInputStream> const xRequestInStream( |
| 2480 |
io::SequenceInputStream::createStreamFromSequence(m_xContext, |
| 2481 |
xSeqOutStream->getWrittenBytes())); |
| 2482 |
assert(xRequestInStream.is()); |
| 2483 |
|
| 2484 |
::std::unique_ptr<curl_slist, deleter_from_fn<curl_slist, curl_slist_free_all>> pList; |
| 2485 |
pList.reset(curl_slist_append(pList.release(), "Content-Type: application/xml")); |
| 2486 |
if (!pList) |
| 2487 |
{ |
| 2488 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 2489 |
} |
| 2490 |
OString depth; |
| 2491 |
switch (rLock.Depth) |
| 2492 |
{ |
| 2493 |
case ucb::LockDepth_ZERO: |
| 2494 |
depth = "Depth: 0"_ostr; |
| 2495 |
break; |
| 2496 |
case ucb::LockDepth_ONE: |
| 2497 |
depth = "Depth: 1"_ostr; |
| 2498 |
break; |
| 2499 |
case ucb::LockDepth_INFINITY: |
| 2500 |
depth = "Depth: infinity"_ostr; |
| 2501 |
break; |
| 2502 |
default: |
| 2503 |
assert(false); |
| 2504 |
} |
| 2505 |
pList.reset(curl_slist_append(pList.release(), depth.getStr())); |
| 2506 |
if (!pList) |
| 2507 |
{ |
| 2508 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 2509 |
} |
| 2510 |
OString timeout; |
| 2511 |
switch (rLock.Timeout) |
| 2512 |
{ |
| 2513 |
case -1: |
| 2514 |
timeout = "Timeout: Infinite"_ostr; |
| 2515 |
break; |
| 2516 |
case 0: |
| 2517 |
timeout = "Timeout: Second-180"_ostr; |
| 2518 |
break; |
| 2519 |
default: |
| 2520 |
timeout = "Timeout: Second-" + OString::number(rLock.Timeout); |
| 2521 |
assert(0 < rLock.Timeout); |
| 2522 |
break; |
| 2523 |
} |
| 2524 |
pList.reset(curl_slist_append(pList.release(), timeout.getStr())); |
| 2525 |
if (!pList) |
| 2526 |
{ |
| 2527 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 2528 |
} |
| 2529 |
|
| 2530 |
auto const acquiredLocks |
| 2531 |
= CurlProcessor::Lock(*this, uri, &rEnv, ::std::move(pList), &xRequestInStream); |
| 2532 |
|
| 2533 |
for (auto const& rAcquiredLock : acquiredLocks) |
| 2534 |
{ |
| 2535 |
g_Init.LockStore.addLock(uri.GetURI(), rAcquiredLock.first, |
| 2536 |
rAcquiredLock.first.LockTokens[0], this, rAcquiredLock.second); |
| 2537 |
SAL_INFO("ucb.ucp.webdav.curl", "created LOCK for " << rURIReference); |
| 2538 |
} |
| 2539 |
} |
| 2540 |
|
| 2541 |
auto CurlProcessor::Unlock(CurlSession& rSession, CurlUri const& rURI, |
| 2542 |
DAVRequestEnvironment const* const pEnv) -> void |
| 2543 |
{ |
| 2544 |
OUString const* const pToken(g_Init.LockStore.getLockTokenForURI(rURI.GetURI(), nullptr)); |
| 2545 |
if (!pToken) |
| 2546 |
{ |
| 2547 |
SAL_WARN("ucb.ucp.webdav.curl", "attempt to unlock but not locked"); |
| 2548 |
throw DAVException(DAVException::DAV_NOT_LOCKED); |
| 2549 |
} |
| 2550 |
OString const utf8LockToken("Lock-Token: <" |
| 2551 |
+ OUStringToOString(*pToken, RTL_TEXTENCODING_ASCII_US) + ">"); |
| 2552 |
::std::unique_ptr<curl_slist, deleter_from_fn<curl_slist, curl_slist_free_all>> pList( |
| 2553 |
curl_slist_append(nullptr, utf8LockToken.getStr())); |
| 2554 |
if (!pList) |
| 2555 |
{ |
| 2556 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 2557 |
} |
| 2558 |
|
| 2559 |
::std::vector<CurlOption> const options{ { CURLOPT_CUSTOMREQUEST, "UNLOCK", |
| 2560 |
"CURLOPT_CUSTOMREQUEST" } }; |
| 2561 |
|
| 2562 |
CurlProcessor::ProcessRequest(rSession, rURI, u"UNLOCK"_ustr, options, pEnv, ::std::move(pList), |
| 2563 |
nullptr, nullptr, nullptr); |
| 2564 |
} |
| 2565 |
|
| 2566 |
auto CurlSession::UNLOCK(OUString const& rURIReference, DAVRequestEnvironment const& rEnv) -> void |
| 2567 |
{ |
| 2568 |
SAL_INFO("ucb.ucp.webdav.curl", "UNLOCK: " << rURIReference); |
| 2569 |
|
| 2570 |
// note: no m_Mutex lock needed here, only in CurlProcessor::Unlock() |
| 2571 |
|
| 2572 |
CurlUri const uri(CurlProcessor::URIReferenceToURI(*this, rURIReference)); |
| 2573 |
|
| 2574 |
CurlProcessor::Unlock(*this, uri, &rEnv); |
| 2575 |
|
| 2576 |
g_Init.LockStore.removeLock(uri.GetURI()); |
| 2577 |
} |
| 2578 |
|
| 2579 |
auto CurlSession::NonInteractive_LOCK(OUString const& rURI, ::std::u16string_view const rLockToken, |
| 2580 |
sal_Int32& o_rLastChanceToSendRefreshRequest, |
| 2581 |
bool& o_rIsAuthFailed) -> bool |
| 2582 |
{ |
| 2583 |
SAL_INFO("ucb.ucp.webdav.curl", "NonInteractive_LOCK: " << rURI); |
| 2584 |
|
| 2585 |
// note: no m_Mutex lock needed here, only in CurlProcessor::Lock() |
| 2586 |
|
| 2587 |
try |
| 2588 |
{ |
| 2589 |
CurlUri const uri(rURI); |
| 2590 |
::std::unique_ptr<curl_slist, deleter_from_fn<curl_slist, curl_slist_free_all>> pList( |
| 2591 |
curl_slist_append(nullptr, "Timeout: Second-180")); |
| 2592 |
|
| 2593 |
assert(!rLockToken.empty()); // LockStore is the caller |
| 2594 |
OString const utf8If("If: (<" + OUStringToOString(rLockToken, RTL_TEXTENCODING_ASCII_US) |
| 2595 |
+ ">)"); |
| 2596 |
pList.reset(curl_slist_append(pList.release(), utf8If.getStr())); |
| 2597 |
if (!pList) |
| 2598 |
{ |
| 2599 |
throw uno::RuntimeException(u"curl_slist_append failed"_ustr); |
| 2600 |
} |
| 2601 |
|
| 2602 |
auto const acquiredLocks |
| 2603 |
= CurlProcessor::Lock(*this, uri, nullptr, ::std::move(pList), nullptr); |
| 2604 |
|
| 2605 |
SAL_WARN_IF(1 < acquiredLocks.size(), "ucb.ucp.webdav.curl", |
| 2606 |
"multiple locks acquired on refresh for " << rURI); |
| 2607 |
if (!acquiredLocks.empty()) |
| 2608 |
{ |
| 2609 |
o_rLastChanceToSendRefreshRequest = acquiredLocks.begin()->second; |
| 2610 |
} |
| 2611 |
SAL_INFO("ucb.ucp.webdav.curl", "NonInteractive_LOCK succeeded on " << rURI); |
| 2612 |
return true; |
| 2613 |
} |
| 2614 |
catch (DAVException const& rException) |
| 2615 |
{ |
| 2616 |
SAL_INFO("ucb.ucp.webdav.curl", "NonInteractive_LOCK failed on " << rURI); |
| 2617 |
switch (rException.getError()) |
| 2618 |
{ |
| 2619 |
case DAVException::DAV_HTTP_AUTH: |
| 2620 |
case DAVException::DAV_HTTP_NOAUTH: |
| 2621 |
o_rIsAuthFailed = true; |
| 2622 |
break; |
| 2623 |
default: |
| 2624 |
break; |
| 2625 |
} |
| 2626 |
return false; |
| 2627 |
} |
| 2628 |
catch (...) |
| 2629 |
{ |
| 2630 |
SAL_INFO("ucb.ucp.webdav.curl", "NonInteractive_LOCK failed on " << rURI); |
| 2631 |
return false; |
| 2632 |
} |
| 2633 |
} |
| 2634 |
|
| 2635 |
auto CurlSession::NonInteractive_UNLOCK(OUString const& rURI) -> void |
| 2636 |
{ |
| 2637 |
SAL_INFO("ucb.ucp.webdav.curl", "NonInteractive_UNLOCK: " << rURI); |
| 2638 |
|
| 2639 |
// note: no m_Mutex lock needed here, only in CurlProcessor::Unlock() |
| 2640 |
|
| 2641 |
try |
| 2642 |
{ |
| 2643 |
CurlUri const uri(rURI); |
| 2644 |
|
| 2645 |
CurlProcessor::Unlock(*this, uri, nullptr); |
| 2646 |
|
| 2647 |
// the only caller is the dtor of the LockStore, don't call remove! |
| 2648 |
SAL_INFO("ucb.ucp.webdav.curl", "NonInteractive_UNLOCK succeeded on " << rURI); |
| 2649 |
} |
| 2650 |
catch (...) |
| 2651 |
{ |
| 2652 |
SAL_INFO("ucb.ucp.webdav.curl", "NonInteractive_UNLOCK failed on " << rURI); |
| 2653 |
} |
| 2654 |
} |
| 2655 |
|
| 2656 |
} // namespace http_dav_ucp |
| 2657 |
|
| 2658 |
namespace |
| 2659 |
{ |
| 2660 |
/// Manage lifecycle of global DAV worker threads |
| 2661 |
class WebDAVManager : public cppu::WeakImplHelper<css::lang::XServiceInfo>, |
| 2662 |
public comphelper::LibreOfficeKit::ThreadJoinable |
| 2663 |
{ |
| 2664 |
public: |
| 2665 |
WebDAVManager() {} |
| 2666 |
|
| 2667 |
// XServiceInfo |
| 2668 |
virtual OUString SAL_CALL getImplementationName() override |
| 2669 |
{ |
| 2670 |
return "com.sun.star.comp.WebDAVManager"; |
| 2671 |
} |
| 2672 |
virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override |
| 2673 |
{ |
| 2674 |
return cppu::supportsService(this, ServiceName); |
| 2675 |
} |
| 2676 |
virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override |
| 2677 |
{ |
| 2678 |
return { "com.sun.star.ucb.WebDAVManager" }; |
| 2679 |
} |
| 2680 |
|
| 2681 |
// comphelper::LibreOfficeKit::ThreadJoinable |
| 2682 |
virtual bool joinThreads() override { return g_Init.LockStore.joinThreads(); } |
| 2683 |
|
| 2684 |
virtual void startThreads() override { g_Init.LockStore.startThreads(); } |
| 2685 |
}; |
| 2686 |
|
| 2687 |
} // anonymous namespace |
| 2688 |
|
| 2689 |
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* |
| 2690 |
ucb_webdav_manager_get_implementation(css::uno::XComponentContext*, |
| 2691 |
css::uno::Sequence<css::uno::Any> const&) |
| 2692 |
{ |
| 2693 |
return cppu::acquire(new WebDAVManager()); |
| 2694 |
} |
| 2695 |
|
| 2696 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |