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 "SerfGetReqProcImpl.hxx"
26 
27 using namespace com::sun::star;
28 
29 namespace http_dav_ucp
30 {
31 
SerfGetReqProcImpl(const char * inPath,const DAVRequestHeaders & inRequestHeaders,const com::sun::star::uno::Reference<SerfInputStream> & xioInStrm)32 SerfGetReqProcImpl::SerfGetReqProcImpl( const char* inPath,
33                                         const DAVRequestHeaders& inRequestHeaders,
34                                         const com::sun::star::uno::Reference< SerfInputStream > & xioInStrm )
35     : SerfRequestProcessorImpl( inPath, inRequestHeaders )
36     , xInputStream( xioInStrm )
37     , xOutputStream()
38     , mpHeaderNames( 0 )
39     , mpResource( 0 )
40 {
41 }
42 
SerfGetReqProcImpl(const char * inPath,const DAVRequestHeaders & inRequestHeaders,const com::sun::star::uno::Reference<SerfInputStream> & xioInStrm,const std::vector<::rtl::OUString> & inHeaderNames,DAVResource & ioResource)43 SerfGetReqProcImpl::SerfGetReqProcImpl( const char* inPath,
44                                         const DAVRequestHeaders& inRequestHeaders,
45                                         const com::sun::star::uno::Reference< SerfInputStream > & xioInStrm,
46                                         const std::vector< ::rtl::OUString > & inHeaderNames,
47                                         DAVResource & ioResource )
48     : SerfRequestProcessorImpl( inPath, inRequestHeaders )
49     , xInputStream( xioInStrm )
50     , xOutputStream()
51     , mpHeaderNames( &inHeaderNames )
52     , mpResource( &ioResource )
53 {
54 }
55 
SerfGetReqProcImpl(const char * inPath,const DAVRequestHeaders & inRequestHeaders,const com::sun::star::uno::Reference<com::sun::star::io::XOutputStream> & xioOutStrm)56 SerfGetReqProcImpl::SerfGetReqProcImpl( const char* inPath,
57                                         const DAVRequestHeaders& inRequestHeaders,
58                                         const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > & xioOutStrm )
59     : SerfRequestProcessorImpl( inPath, inRequestHeaders )
60     , xInputStream()
61     , xOutputStream( xioOutStrm )
62     , mpHeaderNames( 0 )
63     , mpResource( 0 )
64 {
65 }
66 
SerfGetReqProcImpl(const char * inPath,const DAVRequestHeaders & inRequestHeaders,const com::sun::star::uno::Reference<com::sun::star::io::XOutputStream> & xioOutStrm,const std::vector<::rtl::OUString> & inHeaderNames,DAVResource & ioResource)67 SerfGetReqProcImpl::SerfGetReqProcImpl( const char* inPath,
68                                         const DAVRequestHeaders& inRequestHeaders,
69                                         const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > & xioOutStrm,
70                                         const std::vector< ::rtl::OUString > & inHeaderNames,
71                                         DAVResource & ioResource )
72     : SerfRequestProcessorImpl( inPath, inRequestHeaders )
73     , xInputStream()
74     , xOutputStream( xioOutStrm )
75     , mpHeaderNames( &inHeaderNames )
76     , mpResource( &ioResource )
77 {
78 }
79 
~SerfGetReqProcImpl()80 SerfGetReqProcImpl::~SerfGetReqProcImpl()
81 {
82 }
83 
createSerfRequestBucket(serf_request_t * inSerfRequest)84 serf_bucket_t * SerfGetReqProcImpl::createSerfRequestBucket( serf_request_t * inSerfRequest )
85 {
86     // create serf request
87     serf_bucket_t *req_bkt = serf_request_bucket_request_create( inSerfRequest,
88                                                                  "GET",
89                                                                  getPathStr(),
90                                                                  0,
91                                                                  serf_request_get_alloc( inSerfRequest ) );
92 
93     // set request header fields
94     serf_bucket_t* hdrs_bkt = serf_bucket_request_get_headers( req_bkt );
95     // general header fields provided by caller
96     setRequestHeaders( hdrs_bkt );
97 
98     return req_bkt;
99 }
100 
processChunkOfResponseData(const char * data,apr_size_t len)101 void SerfGetReqProcImpl::processChunkOfResponseData( const char* data,
102                                                      apr_size_t len )
103 {
104     if ( xInputStream.is() )
105     {
106         xInputStream->AddToStream( data, len );
107     }
108     else if ( xOutputStream.is() )
109     {
110         const uno::Sequence< sal_Int8 > aDataSeq( (sal_Int8 *)data, len );
111         xOutputStream->writeBytes( aDataSeq );
112     }
113 }
114 
115 namespace
116 {
Serf_ProcessResponseHeader(void * inUserData,const char * inHeaderName,const char * inHeaderValue)117     apr_status_t Serf_ProcessResponseHeader( void* inUserData,
118                                              const char* inHeaderName,
119                                              const char* inHeaderValue )
120     {
121         SerfGetReqProcImpl* pReqProcImpl = static_cast< SerfGetReqProcImpl* >( inUserData );
122         pReqProcImpl->processSingleResponseHeader( inHeaderName,
123                                                    inHeaderValue );
124 
125         return APR_SUCCESS;
126     }
127 } // end of anonymous namespace
128 
handleEndOfResponseData(serf_bucket_t * inSerfResponseBucket)129 void SerfGetReqProcImpl::handleEndOfResponseData( serf_bucket_t * inSerfResponseBucket )
130 {
131     // read response header, if requested
132     if ( mpHeaderNames != 0 && mpResource != 0 )
133     {
134         serf_bucket_t* SerfHeaderBucket = serf_bucket_response_get_headers( inSerfResponseBucket );
135         if ( SerfHeaderBucket != 0 )
136         {
137             serf_bucket_headers_do( SerfHeaderBucket,
138                                     Serf_ProcessResponseHeader,
139                                     this );
140         }
141     }
142 }
143 
processSingleResponseHeader(const char * inHeaderName,const char * inHeaderValue)144 void SerfGetReqProcImpl::processSingleResponseHeader( const char* inHeaderName,
145                                                       const char* inHeaderValue )
146 {
147     rtl::OUString aHeaderName( rtl::OUString::createFromAscii( inHeaderName ) );
148 
149     bool bStoreHeaderField = false;
150 
151     if ( mpHeaderNames->size() == 0 )
152     {
153         // store all header fields
154         bStoreHeaderField = true;
155     }
156     else
157     {
158         // store only header fields which are requested
159         std::vector< ::rtl::OUString >::const_iterator it( mpHeaderNames->begin() );
160         const std::vector< ::rtl::OUString >::const_iterator end( mpHeaderNames->end() );
161 
162         while ( it != end )
163         {
164             // header names are case insensitive
165             if ( (*it).equalsIgnoreAsciiCase( aHeaderName ) )
166             {
167                 bStoreHeaderField = true;
168                 break;
169             }
170             else
171             {
172                 ++it;
173             }
174         }
175     }
176 
177     if ( bStoreHeaderField )
178     {
179         DAVPropertyValue thePropertyValue;
180         thePropertyValue.IsCaseSensitive = false;
181         thePropertyValue.Name = aHeaderName;
182         thePropertyValue.Value <<= rtl::OUString::createFromAscii( inHeaderValue );
183         mpResource->properties.push_back( thePropertyValue );
184     }
185 }
186 
187 } // namespace http_dav_ucp
188