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 * ====================================================================
23 #include <apr_general.h> /* for strcasecmp() */
26 #include "serf_bucket_util.h"
28 #include "serf_private.h" /* for serf__bucket_headers_remove */
31 typedef struct header_list {
35 apr_size_t header_size;
36 apr_size_t value_size;
39 #define ALLOC_HEADER 0x0001 /* header lives in our allocator */
40 #define ALLOC_VALUE 0x0002 /* value lives in our allocator */
42 struct header_list *next;
49 header_list_t *cur_read;
51 READ_START, /* haven't started reading yet */
52 READ_HEADER, /* reading cur_read->header */
53 READ_SEP, /* reading ": " */
54 READ_VALUE, /* reading cur_read->value */
55 READ_CRLF, /* reading "\r\n" */
56 READ_TERM, /* reading the final "\r\n" */
57 READ_DONE /* no more data to read */
59 apr_size_t amt_read; /* how much of the current state we've read */
64 serf_bucket_t *serf_bucket_headers_create(
65 serf_bucket_alloc_t *allocator)
67 headers_context_t *ctx;
69 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
72 ctx->state = READ_START;
74 return serf_bucket_create(&serf_bucket_type_headers, allocator, ctx);
77 void serf_bucket_headers_setx(
79 const char *header, apr_size_t header_size, int header_copy,
80 const char *value, apr_size_t value_size, int value_copy)
82 headers_context_t *ctx = bkt->data;
86 /* ### include this? */
88 /* we started reading. can't change now. */
93 hdr = serf_bucket_mem_alloc(bkt->allocator, sizeof(*hdr));
94 hdr->header_size = header_size;
95 hdr->value_size = value_size;
100 hdr->header = serf_bstrmemdup(bkt->allocator, header, header_size);
101 hdr->alloc_flags |= ALLOC_HEADER;
104 hdr->header = header;
108 hdr->value = serf_bstrmemdup(bkt->allocator, value, value_size);
109 hdr->alloc_flags |= ALLOC_VALUE;
115 /* Add the new header at the end of the list. */
117 ctx->last->next = hdr;
124 void serf_bucket_headers_set(
125 serf_bucket_t *headers_bucket,
129 serf_bucket_headers_setx(headers_bucket,
130 header, strlen(header), 0,
131 value, strlen(value), 1);
134 void serf_bucket_headers_setc(
135 serf_bucket_t *headers_bucket,
139 serf_bucket_headers_setx(headers_bucket,
140 header, strlen(header), 1,
141 value, strlen(value), 1);
144 void serf_bucket_headers_setn(
145 serf_bucket_t *headers_bucket,
149 serf_bucket_headers_setx(headers_bucket,
150 header, strlen(header), 0,
151 value, strlen(value), 0);
154 const char *serf_bucket_headers_get(
155 serf_bucket_t *headers_bucket,
158 headers_context_t *ctx = headers_bucket->data;
159 header_list_t *found = ctx->list;
160 const char *val = NULL;
165 if (strcasecmp(found->header, header) == 0) {
167 /* The header is already present. RFC 2616, section 4.2
168 indicates that we should append the new value, separated by
169 a comma. Reasoning: for headers whose values are known to
170 be comma-separated, that is clearly the correct behavior;
171 for others, the correct behavior is undefined anyway. */
173 /* The "+1" is for the comma; the +1 in the alloc
174 call is for the terminating '\0' */
175 apr_size_t new_size = found->value_size + value_size + 1;
176 char *new_val = serf_bucket_mem_alloc(headers_bucket->allocator,
178 memcpy(new_val, val, value_size);
179 new_val[value_size] = ',';
180 memcpy(new_val + value_size + 1, found->value,
182 new_val[new_size] = '\0';
183 /* Copy the new value over the already existing value. */
185 serf_bucket_mem_free(headers_bucket->allocator, (void*)val);
186 val_alloc |= ALLOC_VALUE;
188 value_size = new_size;
192 value_size = found->value_size;
201 void serf__bucket_headers_remove(serf_bucket_t *bucket, const char *header)
203 headers_context_t *ctx = bucket->data;
204 header_list_t *scan = ctx->list, *prev = NULL;
206 /* Find and delete all items with the same header (case insensitive) */
208 if (strcasecmp(scan->header, header) == 0) {
210 prev->next = scan->next;
212 ctx->list = scan->next;
214 if (ctx->last == scan) {
224 void serf_bucket_headers_do(
225 serf_bucket_t *headers_bucket,
226 serf_bucket_headers_do_callback_fn_t func,
229 headers_context_t *ctx = headers_bucket->data;
230 header_list_t *scan = ctx->list;
233 if (func(baton, scan->header, scan->value) != 0) {
240 static void serf_headers_destroy_and_data(serf_bucket_t *bucket)
242 headers_context_t *ctx = bucket->data;
243 header_list_t *scan = ctx->list;
246 header_list_t *next_hdr = scan->next;
248 if (scan->alloc_flags & ALLOC_HEADER)
249 serf_bucket_mem_free(bucket->allocator, (void *)scan->header);
250 if (scan->alloc_flags & ALLOC_VALUE)
251 serf_bucket_mem_free(bucket->allocator, (void *)scan->value);
252 serf_bucket_mem_free(bucket->allocator, scan);
257 serf_default_destroy_and_data(bucket);
260 static void select_value(
261 headers_context_t *ctx,
268 if (ctx->state == READ_START) {
269 if (ctx->list == NULL) {
270 /* No headers. Move straight to the TERM state. */
271 ctx->state = READ_TERM;
274 ctx->state = READ_HEADER;
275 ctx->cur_read = ctx->list;
280 switch (ctx->state) {
282 v = ctx->cur_read->header;
283 l = ctx->cur_read->header_size;
290 v = ctx->cur_read->value;
291 l = ctx->cur_read->value_size;
306 *value = v + ctx->amt_read;
307 *len = l - ctx->amt_read;
310 /* the current data chunk has been read/consumed. move our internal state. */
311 static apr_status_t consume_chunk(headers_context_t *ctx)
313 /* move to the next state, resetting the amount read. */
317 /* just sent the terminator and moved to DONE. signal completion. */
318 if (ctx->state == READ_DONE)
321 /* end of this header. move to the next one. */
322 if (ctx->state == READ_TERM) {
323 ctx->cur_read = ctx->cur_read->next;
324 if (ctx->cur_read != NULL) {
325 /* We've got another head to send. Reset the read state. */
326 ctx->state = READ_HEADER;
328 /* else leave in READ_TERM */
331 /* there is more data which can be read immediately. */
335 static apr_status_t serf_headers_peek(serf_bucket_t *bucket,
339 headers_context_t *ctx = bucket->data;
341 select_value(ctx, data, len);
343 /* already done or returning the CRLF terminator? return EOF */
344 if (ctx->state == READ_DONE || ctx->state == READ_TERM)
350 static apr_status_t serf_headers_read(serf_bucket_t *bucket,
351 apr_size_t requested,
352 const char **data, apr_size_t *len)
354 headers_context_t *ctx = bucket->data;
357 select_value(ctx, data, &avail);
358 if (ctx->state == READ_DONE) {
363 if (requested >= avail) {
364 /* return everything from this chunk */
367 /* we consumed this chunk. advance the state. */
368 return consume_chunk(ctx);
371 /* return just the amount requested, and advance our pointer */
373 ctx->amt_read += requested;
375 /* there is more that can be read immediately */
379 static apr_status_t serf_headers_readline(serf_bucket_t *bucket,
380 int acceptable, int *found,
381 const char **data, apr_size_t *len)
383 headers_context_t *ctx = bucket->data;
386 /* ### what behavior should we use here? APR_EGENERAL for now */
387 if ((acceptable & SERF_NEWLINE_CRLF) == 0)
390 /* get whatever is in this chunk */
391 select_value(ctx, data, len);
392 if (ctx->state == READ_DONE)
395 /* we consumed this chunk. advance the state. */
396 status = consume_chunk(ctx);
398 /* the type of newline found is easy... */
399 *found = (ctx->state == READ_CRLF || ctx->state == READ_TERM)
400 ? SERF_NEWLINE_CRLF : SERF_NEWLINE_NONE;
405 static apr_status_t serf_headers_read_iovec(serf_bucket_t *bucket,
406 apr_size_t requested,
411 apr_size_t avail = requested;
416 for (i = 0; i < vecs_size; i++) {
421 /* Calling read() would not be a safe opt in the general case, but it
422 * is here for the header bucket as it only frees all of the header
423 * keys and values when the entire bucket goes away - not on a
424 * per-read() basis as is normally the case.
426 status = serf_headers_read(bucket, avail, &data, &len);
429 vecs[*vecs_used].iov_base = (char*)data;
430 vecs[*vecs_used].iov_len = len;
434 if (avail != SERF_READ_ALL_AVAIL) {
437 /* If we reach 0, then read()'s status will suffice. */
452 const serf_bucket_type_t serf_bucket_type_headers = {
455 serf_headers_readline,
456 serf_headers_read_iovec,
457 serf_default_read_for_sendfile,
458 serf_default_read_bucket,
460 serf_headers_destroy_and_data,