1251876Speter/* Licensed to the Apache Software Foundation (ASF) under one or more
2251876Speter * contributor license agreements.  See the NOTICE file distributed with
3251876Speter * this work for additional information regarding copyright ownership.
4251876Speter * The ASF licenses this file to You under the Apache License, Version 2.0
5251876Speter * (the "License"); you may not use this file except in compliance with
6251876Speter * the License.  You may obtain a copy of the License at
7251876Speter *
8251876Speter *     http://www.apache.org/licenses/LICENSE-2.0
9251876Speter *
10251876Speter * Unless required by applicable law or agreed to in writing, software
11251876Speter * distributed under the License is distributed on an "AS IS" BASIS,
12251876Speter * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13251876Speter * See the License for the specific language governing permissions and
14251876Speter * limitations under the License.
15251876Speter */
16251876Speter
17251876Speter#include "apr_buckets.h"
18251876Speter
19251876Speterstatic apr_status_t pipe_bucket_read(apr_bucket *a, const char **str,
20251876Speter                                     apr_size_t *len, apr_read_type_e block)
21251876Speter{
22251876Speter    apr_file_t *p = a->data;
23251876Speter    char *buf;
24251876Speter    apr_status_t rv;
25251876Speter    apr_interval_time_t timeout;
26251876Speter
27251876Speter    if (block == APR_NONBLOCK_READ) {
28251876Speter        apr_file_pipe_timeout_get(p, &timeout);
29251876Speter        apr_file_pipe_timeout_set(p, 0);
30251876Speter    }
31251876Speter
32251876Speter    *str = NULL;
33251876Speter    *len = APR_BUCKET_BUFF_SIZE;
34251876Speter    buf = apr_bucket_alloc(*len, a->list); /* XXX: check for failure? */
35251876Speter
36251876Speter    rv = apr_file_read(p, buf, len);
37251876Speter
38251876Speter    if (block == APR_NONBLOCK_READ) {
39251876Speter        apr_file_pipe_timeout_set(p, timeout);
40251876Speter    }
41251876Speter
42251876Speter    if (rv != APR_SUCCESS && rv != APR_EOF) {
43251876Speter        apr_bucket_free(buf);
44251876Speter        return rv;
45251876Speter    }
46251876Speter    /*
47251876Speter     * If there's more to read we have to keep the rest of the pipe
48251876Speter     * for later.  Otherwise, we'll close the pipe.
49251876Speter     * XXX: Note that more complicated bucket types that
50251876Speter     * refer to data not in memory and must therefore have a read()
51251876Speter     * function similar to this one should be wary of copying this
52251876Speter     * code because if they have a destroy function they probably
53251876Speter     * want to migrate the bucket's subordinate structure from the
54251876Speter     * old bucket to a raw new one and adjust it as appropriate,
55251876Speter     * rather than destroying the old one and creating a completely
56251876Speter     * new bucket.
57251876Speter     */
58251876Speter    if (*len > 0) {
59251876Speter        apr_bucket_heap *h;
60251876Speter        /* Change the current bucket to refer to what we read */
61251876Speter        a = apr_bucket_heap_make(a, buf, *len, apr_bucket_free);
62251876Speter        h = a->data;
63251876Speter        h->alloc_len = APR_BUCKET_BUFF_SIZE; /* note the real buffer size */
64251876Speter        *str = buf;
65251876Speter        APR_BUCKET_INSERT_AFTER(a, apr_bucket_pipe_create(p, a->list));
66251876Speter    }
67251876Speter    else {
68251876Speter        apr_bucket_free(buf);
69251876Speter        a = apr_bucket_immortal_make(a, "", 0);
70251876Speter        *str = a->data;
71251876Speter        if (rv == APR_EOF) {
72251876Speter            apr_file_close(p);
73251876Speter        }
74251876Speter    }
75251876Speter    return APR_SUCCESS;
76251876Speter}
77251876Speter
78251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_pipe_make(apr_bucket *b, apr_file_t *p)
79251876Speter{
80251876Speter    /*
81251876Speter     * A pipe is closed when the end is reached in pipe_bucket_read().  If
82251876Speter     * the pipe isn't read to the end (e.g., error path), the pipe will be
83251876Speter     * closed when its pool goes away.
84251876Speter     *
85251876Speter     * Note that typically the pipe is allocated from the request pool
86251876Speter     * so it will disappear when the request is finished. However the
87251876Speter     * core filter may decide to set aside the tail end of a CGI
88251876Speter     * response if the connection is pipelined. This turns out not to
89251876Speter     * be a problem because the core will have read to the end of the
90251876Speter     * stream so the bucket(s) that it sets aside will be the heap
91251876Speter     * buckets created by pipe_bucket_read() above.
92251876Speter     */
93251876Speter    b->type        = &apr_bucket_type_pipe;
94251876Speter    b->length      = (apr_size_t)(-1);
95251876Speter    b->start       = -1;
96251876Speter    b->data        = p;
97251876Speter
98251876Speter    return b;
99251876Speter}
100251876Speter
101251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_pipe_create(apr_file_t *p,
102251876Speter                                                 apr_bucket_alloc_t *list)
103251876Speter{
104251876Speter    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
105251876Speter
106251876Speter    APR_BUCKET_INIT(b);
107251876Speter    b->free = apr_bucket_free;
108251876Speter    b->list = list;
109251876Speter    return apr_bucket_pipe_make(b, p);
110251876Speter}
111251876Speter
112251876SpeterAPU_DECLARE_DATA const apr_bucket_type_t apr_bucket_type_pipe = {
113251876Speter    "PIPE", 5, APR_BUCKET_DATA,
114251876Speter    apr_bucket_destroy_noop,
115251876Speter    pipe_bucket_read,
116251876Speter    apr_bucket_setaside_notimpl,
117251876Speter    apr_bucket_split_notimpl,
118251876Speter    apr_bucket_copy_notimpl
119251876Speter};
120