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 22c1c10f68SAriel Constenla-Haile #include "SerfRequestProcessorImpl.hxx" 23*6601ee31SAriel Constenla-Haile #include "webdavuseragent.hxx" 248590a0fdSAndre Fischer 2510e20387SAndre Fischer namespace 2610e20387SAndre Fischer { 2710e20387SAndre Fischer // Define a magic value that is used by serf to reset chunked 2810e20387SAndre Fischer // encoding. The value definition is not supported by serf, hence the 2910e20387SAndre Fischer // definition here. 3010e20387SAndre Fischer static const apr_int64_t SERF_UNKNOWN_LENGTH (-1); 3110e20387SAndre Fischer } 3210e20387SAndre Fischer 338590a0fdSAndre Fischer namespace http_dav_ucp 348590a0fdSAndre Fischer { 358590a0fdSAndre Fischer 36e9ff7e89SOliver-Rainer Wittmann SerfRequestProcessorImpl::SerfRequestProcessorImpl( const char* inPath, 37e9ff7e89SOliver-Rainer Wittmann const DAVRequestHeaders& inRequestHeaders ) 388590a0fdSAndre Fischer : mPathStr( inPath ) 39e9ff7e89SOliver-Rainer Wittmann , mrRequestHeaders( inRequestHeaders ) 4049989859SOliver-Rainer Wittmann , mbUseChunkedEncoding( false ) 418590a0fdSAndre Fischer { 428590a0fdSAndre Fischer } 438590a0fdSAndre Fischer 448590a0fdSAndre Fischer SerfRequestProcessorImpl::~SerfRequestProcessorImpl() 458590a0fdSAndre Fischer { 468590a0fdSAndre Fischer } 478590a0fdSAndre Fischer 488590a0fdSAndre Fischer const char* SerfRequestProcessorImpl::getPathStr() const 498590a0fdSAndre Fischer { 508590a0fdSAndre Fischer return mPathStr; 518590a0fdSAndre Fischer } 528590a0fdSAndre Fischer 5349989859SOliver-Rainer Wittmann void SerfRequestProcessorImpl::activateChunkedEncoding() 5449989859SOliver-Rainer Wittmann { 5549989859SOliver-Rainer Wittmann mbUseChunkedEncoding = true; 5649989859SOliver-Rainer Wittmann } 5749989859SOliver-Rainer Wittmann 583277e71fSAriel Constenla-Haile bool SerfRequestProcessorImpl::useChunkedEncoding() const 5949989859SOliver-Rainer Wittmann { 6049989859SOliver-Rainer Wittmann return mbUseChunkedEncoding; 6149989859SOliver-Rainer Wittmann } 6249989859SOliver-Rainer Wittmann 6310e20387SAndre Fischer 6410e20387SAndre Fischer void SerfRequestProcessorImpl::handleChunkedEncoding ( 6510e20387SAndre Fischer serf_bucket_t* pRequestBucket, 6610e20387SAndre Fischer apr_int64_t nLength) const 6710e20387SAndre Fischer { 6810e20387SAndre Fischer if (pRequestBucket != NULL) 6910e20387SAndre Fischer { 7010e20387SAndre Fischer if (useChunkedEncoding()) 7110e20387SAndre Fischer { 7210e20387SAndre Fischer // Activate chunked encoding. 7310e20387SAndre Fischer serf_bucket_request_set_CL(pRequestBucket, SERF_UNKNOWN_LENGTH); 7410e20387SAndre Fischer } 7510e20387SAndre Fischer else 7610e20387SAndre Fischer { 7710e20387SAndre Fischer // Deactivate chunked encoding by setting the length. 7810e20387SAndre Fischer serf_bucket_request_set_CL(pRequestBucket, nLength); 7910e20387SAndre Fischer } 8010e20387SAndre Fischer } 8110e20387SAndre Fischer } 8210e20387SAndre Fischer 8310e20387SAndre Fischer 84e9ff7e89SOliver-Rainer Wittmann void SerfRequestProcessorImpl::setRequestHeaders( serf_bucket_t* inoutSerfHeaderBucket ) 85e9ff7e89SOliver-Rainer Wittmann { 86*6601ee31SAriel Constenla-Haile bool bHasUserAgent( false ); 87e9ff7e89SOliver-Rainer Wittmann DAVRequestHeaders::const_iterator aHeaderIter( mrRequestHeaders.begin() ); 88e9ff7e89SOliver-Rainer Wittmann const DAVRequestHeaders::const_iterator aEnd( mrRequestHeaders.end() ); 89e9ff7e89SOliver-Rainer Wittmann 90e9ff7e89SOliver-Rainer Wittmann while ( aHeaderIter != aEnd ) 91e9ff7e89SOliver-Rainer Wittmann { 92e9ff7e89SOliver-Rainer Wittmann const rtl::OString aHeader = rtl::OUStringToOString( (*aHeaderIter).first, 93e9ff7e89SOliver-Rainer Wittmann RTL_TEXTENCODING_UTF8 ); 94e9ff7e89SOliver-Rainer Wittmann const rtl::OString aValue = rtl::OUStringToOString( (*aHeaderIter).second, 95e9ff7e89SOliver-Rainer Wittmann RTL_TEXTENCODING_UTF8 ); 96e9ff7e89SOliver-Rainer Wittmann 97*6601ee31SAriel Constenla-Haile OSL_TRACE( "Request Header - \"%s: %s\"", aHeader.getStr(), aValue.getStr() ); 98*6601ee31SAriel Constenla-Haile if ( !bHasUserAgent ) 99*6601ee31SAriel Constenla-Haile bHasUserAgent = aHeaderIter->first.equalsAsciiL( 100*6601ee31SAriel Constenla-Haile RTL_CONSTASCII_STRINGPARAM( "User-Agent" ) ); 101*6601ee31SAriel Constenla-Haile 102e9ff7e89SOliver-Rainer Wittmann serf_bucket_headers_set( inoutSerfHeaderBucket, 103e9ff7e89SOliver-Rainer Wittmann aHeader.getStr(), 104e9ff7e89SOliver-Rainer Wittmann aValue.getStr() ); 105e9ff7e89SOliver-Rainer Wittmann 106e9ff7e89SOliver-Rainer Wittmann ++aHeaderIter; 107e9ff7e89SOliver-Rainer Wittmann } 108e9ff7e89SOliver-Rainer Wittmann 109*6601ee31SAriel Constenla-Haile if ( !bHasUserAgent ) 110*6601ee31SAriel Constenla-Haile { 111*6601ee31SAriel Constenla-Haile const rtl::OUString &rUserAgent = WebDAVUserAgent::get(); 112*6601ee31SAriel Constenla-Haile serf_bucket_headers_set( inoutSerfHeaderBucket, 113*6601ee31SAriel Constenla-Haile "User-Agent", 114*6601ee31SAriel Constenla-Haile rtl::OUStringToOString( rUserAgent, RTL_TEXTENCODING_UTF8 ).getStr() ); 115*6601ee31SAriel Constenla-Haile } 116*6601ee31SAriel Constenla-Haile 117e9ff7e89SOliver-Rainer Wittmann serf_bucket_headers_set( inoutSerfHeaderBucket, "Accept-Encoding", "gzip"); 118e9ff7e89SOliver-Rainer Wittmann } 119e9ff7e89SOliver-Rainer Wittmann 12049989859SOliver-Rainer Wittmann bool SerfRequestProcessorImpl::processSerfResponseBucket( serf_request_t * /*inSerfRequest*/, 12149989859SOliver-Rainer Wittmann serf_bucket_t * inSerfResponseBucket, 12249989859SOliver-Rainer Wittmann apr_pool_t * /*inAprPool*/, 12349989859SOliver-Rainer Wittmann apr_status_t & outStatus ) 12449989859SOliver-Rainer Wittmann { 12549989859SOliver-Rainer Wittmann const char* data; 12649989859SOliver-Rainer Wittmann apr_size_t len; 12749989859SOliver-Rainer Wittmann 12849989859SOliver-Rainer Wittmann while (1) { 12949989859SOliver-Rainer Wittmann outStatus = serf_bucket_read(inSerfResponseBucket, 8096, &data, &len); 13049989859SOliver-Rainer Wittmann if (SERF_BUCKET_READ_ERROR(outStatus)) 13149989859SOliver-Rainer Wittmann { 13249989859SOliver-Rainer Wittmann return true; 13349989859SOliver-Rainer Wittmann } 13449989859SOliver-Rainer Wittmann 13549989859SOliver-Rainer Wittmann if ( len > 0 ) 13649989859SOliver-Rainer Wittmann { 13749989859SOliver-Rainer Wittmann processChunkOfResponseData( data, len ); 13849989859SOliver-Rainer Wittmann } 13949989859SOliver-Rainer Wittmann 14049989859SOliver-Rainer Wittmann /* are we done yet? */ 14149989859SOliver-Rainer Wittmann if (APR_STATUS_IS_EOF(outStatus)) 14249989859SOliver-Rainer Wittmann { 14349989859SOliver-Rainer Wittmann handleEndOfResponseData( inSerfResponseBucket ); 14449989859SOliver-Rainer Wittmann 14549989859SOliver-Rainer Wittmann outStatus = APR_EOF; 14649989859SOliver-Rainer Wittmann return true; 14749989859SOliver-Rainer Wittmann } 14849989859SOliver-Rainer Wittmann 14949989859SOliver-Rainer Wittmann /* have we drained the response so far? */ 15049989859SOliver-Rainer Wittmann if ( APR_STATUS_IS_EAGAIN( outStatus ) ) 15149989859SOliver-Rainer Wittmann { 15249989859SOliver-Rainer Wittmann return false; 15349989859SOliver-Rainer Wittmann } 15449989859SOliver-Rainer Wittmann } 15549989859SOliver-Rainer Wittmann 15649989859SOliver-Rainer Wittmann /* NOTREACHED */ 15749989859SOliver-Rainer Wittmann return true; 15849989859SOliver-Rainer Wittmann } 15949989859SOliver-Rainer Wittmann 1608590a0fdSAndre Fischer } // namespace http_dav_ucp 1618590a0fdSAndre Fischer 162