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 
SerfHeadReqProcImpl(const char * inPath,const DAVRequestHeaders & inRequestHeaders,const std::vector<::rtl::OUString> & inHeaderNames,DAVResource & ioResource)32 SerfHeadReqProcImpl::SerfHeadReqProcImpl( const char* inPath,
33                                           const DAVRequestHeaders& inRequestHeaders,
34                                           const std::vector< ::rtl::OUString > & inHeaderNames,
35                                           DAVResource & ioResource )
36     : SerfRequestProcessorImpl( inPath, inRequestHeaders )
37     , mpHeaderNames( &inHeaderNames )
38     , mpResource( &ioResource )
39 {
40 }
41 
~SerfHeadReqProcImpl()42 SerfHeadReqProcImpl::~SerfHeadReqProcImpl()
43 {
44 }
45 
createSerfRequestBucket(serf_request_t * inSerfRequest)46 serf_bucket_t * SerfHeadReqProcImpl::createSerfRequestBucket( serf_request_t * inSerfRequest )
47 {
48     // create serf request
49     serf_bucket_t *req_bkt = serf_request_bucket_request_create( inSerfRequest,
50                                                                  "HEAD",
51                                                                  getPathStr(),
52                                                                  0,
53                                                                  serf_request_get_alloc( inSerfRequest ) );
54 
55     // set request header fields
56     serf_bucket_t* hdrs_bkt = serf_bucket_request_get_headers( req_bkt );
57     // general header fields provided by caller
58     setRequestHeaders( hdrs_bkt );
59 
60     return req_bkt;
61 }
62 
processChunkOfResponseData(const char *,apr_size_t)63 void SerfHeadReqProcImpl::processChunkOfResponseData( const char* /*data*/,
64                                                       apr_size_t /*len*/ )
65 {
66     // nothing to do
67 }
68 
69 namespace
70 {
Serf_ProcessResponseHeader(void * inUserData,const char * inHeaderName,const char * inHeaderValue)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 
handleEndOfResponseData(serf_bucket_t * inSerfResponseBucket)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 
processSingleResponseHeader(const char * inHeaderName,const char * inHeaderValue)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