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 
23 
24 #ifndef INCLUDED_SERFSESSION_HXX
25 #define INCLUDED_SERFSESSION_HXX
26 
27 #include <vector>
28 #include <boost/shared_ptr.hpp>
29 #include <osl/mutex.hxx>
30 #include "DAVSession.hxx"
31 #include "SerfTypes.hxx"
32 //#include "SerfLockStore.hxx"
33 #include "SerfUri.hxx"
34 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 
36 namespace ucbhelper { class ProxyDecider; }
37 
38 namespace http_dav_ucp
39 {
40 
41 class SerfRequestProcessor;
42 
43 // -------------------------------------------------------------------
44 // SerfSession
45 // A DAVSession implementation using the neon/expat library
46 // -------------------------------------------------------------------
47 
48 class SerfSession : public DAVSession
49 {
50 private:
51     osl::Mutex              m_aMutex;
52 
53     SerfUri                 m_aUri;
54 
55     rtl::OUString           m_aProxyName;
56     sal_Int32               m_nProxyPort;
57 
58     SerfConnection*         m_pSerfConnection;
59     serf_context_t*         m_pSerfContext;
60     serf_bucket_alloc_t*    m_pSerfBucket_Alloc;
61     bool                    m_bIsHeadRequestInProgress;
62     bool                    m_bUseChunkedEncoding;
63     sal_Int16               m_bNoOfTransferEncodingSwitches;
64 
65     const ucbhelper::InternetProxyDecider & m_rProxyDecider;
66 
67     DAVRequestEnvironment m_aEnv;
68 
69 //    static SerfLockStore m_aSerfLockStore;
70 
71     char* getHostinfo();
72     bool isSSLNeeded();
73 
74     SerfRequestProcessor* createReqProc( const rtl::OUString & inPath );
75 
76 protected:
77     virtual ~SerfSession();
78 
79 public:
80     SerfSession( const rtl::Reference< DAVSessionFactory > & rSessionFactory,
81                  const rtl::OUString& inUri,
82                  const ucbhelper::InternetProxyDecider & rProxyDecider )
83         throw ( DAVException );
84 
85     // Serf library callbacks
86     apr_status_t setupSerfConnection( apr_socket_t * inAprSocket,
87                                       serf_bucket_t **outSerfInputBucket,
88                                       serf_bucket_t **outSerfOutputBucket,
89                                       apr_pool_t* inAprPool );
90 
91     apr_status_t provideSerfCredentials( bool bGiveProvidedCredentialsASecondTry,
92                                          char ** outUsername,
93                                          char ** outPassword,
94                                          serf_request_t * inRequest,
95                                          int inCode,
96                                          const char *inAuthProtocol,
97                                          const char *inRealm,
98                                          apr_pool_t *inAprPool );
99 
100     apr_status_t verifySerfCertificateChain (
101         int nFailures,
102         const serf_ssl_certificate_t * const * pCertificateChainBase64Encoded,
103         int nCertificateChainLength);
104 
105     serf_bucket_t* acceptSerfResponse( serf_request_t * inSerfRequest,
106                                        serf_bucket_t * inSerfStreamBucket,
107                                        apr_pool_t* inAprPool );
108 
109     // Serf-related data structures
110     apr_pool_t* getAprPool();
111     serf_bucket_alloc_t* getSerfBktAlloc();
112     serf_context_t* getSerfContext();
113     SerfConnection* getSerfConnection();
114 
115     // DAVSession methods
116     virtual sal_Bool CanUse( const ::rtl::OUString & inUri );
117 
118     virtual sal_Bool UsesProxy();
119 
120     const DAVRequestEnvironment & getRequestEnvironment() const
121     { return m_aEnv; }
122 
123     // allprop & named
124     virtual void
125     PROPFIND( const ::rtl::OUString & inPath,
126               const Depth inDepth,
127               const std::vector< ::rtl::OUString > & inPropNames,
128               std::vector< DAVResource > & ioResources,
129               const DAVRequestEnvironment & rEnv )
130         throw ( DAVException );
131 
132     // propnames
133     virtual void
134     PROPFIND( const ::rtl::OUString & inPath,
135               const Depth inDepth,
136               std::vector< DAVResourceInfo >& ioResInfo,
137               const DAVRequestEnvironment & rEnv )
138         throw ( DAVException );
139 
140     virtual void
141     PROPPATCH( const ::rtl::OUString & inPath,
142                const std::vector< ProppatchValue > & inValues,
143                const DAVRequestEnvironment & rEnv )
144         throw ( DAVException );
145 
146     virtual void
147     HEAD( const ::rtl::OUString &  inPath,
148           const std::vector< ::rtl::OUString > & inHeaderNames,
149           DAVResource & ioResource,
150           const DAVRequestEnvironment & rEnv )
151         throw ( DAVException );
152 
153     bool isHeadRequestInProgress();
154 
155     virtual com::sun::star::uno::Reference< com::sun::star::io::XInputStream >
156     GET( const ::rtl::OUString & inPath,
157          const DAVRequestEnvironment & rEnv )
158         throw ( DAVException );
159 
160     virtual void
161     GET( const ::rtl::OUString & inPath,
162          com::sun::star::uno::Reference<
163              com::sun::star::io::XOutputStream > &  ioOutputStream,
164          const DAVRequestEnvironment & rEnv )
165         throw ( DAVException );
166 
167     virtual com::sun::star::uno::Reference< com::sun::star::io::XInputStream >
168     GET( const ::rtl::OUString & inPath,
169          const std::vector< ::rtl::OUString > & inHeaderNames,
170          DAVResource & ioResource,
171          const DAVRequestEnvironment & rEnv )
172         throw ( DAVException );
173 
174     virtual void
175     GET( const ::rtl::OUString & inPath,
176          com::sun::star::uno::Reference<
177              com::sun::star::io::XOutputStream > & ioOutputStream,
178          const std::vector< ::rtl::OUString > & inHeaderNames,
179          DAVResource & ioResource,
180          const DAVRequestEnvironment & rEnv )
181         throw ( DAVException );
182 
183     virtual void
184     PUT( const ::rtl::OUString & inPath,
185          const com::sun::star::uno::Reference<
186              com::sun::star::io::XInputStream > & inInputStream,
187          const DAVRequestEnvironment & rEnv )
188         throw ( DAVException );
189 
190     virtual com::sun::star::uno::Reference< com::sun::star::io::XInputStream >
191     POST( const rtl::OUString & inPath,
192           const rtl::OUString & rContentType,
193           const rtl::OUString & rReferer,
194           const com::sun::star::uno::Reference<
195               com::sun::star::io::XInputStream > & inInputStream,
196           const DAVRequestEnvironment & rEnv )
197         throw ( DAVException );
198 
199     virtual void
200     POST( const rtl::OUString & inPath,
201           const rtl::OUString & rContentType,
202           const rtl::OUString & rReferer,
203           const com::sun::star::uno::Reference<
204               com::sun::star::io::XInputStream > & inInputStream,
205           com::sun::star::uno::Reference<
206               com::sun::star::io::XOutputStream > & oOutputStream,
207           const DAVRequestEnvironment & rEnv )
208         throw ( DAVException );
209 
210     virtual void
211     MKCOL( const ::rtl::OUString & inPath,
212            const DAVRequestEnvironment & rEnv )
213         throw ( DAVException );
214 
215     virtual void
216     COPY( const ::rtl::OUString & inSourceURL,
217           const ::rtl::OUString & inDestinationURL,
218           const DAVRequestEnvironment & rEnv,
219           sal_Bool inOverWrite )
220         throw ( DAVException );
221 
222     virtual void
223     MOVE( const ::rtl::OUString & inSourceURL,
224           const ::rtl::OUString & inDestinationURL,
225           const DAVRequestEnvironment & rEnv,
226           sal_Bool inOverWrite )
227         throw ( DAVException );
228 
229     virtual void DESTROY( const ::rtl::OUString & inPath,
230                           const DAVRequestEnvironment & rEnv )
231         throw ( DAVException );
232 
233     // set new lock.
234     virtual void LOCK( const ::rtl::OUString & inURL,
235                        com::sun::star::ucb::Lock & inLock,
236                        const DAVRequestEnvironment & rEnv )
237         throw ( DAVException );
238 
239     // refresh existing lock.
240     virtual sal_Int64 LOCK( const ::rtl::OUString & inURL,
241                             sal_Int64 nTimeout,
242                             const DAVRequestEnvironment & rEnv )
243         throw ( DAVException );
244 
245     virtual void UNLOCK( const ::rtl::OUString & inURL,
246                          const DAVRequestEnvironment & rEnv )
247         throw ( DAVException );
248 
249     // helpers
250     virtual void abort()
251         throw ( DAVException );
252 
253     const rtl::OUString & getHostName() const { return m_aUri.GetHost(); }
254     int getPort() const { return m_aUri.GetPort(); }
255 
256     const ::uno::Reference< ::lang::XMultiServiceFactory > getMSF()
257     { return m_xFactory->getServiceFactory(); }
258 
259     sal_Bool isDomainMatch( rtl::OUString certHostName );
260 
261 private:
262     friend class SerfLockStore;
263 
264     void Init( void )
265         throw ( DAVException );
266 
267     void Init( const DAVRequestEnvironment & rEnv )
268         throw ( DAVException );
269 
270     void HandleError( boost::shared_ptr<SerfRequestProcessor> rReqProc )
271         throw ( DAVException );
272 
273     const ucbhelper::InternetProxyServer & getProxySettings() const;
274 
275     bool removeExpiredLocktoken( const rtl::OUString & inURL,
276                                  const DAVRequestEnvironment & rEnv );
277 
278     // refresh lock, called by SerfLockStore::refreshLocks
279     bool LOCK( SerfLock * pLock,
280                sal_Int32 & rlastChanceToSendRefreshRequest );
281 
282     // unlock, called by SerfLockStore::~SerfLockStore
283     bool UNLOCK( SerfLock * pLock );
284 
285     /*
286     // low level GET implementation, used by public GET implementations
287     static int GET( SerfConnection * sess,
288                     const char * uri,
289                     //ne_block_reader reader,
290                     bool getheaders,
291                     void * userdata );
292 
293     // Buffer-based PUT implementation. Serf only has file descriptor-
294     // based API.
295     static int PUT( SerfConnection * sess,
296                     const char * uri,
297                     const char * buffer,
298                     size_t size );
299 
300     // Buffer-based POST implementation. Serf only has file descriptor-
301     // based API.
302     int POST( SerfConnection * sess,
303               const char * uri,
304               const char * buffer,
305               //ne_block_reader reader,
306               void * userdata,
307               const rtl::OUString & rContentType,
308               const rtl::OUString & rReferer );
309     */
310 
311     // Helper: XInputStream -> Sequence< sal_Int8 >
312     static bool getDataFromInputStream(
313         const com::sun::star::uno::Reference<
314             com::sun::star::io::XInputStream > & xStream,
315         com::sun::star::uno::Sequence< sal_Int8 > & rData,
316         bool bAppendTrailingZeroByte );
317 
318     /*
319     rtl::OUString makeAbsoluteURL( rtl::OUString const & rURL ) const;
320     */
321 };
322 
323 } // namespace http_dav_ucp
324 
325 #endif // INCLUDED_SERFSESSION_HXX
326