LibreOffice Module ucb (master)  1
SerfGetReqProcImpl.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include "SerfGetReqProcImpl.hxx"
21 
22 using namespace com::sun::star;
23 
24 namespace http_dav_ucp
25 {
26 
27 SerfGetReqProcImpl::SerfGetReqProcImpl( const char* inPath,
28  const DAVRequestHeaders& inRequestHeaders,
29  const rtl::Reference< SerfInputStream > & xioInStrm )
30  : SerfRequestProcessorImpl( inPath, inRequestHeaders )
31  , xInputStream( xioInStrm )
32  , xOutputStream()
33  , mpHeaderNames( nullptr )
34  , mpResource( nullptr )
35 {
36 }
37 
39  const DAVRequestHeaders& inRequestHeaders,
40  const rtl::Reference< SerfInputStream > & xioInStrm,
41  const std::vector< OUString > & inHeaderNames,
42  DAVResource & ioResource )
43  : SerfRequestProcessorImpl( inPath, inRequestHeaders )
44  , xInputStream( xioInStrm )
45  , xOutputStream()
46  , mpHeaderNames( &inHeaderNames )
47  , mpResource( &ioResource )
48 {
49 }
50 
52  const DAVRequestHeaders& inRequestHeaders,
53  const css::uno::Reference< css::io::XOutputStream > & xioOutStrm )
54  : SerfRequestProcessorImpl( inPath, inRequestHeaders )
55  , xInputStream()
56  , xOutputStream( xioOutStrm )
57  , mpHeaderNames( nullptr )
58  , mpResource( nullptr )
59 {
60 }
61 
63  const DAVRequestHeaders& inRequestHeaders,
64  const css::uno::Reference< css::io::XOutputStream > & xioOutStrm,
65  const std::vector< OUString > & inHeaderNames,
66  DAVResource & ioResource )
67  : SerfRequestProcessorImpl( inPath, inRequestHeaders )
68  , xInputStream()
69  , xOutputStream( xioOutStrm )
70  , mpHeaderNames( &inHeaderNames )
71  , mpResource( &ioResource )
72 {
73 }
74 
76 {
77 }
78 
79 serf_bucket_t * SerfGetReqProcImpl::createSerfRequestBucket( serf_request_t * inSerfRequest )
80 {
81  // create serf request
82  serf_bucket_t *req_bkt = serf_request_bucket_request_create( inSerfRequest,
83  "GET",
84  getPathStr(),
85  nullptr,
86  serf_request_get_alloc( inSerfRequest ) );
87 
88  // set request header fields
89  serf_bucket_t* hdrs_bkt = serf_bucket_request_get_headers( req_bkt );
90  // general header fields provided by caller
91  setRequestHeaders( hdrs_bkt );
92 
93  return req_bkt;
94 }
95 
97  apr_size_t len )
98 {
99  if ( xInputStream.is() )
100  {
101  xInputStream->AddToStream( data, len );
102  }
103  else if ( xOutputStream.is() )
104  {
105  const uno::Sequence< sal_Int8 > aDataSeq( reinterpret_cast<const sal_Int8 *>(data), len );
106  xOutputStream->writeBytes( aDataSeq );
107  }
108 }
109 
110 namespace
111 {
112  apr_status_t Serf_ProcessResponseHeader( void* inUserData,
113  const char* inHeaderName,
114  const char* inHeaderValue )
115  {
116  SerfGetReqProcImpl* pReqProcImpl = static_cast< SerfGetReqProcImpl* >( inUserData );
117  pReqProcImpl->processSingleResponseHeader( inHeaderName,
118  inHeaderValue );
119 
120  return APR_SUCCESS;
121  }
122 } // end of anonymous namespace
123 
124 void SerfGetReqProcImpl::handleEndOfResponseData( serf_bucket_t * inSerfResponseBucket )
125 {
126  // read response header, if requested
127  if ( mpHeaderNames != nullptr && mpResource != nullptr )
128  {
129  serf_bucket_t* SerfHeaderBucket = serf_bucket_response_get_headers( inSerfResponseBucket );
130  if ( SerfHeaderBucket != nullptr )
131  {
132  serf_bucket_headers_do( SerfHeaderBucket,
133  Serf_ProcessResponseHeader,
134  this );
135  }
136  }
137 }
138 
139 void SerfGetReqProcImpl::processSingleResponseHeader( const char* inHeaderName,
140  const char* inHeaderValue )
141 {
142  OUString aHeaderName( OUString::createFromAscii( inHeaderName ) );
143 
144  bool bStoreHeaderField = false;
145 
146  if ( mpHeaderNames->empty() )
147  {
148  // store all header fields
149  bStoreHeaderField = true;
150  }
151  else
152  {
153  // store only header fields which are requested
154  bStoreHeaderField = std::any_of(mpHeaderNames->begin(), mpHeaderNames->end(),
155  [&aHeaderName](const OUString& rHeaderName) {
156  // header names are case insensitive
157  return rHeaderName.equalsIgnoreAsciiCase( aHeaderName );
158  });
159  }
160 
161  if ( bStoreHeaderField )
162  {
163  DAVPropertyValue thePropertyValue;
164  thePropertyValue.IsCaseSensitive = false;
165  thePropertyValue.Name = aHeaderName;
166  thePropertyValue.Value <<= OUString::createFromAscii( inHeaderValue );
167  mpResource->properties.push_back( thePropertyValue );
168  }
169 }
170 
171 } // namespace http_dav_ucp
172 
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::vector< DAVRequestHeader > DAVRequestHeaders
void setRequestHeaders(serf_bucket_t *inoutSerfHeaderBucket)
SerfGetReqProcImpl(const char *inPath, const DAVRequestHeaders &inRequestHeaders, const rtl::Reference< SerfInputStream > &xioInStrm)
virtual void handleEndOfResponseData(serf_bucket_t *inSerfResponseBucket) override
virtual serf_bucket_t * createSerfRequestBucket(serf_request_t *inSerfRequest) override
void processSingleResponseHeader(const char *inHeaderName, const char *inHeaderValue)
virtual void processChunkOfResponseData(const char *data, apr_size_t len) override
rtl::Reference< SerfInputStream > xInputStream
std::vector< DAVPropertyValue > properties
Definition: DAVResource.hxx:44
css::uno::Reference< css::io::XOutputStream > xOutputStream
const std::vector< OUString > * mpHeaderNames