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