1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "SerfRequestProcessorImpl.hxx"
21 #include <sal/log.hxx>
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
28 static const apr_int64_t
SERF_UNKNOWN_LENGTH (-1);
31 namespace http_dav_ucp
34 SerfRequestProcessorImpl::SerfRequestProcessorImpl( const char* inPath
,
35 const DAVRequestHeaders
& inRequestHeaders
)
37 , mrRequestHeaders( inRequestHeaders
)
38 , mbUseChunkedEncoding( false )
42 SerfRequestProcessorImpl::~SerfRequestProcessorImpl()
46 const char* SerfRequestProcessorImpl::getPathStr() const
51 void SerfRequestProcessorImpl::activateChunkedEncoding()
53 mbUseChunkedEncoding
= true;
56 bool SerfRequestProcessorImpl::useChunkedEncoding() const
58 return mbUseChunkedEncoding
;
62 void SerfRequestProcessorImpl::handleChunkedEncoding (
63 serf_bucket_t
* pRequestBucket
,
64 apr_int64_t nLength
) const
66 if (pRequestBucket
!= nullptr)
68 if (useChunkedEncoding())
70 // Activate chunked encoding.
71 serf_bucket_request_set_CL(pRequestBucket
, SERF_UNKNOWN_LENGTH
);
75 // Deactivate chunked encoding by setting the length.
76 serf_bucket_request_set_CL(pRequestBucket
, nLength
);
82 void SerfRequestProcessorImpl::setRequestHeaders( serf_bucket_t
* inoutSerfHeaderBucket
)
84 bool bHasUserAgent( false );
86 for ( const auto& rHeader
: mrRequestHeaders
)
88 const OString aHeader
= OUStringToOString( rHeader
.first
, RTL_TEXTENCODING_UTF8
);
89 const OString aValue
= OUStringToOString( rHeader
.second
, RTL_TEXTENCODING_UTF8
);
91 SAL_INFO("ucb.ucp.webdav", "Request Header - \"" << aHeader
<< ": " << aValue
<< "\"");
93 bHasUserAgent
= rHeader
.first
== "User-Agent";
95 serf_bucket_headers_setc( inoutSerfHeaderBucket
,
100 if ( !bHasUserAgent
)
102 serf_bucket_headers_set( inoutSerfHeaderBucket
,
103 "User-Agent", "LibreOffice" );
106 serf_bucket_headers_set( inoutSerfHeaderBucket
, "Accept-Encoding", "gzip");
109 bool SerfRequestProcessorImpl::processSerfResponseBucket( serf_request_t
* /*inSerfRequest*/,
110 serf_bucket_t
* inSerfResponseBucket
,
111 apr_pool_t
* /*inAprPool*/,
112 apr_status_t
& outStatus
)
118 outStatus
= serf_bucket_read(inSerfResponseBucket
, 8096, &data
, &len
);
119 if (SERF_BUCKET_READ_ERROR(outStatus
))
126 processChunkOfResponseData( data
, len
);
129 /* are we done yet? */
130 if (APR_STATUS_IS_EOF(outStatus
))
132 handleEndOfResponseData( inSerfResponseBucket
);
138 /* have we drained the response so far? */
139 if ( APR_STATUS_IS_EAGAIN( outStatus
) )
149 } // namespace http_dav_ucp
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */