diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-11-16 12:02:11 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-12-19 18:13:43 +0300 |
commit | e4db5b26c71fa276437d7ddafa0b36c9a069f021 (patch) | |
tree | adfaf4eea346adc94e1f41f0e419face216fade7 | |
parent | 3444792f14d96a58648add92b8f3b70e4babf772 (diff) | |
download | libmicrohttpd-e4db5b26c71fa276437d7ddafa0b36c9a069f021.tar.gz libmicrohttpd-e4db5b26c71fa276437d7ddafa0b36c9a069f021.zip |
response.c: added new internal function to avoid repetitive malloc()s
-rw-r--r-- | src/microhttpd/response.c | 81 | ||||
-rw-r--r-- | src/microhttpd/response.h | 27 |
2 files changed, 89 insertions, 19 deletions
diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c index d352a804..b29f48f3 100644 --- a/src/microhttpd/response.c +++ b/src/microhttpd/response.c | |||
@@ -145,6 +145,51 @@ | |||
145 | } while (0) | 145 | } while (0) |
146 | 146 | ||
147 | /** | 147 | /** |
148 | * Add preallocated strings a header or footer line to the response without | ||
149 | * checking. | ||
150 | * | ||
151 | * Header/footer strings are not checked and assumed to be correct. | ||
152 | * | ||
153 | * The string must not be statically allocated! | ||
154 | * The strings must be malloc()'ed and zero terminated. The strings will | ||
155 | * be free()'ed when the response is destroyed. | ||
156 | * | ||
157 | * @param response response to add a header to | ||
158 | * @param kind header or footer | ||
159 | * @param header the header string to add, must be malloc()'ed and | ||
160 | * zero-terminated | ||
161 | * @param header_len the length of the @a header | ||
162 | * @param content the value string to add, must be malloc()'ed and | ||
163 | * zero-terminated | ||
164 | * @param content_len the length of the @a content | ||
165 | */ | ||
166 | bool | ||
167 | MHD_add_response_entry_no_alloc_ (struct MHD_Response *response, | ||
168 | enum MHD_ValueKind kind, | ||
169 | char *header, | ||
170 | size_t header_len, | ||
171 | char *content, | ||
172 | size_t content_len) | ||
173 | { | ||
174 | struct MHD_HTTP_Res_Header *hdr; | ||
175 | |||
176 | mhd_assert (0 != header_len); | ||
177 | mhd_assert (0 != content_len); | ||
178 | if (NULL == (hdr = MHD_calloc_ (1, sizeof (struct MHD_HTTP_Res_Header)))) | ||
179 | return false; | ||
180 | |||
181 | hdr->header = header; | ||
182 | hdr->header_size = header_len; | ||
183 | hdr->value = content; | ||
184 | hdr->value_size = content_len; | ||
185 | hdr->kind = kind; | ||
186 | _MHD_insert_header_last (response, hdr); | ||
187 | |||
188 | return true; /* Success exit point */ | ||
189 | } | ||
190 | |||
191 | |||
192 | /** | ||
148 | * Add a header or footer line to the response without checking. | 193 | * Add a header or footer line to the response without checking. |
149 | * | 194 | * |
150 | * It is assumed that parameters are correct. | 195 | * It is assumed that parameters are correct. |
@@ -166,34 +211,32 @@ MHD_add_response_entry_no_check_ (struct MHD_Response *response, | |||
166 | const char *content, | 211 | const char *content, |
167 | size_t content_len) | 212 | size_t content_len) |
168 | { | 213 | { |
169 | struct MHD_HTTP_Res_Header *hdr; | 214 | char *header_malloced; |
215 | char *value_malloced; | ||
170 | 216 | ||
171 | mhd_assert (0 != header_len); | 217 | mhd_assert (0 != header_len); |
172 | mhd_assert (0 != content_len); | 218 | mhd_assert (0 != content_len); |
173 | if (NULL == (hdr = MHD_calloc_ (1, sizeof (struct MHD_HTTP_Res_Header)))) | 219 | header_malloced = malloc (header_len + 1); |
220 | if (NULL == header_malloced) | ||
174 | return false; | 221 | return false; |
175 | hdr->header = malloc (header_len + 1); | ||
176 | if (NULL != hdr->header) | ||
177 | { | ||
178 | memcpy (hdr->header, header, header_len); | ||
179 | hdr->header[header_len] = 0; | ||
180 | hdr->header_size = header_len; | ||
181 | 222 | ||
182 | hdr->value = malloc (content_len + 1); | 223 | memcpy (header_malloced, header, header_len); |
183 | if (NULL != hdr->value) | 224 | header_malloced[header_len] = 0; |
184 | { | ||
185 | memcpy (hdr->value, content, content_len); | ||
186 | hdr->value[content_len] = 0; | ||
187 | hdr->value_size = content_len; | ||
188 | 225 | ||
189 | hdr->kind = kind; | 226 | value_malloced = malloc (content_len + 1); |
190 | _MHD_insert_header_last (response, hdr); | 227 | if (NULL != value_malloced) |
228 | { | ||
229 | memcpy (value_malloced, content, content_len); | ||
230 | value_malloced[content_len] = 0; | ||
191 | 231 | ||
232 | if (MHD_add_response_entry_no_alloc_ (response, kind, | ||
233 | header_malloced, header_len, | ||
234 | value_malloced, content_len)) | ||
192 | return true; /* Success exit point */ | 235 | return true; /* Success exit point */ |
193 | } | 236 | |
194 | free (hdr->header); | 237 | free (value_malloced); |
195 | } | 238 | } |
196 | free (hdr); | 239 | free (header_malloced); |
197 | 240 | ||
198 | return false; /* Failure exit point */ | 241 | return false; /* Failure exit point */ |
199 | } | 242 | } |
diff --git a/src/microhttpd/response.h b/src/microhttpd/response.h index 4f9c32c9..ad614c1b 100644 --- a/src/microhttpd/response.h +++ b/src/microhttpd/response.h | |||
@@ -95,4 +95,31 @@ MHD_add_response_entry_no_check_ (struct MHD_Response *response, | |||
95 | const char *content, | 95 | const char *content, |
96 | size_t content_len); | 96 | size_t content_len); |
97 | 97 | ||
98 | /** | ||
99 | * Add preallocated strings a header or footer line to the response without | ||
100 | * checking. | ||
101 | * | ||
102 | * Header/footer strings are not checked and assumed to be correct. | ||
103 | * | ||
104 | * The string must not be statically allocated! | ||
105 | * The strings must be malloc()'ed and zero terminated. The strings will | ||
106 | * be free()'ed when the response is destroyed. | ||
107 | * | ||
108 | * @param response response to add a header to | ||
109 | * @param kind header or footer | ||
110 | * @param header the header string to add, must be malloc()'ed and | ||
111 | * zero-terminated | ||
112 | * @param header_len the length of the @a header | ||
113 | * @param content the value string to add, must be malloc()'ed and | ||
114 | * zero-terminated | ||
115 | * @param content_len the length of the @a content | ||
116 | */ | ||
117 | bool | ||
118 | MHD_add_response_entry_no_alloc_ (struct MHD_Response *response, | ||
119 | enum MHD_ValueKind kind, | ||
120 | char *header, | ||
121 | size_t header_len, | ||
122 | char *content, | ||
123 | size_t content_len); | ||
124 | |||
98 | #endif | 125 | #endif |