1
/* Copyright 2011 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"
25
/* Total number of buffer stored in the vecs var. */
27
/* Points to the first unread buffer. */
29
/* First buffer offset. */
33
serf_bucket_t *serf_bucket_iovec_create(
36
serf_bucket_alloc_t *allocator)
41
ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
42
ctx->vecs = serf_bucket_mem_alloc(allocator, len * sizeof(struct iovec));
47
/* copy all buffers to our iovec. */
48
for (i = 0; i < len; i++) {
49
ctx->vecs[i].iov_base = vecs[i].iov_base;
50
ctx->vecs[i].iov_len = vecs[i].iov_len;
53
return serf_bucket_create(&serf_bucket_type_iovec, allocator, ctx);
56
static apr_status_t serf_iovec_readline(serf_bucket_t *bucket,
57
int acceptable, int *found,
58
const char **data, apr_size_t *len)
63
static apr_status_t serf_iovec_read_iovec(serf_bucket_t *bucket,
69
iovec_context_t *ctx = bucket->data;
73
/* copy the requested amount of buffers to the provided iovec. */
74
for (; ctx->current_vec < ctx->vecs_len; ctx->current_vec++) {
75
struct iovec vec = ctx->vecs[ctx->current_vec];
78
if (requested != SERF_READ_ALL_AVAIL && requested <= 0)
80
if (*vecs_used >= vecs_size)
83
vecs[*vecs_used].iov_base = (char*)vec.iov_base + ctx->offset;
84
remaining = vec.iov_len - ctx->offset;
86
/* Less bytes requested than remaining in the current buffer. */
87
if (requested != SERF_READ_ALL_AVAIL && requested < remaining) {
88
vecs[*vecs_used].iov_len = requested;
89
ctx->offset += requested;
94
/* Copy the complete buffer. */
95
vecs[*vecs_used].iov_len = remaining;
97
if (requested != SERF_READ_ALL_AVAIL)
98
requested -= remaining;
103
if (ctx->current_vec == ctx->vecs_len && !ctx->offset)
109
static apr_status_t serf_iovec_read(serf_bucket_t *bucket,
110
apr_size_t requested,
111
const char **data, apr_size_t *len)
117
status = serf_iovec_read_iovec(bucket, requested, 1, vec, &vecs_used);
120
*data = vec[0].iov_base;
121
*len = vec[0].iov_len;
129
static apr_status_t serf_iovec_peek(serf_bucket_t *bucket,
133
iovec_context_t *ctx = bucket->data;
135
if (ctx->current_vec >= ctx->vecs_len) {
140
/* Return the first unread buffer, don't bother combining all
142
*data = ctx->vecs[ctx->current_vec].iov_base;
143
*len = ctx->vecs[ctx->current_vec].iov_len;
145
if (ctx->current_vec + 1 == ctx->vecs_len)
151
static void serf_iovec_destroy(serf_bucket_t *bucket)
153
iovec_context_t *ctx = bucket->data;
155
serf_bucket_mem_free(bucket->allocator, ctx->vecs);
156
serf_default_destroy_and_data(bucket);
160
const serf_bucket_type_t serf_bucket_type_iovec = {
164
serf_iovec_read_iovec,
165
serf_default_read_for_sendfile,
166
serf_default_read_bucket,