Blame buckets/simple_buckets.c

Packit 3adb1e
/* ====================================================================
Packit 3adb1e
 *    Licensed to the Apache Software Foundation (ASF) under one
Packit 3adb1e
 *    or more contributor license agreements.  See the NOTICE file
Packit 3adb1e
 *    distributed with this work for additional information
Packit 3adb1e
 *    regarding copyright ownership.  The ASF licenses this file
Packit 3adb1e
 *    to you under the Apache License, Version 2.0 (the
Packit 3adb1e
 *    "License"); you may not use this file except in compliance
Packit 3adb1e
 *    with the License.  You may obtain a copy of the License at
Packit 3adb1e
 *
Packit 3adb1e
 *      http://www.apache.org/licenses/LICENSE-2.0
Packit 3adb1e
 *
Packit 3adb1e
 *    Unless required by applicable law or agreed to in writing,
Packit 3adb1e
 *    software distributed under the License is distributed on an
Packit 3adb1e
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
Packit 3adb1e
 *    KIND, either express or implied.  See the License for the
Packit 3adb1e
 *    specific language governing permissions and limitations
Packit 3adb1e
 *    under the License.
Packit 3adb1e
 * ====================================================================
Packit 3adb1e
 */
Packit 3adb1e
Packit 3adb1e
#include <apr_pools.h>
Packit 3adb1e
Packit 3adb1e
#include "serf.h"
Packit 3adb1e
#include "serf_bucket_util.h"
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
typedef struct {
Packit 3adb1e
    const char *original;
Packit 3adb1e
    const char *current;
Packit 3adb1e
    apr_size_t remaining;
Packit 3adb1e
Packit 3adb1e
    serf_simple_freefunc_t freefunc;
Packit 3adb1e
    void *baton;
Packit 3adb1e
Packit 3adb1e
} simple_context_t;
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
static void free_copied_data(void *baton, const char *data)
Packit 3adb1e
{
Packit 3adb1e
    serf_bucket_mem_free(baton, (char*)data);
Packit 3adb1e
}
Packit 3adb1e
Packit 3adb1e
serf_bucket_t *serf_bucket_simple_create(
Packit 3adb1e
    const char *data,
Packit 3adb1e
    apr_size_t len,
Packit 3adb1e
    serf_simple_freefunc_t freefunc,
Packit 3adb1e
    void *freefunc_baton,
Packit 3adb1e
    serf_bucket_alloc_t *allocator)
Packit 3adb1e
{
Packit 3adb1e
    simple_context_t *ctx;
Packit 3adb1e
Packit 3adb1e
    ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
Packit 3adb1e
    ctx->original = ctx->current = data;
Packit 3adb1e
    ctx->remaining = len;
Packit 3adb1e
    ctx->freefunc = freefunc;
Packit 3adb1e
    ctx->baton = freefunc_baton;
Packit 3adb1e
Packit 3adb1e
    return serf_bucket_create(&serf_bucket_type_simple, allocator, ctx);
Packit 3adb1e
}
Packit 3adb1e
Packit 3adb1e
serf_bucket_t *serf_bucket_simple_copy_create(
Packit 3adb1e
    const char *data, apr_size_t len,
Packit 3adb1e
    serf_bucket_alloc_t *allocator)
Packit 3adb1e
{
Packit 3adb1e
    simple_context_t *ctx;
Packit 3adb1e
Packit 3adb1e
    ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
Packit 3adb1e
Packit 3adb1e
    ctx->original = ctx->current = serf_bucket_mem_alloc(allocator, len);
Packit 3adb1e
    memcpy((char*)ctx->original, data, len);
Packit 3adb1e
Packit 3adb1e
    ctx->remaining = len;
Packit 3adb1e
    ctx->freefunc = free_copied_data;
Packit 3adb1e
    ctx->baton = allocator;
Packit 3adb1e
Packit 3adb1e
    return serf_bucket_create(&serf_bucket_type_simple, allocator, ctx);
Packit 3adb1e
}
Packit 3adb1e
Packit 3adb1e
serf_bucket_t *serf_bucket_simple_own_create(
Packit 3adb1e
    const char *data, apr_size_t len,
Packit 3adb1e
    serf_bucket_alloc_t *allocator)
Packit 3adb1e
{
Packit 3adb1e
    simple_context_t *ctx;
Packit 3adb1e
Packit 3adb1e
    ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
Packit 3adb1e
Packit 3adb1e
    ctx->original = ctx->current = data;
Packit 3adb1e
Packit 3adb1e
    ctx->remaining = len;
Packit 3adb1e
    ctx->freefunc = free_copied_data;
Packit 3adb1e
    ctx->baton = allocator;
Packit 3adb1e
Packit 3adb1e
    return serf_bucket_create(&serf_bucket_type_simple, allocator, ctx);
Packit 3adb1e
}
Packit 3adb1e
Packit 3adb1e
static apr_status_t serf_simple_read(serf_bucket_t *bucket,
Packit 3adb1e
                                     apr_size_t requested,
Packit 3adb1e
                                     const char **data, apr_size_t *len)
Packit 3adb1e
{
Packit 3adb1e
    simple_context_t *ctx = bucket->data;
Packit 3adb1e
Packit 3adb1e
    if (requested == SERF_READ_ALL_AVAIL || requested > ctx->remaining)
Packit 3adb1e
        requested = ctx->remaining;
Packit 3adb1e
Packit 3adb1e
    *data = ctx->current;
Packit 3adb1e
    *len = requested;
Packit 3adb1e
Packit 3adb1e
    ctx->current += requested;
Packit 3adb1e
    ctx->remaining -= requested;
Packit 3adb1e
Packit 3adb1e
    return ctx->remaining ? APR_SUCCESS : APR_EOF;
Packit 3adb1e
}
Packit 3adb1e
Packit 3adb1e
static apr_status_t serf_simple_readline(serf_bucket_t *bucket,
Packit 3adb1e
                                         int acceptable, int *found,
Packit 3adb1e
                                         const char **data, apr_size_t *len)
Packit 3adb1e
{
Packit 3adb1e
    simple_context_t *ctx = bucket->data;
Packit 3adb1e
Packit 3adb1e
    /* Returned data will be from current position. */
Packit 3adb1e
    *data = ctx->current;
Packit 3adb1e
    serf_util_readline(&ctx->current, &ctx->remaining, acceptable, found);
Packit 3adb1e
Packit 3adb1e
    /* See how much ctx->current moved forward. */
Packit 3adb1e
    *len = ctx->current - *data;
Packit 3adb1e
Packit 3adb1e
    return ctx->remaining ? APR_SUCCESS : APR_EOF;
Packit 3adb1e
}
Packit 3adb1e
Packit 3adb1e
static apr_status_t serf_simple_peek(serf_bucket_t *bucket,
Packit 3adb1e
                                     const char **data,
Packit 3adb1e
                                     apr_size_t *len)
Packit 3adb1e
{
Packit 3adb1e
    simple_context_t *ctx = bucket->data;
Packit 3adb1e
Packit 3adb1e
    /* return whatever we have left */
Packit 3adb1e
    *data = ctx->current;
Packit 3adb1e
    *len = ctx->remaining;
Packit 3adb1e
Packit 3adb1e
    /* we returned everything this bucket will ever hold */
Packit 3adb1e
    return APR_EOF;
Packit 3adb1e
}
Packit 3adb1e
Packit 3adb1e
static void serf_simple_destroy(serf_bucket_t *bucket)
Packit 3adb1e
{
Packit 3adb1e
    simple_context_t *ctx = bucket->data;
Packit 3adb1e
Packit 3adb1e
    if (ctx->freefunc)
Packit 3adb1e
        (*ctx->freefunc)(ctx->baton, ctx->original);
Packit 3adb1e
Packit 3adb1e
    serf_default_destroy_and_data(bucket);
Packit 3adb1e
}
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
const serf_bucket_type_t serf_bucket_type_simple = {
Packit 3adb1e
    "SIMPLE",
Packit 3adb1e
    serf_simple_read,
Packit 3adb1e
    serf_simple_readline,
Packit 3adb1e
    serf_default_read_iovec,
Packit 3adb1e
    serf_default_read_for_sendfile,
Packit 3adb1e
    serf_default_read_bucket,
Packit 3adb1e
    serf_simple_peek,
Packit 3adb1e
    serf_simple_destroy,
Packit 3adb1e
};