]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/serf/buckets/dechunk_buckets.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / serf / buckets / dechunk_buckets.c
1 /* Copyright 2002-2004 Justin Erenkrantz and Greg Stein
2  *
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
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <apr_strings.h>
17
18 #include "serf.h"
19 #include "serf_bucket_util.h"
20
21 typedef struct {
22     serf_bucket_t *stream;
23
24     enum {
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 */
29     } state;
30
31     /* Buffer for accumulating a chunk size. */
32     serf_linebuf_t linebuf;
33
34     /* How much of the chunk, or the terminator, do we have left to read? */
35     apr_int64_t body_left;
36
37 } dechunk_context_t;
38
39
40 serf_bucket_t *serf_bucket_dechunk_create(
41     serf_bucket_t *stream,
42     serf_bucket_alloc_t *allocator)
43 {
44     dechunk_context_t *ctx;
45
46     ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
47     ctx->stream = stream;
48     ctx->state = STATE_SIZE;
49
50     serf_linebuf_init(&ctx->linebuf);
51
52     return serf_bucket_create(&serf_bucket_type_dechunk, allocator, ctx);
53 }
54
55 static void serf_dechunk_destroy_and_data(serf_bucket_t *bucket)
56 {
57     dechunk_context_t *ctx = bucket->data;
58
59     serf_bucket_destroy(ctx->stream);
60
61     serf_default_destroy_and_data(bucket);
62 }
63
64 static apr_status_t serf_dechunk_read(serf_bucket_t *bucket,
65                                       apr_size_t requested,
66                                       const char **data, apr_size_t *len)
67 {
68     dechunk_context_t *ctx = bucket->data;
69     apr_status_t status;
70
71     while (1) {
72         switch (ctx->state) {
73         case STATE_SIZE:
74
75             /* fetch a line terminated by CRLF */
76             status = serf_linebuf_fetch(&ctx->linebuf, ctx->stream,
77                                         SERF_NEWLINE_CRLF);
78             if (SERF_BUCKET_READ_ERROR(status))
79                 return status;
80
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';
88
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);
93                 }
94
95                 if (ctx->body_left == 0) {
96                     /* Just read the last-chunk marker. We're DONE. */
97                     ctx->state = STATE_DONE;
98                     status = APR_EOF;
99                 }
100                 else {
101                     /* Got a size, so we'll start reading the chunk now. */
102                     ctx->state = STATE_CHUNK;
103                 }
104
105                 /* If we can read more, then go do so. */
106                 if (!status)
107                     continue;
108             }
109             /* assert: status != 0 */
110
111             /* Note that we didn't actually read anything, so our callers
112              * don't get confused.
113              */
114             *len = 0;
115
116             return status;
117
118         case STATE_CHUNK:
119
120             if (requested > ctx->body_left) {
121                 requested = ctx->body_left;
122             }
123
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))
127                 return status;
128
129             /* Some data was read, so decrement the amount left and see
130              * if we're done reading this chunk.
131              */
132             ctx->body_left -= *len;
133             if (!ctx->body_left) {
134                 ctx->state = STATE_TERM;
135                 ctx->body_left = 2;     /* CRLF */
136             }
137
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;
141             }
142
143             /* Return the data we just read. */
144             return status;
145
146         case STATE_TERM:
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))
150                 return status;
151
152             /* Some data was read, so decrement the amount left and see
153              * if we're done reading the chunk terminator.
154              */
155             ctx->body_left -= *len;
156
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;
160
161             if (!ctx->body_left) {
162                 ctx->state = STATE_SIZE;
163             }
164
165             /* Don't return the CR of CRLF to the caller! */
166             *len = 0;
167
168             if (status)
169                 return status;
170
171             break;
172
173         case STATE_DONE:
174             /* Just keep returning EOF */
175             *len = 0;
176             return APR_EOF;
177
178         default:
179             /* Not reachable */
180             return APR_EGENERAL;
181         }
182     }
183     /* NOTREACHED */
184 }
185
186 /* ### need to implement */
187 #define serf_dechunk_readline NULL
188 #define serf_dechunk_peek NULL
189
190 const serf_bucket_type_t serf_bucket_type_dechunk = {
191     "DECHUNK",
192     serf_dechunk_read,
193     serf_dechunk_readline,
194     serf_default_read_iovec,
195     serf_default_read_for_sendfile,
196     serf_default_read_bucket,
197     serf_dechunk_peek,
198     serf_dechunk_destroy_and_data,
199 };