1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_ucb.hxx"
24 
25 #include "SerfTypes.hxx"
26 #include "SerfLockReqProcImpl.hxx"
27 #include "DAVProperties.hxx"
28 
29 #include "webdavresponseparser.hxx"
30 #include <serf/serf.h>
31 #include <rtl/ustrbuf.hxx>
32 #include <apr/apr_strings.h>
33 
34 namespace http_dav_ucp
35 {
36 
37 SerfLockReqProcImpl::SerfLockReqProcImpl( const char* inSourcePath,
38                                           const DAVRequestHeaders& inRequestHeaders, // on debug the header look empty
39                                           const ucb::Lock& inLock,
40                                           const char* inTimeout,
41                                           DAVPropertyValue & outLock)
42     : SerfRequestProcessorImpl( inSourcePath, inRequestHeaders )
43     , mLock( inLock )
44     , mTimeout(inTimeout)
45     , mLockObtained( &outLock )
46     , xInputStream( new SerfInputStream() )
47 {
48     switch ( inLock.Depth )
49     {
50         //i126305 TODO investigate on this case...
51     case ucb::LockDepth_MAKE_FIXED_SIZE:
52 
53     case ucb::LockDepth_ZERO:
54         mDepthStr = "0";
55         break;
56     case ucb::LockDepth_ONE:
57             mDepthStr = "1";
58             break;
59     case ucb::LockDepth_INFINITY:
60             mDepthStr = "infinity";
61             break;
62     }
63 
64     switch ( inLock.Scope )
65     {
66         //i126305 TODO investigate on this case...
67     case ucb::LockScope_MAKE_FIXED_SIZE:
68 
69     case ucb::LockScope_EXCLUSIVE:
70         mLockScope = "<lockscope><exclusive/></lockscope>";
71         break;
72     case ucb::LockScope_SHARED:
73         mLockScope = "<lockscope><shared/></lockscope>";
74         break;
75     }
76 }
77 
78 SerfLockReqProcImpl::~SerfLockReqProcImpl()
79 {
80 }
81 
82 #define LOCK_HEADER "<?xml version=\"1.0\" encoding=\"utf-8\"?><lockinfo xmlns=\"DAV:\">"
83 #define LOCK_TYPE "<locktype><write/></locktype>"
84 #define LOCK_TRAILER "</lockinfo>"
85 
86 serf_bucket_t * SerfLockReqProcImpl::createSerfRequestBucket( serf_request_t * inSerfRequest )
87 {
88   //prepare body of request:
89     serf_bucket_alloc_t* pSerfBucketAlloc = serf_request_get_alloc( inSerfRequest );
90     serf_bucket_t* body_bkt = 0;
91     rtl::OString aBodyText;
92     {
93        rtl::OUStringBuffer aBuffer;
94         aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( LOCK_HEADER ));
95         aBuffer.appendAscii( mLockScope );
96         aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( LOCK_TYPE ));
97         aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "<owner>" ));
98         rtl::OUString aStr;
99         mLock.Owner >>= aStr;
100         aBuffer.appendAscii( rtl::OUStringToOString( aStr, RTL_TEXTENCODING_UTF8 ).getStr() );
101         aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "</owner>" ));
102         aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( LOCK_TRAILER ));
103         aBodyText = rtl::OUStringToOString( aBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
104         body_bkt = serf_bucket_simple_copy_create( aBodyText.getStr(),
105                                                    aBodyText.getLength(),
106                                                    pSerfBucketAlloc );
107     }
108 
109     // create serf request
110     serf_bucket_t *req_bkt = serf_request_bucket_request_create( inSerfRequest,
111                                                                  "LOCK",
112                                                                  getPathStr(),
113                                                                  body_bkt,
114                                                                  pSerfBucketAlloc );
115     handleChunkedEncoding(req_bkt, aBodyText.getLength());
116 
117     // set request header fields
118     serf_bucket_t* hdrs_bkt = serf_bucket_request_get_headers( req_bkt );
119     if (hdrs_bkt != NULL)
120     {
121         // general header fields provided by caller
122         setRequestHeaders( hdrs_bkt );
123 
124         // request specific header fields
125         serf_bucket_headers_set( hdrs_bkt, "Timeout", mTimeout );
126         serf_bucket_headers_set( hdrs_bkt, "Depth", mDepthStr );
127         if (hdrs_bkt!=NULL && body_bkt != 0 && aBodyText.getLength() > 0 )
128         {
129             serf_bucket_headers_set( hdrs_bkt, "Content-Type", "application/xml" );
130         }
131     }
132     else
133     {
134         OSL_ASSERT("Headers Bucket missing");
135     }
136 
137     return req_bkt;
138 }
139 
140 void SerfLockReqProcImpl::processChunkOfResponseData( const char* data,
141                                                       apr_size_t len )
142 {
143     if ( xInputStream.is() )
144     {
145         xInputStream->AddToStream( data, len );
146     }
147 }
148 
149 void SerfLockReqProcImpl::handleEndOfResponseData( serf_bucket_t * /*inSerfResponseBucket*/ )
150 {
151     const DAVPropertyValue rLocksValue( parseWebDAVLockResponse( xInputStream.get() ) );
152     *mLockObtained = rLocksValue;
153 }
154 
155 } // namespace http_dav_ucp
156