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_strings.h>
19 #include "serf_bucket_util.h"
22 serf_bucket_t *stream;
25 STATE_SIZE, /* reading the chunk size */
26 STATE_CHUNK, /* reading the chunk */
27 STATE_TERM, /* reading the chunk terminator */
28 STATE_DONE /* body is done; we've returned EOF */
31 /* Buffer for accumulating a chunk size. */
32 serf_linebuf_t linebuf;
34 /* How much of the chunk, or the terminator, do we have left to read? */
35 apr_int64_t body_left;
40 serf_bucket_t *serf_bucket_dechunk_create(
41 serf_bucket_t *stream,
42 serf_bucket_alloc_t *allocator)
44 dechunk_context_t *ctx;
46 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
48 ctx->state = STATE_SIZE;
50 serf_linebuf_init(&ctx->linebuf);
52 return serf_bucket_create(&serf_bucket_type_dechunk, allocator, ctx);
55 static void serf_dechunk_destroy_and_data(serf_bucket_t *bucket)
57 dechunk_context_t *ctx = bucket->data;
59 serf_bucket_destroy(ctx->stream);
61 serf_default_destroy_and_data(bucket);
64 static apr_status_t serf_dechunk_read(serf_bucket_t *bucket,
66 const char **data, apr_size_t *len)
68 dechunk_context_t *ctx = bucket->data;
75 /* fetch a line terminated by CRLF */
76 status = serf_linebuf_fetch(&ctx->linebuf, ctx->stream,
78 if (SERF_BUCKET_READ_ERROR(status))
81 /* if a line was read, then parse it. */
82 if (ctx->linebuf.state == SERF_LINEBUF_READY) {
83 /* NUL-terminate the line. if it filled the entire buffer,
84 then just assume the thing is too large. */
85 if (ctx->linebuf.used == sizeof(ctx->linebuf.line))
86 return APR_FROM_OS_ERROR(ERANGE);
87 ctx->linebuf.line[ctx->linebuf.used] = '\0';
89 /* convert from HEX digits. */
90 ctx->body_left = apr_strtoi64(ctx->linebuf.line, NULL, 16);
91 if (errno == ERANGE) {
92 return APR_FROM_OS_ERROR(ERANGE);
95 if (ctx->body_left == 0) {
96 /* Just read the last-chunk marker. We're DONE. */
97 ctx->state = STATE_DONE;
101 /* Got a size, so we'll start reading the chunk now. */
102 ctx->state = STATE_CHUNK;
105 /* If we can read more, then go do so. */
109 /* assert: status != 0 */
111 /* Note that we didn't actually read anything, so our callers
112 * don't get confused.
120 if (requested > ctx->body_left) {
121 requested = ctx->body_left;
124 /* Delegate to the stream bucket to do the read. */
125 status = serf_bucket_read(ctx->stream, requested, data, len);
126 if (SERF_BUCKET_READ_ERROR(status))
129 /* Some data was read, so decrement the amount left and see
130 * if we're done reading this chunk.
132 ctx->body_left -= *len;
133 if (!ctx->body_left) {
134 ctx->state = STATE_TERM;
135 ctx->body_left = 2; /* CRLF */
138 /* We need more data but there is no more available. */
139 if (ctx->body_left && APR_STATUS_IS_EOF(status)) {
140 return SERF_ERROR_TRUNCATED_HTTP_RESPONSE;
143 /* Return the data we just read. */
147 /* Delegate to the stream bucket to do the read. */
148 status = serf_bucket_read(ctx->stream, ctx->body_left, data, len);
149 if (SERF_BUCKET_READ_ERROR(status))
152 /* Some data was read, so decrement the amount left and see
153 * if we're done reading the chunk terminator.
155 ctx->body_left -= *len;
157 /* We need more data but there is no more available. */
158 if (ctx->body_left && APR_STATUS_IS_EOF(status))
159 return SERF_ERROR_TRUNCATED_HTTP_RESPONSE;
161 if (!ctx->body_left) {
162 ctx->state = STATE_SIZE;
165 /* Don't return the CR of CRLF to the caller! */
174 /* Just keep returning EOF */
186 /* ### need to implement */
187 #define serf_dechunk_readline NULL
188 #define serf_dechunk_peek NULL
190 const serf_bucket_type_t serf_bucket_type_dechunk = {
193 serf_dechunk_readline,
194 serf_default_read_iovec,
195 serf_default_read_for_sendfile,
196 serf_default_read_bucket,
198 serf_dechunk_destroy_and_data,