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"
26 /* Older versions of APR do not have this macro. */
28 #define REQUESTED_MAX APR_SIZE_MAX
30 #define REQUESTED_MAX (~((apr_size_t)0))
35 serf_bucket_t *stream;
36 apr_uint64_t remaining;
39 serf_bucket_t *serf_bucket_response_body_create(
40 serf_bucket_t *stream, apr_uint64_t len, serf_bucket_alloc_t *allocator)
44 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
48 return serf_bucket_create(&serf_bucket_type_response_body, allocator, ctx);
51 static apr_status_t serf_response_body_read(serf_bucket_t *bucket,
56 body_context_t *ctx = bucket->data;
59 if (!ctx->remaining) {
64 if (requested == SERF_READ_ALL_AVAIL || requested > ctx->remaining) {
65 if (ctx->remaining <= REQUESTED_MAX) {
66 requested = (apr_size_t) ctx->remaining;
68 requested = REQUESTED_MAX;
72 status = serf_bucket_read(ctx->stream, requested, data, len);
74 if (!SERF_BUCKET_READ_ERROR(status)) {
75 ctx->remaining -= *len;
78 if (APR_STATUS_IS_EOF(status) && ctx->remaining > 0) {
79 /* The server sent less data than expected. */
80 status = SERF_ERROR_TRUNCATED_HTTP_RESPONSE;
86 static apr_status_t serf_response_body_readline(serf_bucket_t *bucket,
87 int acceptable, int *found,
91 body_context_t *ctx = bucket->data;
94 if (!ctx->remaining) {
99 status = serf_bucket_readline(ctx->stream, acceptable, found, data, len);
101 if (!SERF_BUCKET_READ_ERROR(status)) {
102 ctx->remaining -= *len;
105 if (APR_STATUS_IS_EOF(status) && ctx->remaining > 0) {
106 /* The server sent less data than expected. */
107 status = SERF_ERROR_TRUNCATED_HTTP_RESPONSE;
113 static apr_status_t serf_response_body_peek(serf_bucket_t *bucket,
117 body_context_t *ctx = bucket->data;
119 return serf_bucket_peek(ctx->stream, data, len);
122 static void serf_response_body_destroy(serf_bucket_t *bucket)
124 body_context_t *ctx = bucket->data;
126 serf_bucket_destroy(ctx->stream);
128 serf_default_destroy_and_data(bucket);
131 const serf_bucket_type_t serf_bucket_type_response_body = {
133 serf_response_body_read,
134 serf_response_body_readline,
135 serf_default_read_iovec,
136 serf_default_read_for_sendfile,
137 serf_default_read_bucket,
138 serf_response_body_peek,
139 serf_response_body_destroy,