Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / ucb / source / ucp / webdav-curl / DAVTypes.cxx
blobe41c5426e959ecb80fcc0a31a2ccaf726c34961d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
11 #include "DAVTypes.hxx"
13 #include "CurlUri.hxx"
14 #include "../inc/urihelper.hxx"
16 #include <osl/time.h>
19 using namespace http_dav_ucp;
20 using namespace com::sun::star;
22 // DAVOptions implementation
24 DAVOptions::DAVOptions() :
25 m_isClass1( false ),
26 m_isClass2( false ),
27 m_isClass3( false ),
28 m_isHeadAllowed( true ),
29 m_isLocked( false ),
30 m_aAllowedMethods(),
31 m_nStaleTime( 0 ),
32 m_nRequestedTimeLife( 0 ),
33 m_sURL(),
34 m_sRedirectedURL(),
35 m_nHttpResponseStatusCode( 0 ),
36 m_sHttpResponseStatusText()
40 DAVOptions::DAVOptions( const DAVOptions & rOther ) :
41 m_isClass1( rOther.m_isClass1 ),
42 m_isClass2( rOther.m_isClass2 ),
43 m_isClass3( rOther.m_isClass3 ),
44 m_isHeadAllowed( rOther.m_isHeadAllowed ),
45 m_isLocked( rOther.m_isLocked ),
46 m_aAllowedMethods( rOther.m_aAllowedMethods ),
47 m_nStaleTime( rOther.m_nStaleTime ),
48 m_nRequestedTimeLife( rOther.m_nRequestedTimeLife ),
49 m_sURL( rOther.m_sURL ),
50 m_sRedirectedURL( rOther.m_sRedirectedURL),
51 m_nHttpResponseStatusCode( rOther.m_nHttpResponseStatusCode ),
52 m_sHttpResponseStatusText( rOther.m_sHttpResponseStatusText )
56 DAVOptions::~DAVOptions()
60 DAVOptions & DAVOptions::operator=( const DAVOptions& rOpts )
62 m_isClass1 = rOpts.m_isClass1;
63 m_isClass2 = rOpts.m_isClass2;
64 m_isClass3 = rOpts.m_isClass3;
65 m_isLocked = rOpts.m_isLocked;
66 m_isHeadAllowed = rOpts.m_isHeadAllowed;
67 m_aAllowedMethods = rOpts.m_aAllowedMethods;
68 m_nStaleTime = rOpts.m_nStaleTime;
69 m_nRequestedTimeLife = rOpts.m_nRequestedTimeLife;
70 m_sURL = rOpts.m_sURL;
71 m_sRedirectedURL = rOpts.m_sRedirectedURL;
72 m_nHttpResponseStatusCode = rOpts.m_nHttpResponseStatusCode;
73 m_sHttpResponseStatusText = rOpts.m_sHttpResponseStatusText;
74 return *this;
77 bool DAVOptions::operator==( const DAVOptions& rOpts ) const
79 return
80 m_isClass1 == rOpts.m_isClass1 &&
81 m_isClass2 == rOpts.m_isClass2 &&
82 m_isClass3 == rOpts.m_isClass3 &&
83 m_isLocked == rOpts.m_isLocked &&
84 m_isHeadAllowed == rOpts.m_isHeadAllowed &&
85 m_aAllowedMethods == rOpts.m_aAllowedMethods &&
86 m_nStaleTime == rOpts.m_nStaleTime &&
87 m_nRequestedTimeLife == rOpts.m_nRequestedTimeLife &&
88 m_sURL == rOpts.m_sURL &&
89 m_sRedirectedURL == rOpts.m_sRedirectedURL &&
90 m_nHttpResponseStatusCode == rOpts.m_nHttpResponseStatusCode &&
91 m_sHttpResponseStatusText == rOpts.m_sHttpResponseStatusText;
95 // DAVOptionsCache implementation
97 DAVOptionsCache::DAVOptionsCache()
101 DAVOptionsCache::~DAVOptionsCache()
105 bool DAVOptionsCache::getDAVOptions( const OUString & rURL, DAVOptions & rDAVOptions )
107 std::unique_lock aGuard( m_aMutex );
108 OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( DecodeURI(rURL) ) );
109 normalizeURLLastChar( aEncodedUrl );
111 // search the URL in the static map
112 DAVOptionsMap::iterator it;
113 it = m_aTheCache.find( aEncodedUrl );
114 if ( it == m_aTheCache.end() )
115 return false;
116 else
118 // check if the capabilities are stale, before restoring
119 TimeValue t1;
120 osl_getSystemTime( &t1 );
121 if ( (*it).second.getStaleTime() < t1.Seconds )
123 // if stale, remove from cache, do not restore
124 m_aTheCache.erase( it );
125 return false;
126 // return false instead
128 rDAVOptions = (*it).second;
129 return true;
133 void DAVOptionsCache::removeDAVOptions( const OUString & rURL )
135 std::unique_lock aGuard( m_aMutex );
136 OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( DecodeURI(rURL) ) );
137 normalizeURLLastChar( aEncodedUrl );
139 DAVOptionsMap::iterator it;
140 it = m_aTheCache.find( aEncodedUrl );
141 if ( it != m_aTheCache.end() )
143 m_aTheCache.erase( it );
147 void DAVOptionsCache::addDAVOptions( DAVOptions & rDAVOptions, const sal_uInt32 nLifeTime )
149 std::unique_lock aGuard( m_aMutex );
150 OUString aURL( rDAVOptions.getURL() );
152 OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( DecodeURI(aURL) ) );
153 normalizeURLLastChar( aEncodedUrl );
154 rDAVOptions.setURL( aEncodedUrl );
156 // unchanged, it may be used to access a server
157 OUString aRedirURL( rDAVOptions.getRedirectedURL() );
158 rDAVOptions.setRedirectedURL( aRedirURL );
160 // check if already cached
161 DAVOptionsMap::iterator it;
162 it = m_aTheCache.find( aEncodedUrl );
163 if ( it != m_aTheCache.end() )
164 { // already in cache, check LifeTime
165 if ( (*it).second.getRequestedTimeLife() == nLifeTime )
166 return; // same lifetime, do nothing
168 // tdf#153642 keep cached Class1 bit at aDAVOptionsException to avoid of
169 // losing the ability to resave the document within the lifetime because
170 // of disabled DAV detection in getResourceType()
171 if ((*it).second.isClass1())
173 rDAVOptions.setClass1( (*it).second.isClass1() );
176 // not in cache, add it
177 TimeValue t1;
178 osl_getSystemTime( &t1 );
179 rDAVOptions.setStaleTime( t1.Seconds + nLifeTime );
181 m_aTheCache[ aEncodedUrl ] = rDAVOptions;
184 void DAVOptionsCache::setHeadAllowed( const OUString & rURL, const bool HeadAllowed )
186 std::unique_lock aGuard( m_aMutex );
187 OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( DecodeURI(rURL) ) );
188 normalizeURLLastChar( aEncodedUrl );
190 DAVOptionsMap::iterator it;
191 it = m_aTheCache.find( aEncodedUrl );
192 if ( it != m_aTheCache.end() )
194 // first check for stale
195 TimeValue t1;
196 osl_getSystemTime( &t1 );
197 if( (*it).second.getStaleTime() < t1.Seconds )
199 m_aTheCache.erase( it );
200 return;
202 // check if the resource was present on server
203 (*it).second.setHeadAllowed( HeadAllowed );
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */