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 <rtl/ustring.hxx>
26 
27 #include <SerfPutReqProcImpl.hxx>
28 
29 #include <serf.h>
30 
31 namespace http_dav_ucp
32 {
33 
34 SerfPutReqProcImpl::SerfPutReqProcImpl( const char* inPath,
35                                         const char* inData,
36                                         apr_size_t inDataLen )
37     : SerfRequestProcessorImpl( inPath )
38     , mpData( inData )
39     , mnDataLen( inDataLen )
40 {
41 }
42 
43 SerfPutReqProcImpl::~SerfPutReqProcImpl()
44 {
45 }
46 
47 serf_bucket_t * SerfPutReqProcImpl::createSerfRequestBucket( serf_request_t * inSerfRequest )
48 {
49     serf_bucket_alloc_t* pSerfBucketAlloc = serf_request_get_alloc( inSerfRequest );
50 
51     // create body bucket
52     serf_bucket_t* body_bkt = 0;
53     if ( mpData != 0 && mnDataLen > 0 )
54     {
55         body_bkt = SERF_BUCKET_SIMPLE_STRING_LEN( mpData, mnDataLen, pSerfBucketAlloc );
56     }
57 
58     // create serf request
59     serf_bucket_t *req_bkt = serf_request_bucket_request_create( inSerfRequest,
60                                                                  "PUT",
61                                                                  getPathStr(),
62                                                                  body_bkt,
63                                                                  serf_request_get_alloc( inSerfRequest ) );
64 
65     // TODO - correct headers
66     // set request header fields
67     serf_bucket_t* hdrs_bkt = serf_bucket_request_get_headers( req_bkt );
68     serf_bucket_headers_setn( hdrs_bkt, "User-Agent", "www.openoffice.org/ucb/" );
69     serf_bucket_headers_setn( hdrs_bkt, "Accept-Encoding", "gzip");
70 
71     // request specific header fields
72     if ( body_bkt != 0 )
73     {
74         serf_bucket_headers_setn( hdrs_bkt, "Content-Length",
75                                   rtl::OUStringToOString( rtl::OUString::valueOf( (sal_Int32)mnDataLen ), RTL_TEXTENCODING_UTF8 ) );
76     }
77 
78 
79     return req_bkt;
80 }
81 
82 
83 bool SerfPutReqProcImpl::processSerfResponseBucket( serf_request_t * /*inSerfRequest*/,
84                                                     serf_bucket_t * inSerfResponseBucket,
85                                                     apr_pool_t * /*inAprPool*/,
86                                                     apr_status_t & outStatus )
87 {
88     const char* data;
89     apr_size_t len;
90 
91     while (1) {
92         outStatus = serf_bucket_read(inSerfResponseBucket, 8096, &data, &len);
93         if (SERF_BUCKET_READ_ERROR(outStatus))
94         {
95             return true;
96         }
97 
98         /* are we done yet? */
99         if (APR_STATUS_IS_EOF(outStatus))
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