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
9 *
10 *      http://www.apache.org/licenses/LICENSE-2.0
11 *
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
17 *    under the License.
18 * ====================================================================
19 */
20
21#include <apr_strings.h>
22
23#include "serf.h"
24#include "serf_bucket_util.h"
25
26typedef struct {
27    serf_bucket_t *stream;
28
29    enum {
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 */
34    } state;
35
36    /* Buffer for accumulating a chunk size. */
37    serf_linebuf_t linebuf;
38
39    /* How much of the chunk, or the terminator, do we have left to read? */
40    apr_int64_t body_left;
41
42} dechunk_context_t;
43
44
45serf_bucket_t *serf_bucket_dechunk_create(
46    serf_bucket_t *stream,
47    serf_bucket_alloc_t *allocator)
48{
49    dechunk_context_t *ctx;
50
51    ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
52    ctx->stream = stream;
53    ctx->state = STATE_SIZE;
54
55    serf_linebuf_init(&ctx->linebuf);
56
57    return serf_bucket_create(&serf_bucket_type_dechunk, allocator, ctx);
58}
59
60static void serf_dechunk_destroy_and_data(serf_bucket_t *bucket)
61{
62    dechunk_context_t *ctx = bucket->data;
63
64    serf_bucket_destroy(ctx->stream);
65
66    serf_default_destroy_and_data(bucket);
67}
68
69static apr_status_t serf_dechunk_read(serf_bucket_t *bucket,
70                                      apr_size_t requested,
71                                      const char **data, apr_size_t *len)
72{
73    dechunk_context_t *ctx = bucket->data;
74    apr_status_t status;
75
76    while (1) {
77        switch (ctx->state) {
78        case STATE_SIZE:
79
80            /* fetch a line terminated by CRLF */
81            status = serf_linebuf_fetch(&ctx->linebuf, ctx->stream,
82                                        SERF_NEWLINE_CRLF);
83            if (SERF_BUCKET_READ_ERROR(status))
84                return status;
85
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';
93
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);
98                }
99
100                if (ctx->body_left == 0) {
101                    /* Just read the last-chunk marker. We're DONE. */
102                    ctx->state = STATE_DONE;
103                    status = APR_EOF;
104                }
105                else {
106                    /* Got a size, so we'll start reading the chunk now. */
107                    ctx->state = STATE_CHUNK;
108                }
109
110                /* If we can read more, then go do so. */
111                if (!status)
112                    continue;
113            }
114            /* assert: status != 0 */
115
116            /* Note that we didn't actually read anything, so our callers
117             * don't get confused.
118             */
119            *len = 0;
120
121            return status;
122
123        case STATE_CHUNK:
124
125            if (requested > ctx->body_left) {
126                requested = ctx->body_left;
127            }
128
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))
132                return status;
133
134            /* Some data was read, so decrement the amount left and see
135             * if we're done reading this chunk.
136             */
137            ctx->body_left -= *len;
138            if (!ctx->body_left) {
139                ctx->state = STATE_TERM;
140                ctx->body_left = 2;     /* CRLF */
141            }
142
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;
146            }
147
148            /* Return the data we just read. */
149            return status;
150
151        case STATE_TERM:
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))
155                return status;
156
157            /* Some data was read, so decrement the amount left and see
158             * if we're done reading the chunk terminator.
159             */
160            ctx->body_left -= *len;
161
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;
165
166            if (!ctx->body_left) {
167                ctx->state = STATE_SIZE;
168            }
169
170            /* Don't return the CR of CRLF to the caller! */
171            *len = 0;
172
173            if (status)
174                return status;
175
176            break;
177
178        case STATE_DONE:
179            /* Just keep returning EOF */
180            *len = 0;
181            return APR_EOF;
182
183        default:
184            /* Not reachable */
185            return APR_EGENERAL;
186        }
187    }
188    /* NOTREACHED */
189}
190
191/* ### need to implement */
192#define serf_dechunk_readline NULL
193#define serf_dechunk_peek NULL
194
195const serf_bucket_type_t serf_bucket_type_dechunk = {
196    "DECHUNK",
197    serf_dechunk_read,
198    serf_dechunk_readline,
199    serf_default_read_iovec,
200    serf_default_read_for_sendfile,
201    serf_default_read_bucket,
202    serf_dechunk_peek,
203    serf_dechunk_destroy_and_data,
204};
205