LibreOffice Module ucb (master)  1
SerfRequestProcessorImpl.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 
21 #include <sal/log.hxx>
22 
23 namespace
24 {
25 // Define a magic value that is used by serf to reset chunked
26 // encoding. The value definition is not supported by serf, hence the
27 // definition here.
28 static const apr_int64_t SERF_UNKNOWN_LENGTH (-1);
29 }
30 
31 namespace http_dav_ucp
32 {
33 
35  const DAVRequestHeaders& inRequestHeaders )
36  : mPathStr( inPath )
37  , mrRequestHeaders( inRequestHeaders )
38  , mbUseChunkedEncoding( false )
39 {
40 }
41 
43 {
44 }
45 
47 {
48  return mPathStr;
49 }
50 
52 {
53  mbUseChunkedEncoding = true;
54 }
55 
57 {
58  return mbUseChunkedEncoding;
59 }
60 
61 
63  serf_bucket_t* pRequestBucket,
64  apr_int64_t nLength) const
65 {
66  if (pRequestBucket != nullptr)
67  {
68  if (useChunkedEncoding())
69  {
70  // Activate chunked encoding.
71  serf_bucket_request_set_CL(pRequestBucket, SERF_UNKNOWN_LENGTH);
72  }
73  else
74  {
75  // Deactivate chunked encoding by setting the length.
76  serf_bucket_request_set_CL(pRequestBucket, nLength);
77  }
78  }
79 }
80 
81 
82 void SerfRequestProcessorImpl::setRequestHeaders( serf_bucket_t* inoutSerfHeaderBucket )
83 {
84  bool bHasUserAgent( false );
85 
86  for ( const auto& rHeader : mrRequestHeaders )
87  {
88  const OString aHeader = OUStringToOString( rHeader.first, RTL_TEXTENCODING_UTF8 );
89  const OString aValue = OUStringToOString( rHeader.second, RTL_TEXTENCODING_UTF8 );
90 
91  SAL_INFO("ucb.ucp.webdav", "Request Header - \"" << aHeader << ": " << aValue << "\"");
92  if ( !bHasUserAgent )
93  bHasUserAgent = rHeader.first == "User-Agent";
94 
95  serf_bucket_headers_setc( inoutSerfHeaderBucket,
96  aHeader.getStr(),
97  aValue.getStr() );
98  }
99 
100  if ( !bHasUserAgent )
101  {
102  serf_bucket_headers_set( inoutSerfHeaderBucket,
103  "User-Agent", "LibreOffice" );
104  }
105 
106  serf_bucket_headers_set( inoutSerfHeaderBucket, "Accept-Encoding", "gzip");
107 }
108 
109 bool SerfRequestProcessorImpl::processSerfResponseBucket( serf_request_t * /*inSerfRequest*/,
110  serf_bucket_t * inSerfResponseBucket,
111  apr_pool_t * /*inAprPool*/,
112  apr_status_t & outStatus )
113 {
114  const char* data;
115  apr_size_t len;
116 
117  while (true) {
118  outStatus = serf_bucket_read(inSerfResponseBucket, 8096, &data, &len);
119  if (SERF_BUCKET_READ_ERROR(outStatus))
120  {
121  return true;
122  }
123 
124  if ( len > 0 )
125  {
126  processChunkOfResponseData( data, len );
127  }
128 
129  /* are we done yet? */
130  if (APR_STATUS_IS_EOF(outStatus))
131  {
132  handleEndOfResponseData( inSerfResponseBucket );
133 
134  outStatus = APR_EOF;
135  return true;
136  }
137 
138  /* have we drained the response so far? */
139  if ( APR_STATUS_IS_EAGAIN( outStatus ) )
140  {
141  return false;
142  }
143  }
144 
145  /* NOTREACHED */
146  return true;
147 }
148 
149 } // namespace http_dav_ucp
150 
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::vector< DAVRequestHeader > DAVRequestHeaders
virtual void handleEndOfResponseData(serf_bucket_t *inSerfResponseBucket)=0
SerfRequestProcessorImpl(const char *inPath, const DAVRequestHeaders &inRequestHeaders)
void setRequestHeaders(serf_bucket_t *inoutSerfHeaderBucket)
virtual void processChunkOfResponseData(const char *data, apr_size_t len)=0
void handleChunkedEncoding(serf_bucket_t *pRequestBucket, apr_int64_t nLength) const
Turn chunked encoding on or off, depending on the result of useChunkedEncoding(). ...
bool processSerfResponseBucket(serf_request_t *inSerfRequest, serf_bucket_t *inSerfResponseBucket, apr_pool_t *inAprPool, apr_status_t &outStatus)
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
#define SAL_INFO(area, stream)