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 // MARKER(update_precomp.py): autogen include statement, do not remove 23 #include "precompiled_ucb.hxx" 24 25 #include <SerfPostReqProcImpl.hxx> 26 27 #include <serf.h> 28 29 using namespace com::sun::star; 30 31 namespace http_dav_ucp 32 { 33 34 SerfPostReqProcImpl::SerfPostReqProcImpl( const char* inPath, 35 const DAVRequestHeaders& inRequestHeaders, 36 const char* inData, 37 apr_size_t inDataLen, 38 const char* inContentType, 39 const char* inReferer, 40 const com::sun::star::uno::Reference< SerfInputStream > & xioInStrm ) 41 : SerfRequestProcessorImpl( inPath, inRequestHeaders ) 42 , mpPostData( inData ) 43 , mnPostDataLen( inDataLen ) 44 , mpContentType( inContentType ) 45 , mpReferer( inReferer ) 46 , xInputStream( xioInStrm ) 47 , xOutputStream() 48 { 49 } 50 51 SerfPostReqProcImpl::SerfPostReqProcImpl( const char* inPath, 52 const DAVRequestHeaders& inRequestHeaders, 53 const char* inData, 54 apr_size_t inDataLen, 55 const char* inContentType, 56 const char* inReferer, 57 const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > & xioOutStrm ) 58 : SerfRequestProcessorImpl( inPath, inRequestHeaders ) 59 , mpPostData( inData ) 60 , mnPostDataLen( inDataLen ) 61 , mpContentType( inContentType ) 62 , mpReferer( inReferer ) 63 , xInputStream() 64 , xOutputStream( xioOutStrm ) 65 { 66 } 67 68 SerfPostReqProcImpl::~SerfPostReqProcImpl() 69 { 70 } 71 72 serf_bucket_t * SerfPostReqProcImpl::createSerfRequestBucket( serf_request_t * inSerfRequest ) 73 { 74 serf_bucket_alloc_t* pSerfBucketAlloc = serf_request_get_alloc( inSerfRequest ); 75 76 // create body bucket 77 serf_bucket_t* body_bkt = 0; 78 if ( mpPostData != 0 && mnPostDataLen > 0 ) 79 { 80 body_bkt = SERF_BUCKET_SIMPLE_STRING_LEN( mpPostData, mnPostDataLen, pSerfBucketAlloc ); 81 if ( useChunkedEncoding() ) 82 { 83 body_bkt = serf_bucket_chunk_create( body_bkt, pSerfBucketAlloc ); 84 } 85 } 86 87 // create serf request 88 serf_bucket_t *req_bkt = serf_request_bucket_request_create( inSerfRequest, 89 "POST", 90 getPathStr(), 91 body_bkt, 92 serf_request_get_alloc( inSerfRequest ) ); 93 94 // set request header fields 95 serf_bucket_t* hdrs_bkt = serf_bucket_request_get_headers( req_bkt ); 96 // general header fields provided by caller 97 setRequestHeaders( hdrs_bkt ); 98 99 // request specific header fields 100 if ( body_bkt != 0 ) 101 { 102 if ( useChunkedEncoding() ) 103 { 104 serf_bucket_headers_set( hdrs_bkt, "Transfer-Encoding", "chunked"); 105 } 106 serf_bucket_headers_set( hdrs_bkt, "Content-Length", 107 rtl::OUStringToOString( rtl::OUString::valueOf( (sal_Int32)mnPostDataLen ), RTL_TEXTENCODING_UTF8 ) ); 108 } 109 if ( mpContentType != 0 ) 110 { 111 serf_bucket_headers_set( hdrs_bkt, "Content-Type", mpContentType ); 112 } 113 if ( mpReferer != 0 ) 114 { 115 serf_bucket_headers_set( hdrs_bkt, "Referer", mpReferer ); 116 } 117 118 return req_bkt; 119 } 120 121 void SerfPostReqProcImpl::processChunkOfResponseData( const char* data, 122 apr_size_t len ) 123 { 124 if ( xInputStream.is() ) 125 { 126 xInputStream->AddToStream( data, len ); 127 } 128 else if ( xOutputStream.is() ) 129 { 130 const uno::Sequence< sal_Int8 > aDataSeq( (sal_Int8 *)data, len ); 131 xOutputStream->writeBytes( aDataSeq ); 132 } 133 } 134 135 void SerfPostReqProcImpl::handleEndOfResponseData( serf_bucket_t * /*inSerfResponseBucket*/ ) 136 { 137 // nothing to do; 138 } 139 140 } // namespace http_dav_ucp 141