]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/serf/buckets/simple_buckets.c
Merge ACPICA 20130725.
[FreeBSD/FreeBSD.git] / contrib / serf / buckets / simple_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
18 #include "serf.h"
19 #include "serf_bucket_util.h"
20
21
22 typedef struct {
23     const char *original;
24     const char *current;
25     apr_size_t remaining;
26
27     serf_simple_freefunc_t freefunc;
28     void *baton;
29
30 } simple_context_t;
31
32
33 static void free_copied_data(void *baton, const char *data)
34 {
35     serf_bucket_mem_free(baton, (char*)data);
36 }
37
38 serf_bucket_t *serf_bucket_simple_create(
39     const char *data,
40     apr_size_t len,
41     serf_simple_freefunc_t freefunc,
42     void *freefunc_baton,
43     serf_bucket_alloc_t *allocator)
44 {
45     simple_context_t *ctx;
46
47     ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
48     ctx->original = ctx->current = data;
49     ctx->remaining = len;
50     ctx->freefunc = freefunc;
51     ctx->baton = freefunc_baton;
52
53     return serf_bucket_create(&serf_bucket_type_simple, allocator, ctx);
54 }
55
56 serf_bucket_t *serf_bucket_simple_copy_create(
57     const char *data, apr_size_t len,
58     serf_bucket_alloc_t *allocator)
59 {
60     simple_context_t *ctx;
61
62     ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
63
64     ctx->original = ctx->current = serf_bucket_mem_alloc(allocator, len);
65     memcpy((char*)ctx->original, data, len);
66
67     ctx->remaining = len;
68     ctx->freefunc = free_copied_data;
69     ctx->baton = allocator;
70
71     return serf_bucket_create(&serf_bucket_type_simple, allocator, ctx);
72 }
73
74 static apr_status_t serf_simple_read(serf_bucket_t *bucket,
75                                      apr_size_t requested,
76                                      const char **data, apr_size_t *len)
77 {
78     simple_context_t *ctx = bucket->data;
79
80     if (requested == SERF_READ_ALL_AVAIL || requested > ctx->remaining)
81         requested = ctx->remaining;
82
83     *data = ctx->current;
84     *len = requested;
85
86     ctx->current += requested;
87     ctx->remaining -= requested;
88
89     return ctx->remaining ? APR_SUCCESS : APR_EOF;
90 }
91
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)
95 {
96     simple_context_t *ctx = bucket->data;
97
98     /* Returned data will be from current position. */
99     *data = ctx->current;
100     serf_util_readline(&ctx->current, &ctx->remaining, acceptable, found);
101
102     /* See how much ctx->current moved forward. */
103     *len = ctx->current - *data;
104
105     return ctx->remaining ? APR_SUCCESS : APR_EOF;
106 }
107
108 static apr_status_t serf_simple_peek(serf_bucket_t *bucket,
109                                      const char **data,
110                                      apr_size_t *len)
111 {
112     simple_context_t *ctx = bucket->data;
113
114     /* return whatever we have left */
115     *data = ctx->current;
116     *len = ctx->remaining;
117
118     /* we returned everything this bucket will ever hold */
119     return APR_EOF;
120 }
121
122 static void serf_simple_destroy(serf_bucket_t *bucket)
123 {
124     simple_context_t *ctx = bucket->data;
125
126     if (ctx->freefunc)
127         (*ctx->freefunc)(ctx->baton, ctx->original);
128
129     serf_default_destroy_and_data(bucket);
130 }
131
132
133 const serf_bucket_type_t serf_bucket_type_simple = {
134     "SIMPLE",
135     serf_simple_read,
136     serf_simple_readline,
137     serf_default_read_iovec,
138     serf_default_read_for_sendfile,
139     serf_default_read_bucket,
140     serf_simple_peek,
141     serf_simple_destroy,
142 };