1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
11 #include "DAVTypes.hxx"
13 #include "CurlUri.hxx"
14 #include "../inc/urihelper.hxx"
19 using namespace http_dav_ucp
;
20 using namespace com::sun::star
;
22 // DAVOptions implementation
24 DAVOptions::DAVOptions() :
28 m_isHeadAllowed( true ),
32 m_nRequestedTimeLife( 0 ),
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
;
77 bool DAVOptions::operator==( const DAVOptions
& rOpts
) const
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() )
118 // check if the capabilities are stale, before restoring
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
);
126 // return false instead
128 rDAVOptions
= (*it
).second
;
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
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
196 osl_getSystemTime( &t1
);
197 if( (*it
).second
.getStaleTime() < t1
.Seconds
)
199 m_aTheCache
.erase( it
);
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: */