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 void SerfHeadReqProcImpl::processChunkOfResponseData( const char* /*data*/,
64                                                       apr_size_t /*len*/ )
65 {
66     // nothing to do
67 }
68 
69 namespace
70 {
71     apr_status_t Serf_ProcessResponseHeader( void* inUserData,
72                                              const char* inHeaderName,
73                                              const char* inHeaderValue )
74     {
75         SerfHeadReqProcImpl* pReqProcImpl = static_cast< SerfHeadReqProcImpl* >( inUserData );
76         pReqProcImpl->processSingleResponseHeader( inHeaderName,
77                                                    inHeaderValue );
78 
79         return APR_SUCCESS;
80     }
81 } // end of anonymous namespace
82 
83 void SerfHeadReqProcImpl::handleEndOfResponseData( serf_bucket_t * inSerfResponseBucket )
84 {
85     // read response header, if requested
86     if ( mpHeaderNames != 0 && mpResource != 0 )
87     {
88         serf_bucket_t* SerfHeaderBucket = serf_bucket_response_get_headers( inSerfResponseBucket );
89         if ( SerfHeaderBucket != 0 )
90         {
91             serf_bucket_headers_do( SerfHeaderBucket,
92                                     Serf_ProcessResponseHeader,
93                                     this );
94         }
95     }
96 }
97 
98 void SerfHeadReqProcImpl::processSingleResponseHeader( const char* inHeaderName,
99                                                        const char* inHeaderValue )
100 {
101     rtl::OUString aHeaderName( rtl::OUString::createFromAscii( inHeaderName ) );
102 
103     bool bStoreHeaderField = false;
104 
105     if ( mpHeaderNames->size() == 0 )
106     {
107         // store all header fields
108         bStoreHeaderField = true;
109     }
110     else
111     {
112         // store only header fields which are requested
113         std::vector< ::rtl::OUString >::const_iterator it( mpHeaderNames->begin() );
114         const std::vector< ::rtl::OUString >::const_iterator end( mpHeaderNames->end() );
115 
116         while ( it != end )
117         {
118             // header names are case insensitive
119             if ( (*it).equalsIgnoreAsciiCase( aHeaderName ) )
120             {
121                 bStoreHeaderField = true;
122                 break;
123             }
124             else
125             {
126                 ++it;
127             }
128         }
129     }
130 
131     if ( bStoreHeaderField )
132     {
133         DAVPropertyValue thePropertyValue;
134         thePropertyValue.IsCaseSensitive = false;
135         thePropertyValue.Name = aHeaderName;
136         thePropertyValue.Value <<= rtl::OUString::createFromAscii( inHeaderValue );
137         mpResource->properties.push_back( thePropertyValue );
138     }
139 }
140 
141 } // namespace http_dav_ucp
142