xref: /AOO41X/main/ucb/source/ucp/webdav/SerfRequestProcessorImpl.cxx (revision 10e2038720f85a02a5e82dea706eb21b45c72235)
18590a0fdSAndre Fischer /**************************************************************
28590a0fdSAndre Fischer  *
38590a0fdSAndre Fischer  * Licensed to the Apache Software Foundation (ASF) under one
48590a0fdSAndre Fischer  * or more contributor license agreements.  See the NOTICE file
58590a0fdSAndre Fischer  * distributed with this work for additional information
68590a0fdSAndre Fischer  * regarding copyright ownership.  The ASF licenses this file
78590a0fdSAndre Fischer  * to you under the Apache License, Version 2.0 (the
88590a0fdSAndre Fischer  * "License"); you may not use this file except in compliance
98590a0fdSAndre Fischer  * with the License.  You may obtain a copy of the License at
108590a0fdSAndre Fischer  *
118590a0fdSAndre Fischer  *   http://www.apache.org/licenses/LICENSE-2.0
128590a0fdSAndre Fischer  *
138590a0fdSAndre Fischer  * Unless required by applicable law or agreed to in writing,
148590a0fdSAndre Fischer  * software distributed under the License is distributed on an
158590a0fdSAndre Fischer  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
168590a0fdSAndre Fischer  * KIND, either express or implied.  See the License for the
178590a0fdSAndre Fischer  * specific language governing permissions and limitations
188590a0fdSAndre Fischer  * under the License.
198590a0fdSAndre Fischer  *
208590a0fdSAndre Fischer  *************************************************************/
218590a0fdSAndre Fischer 
228590a0fdSAndre Fischer #include <SerfRequestProcessorImpl.hxx>
238590a0fdSAndre Fischer 
24*10e20387SAndre Fischer namespace
25*10e20387SAndre Fischer {
26*10e20387SAndre Fischer // Define a magic value that is used by serf to reset chunked
27*10e20387SAndre Fischer // encoding.  The value definition is not supported by serf, hence the
28*10e20387SAndre Fischer // definition here.
29*10e20387SAndre Fischer static const apr_int64_t SERF_UNKNOWN_LENGTH (-1);
30*10e20387SAndre Fischer }
31*10e20387SAndre Fischer 
328590a0fdSAndre Fischer namespace http_dav_ucp
338590a0fdSAndre Fischer {
348590a0fdSAndre Fischer 
35e9ff7e89SOliver-Rainer Wittmann SerfRequestProcessorImpl::SerfRequestProcessorImpl( const char* inPath,
36e9ff7e89SOliver-Rainer Wittmann                                                     const DAVRequestHeaders& inRequestHeaders )
378590a0fdSAndre Fischer     : mPathStr( inPath )
38e9ff7e89SOliver-Rainer Wittmann     , mrRequestHeaders( inRequestHeaders )
3949989859SOliver-Rainer Wittmann     , mbUseChunkedEncoding( false )
408590a0fdSAndre Fischer {
418590a0fdSAndre Fischer }
428590a0fdSAndre Fischer 
438590a0fdSAndre Fischer SerfRequestProcessorImpl::~SerfRequestProcessorImpl()
448590a0fdSAndre Fischer {
458590a0fdSAndre Fischer }
468590a0fdSAndre Fischer 
478590a0fdSAndre Fischer const char* SerfRequestProcessorImpl::getPathStr() const
488590a0fdSAndre Fischer {
498590a0fdSAndre Fischer     return mPathStr;
508590a0fdSAndre Fischer }
518590a0fdSAndre Fischer 
5249989859SOliver-Rainer Wittmann void SerfRequestProcessorImpl::activateChunkedEncoding()
5349989859SOliver-Rainer Wittmann {
5449989859SOliver-Rainer Wittmann     mbUseChunkedEncoding = true;
5549989859SOliver-Rainer Wittmann }
5649989859SOliver-Rainer Wittmann 
5749989859SOliver-Rainer Wittmann const bool SerfRequestProcessorImpl::useChunkedEncoding() const
5849989859SOliver-Rainer Wittmann {
5949989859SOliver-Rainer Wittmann     return mbUseChunkedEncoding;
6049989859SOliver-Rainer Wittmann }
6149989859SOliver-Rainer Wittmann 
62*10e20387SAndre Fischer 
63*10e20387SAndre Fischer void SerfRequestProcessorImpl::handleChunkedEncoding (
64*10e20387SAndre Fischer     serf_bucket_t* pRequestBucket,
65*10e20387SAndre Fischer     apr_int64_t nLength) const
66*10e20387SAndre Fischer {
67*10e20387SAndre Fischer     if (pRequestBucket != NULL)
68*10e20387SAndre Fischer     {
69*10e20387SAndre Fischer         if (useChunkedEncoding())
70*10e20387SAndre Fischer         {
71*10e20387SAndre Fischer             // Activate chunked encoding.
72*10e20387SAndre Fischer             serf_bucket_request_set_CL(pRequestBucket, SERF_UNKNOWN_LENGTH);
73*10e20387SAndre Fischer         }
74*10e20387SAndre Fischer         else
75*10e20387SAndre Fischer         {
76*10e20387SAndre Fischer             // Deactivate chunked encoding by setting the length.
77*10e20387SAndre Fischer             serf_bucket_request_set_CL(pRequestBucket, nLength);
78*10e20387SAndre Fischer         }
79*10e20387SAndre Fischer     }
80*10e20387SAndre Fischer }
81*10e20387SAndre Fischer 
82*10e20387SAndre Fischer 
83e9ff7e89SOliver-Rainer Wittmann void SerfRequestProcessorImpl::setRequestHeaders( serf_bucket_t* inoutSerfHeaderBucket )
84e9ff7e89SOliver-Rainer Wittmann {
85e9ff7e89SOliver-Rainer Wittmann     DAVRequestHeaders::const_iterator aHeaderIter( mrRequestHeaders.begin() );
86e9ff7e89SOliver-Rainer Wittmann     const DAVRequestHeaders::const_iterator aEnd( mrRequestHeaders.end() );
87e9ff7e89SOliver-Rainer Wittmann 
88e9ff7e89SOliver-Rainer Wittmann     while ( aHeaderIter != aEnd )
89e9ff7e89SOliver-Rainer Wittmann     {
90e9ff7e89SOliver-Rainer Wittmann         const rtl::OString aHeader = rtl::OUStringToOString( (*aHeaderIter).first,
91e9ff7e89SOliver-Rainer Wittmann                                                                RTL_TEXTENCODING_UTF8 );
92e9ff7e89SOliver-Rainer Wittmann         const rtl::OString aValue = rtl::OUStringToOString( (*aHeaderIter).second,
93e9ff7e89SOliver-Rainer Wittmann                                                             RTL_TEXTENCODING_UTF8 );
94e9ff7e89SOliver-Rainer Wittmann 
95e9ff7e89SOliver-Rainer Wittmann         serf_bucket_headers_set( inoutSerfHeaderBucket,
96e9ff7e89SOliver-Rainer Wittmann                                  aHeader.getStr(),
97e9ff7e89SOliver-Rainer Wittmann                                  aValue.getStr() );
98e9ff7e89SOliver-Rainer Wittmann 
99e9ff7e89SOliver-Rainer Wittmann         ++aHeaderIter;
100e9ff7e89SOliver-Rainer Wittmann     }
101e9ff7e89SOliver-Rainer Wittmann 
102e9ff7e89SOliver-Rainer Wittmann     serf_bucket_headers_set( inoutSerfHeaderBucket, "Accept-Encoding", "gzip");
103e9ff7e89SOliver-Rainer Wittmann }
104e9ff7e89SOliver-Rainer Wittmann 
10549989859SOliver-Rainer Wittmann bool SerfRequestProcessorImpl::processSerfResponseBucket( serf_request_t * /*inSerfRequest*/,
10649989859SOliver-Rainer Wittmann                                                           serf_bucket_t * inSerfResponseBucket,
10749989859SOliver-Rainer Wittmann                                                           apr_pool_t * /*inAprPool*/,
10849989859SOliver-Rainer Wittmann                                                           apr_status_t & outStatus )
10949989859SOliver-Rainer Wittmann {
11049989859SOliver-Rainer Wittmann     const char* data;
11149989859SOliver-Rainer Wittmann     apr_size_t len;
11249989859SOliver-Rainer Wittmann 
11349989859SOliver-Rainer Wittmann     while (1) {
11449989859SOliver-Rainer Wittmann         outStatus = serf_bucket_read(inSerfResponseBucket, 8096, &data, &len);
11549989859SOliver-Rainer Wittmann         if (SERF_BUCKET_READ_ERROR(outStatus))
11649989859SOliver-Rainer Wittmann         {
11749989859SOliver-Rainer Wittmann             return true;
11849989859SOliver-Rainer Wittmann         }
11949989859SOliver-Rainer Wittmann 
12049989859SOliver-Rainer Wittmann         if ( len > 0 )
12149989859SOliver-Rainer Wittmann         {
12249989859SOliver-Rainer Wittmann             processChunkOfResponseData( data, len );
12349989859SOliver-Rainer Wittmann         }
12449989859SOliver-Rainer Wittmann 
12549989859SOliver-Rainer Wittmann         /* are we done yet? */
12649989859SOliver-Rainer Wittmann         if (APR_STATUS_IS_EOF(outStatus))
12749989859SOliver-Rainer Wittmann         {
12849989859SOliver-Rainer Wittmann             handleEndOfResponseData( inSerfResponseBucket );
12949989859SOliver-Rainer Wittmann 
13049989859SOliver-Rainer Wittmann             outStatus = APR_EOF;
13149989859SOliver-Rainer Wittmann             return true;
13249989859SOliver-Rainer Wittmann         }
13349989859SOliver-Rainer Wittmann 
13449989859SOliver-Rainer Wittmann         /* have we drained the response so far? */
13549989859SOliver-Rainer Wittmann         if ( APR_STATUS_IS_EAGAIN( outStatus ) )
13649989859SOliver-Rainer Wittmann         {
13749989859SOliver-Rainer Wittmann             return false;
13849989859SOliver-Rainer Wittmann         }
13949989859SOliver-Rainer Wittmann     }
14049989859SOliver-Rainer Wittmann 
14149989859SOliver-Rainer Wittmann     /* NOTREACHED */
14249989859SOliver-Rainer Wittmann     return true;
14349989859SOliver-Rainer Wittmann }
14449989859SOliver-Rainer Wittmann 
1458590a0fdSAndre Fischer } // namespace http_dav_ucp
1468590a0fdSAndre Fischer 
147