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* inLockToken,
39                                           const char* inContentType,
40                                           const char* inReferer,
41                                           const com::sun::star::uno::Reference< SerfInputStream > & xioInStrm )
42     : SerfRequestProcessorImpl( inPath, inRequestHeaders )
43     , mpPostData( inData )
44     , mnPostDataLen( inDataLen )
45     , mpLockToken( inLockToken )
46     , mpContentType( inContentType )
47     , mpReferer( inReferer )
48     , xInputStream( xioInStrm )
49     , xOutputStream()
50 {
51 }
52 
53 SerfPostReqProcImpl::SerfPostReqProcImpl( const char* inPath,
54                                           const DAVRequestHeaders& inRequestHeaders,
55                                           const char* inData,
56                                           apr_size_t inDataLen,
57                                           const char* inLockToken,
58                                           const char* inContentType,
59                                           const char* inReferer,
60                                           const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > & xioOutStrm )
61     : SerfRequestProcessorImpl( inPath, inRequestHeaders )
62     , mpPostData( inData )
63     , mnPostDataLen( inDataLen )
64     , mpLockToken( inLockToken )
65     , mpContentType( inContentType )
66     , mpReferer( inReferer )
67     , xInputStream()
68     , xOutputStream( xioOutStrm )
69 {
70 }
71 
72 SerfPostReqProcImpl::~SerfPostReqProcImpl()
73 {
74 }
75 
76 serf_bucket_t * SerfPostReqProcImpl::createSerfRequestBucket( serf_request_t * inSerfRequest )
77 {
78     serf_bucket_alloc_t* pSerfBucketAlloc = serf_request_get_alloc( inSerfRequest );
79 
80     // create body bucket
81     serf_bucket_t* body_bkt = 0;
82     if ( mpPostData != 0 && mnPostDataLen > 0 )
83     {
84         body_bkt = SERF_BUCKET_SIMPLE_STRING_LEN( mpPostData, mnPostDataLen, pSerfBucketAlloc );
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     handleChunkedEncoding(req_bkt, mnPostDataLen);
94 
95     // set request header fields
96     serf_bucket_t* hdrs_bkt = serf_bucket_request_get_headers( req_bkt );
97     // general header fields provided by caller
98     setRequestHeaders( hdrs_bkt );
99 
100     // request specific header fields
101     if ( mpContentType != 0 )
102     {
103         serf_bucket_headers_set( hdrs_bkt, "Content-Type", mpContentType );
104     }
105     if ( mpReferer != 0 )
106     {
107         serf_bucket_headers_set( hdrs_bkt, "Referer", mpReferer );
108     }
109     if(mpLockToken)
110     {
111         serf_bucket_headers_set( hdrs_bkt, "if", mpLockToken );
112     }
113 
114     return req_bkt;
115 }
116 
117 void SerfPostReqProcImpl::processChunkOfResponseData( const char* data,
118                                                       apr_size_t len )
119 {
120     if ( xInputStream.is() )
121     {
122         xInputStream->AddToStream( data, len );
123     }
124     else if ( xOutputStream.is() )
125     {
126         const uno::Sequence< sal_Int8 > aDataSeq( (sal_Int8 *)data, len );
127         xOutputStream->writeBytes( aDataSeq );
128     }
129 }
130 
131 void SerfPostReqProcImpl::handleEndOfResponseData( serf_bucket_t * /*inSerfResponseBucket*/ )
132 {
133     // nothing to do;
134 }
135 
136 } // namespace http_dav_ucp
137