1 /* Copyright 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.
18 #include <apr_general.h> /* for strcasecmp() */
21 #include "serf_bucket_util.h"
23 #include "serf_private.h" /* for serf__bucket_headers_remove */
26 typedef struct header_list {
30 apr_size_t header_size;
31 apr_size_t value_size;
34 #define ALLOC_HEADER 0x0001 /* header lives in our allocator */
35 #define ALLOC_VALUE 0x0002 /* value lives in our allocator */
37 struct header_list *next;
44 header_list_t *cur_read;
46 READ_START, /* haven't started reading yet */
47 READ_HEADER, /* reading cur_read->header */
48 READ_SEP, /* reading ": " */
49 READ_VALUE, /* reading cur_read->value */
50 READ_CRLF, /* reading "\r\n" */
51 READ_TERM, /* reading the final "\r\n" */
52 READ_DONE /* no more data to read */
54 apr_size_t amt_read; /* how much of the current state we've read */
59 serf_bucket_t *serf_bucket_headers_create(
60 serf_bucket_alloc_t *allocator)
62 headers_context_t *ctx;
64 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
67 ctx->state = READ_START;
69 return serf_bucket_create(&serf_bucket_type_headers, allocator, ctx);
72 void serf_bucket_headers_setx(
74 const char *header, apr_size_t header_size, int header_copy,
75 const char *value, apr_size_t value_size, int value_copy)
77 headers_context_t *ctx = bkt->data;
81 /* ### include this? */
83 /* we started reading. can't change now. */
88 hdr = serf_bucket_mem_alloc(bkt->allocator, sizeof(*hdr));
89 hdr->header_size = header_size;
90 hdr->value_size = value_size;
95 hdr->header = serf_bstrmemdup(bkt->allocator, header, header_size);
96 hdr->alloc_flags |= ALLOC_HEADER;
103 hdr->value = serf_bstrmemdup(bkt->allocator, value, value_size);
104 hdr->alloc_flags |= ALLOC_VALUE;
110 /* Add the new header at the end of the list. */
112 ctx->last->next = hdr;
119 void serf_bucket_headers_set(
120 serf_bucket_t *headers_bucket,
124 serf_bucket_headers_setx(headers_bucket,
125 header, strlen(header), 0,
126 value, strlen(value), 1);
129 void serf_bucket_headers_setc(
130 serf_bucket_t *headers_bucket,
134 serf_bucket_headers_setx(headers_bucket,
135 header, strlen(header), 1,
136 value, strlen(value), 1);
139 void serf_bucket_headers_setn(
140 serf_bucket_t *headers_bucket,
144 serf_bucket_headers_setx(headers_bucket,
145 header, strlen(header), 0,
146 value, strlen(value), 0);
149 const char *serf_bucket_headers_get(
150 serf_bucket_t *headers_bucket,
153 headers_context_t *ctx = headers_bucket->data;
154 header_list_t *found = ctx->list;
155 const char *val = NULL;
160 if (strcasecmp(found->header, header) == 0) {
162 /* The header is already present. RFC 2616, section 4.2
163 indicates that we should append the new value, separated by
164 a comma. Reasoning: for headers whose values are known to
165 be comma-separated, that is clearly the correct behavior;
166 for others, the correct behavior is undefined anyway. */
168 /* The "+1" is for the comma; the +1 in the alloc
169 call is for the terminating '\0' */
170 apr_size_t new_size = found->value_size + value_size + 1;
171 char *new_val = serf_bucket_mem_alloc(headers_bucket->allocator,
173 memcpy(new_val, val, value_size);
174 new_val[value_size] = ',';
175 memcpy(new_val + value_size + 1, found->value,
177 new_val[new_size] = '\0';
178 /* Copy the new value over the already existing value. */
180 serf_bucket_mem_free(headers_bucket->allocator, (void*)val);
181 val_alloc |= ALLOC_VALUE;
183 value_size = new_size;
187 value_size = found->value_size;
196 void serf__bucket_headers_remove(serf_bucket_t *bucket, const char *header)
198 headers_context_t *ctx = bucket->data;
199 header_list_t *scan = ctx->list, *prev = NULL;
201 /* Find and delete all items with the same header (case insensitive) */
203 if (strcasecmp(scan->header, header) == 0) {
205 prev->next = scan->next;
207 ctx->list = scan->next;
209 if (ctx->last == scan) {
219 void serf_bucket_headers_do(
220 serf_bucket_t *headers_bucket,
221 serf_bucket_headers_do_callback_fn_t func,
224 headers_context_t *ctx = headers_bucket->data;
225 header_list_t *scan = ctx->list;
228 if (func(baton, scan->header, scan->value) != 0) {
235 static void serf_headers_destroy_and_data(serf_bucket_t *bucket)
237 headers_context_t *ctx = bucket->data;
238 header_list_t *scan = ctx->list;
241 header_list_t *next_hdr = scan->next;
243 if (scan->alloc_flags & ALLOC_HEADER)
244 serf_bucket_mem_free(bucket->allocator, (void *)scan->header);
245 if (scan->alloc_flags & ALLOC_VALUE)
246 serf_bucket_mem_free(bucket->allocator, (void *)scan->value);
247 serf_bucket_mem_free(bucket->allocator, scan);
252 serf_default_destroy_and_data(bucket);
255 static void select_value(
256 headers_context_t *ctx,
263 if (ctx->state == READ_START) {
264 if (ctx->list == NULL) {
265 /* No headers. Move straight to the TERM state. */
266 ctx->state = READ_TERM;
269 ctx->state = READ_HEADER;
270 ctx->cur_read = ctx->list;
275 switch (ctx->state) {
277 v = ctx->cur_read->header;
278 l = ctx->cur_read->header_size;
285 v = ctx->cur_read->value;
286 l = ctx->cur_read->value_size;
301 *value = v + ctx->amt_read;
302 *len = l - ctx->amt_read;
305 /* the current data chunk has been read/consumed. move our internal state. */
306 static apr_status_t consume_chunk(headers_context_t *ctx)
308 /* move to the next state, resetting the amount read. */
312 /* just sent the terminator and moved to DONE. signal completion. */
313 if (ctx->state == READ_DONE)
316 /* end of this header. move to the next one. */
317 if (ctx->state == READ_TERM) {
318 ctx->cur_read = ctx->cur_read->next;
319 if (ctx->cur_read != NULL) {
320 /* We've got another head to send. Reset the read state. */
321 ctx->state = READ_HEADER;
323 /* else leave in READ_TERM */
326 /* there is more data which can be read immediately. */
330 static apr_status_t serf_headers_peek(serf_bucket_t *bucket,
334 headers_context_t *ctx = bucket->data;
336 select_value(ctx, data, len);
338 /* already done or returning the CRLF terminator? return EOF */
339 if (ctx->state == READ_DONE || ctx->state == READ_TERM)
345 static apr_status_t serf_headers_read(serf_bucket_t *bucket,
346 apr_size_t requested,
347 const char **data, apr_size_t *len)
349 headers_context_t *ctx = bucket->data;
352 select_value(ctx, data, &avail);
353 if (ctx->state == READ_DONE) {
358 if (requested >= avail) {
359 /* return everything from this chunk */
362 /* we consumed this chunk. advance the state. */
363 return consume_chunk(ctx);
366 /* return just the amount requested, and advance our pointer */
368 ctx->amt_read += requested;
370 /* there is more that can be read immediately */
374 static apr_status_t serf_headers_readline(serf_bucket_t *bucket,
375 int acceptable, int *found,
376 const char **data, apr_size_t *len)
378 headers_context_t *ctx = bucket->data;
381 /* ### what behavior should we use here? APR_EGENERAL for now */
382 if ((acceptable & SERF_NEWLINE_CRLF) == 0)
385 /* get whatever is in this chunk */
386 select_value(ctx, data, len);
387 if (ctx->state == READ_DONE)
390 /* we consumed this chunk. advance the state. */
391 status = consume_chunk(ctx);
393 /* the type of newline found is easy... */
394 *found = (ctx->state == READ_CRLF || ctx->state == READ_TERM)
395 ? SERF_NEWLINE_CRLF : SERF_NEWLINE_NONE;
400 static apr_status_t serf_headers_read_iovec(serf_bucket_t *bucket,
401 apr_size_t requested,
406 apr_size_t avail = requested;
411 for (i = 0; i < vecs_size; i++) {
416 /* Calling read() would not be a safe opt in the general case, but it
417 * is here for the header bucket as it only frees all of the header
418 * keys and values when the entire bucket goes away - not on a
419 * per-read() basis as is normally the case.
421 status = serf_headers_read(bucket, avail, &data, &len);
424 vecs[*vecs_used].iov_base = (char*)data;
425 vecs[*vecs_used].iov_len = len;
429 if (avail != SERF_READ_ALL_AVAIL) {
432 /* If we reach 0, then read()'s status will suffice. */
447 const serf_bucket_type_t serf_bucket_type_headers = {
450 serf_headers_readline,
451 serf_headers_read_iovec,
452 serf_default_read_for_sendfile,
453 serf_default_read_bucket,
455 serf_headers_destroy_and_data,