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_DECLARE(serf_bucket_t *) serf_bucket_simple_create(
39 const char *data, apr_size_t len,
40 serf_simple_freefunc_t freefunc,
42 serf_bucket_alloc_t *allocator)
44 simple_context_t *ctx;
46 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
47 ctx->original = ctx->current = data;
49 ctx->freefunc = freefunc;
50 ctx->baton = freefunc_baton;
52 return serf_bucket_create(&serf_bucket_type_simple, allocator, ctx);
55 SERF_DECLARE(serf_bucket_t *) serf_bucket_simple_copy_create(
56 const char *data, apr_size_t len,
57 serf_bucket_alloc_t *allocator)
59 simple_context_t *ctx;
61 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
63 ctx->original = ctx->current = serf_bucket_mem_alloc(allocator, len);
64 memcpy((char*)ctx->original, data, len);
67 ctx->freefunc = free_copied_data;
68 ctx->baton = allocator;
70 return serf_bucket_create(&serf_bucket_type_simple, allocator, ctx);
73 static apr_status_t serf_simple_read(serf_bucket_t *bucket,
75 const char **data, apr_size_t *len)
77 simple_context_t *ctx = bucket->data;
79 if (requested == SERF_READ_ALL_AVAIL || requested > ctx->remaining)
80 requested = ctx->remaining;
85 ctx->current += requested;
86 ctx->remaining -= requested;
88 return ctx->remaining ? APR_SUCCESS : APR_EOF;
91 static apr_status_t serf_simple_readline(serf_bucket_t *bucket,
92 int acceptable, int *found,
93 const char **data, apr_size_t *len)
95 /* ### need our utility function... */
99 static apr_status_t serf_simple_peek(serf_bucket_t *bucket,
103 simple_context_t *ctx = bucket->data;
105 /* return whatever we have left */
106 *data = ctx->current;
107 *len = ctx->remaining;
109 /* we returned everything this bucket will ever hold */
113 static void serf_simple_destroy(serf_bucket_t *bucket)
115 simple_context_t *ctx = bucket->data;
118 (*ctx->freefunc)(ctx->baton, ctx->original);
120 serf_default_destroy_and_data(bucket);
123 SERF_DECLARE_DATA const serf_bucket_type_t serf_bucket_type_simple = {
126 serf_simple_readline,
127 serf_default_read_iovec,
128 serf_default_read_for_sendfile,
129 serf_default_read_bucket,