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