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>
29 namespace http_dav_ucp
32 SerfLockReqProcImpl::SerfLockReqProcImpl( const char* inPath
,
33 const DAVRequestHeaders
& inRequestHeaders
,
34 SerfSession
& rSession
,
35 const css::ucb::Lock
& rLock
,
36 sal_Int32
* plastChanceToSendRefreshRequest
)
37 : SerfRequestProcessorImpl( inPath
, inRequestHeaders
)
38 , m_rSession( rSession
)
40 , m_plastChanceToSendRefreshRequest( plastChanceToSendRefreshRequest
)
41 , m_xInputStream( new SerfInputStream() )
45 SerfLockReqProcImpl::~SerfLockReqProcImpl()
49 serf_bucket_t
* SerfLockReqProcImpl::createSerfRequestBucket( serf_request_t
* inSerfRequest
)
51 serf_bucket_alloc_t
* pSerfBucketAlloc
= serf_request_get_alloc( inSerfRequest
);
53 OStringBuffer
aBody("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
54 "<lockinfo xmlns='DAV:'>\n <lockscope>");
57 switch ( m_aLock
.Scope
)
59 case css::ucb::LockScope_EXCLUSIVE
:
60 aBody
.append("<exclusive/>");
62 case css::ucb::LockScope_SHARED
:
63 aBody
.append("<shared/>");
66 throw DAVException( DAVException::DAV_INVALID_ARG
);
68 aBody
.append("</lockscope>\n <locktype><write/></locktype>\n");
72 if ((m_aLock
.Owner
>>= aValue
) && !aValue
.isEmpty())
74 aBody
.append(" <owner>");
75 aBody
.append(OUStringToOString(aValue
, RTL_TEXTENCODING_UTF8
));
76 aBody
.append("</owner>\n");
78 aBody
.append("</lockinfo>\n");
80 const OString
aBodyText(aBody
.makeStringAndClear());
81 serf_bucket_t
* body_bkt
= 0;
83 if (!m_plastChanceToSendRefreshRequest
)
84 body_bkt
= serf_bucket_simple_copy_create( aBodyText
.getStr(),
85 aBodyText
.getLength(),
88 // create serf request
89 serf_bucket_t
*req_bkt
= serf_request_bucket_request_create( inSerfRequest
,
94 if (!m_plastChanceToSendRefreshRequest
)
95 handleChunkedEncoding(req_bkt
, aBodyText
.getLength());
97 // set request header fields
98 serf_bucket_t
* hdrs_bkt
= serf_bucket_request_get_headers( req_bkt
);
100 // general header fields provided by caller
101 setRequestHeaders( hdrs_bkt
);
103 // request specific header fields
104 const char * depth
= 0;
105 switch( m_aLock
.Depth
)
107 case css::ucb::LockDepth_ZERO
:
110 case css::ucb::LockDepth_ONE
:
113 case css::ucb::LockDepth_INFINITY
:
117 throw DAVException( DAVException::DAV_INVALID_ARG
);
119 if (!m_plastChanceToSendRefreshRequest
)
121 serf_bucket_headers_set( hdrs_bkt
, "Depth", depth
);
122 serf_bucket_headers_set( hdrs_bkt
, "Content-Type", "application/xml" );
126 const OString
sToken( "(<" + OUStringToOString( apr_environment::AprEnv::getAprEnv()->
127 getSerfLockStore()->getLockToken( OUString::createFromAscii(getPathStr())),
128 RTL_TEXTENCODING_UTF8
) + ">)" );
129 serf_bucket_headers_set( hdrs_bkt
, "If", sToken
.getStr() );
132 // Set the lock timeout
133 if (m_aLock
.Timeout
== -1)
134 serf_bucket_headers_set( hdrs_bkt
, "Timeout", "Infinite" );
135 else if (m_aLock
.Timeout
> 0)
137 const OString
aTimeValue("Second-" + OString::number(m_aLock
.Timeout
));
138 serf_bucket_headers_set( hdrs_bkt
, "Timeout", aTimeValue
.getStr() );
141 serf_bucket_headers_set( hdrs_bkt
, "Timeout", "Second-180" );
143 osl_getSystemTime( &m_aStartCall
);
148 void SerfLockReqProcImpl::processChunkOfResponseData( const char* data
,
151 if ( m_xInputStream
.is() )
153 m_xInputStream
->AddToStream( data
, len
);
157 void SerfLockReqProcImpl::handleEndOfResponseData( serf_bucket_t
* )
159 const std::vector
< css::ucb::Lock
> aLocks( parseWebDAVLockResponse( m_xInputStream
.get() ) );
163 for (size_t i
= 0; i
< aLocks
.size(); ++i
)
165 sal_Int64 timeout
= aLocks
[i
].Timeout
;
167 osl_getSystemTime( &aEnd
);
168 // Try to estimate a safe absolute time for sending the
169 // lock refresh request.
170 sal_Int32 lastChanceToSendRefreshRequest
= -1;
173 sal_Int32 calltime
= aEnd
.Seconds
- m_aStartCall
.Seconds
;
174 if ( calltime
<= timeout
)
175 lastChanceToSendRefreshRequest
= aEnd
.Seconds
+ timeout
- calltime
;
177 SAL_WARN("ucb.ucp.webdav", "No chance to refresh lock before timeout!" );
179 if (m_plastChanceToSendRefreshRequest
)
181 *m_plastChanceToSendRefreshRequest
= lastChanceToSendRefreshRequest
;
182 assert(aLocks
.size() == 1);
183 // We are just refreshing lock, do not add it into SerfLockStore
186 apr_environment::AprEnv::getAprEnv()->getSerfLockStore()->addLock(
187 OUString::createFromAscii(getPathStr()),
188 aLocks
[i
].LockTokens
[0],
189 &m_rSession
, lastChanceToSendRefreshRequest
);
190 SAL_INFO("ucb.ucp.webdav", "SerfSession::LOCK: created lock for "
191 << getPathStr() << ". token: " << aLocks
[i
].LockTokens
[0]);
196 SAL_INFO("ucb.ucp.webdav", "SerfSession::LOCK: obtaining lock failed!");
200 } // namespace http_dav_ucp
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */