1 /* Copyright 2002-2004 Justin Erenkrantz and Greg Stein
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <apr_pools.h>
17 #include <apr_strings.h>
20 #include "serf_bucket_util.h"
26 serf_bucket_t *headers;
31 #define LENGTH_UNKNOWN ((apr_int64_t)-1)
34 serf_bucket_t *serf_bucket_request_create(
38 serf_bucket_alloc_t *allocator)
40 request_context_t *ctx;
42 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
45 ctx->headers = serf_bucket_headers_create(allocator);
47 ctx->len = LENGTH_UNKNOWN;
49 return serf_bucket_create(&serf_bucket_type_request, allocator, ctx);
52 void serf_bucket_request_set_CL(
53 serf_bucket_t *bucket,
56 request_context_t *ctx = (request_context_t *)bucket->data;
61 serf_bucket_t *serf_bucket_request_get_headers(
62 serf_bucket_t *bucket)
64 return ((request_context_t *)bucket->data)->headers;
67 void serf_bucket_request_set_root(
68 serf_bucket_t *bucket,
71 request_context_t *ctx = (request_context_t *)bucket->data;
73 /* If uri is already absolute, don't change it. */
74 if (ctx->uri[0] != '/')
77 /* If uri is '/' replace it with root_url. */
78 if (ctx->uri[1] == '\0')
82 apr_pstrcat(serf_bucket_allocator_get_pool(bucket->allocator),
88 static void serialize_data(serf_bucket_t *bucket)
90 request_context_t *ctx = bucket->data;
91 serf_bucket_t *new_bucket;
96 /* Serialize the request-line and headers into one mother string,
97 * and wrap a bucket around it.
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;
108 /* ### pool allocation! */
109 new_data = apr_pstrcatv(serf_bucket_allocator_get_pool(bucket->allocator),
112 /* Create a new bucket for this string. A free function isn't needed
113 * since the string is residing in a pool.
115 new_bucket = SERF_BUCKET_SIMPLE_STRING_LEN(new_data, nbytes,
118 /* Build up the new bucket structure.
120 * Note that self needs to become an aggregate bucket so that a
121 * pointer to self still represents the "right" data.
123 serf_bucket_aggregate_become(bucket);
125 /* Insert the two buckets. */
126 serf_bucket_aggregate_append(bucket, new_bucket);
127 serf_bucket_aggregate_append(bucket, ctx->headers);
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) {
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);
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);
145 /* Our private context is no longer needed, and is not referred to by
146 * any existing bucket. Toss it.
148 serf_bucket_mem_free(bucket->allocator, ctx);
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)
155 /* Seralize our private data into a new aggregate bucket. */
156 serialize_data(bucket);
158 /* Delegate to the "new" aggregate bucket to do the read. */
159 return serf_bucket_read(bucket, requested, data, len);
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)
166 /* Seralize our private data into a new aggregate bucket. */
167 serialize_data(bucket);
169 /* Delegate to the "new" aggregate bucket to do the readline. */
170 return serf_bucket_readline(bucket, acceptable, found, data, len);
173 static apr_status_t serf_request_read_iovec(serf_bucket_t *bucket,
174 apr_size_t requested,
179 /* Seralize our private data into a new aggregate bucket. */
180 serialize_data(bucket);
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);
187 static apr_status_t serf_request_peek(serf_bucket_t *bucket,
191 /* Seralize our private data into a new aggregate bucket. */
192 serialize_data(bucket);
194 /* Delegate to the "new" aggregate bucket to do the peek. */
195 return serf_bucket_peek(bucket, data, len);
198 void serf_bucket_request_become(
199 serf_bucket_t *bucket,
204 request_context_t *ctx;
206 ctx = serf_bucket_mem_alloc(bucket->allocator, sizeof(*ctx));
207 ctx->method = method;
209 ctx->headers = serf_bucket_headers_create(bucket->allocator);
212 bucket->type = &serf_bucket_type_request;
215 /* The allocator remains the same. */
218 const serf_bucket_type_t serf_bucket_type_request = {
221 serf_request_readline,
222 serf_request_read_iovec,
223 serf_default_read_for_sendfile,
224 serf_default_read_bucket,
226 serf_default_destroy_and_data,