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