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"
23 serf_bucket_t *stream;
28 SERF_DECLARE(serf_bucket_t *) serf_bucket_limit_create(
29 serf_bucket_t *stream, apr_size_t len, serf_bucket_alloc_t *allocator)
33 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
37 return serf_bucket_create(&serf_bucket_type_limit, allocator, ctx);
40 static apr_status_t serf_limit_read(serf_bucket_t *bucket,
42 const char **data, apr_size_t *len)
44 limit_context_t *ctx = bucket->data;
47 if (!ctx->remaining) {
52 if (requested == SERF_READ_ALL_AVAIL || requested > ctx->remaining)
53 requested = ctx->remaining;
55 status = serf_bucket_read(ctx->stream, requested, data, len);
57 if (!SERF_BUCKET_READ_ERROR(status)) {
58 ctx->remaining -= *len;
61 /* If we have met our limit and don't have a status, return EOF. */
62 if (!ctx->remaining && !status) {
69 static apr_status_t serf_limit_readline(serf_bucket_t *bucket,
70 int acceptable, int *found,
71 const char **data, apr_size_t *len)
73 limit_context_t *ctx = bucket->data;
76 if (!ctx->remaining) {
81 status = serf_bucket_readline(ctx->stream, acceptable, found, data, len);
83 if (!SERF_BUCKET_READ_ERROR(status)) {
84 ctx->remaining -= *len;
87 /* If we have met our limit and don't have a status, return EOF. */
88 if (!ctx->remaining && !status) {
95 static apr_status_t serf_limit_peek(serf_bucket_t *bucket,
99 limit_context_t *ctx = bucket->data;
101 return serf_bucket_peek(ctx->stream, data, len);
104 static void serf_limit_destroy(serf_bucket_t *bucket)
106 limit_context_t *ctx = bucket->data;
108 serf_bucket_destroy(ctx->stream);
110 serf_default_destroy_and_data(bucket);
113 SERF_DECLARE_DATA const serf_bucket_type_t serf_bucket_type_limit = {
117 serf_default_read_iovec,
118 serf_default_read_for_sendfile,
119 serf_default_read_bucket,