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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "SerfLockReqProcImpl.hxx"
23 #include "SerfSession.hxx"
24 #include "DAVException.hxx"
26 #include "webdavresponseparser.hxx"
27 #include <rtl/strbuf.hxx>
28 #include <sal/log.hxx>
30 namespace http_dav_ucp
33 SerfLockReqProcImpl::SerfLockReqProcImpl( const char* inPath
,
34 const DAVRequestHeaders
& inRequestHeaders
,
35 SerfSession
& rSession
,
36 const css::ucb::Lock
& rLock
,
37 sal_Int32
* plastChanceToSendRefreshRequest
)
38 : SerfRequestProcessorImpl( inPath
, inRequestHeaders
)
39 , m_rSession( rSession
)
41 , m_plastChanceToSendRefreshRequest( plastChanceToSendRefreshRequest
)
42 , m_xInputStream( new SerfInputStream() )
46 SerfLockReqProcImpl::~SerfLockReqProcImpl()
50 serf_bucket_t
* SerfLockReqProcImpl::createSerfRequestBucket( serf_request_t
* inSerfRequest
)
52 serf_bucket_alloc_t
* pSerfBucketAlloc
= serf_request_get_alloc( inSerfRequest
);
54 OStringBuffer
aBody("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
55 "<lockinfo xmlns='DAV:'>\n <lockscope>");
58 switch ( m_aLock
.Scope
)
60 case css::ucb::LockScope_EXCLUSIVE
:
61 aBody
.append("<exclusive/>");
63 case css::ucb::LockScope_SHARED
:
64 aBody
.append("<shared/>");
67 throw DAVException( DAVException::DAV_INVALID_ARG
);
69 aBody
.append("</lockscope>\n <locktype><write/></locktype>\n");
73 if ((m_aLock
.Owner
>>= aValue
) && !aValue
.isEmpty())
75 aBody
.append(" <owner>");
76 aBody
.append(OUStringToOString(aValue
, RTL_TEXTENCODING_UTF8
));
77 aBody
.append("</owner>\n");
79 aBody
.append("</lockinfo>\n");
81 const OString
aBodyText(aBody
.makeStringAndClear());
82 serf_bucket_t
* body_bkt
= nullptr;
84 if (!m_plastChanceToSendRefreshRequest
)
85 body_bkt
= serf_bucket_simple_copy_create( aBodyText
.getStr(),
86 aBodyText
.getLength(),
89 // create serf request
90 serf_bucket_t
*req_bkt
= serf_request_bucket_request_create( inSerfRequest
,
95 if (!m_plastChanceToSendRefreshRequest
)
96 handleChunkedEncoding(req_bkt
, aBodyText
.getLength());
98 // set request header fields
99 serf_bucket_t
* hdrs_bkt
= serf_bucket_request_get_headers( req_bkt
);
101 // general header fields provided by caller
102 setRequestHeaders( hdrs_bkt
);
104 // request specific header fields
105 const char * depth
= nullptr;
106 switch( m_aLock
.Depth
)
108 case css::ucb::LockDepth_ZERO
:
111 case css::ucb::LockDepth_ONE
:
114 case css::ucb::LockDepth_INFINITY
:
118 throw DAVException( DAVException::DAV_INVALID_ARG
);
120 if (!m_plastChanceToSendRefreshRequest
)
122 serf_bucket_headers_set( hdrs_bkt
, "Depth", depth
);
123 serf_bucket_headers_set( hdrs_bkt
, "Content-Type", "application/xml" );
127 const OString
sToken( "(<" + OUStringToOString( apr_environment::AprEnv::getAprEnv()->
128 getSerfLockStore()->getLockToken( OUString::createFromAscii(getPathStr())),
129 RTL_TEXTENCODING_UTF8
) + ">)" );
130 serf_bucket_headers_set( hdrs_bkt
, "If", sToken
.getStr() );
133 // Set the lock timeout
134 if (m_aLock
.Timeout
== -1)
135 serf_bucket_headers_set( hdrs_bkt
, "Timeout", "Infinite" );
136 else if (m_aLock
.Timeout
> 0)
138 const OString
aTimeValue("Second-" + OString::number(m_aLock
.Timeout
));
139 serf_bucket_headers_set( hdrs_bkt
, "Timeout", aTimeValue
.getStr() );
142 serf_bucket_headers_set( hdrs_bkt
, "Timeout", "Second-180" );
144 osl_getSystemTime( &m_aStartCall
);
149 void SerfLockReqProcImpl::processChunkOfResponseData( const char* data
,
152 if ( m_xInputStream
.is() )
154 m_xInputStream
->AddToStream( data
, len
);
158 void SerfLockReqProcImpl::handleEndOfResponseData( serf_bucket_t
* )
160 const std::vector
< css::ucb::Lock
> aLocks( parseWebDAVLockResponse( m_xInputStream
.get() ) );
164 for (size_t i
= 0; i
< aLocks
.size(); ++i
)
166 sal_Int64 timeout
= aLocks
[i
].Timeout
;
168 osl_getSystemTime( &aEnd
);
169 // Try to estimate a safe absolute time for sending the
170 // lock refresh request.
171 sal_Int32 lastChanceToSendRefreshRequest
= -1;
174 sal_Int32 calltime
= aEnd
.Seconds
- m_aStartCall
.Seconds
;
175 if ( calltime
<= timeout
)
176 lastChanceToSendRefreshRequest
= aEnd
.Seconds
+ timeout
- calltime
;
178 SAL_WARN("ucb.ucp.webdav", "No chance to refresh lock before timeout!" );
180 if (m_plastChanceToSendRefreshRequest
)
182 *m_plastChanceToSendRefreshRequest
= lastChanceToSendRefreshRequest
;
183 assert(aLocks
.size() == 1);
184 // We are just refreshing lock, do not add it into SerfLockStore
187 apr_environment::AprEnv::getAprEnv()->getSerfLockStore()->addLock(
188 OUString::createFromAscii(getPathStr()),
189 aLocks
[i
].LockTokens
[0],
190 &m_rSession
, lastChanceToSendRefreshRequest
);
191 SAL_INFO("ucb.ucp.webdav", "SerfSession::LOCK: created lock for "
192 << getPathStr() << ". token: " << aLocks
[i
].LockTokens
[0]);
197 SAL_INFO("ucb.ucp.webdav", "SerfSession::LOCK: obtaining lock failed!");
201 } // namespace http_dav_ucp
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */