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 #include <SerfRequestProcessorImpl.hxx>
23 
24 namespace http_dav_ucp
25 {
26 
27 SerfRequestProcessorImpl::SerfRequestProcessorImpl( const char* inPath,
28                                                     const DAVRequestHeaders& inRequestHeaders )
29     : mPathStr( inPath )
30     , mrRequestHeaders( inRequestHeaders )
31     , mbUseChunkedEncoding( false )
32 {
33 }
34 
35 SerfRequestProcessorImpl::~SerfRequestProcessorImpl()
36 {
37 }
38 
39 const char* SerfRequestProcessorImpl::getPathStr() const
40 {
41     return mPathStr;
42 }
43 
44 void SerfRequestProcessorImpl::activateChunkedEncoding()
45 {
46     mbUseChunkedEncoding = true;
47 }
48 
49 const bool SerfRequestProcessorImpl::useChunkedEncoding() const
50 {
51     return mbUseChunkedEncoding;
52 }
53 
54 void SerfRequestProcessorImpl::setRequestHeaders( serf_bucket_t* inoutSerfHeaderBucket )
55 {
56     DAVRequestHeaders::const_iterator aHeaderIter( mrRequestHeaders.begin() );
57     const DAVRequestHeaders::const_iterator aEnd( mrRequestHeaders.end() );
58 
59     while ( aHeaderIter != aEnd )
60     {
61         const rtl::OString aHeader = rtl::OUStringToOString( (*aHeaderIter).first,
62                                                                RTL_TEXTENCODING_UTF8 );
63         const rtl::OString aValue = rtl::OUStringToOString( (*aHeaderIter).second,
64                                                             RTL_TEXTENCODING_UTF8 );
65 
66         serf_bucket_headers_set( inoutSerfHeaderBucket,
67                                  aHeader.getStr(),
68                                  aValue.getStr() );
69 
70         ++aHeaderIter;
71     }
72 
73     serf_bucket_headers_set( inoutSerfHeaderBucket, "Accept-Encoding", "gzip");
74 }
75 
76 bool SerfRequestProcessorImpl::processSerfResponseBucket( serf_request_t * /*inSerfRequest*/,
77                                                           serf_bucket_t * inSerfResponseBucket,
78                                                           apr_pool_t * /*inAprPool*/,
79                                                           apr_status_t & outStatus )
80 {
81     const char* data;
82     apr_size_t len;
83 
84     while (1) {
85         outStatus = serf_bucket_read(inSerfResponseBucket, 8096, &data, &len);
86         if (SERF_BUCKET_READ_ERROR(outStatus))
87         {
88             return true;
89         }
90 
91         if ( len > 0 )
92         {
93             processChunkOfResponseData( data, len );
94         }
95 
96         /* are we done yet? */
97         if (APR_STATUS_IS_EOF(outStatus))
98         {
99             handleEndOfResponseData( inSerfResponseBucket );
100 
101             outStatus = APR_EOF;
102             return true;
103         }
104 
105         /* have we drained the response so far? */
106         if ( APR_STATUS_IS_EAGAIN( outStatus ) )
107         {
108             return false;
109         }
110     }
111 
112     /* NOTREACHED */
113     return true;
114 }
115 
116 } // namespace http_dav_ucp
117 
118