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"
20 #include "serf_private.h"
23 serf_bucket_t *stream;
24 apr_uint64_t remaining;
28 serf_bucket_t *serf_bucket_limit_create(
29 serf_bucket_t *stream, apr_uint64_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 if (ctx->remaining <= REQUESTED_MAX) {
54 requested = (apr_size_t) ctx->remaining;
56 requested = REQUESTED_MAX;
60 status = serf_bucket_read(ctx->stream, requested, data, len);
62 if (!SERF_BUCKET_READ_ERROR(status)) {
63 ctx->remaining -= *len;
66 /* If we have met our limit and don't have a status, return EOF. */
67 if (!ctx->remaining && !status) {
74 static apr_status_t serf_limit_readline(serf_bucket_t *bucket,
75 int acceptable, int *found,
76 const char **data, apr_size_t *len)
78 limit_context_t *ctx = bucket->data;
81 if (!ctx->remaining) {
86 status = serf_bucket_readline(ctx->stream, acceptable, found, data, len);
88 if (!SERF_BUCKET_READ_ERROR(status)) {
89 ctx->remaining -= *len;
92 /* If we have met our limit and don't have a status, return EOF. */
93 if (!ctx->remaining && !status) {
100 static apr_status_t serf_limit_peek(serf_bucket_t *bucket,
104 limit_context_t *ctx = bucket->data;
106 return serf_bucket_peek(ctx->stream, data, len);
109 static void serf_limit_destroy(serf_bucket_t *bucket)
111 limit_context_t *ctx = bucket->data;
113 serf_bucket_destroy(ctx->stream);
115 serf_default_destroy_and_data(bucket);
118 const serf_bucket_type_t serf_bucket_type_limit = {
122 serf_default_read_iovec,
123 serf_default_read_for_sendfile,
124 serf_default_read_bucket,