]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/serf/buckets/request_buckets.c
- Move videodev headers from compat/linux to contrib/v4l (cp from vendor and
[FreeBSD/FreeBSD.git] / contrib / serf / buckets / request_buckets.c
1 /* Copyright 2002-2004 Justin Erenkrantz and Greg Stein
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <apr_pools.h>
17 #include <apr_strings.h>
18
19 #include "serf.h"
20 #include "serf_bucket_util.h"
21
22
23 typedef struct {
24     const char *method;
25     const char *uri;
26     serf_bucket_t *headers;
27     serf_bucket_t *body;
28     apr_int64_t len;
29 } request_context_t;
30
31 #define LENGTH_UNKNOWN ((apr_int64_t)-1)
32
33
34 serf_bucket_t *serf_bucket_request_create(
35     const char *method,
36     const char *URI,
37     serf_bucket_t *body,
38     serf_bucket_alloc_t *allocator)
39 {
40     request_context_t *ctx;
41
42     ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
43     ctx->method = method;
44     ctx->uri = URI;
45     ctx->headers = serf_bucket_headers_create(allocator);
46     ctx->body = body;
47     ctx->len = LENGTH_UNKNOWN;
48
49     return serf_bucket_create(&serf_bucket_type_request, allocator, ctx);
50 }
51
52 void serf_bucket_request_set_CL(
53     serf_bucket_t *bucket,
54     apr_int64_t len)
55 {
56     request_context_t *ctx = (request_context_t *)bucket->data;
57
58     ctx->len = len;
59 }
60
61 serf_bucket_t *serf_bucket_request_get_headers(
62     serf_bucket_t *bucket)
63 {
64     return ((request_context_t *)bucket->data)->headers;
65 }
66
67 void serf_bucket_request_set_root(
68     serf_bucket_t *bucket,
69     const char *root_url)
70 {
71     request_context_t *ctx = (request_context_t *)bucket->data;
72
73     /* If uri is already absolute, don't change it. */
74     if (ctx->uri[0] != '/')
75         return;
76
77     /* If uri is '/' replace it with root_url. */
78     if (ctx->uri[1] == '\0')
79         ctx->uri = root_url;
80     else
81         ctx->uri =
82             apr_pstrcat(serf_bucket_allocator_get_pool(bucket->allocator),
83                         root_url,
84                         ctx->uri,
85                         NULL);
86 }
87
88 static void serialize_data(serf_bucket_t *bucket)
89 {
90     request_context_t *ctx = bucket->data;
91     serf_bucket_t *new_bucket;
92     const char *new_data;
93     struct iovec iov[4];
94     apr_size_t nbytes;
95
96     /* Serialize the request-line and headers into one mother string,
97      * and wrap a bucket around it.
98      */
99     iov[0].iov_base = (char*)ctx->method;
100     iov[0].iov_len = strlen(ctx->method);
101     iov[1].iov_base = " ";
102     iov[1].iov_len = sizeof(" ") - 1;
103     iov[2].iov_base = (char*)ctx->uri;
104     iov[2].iov_len = strlen(ctx->uri);
105     iov[3].iov_base = " HTTP/1.1\r\n";
106     iov[3].iov_len = sizeof(" HTTP/1.1\r\n") - 1;
107
108     /* ### pool allocation! */
109     new_data = apr_pstrcatv(serf_bucket_allocator_get_pool(bucket->allocator),
110                             iov, 4, &nbytes);
111
112     /* Create a new bucket for this string. A free function isn't needed
113      * since the string is residing in a pool.
114      */
115     new_bucket = SERF_BUCKET_SIMPLE_STRING_LEN(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 void serf_bucket_request_become(
199     serf_bucket_t *bucket,
200     const char *method,
201     const char *uri,
202     serf_bucket_t *body)
203 {
204     request_context_t *ctx;
205
206     ctx = serf_bucket_mem_alloc(bucket->allocator, sizeof(*ctx));
207     ctx->method = method;
208     ctx->uri = uri;
209     ctx->headers = serf_bucket_headers_create(bucket->allocator);
210     ctx->body = body;
211
212     bucket->type = &serf_bucket_type_request;
213     bucket->data = ctx;
214
215     /* The allocator remains the same. */
216 }
217
218 const serf_bucket_type_t serf_bucket_type_request = {
219     "REQUEST",
220     serf_request_read,
221     serf_request_readline,
222     serf_request_read_iovec,
223     serf_default_read_for_sendfile,
224     serf_default_read_bucket,
225     serf_request_peek,
226     serf_default_destroy_and_data,
227 };
228