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