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     }
82 
83     // create serf request
84     serf_bucket_t *req_bkt = serf_request_bucket_request_create( inSerfRequest,
85                                                                  "POST",
86                                                                  getPathStr(),
87                                                                  body_bkt,
88                                                                  serf_request_get_alloc( inSerfRequest ) );
89     handleChunkedEncoding(req_bkt, mnPostDataLen);
90 
91     // set request header fields
92     serf_bucket_t* hdrs_bkt = serf_bucket_request_get_headers( req_bkt );
93     // general header fields provided by caller
94     setRequestHeaders( hdrs_bkt );
95 
96     // request specific header fields
97     if ( mpContentType != 0 )
98     {
99         serf_bucket_headers_set( hdrs_bkt, "Content-Type", mpContentType );
100     }
101     if ( mpReferer != 0 )
102     {
103         serf_bucket_headers_set( hdrs_bkt, "Referer", mpReferer );
104     }
105 
106     return req_bkt;
107 }
108 
109 void SerfPostReqProcImpl::processChunkOfResponseData( const char* data,
110                                                       apr_size_t len )
111 {
112     if ( xInputStream.is() )
113     {
114         xInputStream->AddToStream( data, len );
115     }
116     else if ( xOutputStream.is() )
117     {
118         const uno::Sequence< sal_Int8 > aDataSeq( (sal_Int8 *)data, len );
119         xOutputStream->writeBytes( aDataSeq );
120     }
121 }
122 
123 void SerfPostReqProcImpl::handleEndOfResponseData( serf_bucket_t * /*inSerfResponseBucket*/ )
124 {
125     // nothing to do;
126 }
127 
128 } // namespace http_dav_ucp
129