diff options
Diffstat (limited to 'src/microhttpd/response.c')
-rw-r--r-- | src/microhttpd/response.c | 136 |
1 files changed, 106 insertions, 30 deletions
diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c index 3bdd2a59..cffb859a 100644 --- a/src/microhttpd/response.c +++ b/src/microhttpd/response.c | |||
@@ -145,6 +145,103 @@ | |||
145 | } while (0) | 145 | } while (0) |
146 | 146 | ||
147 | /** | 147 | /** |
148 | * Add a header or footer line to the response without checking. | ||
149 | * | ||
150 | * It is assumed that parameters are correct. | ||
151 | * | ||
152 | * @param response response to add a header to | ||
153 | * @param kind header or footer | ||
154 | * @param header the header to add, does not need to be zero-terminated | ||
155 | * @param header_len the length of the @a header | ||
156 | * @param content value to add, does not need to be zero-terminated | ||
157 | * @param content_len the length of the @a content | ||
158 | * @return false on error (like out-of-memory), | ||
159 | * true if succeed | ||
160 | */ | ||
161 | bool | ||
162 | MHD_add_response_entry_no_check_ (struct MHD_Response *response, | ||
163 | enum MHD_ValueKind kind, | ||
164 | const char *header, | ||
165 | size_t header_len, | ||
166 | const char *content, | ||
167 | size_t content_len) | ||
168 | { | ||
169 | struct MHD_HTTP_Res_Header *hdr; | ||
170 | |||
171 | mhd_assert (0 != header_len); | ||
172 | mhd_assert (0 != content_len); | ||
173 | if (NULL == (hdr = MHD_calloc_ (1, sizeof (struct MHD_HTTP_Res_Header)))) | ||
174 | 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 | |||
182 | hdr->value = malloc (content_len + 1); | ||
183 | if (NULL != hdr->value) | ||
184 | { | ||
185 | memcpy (hdr->value, content, content_len); | ||
186 | hdr->value[content_len] = 0; | ||
187 | hdr->value_size = content_len; | ||
188 | |||
189 | hdr->kind = kind; | ||
190 | _MHD_insert_header_last (response, hdr); | ||
191 | |||
192 | return true; /* Success exit point */ | ||
193 | } | ||
194 | free (hdr->header); | ||
195 | } | ||
196 | free (hdr); | ||
197 | |||
198 | return false; /* Failure exit point */ | ||
199 | } | ||
200 | |||
201 | |||
202 | /** | ||
203 | * Add a header or footer line to the response. | ||
204 | * | ||
205 | * @param header the header to add, does not need to be zero-terminated | ||
206 | * @param header_len the length of the @a header | ||
207 | * @param content value to add, does not need to be zero-terminated | ||
208 | * @param content_len the length of the @a content | ||
209 | * @return false on error (out-of-memory, invalid header or content), | ||
210 | * true if succeed | ||
211 | */ | ||
212 | static bool | ||
213 | add_response_entry_n (struct MHD_Response *response, | ||
214 | enum MHD_ValueKind kind, | ||
215 | const char *header, | ||
216 | size_t header_len, | ||
217 | const char *content, | ||
218 | size_t content_len) | ||
219 | { | ||
220 | if (NULL == response) | ||
221 | return false; | ||
222 | if (0 == header_len) | ||
223 | return false; | ||
224 | if (0 == content_len) | ||
225 | return false; | ||
226 | if (NULL != memchr (header, '\t', header_len)) | ||
227 | return false; | ||
228 | if (NULL != memchr (header, ' ', header_len)) | ||
229 | return false; | ||
230 | if (NULL != memchr (header, '\r', header_len)) | ||
231 | return false; | ||
232 | if (NULL != memchr (header, '\n', header_len)) | ||
233 | return false; | ||
234 | if (NULL != memchr (content, '\r', content_len)) | ||
235 | return false; | ||
236 | if (NULL != memchr (content, '\n', content_len)) | ||
237 | return false; | ||
238 | |||
239 | return MHD_add_response_entry_no_check_ (response, kind, header, header_len, | ||
240 | content, content_len); | ||
241 | } | ||
242 | |||
243 | |||
244 | /** | ||
148 | * Add a header or footer line to the response. | 245 | * Add a header or footer line to the response. |
149 | * | 246 | * |
150 | * @param response response to add a header to | 247 | * @param response response to add a header to |
@@ -159,38 +256,17 @@ add_response_entry (struct MHD_Response *response, | |||
159 | const char *header, | 256 | const char *header, |
160 | const char *content) | 257 | const char *content) |
161 | { | 258 | { |
162 | struct MHD_HTTP_Res_Header *hdr; | 259 | size_t header_len; |
260 | size_t content_len; | ||
163 | 261 | ||
164 | if ( (NULL == response) || | 262 | if (NULL == content) |
165 | (NULL == header) || | ||
166 | (NULL == content) || | ||
167 | (0 == header[0]) || | ||
168 | (0 == content[0]) || | ||
169 | (NULL != strchr (header, '\t')) || | ||
170 | (NULL != strchr (header, ' ')) || | ||
171 | (NULL != strchr (header, '\r')) || | ||
172 | (NULL != strchr (header, '\n')) || | ||
173 | (NULL != strchr (content, '\r')) || | ||
174 | (NULL != strchr (content, '\n')) ) | ||
175 | return MHD_NO; | 263 | return MHD_NO; |
176 | if (NULL == (hdr = MHD_calloc_ (1, sizeof (struct MHD_HTTP_Res_Header)))) | 264 | |
177 | return MHD_NO; | 265 | header_len = strlen (header); |
178 | if (NULL == (hdr->header = strdup (header))) | 266 | content_len = strlen (content); |
179 | { | 267 | return add_response_entry_n (response, kind, header, |
180 | free (hdr); | 268 | header_len, content, |
181 | return MHD_NO; | 269 | content_len) ? MHD_YES : MHD_NO; |
182 | } | ||
183 | hdr->header_size = strlen (header); | ||
184 | if (NULL == (hdr->value = strdup (content))) | ||
185 | { | ||
186 | free (hdr->header); | ||
187 | free (hdr); | ||
188 | return MHD_NO; | ||
189 | } | ||
190 | hdr->value_size = strlen (content); | ||
191 | hdr->kind = kind; | ||
192 | _MHD_insert_header_last (response, hdr); | ||
193 | return MHD_YES; | ||
194 | } | 270 | } |
195 | 271 | ||
196 | 272 | ||