]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/serf/buckets/request_buckets.c
Update Apache Serf to 1.3.9 to support OpenSSL 1.1.1.
[FreeBSD/FreeBSD.git] / contrib / serf / buckets / request_buckets.c
1 /* ====================================================================
2  *    Licensed to the Apache Software Foundation (ASF) under one
3  *    or more contributor license agreements.  See the NOTICE file
4  *    distributed with this work for additional information
5  *    regarding copyright ownership.  The ASF licenses this file
6  *    to you under the Apache License, Version 2.0 (the
7  *    "License"); you may not use this file except in compliance
8  *    with the License.  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing,
13  *    software distributed under the License is distributed on an
14  *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  *    KIND, either express or implied.  See the License for the
16  *    specific language governing permissions and limitations
17  *    under the License.
18  * ====================================================================
19  */
20
21 #include <apr_pools.h>
22 #include <apr_strings.h>
23
24 #include "serf.h"
25 #include "serf_bucket_util.h"
26
27
28 typedef struct {
29     const char *method;
30     const char *uri;
31     serf_bucket_t *headers;
32     serf_bucket_t *body;
33     apr_int64_t len;
34 } request_context_t;
35
36 #define LENGTH_UNKNOWN ((apr_int64_t)-1)
37
38
39 serf_bucket_t *serf_bucket_request_create(
40     const char *method,
41     const char *URI,
42     serf_bucket_t *body,
43     serf_bucket_alloc_t *allocator)
44 {
45     request_context_t *ctx;
46
47     ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
48     ctx->method = method;
49     ctx->uri = URI;
50     ctx->headers = serf_bucket_headers_create(allocator);
51     ctx->body = body;
52     ctx->len = LENGTH_UNKNOWN;
53
54     return serf_bucket_create(&serf_bucket_type_request, allocator, ctx);
55 }
56
57 void serf_bucket_request_set_CL(
58     serf_bucket_t *bucket,
59     apr_int64_t len)
60 {
61     request_context_t *ctx = (request_context_t *)bucket->data;
62
63     ctx->len = len;
64 }
65
66 serf_bucket_t *serf_bucket_request_get_headers(
67     serf_bucket_t *bucket)
68 {
69     return ((request_context_t *)bucket->data)->headers;
70 }
71
72 void serf_bucket_request_set_root(
73     serf_bucket_t *bucket,
74     const char *root_url)
75 {
76     request_context_t *ctx = (request_context_t *)bucket->data;
77
78     /* If uri is already absolute, don't change it. */
79     if (ctx->uri[0] != '/')
80         return;
81
82     /* If uri is '/' replace it with root_url. */
83     if (ctx->uri[1] == '\0')
84         ctx->uri = root_url;
85     else
86         ctx->uri =
87             apr_pstrcat(serf_bucket_allocator_get_pool(bucket->allocator),
88                         root_url,
89                         ctx->uri,
90                         NULL);
91 }
92
93 static void serialize_data(serf_bucket_t *bucket)
94 {
95     request_context_t *ctx = bucket->data;
96     serf_bucket_t *new_bucket;
97     const char *new_data;
98     struct iovec iov[4];
99     apr_size_t nbytes;
100
101     /* Serialize the request-line and headers into one mother string,
102      * and wrap a bucket around it.
103      */
104     iov[0].iov_base = (char*)ctx->method;
105     iov[0].iov_len = strlen(ctx->method);
106     iov[1].iov_base = " ";
107     iov[1].iov_len = sizeof(" ") - 1;
108     iov[2].iov_base = (char*)ctx->uri;
109     iov[2].iov_len = strlen(ctx->uri);
110     iov[3].iov_base = " HTTP/1.1\r\n";
111     iov[3].iov_len = sizeof(" HTTP/1.1\r\n") - 1;
112
113     /* Create a new bucket for this string with a flat string.  */
114     new_data = serf_bstrcatv(bucket->allocator, iov, 4, &nbytes);
115     new_bucket = serf_bucket_simple_own_create(new_data, nbytes,
116                                                bucket->allocator);
117
118     /* Build up the new bucket structure.
119      *
120      * Note that self needs to become an aggregate bucket so that a
121      * pointer to self still represents the "right" data.
122      */
123     serf_bucket_aggregate_become(bucket);
124
125     /* Insert the two buckets. */
126     serf_bucket_aggregate_append(bucket, new_bucket);
127     serf_bucket_aggregate_append(bucket, ctx->headers);
128
129     /* If we know the length, then use C-L and the raw body. Otherwise,
130        use chunked encoding for the request.  */
131     if (ctx->len != LENGTH_UNKNOWN) {
132         char buf[30];
133         sprintf(buf, "%" APR_INT64_T_FMT, ctx->len);
134         serf_bucket_headers_set(ctx->headers, "Content-Length", buf);
135         if (ctx->body != NULL)
136             serf_bucket_aggregate_append(bucket, ctx->body);
137     }
138     else if (ctx->body != NULL) {
139         /* Morph the body bucket to a chunked encoding bucket for now. */
140         serf_bucket_headers_setn(ctx->headers, "Transfer-Encoding", "chunked");
141         ctx->body = serf_bucket_chunk_create(ctx->body, bucket->allocator);
142         serf_bucket_aggregate_append(bucket, ctx->body);
143     }
144
145     /* Our private context is no longer needed, and is not referred to by
146      * any existing bucket. Toss it.
147      */
148     serf_bucket_mem_free(bucket->allocator, ctx);
149 }
150
151 static apr_status_t serf_request_read(serf_bucket_t *bucket,
152                                       apr_size_t requested,
153                                       const char **data, apr_size_t *len)
154 {
155     /* Seralize our private data into a new aggregate bucket. */
156     serialize_data(bucket);
157
158     /* Delegate to the "new" aggregate bucket to do the read. */
159     return serf_bucket_read(bucket, requested, data, len);
160 }
161
162 static apr_status_t serf_request_readline(serf_bucket_t *bucket,
163                                           int acceptable, int *found,
164                                           const char **data, apr_size_t *len)
165 {
166     /* Seralize our private data into a new aggregate bucket. */
167     serialize_data(bucket);
168
169     /* Delegate to the "new" aggregate bucket to do the readline. */
170     return serf_bucket_readline(bucket, acceptable, found, data, len);
171 }
172
173 static apr_status_t serf_request_read_iovec(serf_bucket_t *bucket,
174                                             apr_size_t requested,
175                                             int vecs_size,
176                                             struct iovec *vecs,
177                                             int *vecs_used)
178 {
179     /* Seralize our private data into a new aggregate bucket. */
180     serialize_data(bucket);
181
182     /* Delegate to the "new" aggregate bucket to do the read. */
183     return serf_bucket_read_iovec(bucket, requested,
184                                   vecs_size, vecs, vecs_used);
185 }
186
187 static apr_status_t serf_request_peek(serf_bucket_t *bucket,
188                                       const char **data,
189                                       apr_size_t *len)
190 {
191     /* Seralize our private data into a new aggregate bucket. */
192     serialize_data(bucket);
193
194     /* Delegate to the "new" aggregate bucket to do the peek. */
195     return serf_bucket_peek(bucket, data, len);
196 }
197
198 /* Note that this function is only called when serialize_data()
199    hasn't been called on the bucket */
200 static void serf_request_destroy(serf_bucket_t *bucket)
201 {
202   request_context_t *ctx = bucket->data;
203
204   serf_bucket_destroy(ctx->headers);
205
206   if (ctx->body)
207     serf_bucket_destroy(ctx->body);
208
209   serf_default_destroy_and_data(bucket);
210 }
211
212 void serf_bucket_request_become(
213     serf_bucket_t *bucket,
214     const char *method,
215     const char *uri,
216     serf_bucket_t *body)
217 {
218     request_context_t *ctx;
219
220     ctx = serf_bucket_mem_alloc(bucket->allocator, sizeof(*ctx));
221     ctx->method = method;
222     ctx->uri = uri;
223     ctx->headers = serf_bucket_headers_create(bucket->allocator);
224     ctx->body = body;
225
226     bucket->type = &serf_bucket_type_request;
227     bucket->data = ctx;
228
229     /* The allocator remains the same. */
230 }
231
232 const serf_bucket_type_t serf_bucket_type_request = {
233     "REQUEST",
234     serf_request_read,
235     serf_request_readline,
236     serf_request_read_iovec,
237     serf_default_read_for_sendfile,
238     serf_default_read_bucket,
239     serf_request_peek,
240     serf_request_destroy,
241 };
242