1 /* ====================================================================
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
18 * ====================================================================
21 #include <apr_pools.h>
24 #include "serf_bucket_util.h"
25 #include "serf_private.h"
28 serf_bucket_t *stream;
29 apr_uint64_t remaining;
33 serf_bucket_t *serf_bucket_limit_create(
34 serf_bucket_t *stream, apr_uint64_t len, serf_bucket_alloc_t *allocator)
38 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
42 return serf_bucket_create(&serf_bucket_type_limit, allocator, ctx);
45 static apr_status_t serf_limit_read(serf_bucket_t *bucket,
47 const char **data, apr_size_t *len)
49 limit_context_t *ctx = bucket->data;
52 if (!ctx->remaining) {
57 if (requested == SERF_READ_ALL_AVAIL || requested > ctx->remaining) {
58 if (ctx->remaining <= REQUESTED_MAX) {
59 requested = (apr_size_t) ctx->remaining;
61 requested = REQUESTED_MAX;
65 status = serf_bucket_read(ctx->stream, requested, data, len);
67 if (!SERF_BUCKET_READ_ERROR(status)) {
68 ctx->remaining -= *len;
71 /* If we have met our limit and don't have a status, return EOF. */
72 if (!ctx->remaining && !status) {
79 static apr_status_t serf_limit_readline(serf_bucket_t *bucket,
80 int acceptable, int *found,
81 const char **data, apr_size_t *len)
83 limit_context_t *ctx = bucket->data;
86 if (!ctx->remaining) {
91 status = serf_bucket_readline(ctx->stream, acceptable, found, data, len);
93 if (!SERF_BUCKET_READ_ERROR(status)) {
94 ctx->remaining -= *len;
97 /* If we have met our limit and don't have a status, return EOF. */
98 if (!ctx->remaining && !status) {
105 static apr_status_t serf_limit_peek(serf_bucket_t *bucket,
109 limit_context_t *ctx = bucket->data;
111 return serf_bucket_peek(ctx->stream, data, len);
114 static void serf_limit_destroy(serf_bucket_t *bucket)
116 limit_context_t *ctx = bucket->data;
118 serf_bucket_destroy(ctx->stream);
120 serf_default_destroy_and_data(bucket);
123 const serf_bucket_type_t serf_bucket_type_limit = {
127 serf_default_read_iovec,
128 serf_default_read_for_sendfile,
129 serf_default_read_bucket,