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/.
13 #include "DAVTypes.hxx"
14 #include "../inc/urihelper.hxx"
15 #include "NeonUri.hxx"
17 using namespace webdav_ucp
;
18 using namespace com::sun::star
;
20 // DAVOptions implementation
22 DAVOptions::DAVOptions() :
26 m_isHeadAllowed( true ),
30 m_nRequestedTimeLife( 0 ),
33 m_nHttpResponseStatusCode( 0 ),
34 m_sHttpResponseStatusText()
38 DAVOptions::DAVOptions( const DAVOptions
& rOther
) :
39 m_isClass1( rOther
.m_isClass1
),
40 m_isClass2( rOther
.m_isClass2
),
41 m_isClass3( rOther
.m_isClass3
),
42 m_isHeadAllowed( rOther
.m_isHeadAllowed
),
43 m_isLocked( rOther
.m_isLocked
),
44 m_aAllowedMethods( rOther
.m_aAllowedMethods
),
45 m_nStaleTime( rOther
.m_nStaleTime
),
46 m_nRequestedTimeLife( rOther
.m_nRequestedTimeLife
),
47 m_sURL( rOther
.m_sURL
),
48 m_sRedirectedURL( rOther
.m_sRedirectedURL
),
49 m_nHttpResponseStatusCode( rOther
.m_nHttpResponseStatusCode
),
50 m_sHttpResponseStatusText( rOther
.m_sHttpResponseStatusText
)
54 DAVOptions::~DAVOptions()
58 DAVOptions
& DAVOptions::operator=( const DAVOptions
& rOpts
)
60 m_isClass1
= rOpts
.m_isClass1
;
61 m_isClass2
= rOpts
.m_isClass2
;
62 m_isClass3
= rOpts
.m_isClass3
;
63 m_isLocked
= rOpts
.m_isLocked
;
64 m_isHeadAllowed
= rOpts
.m_isHeadAllowed
;
65 m_aAllowedMethods
= rOpts
.m_aAllowedMethods
;
66 m_nStaleTime
= rOpts
.m_nStaleTime
;
67 m_nRequestedTimeLife
= rOpts
.m_nRequestedTimeLife
;
68 m_sURL
= rOpts
.m_sURL
;
69 m_sRedirectedURL
= rOpts
.m_sRedirectedURL
;
70 m_nHttpResponseStatusCode
= rOpts
.m_nHttpResponseStatusCode
;
71 m_sHttpResponseStatusText
= rOpts
.m_sHttpResponseStatusText
;
75 bool DAVOptions::operator==( const DAVOptions
& rOpts
) const
78 m_isClass1
== rOpts
.m_isClass1
&&
79 m_isClass2
== rOpts
.m_isClass2
&&
80 m_isClass3
== rOpts
.m_isClass3
&&
81 m_isLocked
== rOpts
.m_isLocked
&&
82 m_isHeadAllowed
== rOpts
.m_isHeadAllowed
&&
83 m_aAllowedMethods
== rOpts
.m_aAllowedMethods
&&
84 m_nStaleTime
== rOpts
.m_nStaleTime
&&
85 m_nRequestedTimeLife
== rOpts
.m_nRequestedTimeLife
&&
86 m_sURL
== rOpts
.m_sURL
&&
87 m_sRedirectedURL
== rOpts
.m_sRedirectedURL
&&
88 m_nHttpResponseStatusCode
== rOpts
.m_nHttpResponseStatusCode
&&
89 m_sHttpResponseStatusText
== rOpts
.m_sHttpResponseStatusText
;
93 // DAVOptionsCache implementation
95 DAVOptionsCache::DAVOptionsCache()
99 DAVOptionsCache::~DAVOptionsCache()
103 bool DAVOptionsCache::getDAVOptions( const OUString
& rURL
, DAVOptions
& rDAVOptions
)
105 osl::MutexGuard
aGuard( m_aMutex
);
106 OUString
aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( rURL
) ) );
107 normalizeURLLastChar( aEncodedUrl
);
109 // search the URL in the static map
110 DAVOptionsMap::iterator it
= m_aTheCache
.find( aEncodedUrl
);
111 if ( it
== m_aTheCache
.end() )
115 // check if the capabilities are stale, before restoring
117 osl_getSystemTime( &t1
);
118 if ( (*it
).second
.getStaleTime() < t1
.Seconds
)
120 // if stale, remove from cache, do not restore
121 m_aTheCache
.erase( it
);
123 // return false instead
125 rDAVOptions
= (*it
).second
;
130 void DAVOptionsCache::removeDAVOptions( const OUString
& rURL
)
132 osl::MutexGuard
aGuard( m_aMutex
);
133 OUString
aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( rURL
) ) );
134 normalizeURLLastChar( aEncodedUrl
);
136 DAVOptionsMap::iterator it
= m_aTheCache
.find( aEncodedUrl
);
137 if ( it
!= m_aTheCache
.end() )
139 m_aTheCache
.erase( it
);
143 void DAVOptionsCache::addDAVOptions( DAVOptions
& rDAVOptions
, const sal_uInt32 nLifeTime
)
145 osl::MutexGuard
aGuard( m_aMutex
);
146 OUString
aURL( rDAVOptions
.getURL() );
148 OUString
aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( aURL
) ) );
149 normalizeURLLastChar( aEncodedUrl
);
150 rDAVOptions
.setURL( aEncodedUrl
);
152 // unchanged, it may be used to access a server
153 OUString
aRedirURL( rDAVOptions
.getRedirectedURL() );
154 rDAVOptions
.setRedirectedURL( aRedirURL
);
156 // check if already cached
157 DAVOptionsMap::iterator it
= m_aTheCache
.find( aEncodedUrl
);
158 if ( it
!= m_aTheCache
.end() )
159 { // already in cache, check LifeTime
160 if ( (*it
).second
.getRequestedTimeLife() == nLifeTime
)
161 return; // same lifetime, do nothing
163 // not in cache, add it
165 osl_getSystemTime( &t1
);
166 rDAVOptions
.setStaleTime( t1
.Seconds
+ nLifeTime
);
168 m_aTheCache
[ aEncodedUrl
] = rDAVOptions
;
171 void DAVOptionsCache::setHeadAllowed( const OUString
& rURL
, const bool HeadAllowed
)
173 osl::MutexGuard
aGuard( m_aMutex
);
174 OUString
aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( rURL
) ) );
175 normalizeURLLastChar( aEncodedUrl
);
177 DAVOptionsMap::iterator it
= m_aTheCache
.find( aEncodedUrl
);
178 if ( it
== m_aTheCache
.end() )
181 // first check for stale
183 osl_getSystemTime( &t1
);
184 if( (*it
).second
.getStaleTime() < t1
.Seconds
)
186 m_aTheCache
.erase( it
);
189 // check if the resource was present on server
190 (*it
).second
.setHeadAllowed( HeadAllowed
);
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */