xref: /lighttpd1.4/src/reqpool.c (revision df362494)
1 /*
2  * reqpool - request objects
3  *
4  * Copyright(c) 2020 Glenn Strauss gstrauss()gluelogic.com  All rights reserved
5  * License: BSD 3-clause (same as lighttpd)
6  */
7 #include "first.h"
8 #include "reqpool.h"
9 
10 #include <stdlib.h>
11 #include <string.h>
12 
13 #include "base.h"
14 #include "buffer.h"
15 #include "chunk.h"
16 #include "plugins.h"
17 #include "plugin_config.h"
18 #include "request.h"
19 #include "response.h"
20 
21 #ifdef HAVE_PCRE2_H
22 #define PCRE2_CODE_UNIT_WIDTH 8
23 #include <pcre2.h>
24 #endif
25 
26 
27 static const request_config *request_config_defaults;
28 
29 
30 void
31 request_config_set_defaults (const request_config *config_defaults)
32 {
33     request_config_defaults = config_defaults;
34 }
35 
36 
37 __attribute_noinline__
38 void
39 request_config_reset (request_st * const r)
40 {
41     memcpy(&r->conf, request_config_defaults, sizeof(request_config));
42 }
43 
44 
45 void
46 request_init_data (request_st * const r, connection * const con, server * const srv)
47 {
48     chunkqueue_init(&r->write_queue);
49     chunkqueue_init(&r->read_queue);
50     chunkqueue_init(&r->reqbody_queue);
51 
52     r->http_method = HTTP_METHOD_UNSET;
53     r->http_version = HTTP_VERSION_UNSET;
54     r->resp_header_len = 0;
55     r->loops_per_request = 0;
56     r->con = con;
57     r->tmp_buf = srv->tmp_buf;
58     r->resp_body_scratchpad = -1;
59     r->server_name = &r->uri.authority;
60 
61     /* init plugin-specific per-request structures */
62     r->plugin_ctx = ck_calloc(srv->plugins.used + 1, sizeof(void *));
63     r->cond_cache = ck_calloc(srv->config_context->used, sizeof(cond_cache_t));
64 
65   #ifdef HAVE_PCRE
66     if (srv->config_captures) {
67         r->cond_captures = srv->config_captures;
68         r->cond_match = ck_calloc(srv->config_captures, sizeof(cond_match_t *));
69         r->cond_match_data = ck_calloc(srv->config_captures,
70                                        sizeof(cond_match_t));
71     }
72   #endif
73 
74     request_config_reset(r);
75 }
76 
77 
78 void
79 request_reset (request_st * const r)
80 {
81     plugins_call_handle_request_reset(r);
82 
83     http_response_reset(r);
84 
85     r->loops_per_request = 0;
86     r->keep_alive = 0;
87 
88     r->h2state = 0; /* H2_STATE_IDLE */
89     r->h2id = 0;
90     r->http_method = HTTP_METHOD_UNSET;
91     r->http_version = HTTP_VERSION_UNSET;
92 
93     /*con->proto_default_port = 80;*//*set to default in connection_accepted()*/
94 
95     r->http_host = NULL;
96     r->reqbody_length = 0;
97     r->te_chunked = 0;
98     r->resp_body_scratchpad = -1;
99     r->rqst_htags = 0;
100 
101     r->async_callback = 0;
102     r->error_handler_saved_status = 0;
103     /*r->error_handler_saved_method = HTTP_METHOD_UNSET;*/
104     /*(error_handler_saved_method value is not valid
105      * unless error_handler_saved_status is set)*/
106     r->h2_connect_ext = 0;
107 
108     buffer_clear(&r->uri.scheme);
109 
110     if (r->rqst_header_len <= BUFFER_MAX_REUSE_SIZE) {
111         r->rqst_headers.used = 0;
112         /* (Note: total header size not recalculated on HANDLER_COMEBACK
113          *  even if other request headers changed during processing)
114          * (While this might delay release of larger buffers, it is not
115          *  expected to be the general case.  For those systems where it
116          *  is a typical case, the larger buffers are likely to be reused) */
117         buffer_clear(&r->target);
118         buffer_clear(&r->pathinfo);
119         /*buffer_clear(&r->target_orig);*/  /* reset later; used by mod_status*/
120         /*buffer_clear(&r->uri.path);*/     /* reset later; used by mod_status*/
121         /*buffer_clear(&r->uri.query);*/    /* reset later; used by mod_status*/
122         /*buffer_clear(&r->uri.authority);*//* reset later; used by mod_status*/
123         /*buffer_clear(&r->server_name_buf);*//* reset when used */
124     }
125     else {
126         buffer_reset(&r->target);
127         buffer_reset(&r->pathinfo);
128         /*buffer_reset(&r->target_orig);*/  /* reset later; used by mod_status*/
129         /*buffer_reset(&r->uri.path);*/     /* reset later; used by mod_status*/
130         /*buffer_reset(&r->uri.query);*/    /* reset later; used by mod_status*/
131         /*buffer_clear(&r->uri.authority);*//* reset later; used by mod_status*/
132         /*buffer_clear(&r->server_name_buf);*//* reset when used */
133         array_reset_data_strings(&r->rqst_headers);
134     }
135     r->rqst_header_len = 0;
136     if (0 != r->env.used)
137         array_reset_data_strings(&r->env);
138 
139     chunkqueue_reset(&r->reqbody_queue);
140     /* r->read_queue, r->write_queue are shared with con for HTTP/1.1
141      * but are different than con->read_queue, con->write_queue for HTTP/2
142      * For HTTP/1.1, when &r->read_queue == con->read_queue, r->read_queue
143      * is not cleared between requests since it might contain subsequent
144      * requests.  (see also request_release()) */
145 
146     /* The cond_cache gets reset in response.c */
147     /* config_cond_cache_reset(r); */
148 
149     request_config_reset(r);
150 }
151 
152 
153 #if 0 /* DEBUG_DEV */
154 __attribute_cold__
155 static void request_plugin_ctx_check(request_st * const r, server * const srv) {
156     /* plugins should have cleaned themselves up */
157     for (uint32_t i = 0, used = srv->plugins.used; i < used; ++i) {
158         plugin *p = ((plugin **)(srv->plugins.ptr))[i];
159         plugin_data_base *pd = p->data;
160         if (!pd) continue;
161         if (NULL == r->plugin_ctx[pd->id]
162             && NULL == r->con->plugin_ctx[pd->id]) continue;
163         log_error(r->conf.errh, __FILE__, __LINE__,
164           "missing cleanup in %s", p->name);
165         r->plugin_ctx[pd->id] = NULL;
166         r->con->plugin_ctx[pd->id] = NULL;
167     }
168 }
169 #endif
170 
171 
172 void
173 request_reset_ex (request_st * const r)
174 {
175   #if 0 /* DEBUG_DEV */
176     /* plugins should have cleaned themselves up (id range: [1,used]) */
177     connection * const con = r->con;
178     server * const srv = con->srv;
179     for (uint32_t i = 1; i <= srv->plugins.used; ++i) {
180         if (NULL != r->plugin_ctx[i] || NULL != con->plugin_ctx[i]) {
181             request_plugin_ctx_check(r, srv);
182             break;
183         }
184     }
185   #endif
186 
187     r->server_name = &r->uri.authority;
188     buffer_clear(&r->uri.authority);
189     buffer_reset(&r->uri.path);
190     buffer_reset(&r->uri.query);
191     buffer_reset(&r->physical.path);
192     buffer_reset(&r->physical.rel_path);
193     buffer_reset(&r->target_orig);
194     buffer_reset(&r->target);       /*(see comments in request_reset())*/
195     buffer_reset(&r->pathinfo);     /*(see comments in request_reset())*/
196 
197     /* preserve; callers must handle changes */
198     /*r->state = CON_STATE_CONNECT;*/
199 }
200 
201 
202 void
203 request_free_data (request_st * const r)
204 {
205     chunkqueue_reset(&r->reqbody_queue);
206     chunkqueue_reset(&r->write_queue);
207     chunkqueue_reset(&r->read_queue);
208     array_free_data(&r->rqst_headers);
209     array_free_data(&r->resp_headers);
210     array_free_data(&r->env);
211 
212     free(r->target.ptr);
213     free(r->target_orig.ptr);
214 
215     free(r->uri.scheme.ptr);
216     free(r->uri.authority.ptr);
217     free(r->uri.path.ptr);
218     free(r->uri.query.ptr);
219 
220     free(r->physical.doc_root.ptr);
221     free(r->physical.path.ptr);
222     free(r->physical.basedir.ptr);
223     free(r->physical.rel_path.ptr);
224 
225     free(r->pathinfo.ptr);
226     free(r->server_name_buf.ptr);
227 
228     free(r->plugin_ctx);
229     free(r->cond_cache);
230   #ifdef HAVE_PCRE
231     if (r->cond_match_data) {
232         for (int i = 0, used = r->cond_captures; i < used; ++i) {
233           #ifdef HAVE_PCRE2_H
234             if (r->cond_match_data[i].match_data)
235                 pcre2_match_data_free(r->cond_match_data[i].match_data);
236           #else /* HAVE_PCRE_H */
237             if (r->cond_match_data[i].matches)
238                 free(r->cond_match_data[i].matches);
239           #endif
240         }
241         free(r->cond_match_data);
242         free(r->cond_match);
243     }
244   #endif
245 
246     /* note: r is not zeroed here and r is not freed here */
247 }
248 
249 
250 /* linked list of (request_st *) cached for reuse */
251 static request_st *reqpool;
252 
253 
254 void
255 request_pool_free (void)
256 {
257     while (reqpool) {
258         request_st * const r = reqpool;
259         reqpool = (request_st *)r->con; /*(reuse r->con as next ptr)*/
260         request_free_data(r);
261         free(r);
262     }
263 }
264 
265 
266 void
267 request_release (request_st * const r)
268 {
269     /* (For HTTP/1.1, r == &con->request, and so request_release() not called)
270      * r->read_queue, r->write_queue are shared with con for HTTP/1.1
271      * but are different than con->read_queue, con->write_queue for HTTP/2
272      * For HTTP/1.1, when &r->read_queue == con->read_queue, r->read_queue
273      * is not cleared between requests since it might contain subsequent
274      * requests.  (see also request_reset()) */
275     chunkqueue_reset(&r->read_queue);
276 
277     /*(r->cond_cache and r->cond_match are re-init in h2_init_stream())*/
278 
279     request_reset(r);
280     request_reset_ex(r);
281     r->state = CON_STATE_CONNECT;
282 
283     r->con = (connection *)reqpool; /*(reuse r->con as next ptr)*/
284     reqpool = r;
285 }
286 
287 
288 request_st *
289 request_acquire (connection * const con)
290 {
291     request_st *r = reqpool;
292     if (r) {
293         reqpool = (request_st *)r->con; /*(reuse r->con as next ptr)*/
294     }
295     else {
296         r = ck_calloc(1, sizeof(request_st));
297         request_init_data(r, con, con->srv);
298     }
299 
300     r->con = con;
301     r->tmp_buf = con->srv->tmp_buf;
302     return r;
303 }
304