xref: /AOO41X/main/ucb/source/ucp/webdav/SerfGetReqProcImpl.cxx (revision a0e9f0af3a76fb6e0a29a864fffa1eb625dafa7c)
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 namespace
99 {
100     apr_status_t Serf_ProcessResponseHeader( void* inUserData,
101                                              const char* inHeaderName,
102                                              const char* inHeaderValue )
103     {
104         SerfGetReqProcImpl* pReqProcImpl = static_cast< SerfGetReqProcImpl* >( inUserData );
105         pReqProcImpl->processSingleResponseHeader( inHeaderName,
106                                                    inHeaderValue );
107 
108         return APR_SUCCESS;
109     }
110 } // end of anonymous namespace
111 
112 bool SerfGetReqProcImpl::processSerfResponseBucket( serf_request_t * /*inSerfRequest*/,
113                                                     serf_bucket_t * inSerfResponseBucket,
114                                                     apr_pool_t * /*inAprPool*/,
115                                                     apr_status_t & outStatus )
116 {
117     const char* data;
118     apr_size_t len;
119 
120     while (1) {
121         outStatus = serf_bucket_read(inSerfResponseBucket, 8096, &data, &len);
122         if (SERF_BUCKET_READ_ERROR(outStatus))
123         {
124             return true;
125         }
126 
127         if ( len > 0 )
128         {
129             if ( xInputStream.is() )
130             {
131                 xInputStream->AddToStream( data, len );
132             }
133             else if ( xOutputStream.is() )
134             {
135                 const uno::Sequence< sal_Int8 > aDataSeq( (sal_Int8 *)data, len );
136                 xOutputStream->writeBytes( aDataSeq );
137             }
138         }
139 
140         /* are we done yet? */
141         if (APR_STATUS_IS_EOF(outStatus))
142         {
143             // read response header, if requested
144             if ( mpHeaderNames != 0 && mpResource != 0 )
145             {
146                 serf_bucket_t* SerfHeaderBucket = serf_bucket_response_get_headers( inSerfResponseBucket );
147                 if ( SerfHeaderBucket != 0 )
148                 {
149                     serf_bucket_headers_do( SerfHeaderBucket,
150                                             Serf_ProcessResponseHeader,
151                                             this );
152                 }
153             }
154 
155             outStatus = APR_EOF;
156             return true;
157         }
158 
159         /* have we drained the response so far? */
160         if ( APR_STATUS_IS_EAGAIN( outStatus ) )
161         {
162             return false;
163         }
164     }
165 
166     /* NOTREACHED */
167     return true;
168 }
169 
170 void SerfGetReqProcImpl::processSingleResponseHeader( const char* inHeaderName,
171                                                       const char* inHeaderValue )
172 {
173     rtl::OUString aHeaderName( rtl::OUString::createFromAscii( inHeaderName ) );
174 
175     bool bStoreHeaderField = false;
176 
177     if ( mpHeaderNames->size() == 0 )
178     {
179         // store all header fields
180         bStoreHeaderField = true;
181     }
182     else
183     {
184         // store only header fields which are requested
185         std::vector< ::rtl::OUString >::const_iterator it( mpHeaderNames->begin() );
186         const std::vector< ::rtl::OUString >::const_iterator end( mpHeaderNames->end() );
187 
188         while ( it != end )
189         {
190             // header names are case insensitive
191             if ( (*it).equalsIgnoreAsciiCase( aHeaderName ) )
192             {
193                 bStoreHeaderField = true;
194                 break;
195             }
196             else
197             {
198                 ++it;
199             }
200         }
201     }
202 
203     if ( bStoreHeaderField )
204     {
205         DAVPropertyValue thePropertyValue;
206         thePropertyValue.IsCaseSensitive = false;
207         thePropertyValue.Name = aHeaderName;
208         thePropertyValue.Value <<= rtl::OUString::createFromAscii( inHeaderValue );
209         mpResource->properties.push_back( thePropertyValue );
210     }
211 }
212 
213 } // namespace http_dav_ucp
214