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 socket_bucket_read(apr_bucket *a, const char **str,
20251876Speter                                       apr_size_t *len, apr_read_type_e block)
21251876Speter{
22251876Speter    apr_socket_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_socket_timeout_get(p, &timeout);
29251876Speter        apr_socket_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_socket_recv(p, buf, len);
37251876Speter
38251876Speter    if (block == APR_NONBLOCK_READ) {
39251876Speter        apr_socket_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 socket
48251876Speter     * for later. XXX: Note that more complicated bucket types that
49251876Speter     * refer to data not in memory and must therefore have a read()
50251876Speter     * function similar to this one should be wary of copying this
51251876Speter     * code because if they have a destroy function they probably
52251876Speter     * want to migrate the bucket's subordinate structure from the
53251876Speter     * old bucket to a raw new one and adjust it as appropriate,
54251876Speter     * rather than destroying the old one and creating a completely
55251876Speter     * new bucket.
56251876Speter     *
57251876Speter     * Even if there is nothing more to read, don't close the socket here
58251876Speter     * as we have to use it to send any response :)  We could shut it
59251876Speter     * down for reading, but there is no benefit to doing so.
60251876Speter     */
61251876Speter    if (*len > 0) {
62251876Speter        apr_bucket_heap *h;
63251876Speter        /* Change the current bucket to refer to what we read */
64251876Speter        a = apr_bucket_heap_make(a, buf, *len, apr_bucket_free);
65251876Speter        h = a->data;
66251876Speter        h->alloc_len = APR_BUCKET_BUFF_SIZE; /* note the real buffer size */
67251876Speter        *str = buf;
68251876Speter        APR_BUCKET_INSERT_AFTER(a, apr_bucket_socket_create(p, a->list));
69251876Speter    }
70251876Speter    else {
71251876Speter        apr_bucket_free(buf);
72251876Speter        a = apr_bucket_immortal_make(a, "", 0);
73251876Speter        *str = a->data;
74251876Speter    }
75251876Speter    return APR_SUCCESS;
76251876Speter}
77251876Speter
78251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_socket_make(apr_bucket *b, apr_socket_t *p)
79251876Speter{
80251876Speter    /*
81251876Speter     * XXX: We rely on a cleanup on some pool or other to actually
82251876Speter     * destroy the socket. We should probably explicitly call apr to
83251876Speter     * destroy it instead.
84251876Speter     *
85251876Speter     * Note that typically the socket is allocated from the connection pool
86251876Speter     * so it will disappear when the connection is finished.
87251876Speter     */
88251876Speter    b->type        = &apr_bucket_type_socket;
89251876Speter    b->length      = (apr_size_t)(-1);
90251876Speter    b->start       = -1;
91251876Speter    b->data        = p;
92251876Speter
93251876Speter    return b;
94251876Speter}
95251876Speter
96251876SpeterAPU_DECLARE(apr_bucket *) apr_bucket_socket_create(apr_socket_t *p,
97251876Speter                                                   apr_bucket_alloc_t *list)
98251876Speter{
99251876Speter    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
100251876Speter
101251876Speter    APR_BUCKET_INIT(b);
102251876Speter    b->free = apr_bucket_free;
103251876Speter    b->list = list;
104251876Speter    return apr_bucket_socket_make(b, p);
105251876Speter}
106251876Speter
107251876SpeterAPU_DECLARE_DATA const apr_bucket_type_t apr_bucket_type_socket = {
108251876Speter    "SOCKET", 5, APR_BUCKET_DATA,
109251876Speter    apr_bucket_destroy_noop,
110251876Speter    socket_bucket_read,
111251876Speter    apr_bucket_setaside_notimpl,
112251876Speter    apr_bucket_split_notimpl,
113251876Speter    apr_bucket_copy_notimpl
114251876Speter};
115