response_funcs.c (4573B)
1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ 2 /* 3 This file is part of GNU libmicrohttpd. 4 Copyright (C) 2024 Evgeny Grin (Karlson2k) 5 6 GNU libmicrohttpd is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public 8 License as published by the Free Software Foundation; either 9 version 2.1 of the License, or (at your option) any later version. 10 11 GNU libmicrohttpd is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 Alternatively, you can redistribute GNU libmicrohttpd and/or 17 modify it under the terms of the GNU General Public License as 18 published by the Free Software Foundation; either version 2 of 19 the License, or (at your option) any later version, together 20 with the eCos exception, as follows: 21 22 As a special exception, if other files instantiate templates or 23 use macros or inline functions from this file, or you compile this 24 file and link it with other works to produce a work based on this 25 file, this file does not by itself cause the resulting work to be 26 covered by the GNU General Public License. However the source code 27 for this file must still be made available in accordance with 28 section (3) of the GNU General Public License v2. 29 30 This exception does not invalidate any other reasons why a work 31 based on this file might be covered by the GNU General Public 32 License. 33 34 You should have received copies of the GNU Lesser General Public 35 License and the GNU General Public License along with this library; 36 if not, see <https://www.gnu.org/licenses/>. 37 */ 38 39 /** 40 * @file src/mhd2/response_funcs.c 41 * @brief The definition of the internal response helper functions 42 * @author Karlson2k (Evgeny Grin) 43 */ 44 45 #include "mhd_sys_options.h" 46 47 #include "sys_malloc.h" 48 49 #include "mhd_assert.h" 50 #include "sys_null_macro.h" 51 #include "mhd_response.h" 52 #include "response_funcs.h" 53 #include "mhd_locks.h" 54 #include "response_options.h" 55 56 #include "mhd_atomic_counter.h" 57 58 59 MHD_INTERNAL 60 MHD_FN_PAR_NONNULL_ALL_ bool 61 response_make_reusable (struct MHD_Response *restrict r) 62 { 63 mhd_assert (! r->reuse.reusable); 64 mhd_assert (! r->frozen); 65 mhd_assert (NULL != r->settings); 66 67 if (mhd_mutex_init (&(r->reuse.settings_lock))) 68 { 69 if (mhd_atomic_counter_init (&(r->reuse.counter), 1)) 70 { 71 r->reuse.reusable = true; 72 return true; 73 } 74 (void) mhd_mutex_destroy (&(r->reuse.settings_lock)); 75 } 76 return false; 77 } 78 79 80 MHD_INTERNAL 81 MHD_FN_PAR_NONNULL_ALL_ void 82 mhd_response_deinit_reusable (struct MHD_Response *restrict r) 83 { 84 mhd_assert (r->reuse.reusable); 85 mhd_assert (0 == mhd_atomic_counter_get (&(r->reuse.counter))); 86 87 mhd_atomic_counter_deinit (&(r->reuse.counter)); 88 mhd_mutex_destroy_chk (&(r->reuse.settings_lock)); 89 } 90 91 92 static MHD_FN_PAR_NONNULL_ALL_ void 93 response_set_properties (struct MHD_Response *restrict r) 94 { 95 struct ResponseOptions *restrict const s = r->settings; 96 mhd_assert (NULL != s); 97 98 r->cfg.head_only = s->head_only_response; 99 if (s->http_1_0_compatible_strict) 100 { 101 r->cfg.close_forced = true; 102 r->cfg.chunked = false; 103 r->cfg.mode_1_0 = s->http_1_0_server; 104 } 105 else if (s->http_1_0_server) 106 { 107 r->cfg.close_forced = s->conn_close || (MHD_SIZE_UNKNOWN == r->cntn_size); 108 r->cfg.chunked = false; 109 r->cfg.mode_1_0 = true; 110 } 111 else 112 { 113 r->cfg.close_forced = s->conn_close; 114 r->cfg.chunked = s->chunked_enc || (MHD_SIZE_UNKNOWN == r->cntn_size); 115 r->cfg.mode_1_0 = false; 116 } 117 118 r->cfg.cnt_len_by_app = s->insanity_header_content_length; // TODO: set only if "content-lengh" header is used 119 120 // TODO: calculate size of the headers and the "Connection:" header 121 122 r->frozen = true; 123 124 r->settings = NULL; 125 free (s); 126 } 127 128 129 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ void 130 mhd_response_check_frozen_freeze (struct MHD_Response *restrict response) 131 { 132 bool need_unlock; 133 if (response->frozen) 134 return; 135 136 if (response->reuse.reusable) 137 { 138 mhd_mutex_lock_chk (&(response->reuse.settings_lock)); 139 need_unlock = true; 140 } 141 else 142 need_unlock = false; 143 144 if (! response->frozen)/* Re-check under the lock */ 145 { 146 mhd_assert ((! response->reuse.reusable) || \ 147 (1 == mhd_atomic_counter_get (&(response->reuse.counter)))); 148 response_set_properties (response); 149 } 150 151 if (need_unlock) 152 mhd_mutex_unlock_chk (&(response->reuse.settings_lock)); 153 }