Blame buckets/apr_buckets_pipe.c

Packit 383869
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 383869
 * contributor license agreements.  See the NOTICE file distributed with
Packit 383869
 * this work for additional information regarding copyright ownership.
Packit 383869
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 383869
 * (the "License"); you may not use this file except in compliance with
Packit 383869
 * the License.  You may obtain a copy of the License at
Packit 383869
 *
Packit 383869
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 383869
 *
Packit 383869
 * Unless required by applicable law or agreed to in writing, software
Packit 383869
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 383869
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 383869
 * See the License for the specific language governing permissions and
Packit 383869
 * limitations under the License.
Packit 383869
 */
Packit 383869
Packit 383869
#include "apr_buckets.h"
Packit 383869
Packit 383869
static apr_status_t pipe_bucket_read(apr_bucket *a, const char **str,
Packit 383869
                                     apr_size_t *len, apr_read_type_e block)
Packit 383869
{
Packit 383869
    apr_file_t *p = a->data;
Packit 383869
    char *buf;
Packit 383869
    apr_status_t rv;
Packit 383869
    apr_interval_time_t timeout;
Packit 383869
Packit 383869
    if (block == APR_NONBLOCK_READ) {
Packit 383869
        apr_file_pipe_timeout_get(p, &timeout);
Packit 383869
        apr_file_pipe_timeout_set(p, 0);
Packit 383869
    }
Packit 383869
Packit 383869
    *str = NULL;
Packit 383869
    *len = APR_BUCKET_BUFF_SIZE;
Packit 383869
    buf = apr_bucket_alloc(*len, a->list); /* XXX: check for failure? */
Packit 383869
Packit 383869
    rv = apr_file_read(p, buf, len);
Packit 383869
Packit 383869
    if (block == APR_NONBLOCK_READ) {
Packit 383869
        apr_file_pipe_timeout_set(p, timeout);
Packit 383869
    }
Packit 383869
Packit 383869
    if (rv != APR_SUCCESS && rv != APR_EOF) {
Packit 383869
        apr_bucket_free(buf);
Packit 383869
        return rv;
Packit 383869
    }
Packit 383869
    /*
Packit 383869
     * If there's more to read we have to keep the rest of the pipe
Packit 383869
     * for later.  Otherwise, we'll close the pipe.
Packit 383869
     * XXX: Note that more complicated bucket types that 
Packit 383869
     * refer to data not in memory and must therefore have a read()
Packit 383869
     * function similar to this one should be wary of copying this
Packit 383869
     * code because if they have a destroy function they probably
Packit 383869
     * want to migrate the bucket's subordinate structure from the
Packit 383869
     * old bucket to a raw new one and adjust it as appropriate,
Packit 383869
     * rather than destroying the old one and creating a completely
Packit 383869
     * new bucket.
Packit 383869
     */
Packit 383869
    if (*len > 0) {
Packit 383869
        apr_bucket_heap *h;
Packit 383869
        /* Change the current bucket to refer to what we read */
Packit 383869
        a = apr_bucket_heap_make(a, buf, *len, apr_bucket_free);
Packit 383869
        h = a->data;
Packit 383869
        h->alloc_len = APR_BUCKET_BUFF_SIZE; /* note the real buffer size */
Packit 383869
        *str = buf;
Packit 383869
        APR_BUCKET_INSERT_AFTER(a, apr_bucket_pipe_create(p, a->list));
Packit 383869
    }
Packit 383869
    else {
Packit 383869
        apr_bucket_free(buf);
Packit 383869
        a = apr_bucket_immortal_make(a, "", 0);
Packit 383869
        *str = a->data;
Packit 383869
        if (rv == APR_EOF) {
Packit 383869
            apr_file_close(p);
Packit 383869
        }
Packit 383869
    }
Packit 383869
    return APR_SUCCESS;
Packit 383869
}
Packit 383869
Packit 383869
APU_DECLARE(apr_bucket *) apr_bucket_pipe_make(apr_bucket *b, apr_file_t *p)
Packit 383869
{
Packit 383869
    /*
Packit 383869
     * A pipe is closed when the end is reached in pipe_bucket_read().  If
Packit 383869
     * the pipe isn't read to the end (e.g., error path), the pipe will be
Packit 383869
     * closed when its pool goes away.
Packit 383869
     *
Packit 383869
     * Note that typically the pipe is allocated from the request pool
Packit 383869
     * so it will disappear when the request is finished. However the
Packit 383869
     * core filter may decide to set aside the tail end of a CGI
Packit 383869
     * response if the connection is pipelined. This turns out not to
Packit 383869
     * be a problem because the core will have read to the end of the
Packit 383869
     * stream so the bucket(s) that it sets aside will be the heap
Packit 383869
     * buckets created by pipe_bucket_read() above.
Packit 383869
     */
Packit 383869
    b->type        = &apr_bucket_type_pipe;
Packit 383869
    b->length      = (apr_size_t)(-1);
Packit 383869
    b->start       = -1;
Packit 383869
    b->data        = p;
Packit 383869
    
Packit 383869
    return b;
Packit 383869
}
Packit 383869
Packit 383869
APU_DECLARE(apr_bucket *) apr_bucket_pipe_create(apr_file_t *p,
Packit 383869
                                                 apr_bucket_alloc_t *list)
Packit 383869
{
Packit 383869
    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
Packit 383869
Packit 383869
    APR_BUCKET_INIT(b);
Packit 383869
    b->free = apr_bucket_free;
Packit 383869
    b->list = list;
Packit 383869
    return apr_bucket_pipe_make(b, p);
Packit 383869
}
Packit 383869
Packit 383869
APU_DECLARE_DATA const apr_bucket_type_t apr_bucket_type_pipe = {
Packit 383869
    "PIPE", 5, APR_BUCKET_DATA, 
Packit 383869
    apr_bucket_destroy_noop,
Packit 383869
    pipe_bucket_read,
Packit 383869
    apr_bucket_setaside_notimpl,
Packit 383869
    apr_bucket_split_notimpl,
Packit 383869
    apr_bucket_copy_notimpl
Packit 383869
};