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_strings.h>
24 #include "serf_bucket_util.h"
27 serf_bucket_t *stream;
30 STATE_SIZE, /* reading the chunk size */
31 STATE_CHUNK, /* reading the chunk */
32 STATE_TERM, /* reading the chunk terminator */
33 STATE_DONE /* body is done; we've returned EOF */
36 /* Buffer for accumulating a chunk size. */
37 serf_linebuf_t linebuf;
39 /* How much of the chunk, or the terminator, do we have left to read? */
40 apr_int64_t body_left;
45 serf_bucket_t *serf_bucket_dechunk_create(
46 serf_bucket_t *stream,
47 serf_bucket_alloc_t *allocator)
49 dechunk_context_t *ctx;
51 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
53 ctx->state = STATE_SIZE;
55 serf_linebuf_init(&ctx->linebuf);
57 return serf_bucket_create(&serf_bucket_type_dechunk, allocator, ctx);
60 static void serf_dechunk_destroy_and_data(serf_bucket_t *bucket)
62 dechunk_context_t *ctx = bucket->data;
64 serf_bucket_destroy(ctx->stream);
66 serf_default_destroy_and_data(bucket);
69 static apr_status_t serf_dechunk_read(serf_bucket_t *bucket,
71 const char **data, apr_size_t *len)
73 dechunk_context_t *ctx = bucket->data;
80 /* fetch a line terminated by CRLF */
81 status = serf_linebuf_fetch(&ctx->linebuf, ctx->stream,
83 if (SERF_BUCKET_READ_ERROR(status))
86 /* if a line was read, then parse it. */
87 if (ctx->linebuf.state == SERF_LINEBUF_READY) {
88 /* NUL-terminate the line. if it filled the entire buffer,
89 then just assume the thing is too large. */
90 if (ctx->linebuf.used == sizeof(ctx->linebuf.line))
91 return APR_FROM_OS_ERROR(ERANGE);
92 ctx->linebuf.line[ctx->linebuf.used] = '\0';
94 /* convert from HEX digits. */
95 ctx->body_left = apr_strtoi64(ctx->linebuf.line, NULL, 16);
96 if (errno == ERANGE) {
97 return APR_FROM_OS_ERROR(ERANGE);
100 if (ctx->body_left == 0) {
101 /* Just read the last-chunk marker. We're DONE. */
102 ctx->state = STATE_DONE;
106 /* Got a size, so we'll start reading the chunk now. */
107 ctx->state = STATE_CHUNK;
110 /* If we can read more, then go do so. */
114 /* assert: status != 0 */
116 /* Note that we didn't actually read anything, so our callers
117 * don't get confused.
125 if (requested > ctx->body_left) {
126 requested = ctx->body_left;
129 /* Delegate to the stream bucket to do the read. */
130 status = serf_bucket_read(ctx->stream, requested, data, len);
131 if (SERF_BUCKET_READ_ERROR(status))
134 /* Some data was read, so decrement the amount left and see
135 * if we're done reading this chunk.
137 ctx->body_left -= *len;
138 if (!ctx->body_left) {
139 ctx->state = STATE_TERM;
140 ctx->body_left = 2; /* CRLF */
143 /* We need more data but there is no more available. */
144 if (ctx->body_left && APR_STATUS_IS_EOF(status)) {
145 return SERF_ERROR_TRUNCATED_HTTP_RESPONSE;
148 /* Return the data we just read. */
152 /* Delegate to the stream bucket to do the read. */
153 status = serf_bucket_read(ctx->stream, ctx->body_left, data, len);
154 if (SERF_BUCKET_READ_ERROR(status))
157 /* Some data was read, so decrement the amount left and see
158 * if we're done reading the chunk terminator.
160 ctx->body_left -= *len;
162 /* We need more data but there is no more available. */
163 if (ctx->body_left && APR_STATUS_IS_EOF(status))
164 return SERF_ERROR_TRUNCATED_HTTP_RESPONSE;
166 if (!ctx->body_left) {
167 ctx->state = STATE_SIZE;
170 /* Don't return the CR of CRLF to the caller! */
179 /* Just keep returning EOF */
191 /* ### need to implement */
192 #define serf_dechunk_readline NULL
193 #define serf_dechunk_peek NULL
195 const serf_bucket_type_t serf_bucket_type_dechunk = {
198 serf_dechunk_readline,
199 serf_default_read_iovec,
200 serf_default_read_for_sendfile,
201 serf_default_read_bucket,
203 serf_dechunk_destroy_and_data,