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