GNU libmicrohttpd  0.9.68
response.c
Go to the documentation of this file.
1 /*
2  This file is part of libmicrohttpd
3  Copyright (C) 2007, 2009, 2010, 2016, 2017 Daniel Pittman and Christian Grothoff
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
27 #define MHD_NO_DEPRECATION 1
28 
29 #include "mhd_options.h"
30 #ifdef HAVE_SYS_IOCTL_H
31 #include <sys/ioctl.h>
32 #endif /* HAVE_SYS_IOCTL_H */
33 #if defined(_WIN32) && ! defined(__CYGWIN__)
34 #include <windows.h>
35 #endif /* _WIN32 && !__CYGWIN__ */
36 
37 #include "internal.h"
38 #include "response.h"
39 #include "mhd_limits.h"
40 #include "mhd_sockets.h"
41 #include "mhd_itc.h"
42 #include "mhd_str.h"
43 #include "connection.h"
44 #include "memorypool.h"
45 #include "mhd_compat.h"
46 
47 
48 #if defined(MHD_W32_MUTEX_)
49 #ifndef WIN32_LEAN_AND_MEAN
50 #define WIN32_LEAN_AND_MEAN 1
51 #endif /* !WIN32_LEAN_AND_MEAN */
52 #include <windows.h>
53 #endif /* MHD_W32_MUTEX_ */
54 #if defined(_WIN32)
55 #include <io.h> /* for lseek(), read() */
56 #endif /* _WIN32 */
57 
58 
63 #ifndef MHD_FILE_READ_BLOCK_SIZE
64 #ifdef _WIN32
65 #define MHD_FILE_READ_BLOCK_SIZE 16384 /* 16k */
66 #else /* _WIN32 */
67 #define MHD_FILE_READ_BLOCK_SIZE 4096 /* 4k */
68 #endif /* _WIN32 */
69 #endif /* !MHD_FD_BLOCK_SIZE */
70 
71 
81 static int
82 add_response_entry (struct MHD_Response *response,
83  enum MHD_ValueKind kind,
84  const char *header,
85  const char *content)
86 {
87  struct MHD_HTTP_Header *hdr;
88 
89  if ( (NULL == response) ||
90  (NULL == header) ||
91  (NULL == content) ||
92  (0 == header[0]) ||
93  (0 == content[0]) ||
94  (NULL != strchr (header, '\t')) ||
95  (NULL != strchr (header, '\r')) ||
96  (NULL != strchr (header, '\n')) ||
97  (NULL != strchr (content, '\t')) ||
98  (NULL != strchr (content, '\r')) ||
99  (NULL != strchr (content, '\n')) )
100  return MHD_NO;
101  if (NULL == (hdr = malloc (sizeof (struct MHD_HTTP_Header))))
102  return MHD_NO;
103  if (NULL == (hdr->header = strdup (header)))
104  {
105  free (hdr);
106  return MHD_NO;
107  }
108  hdr->header_size = strlen (header);
109  if (NULL == (hdr->value = strdup (content)))
110  {
111  free (hdr->header);
112  free (hdr);
113  return MHD_NO;
114  }
115  hdr->value_size = strlen (content);
116  hdr->kind = kind;
117  hdr->next = response->first_header;
118  response->first_header = hdr;
119  return MHD_YES;
120 }
121 
122 
132 int
134  const char *header,
135  const char *content)
136 {
139  (! MHD_str_equal_caseless_ (content,
140  "identity")) &&
141  (! MHD_str_equal_caseless_ (content,
142  "chunked")) )
143  {
144  /* Setting transfer encodings other than "identity" or
145  "chunked" is not allowed. Note that MHD will set the
146  correct transfer encoding if required automatically. */
147  /* NOTE: for compressed bodies, use the "Content-encoding" header */
148  return MHD_NO;
149  }
151  & response->flags)) &&
154  {
155  /* MHD will set Content-length if allowed and possible,
156  reject attempt by application */
157  return MHD_NO;
158  }
159 
160  return add_response_entry (response,
162  header,
163  content);
164 }
165 
166 
176 int
178  const char *footer,
179  const char *content)
180 {
181  return add_response_entry (response,
183  footer,
184  content);
185 }
186 
187 
197 int
199  const char *header,
200  const char *content)
201 {
202  struct MHD_HTTP_Header *pos;
203  struct MHD_HTTP_Header *prev;
204  size_t header_len;
205  size_t content_len;
206 
207  if ( (NULL == header) ||
208  (NULL == content) )
209  return MHD_NO;
210  header_len = strlen (header);
211  content_len = strlen (content);
212  prev = NULL;
213  pos = response->first_header;
214  while (NULL != pos)
215  {
216  if ((header_len == pos->header_size) &&
217  (content_len == pos->value_size) &&
218  (0 == memcmp (header,
219  pos->header,
220  header_len)) &&
221  (0 == memcmp (content,
222  pos->value,
223  content_len)))
224  {
225  free (pos->header);
226  free (pos->value);
227  if (NULL == prev)
228  response->first_header = pos->next;
229  else
230  prev->next = pos->next;
231  free (pos);
232  return MHD_YES;
233  }
234  prev = pos;
235  pos = pos->next;
236  }
237  return MHD_NO;
238 }
239 
240 
251 int
253  MHD_KeyValueIterator iterator,
254  void *iterator_cls)
255 {
256  int numHeaders = 0;
257  struct MHD_HTTP_Header *pos;
258 
259  for (pos = response->first_header;
260  NULL != pos;
261  pos = pos->next)
262  {
263  numHeaders++;
264  if ((NULL != iterator) &&
265  (MHD_YES != iterator (iterator_cls,
266  pos->kind,
267  pos->header,
268  pos->value)))
269  break;
270  }
271  return numHeaders;
272 }
273 
274 
283 const char *
285  const char *key)
286 {
287  struct MHD_HTTP_Header *pos;
288  size_t key_size;
289 
290  if (NULL == key)
291  return NULL;
292 
293  key_size = strlen (key);
294  for (pos = response->first_header;
295  NULL != pos;
296  pos = pos->next)
297  {
298  if ((pos->header_size == key_size) &&
300  return pos->value;
301  }
302  return NULL;
303 }
304 
305 
322 bool
324  const char *key,
325  size_t key_len,
326  const char *token,
327  size_t token_len)
328 {
329  struct MHD_HTTP_Header *pos;
330 
331  if ( (NULL == key) ||
332  ('\0' == key[0]) ||
333  (NULL == token) ||
334  ('\0' == token[0]) )
335  return false;
336 
337  /* Token must not contain binary zero! */
338  mhd_assert (strlen (token) == token_len);
339 
340  for (pos = response->first_header;
341  NULL != pos;
342  pos = pos->next)
343  {
344  if ( (pos->kind == MHD_HEADER_KIND) &&
345  (key_len == pos->header_size) &&
347  key,
348  key_len) &&
350  token,
351  token_len) )
352  return true;
353  }
354  return false;
355 }
356 
357 
374 struct MHD_Response *
376  size_t block_size,
378  void *crc_cls,
380 {
381  struct MHD_Response *response;
382 
383  if ((NULL == crc) || (0 == block_size))
384  return NULL;
385  if (NULL == (response = MHD_calloc_ (1, sizeof (struct MHD_Response)
386  + block_size)))
387  return NULL;
388  response->fd = -1;
389  response->data = (void *) &response[1];
390  response->data_buffer_size = block_size;
391 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
392  if (! MHD_mutex_init_ (&response->mutex))
393  {
394  free (response);
395  return NULL;
396  }
397 #endif
398  response->crc = crc;
399  response->crfc = crfc;
400  response->crc_cls = crc_cls;
401  response->reference_count = 1;
402  response->total_size = size;
403  return response;
404 }
405 
406 
415 int
418  ...)
419 {
420  va_list ap;
421  int ret;
422  enum MHD_ResponseOptions ro;
423 
424  ret = MHD_YES;
425  response->flags = flags;
426  va_start (ap, flags);
427  while (MHD_RO_END != (ro = va_arg (ap, enum MHD_ResponseOptions)))
428  {
429  switch (ro)
430  {
431  default:
432  ret = MHD_NO;
433  break;
434  }
435  }
436  va_end (ap);
437  return ret;
438 }
439 
440 
451 static ssize_t
452 file_reader (void *cls,
453  uint64_t pos,
454  char *buf,
455  size_t max)
456 {
457  struct MHD_Response *response = cls;
458 #if ! defined(_WIN32) || defined(__CYGWIN__)
459  ssize_t n;
460 #else /* _WIN32 && !__CYGWIN__ */
461  const HANDLE fh = (HANDLE) _get_osfhandle (response->fd);
462 #endif /* _WIN32 && !__CYGWIN__ */
463  const int64_t offset64 = (int64_t) (pos + response->fd_off);
464 
465  if (offset64 < 0)
466  return MHD_CONTENT_READER_END_WITH_ERROR; /* seek to required position is not possible */
467 
468 #if ! defined(_WIN32) || defined(__CYGWIN__)
469  if (max > SSIZE_MAX)
470  max = SSIZE_MAX; /* Clamp to maximum return value. */
471 
472 #if defined(HAVE_PREAD64)
473  n = pread64 (response->fd, buf, max, offset64);
474 #elif defined(HAVE_PREAD)
475  if ( (sizeof(off_t) < sizeof (uint64_t)) &&
476  (offset64 > (uint64_t) INT32_MAX) )
477  return MHD_CONTENT_READER_END_WITH_ERROR; /* Read at required position is not possible. */
478 
479  n = pread (response->fd, buf, max, (off_t) offset64);
480 #else /* ! HAVE_PREAD */
481 #if defined(HAVE_LSEEK64)
482  if (lseek64 (response->fd,
483  offset64,
484  SEEK_SET) != offset64)
485  return MHD_CONTENT_READER_END_WITH_ERROR; /* can't seek to required position */
486 #else /* ! HAVE_LSEEK64 */
487  if ( (sizeof(off_t) < sizeof (uint64_t)) &&
488  (offset64 > (uint64_t) INT32_MAX) )
489  return MHD_CONTENT_READER_END_WITH_ERROR; /* seek to required position is not possible */
490 
491  if (lseek (response->fd,
492  (off_t) offset64,
493  SEEK_SET) != (off_t) offset64)
494  return MHD_CONTENT_READER_END_WITH_ERROR; /* can't seek to required position */
495 #endif /* ! HAVE_LSEEK64 */
496  n = read (response->fd,
497  buf,
498  max);
499 
500 #endif /* ! HAVE_PREAD */
501  if (0 == n)
503  if (n < 0)
505  return n;
506 #else /* _WIN32 && !__CYGWIN__ */
507  if (INVALID_HANDLE_VALUE == fh)
508  return MHD_CONTENT_READER_END_WITH_ERROR; /* Value of 'response->fd' is not valid. */
509  else
510  {
511  OVERLAPPED f_ol = {0, 0, {{0, 0}}, 0}; /* Initialize to zero. */
512  ULARGE_INTEGER pos_uli;
513  DWORD toRead = (max > INT32_MAX) ? INT32_MAX : (DWORD) max;
514  DWORD resRead;
515 
516  pos_uli.QuadPart = (uint64_t) offset64; /* Simple transformation 64bit -> 2x32bit. */
517  f_ol.Offset = pos_uli.LowPart;
518  f_ol.OffsetHigh = pos_uli.HighPart;
519  if (! ReadFile (fh, (void*) buf, toRead, &resRead, &f_ol))
520  return MHD_CONTENT_READER_END_WITH_ERROR; /* Read error. */
521  if (0 == resRead)
523  return (ssize_t) resRead;
524  }
525 #endif /* _WIN32 && !__CYGWIN__ */
526 }
527 
528 
535 static void
536 free_callback (void *cls)
537 {
538  struct MHD_Response *response = cls;
539 
540  (void) close (response->fd);
541  response->fd = -1;
542 }
543 
544 #undef MHD_create_response_from_fd_at_offset
545 
562 struct MHD_Response *
564  int fd,
565  off_t offset)
566 {
568  fd,
569  offset);
570 }
571 
572 
589 _MHD_EXTERN struct MHD_Response *
591  int fd,
592  uint64_t offset)
593 {
594  struct MHD_Response *response;
595 
596 #if ! defined(HAVE___LSEEKI64) && ! defined(HAVE_LSEEK64)
597  if ( (sizeof(uint64_t) > sizeof(off_t)) &&
598  ( (size > (uint64_t) INT32_MAX) ||
599  (offset > (uint64_t) INT32_MAX) ||
600  ((size + offset) >= (uint64_t) INT32_MAX) ) )
601  return NULL;
602 #endif
603  if ( ((int64_t) size < 0) ||
604  ((int64_t) offset < 0) ||
605  ((int64_t) (size + offset) < 0) )
606  return NULL;
607 
608  response = MHD_create_response_from_callback (size,
610  &file_reader,
611  NULL,
612  &free_callback);
613  if (NULL == response)
614  return NULL;
615  response->fd = fd;
616  response->fd_off = offset;
617  response->crc_cls = response;
618  return response;
619 }
620 
621 
631 struct MHD_Response *
633  int fd)
634 {
636  fd,
637  0);
638 }
639 
640 
654 _MHD_EXTERN struct MHD_Response *
656  int fd)
657 {
659  fd,
660  0);
661 }
662 
663 
678 struct MHD_Response *
680  void *data,
681  int must_free,
682  int must_copy)
683 {
684  struct MHD_Response *response;
685  void *tmp;
686 
687  if ((NULL == data) && (size > 0))
688  return NULL;
689  if (NULL == (response = MHD_calloc_ (1, sizeof (struct MHD_Response))))
690  return NULL;
691  response->fd = -1;
692 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
693  if (! MHD_mutex_init_ (&response->mutex))
694  {
695  free (response);
696  return NULL;
697  }
698 #endif
699  if ((must_copy) && (size > 0))
700  {
701  if (NULL == (tmp = malloc (size)))
702  {
703 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
704  MHD_mutex_destroy_chk_ (&response->mutex);
705 #endif
706  free (response);
707  return NULL;
708  }
709  memcpy (tmp, data, size);
710  must_free = MHD_YES;
711  data = tmp;
712  }
713  if (must_free)
714  {
715  response->crfc = &free;
716  response->crc_cls = data;
717  }
718  response->reference_count = 1;
719  response->total_size = size;
720  response->data = data;
721  response->data_size = size;
722  return response;
723 }
724 
725 
736 struct MHD_Response *
738  void *buffer,
739  enum MHD_ResponseMemoryMode mode)
740 {
741  return MHD_create_response_from_data (size,
742  buffer,
743  mode == MHD_RESPMEM_MUST_FREE,
744  mode == MHD_RESPMEM_MUST_COPY);
745 }
746 
747 
758 _MHD_EXTERN struct MHD_Response *
760  void *buffer,
762  crfc)
763 {
764  struct MHD_Response *r;
765 
767  buffer,
768  MHD_YES,
769  MHD_NO);
770  if (NULL == r)
771  return r;
772  r->crfc = crfc;
773  return r;
774 }
775 
776 
777 #ifdef UPGRADE_SUPPORT
778 
790 _MHD_EXTERN int
791 MHD_upgrade_action (struct MHD_UpgradeResponseHandle *urh,
793  ...)
794 {
795  struct MHD_Connection *connection;
796  struct MHD_Daemon *daemon;
797 
798  if (NULL == urh)
799  return MHD_NO;
800  connection = urh->connection;
801 
802  /* Precaution checks on external data. */
803  if (NULL == connection)
804  return MHD_NO;
805  daemon = connection->daemon;
806  if (NULL == daemon)
807  return MHD_NO;
808 
809  switch (action)
810  {
812  if (urh->was_closed)
813  return MHD_NO; /* Already closed. */
814 
815  /* transition to special 'closed' state for start of cleanup */
816 #ifdef HTTPS_SUPPORT
817  if (0 != (daemon->options & MHD_USE_TLS) )
818  {
819  /* signal that app is done by shutdown() of 'app' socket */
820  /* Application will not use anyway this socket after this command. */
821  shutdown (urh->app.socket,
822  SHUT_RDWR);
823  }
824 #endif /* HTTPS_SUPPORT */
825  mhd_assert (MHD_CONNECTION_UPGRADE == connection->state);
826  urh->was_closed = true;
827  /* As soon as connection will be marked with BOTH
828  * 'urh->was_closed' AND 'urh->clean_ready', it will
829  * be moved to cleanup list by MHD_resume_connection(). */
830  MHD_resume_connection (connection);
831  return MHD_YES;
833  if (connection->sk_cork_on)
834  return MHD_YES;
835 #ifdef HTTPS_SUPPORT
836  if (0 != (daemon->options & MHD_USE_TLS) )
837  {
838  gnutls_record_cork (connection->tls_session);
839  connection->sk_cork_on = true;
840  return MHD_YES;
841  }
842  else
843 #endif
844  {
845  if (0 ==
846  MHD_socket_cork_ (connection->socket_fd,
847  true))
848  {
849  connection->sk_cork_on = true;
850  return MHD_YES;
851  }
852  return MHD_NO;
853  }
855  if (! connection->sk_cork_on)
856  return MHD_YES;
857 #ifdef HTTPS_SUPPORT
858  if (0 != (daemon->options & MHD_USE_TLS) )
859  {
860  gnutls_record_uncork (connection->tls_session, 0);
861  connection->sk_cork_on = false;
862  return MHD_YES;
863  }
864  else
865 #endif
866  {
867  if (0 ==
868  MHD_socket_cork_ (connection->socket_fd,
869  false))
870  {
871  connection->sk_cork_on = false;
872  return MHD_YES;
873  }
874  return MHD_NO;
875  }
876  default:
877  /* we don't understand this one */
878  return MHD_NO;
879  }
880 }
881 
882 
896 int
898  struct MHD_Connection *connection)
899 {
900  struct MHD_Daemon *daemon = connection->daemon;
901  struct MHD_UpgradeResponseHandle *urh;
902  size_t rbo;
903 
904  if (0 == (daemon->options & MHD_ALLOW_UPGRADE))
905  return MHD_NO;
906 
907  if (NULL ==
908  MHD_get_response_header (response,
910  {
911 #ifdef HAVE_MESSAGES
912  MHD_DLOG (daemon,
913  _ (
914  "Invalid response for upgrade: application failed to set the 'Upgrade' header!\n"));
915 #endif
916  return MHD_NO;
917  }
918 
919  urh = MHD_calloc_ (1, sizeof (struct MHD_UpgradeResponseHandle));
920  if (NULL == urh)
921  return MHD_NO;
922  urh->connection = connection;
923  rbo = connection->read_buffer_offset;
924  connection->read_buffer_offset = 0;
925 #ifdef HTTPS_SUPPORT
926  if (0 != (daemon->options & MHD_USE_TLS) )
927  {
928  struct MemoryPool *pool;
929  size_t avail;
930  char *buf;
931  MHD_socket sv[2];
932 #if defined(MHD_socket_nosignal_) || ! defined(MHD_socket_pair_nblk_)
933  int res1;
934  int res2;
935 #endif /* MHD_socket_nosignal_ || !MHD_socket_pair_nblk_ */
936 
937 #ifdef MHD_socket_pair_nblk_
938  if (! MHD_socket_pair_nblk_ (sv))
939  {
940  free (urh);
941  return MHD_NO;
942  }
943 #else /* !MHD_socket_pair_nblk_ */
944  if (! MHD_socket_pair_ (sv))
945  {
946  free (urh);
947  return MHD_NO;
948  }
949  res1 = MHD_socket_nonblocking_ (sv[0]);
950  res2 = MHD_socket_nonblocking_ (sv[1]);
951  if ( (! res1) || (! res2) )
952  {
953 #ifdef HAVE_MESSAGES
954  MHD_DLOG (daemon,
955  _ ("Failed to make loopback sockets non-blocking.\n"));
956 #endif
957  if (! res2)
958  {
959  /* Socketpair cannot be used. */
960  MHD_socket_close_chk_ (sv[0]);
961  MHD_socket_close_chk_ (sv[1]);
962  free (urh);
963  return MHD_NO;
964  }
965  }
966 #endif /* !MHD_socket_pair_nblk_ */
967 #ifdef MHD_socket_nosignal_
968  res1 = MHD_socket_nosignal_ (sv[0]);
969  res2 = MHD_socket_nosignal_ (sv[1]);
970  if ( (! res1) || (! res2) )
971  {
972 #ifdef HAVE_MESSAGES
973  MHD_DLOG (daemon,
974  _ ("Failed to set SO_NOSIGPIPE on loopback sockets.\n"));
975 #endif
976 #ifndef MSG_NOSIGNAL
977  if (! res2)
978  {
979  /* Socketpair cannot be used. */
980  MHD_socket_close_chk_ (sv[0]);
981  MHD_socket_close_chk_ (sv[1]);
982  free (urh);
983  return MHD_NO;
984  }
985 #endif /* ! MSG_NOSIGNAL */
986  }
987 #endif /* MHD_socket_nosignal_ */
988  if ( (! MHD_SCKT_FD_FITS_FDSET_ (sv[1],
989  NULL)) &&
990  (0 == (daemon->options & (MHD_USE_POLL | MHD_USE_EPOLL))) )
991  {
992 #ifdef HAVE_MESSAGES
993  MHD_DLOG (daemon,
994  _ ("Socketpair descriptor larger than FD_SETSIZE: %d > %d\n"),
995  (int) sv[1],
996  (int) FD_SETSIZE);
997 #endif
998  MHD_socket_close_chk_ (sv[0]);
999  MHD_socket_close_chk_ (sv[1]);
1000  free (urh);
1001  return MHD_NO;
1002  }
1003  urh->app.socket = sv[0];
1004  urh->app.urh = urh;
1005  urh->app.celi = MHD_EPOLL_STATE_UNREADY;
1006  urh->mhd.socket = sv[1];
1007  urh->mhd.urh = urh;
1008  urh->mhd.celi = MHD_EPOLL_STATE_UNREADY;
1009  pool = connection->pool;
1010  avail = MHD_pool_get_free (pool);
1011  if (avail < RESERVE_EBUF_SIZE)
1012  {
1013  /* connection's pool is totally at the limit,
1014  use our 'emergency' buffer of #RESERVE_EBUF_SIZE bytes. */
1015  avail = RESERVE_EBUF_SIZE;
1016  buf = urh->e_buf;
1017  }
1018  else
1019  {
1020  /* Normal case: grab all remaining memory from the
1021  connection's pool for the IO buffers; the connection
1022  certainly won't need it anymore as we've upgraded
1023  to another protocol. */
1024  buf = MHD_pool_allocate (pool,
1025  avail,
1026  false);
1027  }
1028  /* use half the buffer for inbound, half for outbound */
1029  urh->in_buffer_size = avail / 2;
1030  urh->out_buffer_size = avail - urh->in_buffer_size;
1031  urh->in_buffer = buf;
1032  urh->out_buffer = &buf[urh->in_buffer_size];
1033 #ifdef EPOLL_SUPPORT
1034  /* Launch IO processing by the event loop */
1035  if (0 != (daemon->options & MHD_USE_EPOLL))
1036  {
1037  /* We're running with epoll(), need to add the sockets
1038  to the event set of the daemon's `epoll_upgrade_fd` */
1039  struct epoll_event event;
1040 
1041  mhd_assert (-1 != daemon->epoll_upgrade_fd);
1042  /* First, add network socket */
1043  event.events = EPOLLIN | EPOLLOUT | EPOLLPRI | EPOLLET;
1044  event.data.ptr = &urh->app;
1045  if (0 != epoll_ctl (daemon->epoll_upgrade_fd,
1046  EPOLL_CTL_ADD,
1047  connection->socket_fd,
1048  &event))
1049  {
1050 #ifdef HAVE_MESSAGES
1051  MHD_DLOG (daemon,
1052  _ ("Call to epoll_ctl failed: %s\n"),
1054 #endif
1055  MHD_socket_close_chk_ (sv[0]);
1056  MHD_socket_close_chk_ (sv[1]);
1057  free (urh);
1058  return MHD_NO;
1059  }
1060 
1061  /* Second, add our end of the UNIX socketpair() */
1062  event.events = EPOLLIN | EPOLLOUT | EPOLLPRI | EPOLLET;
1063  event.data.ptr = &urh->mhd;
1064  if (0 != epoll_ctl (daemon->epoll_upgrade_fd,
1065  EPOLL_CTL_ADD,
1066  urh->mhd.socket,
1067  &event))
1068  {
1069  event.events = EPOLLIN | EPOLLOUT | EPOLLPRI;
1070  event.data.ptr = &urh->app;
1071  if (0 != epoll_ctl (daemon->epoll_upgrade_fd,
1072  EPOLL_CTL_DEL,
1073  connection->socket_fd,
1074  &event))
1075  MHD_PANIC (_ ("Error cleaning up while handling epoll error"));
1076 #ifdef HAVE_MESSAGES
1077  MHD_DLOG (daemon,
1078  _ ("Call to epoll_ctl failed: %s\n"),
1080 #endif
1081  MHD_socket_close_chk_ (sv[0]);
1082  MHD_socket_close_chk_ (sv[1]);
1083  free (urh);
1084  return MHD_NO;
1085  }
1086  EDLL_insert (daemon->eready_urh_head,
1087  daemon->eready_urh_tail,
1088  urh);
1089  urh->in_eready_list = true;
1090  }
1091 #endif /* EPOLL_SUPPORT */
1092  if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION) )
1093  {
1094  /* This takes care of further processing for most event loops:
1095  simply add to DLL for bi-direcitonal processing */
1096  DLL_insert (daemon->urh_head,
1097  daemon->urh_tail,
1098  urh);
1099  }
1100  /* In thread-per-connection mode, thread will switch to forwarding once
1101  * connection.urh is not NULL and connection.state == MHD_CONNECTION_UPGRADE.
1102  */
1103  }
1104  else
1105  {
1106  urh->app.socket = MHD_INVALID_SOCKET;
1107  urh->mhd.socket = MHD_INVALID_SOCKET;
1108  /* Non-TLS connection do not hold any additional resources. */
1109  urh->clean_ready = true;
1110  }
1111 #else /* ! HTTPS_SUPPORT */
1112  urh->clean_ready = true;
1113 #endif /* ! HTTPS_SUPPORT */
1114  connection->urh = urh;
1115  /* As far as MHD's event loops are concerned, this connection is
1116  suspended; it will be resumed once application is done by the
1117  #MHD_upgrade_action() function */
1118  internal_suspend_connection_ (connection);
1119 
1120  /* hand over socket to application */
1121  response->upgrade_handler (response->upgrade_handler_cls,
1122  connection,
1123  connection->client_context,
1124  connection->read_buffer,
1125  rbo,
1126 #ifdef HTTPS_SUPPORT
1127  (0 == (daemon->options & MHD_USE_TLS) ) ?
1128  connection->socket_fd : urh->app.socket,
1129 #else /* ! HTTPS_SUPPORT */
1130  connection->socket_fd,
1131 #endif /* ! HTTPS_SUPPORT */
1132  urh);
1133  return MHD_YES;
1134 }
1135 
1136 
1166 _MHD_EXTERN struct MHD_Response *
1168  void *upgrade_handler_cls)
1169 {
1170  struct MHD_Response *response;
1171 
1172  if (NULL == upgrade_handler)
1173  return NULL; /* invalid request */
1174  response = MHD_calloc_ (1, sizeof (struct MHD_Response));
1175  if (NULL == response)
1176  return NULL;
1177 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
1178  if (! MHD_mutex_init_ (&response->mutex))
1179  {
1180  free (response);
1181  return NULL;
1182  }
1183 #endif
1184  response->upgrade_handler = upgrade_handler;
1185  response->upgrade_handler_cls = upgrade_handler_cls;
1186  response->total_size = MHD_SIZE_UNKNOWN;
1187  response->reference_count = 1;
1188  if (MHD_NO ==
1189  MHD_add_response_header (response,
1191  "Upgrade"))
1192  {
1193  MHD_destroy_response (response);
1194  return NULL;
1195  }
1196  return response;
1197 }
1198 #endif /* UPGRADE_SUPPORT */
1199 
1200 
1210 void
1212 {
1213  struct MHD_HTTP_Header *pos;
1214 
1215  if (NULL == response)
1216  return;
1217 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
1218  MHD_mutex_lock_chk_ (&response->mutex);
1219 #endif
1220  if (0 != --(response->reference_count))
1221  {
1222 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
1223  MHD_mutex_unlock_chk_ (&response->mutex);
1224 #endif
1225  return;
1226  }
1227 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
1228  MHD_mutex_unlock_chk_ (&response->mutex);
1229  MHD_mutex_destroy_chk_ (&response->mutex);
1230 #endif
1231  if (NULL != response->crfc)
1232  response->crfc (response->crc_cls);
1233  while (NULL != response->first_header)
1234  {
1235  pos = response->first_header;
1236  response->first_header = pos->next;
1237  free (pos->header);
1238  free (pos->value);
1239  free (pos);
1240  }
1241  free (response);
1242 }
1243 
1244 
1250 void
1252 {
1253 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
1254  MHD_mutex_lock_chk_ (&response->mutex);
1255 #endif
1256  (response->reference_count)++;
1257 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
1258  MHD_mutex_unlock_chk_ (&response->mutex);
1259 #endif
1260 }
1261 
1262 
1263 /* end of response.c */
#define SSIZE_MAX
Definition: mhd_limits.h:111
#define MHD_FILE_READ_BLOCK_SIZE
Definition: response.c:67
int(* MHD_KeyValueIterator)(void *cls, enum MHD_ValueKind kind, const char *key, const char *value)
Definition: microhttpd.h:2237
#define MHD_PANIC(msg)
Definition: internal.h:69
uint64_t total_size
Definition: internal.h:1642
Header for platform missing functions.
additional automatic macros for MHD_config.h
_MHD_EXTERN struct MHD_Response * MHD_create_response_from_callback(uint64_t size, size_t block_size, MHD_ContentReaderCallback crc, void *crc_cls, MHD_ContentReaderFreeCallback crfc)
Definition: response.c:375
uint64_t fd_off
Definition: internal.h:1653
enum MHD_CONNECTION_STATE state
Definition: internal.h:922
void * data
Definition: microhttpd.h:3031
_MHD_EXTERN int MHD_add_response_header(struct MHD_Response *response, const char *header, const char *content)
Definition: response.c:133
void int int must_copy
Definition: microhttpd.h:3031
int MHD_set_response_options(struct MHD_Response *response, enum MHD_ResponseFlags flags,...)
Definition: response.c:416
#define MHD_mutex_unlock_chk_(pmutex)
Definition: mhd_locks.h:180
size_t MHD_pool_get_free(struct MemoryPool *pool)
Definition: memorypool.c:185
MHD_ContentReaderFreeCallback crfc
Definition: internal.h:1606
Methods for managing connections.
void(* MHD_ContentReaderFreeCallback)(void *cls)
Definition: microhttpd.h:2331
#define MHD_mutex_destroy_chk_(pmutex)
Definition: mhd_locks.h:121
void * MHD_calloc_(size_t nelem, size_t elsize)
Definition: mhd_compat.c:96
#define EDLL_insert(head, tail, element)
Definition: internal.h:1832
#define MHD_YES
Definition: microhttpd.h:140
char * data
Definition: internal.h:1588
_MHD_EXTERN struct MHD_Response * MHD_create_response_from_fd64(uint64_t size, int fd)
Definition: response.c:655
bool MHD_str_has_token_caseless_(const char *str, const char *const token, size_t token_len)
Definition: mhd_str.c:397
size_t data_size
Definition: internal.h:1659
int MHD_str_equal_caseless_(const char *str1, const char *str2)
Definition: mhd_str.c:333
char * value
Definition: internal.h:352
enum MHD_ValueKind kind
Definition: internal.h:358
#define MHD_HTTP_HEADER_UPGRADE
Definition: microhttpd.h:624
int MHD_socket
Definition: microhttpd.h:187
int MHD_socket_cork_(MHD_socket sock, bool on)
Definition: mhd_sockets.c:504
Methods for managing response objects.
char * header
Definition: internal.h:347
struct MHD_Daemon * daemon
Definition: internal.h:675
int MHD_response_execute_upgrade_(struct MHD_Response *response, struct MHD_Connection *connection)
Header for platform-independent inter-thread communication.
MHD_ResponseOptions
Definition: microhttpd.h:2967
#define DLL_insert(head, tail, element)
Definition: internal.h:1745
void MHD_increment_response_rc(struct MHD_Response *response)
Definition: response.c:1251
struct MHD_HTTP_Header * first_header
Definition: internal.h:1582
#define MHD_socket_last_strerr_()
Definition: mhd_sockets.h:548
size_t data_buffer_size
Definition: internal.h:1664
void * crc_cls
Definition: internal.h:1594
void * MHD_pool_allocate(struct MemoryPool *pool, size_t size, int from_end)
Definition: memorypool.c:203
int fd
Definition: microhttpd.h:3161
void * client_context
Definition: internal.h:696
#define MHD_INVALID_SOCKET
Definition: microhttpd.h:188
MHD_socket socket_fd
Definition: internal.h:752
internal shared structures
_MHD_EXTERN int MHD_add_response_footer(struct MHD_Response *response, const char *footer, const char *content)
Definition: response.c:177
_MHD_EXTERN struct MHD_Response * MHD_create_response_from_fd_at_offset64(uint64_t size, int fd, uint64_t offset)
Definition: response.c:590
enum MHD_FLAG options
Definition: internal.h:1603
#define MHD_socket_close_chk_(fd)
Definition: mhd_sockets.h:248
ssize_t(* MHD_ContentReaderCallback)(void *cls, uint64_t pos, char *buf, size_t max)
Definition: microhttpd.h:2315
unsigned int reference_count
Definition: internal.h:1675
_MHD_EXTERN void MHD_destroy_response(struct MHD_Response *response)
Definition: response.c:1211
struct MHD_Action action
Definition: internal.h:1575
#define _MHD_EXTERN
Definition: mhd_options.h:51
void int must_free
Definition: microhttpd.h:3031
#define MHD_CONTENT_READER_END_OF_STREAM
Definition: microhttpd.h:166
#define NULL
Definition: reason_phrase.c:30
#define INT32_MAX
Definition: mhd_limits.h:65
bool sk_cork_on
Definition: internal.h:883
struct MHD_Response * MHD_create_response_from_data(size_t size, void *data, int must_free, int must_copy)
Definition: response.c:679
MHD_ValueKind
Definition: microhttpd.h:1757
Header for string manipulating helpers.
char * read_buffer
Definition: internal.h:736
size_t value_size
Definition: internal.h:289
_MHD_EXTERN struct MHD_Response * MHD_create_response_from_fd(size_t size, int fd)
Definition: response.c:632
static ssize_t file_reader(void *cls, uint64_t pos, char *buf, size_t max)
Definition: response.c:452
_MHD_EXTERN int MHD_upgrade_action(struct MHD_UpgradeResponseHandle *urh, enum MHD_UpgradeAction action,...)
int off_t offset
Definition: microhttpd.h:3161
void internal_suspend_connection_(struct MHD_Connection *connection)
Definition: daemon.c:2795
size_t header_size
Definition: internal.h:279
_MHD_EXTERN struct MHD_Response * MHD_create_response_from_buffer(size_t size, void *buffer, enum MHD_ResponseMemoryMode mode)
Definition: response.c:737
#define mhd_assert(CHK)
Definition: mhd_assert.h:39
enum MHD_ResponseFlags flags
Definition: internal.h:400
struct MHD_HTTP_Header * next
Definition: internal.h:342
_MHD_EXTERN struct MHD_Response * MHD_create_response_for_upgrade(MHD_UpgradeHandler upgrade_handler, void *upgrade_handler_cls)
#define MHD_mutex_lock_chk_(pmutex)
Definition: mhd_locks.h:154
void(* MHD_UpgradeHandler)(void *cls, struct MHD_Connection *connection, void *con_cls, const char *extra_in, size_t extra_in_size, MHD_socket sock, struct MHD_UpgradeResponseHandle *urh)
Definition: microhttpd.h:3299
#define MHD_SCKT_FD_FITS_FDSET_(fd, pset)
Definition: mhd_sockets.h:309
#define MHD_HTTP_HEADER_CONNECTION
Definition: microhttpd.h:560
struct MemoryPool * pool
Definition: internal.h:685
int MHD_socket_nonblocking_(MHD_socket sock)
Definition: mhd_sockets.c:405
#define MHD_CONTENT_READER_END_WITH_ERROR
Definition: microhttpd.h:167
MHD_ContentReaderCallback crc
Definition: internal.h:1600
MHD_UpgradeAction
Definition: microhttpd.h:3202
MHD_mutex_ mutex
Definition: internal.h:1637
_MHD_EXTERN struct MHD_Response * MHD_create_response_from_buffer_with_free_callback(size_t size, void *buffer, MHD_ContentReaderFreeCallback crfc)
Definition: response.c:759
#define _(String)
Definition: mhd_options.h:42
static int add_response_entry(struct MHD_Response *response, enum MHD_ValueKind kind, const char *header, const char *content)
Definition: response.c:82
#define MHD_HTTP_HEADER_CONTENT_LENGTH
Definition: microhttpd.h:566
#define MHD_SIZE_UNKNOWN
Definition: microhttpd.h:159
_MHD_EXTERN int MHD_del_response_header(struct MHD_Response *response, const char *header, const char *content)
Definition: response.c:198
size_t read_buffer_offset
Definition: internal.h:787
#define MHD_NO
Definition: microhttpd.h:145
volatile bool shutdown
Definition: internal.h:1526
MHD_ResponseFlags
Definition: microhttpd.h:2929
_MHD_EXTERN const char * MHD_get_response_header(struct MHD_Response *response, const char *key)
Definition: response.c:284
limits values definitions
#define MHD_HTTP_HEADER_TRANSFER_ENCODING
Definition: microhttpd.h:622
struct MHD_Response * MHD_create_response_from_fd_at_offset(size_t size, int fd, off_t offset)
Definition: response.c:563
bool MHD_check_response_header_token_ci(const struct MHD_Response *response, const char *key, size_t key_len, const char *token, size_t token_len)
Definition: response.c:323
_MHD_EXTERN int MHD_get_response_headers(struct MHD_Response *response, MHD_KeyValueIterator iterator, void *iterator_cls)
Definition: response.c:252
MHD_ResponseMemoryMode
Definition: microhttpd.h:3041
bool MHD_str_equal_caseless_bin_n_(const char *const str1, const char *const str2, size_t len)
Definition: mhd_str.c:393
_MHD_EXTERN void MHD_resume_connection(struct MHD_Connection *connection)
Definition: daemon.c:2921
static void free_callback(void *cls)
Definition: response.c:536