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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
18 * ====================================================================
21 #include <apr_pools.h>
22 #include <apr_strings.h>
25 #include "serf_bucket_util.h"
31 serf_bucket_t *headers;
36 #define LENGTH_UNKNOWN ((apr_int64_t)-1)
39 serf_bucket_t *serf_bucket_request_create(
43 serf_bucket_alloc_t *allocator)
45 request_context_t *ctx;
47 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
50 ctx->headers = serf_bucket_headers_create(allocator);
52 ctx->len = LENGTH_UNKNOWN;
54 return serf_bucket_create(&serf_bucket_type_request, allocator, ctx);
57 void serf_bucket_request_set_CL(
58 serf_bucket_t *bucket,
61 request_context_t *ctx = (request_context_t *)bucket->data;
66 serf_bucket_t *serf_bucket_request_get_headers(
67 serf_bucket_t *bucket)
69 return ((request_context_t *)bucket->data)->headers;
72 void serf_bucket_request_set_root(
73 serf_bucket_t *bucket,
76 request_context_t *ctx = (request_context_t *)bucket->data;
78 /* If uri is already absolute, don't change it. */
79 if (ctx->uri[0] != '/')
82 /* If uri is '/' replace it with root_url. */
83 if (ctx->uri[1] == '\0')
87 apr_pstrcat(serf_bucket_allocator_get_pool(bucket->allocator),
93 static void serialize_data(serf_bucket_t *bucket)
95 request_context_t *ctx = bucket->data;
96 serf_bucket_t *new_bucket;
101 /* Serialize the request-line and headers into one mother string,
102 * and wrap a bucket around it.
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;
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,
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 /* 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)
202 request_context_t *ctx = bucket->data;
204 serf_bucket_destroy(ctx->headers);
207 serf_bucket_destroy(ctx->body);
209 serf_default_destroy_and_data(bucket);
212 void serf_bucket_request_become(
213 serf_bucket_t *bucket,
218 request_context_t *ctx;
220 ctx = serf_bucket_mem_alloc(bucket->allocator, sizeof(*ctx));
221 ctx->method = method;
223 ctx->headers = serf_bucket_headers_create(bucket->allocator);
226 bucket->type = &serf_bucket_type_request;
229 /* The allocator remains the same. */
232 const serf_bucket_type_t serf_bucket_type_request = {
235 serf_request_readline,
236 serf_request_read_iovec,
237 serf_default_read_for_sendfile,
238 serf_default_read_bucket,
240 serf_request_destroy,