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
#ifndef SERF_BUCKET_UTIL_H
17
#define SERF_BUCKET_UTIL_H
20
* @file serf_bucket_util.h
21
* @brief This header defines a set of functions and other utilities
22
* for implementing buckets. It is not needed by users of the bucket
34
* Basic bucket creation function.
36
* This function will create a bucket of @a type, allocating the necessary
37
* memory from @a allocator. The @a data bucket-private information will
38
* be stored into the bucket.
40
serf_bucket_t *serf_bucket_create(
41
const serf_bucket_type_t *type,
42
serf_bucket_alloc_t *allocator,
46
* Default implementation of the @see read_iovec functionality.
48
* This function will use the @see read function to get a block of memory,
49
* then return it in the iovec.
51
apr_status_t serf_default_read_iovec(
52
serf_bucket_t *bucket,
59
* Default implementation of the @see read_for_sendfile functionality.
61
* This function will use the @see read function to get a block of memory,
62
* then return it as a header. No file will be returned.
64
apr_status_t serf_default_read_for_sendfile(
65
serf_bucket_t *bucket,
73
* Default implementation of the @see read_bucket functionality.
75
* This function will always return NULL, indicating that the @a type
76
* of bucket cannot be found within @a bucket.
78
serf_bucket_t *serf_default_read_bucket(
79
serf_bucket_t *bucket,
80
const serf_bucket_type_t *type);
83
* Default implementation of the @see destroy functionality.
85
* This function will return the @a bucket to its allcoator.
87
void serf_default_destroy(
88
serf_bucket_t *bucket);
92
* Default implementation of the @see destroy functionality.
94
* This function will return the @a bucket, and the data member to its
97
void serf_default_destroy_and_data(
98
serf_bucket_t *bucket);
102
* Allocate @a size bytes of memory using @a allocator.
104
* Returns NULL of the requested memory size could not be allocated.
106
void *serf_bucket_mem_alloc(
107
serf_bucket_alloc_t *allocator,
111
* Allocate @a size bytes of memory using @a allocator and set all of the
114
* Returns NULL of the requested memory size could not be allocated.
116
void *serf_bucket_mem_calloc(
117
serf_bucket_alloc_t *allocator,
121
* Free the memory at @a block, returning it to @a allocator.
123
void serf_bucket_mem_free(
124
serf_bucket_alloc_t *allocator,
129
* Analogous to apr_pstrmemdup, using a bucket allocator instead.
131
char *serf_bstrmemdup(
132
serf_bucket_alloc_t *allocator,
137
* Analogous to apr_pmemdup, using a bucket allocator instead.
140
serf_bucket_alloc_t *allocator,
145
* Analogous to apr_pstrdup, using a bucket allocator instead.
148
serf_bucket_alloc_t *allocator,
153
* Read data up to a newline.
155
* @a acceptable contains the allowed forms of a newline, and @a found
156
* will return the particular newline type that was found. If a newline
157
* is not found, then SERF_NEWLINE_NONE will be placed in @a found.
159
* @a data should contain a pointer to the data to be scanned. @a len
160
* should specify the length of that data buffer. On exit, @a data will
161
* be advanced past the newline, and @a len will specify the remaining
162
* amount of data in the buffer.
164
* Given this pattern of behavior, the caller should store the initial
165
* value of @a data as the line start. The difference between the
166
* returned value of @a data and the saved start is the length of the
169
* Note that the newline character(s) will remain within the buffer.
170
* This function scans at a byte level for the newline characters. Thus,
171
* the data buffer may contain NUL characters. As a corollary, this
172
* function only works on 8-bit character encodings.
174
* If the data is fully consumed (@a len gets set to zero) and a CR
175
* character is found at the end and the CRLF sequence is allowed, then
176
* this function may store SERF_NEWLINE_CRLF_SPLIT into @a found. The
177
* caller should take particular consideration for the CRLF sequence
178
* that may be split across data buffer boundaries.
180
void serf_util_readline(
187
/** The buffer size used within @see serf_databuf_t. */
188
#define SERF_DATABUF_BUFSIZE 8000
190
/** Callback function which is used to refill the data buffer.
192
* The function takes @a baton, which is the @see read_baton value
193
* from the serf_databuf_t structure. Data should be placed into
194
* a buffer specified by @a buf, which is @a bufsize bytes long.
195
* The amount of data read should be returned in @a len.
197
* APR_EOF should be returned if no more data is available. APR_EAGAIN
198
* should be returned, rather than blocking. In both cases, @a buf
199
* should be filled in and @a len set, as appropriate.
201
typedef apr_status_t (*serf_databuf_reader_t)(
208
* This structure is used as an intermediate data buffer for some "external"
209
* source of data. It works as a scratch pad area for incoming data to be
210
* stored, and then returned as a ptr/len pair by the bucket read functions.
212
* This structure should be initialized by calling @see serf_databuf_init.
213
* Users should not bother to zero the structure beforehand.
216
/** The current data position within the buffer. */
219
/** Amount of data remaining in the buffer. */
220
apr_size_t remaining;
222
/** Callback function. */
223
serf_databuf_reader_t read;
225
/** A baton to hold context-specific data. */
228
/** Records the status from the last @see read operation. */
231
/** Holds the data until it can be returned. */
232
char buf[SERF_DATABUF_BUFSIZE];
237
* Initialize the @see serf_databuf_t structure specified by @a databuf.
239
void serf_databuf_init(
240
serf_databuf_t *databuf);
243
* Implement a bucket-style read function from the @see serf_databuf_t
244
* structure given by @a databuf.
246
* The @a requested, @a data, and @a len fields are interpreted and used
247
* as in the read function of @see serf_bucket_t.
249
apr_status_t serf_databuf_read(
250
serf_databuf_t *databuf,
251
apr_size_t requested,
256
* Implement a bucket-style readline function from the @see serf_databuf_t
257
* structure given by @a databuf.
259
* The @a acceptable, @a found, @a data, and @a len fields are interpreted
260
* and used as in the read function of @see serf_bucket_t.
262
apr_status_t serf_databuf_readline(
263
serf_databuf_t *databuf,
270
* Implement a bucket-style peek function from the @see serf_databuf_t
271
* structure given by @a databuf.
273
* The @a data, and @a len fields are interpreted and used as in the
274
* peek function of @see serf_bucket_t.
276
apr_status_t serf_databuf_peek(
277
serf_databuf_t *databuf,
286
#endif /* !SERF_BUCKET_UTIL_H */