Debian Codegraph @

ucb/source/ucp/webdav-curl/DAVResourceAccess.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20
21 #include <com/sun/star/task/XInteractionAbort.hpp>
22 #include <com/sun/star/ucb/XWebDAVCommandEnvironment.hpp>
23
24 #include <ucbhelper/simpleauthenticationrequest.hxx>
25 #include <comphelper/seekableinput.hxx>
26
27 #include "DAVAuthListenerImpl.hxx"
28 #include "DAVResourceAccess.hxx"
29 #include "webdavprovider.hxx"
30
31 #include <officecfg/Office/Security.hxx>
32
33 #include <com/sun/star/lang/IllegalArgumentException.hpp>
34 #include <com/sun/star/io/IOException.hpp>
35 #include <utility>
36
37 using namespace http_dav_ucp;
38 using namespace com::sun::star;
39
40
41 // DAVAuthListener_Impl Implementation.
42
43
44 // virtual
45 int DAVAuthListener_Impl::authenticate(
46 const OUString & inRealm,
47 const OUString & inHostName,
48 OUString & inoutUserName,
49 OUString & outPassWord,
50 bool bCanUseSystemCredentials )
51 {
52 if ( m_xEnv.is() )
53 {
54 uno::Reference< task::XInteractionHandler > xIH
55 = m_xEnv->getInteractionHandler();
56
57 if ( xIH.is() )
58 {
59 // Providing previously retrieved credentials will cause the password
60 // container to reject these. Thus, the credential input dialog will be shown again.
61 // #102871# - Supply username and password from previous try.
62 // Password container service depends on this!
63 if ( inoutUserName.isEmpty() )
64 inoutUserName = m_aPrevUsername;
65
66 if ( outPassWord.isEmpty() )
67 outPassWord = m_aPrevPassword;
68
69 rtl::Reference< ucbhelper::SimpleAuthenticationRequest > xRequest
70 = new ucbhelper::SimpleAuthenticationRequest(
71 m_aURL, inHostName, inRealm, inoutUserName,
72 outPassWord,
73 bCanUseSystemCredentials );
74 xIH->handle( xRequest );
75
76 rtl::Reference< ucbhelper::InteractionContinuation > xSelection
77 = xRequest->getSelection();
78
79 if ( xSelection.is() )
80 {
81 // Handler handled the request.
82 uno::Reference< task::XInteractionAbort > xAbort(
83 xSelection->getXWeak(), uno::UNO_QUERY );
84 if ( !xAbort.is() )
85 {
86 const rtl::Reference<
87 ucbhelper::InteractionSupplyAuthentication > & xSupp
88 = xRequest->getAuthenticationSupplier();
89
90 bool bUseSystemCredentials = false;
91
92 if ( bCanUseSystemCredentials )
93 bUseSystemCredentials
94 = xSupp->getUseSystemCredentials();
95
96 if ( bUseSystemCredentials )
97 {
98 // This is the (strange) way to tell neon to use
99 // system credentials.
100 inoutUserName.clear();
101 outPassWord.clear();
102 }
103 else
104 {
105 inoutUserName = xSupp->getUserName();
106 outPassWord = xSupp->getPassword();
107 }
108
109 // #102871# - Remember username and password.
110 m_aPrevUsername = inoutUserName;
111 m_aPrevPassword = outPassWord;
112
113 // go on.
114 return 0;
115 }
116 }
117 }
118 }
119 // Abort.
120 return -1;
121 }
122
123
124 // DAVResourceAccess Implementation.
125
126 constexpr size_t g_nRedirectLimit = 5;
127
128 DAVResourceAccess::DAVResourceAccess(
129 uno::Reference< uno::XComponentContext > xContext,
130 rtl::Reference< DAVSessionFactory > xSessionFactory,
131 OUString aURL )
132 : m_aURL(std::move( aURL )),
133 m_xSessionFactory(std::move( xSessionFactory )),
134 m_xContext(std::move( xContext ))
135 {
136 }
137
138
139 DAVResourceAccess::DAVResourceAccess( const DAVResourceAccess & rOther )
140 : m_aURL( rOther.m_aURL ),
141 m_aPath( rOther.m_aPath ),
142 m_aFlags( rOther.m_aFlags ),
143 m_xSession( rOther.m_xSession ),
144 m_xSessionFactory( rOther.m_xSessionFactory ),
145 m_xContext( rOther.m_xContext ),
146 m_aRedirectURIs( rOther.m_aRedirectURIs )
147 {
148 }
149
150
151 DAVResourceAccess & DAVResourceAccess::operator=(
152 const DAVResourceAccess & rOther )
153 {
154 m_aURL = rOther.m_aURL;
155 m_aPath = rOther.m_aPath;
156 m_aFlags = rOther.m_aFlags;
157 m_xSession = rOther.m_xSession;
158 m_xSessionFactory = rOther.m_xSessionFactory;
159 m_xContext = rOther.m_xContext;
160 m_aRedirectURIs = rOther.m_aRedirectURIs;
161
162 return *this;
163 }
164
165 void DAVResourceAccess::OPTIONS(
166 DAVOptions & rOptions,
167 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
168 {
169 initialize();
170
171 int errorCount = 0;
172 bool bRetry;
173 do
174 {
175 bRetry = false;
176 try
177 {
178 DAVRequestHeaders aHeaders;
179
180 getUserRequestHeaders( xEnv,
181 getRequestURI(),
182 ucb::WebDAVHTTPMethod_OPTIONS,
183 aHeaders );
184
185 m_xSession->OPTIONS( getRequestURI(),
186 rOptions,
187 DAVRequestEnvironment(
188 new DAVAuthListener_Impl( xEnv, m_aURL ),
189 std::move(aHeaders) ) );
190 }
191 catch (DAVException const& e)
192 {
193 errorCount++;
194 bRetry = handleException( e, errorCount );
195 if ( !bRetry )
196 throw;
197 }
198 }
199 while ( bRetry );
200 }
201
202 void DAVResourceAccess::PROPFIND(
203 const Depth nDepth,
204 const std::vector< OUString > & rPropertyNames,
205 std::vector< DAVResource > & rResources,
206 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
207 {
208 initialize();
209
210 int errorCount = 0;
211 bool bRetry;
212 do
213 {
214 bRetry = false;
215 try
216 {
217 DAVRequestHeaders aHeaders;
218
219 getUserRequestHeaders( xEnv,
220 getRequestURI(),
221 ucb::WebDAVHTTPMethod_PROPFIND,
222 aHeaders );
223
224 m_xSession->PROPFIND( getRequestURI(),
225 nDepth,
226 rPropertyNames,
227 rResources,
228 DAVRequestEnvironment(
229 new DAVAuthListener_Impl( xEnv, m_aURL ),
230 std::move(aHeaders) ) );
231 }
232 catch (DAVException const& e)
233 {
234 errorCount++;
235 bRetry = handleException( e, errorCount );
236 if ( !bRetry )
237 throw;
238 }
239 }
240 while ( bRetry );
241 }
242
243
244 void DAVResourceAccess::PROPFIND(
245 const Depth nDepth,
246 std::vector< DAVResourceInfo > & rResInfo,
247 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
248 {
249 initialize();
250
251 int errorCount = 0;
252 bool bRetry;
253 do
254 {
255 bRetry = false;
256 try
257 {
258 DAVRequestHeaders aHeaders;
259 getUserRequestHeaders( xEnv,
260 getRequestURI(),
261 ucb::WebDAVHTTPMethod_PROPFIND,
262 aHeaders );
263
264 m_xSession->PROPFIND( getRequestURI(),
265 nDepth,
266 rResInfo,
267 DAVRequestEnvironment(
268 new DAVAuthListener_Impl( xEnv, m_aURL ),
269 std::move(aHeaders) ) ) ;
270 }
271 catch (DAVException const& e)
272 {
273 errorCount++;
274 bRetry = handleException( e, errorCount );
275 if ( !bRetry )
276 throw;
277 }
278 }
279 while ( bRetry );
280 }
281
282
283 void DAVResourceAccess::PROPPATCH(
284 const std::vector< ProppatchValue >& rValues,
285 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
286 {
287 initialize();
288
289 int errorCount = 0;
290 bool bRetry;
291 do
292 {
293 bRetry = false;
294 try
295 {
296 DAVRequestHeaders aHeaders;
297 getUserRequestHeaders( xEnv,
298 getRequestURI(),
299 ucb::WebDAVHTTPMethod_PROPPATCH,
300 aHeaders );
301
302 m_xSession->PROPPATCH( getRequestURI(),
303 rValues,
304 DAVRequestEnvironment(
305 new DAVAuthListener_Impl( xEnv, m_aURL ),
306 std::move(aHeaders) ) );
307 }
308 catch (DAVException const& e)
309 {
310 errorCount++;
311 bRetry = handleException( e, errorCount );
312 if ( !bRetry )
313 throw;
314 }
315 }
316 while ( bRetry );
317 }
318
319
320 void DAVResourceAccess::HEAD(
321 const std::vector< OUString > & rHeaderNames,
322 DAVResource & rResource,
323 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
324 {
325 initialize();
326
327 int errorCount = 0;
328 bool bRetry;
329 do
330 {
331 bRetry = false;
332 try
333 {
334 DAVRequestHeaders aHeaders;
335 getUserRequestHeaders( xEnv,
336 getRequestURI(),
337 ucb::WebDAVHTTPMethod_HEAD,
338 aHeaders );
339
340 m_xSession->HEAD( getRequestURI(),
341 rHeaderNames,
342 rResource,
343 DAVRequestEnvironment(
344 new DAVAuthListener_Impl( xEnv, m_aURL ),
345 std::move(aHeaders) ) );
346 }
347 catch (DAVException const& e)
348 {
349 errorCount++;
350 bRetry = handleException( e, errorCount );
351 if ( !bRetry )
352 throw;
353 }
354 }
355 while ( bRetry );
356 }
357
358
359 uno::Reference< io::XInputStream > DAVResourceAccess::GET(
360 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
361 {
362 initialize();
363
364 uno::Reference< io::XInputStream > xStream;
365 int errorCount = 0;
366 bool bRetry;
367 do
368 {
369 bRetry = false;
370 try
371 {
372 DAVRequestHeaders aHeaders;
373 getUserRequestHeaders( xEnv,
374 getRequestURI(),
375 ucb::WebDAVHTTPMethod_GET,
376 aHeaders );
377
378 xStream = m_xSession->GET( getRequestURI(),
379 DAVRequestEnvironment(
380 new DAVAuthListener_Impl(
381 xEnv, m_aURL ),
382 std::move(aHeaders) ) );
383 }
384 catch (DAVException const& e)
385 {
386 errorCount++;
387 bRetry = handleException( e, errorCount );
388 if ( !bRetry )
389 throw;
390 }
391 }
392 while ( bRetry );
393
394 return xStream;
395 }
396
397
398 void DAVResourceAccess::GET(
399 uno::Reference< io::XOutputStream > & rStream,
400 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
401 {
402 initialize();
403
404 int errorCount = 0;
405 bool bRetry;
406 do
407 {
408 bRetry = false;
409 try
410 {
411 DAVRequestHeaders aHeaders;
412 getUserRequestHeaders( xEnv,
413 getRequestURI(),
414 ucb::WebDAVHTTPMethod_GET,
415 aHeaders );
416
417 m_xSession->GET( getRequestURI(),
418 rStream,
419 DAVRequestEnvironment(
420 new DAVAuthListener_Impl( xEnv, m_aURL ),
421 std::move(aHeaders) ) );
422 }
423 catch (DAVException const& e)
424 {
425 errorCount++;
426 bRetry = handleException( e, errorCount );
427 if ( !bRetry )
428 throw;
429 }
430 }
431 while ( bRetry );
432 }
433
434
435 uno::Reference< io::XInputStream > DAVResourceAccess::GET(
436 const std::vector< OUString > & rHeaderNames,
437 DAVResource & rResource,
438 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
439 {
440 initialize();
441
442 uno::Reference< io::XInputStream > xStream;
443 int errorCount = 0;
444 bool bRetry;
445 do
446 {
447 bRetry = false;
448 try
449 {
450 DAVRequestHeaders aHeaders;
451 getUserRequestHeaders( xEnv,
452 getRequestURI(),
453 ucb::WebDAVHTTPMethod_GET,
454 aHeaders );
455
456 xStream = m_xSession->GET( getRequestURI(),
457 rHeaderNames,
458 rResource,
459 DAVRequestEnvironment(
460 new DAVAuthListener_Impl(
461 xEnv, m_aURL ),
462 std::move(aHeaders) ) );
463 }
464 catch (DAVException const& e)
465 {
466 errorCount++;
467 bRetry = handleException( e, errorCount );
468 if ( !bRetry )
469 throw;
470 }
471 }
472 while ( bRetry );
473
474 return xStream;
475 }
476
477
478 // used as HEAD substitute when HEAD is not implemented on server
479 void DAVResourceAccess::GET0(
480 DAVRequestHeaders &rRequestHeaders,
481 const std::vector< OUString > & rHeaderNames,
482 DAVResource & rResource,
483 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
484 {
485 initialize();
486
487 int errorCount = 0;
488 bool bRetry;
489 do
490 {
491 bRetry = false;
492 try
493 {
494 getUserRequestHeaders( xEnv,
495 getRequestURI(),
496 ucb::WebDAVHTTPMethod_GET,
497 rRequestHeaders );
498
499 m_xSession->GET( getRequestURI(),
500 rHeaderNames,
501 rResource,
502 DAVRequestEnvironment(
503 new DAVAuthListener_Impl(
504 xEnv, m_aURL ),
505 rRequestHeaders ) );
506 }
507 catch (DAVException const& e)
508 {
509 errorCount++;
510 bRetry = handleException( e, errorCount );
511 if ( !bRetry )
512 throw;
513 }
514 }
515 while ( bRetry );
516 }
517
518
519 void DAVResourceAccess::GET(
520 uno::Reference< io::XOutputStream > & rStream,
521 const std::vector< OUString > & rHeaderNames,
522 DAVResource & rResource,
523 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
524 {
525 initialize();
526
527 bool bRetry;
528 int errorCount = 0;
529 do
530 {
531 bRetry = false;
532 try
533 {
534 DAVRequestHeaders aHeaders;
535 getUserRequestHeaders( xEnv,
536 getRequestURI(),
537 ucb::WebDAVHTTPMethod_GET,
538 aHeaders );
539
540 m_xSession->GET( getRequestURI(),
541 rStream,
542 rHeaderNames,
543 rResource,
544 DAVRequestEnvironment(
545 new DAVAuthListener_Impl( xEnv, m_aURL ),
546 std::move(aHeaders) ) );
547 }
548 catch (DAVException const& e)
549 {
550 errorCount++;
551 bRetry = handleException( e, errorCount );
552 if ( !bRetry )
553 throw;
554 }
555 }
556 while ( bRetry );
557 }
558
559
560 void DAVResourceAccess::abort()
561 {
562 // seems pointless to call initialize() here, but prepare for nullptr
563 decltype(m_xSession) xSession;
564 {
565 osl::Guard<osl::Mutex> const g(m_aMutex);
566 xSession = m_xSession;
567 }
568 if (xSession.is())
569 {
570 xSession->abort();
571 }
572 }
573
574
575 namespace {
576
577 /// @throws DAVException
578 void resetInputStream( const uno::Reference< io::XInputStream > & rStream )
579 {
580 try
581 {
582 uno::Reference< io::XSeekable > xSeekable(
583 rStream, uno::UNO_QUERY );
584 if ( xSeekable.is() )
585 {
586 xSeekable->seek( 0 );
587 return;
588 }
589 }
590 catch ( lang::IllegalArgumentException const & )
591 {
592 }
593 catch ( io::IOException const & )
594 {
595 }
596
597 throw DAVException( DAVException::DAV_INVALID_ARG );
598 }
599
600 } // namespace
601
602
603 void DAVResourceAccess::PUT(
604 const uno::Reference< io::XInputStream > & rStream,
605 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
606 {
607 initialize();
608
609 // Make stream seekable, if it not. Needed, if request must be retried.
610 uno::Reference< io::XInputStream > xSeekableStream
611 = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
612 rStream, m_xContext );
613
614 int errorCount = 0;
615 bool bRetry = false;
616 do
617 {
618 if ( bRetry )
619 resetInputStream( xSeekableStream );
620
621 bRetry = false;
622 try
623 {
624 DAVRequestHeaders aHeaders;
625 getUserRequestHeaders( xEnv,
626 getRequestURI(),
627 ucb::WebDAVHTTPMethod_PUT,
628 aHeaders );
629
630 m_xSession->PUT( getRequestURI(),
631 xSeekableStream,
632 DAVRequestEnvironment(
633 new DAVAuthListener_Impl( xEnv, m_aURL ),
634 std::move(aHeaders) ) );
635 }
636 catch (DAVException const& e)
637 {
638 errorCount++;
639 bRetry = handleException( e, errorCount );
640 if ( !bRetry )
641 throw;
642 }
643 }
644 while ( bRetry );
645 }
646
647
648 uno::Reference< io::XInputStream > DAVResourceAccess::POST(
649 const OUString & rContentType,
650 const OUString & rReferer,
651 const uno::Reference< io::XInputStream > & rInputStream,
652 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
653 {
654 initialize();
655
656 // Make stream seekable, if it not. Needed, if request must be retried.
657 uno::Reference< io::XInputStream > xSeekableStream
658 = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
659 rInputStream, m_xContext );
660
661 uno::Reference< io::XInputStream > xStream;
662 int errorCount = 0;
663 bool bRetry = false;
664 do
665 {
666 if ( bRetry )
667 {
668 resetInputStream( xSeekableStream );
669 bRetry = false;
670 }
671
672 try
673 {
674 DAVRequestHeaders aHeaders;
675 getUserRequestHeaders( xEnv,
676 getRequestURI(),
677 ucb::WebDAVHTTPMethod_POST,
678 aHeaders );
679
680 xStream = m_xSession->POST( getRequestURI(),
681 rContentType,
682 rReferer,
683 xSeekableStream,
684 DAVRequestEnvironment(
685 new DAVAuthListener_Impl(
686 xEnv, m_aURL ),
687 std::move(aHeaders) ) );
688 }
689 catch (DAVException const& e)
690 {
691 errorCount++;
692 bRetry = handleException( e, errorCount );
693 if ( !bRetry )
694 throw;
695
696 if ( e.getError() == DAVException::DAV_HTTP_REDIRECT )
697 {
698 // #i74980# - Upon POST redirect, do a GET.
699 return GET( xEnv );
700 }
701 }
702 }
703 while ( bRetry );
704
705 return xStream;
706 }
707
708
709 void DAVResourceAccess::POST(
710 const OUString & rContentType,
711 const OUString & rReferer,
712 const uno::Reference< io::XInputStream > & rInputStream,
713 uno::Reference< io::XOutputStream > & rOutputStream,
714 const uno::Reference< ucb::XCommandEnvironment >& xEnv )
715 {
716 initialize();
717
718 // Make stream seekable, if it not. Needed, if request must be retried.
719 uno::Reference< io::XInputStream > xSeekableStream
720 = comphelper::OSeekableInputWrapper::CheckSeekableCanWrap(
721 rInputStream, m_xContext );
722
723 int errorCount = 0;
724 bool bRetry = false;
725 do
726 {
727 if ( bRetry )
728 {
729 resetInputStream( xSeekableStream );
730 bRetry = false;
731 }
732
733 try
734 {
735 DAVRequestHeaders aHeaders;
736 getUserRequestHeaders( xEnv,
737 getRequestURI(),
738 ucb::WebDAVHTTPMethod_POST,
739 aHeaders );
740
741 m_xSession->POST( getRequestURI(),
742 rContentType,
743 rReferer,
744 xSeekableStream,
745 rOutputStream,
746 DAVRequestEnvironment(
747 new DAVAuthListener_Impl( xEnv, m_aURL ),
748 std::move(aHeaders) ) );
749 }
750 catch (DAVException const& e)
751 {
752 errorCount++;
753 bRetry = handleException( e, errorCount );
754 if ( !bRetry )
755 throw;
756
757 if ( e.getError() == DAVException::DAV_HTTP_REDIRECT )
758 {
759 // #i74980# - Upon POST redirect, do a GET.
760 GET( rOutputStream, xEnv );
761 return;
762 }
763 }
764 }
765 while ( bRetry );
766 }
767
768
769 void DAVResourceAccess::MKCOL(
770 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
771 {
772 initialize();
773
774 int errorCount = 0;
775 bool bRetry;
776 do
777 {
778 bRetry = false;
779 try
780 {
781 DAVRequestHeaders aHeaders;
782 getUserRequestHeaders( xEnv,
783 getRequestURI(),
784 ucb::WebDAVHTTPMethod_MKCOL,
785 aHeaders );
786
787 m_xSession->MKCOL( getRequestURI(),
788 DAVRequestEnvironment(
789 new DAVAuthListener_Impl( xEnv, m_aURL ),
790 std::move(aHeaders) ) );
791 }
792 catch (DAVException const& e)
793 {
794 errorCount++;
795 bRetry = handleException( e, errorCount );
796 if ( !bRetry )
797 throw;
798 }
799 }
800 while ( bRetry );
801 }
802
803
804 void DAVResourceAccess::COPY(
805 const OUString & rSourcePath,
806 const OUString & rDestinationURI,
807 bool bOverwrite,
808 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
809 {
810 initialize();
811
812 int errorCount = 0;
813 bool bRetry;
814 do
815 {
816 bRetry = false;
817 try
818 {
819 DAVRequestHeaders aHeaders;
820 getUserRequestHeaders( xEnv,
821 getRequestURI(),
822 ucb::WebDAVHTTPMethod_COPY,
823 aHeaders );
824
825 m_xSession->COPY( rSourcePath,
826 rDestinationURI,
827 DAVRequestEnvironment(
828 new DAVAuthListener_Impl( xEnv, m_aURL ),
829 std::move(aHeaders) ),
830 bOverwrite );
831 }
832 catch (DAVException const& e)
833 {
834 errorCount++;
835 bRetry = handleException( e, errorCount );
836 if ( !bRetry )
837 throw;
838 }
839 }
840 while ( bRetry );
841 }
842
843
844 void DAVResourceAccess::MOVE(
845 const OUString & rSourcePath,
846 const OUString & rDestinationURI,
847 bool bOverwrite,
848 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
849 {
850 initialize();
851
852 int errorCount = 0;
853 bool bRetry;
854 do
855 {
856 bRetry = false;
857 try
858 {
859 DAVRequestHeaders aHeaders;
860 getUserRequestHeaders( xEnv,
861 getRequestURI(),
862 ucb::WebDAVHTTPMethod_MOVE,
863 aHeaders );
864
865 m_xSession->MOVE( rSourcePath,
866 rDestinationURI,
867 DAVRequestEnvironment(
868 new DAVAuthListener_Impl( xEnv, m_aURL ),
869 std::move(aHeaders) ),
870 bOverwrite );
871 }
872 catch (DAVException const& e)
873 {
874 errorCount++;
875 bRetry = handleException( e, errorCount );
876 if ( !bRetry )
877 throw;
878 }
879 }
880 while ( bRetry );
881 }
882
883
884 void DAVResourceAccess::DESTROY(
885 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
886 {
887 initialize();
888
889 int errorCount = 0;
890 bool bRetry;
891 do
892 {
893 bRetry = false;
894 try
895 {
896 DAVRequestHeaders aHeaders;
897 getUserRequestHeaders( xEnv,
898 getRequestURI(),
899 ucb::WebDAVHTTPMethod_DELETE,
900 aHeaders );
901
902 m_xSession->DESTROY( getRequestURI(),
903 DAVRequestEnvironment(
904 new DAVAuthListener_Impl( xEnv, m_aURL ),
905 std::move(aHeaders) ) );
906 }
907 catch (DAVException const& e)
908 {
909 errorCount++;
910 bRetry = handleException( e, errorCount );
911 if ( !bRetry )
912 throw;
913 }
914 }
915 while ( bRetry );
916 }
917
918
919 // set new lock.
920 void DAVResourceAccess::LOCK(
921 ucb::Lock & inLock,
922 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
923 {
924 initialize();
925
926 int errorCount = 0;
927 bool bRetry;
928 do
929 {
930 bRetry = false;
931 try
932 {
933 DAVRequestHeaders aHeaders;
934 getUserRequestHeaders( xEnv,
935 getRequestURI(),
936 ucb::WebDAVHTTPMethod_LOCK,
937 aHeaders );
938
939 m_xSession->LOCK( getRequestURI(),
940 inLock,
941 DAVRequestEnvironment(
942 new DAVAuthListener_Impl( xEnv, m_aURL ),
943 std::move(aHeaders) ) );
944 }
945 catch (DAVException const& e)
946 {
947 errorCount++;
948 bRetry = handleException( e, errorCount );
949 if ( !bRetry )
950 throw;
951 }
952 }
953 while ( bRetry );
954 }
955
956 void DAVResourceAccess::UNLOCK(
957 const uno::Reference< ucb::XCommandEnvironment > & xEnv )
958 {
959 initialize();
960
961 int errorCount = 0;
962 bool bRetry;
963 do
964 {
965 bRetry = false;
966 try
967 {
968 DAVRequestHeaders aHeaders;
969 getUserRequestHeaders( xEnv,
970 getRequestURI(),
971 ucb::WebDAVHTTPMethod_UNLOCK,
972 aHeaders );
973
974 m_xSession->UNLOCK( getRequestURI(),
975 DAVRequestEnvironment(
976 new DAVAuthListener_Impl( xEnv, m_aURL ),
977 std::move(aHeaders) ) );
978 }
979 catch (DAVException const& e)
980 {
981 errorCount++;
982 bRetry = handleException( e, errorCount );
983 if ( !bRetry )
984 throw;
985 }
986 }
987 while ( bRetry );
988 }
989
990 void DAVResourceAccess::setFlags( const uno::Sequence< beans::NamedValue >& rFlags )
991 {
992 osl::Guard< osl::Mutex > aGuard( m_aMutex );
993 m_aFlags = rFlags;
994 }
995
996 void DAVResourceAccess::setURL( const OUString & rNewURL )
997 {
998 osl::Guard< osl::Mutex > aGuard( m_aMutex );
999 m_aURL = rNewURL;
1000 m_aPath.clear(); // Next initialize() will create new session.
1001 }
1002
1003
1004 // init dav session and path
1005 void DAVResourceAccess::initialize()
1006 {
1007 osl::Guard< osl::Mutex > aGuard( m_aMutex );
1008 if ( m_aPath.isEmpty() )
1009 {
1010 CurlUri aURI(m_aURL);
1011 assert(aURI.GetScheme() == HTTP_URL_SCHEME || aURI.GetScheme() == HTTPS_URL_SCHEME);
1012 if (aURI.GetScheme() == HTTP_URL_SCHEME)
1013 {
1014 if (!officecfg::Office::Security::Net::AllowInsecureProtocols::get())
1015 {
1016 // "http" not allowed -> immediately redirect to "https",
1017 // better than showing confusing error to user
1018 aURI.SetScheme(HTTPS_URL_SCHEME);
1019 }
1020 }
1021 OUString aPath( aURI.GetRelativeReference() );
1022
1023 /* #134089# - Check URI */
1024 if ( aPath.isEmpty() )
1025 throw DAVException( DAVException::DAV_INVALID_ARG );
1026
1027 /* #134089# - Check URI */
1028 if ( aURI.GetHost().isEmpty() )
1029 throw DAVException( DAVException::DAV_INVALID_ARG );
1030
1031 if ( !m_xSession.is() || !m_xSession->CanUse( m_aURL, m_aFlags ) )
1032 {
1033 m_xSession.clear();
1034
1035 // create new webdav session
1036 m_xSession = m_xSessionFactory->createDAVSession(aURI.GetURI(), m_aFlags, m_xContext);
1037
1038 if ( !m_xSession.is() )
1039 return;
1040 }
1041
1042 // Own URI is needed to redirect cycle detection.
1043 m_aRedirectURIs.push_back( aURI );
1044
1045 // Success.
1046 m_aPath = aPath;
1047
1048 // Not only the path has to be encoded
1049 m_aURL = aURI.GetURI();
1050 }
1051 }
1052
1053
1054 const OUString & DAVResourceAccess::getRequestURI() const
1055 {
1056 assert(m_xSession.is() &&
1057 "DAVResourceAccess::getRequestURI - Not initialized!");
1058
1059 // In case a proxy is used we have to use the absolute URI for a request.
1060 if ( m_xSession->UsesProxy() )
1061 return m_aURL;
1062
1063 return m_aPath;
1064 }
1065
1066
1067 // static
1068 void DAVResourceAccess::getUserRequestHeaders(
1069 const uno::Reference< ucb::XCommandEnvironment > & xEnv,
1070 const OUString & rURI,
1071 ucb::WebDAVHTTPMethod eMethod,
1072 DAVRequestHeaders & rRequestHeaders )
1073 {
1074 if ( !xEnv.is() )
1075 return;
1076
1077 uno::Reference< ucb::XWebDAVCommandEnvironment > xDAVEnv(
1078 xEnv, uno::UNO_QUERY );
1079
1080 if ( !xDAVEnv.is() )
1081 return;
1082
1083 uno::Sequence< beans::StringPair > aRequestHeaders
1084 = xDAVEnv->getUserRequestHeaders( rURI, eMethod );
1085
1086 for ( sal_Int32 n = 0; n < aRequestHeaders.getLength(); ++n )
1087 {
1088 rRequestHeaders.push_back(
1089 DAVRequestHeader( aRequestHeaders[ n ].First,
1090 aRequestHeaders[ n ].Second ) );
1091 }
1092 }
1093
1094 // This function member implements the control on cyclical redirections
1095 bool DAVResourceAccess::detectRedirectCycle(
1096 ::std::u16string_view const rRedirectURL)
1097 {
1098 osl::Guard< osl::Mutex > aGuard( m_aMutex );
1099
1100 CurlUri const aUri( rRedirectURL );
1101
1102 // Check for maximum number of redirections
1103 // according to <https://tools.ietf.org/html/rfc7231#section-6.4>.
1104 // A practical limit may be 5, due to earlier specifications:
1105 // <https://tools.ietf.org/html/rfc2068#section-10.3>
1106 // it can be raised keeping in mind the added net activity.
1107 if( g_nRedirectLimit <= m_aRedirectURIs.size() )
1108 return true;
1109
1110 // try to detect a cyclical redirection
1111 return std::any_of(m_aRedirectURIs.begin(), m_aRedirectURIs.end(),
1112 [&aUri](const CurlUri& rUri) { return aUri == rUri; });
1113 }
1114
1115
1116 void DAVResourceAccess::resetUri()
1117 {
1118 osl::Guard< osl::Mutex > aGuard( m_aMutex );
1119 if ( ! m_aRedirectURIs.empty() )
1120 {
1121 OUString sURI = m_aRedirectURIs.front().GetURI();
1122 m_aRedirectURIs.clear();
1123 setURL(sURI);
1124 initialize();
1125 }
1126 }
1127
1128
1129 bool DAVResourceAccess::handleException(DAVException const& e, int const errorCount)
1130 {
1131 switch ( e.getError() )
1132 {
1133 case DAVException::DAV_HTTP_REDIRECT:
1134 if ( !detectRedirectCycle( e.getData() ) )
1135 {
1136 // set new URL and path.
1137 setURL( e.getData() );
1138 initialize();
1139 return true;
1140 }
1141 return false;
1142 // i#67048 copy & paste images doesn't display. This bug refers
1143 // to an old OOo problem about getting resources from sites with a bad connection.
1144 // If we have a bad connection try again. Up to three times.
1145 case DAVException::DAV_HTTP_ERROR:
1146 // retry up to three times, if not a client-side error (4xx error codes)
1147 if ( e.getStatus() < SC_BAD_REQUEST && errorCount < 3 )
1148 return true;
1149 // check the server side errors
1150 switch( e.getStatus() )
1151 {
1152 // the HTTP server side response status codes that can be retried
1153 // [Serf TODO? i#119036] case SC_REQUEST_ENTITY_TOO_LARGE:
1154 case SC_BAD_GATEWAY: // retry, can be an excessive load
1155 case SC_GATEWAY_TIMEOUT: // retry, may be we get lucky
1156 case SC_SERVICE_UNAVAILABLE: // retry, the service may become available
1157 case SC_INSUFFICIENT_STORAGE: // space may be freed, retry
1158 {
1159 if ( errorCount < 3 )
1160 return true;
1161 else
1162 return false;
1163 }
1164 break;
1165 // all the other HTTP server response status codes are NOT retry
1166 default:
1167 return false;
1168 }
1169 break;
1170 // if connection has said retry then retry!
1171 case DAVException::DAV_HTTP_RETRY:
1172 return true;
1173 // <--
1174 default:
1175 return false; // Abort
1176 }
1177 }
1178
1179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */