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>
19 #include "serf_bucket_util.h"
27 serf_simple_freefunc_t freefunc;
33 static void free_copied_data(void *baton, const char *data)
35 serf_bucket_mem_free(baton, (char*)data);
38 serf_bucket_t *serf_bucket_simple_create(
41 serf_simple_freefunc_t freefunc,
43 serf_bucket_alloc_t *allocator)
45 simple_context_t *ctx;
47 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
48 ctx->original = ctx->current = data;
50 ctx->freefunc = freefunc;
51 ctx->baton = freefunc_baton;
53 return serf_bucket_create(&serf_bucket_type_simple, allocator, ctx);
56 serf_bucket_t *serf_bucket_simple_copy_create(
57 const char *data, apr_size_t len,
58 serf_bucket_alloc_t *allocator)
60 simple_context_t *ctx;
62 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
64 ctx->original = ctx->current = serf_bucket_mem_alloc(allocator, len);
65 memcpy((char*)ctx->original, data, len);
68 ctx->freefunc = free_copied_data;
69 ctx->baton = allocator;
71 return serf_bucket_create(&serf_bucket_type_simple, allocator, ctx);
74 static apr_status_t serf_simple_read(serf_bucket_t *bucket,
76 const char **data, apr_size_t *len)
78 simple_context_t *ctx = bucket->data;
80 if (requested == SERF_READ_ALL_AVAIL || requested > ctx->remaining)
81 requested = ctx->remaining;
86 ctx->current += requested;
87 ctx->remaining -= requested;
89 return ctx->remaining ? APR_SUCCESS : APR_EOF;
92 static apr_status_t serf_simple_readline(serf_bucket_t *bucket,
93 int acceptable, int *found,
94 const char **data, apr_size_t *len)
96 simple_context_t *ctx = bucket->data;
98 /* Returned data will be from current position. */
100 serf_util_readline(&ctx->current, &ctx->remaining, acceptable, found);
102 /* See how much ctx->current moved forward. */
103 *len = ctx->current - *data;
105 return ctx->remaining ? APR_SUCCESS : APR_EOF;
108 static apr_status_t serf_simple_peek(serf_bucket_t *bucket,
112 simple_context_t *ctx = bucket->data;
114 /* return whatever we have left */
115 *data = ctx->current;
116 *len = ctx->remaining;
118 /* we returned everything this bucket will ever hold */
122 static void serf_simple_destroy(serf_bucket_t *bucket)
124 simple_context_t *ctx = bucket->data;
127 (*ctx->freefunc)(ctx->baton, ctx->original);
129 serf_default_destroy_and_data(bucket);
133 const serf_bucket_type_t serf_bucket_type_simple = {
136 serf_simple_readline,
137 serf_default_read_iovec,
138 serf_default_read_for_sendfile,
139 serf_default_read_bucket,