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 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include "SerfPropFindReqProcImpl.hxx"
21 #include "DAVProperties.hxx"
22
23 #include "webdavresponseparser.hxx"
24 #include <rtl/strbuf.hxx>
25
26
27 using namespace com::sun::star;
28
29 namespace http_dav_ucp
30 {
31
SerfPropFindReqProcImpl(const char * inPath,const DAVRequestHeaders & inRequestHeaders,const Depth inDepth,const std::vector<OUString> & inPropNames,std::vector<DAVResource> & ioResources)32 SerfPropFindReqProcImpl::SerfPropFindReqProcImpl( const char* inPath,
33 const DAVRequestHeaders& inRequestHeaders,
34 const Depth inDepth,
35 const std::vector< OUString > & inPropNames,
36 std::vector< DAVResource > & ioResources )
37 : SerfRequestProcessorImpl( inPath, inRequestHeaders )
38 , mDepthStr( nullptr )
39 , mpPropNames( &inPropNames )
40 , mpResources( &ioResources )
41 , mpResInfo( nullptr )
42 , mbOnlyPropertyNames( false )
43 , xInputStream( new SerfInputStream() )
44 {
45 init( inDepth );
46 }
47
SerfPropFindReqProcImpl(const char * inPath,const DAVRequestHeaders & inRequestHeaders,const Depth inDepth,std::vector<DAVResourceInfo> & ioResInfo)48 SerfPropFindReqProcImpl::SerfPropFindReqProcImpl( const char* inPath,
49 const DAVRequestHeaders& inRequestHeaders,
50 const Depth inDepth,
51 std::vector< DAVResourceInfo > & ioResInfo )
52 : SerfRequestProcessorImpl( inPath, inRequestHeaders )
53 , mDepthStr( nullptr )
54 , mpPropNames( nullptr )
55 , mpResources( nullptr )
56 , mpResInfo( &ioResInfo )
57 , mbOnlyPropertyNames( true )
58 , xInputStream( new SerfInputStream() )
59 {
60 init( inDepth );
61 }
62
init(const Depth inDepth)63 void SerfPropFindReqProcImpl::init( const Depth inDepth )
64 {
65 switch ( inDepth )
66 {
67 case DAVZERO:
68 mDepthStr = "0";
69 break;
70 case DAVONE:
71 mDepthStr = "1";
72 break;
73 case DAVINFINITY:
74 mDepthStr = "infinity";
75 break;
76 }
77 }
78
~SerfPropFindReqProcImpl()79 SerfPropFindReqProcImpl::~SerfPropFindReqProcImpl()
80 {
81 }
82
83 #define PROPFIND_HEADER "<?xml version=\"1.0\" encoding=\"utf-8\"?><propfind xmlns=\"DAV:\">"
84 #define PROPFIND_TRAILER "</propfind>"
85
createSerfRequestBucket(serf_request_t * inSerfRequest)86 serf_bucket_t * SerfPropFindReqProcImpl::createSerfRequestBucket( serf_request_t * inSerfRequest )
87 {
88 serf_bucket_alloc_t* pSerfBucketAlloc = serf_request_get_alloc( inSerfRequest );
89
90 // body bucket - certain properties OR all properties OR only property names
91 serf_bucket_t* body_bkt = nullptr;
92 OString aBodyText;
93 {
94 OStringBuffer aBuffer;
95 aBuffer.append( PROPFIND_HEADER );
96
97 // create and fill body bucket with requested properties
98 const int nPropCount = ( !mbOnlyPropertyNames && mpPropNames )
99 ? mpPropNames->size()
100 : 0;
101 if ( nPropCount > 0 )
102 {
103 aBuffer.append( "<prop>" );
104 SerfPropName thePropName;
105 for ( int theIndex = 0; theIndex < nPropCount; theIndex ++ )
106 {
107 // split fullname into namespace and name!
108 DAVProperties::createSerfPropName( (*mpPropNames)[ theIndex ],
109 thePropName );
110
111 /* <*propname* xmlns="*propns*" /> */
112 aBuffer.append( "<" );
113 aBuffer.append( thePropName.name );
114 aBuffer.append( " xmlns=\"" );
115 aBuffer.append( thePropName.nspace );
116 aBuffer.append( "\"/>" );
117 }
118
119 aBuffer.append( "</prop>" );
120 }
121 else
122 {
123 if ( mbOnlyPropertyNames )
124 {
125 aBuffer.append( "<propname/>" );
126 }
127 else
128 {
129 aBuffer.append( "<allprop/>" );
130 }
131 }
132
133 aBuffer.append( PROPFIND_TRAILER );
134 aBodyText = aBuffer.makeStringAndClear();
135 body_bkt = serf_bucket_simple_copy_create( aBodyText.getStr(),
136 aBodyText.getLength(),
137 pSerfBucketAlloc );
138 }
139
140 // create serf request
141 serf_bucket_t *req_bkt = serf_request_bucket_request_create( inSerfRequest,
142 "PROPFIND",
143 getPathStr(),
144 body_bkt,
145 pSerfBucketAlloc );
146 handleChunkedEncoding(req_bkt, aBodyText.getLength());
147
148 // set request header fields
149 serf_bucket_t* hdrs_bkt = serf_bucket_request_get_headers( req_bkt );
150 if (hdrs_bkt != nullptr)
151 {
152 // general header fields provided by caller
153 setRequestHeaders( hdrs_bkt );
154
155 // request specific header fields
156 serf_bucket_headers_set( hdrs_bkt, "Depth", mDepthStr );
157 if (hdrs_bkt!=nullptr && body_bkt != nullptr && aBodyText.getLength() > 0 )
158 {
159 serf_bucket_headers_set( hdrs_bkt, "Content-Type", "application/xml" );
160 }
161 }
162 else
163 {
164 assert(!"Headers Bucket missing");
165 }
166
167 return req_bkt;
168 }
169
processChunkOfResponseData(const char * data,apr_size_t len)170 void SerfPropFindReqProcImpl::processChunkOfResponseData( const char* data,
171 apr_size_t len )
172 {
173 if ( xInputStream.is() )
174 {
175 xInputStream->AddToStream( data, len );
176 }
177 }
178
handleEndOfResponseData(serf_bucket_t *)179 void SerfPropFindReqProcImpl::handleEndOfResponseData( serf_bucket_t * /*inSerfResponseBucket*/ )
180 {
181 if ( mbOnlyPropertyNames )
182 {
183 const std::vector< DAVResourceInfo > rResInfo( parseWebDAVPropNameResponse( xInputStream.get() ) );
184 *mpResInfo = rResInfo;
185 }
186 else
187 {
188 const std::vector< DAVResource > rResources( parseWebDAVPropFindResponse( xInputStream.get() ) );
189 *mpResources = rResources;
190 }
191 }
192
193 } // namespace http_dav_ucp
194
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
196