| 1 |
/* |
| 2 |
This file is part of libmicrohttpd |
| 3 |
Copyright (C) 2007-2021 Daniel Pittman and Christian Grothoff |
| 4 |
Copyright (C) 2015-2023 Evgeny Grin (Karlson2k) |
| 5 |
|
| 6 |
This library 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 |
This library 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 |
You should have received a copy of the GNU Lesser General Public |
| 17 |
License along with this library; if not, write to the Free Software |
| 18 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
*/ |
| 20 |
/** |
| 21 |
* @file response.c |
| 22 |
* @brief Methods for managing response objects |
| 23 |
* @author Daniel Pittman |
| 24 |
* @author Christian Grothoff |
| 25 |
* @author Karlson2k (Evgeny Grin) |
| 26 |
*/ |
| 27 |
|
| 28 |
#define MHD_NO_DEPRECATION 1 |
| 29 |
|
| 30 |
#include "mhd_options.h" |
| 31 |
#ifdef HAVE_SYS_IOCTL_H |
| 32 |
#include <sys/ioctl.h> |
| 33 |
#endif /* HAVE_SYS_IOCTL_H */ |
| 34 |
#if defined(_WIN32) && ! defined(__CYGWIN__) |
| 35 |
#include <windows.h> |
| 36 |
#endif /* _WIN32 && !__CYGWIN__ */ |
| 37 |
|
| 38 |
#include "internal.h" |
| 39 |
#include "response.h" |
| 40 |
#include "mhd_limits.h" |
| 41 |
#include "mhd_sockets.h" |
| 42 |
#include "mhd_itc.h" |
| 43 |
#include "mhd_str.h" |
| 44 |
#include "connection.h" |
| 45 |
#include "memorypool.h" |
| 46 |
#include "mhd_send.h" |
| 47 |
#include "mhd_compat.h" |
| 48 |
#include "mhd_assert.h" |
| 49 |
|
| 50 |
|
| 51 |
#if defined(MHD_W32_MUTEX_) |
| 52 |
#ifndef WIN32_LEAN_AND_MEAN |
| 53 |
#define WIN32_LEAN_AND_MEAN 1 |
| 54 |
#endif /* !WIN32_LEAN_AND_MEAN */ |
| 55 |
#include <windows.h> |
| 56 |
#endif /* MHD_W32_MUTEX_ */ |
| 57 |
#if defined(_WIN32) |
| 58 |
#include <io.h> /* for lseek(), read() */ |
| 59 |
#endif /* _WIN32 */ |
| 60 |
|
| 61 |
|
| 62 |
/** |
| 63 |
* Size of single file read operation for |
| 64 |
* file-backed responses. |
| 65 |
*/ |
| 66 |
#ifndef MHD_FILE_READ_BLOCK_SIZE |
| 67 |
#ifdef _WIN32 |
| 68 |
#define MHD_FILE_READ_BLOCK_SIZE 16384 /* 16k */ |
| 69 |
#else /* _WIN32 */ |
| 70 |
#define MHD_FILE_READ_BLOCK_SIZE 4096 /* 4k */ |
| 71 |
#endif /* _WIN32 */ |
| 72 |
#endif /* !MHD_FD_BLOCK_SIZE */ |
| 73 |
|
| 74 |
/** |
| 75 |
* Insert a new header at the first position of the response |
| 76 |
*/ |
| 77 |
#define _MHD_insert_header_first(presponse, phdr) do { \ |
| 78 |
mhd_assert (NULL == phdr->next); \ |
| 79 |
mhd_assert (NULL == phdr->prev); \ |
| 80 |
if (NULL == presponse->first_header) \ |
| 81 |
{ \ |
| 82 |
mhd_assert (NULL == presponse->last_header); \ |
| 83 |
presponse->first_header = phdr; \ |
| 84 |
presponse->last_header = phdr; \ |
| 85 |
} \ |
| 86 |
else \ |
| 87 |
{ \ |
| 88 |
mhd_assert (NULL != presponse->last_header); \ |
| 89 |
presponse->first_header->prev = phdr; \ |
| 90 |
phdr->next = presponse->first_header; \ |
| 91 |
presponse->first_header = phdr; \ |
| 92 |
} \ |
| 93 |
} while (0) |
| 94 |
|
| 95 |
/** |
| 96 |
* Insert a new header at the last position of the response |
| 97 |
*/ |
| 98 |
#define _MHD_insert_header_last(presponse, phdr) do { \ |
| 99 |
mhd_assert (NULL == phdr->next); \ |
| 100 |
mhd_assert (NULL == phdr->prev); \ |
| 101 |
if (NULL == presponse->last_header) \ |
| 102 |
{ \ |
| 103 |
mhd_assert (NULL == presponse->first_header); \ |
| 104 |
presponse->last_header = phdr; \ |
| 105 |
presponse->first_header = phdr; \ |
| 106 |
} \ |
| 107 |
else \ |
| 108 |
{ \ |
| 109 |
mhd_assert (NULL != presponse->first_header); \ |
| 110 |
presponse->last_header->next = phdr; \ |
| 111 |
phdr->prev = presponse->last_header; \ |
| 112 |
presponse->last_header = phdr; \ |
| 113 |
} \ |
| 114 |
} while (0) |
| 115 |
|
| 116 |
|
| 117 |
/** |
| 118 |
* Remove a header from the response |
| 119 |
*/ |
| 120 |
#define _MHD_remove_header(presponse, phdr) do { \ |
| 121 |
mhd_assert (NULL != presponse->first_header); \ |
| 122 |
mhd_assert (NULL != presponse->last_header); \ |
| 123 |
if (NULL == phdr->prev) \ |
| 124 |
{ \ |
| 125 |
mhd_assert (phdr == presponse->first_header); \ |
| 126 |
presponse->first_header = phdr->next; \ |
| 127 |
} \ |
| 128 |
else \ |
| 129 |
{ \ |
| 130 |
mhd_assert (phdr != presponse->first_header); \ |
| 131 |
mhd_assert (phdr == phdr->prev->next); \ |
| 132 |
phdr->prev->next = phdr->next; \ |
| 133 |
} \ |
| 134 |
if (NULL == phdr->next) \ |
| 135 |
{ \ |
| 136 |
mhd_assert (phdr == presponse->last_header); \ |
| 137 |
presponse->last_header = phdr->prev; \ |
| 138 |
} \ |
| 139 |
else \ |
| 140 |
{ \ |
| 141 |
mhd_assert (phdr != presponse->last_header); \ |
| 142 |
mhd_assert (phdr == phdr->next->prev); \ |
| 143 |
phdr->next->prev = phdr->prev; \ |
| 144 |
} \ |
| 145 |
} while (0) |
| 146 |
|
| 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 |
/** |
| 193 |
* Add a header or footer line to the response without checking. |
| 194 |
* |
| 195 |
* It is assumed that parameters are correct. |
| 196 |
* |
| 197 |
* @param response response to add a header to |
| 198 |
* @param kind header or footer |
| 199 |
* @param header the header to add, does not need to be zero-terminated |
| 200 |
* @param header_len the length of the @a header |
| 201 |
* @param content value to add, does not need to be zero-terminated |
| 202 |
* @param content_len the length of the @a content |
| 203 |
* @return false on error (like out-of-memory), |
| 204 |
* true if succeed |
| 205 |
*/ |
| 206 |
bool |
| 207 |
MHD_add_response_entry_no_check_ (struct MHD_Response *response, |
| 208 |
enum MHD_ValueKind kind, |
| 209 |
const char *header, |
| 210 |
size_t header_len, |
| 211 |
const char *content, |
| 212 |
size_t content_len) |
| 213 |
{ |
| 214 |
char *header_malloced; |
| 215 |
char *value_malloced; |
| 216 |
|
| 217 |
mhd_assert (0 != header_len); |
| 218 |
mhd_assert (0 != content_len); |
| 219 |
header_malloced = malloc (header_len + 1); |
| 220 |
if (NULL == header_malloced) |
| 221 |
return false; |
| 222 |
|
| 223 |
memcpy (header_malloced, header, header_len); |
| 224 |
header_malloced[header_len] = 0; |
| 225 |
|
| 226 |
value_malloced = malloc (content_len + 1); |
| 227 |
if (NULL != value_malloced) |
| 228 |
{ |
| 229 |
memcpy (value_malloced, content, content_len); |
| 230 |
value_malloced[content_len] = 0; |
| 231 |
|
| 232 |
if (MHD_add_response_entry_no_alloc_ (response, kind, |
| 233 |
header_malloced, header_len, |
| 234 |
value_malloced, content_len)) |
| 235 |
return true; /* Success exit point */ |
| 236 |
|
| 237 |
free (value_malloced); |
| 238 |
} |
| 239 |
free (header_malloced); |
| 240 |
|
| 241 |
return false; /* Failure exit point */ |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
/** |
| 246 |
* Add a header or footer line to the response. |
| 247 |
* |
| 248 |
* @param header the header to add, does not need to be zero-terminated |
| 249 |
* @param header_len the length of the @a header |
| 250 |
* @param content value to add, does not need to be zero-terminated |
| 251 |
* @param content_len the length of the @a content |
| 252 |
* @return false on error (out-of-memory, invalid header or content), |
| 253 |
* true if succeed |
| 254 |
*/ |
| 255 |
static bool |
| 256 |
add_response_entry_n (struct MHD_Response *response, |
| 257 |
enum MHD_ValueKind kind, |
| 258 |
const char *header, |
| 259 |
size_t header_len, |
| 260 |
const char *content, |
| 261 |
size_t content_len) |
| 262 |
{ |
| 263 |
if (NULL == response) |
| 264 |
return false; |
| 265 |
if (0 == header_len) |
| 266 |
return false; |
| 267 |
if (0 == content_len) |
| 268 |
return false; |
| 269 |
if (NULL != memchr (header, '\t', header_len)) |
| 270 |
return false; |
| 271 |
if (NULL != memchr (header, ' ', header_len)) |
| 272 |
return false; |
| 273 |
if (NULL != memchr (header, '\r', header_len)) |
| 274 |
return false; |
| 275 |
if (NULL != memchr (header, '\n', header_len)) |
| 276 |
return false; |
| 277 |
if (NULL != memchr (content, '\r', content_len)) |
| 278 |
return false; |
| 279 |
if (NULL != memchr (content, '\n', content_len)) |
| 280 |
return false; |
| 281 |
|
| 282 |
return MHD_add_response_entry_no_check_ (response, kind, header, header_len, |
| 283 |
content, content_len); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
/** |
| 288 |
* Add a header or footer line to the response. |
| 289 |
* |
| 290 |
* @param response response to add a header to |
| 291 |
* @param kind header or footer |
| 292 |
* @param header the header to add |
| 293 |
* @param content value to add |
| 294 |
* @return #MHD_NO on error (i.e. invalid header or content format). |
| 295 |
*/ |
| 296 |
static enum MHD_Result |
| 297 |
add_response_entry (struct MHD_Response *response, |
| 298 |
enum MHD_ValueKind kind, |
| 299 |
const char *header, |
| 300 |
const char *content) |
| 301 |
{ |
| 302 |
size_t header_len; |
| 303 |
size_t content_len; |
| 304 |
|
| 305 |
if (NULL == content) |
| 306 |
return MHD_NO; |
| 307 |
|
| 308 |
header_len = strlen (header); |
| 309 |
content_len = strlen (content); |
| 310 |
return add_response_entry_n (response, kind, header, |
| 311 |
header_len, content, |
| 312 |
content_len) ? MHD_YES : MHD_NO; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
/** |
| 317 |
* Add "Connection:" header to the response with special processing. |
| 318 |
* |
| 319 |
* "Connection:" header value will be combined with any existing "Connection:" |
| 320 |
* header, "close" token (if any) will be de-duplicated and moved to the first |
| 321 |
* position. |
| 322 |
* |
| 323 |
* @param response the response to add a header to |
| 324 |
* @param value the value to add |
| 325 |
* @return #MHD_NO on error (no memory). |
| 326 |
*/ |
| 327 |
static enum MHD_Result |
| 328 |
add_response_header_connection (struct MHD_Response *response, |
| 329 |
const char *value) |
| 330 |
{ |
| 331 |
static const char *key = MHD_HTTP_HEADER_CONNECTION; |
| 332 |
/** the length of the "Connection" key */ |
| 333 |
static const size_t key_len = |
| 334 |
MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_CONNECTION); |
| 335 |
size_t value_len; /**< the length of the @a value */ |
| 336 |
size_t old_value_len; /**< the length of the existing "Connection" value */ |
| 337 |
size_t buf_size; /**< the size of the buffer */ |
| 338 |
size_t norm_len; /**< the length of the normalised value */ |
| 339 |
char *buf; /**< the temporal buffer */ |
| 340 |
struct MHD_HTTP_Res_Header *hdr; /**< existing "Connection" header */ |
| 341 |
bool value_has_close; /**< the @a value has "close" token */ |
| 342 |
bool already_has_close; /**< existing "Connection" header has "close" token */ |
| 343 |
size_t pos = 0; /**< position of addition in the @a buf */ |
| 344 |
|
| 345 |
if ( (NULL != strchr (value, '\r')) || |
| 346 |
(NULL != strchr (value, '\n')) ) |
| 347 |
return MHD_NO; |
| 348 |
|
| 349 |
if (0 != (response->flags_auto & MHD_RAF_HAS_CONNECTION_HDR)) |
| 350 |
{ |
| 351 |
hdr = MHD_get_response_element_n_ (response, MHD_HEADER_KIND, |
| 352 |
key, key_len); |
| 353 |
already_has_close = |
| 354 |
(0 != (response->flags_auto & MHD_RAF_HAS_CONNECTION_CLOSE)); |
| 355 |
mhd_assert (already_has_close == (0 == memcmp (hdr->value, "close", 5))); |
| 356 |
mhd_assert (NULL != hdr); |
| 357 |
} |
| 358 |
else |
| 359 |
{ |
| 360 |
hdr = NULL; |
| 361 |
already_has_close = false; |
| 362 |
mhd_assert (NULL == MHD_get_response_element_n_ (response, |
| 363 |
MHD_HEADER_KIND, |
| 364 |
key, key_len)); |
| 365 |
mhd_assert (0 == (response->flags_auto & MHD_RAF_HAS_CONNECTION_CLOSE)); |
| 366 |
} |
| 367 |
if (NULL != hdr) |
| 368 |
old_value_len = hdr->value_size + 2; /* additional size for ", " */ |
| 369 |
else |
| 370 |
old_value_len = 0; |
| 371 |
|
| 372 |
value_len = strlen (value); |
| 373 |
if (value_len >= SSIZE_MAX) |
| 374 |
return MHD_NO; |
| 375 |
/* Additional space for normalisation and zero-termination */ |
| 376 |
norm_len = value_len + value_len / 2 + 1; |
| 377 |
if (norm_len >= SSIZE_MAX) |
| 378 |
return MHD_NO; |
| 379 |
buf_size = old_value_len + (size_t) norm_len; |
| 380 |
|
| 381 |
buf = malloc (buf_size); |
| 382 |
if (NULL == buf) |
| 383 |
return MHD_NO; |
| 384 |
if (1) |
| 385 |
{ /* local scope */ |
| 386 |
ssize_t norm_len_s = (ssize_t) norm_len; |
| 387 |
/* Remove "close" token (if any), it will be moved to the front */ |
| 388 |
value_has_close = MHD_str_remove_token_caseless_ (value, value_len, "close", |
| 389 |
MHD_STATICSTR_LEN_ ( \ |
| 390 |
"close"), |
| 391 |
buf + old_value_len, |
| 392 |
&norm_len_s); |
| 393 |
mhd_assert (0 <= norm_len_s); |
| 394 |
if (0 > norm_len_s) |
| 395 |
{ |
| 396 |
/* Must never happen with realistic sizes */ |
| 397 |
free (buf); |
| 398 |
return MHD_NO; |
| 399 |
} |
| 400 |
else |
| 401 |
norm_len = (size_t) norm_len_s; |
| 402 |
} |
| 403 |
#ifdef UPGRADE_SUPPORT |
| 404 |
if ( (NULL != response->upgrade_handler) && value_has_close) |
| 405 |
{ /* The "close" token cannot be used with connection "upgrade" */ |
| 406 |
free (buf); |
| 407 |
return MHD_NO; |
| 408 |
} |
| 409 |
#endif /* UPGRADE_SUPPORT */ |
| 410 |
if (0 != norm_len) |
| 411 |
MHD_str_remove_tokens_caseless_ (buf + old_value_len, &norm_len, |
| 412 |
"keep-alive", |
| 413 |
MHD_STATICSTR_LEN_ ("keep-alive")); |
| 414 |
if (0 == norm_len) |
| 415 |
{ /* New value is empty after normalisation */ |
| 416 |
if (! value_has_close) |
| 417 |
{ /* The new value had no tokens */ |
| 418 |
free (buf); |
| 419 |
return MHD_NO; |
| 420 |
} |
| 421 |
if (already_has_close) |
| 422 |
{ /* The "close" token is already present, nothing to modify */ |
| 423 |
free (buf); |
| 424 |
return MHD_YES; |
| 425 |
} |
| 426 |
} |
| 427 |
/* Add "close" token if required */ |
| 428 |
if (value_has_close && ! already_has_close) |
| 429 |
{ |
| 430 |
/* Need to insert "close" token at the first position */ |
| 431 |
mhd_assert (buf_size >= old_value_len + norm_len \ |
| 432 |
+ MHD_STATICSTR_LEN_ ("close, ") + 1); |
| 433 |
if (0 != norm_len) |
| 434 |
memmove (buf + MHD_STATICSTR_LEN_ ("close, ") + old_value_len, |
| 435 |
buf + old_value_len, norm_len + 1); |
| 436 |
memcpy (buf, "close", MHD_STATICSTR_LEN_ ("close")); |
| 437 |
pos += MHD_STATICSTR_LEN_ ("close"); |
| 438 |
} |
| 439 |
/* Add old value tokens (if any) */ |
| 440 |
if (0 != old_value_len) |
| 441 |
{ |
| 442 |
if (0 != pos) |
| 443 |
{ |
| 444 |
buf[pos++] = ','; |
| 445 |
buf[pos++] = ' '; |
| 446 |
} |
| 447 |
memcpy (buf + pos, hdr->value, |
| 448 |
hdr->value_size); |
| 449 |
pos += hdr->value_size; |
| 450 |
} |
| 451 |
/* Add new value token (if any) */ |
| 452 |
if (0 != norm_len) |
| 453 |
{ |
| 454 |
if (0 != pos) |
| 455 |
{ |
| 456 |
buf[pos++] = ','; |
| 457 |
buf[pos++] = ' '; |
| 458 |
} |
| 459 |
/* The new value tokens must be already at the correct position */ |
| 460 |
mhd_assert ((value_has_close && ! already_has_close) ? \ |
| 461 |
(MHD_STATICSTR_LEN_ ("close, ") + old_value_len == pos) : \ |
| 462 |
(old_value_len == pos)); |
| 463 |
pos += norm_len; |
| 464 |
} |
| 465 |
mhd_assert (buf_size > pos); |
| 466 |
buf[pos] = 0; /* Null terminate the result */ |
| 467 |
|
| 468 |
if (NULL == hdr) |
| 469 |
{ |
| 470 |
struct MHD_HTTP_Res_Header *new_hdr; /**< new "Connection" header */ |
| 471 |
/* Create new response header entry */ |
| 472 |
new_hdr = MHD_calloc_ (1, sizeof (struct MHD_HTTP_Res_Header)); |
| 473 |
if (NULL != new_hdr) |
| 474 |
{ |
| 475 |
new_hdr->header = malloc (key_len + 1); |
| 476 |
if (NULL != new_hdr->header) |
| 477 |
{ |
| 478 |
memcpy (new_hdr->header, key, key_len + 1); |
| 479 |
new_hdr->header_size = key_len; |
| 480 |
new_hdr->value = buf; |
| 481 |
new_hdr->value_size = pos; |
| 482 |
new_hdr->kind = MHD_HEADER_KIND; |
| 483 |
if (value_has_close) |
| 484 |
response->flags_auto = (MHD_RAF_HAS_CONNECTION_HDR |
| 485 |
| MHD_RAF_HAS_CONNECTION_CLOSE); |
| 486 |
else |
| 487 |
response->flags_auto = MHD_RAF_HAS_CONNECTION_HDR; |
| 488 |
_MHD_insert_header_first (response, new_hdr); |
| 489 |
return MHD_YES; |
| 490 |
} |
| 491 |
free (new_hdr); |
| 492 |
} |
| 493 |
free (buf); |
| 494 |
return MHD_NO; |
| 495 |
} |
| 496 |
|
| 497 |
/* Update existing header entry */ |
| 498 |
free (hdr->value); |
| 499 |
hdr->value = buf; |
| 500 |
hdr->value_size = pos; |
| 501 |
if (value_has_close && ! already_has_close) |
| 502 |
response->flags_auto |= MHD_RAF_HAS_CONNECTION_CLOSE; |
| 503 |
return MHD_YES; |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
/** |
| 508 |
* Remove tokens from "Connection:" header of the response. |
| 509 |
* |
| 510 |
* Provided tokens will be removed from "Connection:" header value. |
| 511 |
* |
| 512 |
* @param response the response to manipulate "Connection:" header |
| 513 |
* @param value the tokens to remove |
| 514 |
* @return #MHD_NO on error (no headers or tokens found). |
| 515 |
*/ |
| 516 |
static enum MHD_Result |
| 517 |
del_response_header_connection (struct MHD_Response *response, |
| 518 |
const char *value) |
| 519 |
{ |
| 520 |
struct MHD_HTTP_Res_Header *hdr; /**< existing "Connection" header */ |
| 521 |
|
| 522 |
hdr = MHD_get_response_element_n_ (response, MHD_HEADER_KIND, |
| 523 |
MHD_HTTP_HEADER_CONNECTION, |
| 524 |
MHD_STATICSTR_LEN_ ( \ |
| 525 |
MHD_HTTP_HEADER_CONNECTION)); |
| 526 |
if (NULL == hdr) |
| 527 |
return MHD_NO; |
| 528 |
|
| 529 |
if (! MHD_str_remove_tokens_caseless_ (hdr->value, &hdr->value_size, value, |
| 530 |
strlen (value))) |
| 531 |
return MHD_NO; |
| 532 |
if (0 == hdr->value_size) |
| 533 |
{ |
| 534 |
_MHD_remove_header (response, hdr); |
| 535 |
free (hdr->value); |
| 536 |
free (hdr->header); |
| 537 |
free (hdr); |
| 538 |
response->flags_auto &= |
| 539 |
~((enum MHD_ResponseAutoFlags) MHD_RAF_HAS_CONNECTION_HDR |
| 540 |
| (enum MHD_ResponseAutoFlags) MHD_RAF_HAS_CONNECTION_CLOSE); |
| 541 |
} |
| 542 |
else |
| 543 |
{ |
| 544 |
hdr->value[hdr->value_size] = 0; /* Null-terminate the result */ |
| 545 |
if (0 != (response->flags_auto |
| 546 |
& ~((enum MHD_ResponseAutoFlags) MHD_RAF_HAS_CONNECTION_CLOSE))) |
| 547 |
{ |
| 548 |
if (MHD_STATICSTR_LEN_ ("close") == hdr->value_size) |
| 549 |
{ |
| 550 |
if (0 != memcmp (hdr->value, "close", MHD_STATICSTR_LEN_ ("close"))) |
| 551 |
response->flags_auto &= |
| 552 |
~((enum MHD_ResponseAutoFlags) MHD_RAF_HAS_CONNECTION_CLOSE); |
| 553 |
} |
| 554 |
else if (MHD_STATICSTR_LEN_ ("close, ") < hdr->value_size) |
| 555 |
{ |
| 556 |
if (0 != memcmp (hdr->value, "close, ", |
| 557 |
MHD_STATICSTR_LEN_ ("close, "))) |
| 558 |
response->flags_auto &= |
| 559 |
~((enum MHD_ResponseAutoFlags) MHD_RAF_HAS_CONNECTION_CLOSE); |
| 560 |
} |
| 561 |
else |
| 562 |
response->flags_auto &= |
| 563 |
~((enum MHD_ResponseAutoFlags) MHD_RAF_HAS_CONNECTION_CLOSE); |
| 564 |
} |
| 565 |
} |
| 566 |
return MHD_YES; |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
/** |
| 571 |
* Add a header line to the response. |
| 572 |
* |
| 573 |
* When reply is generated with queued response, some headers are generated |
| 574 |
* automatically. Automatically generated headers are only sent to the client, |
| 575 |
* but not added back to the response object. |
| 576 |
* |
| 577 |
* The list of automatic headers: |
| 578 |
* + "Date" header is added automatically unless already set by |
| 579 |
* this function |
| 580 |
* @see #MHD_USE_SUPPRESS_DATE_NO_CLOCK |
| 581 |
* + "Content-Length" is added automatically when required, attempt to set |
| 582 |
* it manually by this function is ignored. |
| 583 |
* @see #MHD_RF_INSANITY_HEADER_CONTENT_LENGTH |
| 584 |
* + "Transfer-Encoding" with value "chunked" is added automatically, |
| 585 |
* when chunked transfer encoding is used automatically. Same header with |
| 586 |
* the same value can be set manually by this function to enforce chunked |
| 587 |
* encoding, however for HTTP/1.0 clients chunked encoding will not be used |
| 588 |
* and manually set "Transfer-Encoding" header is automatically removed |
| 589 |
* for HTTP/1.0 clients |
| 590 |
* + "Connection" may be added automatically with value "Keep-Alive" (only |
| 591 |
* for HTTP/1.0 clients) or "Close". The header "Connection" with value |
| 592 |
* "Close" could be set by this function to enforce closure of |
| 593 |
* the connection after sending this response. "Keep-Alive" cannot be |
| 594 |
* enforced and will be removed automatically. |
| 595 |
* @see #MHD_RF_SEND_KEEP_ALIVE_HEADER |
| 596 |
* |
| 597 |
* Some headers are pre-processed by this function: |
| 598 |
* * "Connection" headers are combined into single header entry, value is |
| 599 |
* normilised, "Keep-Alive" tokens are removed. |
| 600 |
* * "Transfer-Encoding" header: the only one header is allowed, the only |
| 601 |
* allowed value is "chunked". |
| 602 |
* * "Date" header: the only one header is allowed, the second added header |
| 603 |
* replaces the first one. |
| 604 |
* * "Content-Length" application-defined header is not allowed. |
| 605 |
* @see #MHD_RF_INSANITY_HEADER_CONTENT_LENGTH |
| 606 |
* |
| 607 |
* Headers are used in order as they were added. |
| 608 |
* |
| 609 |
* @param response the response to add a header to |
| 610 |
* @param header the header name to add, no need to be static, an internal copy |
| 611 |
* will be created automatically |
| 612 |
* @param content the header value to add, no need to be static, an internal |
| 613 |
* copy will be created automatically |
| 614 |
* @return #MHD_YES on success, |
| 615 |
* #MHD_NO on error (i.e. invalid header or content format), |
| 616 |
* or out of memory |
| 617 |
* @ingroup response |
| 618 |
*/ |
| 619 |
_MHD_EXTERN enum MHD_Result |
| 620 |
MHD_add_response_header (struct MHD_Response *response, |
| 621 |
const char *header, |
| 622 |
const char *content) |
| 623 |
{ |
| 624 |
if (MHD_str_equal_caseless_ (header, MHD_HTTP_HEADER_CONNECTION)) |
| 625 |
return add_response_header_connection (response, content); |
| 626 |
|
| 627 |
if (MHD_str_equal_caseless_ (header, |
| 628 |
MHD_HTTP_HEADER_TRANSFER_ENCODING)) |
| 629 |
{ |
| 630 |
if (! MHD_str_equal_caseless_ (content, "chunked")) |
| 631 |
return MHD_NO; /* Only "chunked" encoding is allowed */ |
| 632 |
if (0 != (response->flags_auto & MHD_RAF_HAS_TRANS_ENC_CHUNKED)) |
| 633 |
return MHD_YES; /* Already has "chunked" encoding header */ |
| 634 |
if ( (0 != (response->flags_auto & MHD_RAF_HAS_CONTENT_LENGTH)) && |
| 635 |
(0 == (MHD_RF_INSANITY_HEADER_CONTENT_LENGTH & response->flags)) ) |
| 636 |
return MHD_NO; /* Has "Content-Length" header and no "Insanity" flag */ |
| 637 |
if (MHD_NO != add_response_entry (response, |
| 638 |
MHD_HEADER_KIND, |
| 639 |
header, |
| 640 |
content)) |
| 641 |
{ |
| 642 |
response->flags_auto |= MHD_RAF_HAS_TRANS_ENC_CHUNKED; |
| 643 |
return MHD_YES; |
| 644 |
} |
| 645 |
return MHD_NO; |
| 646 |
} |
| 647 |
if (MHD_str_equal_caseless_ (header, |
| 648 |
MHD_HTTP_HEADER_DATE)) |
| 649 |
{ |
| 650 |
if (0 != (response->flags_auto & MHD_RAF_HAS_DATE_HDR)) |
| 651 |
{ |
| 652 |
struct MHD_HTTP_Res_Header *hdr; |
| 653 |
hdr = MHD_get_response_element_n_ (response, MHD_HEADER_KIND, |
| 654 |
MHD_HTTP_HEADER_DATE, |
| 655 |
MHD_STATICSTR_LEN_ ( \ |
| 656 |
MHD_HTTP_HEADER_DATE)); |
| 657 |
mhd_assert (NULL != hdr); |
| 658 |
_MHD_remove_header (response, hdr); |
| 659 |
if (NULL != hdr->value) |
| 660 |
free (hdr->value); |
| 661 |
free (hdr->header); |
| 662 |
free (hdr); |
| 663 |
} |
| 664 |
if (MHD_NO != add_response_entry (response, |
| 665 |
MHD_HEADER_KIND, |
| 666 |
header, |
| 667 |
content)) |
| 668 |
{ |
| 669 |
response->flags_auto |= MHD_RAF_HAS_DATE_HDR; |
| 670 |
return MHD_YES; |
| 671 |
} |
| 672 |
return MHD_NO; |
| 673 |
} |
| 674 |
|
| 675 |
if (MHD_str_equal_caseless_ (header, |
| 676 |
MHD_HTTP_HEADER_CONTENT_LENGTH)) |
| 677 |
{ |
| 678 |
/* Generally MHD sets automatically correct "Content-Length" always when |
| 679 |
* needed. |
| 680 |
* Custom "Content-Length" header is allowed only in special cases. */ |
| 681 |
if ( (0 != (MHD_RF_INSANITY_HEADER_CONTENT_LENGTH & response->flags)) || |
| 682 |
((0 != (MHD_RF_HEAD_ONLY_RESPONSE & response->flags)) && |
| 683 |
(0 == (response->flags_auto & (MHD_RAF_HAS_TRANS_ENC_CHUNKED |
| 684 |
| MHD_RAF_HAS_CONTENT_LENGTH)))) ) |
| 685 |
{ |
| 686 |
if (MHD_NO != add_response_entry (response, |
| 687 |
MHD_HEADER_KIND, |
| 688 |
header, |
| 689 |
content)) |
| 690 |
{ |
| 691 |
response->flags_auto |= MHD_RAF_HAS_CONTENT_LENGTH; |
| 692 |
return MHD_YES; |
| 693 |
} |
| 694 |
} |
| 695 |
return MHD_NO; |
| 696 |
} |
| 697 |
|
| 698 |
return add_response_entry (response, |
| 699 |
MHD_HEADER_KIND, |
| 700 |
header, |
| 701 |
content); |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
/** |
| 706 |
* Add a footer line to the response. |
| 707 |
* |
| 708 |
* @param response response to remove a header from |
| 709 |
* @param footer the footer to delete |
| 710 |
* @param content value to delete |
| 711 |
* @return #MHD_NO on error (i.e. invalid footer or content format). |
| 712 |
* @ingroup response |
| 713 |
*/ |
| 714 |
_MHD_EXTERN enum MHD_Result |
| 715 |
MHD_add_response_footer (struct MHD_Response *response, |
| 716 |
const char *footer, |
| 717 |
const char *content) |
| 718 |
{ |
| 719 |
return add_response_entry (response, |
| 720 |
MHD_FOOTER_KIND, |
| 721 |
footer, |
| 722 |
content); |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
/** |
| 727 |
* Delete a header (or footer) line from the response. |
| 728 |
* |
| 729 |
* For "Connection" headers this function remove all tokens from existing |
| 730 |
* value. Successful result means that at least one token has been removed. |
| 731 |
* If all tokens are removed from "Connection" header, the empty "Connection" |
| 732 |
* header removed. |
| 733 |
* |
| 734 |
* @param response response to remove a header from |
| 735 |
* @param header the header to delete |
| 736 |
* @param content value to delete |
| 737 |
* @return #MHD_NO on error (no such header known) |
| 738 |
* @ingroup response |
| 739 |
*/ |
| 740 |
_MHD_EXTERN enum MHD_Result |
| 741 |
MHD_del_response_header (struct MHD_Response *response, |
| 742 |
const char *header, |
| 743 |
const char *content) |
| 744 |
{ |
| 745 |
struct MHD_HTTP_Res_Header *pos; |
| 746 |
size_t header_len; |
| 747 |
size_t content_len; |
| 748 |
|
| 749 |
if ( (NULL == header) || |
| 750 |
(NULL == content) ) |
| 751 |
return MHD_NO; |
| 752 |
header_len = strlen (header); |
| 753 |
|
| 754 |
if ((0 != (response->flags_auto & MHD_RAF_HAS_CONNECTION_HDR)) && |
| 755 |
(MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_CONNECTION) == header_len) && |
| 756 |
MHD_str_equal_caseless_bin_n_ (header, MHD_HTTP_HEADER_CONNECTION, |
| 757 |
header_len)) |
| 758 |
return del_response_header_connection (response, content); |
| 759 |
|
| 760 |
content_len = strlen (content); |
| 761 |
pos = response->first_header; |
| 762 |
while (NULL != pos) |
| 763 |
{ |
| 764 |
if ((header_len == pos->header_size) && |
| 765 |
(content_len == pos->value_size) && |
| 766 |
(0 == memcmp (header, |
| 767 |
pos->header, |
| 768 |
header_len)) && |
| 769 |
(0 == memcmp (content, |
| 770 |
pos->value, |
| 771 |
content_len))) |
| 772 |
{ |
| 773 |
_MHD_remove_header (response, pos); |
| 774 |
free (pos->header); |
| 775 |
free (pos->value); |
| 776 |
free (pos); |
| 777 |
if ( (MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_TRANSFER_ENCODING) == |
| 778 |
header_len) && |
| 779 |
MHD_str_equal_caseless_bin_n_ (header, |
| 780 |
MHD_HTTP_HEADER_TRANSFER_ENCODING, |
| 781 |
header_len) ) |
| 782 |
response->flags_auto &= |
| 783 |
~((enum MHD_ResponseAutoFlags) MHD_RAF_HAS_TRANS_ENC_CHUNKED); |
| 784 |
else if ( (MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_DATE) == |
| 785 |
header_len) && |
| 786 |
MHD_str_equal_caseless_bin_n_ (header, |
| 787 |
MHD_HTTP_HEADER_DATE, |
| 788 |
header_len) ) |
| 789 |
response->flags_auto &= |
| 790 |
~((enum MHD_ResponseAutoFlags) MHD_RAF_HAS_DATE_HDR); |
| 791 |
else if ( (MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_CONTENT_LENGTH) == |
| 792 |
header_len) && |
| 793 |
MHD_str_equal_caseless_bin_n_ (header, |
| 794 |
MHD_HTTP_HEADER_CONTENT_LENGTH, |
| 795 |
header_len) ) |
| 796 |
{ |
| 797 |
if (NULL == MHD_get_response_element_n_ (response, |
| 798 |
MHD_HEADER_KIND, |
| 799 |
MHD_HTTP_HEADER_CONTENT_LENGTH, |
| 800 |
header_len)) |
| 801 |
response->flags_auto &= |
| 802 |
~((enum MHD_ResponseAutoFlags) MHD_RAF_HAS_CONTENT_LENGTH); |
| 803 |
} |
| 804 |
return MHD_YES; |
| 805 |
} |
| 806 |
pos = pos->next; |
| 807 |
} |
| 808 |
return MHD_NO; |
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
/** |
| 813 |
* Get all of the headers (and footers) added to a response. |
| 814 |
* |
| 815 |
* @param response response to query |
| 816 |
* @param iterator callback to call on each header; |
| 817 |
* maybe NULL (then just count headers) |
| 818 |
* @param iterator_cls extra argument to @a iterator |
| 819 |
* @return number of entries iterated over |
| 820 |
* @ingroup response |
| 821 |
*/ |
| 822 |
_MHD_EXTERN int |
| 823 |
MHD_get_response_headers (struct MHD_Response *response, |
| 824 |
MHD_KeyValueIterator iterator, |
| 825 |
void *iterator_cls) |
| 826 |
{ |
| 827 |
int numHeaders = 0; |
| 828 |
struct MHD_HTTP_Res_Header *pos; |
| 829 |
|
| 830 |
for (pos = response->first_header; |
| 831 |
NULL != pos; |
| 832 |
pos = pos->next) |
| 833 |
{ |
| 834 |
numHeaders++; |
| 835 |
if ((NULL != iterator) && |
| 836 |
(MHD_NO == iterator (iterator_cls, |
| 837 |
pos->kind, |
| 838 |
pos->header, |
| 839 |
pos->value))) |
| 840 |
break; |
| 841 |
} |
| 842 |
return numHeaders; |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
/** |
| 847 |
* Get a particular header (or footer) from the response. |
| 848 |
* |
| 849 |
* @param response response to query |
| 850 |
* @param key which header to get |
| 851 |
* @return NULL if header does not exist |
| 852 |
* @ingroup response |
| 853 |
*/ |
| 854 |
_MHD_EXTERN const char * |
| 855 |
MHD_get_response_header (struct MHD_Response *response, |
| 856 |
const char *key) |
| 857 |
{ |
| 858 |
struct MHD_HTTP_Res_Header *pos; |
| 859 |
size_t key_size; |
| 860 |
|
| 861 |
if (NULL == key) |
| 862 |
return NULL; |
| 863 |
|
| 864 |
key_size = strlen (key); |
| 865 |
for (pos = response->first_header; |
| 866 |
NULL != pos; |
| 867 |
pos = pos->next) |
| 868 |
{ |
| 869 |
if ((pos->header_size == key_size) && |
| 870 |
(MHD_str_equal_caseless_bin_n_ (pos->header, key, pos->header_size))) |
| 871 |
return pos->value; |
| 872 |
} |
| 873 |
return NULL; |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
/** |
| 878 |
* Get a particular header (or footer) element from the response. |
| 879 |
* |
| 880 |
* Function returns the first found element. |
| 881 |
* @param response response to query |
| 882 |
* @param kind the kind of element: header or footer |
| 883 |
* @param key the key which header to get |
| 884 |
* @param key_len the length of the @a key |
| 885 |
* @return NULL if header element does not exist |
| 886 |
* @ingroup response |
| 887 |
*/ |
| 888 |
struct MHD_HTTP_Res_Header * |
| 889 |
MHD_get_response_element_n_ (struct MHD_Response *response, |
| 890 |
enum MHD_ValueKind kind, |
| 891 |
const char *key, |
| 892 |
size_t key_len) |
| 893 |
{ |
| 894 |
struct MHD_HTTP_Res_Header *pos; |
| 895 |
|
| 896 |
mhd_assert (NULL != key); |
| 897 |
mhd_assert (0 != key[0]); |
| 898 |
mhd_assert (0 != key_len); |
| 899 |
|
| 900 |
for (pos = response->first_header; |
| 901 |
NULL != pos; |
| 902 |
pos = pos->next) |
| 903 |
{ |
| 904 |
if ((pos->header_size == key_len) && |
| 905 |
(kind == pos->kind) && |
| 906 |
(MHD_str_equal_caseless_bin_n_ (pos->header, key, pos->header_size))) |
| 907 |
return pos; |
| 908 |
} |
| 909 |
return NULL; |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
/** |
| 914 |
* Check whether response header contains particular token. |
| 915 |
* |
| 916 |
* Token could be surrounded by spaces and tabs and delimited by comma. |
| 917 |
* Case-insensitive match used for header names and tokens. |
| 918 |
* |
| 919 |
* @param response the response to query |
| 920 |
* @param key header name |
| 921 |
* @param key_len the length of @a key, not including optional |
| 922 |
* terminating null-character. |
| 923 |
* @param token the token to find |
| 924 |
* @param token_len the length of @a token, not including optional |
| 925 |
* terminating null-character. |
| 926 |
* @return true if token is found in specified header, |
| 927 |
* false otherwise |
| 928 |
*/ |
| 929 |
bool |
| 930 |
MHD_check_response_header_token_ci (const struct MHD_Response *response, |
| 931 |
const char *key, |
| 932 |
size_t key_len, |
| 933 |
const char *token, |
| 934 |
size_t token_len) |
| 935 |
{ |
| 936 |
struct MHD_HTTP_Res_Header *pos; |
| 937 |
|
| 938 |
if ( (NULL == key) || |
| 939 |
('\0' == key[0]) || |
| 940 |
(NULL == token) || |
| 941 |
('\0' == token[0]) ) |
| 942 |
return false; |
| 943 |
|
| 944 |
/* Token must not contain binary zero! */ |
| 945 |
mhd_assert (strlen (token) == token_len); |
| 946 |
|
| 947 |
for (pos = response->first_header; |
| 948 |
NULL != pos; |
| 949 |
pos = pos->next) |
| 950 |
{ |
| 951 |
if ( (pos->kind == MHD_HEADER_KIND) && |
| 952 |
(key_len == pos->header_size) && |
| 953 |
MHD_str_equal_caseless_bin_n_ (pos->header, |
| 954 |
key, |
| 955 |
key_len) && |
| 956 |
MHD_str_has_token_caseless_ (pos->value, |
| 957 |
token, |
| 958 |
token_len) ) |
| 959 |
return true; |
| 960 |
} |
| 961 |
return false; |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
/** |
| 966 |
* Create a response object. |
| 967 |
* The response object can be extended with header information and then be used |
| 968 |
* any number of times. |
| 969 |
* |
| 970 |
* If response object is used to answer HEAD request then the body of the |
| 971 |
* response is not used, while all headers (including automatic headers) are |
| 972 |
* used. |
| 973 |
* |
| 974 |
* @param size size of the data portion of the response, #MHD_SIZE_UNKNOWN for unknown |
| 975 |
* @param block_size preferred block size for querying crc (advisory only, |
| 976 |
* MHD may still call @a crc using smaller chunks); this |
| 977 |
* is essentially the buffer size used for IO, clients |
| 978 |
* should pick a value that is appropriate for IO and |
| 979 |
* memory performance requirements |
| 980 |
* @param crc callback to use to obtain response data |
| 981 |
* @param crc_cls extra argument to @a crc |
| 982 |
* @param crfc callback to call to free @a crc_cls resources |
| 983 |
* @return NULL on error (i.e. invalid arguments, out of memory) |
| 984 |
* @ingroup response |
| 985 |
*/ |
| 986 |
_MHD_EXTERN struct MHD_Response * |
| 987 |
MHD_create_response_from_callback (uint64_t size, |
| 988 |
size_t block_size, |
| 989 |
MHD_ContentReaderCallback crc, |
| 990 |
void *crc_cls, |
| 991 |
MHD_ContentReaderFreeCallback crfc) |
| 992 |
{ |
| 993 |
struct MHD_Response *response; |
| 994 |
|
| 995 |
if ((NULL == crc) || (0 == block_size)) |
| 996 |
return NULL; |
| 997 |
if (NULL == (response = MHD_calloc_ (1, sizeof (struct MHD_Response) |
| 998 |
+ block_size))) |
| 999 |
return NULL; |
| 1000 |
response->fd = -1; |
| 1001 |
response->data = (void *) &response[1]; |
| 1002 |
response->data_buffer_size = block_size; |
| 1003 |
#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) |
| 1004 |
if (! MHD_mutex_init_ (&response->mutex)) |
| 1005 |
{ |
| 1006 |
free (response); |
| 1007 |
return NULL; |
| 1008 |
} |
| 1009 |
#endif |
| 1010 |
response->crc = crc; |
| 1011 |
response->crfc = crfc; |
| 1012 |
response->crc_cls = crc_cls; |
| 1013 |
response->reference_count = 1; |
| 1014 |
response->total_size = size; |
| 1015 |
return response; |
| 1016 |
} |
| 1017 |
|
| 1018 |
|
| 1019 |
/** |
| 1020 |
* Set special flags and options for a response. |
| 1021 |
* |
| 1022 |
* @param response the response to modify |
| 1023 |
* @param flags to set for the response |
| 1024 |
* @param ... #MHD_RO_END terminated list of options |
| 1025 |
* @return #MHD_YES on success, #MHD_NO on error |
| 1026 |
*/ |
| 1027 |
_MHD_EXTERN enum MHD_Result |
| 1028 |
MHD_set_response_options (struct MHD_Response *response, |
| 1029 |
enum MHD_ResponseFlags flags, |
| 1030 |
...) |
| 1031 |
{ |
| 1032 |
va_list ap; |
| 1033 |
enum MHD_Result ret; |
| 1034 |
enum MHD_ResponseOptions ro; |
| 1035 |
|
| 1036 |
if (0 != (response->flags_auto & MHD_RAF_HAS_CONTENT_LENGTH)) |
| 1037 |
{ /* Response has custom "Content-Lengh" header */ |
| 1038 |
if ( (0 != (response->flags & MHD_RF_INSANITY_HEADER_CONTENT_LENGTH)) && |
| 1039 |
(0 == (flags & MHD_RF_INSANITY_HEADER_CONTENT_LENGTH))) |
| 1040 |
{ /* Request to remove MHD_RF_INSANITY_HEADER_CONTENT_LENGTH flag */ |
| 1041 |
return MHD_NO; |
| 1042 |
} |
| 1043 |
if ( (0 != (response->flags & MHD_RF_HEAD_ONLY_RESPONSE)) && |
| 1044 |
(0 == (flags & MHD_RF_HEAD_ONLY_RESPONSE))) |
| 1045 |
{ /* Request to remove MHD_RF_HEAD_ONLY_RESPONSE flag */ |
| 1046 |
if (0 == (flags & MHD_RF_INSANITY_HEADER_CONTENT_LENGTH)) |
| 1047 |
return MHD_NO; |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
if ( (0 != (flags & MHD_RF_HEAD_ONLY_RESPONSE)) && |
| 1052 |
(0 != response->total_size) ) |
| 1053 |
return MHD_NO; |
| 1054 |
|
| 1055 |
ret = MHD_YES; |
| 1056 |
response->flags = flags; |
| 1057 |
|
| 1058 |
va_start (ap, flags); |
| 1059 |
while (MHD_RO_END != (ro = va_arg (ap, enum MHD_ResponseOptions))) |
| 1060 |
{ |
| 1061 |
switch (ro) |
| 1062 |
{ |
| 1063 |
case MHD_RO_END: /* Not possible */ |
| 1064 |
break; |
| 1065 |
default: |
| 1066 |
ret = MHD_NO; |
| 1067 |
break; |
| 1068 |
} |
| 1069 |
} |
| 1070 |
va_end (ap); |
| 1071 |
return ret; |
| 1072 |
} |
| 1073 |
|
| 1074 |
|
| 1075 |
/** |
| 1076 |
* Given a file descriptor, read data from the file |
| 1077 |
* to generate the response. |
| 1078 |
* |
| 1079 |
* @param cls pointer to the response |
| 1080 |
* @param pos offset in the file to access |
| 1081 |
* @param buf where to write the data |
| 1082 |
* @param max number of bytes to write at most |
| 1083 |
* @return number of bytes written |
| 1084 |
*/ |
| 1085 |
static ssize_t |
| 1086 |
file_reader (void *cls, |
| 1087 |
uint64_t pos, |
| 1088 |
char *buf, |
| 1089 |
size_t max) |
| 1090 |
{ |
| 1091 |
struct MHD_Response *response = cls; |
| 1092 |
#if ! defined(_WIN32) || defined(__CYGWIN__) |
| 1093 |
ssize_t n; |
| 1094 |
#else /* _WIN32 && !__CYGWIN__ */ |
| 1095 |
const HANDLE fh = (HANDLE) (uintptr_t) _get_osfhandle (response->fd); |
| 1096 |
#endif /* _WIN32 && !__CYGWIN__ */ |
| 1097 |
const int64_t offset64 = (int64_t) (pos + response->fd_off); |
| 1098 |
|
| 1099 |
if (offset64 < 0) |
| 1100 |
return MHD_CONTENT_READER_END_WITH_ERROR; /* seek to required position is not possible */ |
| 1101 |
|
| 1102 |
#if ! defined(_WIN32) || defined(__CYGWIN__) |
| 1103 |
if (max > SSIZE_MAX) |
| 1104 |
max = SSIZE_MAX; /* Clamp to maximum return value. */ |
| 1105 |
|
| 1106 |
#if defined(HAVE_PREAD64) |
| 1107 |
n = pread64 (response->fd, buf, max, offset64); |
| 1108 |
#elif defined(HAVE_PREAD) |
| 1109 |
if ( (sizeof(off_t) < sizeof (uint64_t)) && |
| 1110 |
(offset64 > (uint64_t) INT32_MAX) ) |
| 1111 |
return MHD_CONTENT_READER_END_WITH_ERROR; /* Read at required position is not possible. */ |
| 1112 |
|
| 1113 |
n = pread (response->fd, buf, max, (off_t) offset64); |
| 1114 |
#else /* ! HAVE_PREAD */ |
| 1115 |
#if defined(HAVE_LSEEK64) |
| 1116 |
if (lseek64 (response->fd, |
| 1117 |
offset64, |
| 1118 |
SEEK_SET) != offset64) |
| 1119 |
return MHD_CONTENT_READER_END_WITH_ERROR; /* can't seek to required position */ |
| 1120 |
#else /* ! HAVE_LSEEK64 */ |
| 1121 |
if ( (sizeof(off_t) < sizeof (uint64_t)) && |
| 1122 |
(offset64 > (uint64_t) INT32_MAX) ) |
| 1123 |
return MHD_CONTENT_READER_END_WITH_ERROR; /* seek to required position is not possible */ |
| 1124 |
|
| 1125 |
if (lseek (response->fd, |
| 1126 |
(off_t) offset64, |
| 1127 |
SEEK_SET) != (off_t) offset64) |
| 1128 |
return MHD_CONTENT_READER_END_WITH_ERROR; /* can't seek to required position */ |
| 1129 |
#endif /* ! HAVE_LSEEK64 */ |
| 1130 |
n = read (response->fd, |
| 1131 |
buf, |
| 1132 |
max); |
| 1133 |
|
| 1134 |
#endif /* ! HAVE_PREAD */ |
| 1135 |
if (0 == n) |
| 1136 |
return MHD_CONTENT_READER_END_OF_STREAM; |
| 1137 |
if (n < 0) |
| 1138 |
return MHD_CONTENT_READER_END_WITH_ERROR; |
| 1139 |
return n; |
| 1140 |
#else /* _WIN32 && !__CYGWIN__ */ |
| 1141 |
if (INVALID_HANDLE_VALUE == fh) |
| 1142 |
return MHD_CONTENT_READER_END_WITH_ERROR; /* Value of 'response->fd' is not valid. */ |
| 1143 |
else |
| 1144 |
{ |
| 1145 |
OVERLAPPED f_ol = {0, 0, {{0, 0}}, 0}; /* Initialize to zero. */ |
| 1146 |
ULARGE_INTEGER pos_uli; |
| 1147 |
DWORD toRead = (max > INT32_MAX) ? INT32_MAX : (DWORD) max; |
| 1148 |
DWORD resRead; |
| 1149 |
|
| 1150 |
pos_uli.QuadPart = (uint64_t) offset64; /* Simple transformation 64bit -> 2x32bit. */ |
| 1151 |
f_ol.Offset = pos_uli.LowPart; |
| 1152 |
f_ol.OffsetHigh = pos_uli.HighPart; |
| 1153 |
if (! ReadFile (fh, (void *) buf, toRead, &resRead, &f_ol)) |
| 1154 |
return MHD_CONTENT_READER_END_WITH_ERROR; /* Read error. */ |
| 1155 |
if (0 == resRead) |
| 1156 |
return MHD_CONTENT_READER_END_OF_STREAM; |
| 1157 |
return (ssize_t) resRead; |
| 1158 |
} |
| 1159 |
#endif /* _WIN32 && !__CYGWIN__ */ |
| 1160 |
} |
| 1161 |
|
| 1162 |
|
| 1163 |
/** |
| 1164 |
* Given a pipe descriptor, read data from the pipe |
| 1165 |
* to generate the response. |
| 1166 |
* |
| 1167 |
* @param cls pointer to the response |
| 1168 |
* @param pos offset in the pipe to access (ignored) |
| 1169 |
* @param buf where to write the data |
| 1170 |
* @param max number of bytes to write at most |
| 1171 |
* @return number of bytes written |
| 1172 |
*/ |
| 1173 |
static ssize_t |
| 1174 |
pipe_reader (void *cls, |
| 1175 |
uint64_t pos, |
| 1176 |
char *buf, |
| 1177 |
size_t max) |
| 1178 |
{ |
| 1179 |
struct MHD_Response *response = cls; |
| 1180 |
ssize_t n; |
| 1181 |
|
| 1182 |
(void) pos; |
| 1183 |
|
| 1184 |
#ifndef _WIN32 |
| 1185 |
if (SSIZE_MAX < max) |
| 1186 |
max = SSIZE_MAX; |
| 1187 |
n = read (response->fd, |
| 1188 |
buf, |
| 1189 |
(MHD_SCKT_SEND_SIZE_) max); |
| 1190 |
#else /* _WIN32 */ |
| 1191 |
if (UINT_MAX < max) |
| 1192 |
max = INT_MAX; |
| 1193 |
n = read (response->fd, |
| 1194 |
buf, |
| 1195 |
(unsigned int) max); |
| 1196 |
#endif /* _WIN32 */ |
| 1197 |
|
| 1198 |
if (0 == n) |
| 1199 |
return MHD_CONTENT_READER_END_OF_STREAM; |
| 1200 |
if (n < 0) |
| 1201 |
return MHD_CONTENT_READER_END_WITH_ERROR; |
| 1202 |
return n; |
| 1203 |
} |
| 1204 |
|
| 1205 |
|
| 1206 |
/** |
| 1207 |
* Destroy file reader context. Closes the file |
| 1208 |
* descriptor. |
| 1209 |
* |
| 1210 |
* @param cls pointer to file descriptor |
| 1211 |
*/ |
| 1212 |
static void |
| 1213 |
free_callback (void *cls) |
| 1214 |
{ |
| 1215 |
struct MHD_Response *response = cls; |
| 1216 |
|
| 1217 |
(void) close (response->fd); |
| 1218 |
response->fd = -1; |
| 1219 |
} |
| 1220 |
|
| 1221 |
|
| 1222 |
#undef MHD_create_response_from_fd_at_offset |
| 1223 |
|
| 1224 |
/** |
| 1225 |
* Create a response object with the content of provided file with |
| 1226 |
* specified offset used as the response body. |
| 1227 |
* |
| 1228 |
* The response object can be extended with header information and then |
| 1229 |
* be used any number of times. |
| 1230 |
* |
| 1231 |
* If response object is used to answer HEAD request then the body |
| 1232 |
* of the response is not used, while all headers (including automatic |
| 1233 |
* headers) are used. |
| 1234 |
* |
| 1235 |
* @param size size of the data portion of the response |
| 1236 |
* @param fd file descriptor referring to a file on disk with the |
| 1237 |
* data; will be closed when response is destroyed; |
| 1238 |
* fd should be in 'blocking' mode |
| 1239 |
* @param offset offset to start reading from in the file; |
| 1240 |
* Be careful! `off_t` may have been compiled to be a |
| 1241 |
* 64-bit variable for MHD, in which case your application |
| 1242 |
* also has to be compiled using the same options! Read |
| 1243 |
* the MHD manual for more details. |
| 1244 |
* @return NULL on error (i.e. invalid arguments, out of memory) |
| 1245 |
* @ingroup response |
| 1246 |
*/ |
| 1247 |
_MHD_EXTERN struct MHD_Response * |
| 1248 |
MHD_create_response_from_fd_at_offset (size_t size, |
| 1249 |
int fd, |
| 1250 |
off_t offset) |
| 1251 |
{ |
| 1252 |
if (0 > offset) |
| 1253 |
return NULL; |
| 1254 |
return MHD_create_response_from_fd_at_offset64 (size, |
| 1255 |
fd, |
| 1256 |
(uint64_t) offset); |
| 1257 |
} |
| 1258 |
|
| 1259 |
|
| 1260 |
/** |
| 1261 |
* Create a response object with the content of provided file with |
| 1262 |
* specified offset used as the response body. |
| 1263 |
* |
| 1264 |
* The response object can be extended with header information and then |
| 1265 |
* be used any number of times. |
| 1266 |
* |
| 1267 |
* If response object is used to answer HEAD request then the body |
| 1268 |
* of the response is not used, while all headers (including automatic |
| 1269 |
* headers) are used. |
| 1270 |
* |
| 1271 |
* @param size size of the data portion of the response; |
| 1272 |
* sizes larger than 2 GiB may be not supported by OS or |
| 1273 |
* MHD build; see ::MHD_FEATURE_LARGE_FILE |
| 1274 |
* @param fd file descriptor referring to a file on disk with the |
| 1275 |
* data; will be closed when response is destroyed; |
| 1276 |
* fd should be in 'blocking' mode |
| 1277 |
* @param offset offset to start reading from in the file; |
| 1278 |
* reading file beyond 2 GiB may be not supported by OS or |
| 1279 |
* MHD build; see ::MHD_FEATURE_LARGE_FILE |
| 1280 |
* @return NULL on error (i.e. invalid arguments, out of memory) |
| 1281 |
* @ingroup response |
| 1282 |
*/ |
| 1283 |
_MHD_EXTERN struct MHD_Response * |
| 1284 |
MHD_create_response_from_fd_at_offset64 (uint64_t size, |
| 1285 |
int fd, |
| 1286 |
uint64_t offset) |
| 1287 |
{ |
| 1288 |
struct MHD_Response *response; |
| 1289 |
|
| 1290 |
#if ! defined(HAVE___LSEEKI64) && ! defined(HAVE_LSEEK64) |
| 1291 |
if ( (sizeof(uint64_t) > sizeof(off_t)) && |
| 1292 |
( (size > (uint64_t) INT32_MAX) || |
| 1293 |
(offset > (uint64_t) INT32_MAX) || |
| 1294 |
((size + offset) >= (uint64_t) INT32_MAX) ) ) |
| 1295 |
return NULL; |
| 1296 |
#endif |
| 1297 |
if ( ((int64_t) size < 0) || |
| 1298 |
((int64_t) offset < 0) || |
| 1299 |
((int64_t) (size + offset) < 0) ) |
| 1300 |
return NULL; |
| 1301 |
|
| 1302 |
response = MHD_create_response_from_callback (size, |
| 1303 |
MHD_FILE_READ_BLOCK_SIZE, |
| 1304 |
&file_reader, |
| 1305 |
NULL, |
| 1306 |
&free_callback); |
| 1307 |
if (NULL == response) |
| 1308 |
return NULL; |
| 1309 |
response->fd = fd; |
| 1310 |
response->is_pipe = false; |
| 1311 |
response->fd_off = offset; |
| 1312 |
response->crc_cls = response; |
| 1313 |
return response; |
| 1314 |
} |
| 1315 |
|
| 1316 |
|
| 1317 |
/** |
| 1318 |
* Create a response object with the response body created by reading |
| 1319 |
* the provided pipe. |
| 1320 |
* |
| 1321 |
* The response object can be extended with header information and |
| 1322 |
* then be used ONLY ONCE. |
| 1323 |
* |
| 1324 |
* If response object is used to answer HEAD request then the body |
| 1325 |
* of the response is not used, while all headers (including automatic |
| 1326 |
* headers) are used. |
| 1327 |
* |
| 1328 |
* @param fd file descriptor referring to a read-end of a pipe with the |
| 1329 |
* data; will be closed when response is destroyed; |
| 1330 |
* fd should be in 'blocking' mode |
| 1331 |
* @return NULL on error (i.e. invalid arguments, out of memory) |
| 1332 |
* @ingroup response |
| 1333 |
*/ |
| 1334 |
_MHD_EXTERN struct MHD_Response * |
| 1335 |
MHD_create_response_from_pipe (int fd) |
| 1336 |
{ |
| 1337 |
struct MHD_Response *response; |
| 1338 |
|
| 1339 |
response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, |
| 1340 |
MHD_FILE_READ_BLOCK_SIZE, |
| 1341 |
&pipe_reader, |
| 1342 |
NULL, |
| 1343 |
&free_callback); |
| 1344 |
if (NULL == response) |
| 1345 |
return NULL; |
| 1346 |
response->fd = fd; |
| 1347 |
response->is_pipe = true; |
| 1348 |
response->crc_cls = response; |
| 1349 |
return response; |
| 1350 |
} |
| 1351 |
|
| 1352 |
|
| 1353 |
/** |
| 1354 |
* Create a response object with the content of provided file used as |
| 1355 |
* the response body. |
| 1356 |
* |
| 1357 |
* The response object can be extended with header information and then |
| 1358 |
* be used any number of times. |
| 1359 |
* |
| 1360 |
* If response object is used to answer HEAD request then the body |
| 1361 |
* of the response is not used, while all headers (including automatic |
| 1362 |
* headers) are used. |
| 1363 |
* |
| 1364 |
* @param size size of the data portion of the response |
| 1365 |
* @param fd file descriptor referring to a file on disk with the data |
| 1366 |
* @return NULL on error (i.e. invalid arguments, out of memory) |
| 1367 |
* @ingroup response |
| 1368 |
*/ |
| 1369 |
_MHD_EXTERN struct MHD_Response * |
| 1370 |
MHD_create_response_from_fd (size_t size, |
| 1371 |
int fd) |
| 1372 |
{ |
| 1373 |
return MHD_create_response_from_fd_at_offset64 (size, |
| 1374 |
fd, |
| 1375 |
0); |
| 1376 |
} |
| 1377 |
|
| 1378 |
|
| 1379 |
/** |
| 1380 |
* Create a response object with the content of provided file used as |
| 1381 |
* the response body. |
| 1382 |
* |
| 1383 |
* The response object can be extended with header information and then |
| 1384 |
* be used any number of times. |
| 1385 |
* |
| 1386 |
* If response object is used to answer HEAD request then the body |
| 1387 |
* of the response is not used, while all headers (including automatic |
| 1388 |
* headers) are used. |
| 1389 |
* |
| 1390 |
* @param size size of the data portion of the response; |
| 1391 |
* sizes larger than 2 GiB may be not supported by OS or |
| 1392 |
* MHD build; see ::MHD_FEATURE_LARGE_FILE |
| 1393 |
* @param fd file descriptor referring to a file on disk with the |
| 1394 |
* data; will be closed when response is destroyed; |
| 1395 |
* fd should be in 'blocking' mode |
| 1396 |
* @return NULL on error (i.e. invalid arguments, out of memory) |
| 1397 |
* @ingroup response |
| 1398 |
*/ |
| 1399 |
_MHD_EXTERN struct MHD_Response * |
| 1400 |
MHD_create_response_from_fd64 (uint64_t size, |
| 1401 |
int fd) |
| 1402 |
{ |
| 1403 |
return MHD_create_response_from_fd_at_offset64 (size, |
| 1404 |
fd, |
| 1405 |
0); |
| 1406 |
} |
| 1407 |
|
| 1408 |
|
| 1409 |
/** |
| 1410 |
* Create a response object. |
| 1411 |
* The response object can be extended with header information and then be used |
| 1412 |
* any number of times. |
| 1413 |
* |
| 1414 |
* If response object is used to answer HEAD request then the body of the |
| 1415 |
* response is not used, while all headers (including automatic headers) are |
| 1416 |
* used. |
| 1417 |
* |
| 1418 |
* @param size size of the @a data portion of the response |
| 1419 |
* @param data the data itself |
| 1420 |
* @param must_free libmicrohttpd should free data when done |
| 1421 |
* @param must_copy libmicrohttpd must make a copy of @a data |
| 1422 |
* right away, the data may be released anytime after |
| 1423 |
* this call returns |
| 1424 |
* @return NULL on error (i.e. invalid arguments, out of memory) |
| 1425 |
* @deprecated use #MHD_create_response_from_buffer instead |
| 1426 |
* @ingroup response |
| 1427 |
*/ |
| 1428 |
_MHD_EXTERN struct MHD_Response * |
| 1429 |
MHD_create_response_from_data (size_t size, |
| 1430 |
void *data, |
| 1431 |
int must_free, |
| 1432 |
int must_copy) |
| 1433 |
{ |
| 1434 |
enum MHD_ResponseMemoryMode mode; |
| 1435 |
|
| 1436 |
if (0 != must_copy) |
| 1437 |
mode = MHD_RESPMEM_MUST_COPY; |
| 1438 |
else if (0 != must_free) |
| 1439 |
mode = MHD_RESPMEM_MUST_FREE; |
| 1440 |
else |
| 1441 |
mode = MHD_RESPMEM_PERSISTENT; |
| 1442 |
|
| 1443 |
return MHD_create_response_from_buffer (size, data, mode); |
| 1444 |
} |
| 1445 |
|
| 1446 |
|
| 1447 |
/** |
| 1448 |
* Create a response object with the content of provided buffer used as |
| 1449 |
* the response body. |
| 1450 |
* |
| 1451 |
* The response object can be extended with header information and then |
| 1452 |
* be used any number of times. |
| 1453 |
* |
| 1454 |
* If response object is used to answer HEAD request then the body |
| 1455 |
* of the response is not used, while all headers (including automatic |
| 1456 |
* headers) are used. |
| 1457 |
* |
| 1458 |
* @param size size of the data portion of the response |
| 1459 |
* @param buffer size bytes containing the response's data portion |
| 1460 |
* @param mode flags for buffer management |
| 1461 |
* @return NULL on error (i.e. invalid arguments, out of memory) |
| 1462 |
* @ingroup response |
| 1463 |
*/ |
| 1464 |
_MHD_EXTERN struct MHD_Response * |
| 1465 |
MHD_create_response_from_buffer (size_t size, |
| 1466 |
void *buffer, |
| 1467 |
enum MHD_ResponseMemoryMode mode) |
| 1468 |
{ |
| 1469 |
if (MHD_RESPMEM_MUST_FREE == mode) |
| 1470 |
return MHD_create_response_from_buffer_with_free_callback_cls (size, |
| 1471 |
buffer, |
| 1472 |
&free, |
| 1473 |
buffer); |
| 1474 |
if (MHD_RESPMEM_MUST_COPY == mode) |
| 1475 |
return MHD_create_response_from_buffer_copy (size, |
| 1476 |
buffer); |
| 1477 |
|
| 1478 |
return MHD_create_response_from_buffer_with_free_callback_cls (size, |
| 1479 |
buffer, |
| 1480 |
NULL, |
| 1481 |
NULL); |
| 1482 |
} |
| 1483 |
|
| 1484 |
|
| 1485 |
/** |
| 1486 |
* Create a response object with the content of provided statically allocated |
| 1487 |
* buffer used as the response body. |
| 1488 |
* |
| 1489 |
* The buffer must be valid for the lifetime of the response. The easiest way |
| 1490 |
* to achieve this is to use a statically allocated buffer. |
| 1491 |
* |
| 1492 |
* The response object can be extended with header information and then |
| 1493 |
* be used any number of times. |
| 1494 |
* |
| 1495 |
* If response object is used to answer HEAD request then the body |
| 1496 |
* of the response is not used, while all headers (including automatic |
| 1497 |
* headers) are used. |
| 1498 |
* |
| 1499 |
* @param size the size of the data in @a buffer, can be zero |
| 1500 |
* @param buffer the buffer with the data for the response body, can be NULL |
| 1501 |
* if @a size is zero |
| 1502 |
* @return NULL on error (i.e. invalid arguments, out of memory) |
| 1503 |
* @note Available since #MHD_VERSION 0x00097701 |
| 1504 |
* @ingroup response |
| 1505 |
*/ |
| 1506 |
_MHD_EXTERN struct MHD_Response * |
| 1507 |
MHD_create_response_from_buffer_static (size_t size, |
| 1508 |
const void *buffer) |
| 1509 |
{ |
| 1510 |
return MHD_create_response_from_buffer_with_free_callback_cls (size, |
| 1511 |
buffer, |
| 1512 |
NULL, |
| 1513 |
NULL); |
| 1514 |
} |
| 1515 |
|
| 1516 |
|
| 1517 |
/** |
| 1518 |
* Create a response object with the content of provided temporal buffer |
| 1519 |
* used as the response body. |
| 1520 |
* |
| 1521 |
* An internal copy of the buffer will be made automatically, so buffer have |
| 1522 |
* to be valid only during the call of this function (as a typical example: |
| 1523 |
* buffer is a local (non-static) array). |
| 1524 |
* |
| 1525 |
* The response object can be extended with header information and then |
| 1526 |
* be used any number of times. |
| 1527 |
* |
| 1528 |
* If response object is used to answer HEAD request then the body |
| 1529 |
* of the response is not used, while all headers (including automatic |
| 1530 |
* headers) are used. |
| 1531 |
* |
| 1532 |
* @param size the size of the data in @a buffer, can be zero |
| 1533 |
* @param buffer the buffer with the data for the response body, can be NULL |
| 1534 |
* if @a size is zero |
| 1535 |
* @return NULL on error (i.e. invalid arguments, out of memory) |
| 1536 |
* @note Available since #MHD_VERSION 0x00097701 |
| 1537 |
* @ingroup response |
| 1538 |
*/ |
| 1539 |
_MHD_EXTERN struct MHD_Response * |
| 1540 |
MHD_create_response_from_buffer_copy (size_t size, |
| 1541 |
const void *buffer) |
| 1542 |
{ |
| 1543 |
struct MHD_Response *r; |
| 1544 |
void *mhd_copy; |
| 1545 |
|
| 1546 |
if (0 == size) |
| 1547 |
return MHD_create_response_from_buffer_with_free_callback_cls (0, |
| 1548 |
NULL, |
| 1549 |
NULL, |
| 1550 |
NULL); |
| 1551 |
if (NULL == buffer) |
| 1552 |
return NULL; |
| 1553 |
|
| 1554 |
mhd_copy = malloc (size); |
| 1555 |
if (NULL == mhd_copy) |
| 1556 |
return NULL; |
| 1557 |
memcpy (mhd_copy, buffer, size); |
| 1558 |
|
| 1559 |
r = MHD_create_response_from_buffer_with_free_callback_cls (size, |
| 1560 |
mhd_copy, |
| 1561 |
&free, |
| 1562 |
mhd_copy); |
| 1563 |
if (NULL == r) |
| 1564 |
free (mhd_copy); |
| 1565 |
else |
| 1566 |
{ |
| 1567 |
/* TODO: remove the next assignment, the buffer should not be modifiable */ |
| 1568 |
r->data_buffer_size = size; |
| 1569 |
} |
| 1570 |
|
| 1571 |
return r; |
| 1572 |
} |
| 1573 |
|
| 1574 |
|
| 1575 |
/** |
| 1576 |
* Create a response object with the content of provided buffer used as |
| 1577 |
* the response body. |
| 1578 |
* |
| 1579 |
* The response object can be extended with header information and then |
| 1580 |
* be used any number of times. |
| 1581 |
* |
| 1582 |
* If response object is used to answer HEAD request then the body |
| 1583 |
* of the response is not used, while all headers (including automatic |
| 1584 |
* headers) are used. |
| 1585 |
* |
| 1586 |
* @param size size of the data portion of the response |
| 1587 |
* @param buffer size bytes containing the response's data portion |
| 1588 |
* @param crfc function to call to free the @a buffer |
| 1589 |
* @return NULL on error (i.e. invalid arguments, out of memory) |
| 1590 |
* @ingroup response |
| 1591 |
*/ |
| 1592 |
_MHD_EXTERN struct MHD_Response * |
| 1593 |
MHD_create_response_from_buffer_with_free_callback (size_t size, |
| 1594 |
void *buffer, |
| 1595 |
MHD_ContentReaderFreeCallback |
| 1596 |
crfc) |
| 1597 |
{ |
| 1598 |
return MHD_create_response_from_buffer_with_free_callback_cls (size, |
| 1599 |
buffer, |
| 1600 |
crfc, |
| 1601 |
buffer); |
| 1602 |
} |
| 1603 |
|
| 1604 |
|
| 1605 |
/** |
| 1606 |
* Create a response object with the content of provided buffer used as |
| 1607 |
* the response body. |
| 1608 |
* |
| 1609 |
* The response object can be extended with header information and then |
| 1610 |
* be used any number of times. |
| 1611 |
* |
| 1612 |
* If response object is used to answer HEAD request then the body |
| 1613 |
* of the response is not used, while all headers (including automatic |
| 1614 |
* headers) are used. |
| 1615 |
* |
| 1616 |
* @param size size of the data portion of the response |
| 1617 |
* @param buffer size bytes containing the response's data portion |
| 1618 |
* @param crfc function to call to cleanup, if set to NULL then callback |
| 1619 |
* is not called |
| 1620 |
* @param crfc_cls an argument for @a crfc |
| 1621 |
* @return NULL on error (i.e. invalid arguments, out of memory) |
| 1622 |
* @note Available since #MHD_VERSION 0x00097302 |
| 1623 |
* @note 'const' qualifier is used for @a buffer since #MHD_VERSION 0x00097701 |
| 1624 |
* @ingroup response |
| 1625 |
*/ |
| 1626 |
_MHD_EXTERN struct MHD_Response * |
| 1627 |
MHD_create_response_from_buffer_with_free_callback_cls (size_t size, |
| 1628 |
const void *buffer, |
| 1629 |
MHD_ContentReaderFreeCallback |
| 1630 |
crfc, |
| 1631 |
void *crfc_cls) |
| 1632 |
{ |
| 1633 |
struct MHD_Response *r; |
| 1634 |
|
| 1635 |
if ((NULL == buffer) && (size > 0)) |
| 1636 |
return NULL; |
| 1637 |
#if SIZEOF_SIZE_T >= SIZEOF_UINT64_T |
| 1638 |
if (MHD_SIZE_UNKNOWN == size) |
| 1639 |
return NULL; |
| 1640 |
#endif /* SIZEOF_SIZE_T >= SIZEOF_UINT64_T */ |
| 1641 |
r = MHD_calloc_ (1, sizeof (struct MHD_Response)); |
| 1642 |
if (NULL == r) |
| 1643 |
return NULL; |
| 1644 |
#if defined(MHD_USE_THREADS) |
| 1645 |
if (! MHD_mutex_init_ (&r->mutex)) |
| 1646 |
{ |
| 1647 |
free (r); |
| 1648 |
return NULL; |
| 1649 |
} |
| 1650 |
#endif |
| 1651 |
r->fd = -1; |
| 1652 |
r->reference_count = 1; |
| 1653 |
r->total_size = size; |
| 1654 |
r->data = buffer; |
| 1655 |
r->data_size = size; |
| 1656 |
r->crfc = crfc; |
| 1657 |
r->crc_cls = crfc_cls; |
| 1658 |
return r; |
| 1659 |
} |
| 1660 |
|
| 1661 |
|
| 1662 |
/** |
| 1663 |
* Create a response object with an array of memory buffers |
| 1664 |
* used as the response body. |
| 1665 |
* |
| 1666 |
* The response object can be extended with header information and then |
| 1667 |
* be used any number of times. |
| 1668 |
* |
| 1669 |
* If response object is used to answer HEAD request then the body |
| 1670 |
* of the response is not used, while all headers (including automatic |
| 1671 |
* headers) are used. |
| 1672 |
* |
| 1673 |
* @param iov the array for response data buffers, an internal copy of this |
| 1674 |
* will be made |
| 1675 |
* @param iovcnt the number of elements in @a iov |
| 1676 |
* @param free_cb the callback to clean up any data associated with @a iov when |
| 1677 |
* the response is destroyed. |
| 1678 |
* @param cls the argument passed to @a free_cb |
| 1679 |
* @return NULL on error (i.e. invalid arguments, out of memory) |
| 1680 |
*/ |
| 1681 |
_MHD_EXTERN struct MHD_Response * |
| 1682 |
MHD_create_response_from_iovec (const struct MHD_IoVec *iov, |
| 1683 |
unsigned int iovcnt, |
| 1684 |
MHD_ContentReaderFreeCallback free_cb, |
| 1685 |
void *cls) |
| 1686 |
{ |
| 1687 |
struct MHD_Response *response; |
| 1688 |
unsigned int i; |
| 1689 |
int i_cp = 0; /**< Index in the copy of iov */ |
| 1690 |
uint64_t total_size = 0; |
| 1691 |
const void *last_valid_buffer = NULL; |
| 1692 |
|
| 1693 |
if ((NULL == iov) && (0 < iovcnt)) |
| 1694 |
return NULL; |
| 1695 |
|
| 1696 |
response = MHD_calloc_ (1, sizeof (struct MHD_Response)); |
| 1697 |
if (NULL == response) |
| 1698 |
return NULL; |
| 1699 |
if (! MHD_mutex_init_ (&response->mutex)) |
| 1700 |
{ |
| 1701 |
free (response); |
| 1702 |
return NULL; |
| 1703 |
} |
| 1704 |
/* Calculate final size, number of valid elements, and check 'iov' */ |
| 1705 |
for (i = 0; i < iovcnt; ++i) |
| 1706 |
{ |
| 1707 |
if (0 == iov[i].iov_len) |
| 1708 |
continue; /* skip zero-sized elements */ |
| 1709 |
if (NULL == iov[i].iov_base) |
| 1710 |
{ |
| 1711 |
i_cp = -1; /* error */ |
| 1712 |
break; |
| 1713 |
} |
| 1714 |
if ( (total_size > (total_size + iov[i].iov_len)) || |
| 1715 |
(INT_MAX == i_cp) || |
| 1716 |
(SSIZE_MAX < (total_size + iov[i].iov_len)) ) |
| 1717 |
{ |
| 1718 |
i_cp = -1; /* overflow */ |
| 1719 |
break; |
| 1720 |
} |
| 1721 |
last_valid_buffer = iov[i].iov_base; |
| 1722 |
total_size += iov[i].iov_len; |
| 1723 |
#if defined(MHD_POSIX_SOCKETS) || ! defined(_WIN64) |
| 1724 |
i_cp++; |
| 1725 |
#else /* ! MHD_POSIX_SOCKETS && _WIN64 */ |
| 1726 |
{ |
| 1727 |
int64_t i_add; |
| 1728 |
|
| 1729 |
i_add = (int64_t) (iov[i].iov_len / ULONG_MAX); |
| 1730 |
if (0 != iov[i].iov_len % ULONG_MAX) |
| 1731 |
i_add++; |
| 1732 |
if (INT_MAX < (i_add + i_cp)) |
| 1733 |
{ |
| 1734 |
i_cp = -1; /* overflow */ |
| 1735 |
break; |
| 1736 |
} |
| 1737 |
i_cp += (int) i_add; |
| 1738 |
} |
| 1739 |
#endif /* ! MHD_POSIX_SOCKETS && _WIN64 */ |
| 1740 |
} |
| 1741 |
if (-1 == i_cp) |
| 1742 |
{ |
| 1743 |
/* Some error condition */ |
| 1744 |
MHD_mutex_destroy_chk_ (&response->mutex); |
| 1745 |
free (response); |
| 1746 |
return NULL; |
| 1747 |
} |
| 1748 |
response->fd = -1; |
| 1749 |
response->reference_count = 1; |
| 1750 |
response->total_size = total_size; |
| 1751 |
response->crc_cls = cls; |
| 1752 |
response->crfc = free_cb; |
| 1753 |
if (0 == i_cp) |
| 1754 |
{ |
| 1755 |
mhd_assert (0 == total_size); |
| 1756 |
return response; |
| 1757 |
} |
| 1758 |
if (1 == i_cp) |
| 1759 |
{ |
| 1760 |
mhd_assert (NULL != last_valid_buffer); |
| 1761 |
response->data = last_valid_buffer; |
| 1762 |
response->data_size = (size_t) total_size; |
| 1763 |
return response; |
| 1764 |
} |
| 1765 |
mhd_assert (1 < i_cp); |
| 1766 |
if (1) |
| 1767 |
{ /* for local variables local scope only */ |
| 1768 |
MHD_iovec_ *iov_copy; |
| 1769 |
int num_copy_elements = i_cp; |
| 1770 |
|
| 1771 |
iov_copy = MHD_calloc_ ((size_t) num_copy_elements, \ |
| 1772 |
sizeof(MHD_iovec_)); |
| 1773 |
if (NULL == iov_copy) |
| 1774 |
{ |
| 1775 |
MHD_mutex_destroy_chk_ (&response->mutex); |
| 1776 |
free (response); |
| 1777 |
return NULL; |
| 1778 |
} |
| 1779 |
i_cp = 0; |
| 1780 |
for (i = 0; i < iovcnt; ++i) |
| 1781 |
{ |
| 1782 |
size_t element_size = iov[i].iov_len; |
| 1783 |
const uint8_t *buf = (const uint8_t *) iov[i].iov_base; |
| 1784 |
|
| 1785 |
if (0 == element_size) |
| 1786 |
continue; /* skip zero-sized elements */ |
| 1787 |
#if defined(MHD_WINSOCK_SOCKETS) && defined(_WIN64) |
| 1788 |
while (MHD_IOV_ELMN_MAX_SIZE < element_size) |
| 1789 |
{ |
| 1790 |
iov_copy[i_cp].iov_base = (char *) _MHD_DROP_CONST (buf); |
| 1791 |
iov_copy[i_cp].iov_len = ULONG_MAX; |
| 1792 |
buf += ULONG_MAX; |
| 1793 |
element_size -= ULONG_MAX; |
| 1794 |
i_cp++; |
| 1795 |
} |
| 1796 |
#endif /* MHD_WINSOCK_SOCKETS && _WIN64 */ |
| 1797 |
iov_copy[i_cp].iov_base = _MHD_DROP_CONST (buf); |
| 1798 |
iov_copy[i_cp].iov_len = (MHD_iov_size_) element_size; |
| 1799 |
i_cp++; |
| 1800 |
} |
| 1801 |
mhd_assert (num_copy_elements == i_cp); |
| 1802 |
mhd_assert (0 <= i_cp); |
| 1803 |
response->data_iov = iov_copy; |
| 1804 |
response->data_iovcnt = (unsigned int) i_cp; |
| 1805 |
} |
| 1806 |
return response; |
| 1807 |
} |
| 1808 |
|
| 1809 |
|
| 1810 |
/** |
| 1811 |
* Create a response object with empty (zero size) body. |
| 1812 |
* |
| 1813 |
* The response object can be extended with header information and then be used |
| 1814 |
* any number of times. |
| 1815 |
* |
| 1816 |
* This function is a faster equivalent of #MHD_create_response_from_buffer call |
| 1817 |
* with zero size combined with call of #MHD_set_response_options. |
| 1818 |
* |
| 1819 |
* @param flags the flags for the new response object |
| 1820 |
* @return NULL on error (i.e. invalid arguments, out of memory), |
| 1821 |
* the pointer to the created response object otherwise |
| 1822 |
* @note Available since #MHD_VERSION 0x00097701 |
| 1823 |
* @ingroup response |
| 1824 |
*/ |
| 1825 |
_MHD_EXTERN struct MHD_Response * |
| 1826 |
MHD_create_response_empty (enum MHD_ResponseFlags flags) |
| 1827 |
{ |
| 1828 |
struct MHD_Response *r; |
| 1829 |
r = (struct MHD_Response *) MHD_calloc_ (1, sizeof (struct MHD_Response)); |
| 1830 |
if (NULL != r) |
| 1831 |
{ |
| 1832 |
if (MHD_mutex_init_ (&r->mutex)) |
| 1833 |
{ |
| 1834 |
r->fd = -1; |
| 1835 |
r->reference_count = 1; |
| 1836 |
/* If any flags combination will be not allowed, replace the next |
| 1837 |
* assignment with MHD_set_response_options() call. */ |
| 1838 |
r->flags = flags; |
| 1839 |
|
| 1840 |
return r; /* Successful result */ |
| 1841 |
} |
| 1842 |
free (r); |
| 1843 |
} |
| 1844 |
return NULL; /* Something failed */ |
| 1845 |
} |
| 1846 |
|
| 1847 |
|
| 1848 |
#ifdef UPGRADE_SUPPORT |
| 1849 |
/** |
| 1850 |
* This connection-specific callback is provided by MHD to |
| 1851 |
* applications (unusual) during the #MHD_UpgradeHandler. |
| 1852 |
* It allows applications to perform 'special' actions on |
| 1853 |
* the underlying socket from the upgrade. |
| 1854 |
* |
| 1855 |
* @param urh the handle identifying the connection to perform |
| 1856 |
* the upgrade @a action on. |
| 1857 |
* @param action which action should be performed |
| 1858 |
* @param ... arguments to the action (depends on the action) |
| 1859 |
* @return #MHD_NO on error, #MHD_YES on success |
| 1860 |
*/ |
| 1861 |
_MHD_EXTERN enum MHD_Result |
| 1862 |
MHD_upgrade_action (struct MHD_UpgradeResponseHandle *urh, |
| 1863 |
enum MHD_UpgradeAction action, |
| 1864 |
...) |
| 1865 |
{ |
| 1866 |
struct MHD_Connection *connection; |
| 1867 |
struct MHD_Daemon *daemon; |
| 1868 |
|
| 1869 |
if (NULL == urh) |
| 1870 |
return MHD_NO; |
| 1871 |
connection = urh->connection; |
| 1872 |
|
| 1873 |
/* Precaution checks on external data. */ |
| 1874 |
if (NULL == connection) |
| 1875 |
return MHD_NO; |
| 1876 |
daemon = connection->daemon; |
| 1877 |
if (NULL == daemon) |
| 1878 |
return MHD_NO; |
| 1879 |
|
| 1880 |
switch (action) |
| 1881 |
{ |
| 1882 |
case MHD_UPGRADE_ACTION_CLOSE: |
| 1883 |
if (urh->was_closed) |
| 1884 |
return MHD_NO; /* Already closed. */ |
| 1885 |
|
| 1886 |
/* transition to special 'closed' state for start of cleanup */ |
| 1887 |
#ifdef HTTPS_SUPPORT |
| 1888 |
if (0 != (daemon->options & MHD_USE_TLS) ) |
| 1889 |
{ |
| 1890 |
/* signal that app is done by shutdown() of 'app' socket */ |
| 1891 |
/* Application will not use anyway this socket after this command. */ |
| 1892 |
shutdown (urh->app.socket, |
| 1893 |
SHUT_RDWR); |
| 1894 |
} |
| 1895 |
#endif /* HTTPS_SUPPORT */ |
| 1896 |
mhd_assert (MHD_CONNECTION_UPGRADE == connection->state); |
| 1897 |
/* The next function will mark the connection as closed by application |
| 1898 |
* by setting 'urh->was_closed'. |
| 1899 |
* As soon as connection will be marked with BOTH |
| 1900 |
* 'urh->was_closed' AND 'urh->clean_ready', it will |
| 1901 |
* be moved to cleanup list by MHD_resume_connection(). */ |
| 1902 |
MHD_upgraded_connection_mark_app_closed_ (connection); |
| 1903 |
return MHD_YES; |
| 1904 |
case MHD_UPGRADE_ACTION_CORK_ON: |
| 1905 |
/* Unportable API. TODO: replace with portable action. */ |
| 1906 |
return MHD_connection_set_cork_state_ (connection, |
| 1907 |
true) ? MHD_YES : MHD_NO; |
| 1908 |
case MHD_UPGRADE_ACTION_CORK_OFF: |
| 1909 |
/* Unportable API. TODO: replace with portable action. */ |
| 1910 |
return MHD_connection_set_cork_state_ (connection, |
| 1911 |
false) ? MHD_YES : MHD_NO; |
| 1912 |
default: |
| 1913 |
/* we don't understand this one */ |
| 1914 |
return MHD_NO; |
| 1915 |
} |
| 1916 |
} |
| 1917 |
|
| 1918 |
|
| 1919 |
/** |
| 1920 |
* We are done sending the header of a given response to the client. |
| 1921 |
* Now it is time to perform the upgrade and hand over the connection |
| 1922 |
* to the application. |
| 1923 |
* @remark To be called only from thread that process connection's |
| 1924 |
* recv(), send() and response. Must be called right after sending |
| 1925 |
* response headers. |
| 1926 |
* |
| 1927 |
* @param response the response that was created for an upgrade |
| 1928 |
* @param connection the specific connection we are upgrading |
| 1929 |
* @return #MHD_YES on success, #MHD_NO on failure (will cause |
| 1930 |
* connection to be closed) |
| 1931 |
*/ |
| 1932 |
enum MHD_Result |
| 1933 |
MHD_response_execute_upgrade_ (struct MHD_Response *response, |
| 1934 |
struct MHD_Connection *connection) |
| 1935 |
{ |
| 1936 |
#if defined(HTTPS_SUPPORT) || defined(_DEBUG) || defined(HAVE_MESSAGES) |
| 1937 |
struct MHD_Daemon *const daemon = connection->daemon; |
| 1938 |
#endif /* HTTPS_SUPPORT || _DEBUG || HAVE_MESSAGES */ |
| 1939 |
struct MHD_UpgradeResponseHandle *urh; |
| 1940 |
size_t rbo; |
| 1941 |
|
| 1942 |
#ifdef MHD_USE_THREADS |
| 1943 |
mhd_assert ( (! MHD_D_IS_USING_THREADS_ (daemon)) || \ |
| 1944 |
MHD_thread_handle_ID_is_current_thread_ (connection->tid) ); |
| 1945 |
#endif /* MHD_USE_THREADS */ |
| 1946 |
|
| 1947 |
/* "Upgrade" responses accepted only if MHD_ALLOW_UPGRADE is enabled */ |
| 1948 |
mhd_assert (0 != (daemon->options & MHD_ALLOW_UPGRADE)); |
| 1949 |
/* The header was checked when response queued */ |
| 1950 |
mhd_assert (NULL != \ |
| 1951 |
MHD_get_response_element_n_ (response, MHD_HEADER_KIND, |
| 1952 |
MHD_HTTP_HEADER_UPGRADE, |
| 1953 |
MHD_STATICSTR_LEN_ ( \ |
| 1954 |
MHD_HTTP_HEADER_UPGRADE))); |
| 1955 |
|
| 1956 |
if (! connection->sk_nonblck) |
| 1957 |
{ |
| 1958 |
#ifdef HAVE_MESSAGES |
| 1959 |
MHD_DLOG (daemon, |
| 1960 |
_ ("Cannot execute \"upgrade\" as the socket is in " \ |
| 1961 |
"the blocking mode.\n")); |
| 1962 |
#endif |
| 1963 |
return MHD_NO; |
| 1964 |
} |
| 1965 |
urh = MHD_calloc_ (1, sizeof (struct MHD_UpgradeResponseHandle)); |
| 1966 |
if (NULL == urh) |
| 1967 |
return MHD_NO; |
| 1968 |
urh->connection = connection; |
| 1969 |
rbo = connection->read_buffer_offset; |
| 1970 |
connection->read_buffer_offset = 0; |
| 1971 |
MHD_connection_set_nodelay_state_ (connection, false); |
| 1972 |
MHD_connection_set_cork_state_ (connection, false); |
| 1973 |
#ifdef HTTPS_SUPPORT |
| 1974 |
if (0 != (daemon->options & MHD_USE_TLS) ) |
| 1975 |
{ |
| 1976 |
MHD_socket sv[2]; |
| 1977 |
#if defined(MHD_socket_nosignal_) || ! defined(MHD_socket_pair_nblk_) |
| 1978 |
int res1; |
| 1979 |
int res2; |
| 1980 |
#endif /* MHD_socket_nosignal_ || !MHD_socket_pair_nblk_ */ |
| 1981 |
|
| 1982 |
#ifdef MHD_socket_pair_nblk_ |
| 1983 |
if (! MHD_socket_pair_nblk_ (sv)) |
| 1984 |
{ |
| 1985 |
free (urh); |
| 1986 |
return MHD_NO; |
| 1987 |
} |
| 1988 |
#else /* !MHD_socket_pair_nblk_ */ |
| 1989 |
if (! MHD_socket_pair_ (sv)) |
| 1990 |
{ |
| 1991 |
free (urh); |
| 1992 |
return MHD_NO; |
| 1993 |
} |
| 1994 |
res1 = MHD_socket_nonblocking_ (sv[0]); |
| 1995 |
res2 = MHD_socket_nonblocking_ (sv[1]); |
| 1996 |
if ( (! res1) || (! res2) ) |
| 1997 |
{ |
| 1998 |
#ifdef HAVE_MESSAGES |
| 1999 |
MHD_DLOG (daemon, |
| 2000 |
_ ("Failed to make loopback sockets non-blocking.\n")); |
| 2001 |
#endif |
| 2002 |
if (! res2) |
| 2003 |
{ |
| 2004 |
/* Socketpair cannot be used. */ |
| 2005 |
MHD_socket_close_chk_ (sv[0]); |
| 2006 |
MHD_socket_close_chk_ (sv[1]); |
| 2007 |
free (urh); |
| 2008 |
return MHD_NO; |
| 2009 |
} |
| 2010 |
} |
| 2011 |
#endif /* !MHD_socket_pair_nblk_ */ |
| 2012 |
#ifdef MHD_socket_nosignal_ |
| 2013 |
res1 = MHD_socket_nosignal_ (sv[0]); |
| 2014 |
res2 = MHD_socket_nosignal_ (sv[1]); |
| 2015 |
if ( (! res1) || (! res2) ) |
| 2016 |
{ |
| 2017 |
#ifdef HAVE_MESSAGES |
| 2018 |
MHD_DLOG (daemon, |
| 2019 |
_ ("Failed to set SO_NOSIGPIPE on loopback sockets.\n")); |
| 2020 |
#endif |
| 2021 |
#ifndef MSG_NOSIGNAL |
| 2022 |
if (! res2) |
| 2023 |
{ |
| 2024 |
/* Socketpair cannot be used. */ |
| 2025 |
MHD_socket_close_chk_ (sv[0]); |
| 2026 |
MHD_socket_close_chk_ (sv[1]); |
| 2027 |
free (urh); |
| 2028 |
return MHD_NO; |
| 2029 |
} |
| 2030 |
#endif /* ! MSG_NOSIGNAL */ |
| 2031 |
} |
| 2032 |
#endif /* MHD_socket_nosignal_ */ |
| 2033 |
if (MHD_D_IS_USING_SELECT_ (daemon) && |
| 2034 |
(! MHD_D_DOES_SCKT_FIT_FDSET_ (sv[1], daemon)) ) |
| 2035 |
{ |
| 2036 |
#ifdef HAVE_MESSAGES |
| 2037 |
MHD_DLOG (daemon, |
| 2038 |
_ ("Socketpair descriptor is not less than FD_SETSIZE: " \ |
| 2039 |
"%d >= %d\n"), |
| 2040 |
(int) sv[1], |
| 2041 |
(int) MHD_D_GET_FD_SETSIZE_ (daemon)); |
| 2042 |
#endif |
| 2043 |
MHD_socket_close_chk_ (sv[0]); |
| 2044 |
MHD_socket_close_chk_ (sv[1]); |
| 2045 |
free (urh); |
| 2046 |
return MHD_NO; |
| 2047 |
} |
| 2048 |
urh->app.socket = sv[0]; |
| 2049 |
urh->app.urh = urh; |
| 2050 |
urh->app.celi = MHD_EPOLL_STATE_UNREADY; |
| 2051 |
urh->mhd.socket = sv[1]; |
| 2052 |
urh->mhd.urh = urh; |
| 2053 |
urh->mhd.celi = MHD_EPOLL_STATE_UNREADY; |
| 2054 |
#ifdef EPOLL_SUPPORT |
| 2055 |
/* Launch IO processing by the event loop */ |
| 2056 |
if (MHD_D_IS_USING_EPOLL_ (daemon)) |
| 2057 |
{ |
| 2058 |
/* We're running with epoll(), need to add the sockets |
| 2059 |
to the event set of the daemon's `epoll_upgrade_fd` */ |
| 2060 |
struct epoll_event event; |
| 2061 |
|
| 2062 |
mhd_assert (-1 != daemon->epoll_upgrade_fd); |
| 2063 |
/* First, add network socket */ |
| 2064 |
event.events = EPOLLIN | EPOLLOUT | EPOLLPRI | EPOLLET; |
| 2065 |
event.data.ptr = &urh->app; |
| 2066 |
if (0 != epoll_ctl (daemon->epoll_upgrade_fd, |
| 2067 |
EPOLL_CTL_ADD, |
| 2068 |
connection->socket_fd, |
| 2069 |
&event)) |
| 2070 |
{ |
| 2071 |
#ifdef HAVE_MESSAGES |
| 2072 |
MHD_DLOG (daemon, |
| 2073 |
_ ("Call to epoll_ctl failed: %s\n"), |
| 2074 |
MHD_socket_last_strerr_ ()); |
| 2075 |
#endif |
| 2076 |
MHD_socket_close_chk_ (sv[0]); |
| 2077 |
MHD_socket_close_chk_ (sv[1]); |
| 2078 |
free (urh); |
| 2079 |
return MHD_NO; |
| 2080 |
} |
| 2081 |
|
| 2082 |
/* Second, add our end of the UNIX socketpair() */ |
| 2083 |
event.events = EPOLLIN | EPOLLOUT | EPOLLPRI | EPOLLET; |
| 2084 |
event.data.ptr = &urh->mhd; |
| 2085 |
if (0 != epoll_ctl (daemon->epoll_upgrade_fd, |
| 2086 |
EPOLL_CTL_ADD, |
| 2087 |
urh->mhd.socket, |
| 2088 |
&event)) |
| 2089 |
{ |
| 2090 |
event.events = EPOLLIN | EPOLLOUT | EPOLLPRI; |
| 2091 |
event.data.ptr = &urh->app; |
| 2092 |
if (0 != epoll_ctl (daemon->epoll_upgrade_fd, |
| 2093 |
EPOLL_CTL_DEL, |
| 2094 |
connection->socket_fd, |
| 2095 |
&event)) |
| 2096 |
MHD_PANIC (_ ("Error cleaning up while handling epoll error.\n")); |
| 2097 |
#ifdef HAVE_MESSAGES |
| 2098 |
MHD_DLOG (daemon, |
| 2099 |
_ ("Call to epoll_ctl failed: %s\n"), |
| 2100 |
MHD_socket_last_strerr_ ()); |
| 2101 |
#endif |
| 2102 |
MHD_socket_close_chk_ (sv[0]); |
| 2103 |
MHD_socket_close_chk_ (sv[1]); |
| 2104 |
free (urh); |
| 2105 |
return MHD_NO; |
| 2106 |
} |
| 2107 |
EDLL_insert (daemon->eready_urh_head, |
| 2108 |
daemon->eready_urh_tail, |
| 2109 |
urh); |
| 2110 |
urh->in_eready_list = true; |
| 2111 |
} |
| 2112 |
#endif /* EPOLL_SUPPORT */ |
| 2113 |
if (! MHD_D_IS_USING_THREAD_PER_CONN_ (daemon)) |
| 2114 |
{ |
| 2115 |
/* This takes care of further processing for most event loops: |
| 2116 |
simply add to DLL for bi-direcitonal processing */ |
| 2117 |
DLL_insert (daemon->urh_head, |
| 2118 |
daemon->urh_tail, |
| 2119 |
urh); |
| 2120 |
} |
| 2121 |
/* In thread-per-connection mode, thread will switch to forwarding once |
| 2122 |
* connection.urh is not NULL and connection.state == MHD_CONNECTION_UPGRADE. |
| 2123 |
*/ |
| 2124 |
} |
| 2125 |
else |
| 2126 |
{ |
| 2127 |
urh->app.socket = MHD_INVALID_SOCKET; |
| 2128 |
urh->mhd.socket = MHD_INVALID_SOCKET; |
| 2129 |
/* Non-TLS connection do not hold any additional resources. */ |
| 2130 |
urh->clean_ready = true; |
| 2131 |
} |
| 2132 |
#else /* ! HTTPS_SUPPORT */ |
| 2133 |
urh->clean_ready = true; |
| 2134 |
#endif /* ! HTTPS_SUPPORT */ |
| 2135 |
connection->urh = urh; |
| 2136 |
/* As far as MHD's event loops are concerned, this connection is |
| 2137 |
suspended; it will be resumed once application is done by the |
| 2138 |
#MHD_upgrade_action() function */ |
| 2139 |
internal_suspend_connection_ (connection); |
| 2140 |
|
| 2141 |
/* hand over socket to application */ |
| 2142 |
response->upgrade_handler (response->upgrade_handler_cls, |
| 2143 |
connection, |
| 2144 |
connection->rq.client_context, |
| 2145 |
connection->read_buffer, |
| 2146 |
rbo, |
| 2147 |
#ifdef HTTPS_SUPPORT |
| 2148 |
(0 == (daemon->options & MHD_USE_TLS) ) ? |
| 2149 |
connection->socket_fd : urh->app.socket, |
| 2150 |
#else /* ! HTTPS_SUPPORT */ |
| 2151 |
connection->socket_fd, |
| 2152 |
#endif /* ! HTTPS_SUPPORT */ |
| 2153 |
urh); |
| 2154 |
|
| 2155 |
#ifdef HTTPS_SUPPORT |
| 2156 |
if (0 != (daemon->options & MHD_USE_TLS)) |
| 2157 |
{ |
| 2158 |
struct MemoryPool *const pool = connection->pool; |
| 2159 |
size_t avail; |
| 2160 |
char *buf; |
| 2161 |
|
| 2162 |
/* All data should be sent already */ |
| 2163 |
mhd_assert (connection->write_buffer_send_offset == \ |
| 2164 |
connection->write_buffer_append_offset); |
| 2165 |
MHD_pool_deallocate (pool, connection->write_buffer, |
| 2166 |
connection->write_buffer_size); |
| 2167 |
connection->write_buffer_append_offset = 0; |
| 2168 |
connection->write_buffer_send_offset = 0; |
| 2169 |
connection->write_buffer_size = 0; |
| 2170 |
connection->write_buffer = NULL; |
| 2171 |
|
| 2172 |
/* Extra read data should be processed already by the application */ |
| 2173 |
MHD_pool_deallocate (pool, connection->read_buffer, |
| 2174 |
connection->read_buffer_size); |
| 2175 |
connection->read_buffer_offset = 0; |
| 2176 |
connection->read_buffer_size = 0; |
| 2177 |
connection->read_buffer = NULL; |
| 2178 |
|
| 2179 |
avail = MHD_pool_get_free (pool); |
| 2180 |
if (avail < RESERVE_EBUF_SIZE) |
| 2181 |
{ |
| 2182 |
/* connection's pool is totally at the limit, |
| 2183 |
use our 'emergency' buffer of #RESERVE_EBUF_SIZE bytes. */ |
| 2184 |
avail = RESERVE_EBUF_SIZE; |
| 2185 |
buf = urh->e_buf; |
| 2186 |
#ifdef HAVE_MESSAGES |
| 2187 |
MHD_DLOG (daemon, |
| 2188 |
_ ("Memory shortage in connection's memory pool. " \ |
| 2189 |
"The \"upgraded\" communication will be inefficient.\n")); |
| 2190 |
#endif |
| 2191 |
} |
| 2192 |
else |
| 2193 |
{ |
| 2194 |
/* Normal case: grab all remaining memory from the |
| 2195 |
connection's pool for the IO buffers; the connection |
| 2196 |
certainly won't need it anymore as we've upgraded |
| 2197 |
to another protocol. */ |
| 2198 |
buf = MHD_pool_allocate (pool, |
| 2199 |
avail, |
| 2200 |
false); |
| 2201 |
} |
| 2202 |
/* use half the buffer for inbound, half for outbound */ |
| 2203 |
urh->in_buffer_size = avail / 2; |
| 2204 |
urh->out_buffer_size = avail - urh->in_buffer_size; |
| 2205 |
urh->in_buffer = buf; |
| 2206 |
urh->out_buffer = buf + urh->in_buffer_size; |
| 2207 |
} |
| 2208 |
#endif /* HTTPS_SUPPORT */ |
| 2209 |
return MHD_YES; |
| 2210 |
} |
| 2211 |
|
| 2212 |
|
| 2213 |
/** |
| 2214 |
* Create a response object that can be used for 101 UPGRADE |
| 2215 |
* responses, for example to implement WebSockets. After sending the |
| 2216 |
* response, control over the data stream is given to the callback (which |
| 2217 |
* can then, for example, start some bi-directional communication). |
| 2218 |
* If the response is queued for multiple connections, the callback |
| 2219 |
* will be called for each connection. The callback |
| 2220 |
* will ONLY be called after the response header was successfully passed |
| 2221 |
* to the OS; if there are communication errors before, the usual MHD |
| 2222 |
* connection error handling code will be performed. |
| 2223 |
* |
| 2224 |
* Setting the correct HTTP code (i.e. MHD_HTTP_SWITCHING_PROTOCOLS) |
| 2225 |
* and setting correct HTTP headers for the upgrade must be done |
| 2226 |
* manually (this way, it is possible to implement most existing |
| 2227 |
* WebSocket versions using this API; in fact, this API might be useful |
| 2228 |
* for any protocol switch, not just WebSockets). Note that |
| 2229 |
* draft-ietf-hybi-thewebsocketprotocol-00 cannot be implemented this |
| 2230 |
* way as the header "HTTP/1.1 101 WebSocket Protocol Handshake" |
| 2231 |
* cannot be generated; instead, MHD will always produce "HTTP/1.1 101 |
| 2232 |
* Switching Protocols" (if the response code 101 is used). |
| 2233 |
* |
| 2234 |
* As usual, the response object can be extended with header |
| 2235 |
* information and then be used any number of times (as long as the |
| 2236 |
* header information is not connection-specific). |
| 2237 |
* |
| 2238 |
* @param upgrade_handler function to call with the 'upgraded' socket |
| 2239 |
* @param upgrade_handler_cls closure for @a upgrade_handler |
| 2240 |
* @return NULL on error (i.e. invalid arguments, out of memory) |
| 2241 |
*/ |
| 2242 |
_MHD_EXTERN struct MHD_Response * |
| 2243 |
MHD_create_response_for_upgrade (MHD_UpgradeHandler upgrade_handler, |
| 2244 |
void *upgrade_handler_cls) |
| 2245 |
{ |
| 2246 |
struct MHD_Response *response; |
| 2247 |
|
| 2248 |
if (NULL == upgrade_handler) |
| 2249 |
return NULL; /* invalid request */ |
| 2250 |
response = MHD_calloc_ (1, sizeof (struct MHD_Response)); |
| 2251 |
if (NULL == response) |
| 2252 |
return NULL; |
| 2253 |
#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) |
| 2254 |
if (! MHD_mutex_init_ (&response->mutex)) |
| 2255 |
{ |
| 2256 |
free (response); |
| 2257 |
return NULL; |
| 2258 |
} |
| 2259 |
#endif |
| 2260 |
response->upgrade_handler = upgrade_handler; |
| 2261 |
response->upgrade_handler_cls = upgrade_handler_cls; |
| 2262 |
response->total_size = 0; |
| 2263 |
response->reference_count = 1; |
| 2264 |
if (MHD_NO == |
| 2265 |
MHD_add_response_header (response, |
| 2266 |
MHD_HTTP_HEADER_CONNECTION, |
| 2267 |
"Upgrade")) |
| 2268 |
{ |
| 2269 |
MHD_destroy_response (response); |
| 2270 |
return NULL; |
| 2271 |
} |
| 2272 |
return response; |
| 2273 |
} |
| 2274 |
|
| 2275 |
|
| 2276 |
#endif /* UPGRADE_SUPPORT */ |
| 2277 |
|
| 2278 |
|
| 2279 |
/** |
| 2280 |
* Destroy a response object and associated resources. Note that |
| 2281 |
* libmicrohttpd may keep some of the resources around if the response |
| 2282 |
* is still in the queue for some clients, so the memory may not |
| 2283 |
* necessarily be freed immediately. |
| 2284 |
* |
| 2285 |
* @param response response to destroy |
| 2286 |
* @ingroup response |
| 2287 |
*/ |
| 2288 |
_MHD_EXTERN void |
| 2289 |
MHD_destroy_response (struct MHD_Response *response) |
| 2290 |
{ |
| 2291 |
struct MHD_HTTP_Res_Header *pos; |
| 2292 |
|
| 2293 |
if (NULL == response) |
| 2294 |
return; |
| 2295 |
#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) |
| 2296 |
MHD_mutex_lock_chk_ (&response->mutex); |
| 2297 |
#endif |
| 2298 |
if (0 != --(response->reference_count)) |
| 2299 |
{ |
| 2300 |
#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) |
| 2301 |
MHD_mutex_unlock_chk_ (&response->mutex); |
| 2302 |
#endif |
| 2303 |
return; |
| 2304 |
} |
| 2305 |
#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) |
| 2306 |
MHD_mutex_unlock_chk_ (&response->mutex); |
| 2307 |
MHD_mutex_destroy_chk_ (&response->mutex); |
| 2308 |
#endif |
| 2309 |
if (NULL != response->crfc) |
| 2310 |
response->crfc (response->crc_cls); |
| 2311 |
|
| 2312 |
if (NULL != response->data_iov) |
| 2313 |
{ |
| 2314 |
free (response->data_iov); |
| 2315 |
} |
| 2316 |
|
| 2317 |
while (NULL != response->first_header) |
| 2318 |
{ |
| 2319 |
pos = response->first_header; |
| 2320 |
response->first_header = pos->next; |
| 2321 |
free (pos->header); |
| 2322 |
free (pos->value); |
| 2323 |
free (pos); |
| 2324 |
} |
| 2325 |
free (response); |
| 2326 |
} |
| 2327 |
|
| 2328 |
|
| 2329 |
/** |
| 2330 |
* Increments the reference counter for the @a response. |
| 2331 |
* |
| 2332 |
* @param response object to modify |
| 2333 |
*/ |
| 2334 |
void |
| 2335 |
MHD_increment_response_rc (struct MHD_Response *response) |
| 2336 |
{ |
| 2337 |
#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) |
| 2338 |
MHD_mutex_lock_chk_ (&response->mutex); |
| 2339 |
#endif |
| 2340 |
(response->reference_count)++; |
| 2341 |
#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) |
| 2342 |
MHD_mutex_unlock_chk_ (&response->mutex); |
| 2343 |
#endif |
| 2344 |
} |
| 2345 |
|
| 2346 |
|
| 2347 |
/* end of response.c */ |