OSDN Git Service

* Added serf library.
[modchxj/mod_chxj.git] / src / 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_DECLARE(serf_bucket_t *) serf_bucket_simple_create(
39     const char *data, apr_size_t len,
40     serf_simple_freefunc_t freefunc,
41     void *freefunc_baton,
42     serf_bucket_alloc_t *allocator)
43 {
44     simple_context_t *ctx;
45
46     ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
47     ctx->original = ctx->current = data;
48     ctx->remaining = len;
49     ctx->freefunc = freefunc;
50     ctx->baton = freefunc_baton;
51
52     return serf_bucket_create(&serf_bucket_type_simple, allocator, ctx);
53 }
54
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)
58 {
59     simple_context_t *ctx;
60
61     ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
62
63     ctx->original = ctx->current = serf_bucket_mem_alloc(allocator, len);
64     memcpy((char*)ctx->original, data, len);
65
66     ctx->remaining = len;
67     ctx->freefunc = free_copied_data;
68     ctx->baton = allocator;
69
70     return serf_bucket_create(&serf_bucket_type_simple, allocator, ctx);
71 }
72
73 static apr_status_t serf_simple_read(serf_bucket_t *bucket,
74                                      apr_size_t requested,
75                                      const char **data, apr_size_t *len)
76 {
77     simple_context_t *ctx = bucket->data;
78
79     if (requested == SERF_READ_ALL_AVAIL || requested > ctx->remaining)
80         requested = ctx->remaining;
81
82     *data = ctx->current;
83     *len = requested;
84
85     ctx->current += requested;
86     ctx->remaining -= requested;
87
88     return ctx->remaining ? APR_SUCCESS : APR_EOF;
89 }
90
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)
94 {
95     /* ### need our utility function... */
96     return APR_ENOTIMPL;
97 }
98
99 static apr_status_t serf_simple_peek(serf_bucket_t *bucket,
100                                      const char **data,
101                                      apr_size_t *len)
102 {
103     simple_context_t *ctx = bucket->data;
104
105     /* return whatever we have left */
106     *data = ctx->current;
107     *len = ctx->remaining;
108
109     /* we returned everything this bucket will ever hold */
110     return APR_EOF;
111 }
112
113 static void serf_simple_destroy(serf_bucket_t *bucket)
114 {
115     simple_context_t *ctx = bucket->data;
116
117     if (ctx->freefunc)
118         (*ctx->freefunc)(ctx->baton, ctx->original);
119
120     serf_default_destroy_and_data(bucket);
121 }
122
123 SERF_DECLARE_DATA const serf_bucket_type_t serf_bucket_type_simple = {
124     "SIMPLE",
125     serf_simple_read,
126     serf_simple_readline,
127     serf_default_read_iovec,
128     serf_default_read_for_sendfile,
129     serf_default_read_bucket,
130     serf_simple_peek,
131     serf_simple_destroy,
132 };