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