Coverage Report

Created: 2023-03-26 06:28

/src/httpd/server/headers_bucket.c
Line
Count
Source (jump to first uncovered line)
1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
 * contributor license agreements.  See the NOTICE file distributed with
3
 * this work for additional information regarding copyright ownership.
4
 * The ASF licenses this file to You under the Apache License, Version 2.0
5
 * (the "License"); you may not use this file except in compliance with
6
 * the License.  You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#include "http_protocol.h"
18
#include "apr_buckets.h"
19
#include "apr_strings.h"
20
#if APR_HAVE_STRINGS_H
21
#include <strings.h>
22
#endif
23
24
static apr_status_t dummy_read(apr_bucket *b, const char **str,
25
                               apr_size_t *len, apr_read_type_e block)
26
0
{
27
0
    *str = NULL;
28
0
    *len = 0;
29
0
    return APR_SUCCESS;
30
0
}
31
32
static void request_bucket_destroy(void *data)
33
0
{
34
0
    ap_bucket_request *h = data;
35
36
0
    if (apr_bucket_shared_destroy(h)) {
37
0
        apr_bucket_free(h);
38
0
    }
39
0
}
40
41
AP_DECLARE(apr_bucket *) ap_bucket_request_make(
42
            apr_bucket *b,
43
            const char *method,
44
            const char *uri,
45
            const char *protocol,
46
            apr_table_t *headers,
47
            apr_pool_t *p)
48
0
{
49
0
    return ap_bucket_request_maken(b, apr_pstrdup(p, method),
50
0
                                   apr_pstrdup(p, uri), protocol,
51
0
                                   headers? apr_table_clone(p, headers) : NULL,
52
0
                                   p);
53
0
}
54
55
AP_DECLARE(apr_bucket *) ap_bucket_request_maken(
56
            apr_bucket *b,
57
            const char *method,
58
            const char *uri,
59
            const char *protocol,
60
            apr_table_t *headers,
61
            apr_pool_t *p)
62
0
{
63
0
    ap_bucket_request *h;
64
65
0
    h = apr_bucket_alloc(sizeof(*h), b->list);
66
0
    h->pool = p;
67
0
    h->method = method;
68
0
    h->uri = uri;
69
0
    h->protocol = protocol;
70
0
    h->headers = headers;
71
72
0
    b = apr_bucket_shared_make(b, h, 0, 0);
73
0
    b->type = &ap_bucket_type_request;
74
0
    return b;
75
0
}
76
77
AP_DECLARE(apr_bucket *) ap_bucket_request_create(
78
            const char *method,
79
            const char *uri,
80
            const char *protocol,
81
            apr_table_t *headers,
82
            apr_pool_t *p,
83
            apr_bucket_alloc_t *list)
84
0
{
85
0
    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
86
87
0
    APR_BUCKET_INIT(b);
88
0
    b->free = apr_bucket_free;
89
0
    b->list = list;
90
0
    return ap_bucket_request_make(b, method, uri, protocol, headers, p);
91
0
}
92
93
AP_DECLARE(apr_bucket *) ap_bucket_request_createn(
94
            const char *method,
95
            const char *uri,
96
            const char *protocol,
97
            apr_table_t *headers,
98
            apr_pool_t *p,
99
            apr_bucket_alloc_t *list)
100
0
{
101
0
    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
102
103
0
    APR_BUCKET_INIT(b);
104
0
    b->free = apr_bucket_free;
105
0
    b->list = list;
106
0
    return ap_bucket_request_maken(b, method, uri, protocol, headers, p);
107
0
}
108
109
AP_DECLARE_DATA const apr_bucket_type_t ap_bucket_type_request = {
110
    "REQUEST", 5, APR_BUCKET_METADATA,
111
    request_bucket_destroy,
112
    dummy_read,
113
    apr_bucket_setaside_notimpl,
114
    apr_bucket_split_notimpl,
115
    apr_bucket_shared_copy
116
};
117
118
AP_DECLARE(apr_bucket *) ap_bucket_request_clone(
119
        apr_bucket *source,
120
        apr_pool_t *p,
121
        apr_bucket_alloc_t *list)
122
0
{
123
0
    ap_bucket_request *sreq = source->data;
124
125
0
    AP_DEBUG_ASSERT(AP_BUCKET_IS_REQUEST(source));
126
0
    return ap_bucket_request_create(sreq->method, sreq->uri,
127
0
                                    sreq->protocol, sreq->headers, p, list);
128
0
}
129
130
static void response_bucket_destroy(void *data)
131
0
{
132
0
    ap_bucket_response *h = data;
133
134
0
    if (apr_bucket_shared_destroy(h)) {
135
0
        apr_bucket_free(h);
136
0
    }
137
0
}
138
139
AP_DECLARE(apr_bucket *) ap_bucket_response_make(apr_bucket *b, int status,
140
                                                 const char *reason,
141
                                                 apr_table_t *headers,
142
                                                 apr_table_t *notes,
143
                                                 apr_pool_t *p)
144
0
{
145
0
    ap_bucket_response *h;
146
147
0
    h = apr_bucket_alloc(sizeof(*h), b->list);
148
0
    h->pool = p;
149
0
    h->status = status;
150
0
    h->reason = reason? apr_pstrdup(p, reason) : NULL;
151
0
    h->headers = headers? apr_table_copy(p, headers) : apr_table_make(p, 5);
152
0
    h->notes = notes? apr_table_copy(p, notes) : apr_table_make(p, 5);
153
154
0
    b = apr_bucket_shared_make(b, h, 0, 0);
155
0
    b->type = &ap_bucket_type_response;
156
0
    return b;
157
0
}
158
159
AP_DECLARE(apr_bucket *) ap_bucket_response_create(int status, const char *reason,
160
                                                   apr_table_t *headers,
161
                                                   apr_table_t *notes,
162
                                                   apr_pool_t *p,
163
                                                   apr_bucket_alloc_t *list)
164
0
{
165
0
    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
166
167
0
    APR_BUCKET_INIT(b);
168
0
    b->free = apr_bucket_free;
169
0
    b->list = list;
170
0
    return ap_bucket_response_make(b, status, reason, headers, notes, p);
171
0
}
172
173
AP_DECLARE_DATA const apr_bucket_type_t ap_bucket_type_response = {
174
    "RESPONSE", 5, APR_BUCKET_METADATA,
175
    response_bucket_destroy,
176
    dummy_read,
177
    apr_bucket_setaside_notimpl,
178
    apr_bucket_split_notimpl,
179
    apr_bucket_shared_copy
180
};
181
182
AP_DECLARE(apr_bucket *) ap_bucket_response_clone(apr_bucket *source,
183
                                                  apr_pool_t *p,
184
                                                  apr_bucket_alloc_t *list)
185
0
{
186
0
    ap_bucket_response *sresp = source->data;
187
0
    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
188
0
    ap_bucket_response *h;
189
190
0
    AP_DEBUG_ASSERT(AP_BUCKET_IS_RESPONSE(source));
191
0
    APR_BUCKET_INIT(b);
192
0
    b->free = apr_bucket_free;
193
0
    b->list = list;
194
0
    h = apr_bucket_alloc(sizeof(*h), b->list);
195
0
    h->status = sresp->status;
196
0
    h->reason = sresp->reason? apr_pstrdup(p, sresp->reason) : NULL;
197
0
    h->headers = apr_table_clone(p, sresp->headers);
198
0
    h->notes = apr_table_clone(p, sresp->notes);
199
200
0
    b = apr_bucket_shared_make(b, h, 0, 0);
201
0
    b->type = &ap_bucket_type_response;
202
0
    return b;
203
0
}
204
205
static void headers_bucket_destroy(void *data)
206
0
{
207
0
    ap_bucket_headers *h = data;
208
209
0
    if (apr_bucket_shared_destroy(h)) {
210
0
        apr_bucket_free(h);
211
0
    }
212
0
}
213
214
AP_DECLARE(apr_bucket *) ap_bucket_headers_make(apr_bucket *b,
215
                                                apr_table_t *headers,
216
                                                apr_pool_t *p)
217
0
{
218
0
    ap_bucket_headers *h;
219
220
0
    h = apr_bucket_alloc(sizeof(*h), b->list);
221
0
    h->pool = p;
222
0
    h->headers = headers? apr_table_copy(p, headers) : apr_table_make(p, 5);
223
224
0
    b = apr_bucket_shared_make(b, h, 0, 0);
225
0
    b->type = &ap_bucket_type_headers;
226
0
    return b;
227
0
}
228
229
AP_DECLARE(apr_bucket *) ap_bucket_headers_create(apr_table_t *headers,
230
                                                  apr_pool_t *p,
231
                                                  apr_bucket_alloc_t *list)
232
0
{
233
0
    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
234
235
0
    APR_BUCKET_INIT(b);
236
0
    b->free = apr_bucket_free;
237
0
    b->list = list;
238
0
    return ap_bucket_headers_make(b, headers, p);
239
0
}
240
241
AP_DECLARE_DATA const apr_bucket_type_t ap_bucket_type_headers = {
242
    "HEADERS", 5, APR_BUCKET_METADATA,
243
    headers_bucket_destroy,
244
    dummy_read,
245
    apr_bucket_setaside_notimpl,
246
    apr_bucket_split_notimpl,
247
    apr_bucket_shared_copy
248
};
249
250
AP_DECLARE(apr_bucket *) ap_bucket_headers_clone(apr_bucket *source,
251
                                                 apr_pool_t *p,
252
                                                 apr_bucket_alloc_t *list)
253
0
{
254
0
    ap_bucket_headers *shdrs = source->data;
255
0
    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
256
0
    ap_bucket_headers *h;
257
258
0
    AP_DEBUG_ASSERT(AP_BUCKET_IS_HEADERS(source));
259
0
    APR_BUCKET_INIT(b);
260
0
    b->free = apr_bucket_free;
261
0
    b->list = list;
262
0
    h = apr_bucket_alloc(sizeof(*h), b->list);
263
0
    h->headers = apr_table_clone(p, shdrs->headers);
264
265
0
    b = apr_bucket_shared_make(b, h, 0, 0);
266
0
    b->type = &ap_bucket_type_headers;
267
0
    return b;
268
0
}
269