LibreOffice Module ucb (master) 1
DAVTypes.cxx
Go to the documentation of this file.
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
10
11#include "DAVTypes.hxx"
12
13#include "CurlUri.hxx"
14#include "../inc/urihelper.hxx"
15
16#include <osl/time.h>
17
18
19using namespace http_dav_ucp;
20using namespace com::sun::star;
21
22// DAVOptions implementation
23
24DAVOptions::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()
37{
38}
39
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 )
53{
54}
55
57{
58}
59
61{
62 m_isClass1 = rOpts.m_isClass1;
63 m_isClass2 = rOpts.m_isClass2;
64 m_isClass3 = rOpts.m_isClass3;
65 m_isLocked = rOpts.m_isLocked;
70 m_sURL = rOpts.m_sURL;
74 return *this;
75}
76
77bool DAVOptions::operator==( const DAVOptions& rOpts ) const
78{
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 &&
86 m_nStaleTime == rOpts.m_nStaleTime &&
88 m_sURL == rOpts.m_sURL &&
92}
93
94
95// DAVOptionsCache implementation
96
98{
99}
100
102{
103}
104
105bool DAVOptionsCache::getDAVOptions( const OUString & rURL, DAVOptions & rDAVOptions )
106{
107 std::unique_lock aGuard( m_aMutex );
108 OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( DecodeURI(rURL) ) );
109 normalizeURLLastChar( aEncodedUrl );
110
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
117 {
118 // check if the capabilities are stale, before restoring
119 TimeValue t1;
120 osl_getSystemTime( &t1 );
121 if ( (*it).second.getStaleTime() < t1.Seconds )
122 {
123 // if stale, remove from cache, do not restore
124 m_aTheCache.erase( it );
125 return false;
126 // return false instead
127 }
128 rDAVOptions = (*it).second;
129 return true;
130 }
131}
132
133void DAVOptionsCache::removeDAVOptions( const OUString & rURL )
134{
135 std::unique_lock aGuard( m_aMutex );
136 OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( DecodeURI(rURL) ) );
137 normalizeURLLastChar( aEncodedUrl );
138
139 DAVOptionsMap::iterator it;
140 it = m_aTheCache.find( aEncodedUrl );
141 if ( it != m_aTheCache.end() )
142 {
143 m_aTheCache.erase( it );
144 }
145}
146
147void DAVOptionsCache::addDAVOptions( DAVOptions & rDAVOptions, const sal_uInt32 nLifeTime )
148{
149 std::unique_lock aGuard( m_aMutex );
150 OUString aURL( rDAVOptions.getURL() );
151
152 OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( DecodeURI(aURL) ) );
153 normalizeURLLastChar( aEncodedUrl );
154 rDAVOptions.setURL( aEncodedUrl );
155
156// unchanged, it may be used to access a server
157 OUString aRedirURL( rDAVOptions.getRedirectedURL() );
158 rDAVOptions.setRedirectedURL( aRedirURL );
159
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
167
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())
172 {
173 rDAVOptions.setClass1( (*it).second.isClass1() );
174 }
175 }
176 // not in cache, add it
177 TimeValue t1;
178 osl_getSystemTime( &t1 );
179 rDAVOptions.setStaleTime( t1.Seconds + nLifeTime );
180
181 m_aTheCache[ aEncodedUrl ] = rDAVOptions;
182}
183
184void DAVOptionsCache::setHeadAllowed( const OUString & rURL, const bool HeadAllowed )
185{
186 std::unique_lock aGuard( m_aMutex );
187 OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( DecodeURI(rURL) ) );
188 normalizeURLLastChar( aEncodedUrl );
189
190 DAVOptionsMap::iterator it;
191 it = m_aTheCache.find( aEncodedUrl );
192 if ( it != m_aTheCache.end() )
193 {
194 // first check for stale
195 TimeValue t1;
196 osl_getSystemTime( &t1 );
197 if( (*it).second.getStaleTime() < t1.Seconds )
198 {
199 m_aTheCache.erase( it );
200 return;
201 }
202 // check if the resource was present on server
203 (*it).second.setHeadAllowed( HeadAllowed );
204 }
205}
206
207/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
bool getDAVOptions(const OUString &rURL, DAVOptions &rDAVOptions)
Definition: DAVTypes.cxx:105
void setHeadAllowed(const OUString &rURL, bool HeadAllowed=true)
Definition: DAVTypes.cxx:184
static void normalizeURLLastChar(OUString &aUrl)
remove the last '/' in aUrl, if it exists
Definition: DAVTypes.hxx:176
void removeDAVOptions(const OUString &rURL)
Definition: DAVTypes.cxx:133
void addDAVOptions(DAVOptions &rDAVOptions, const sal_uInt32 nLifeTime)
Definition: DAVTypes.cxx:147
bool operator==(const DAVOptions &rOpts) const
Definition: DAVTypes.cxx:77
bool m_isHeadAllowed
for server that do not implement it
Definition: DAVTypes.hxx:72
const OUString & getURL() const
Definition: DAVTypes.hxx:114
sal_uInt16 m_nHttpResponseStatusCode
The cached HTT response status code. It's 0 if the code was dealt with and there is no need to cache ...
Definition: DAVTypes.hxx:85
sal_uInt32 m_nRequestedTimeLife
Definition: DAVTypes.hxx:80
DAVOptions & operator=(const DAVOptions &rOpts)
Definition: DAVTypes.cxx:60
sal_uInt32 m_nStaleTime
target time when this capability becomes stale
Definition: DAVTypes.hxx:79
OUString m_sHttpResponseStatusText
The cached string with the server returned HTTP response status code string, corresponds to m_nHttpRe...
Definition: DAVTypes.hxx:87
void setStaleTime(const sal_uInt32 nStaleTime)
Definition: DAVTypes.hxx:109
const OUString & getRedirectedURL() const
Definition: DAVTypes.hxx:117
OUString m_aAllowedMethods
contains the methods allowed on this resource
Definition: DAVTypes.hxx:76
void setURL(const OUString &sURL)
Definition: DAVTypes.hxx:115
void setClass1(bool Class1=true)
Definition: DAVTypes.hxx:97
void setRedirectedURL(const OUString &sRedirectedURL)
Definition: DAVTypes.hxx:118
bool m_isLocked
Internally used to maintain the locked state of the resource, only if it's a Class 2 resource.
Definition: DAVTypes.hxx:74
OUString m_sURL
URL aURL
OUString DecodeURI(OUString const &rURI)
Definition: CurlUri.cxx:302
OUString encodeURI(const OUString &rURI)
Definition: urihelper.hxx:44