connection_finish_forward.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. This file is part of libmicrohttpd
  3. Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. /**
  17. * @file lib/connection_finish_forward.c
  18. * @brief complete upgrade socket forwarding operation in TLS mode
  19. * @author Christian Grothoff
  20. */
  21. #include "internal.h"
  22. #include "connection_finish_forward.h"
  23. #if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT)
  24. /**
  25. * Stop TLS forwarding on upgraded connection and
  26. * reflect remote disconnect state to socketpair.
  27. * @remark In thread-per-connection mode this function
  28. * can be called from any thread, in other modes this
  29. * function must be called only from thread that process
  30. * daemon's select()/poll()/etc.
  31. *
  32. * @param connection the upgraded connection
  33. */
  34. void
  35. MHD_connection_finish_forward_ (struct MHD_Connection *connection)
  36. {
  37. struct MHD_Daemon *daemon = connection->daemon;
  38. struct MHD_UpgradeResponseHandle *urh = connection->request.urh;
  39. if (NULL == daemon->tls_api)
  40. return; /* Nothing to do with non-TLS connection. */
  41. if (MHD_TM_THREAD_PER_CONNECTION != daemon->threading_mode)
  42. DLL_remove (daemon->urh_head,
  43. daemon->urh_tail,
  44. urh);
  45. #if EPOLL_SUPPORT
  46. if ( (MHD_ELS_EPOLL == daemon->event_loop_syscall) &&
  47. (0 != epoll_ctl (daemon->epoll_upgrade_fd,
  48. EPOLL_CTL_DEL,
  49. connection->socket_fd,
  50. NULL)) )
  51. {
  52. MHD_PANIC (_ ("Failed to remove FD from epoll set\n"));
  53. }
  54. if (urh->in_eready_list)
  55. {
  56. EDLL_remove (daemon->eready_urh_head,
  57. daemon->eready_urh_tail,
  58. urh);
  59. urh->in_eready_list = false;
  60. }
  61. #endif /* EPOLL_SUPPORT */
  62. if (MHD_INVALID_SOCKET != urh->mhd.socket)
  63. {
  64. #if EPOLL_SUPPORT
  65. if ( (MHD_ELS_EPOLL == daemon->event_loop_syscall) &&
  66. (0 != epoll_ctl (daemon->epoll_upgrade_fd,
  67. EPOLL_CTL_DEL,
  68. urh->mhd.socket,
  69. NULL)) )
  70. {
  71. MHD_PANIC (_ ("Failed to remove FD from epoll set\n"));
  72. }
  73. #endif /* EPOLL_SUPPORT */
  74. /* Reflect remote disconnect to application by breaking
  75. * socketpair connection. */
  76. shutdown (urh->mhd.socket,
  77. SHUT_RDWR);
  78. }
  79. /* Socketpair sockets will remain open as they will be
  80. * used with MHD_UPGRADE_ACTION_CLOSE. They will be
  81. * closed by MHD_cleanup_upgraded_connection_() during
  82. * connection's final cleanup.
  83. */}
  84. #endif /* HTTPS_SUPPORT && UPGRADE_SUPPORT*/
  85. /* end of connection_finish_forward.c */