ops-cgi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include <private-lib-core.h>
  25. static int
  26. rops_handle_POLLIN_cgi(struct lws_context_per_thread *pt, struct lws *wsi,
  27. struct lws_pollfd *pollfd)
  28. {
  29. struct lws_cgi_args args;
  30. assert(wsi->role_ops == &role_ops_cgi);
  31. if (wsi->lsp_channel >= LWS_STDOUT &&
  32. !(pollfd->revents & pollfd->events & LWS_POLLIN))
  33. return LWS_HPI_RET_HANDLED;
  34. if (wsi->lsp_channel == LWS_STDIN &&
  35. !(pollfd->revents & pollfd->events & LWS_POLLOUT))
  36. return LWS_HPI_RET_HANDLED;
  37. if (wsi->lsp_channel == LWS_STDIN &&
  38. lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
  39. lwsl_info("failed at set pollfd\n");
  40. return LWS_HPI_RET_WSI_ALREADY_DIED;
  41. }
  42. if (!wsi->parent) {
  43. lwsl_notice("%s: stdwsi content with parent\n",
  44. __func__);
  45. return LWS_HPI_RET_HANDLED;
  46. }
  47. if (!wsi->parent->http.cgi) {
  48. lwsl_notice("%s: stdwsi content with deleted cgi object\n",
  49. __func__);
  50. return LWS_HPI_RET_HANDLED;
  51. }
  52. if (!wsi->parent->http.cgi->lsp) {
  53. lwsl_notice("%s: stdwsi content with reaped lsp\n",
  54. __func__);
  55. return LWS_HPI_RET_HANDLED;
  56. }
  57. args.ch = wsi->lsp_channel;
  58. args.stdwsi = &wsi->parent->http.cgi->lsp->stdwsi[0];
  59. args.hdr_state = wsi->hdr_state;
  60. lwsl_debug("CGI LWS_STDOUT %p wsistate 0x%x\n",
  61. wsi->parent, wsi->wsistate);
  62. if (user_callback_handle_rxflow(wsi->parent->a.protocol->callback,
  63. wsi->parent, LWS_CALLBACK_CGI,
  64. wsi->parent->user_space,
  65. (void *)&args, 0))
  66. return 1;
  67. return LWS_HPI_RET_HANDLED;
  68. }
  69. static int
  70. rops_handle_POLLOUT_cgi(struct lws *wsi)
  71. {
  72. return LWS_HP_RET_USER_SERVICE;
  73. }
  74. static int
  75. rops_destroy_role_cgi(struct lws *wsi)
  76. {
  77. #if defined(LWS_WITH_ZLIB)
  78. if (!wsi->http.cgi)
  79. return 0;
  80. if (!wsi->http.cgi->gzip_init)
  81. return 0;
  82. inflateEnd(&wsi->http.cgi->inflate);
  83. wsi->http.cgi->gzip_init = 0;
  84. #endif
  85. return 0;
  86. }
  87. static void
  88. lws_cgi_sul_cb(lws_sorted_usec_list_t *sul)
  89. {
  90. struct lws_context_per_thread *pt = lws_container_of(sul,
  91. struct lws_context_per_thread, sul_cgi);
  92. lws_cgi_kill_terminated(pt);
  93. __lws_sul_insert_us(&pt->pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
  94. &pt->sul_cgi, 3 * LWS_US_PER_SEC);
  95. }
  96. static int
  97. rops_pt_init_destroy_cgi(struct lws_context *context,
  98. const struct lws_context_creation_info *info,
  99. struct lws_context_per_thread *pt, int destroy)
  100. {
  101. if (!destroy) {
  102. pt->sul_cgi.cb = lws_cgi_sul_cb;
  103. __lws_sul_insert_us(&pt->pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
  104. &pt->sul_cgi, 3 * LWS_US_PER_SEC);
  105. } else
  106. lws_dll2_remove(&pt->sul_cgi.list);
  107. return 0;
  108. }
  109. static int
  110. rops_close_role_cgi(struct lws_context_per_thread *pt, struct lws *wsi)
  111. {
  112. if (wsi->parent && wsi->parent->http.cgi && wsi->parent->http.cgi->lsp)
  113. lws_spawn_stdwsi_closed(wsi->parent->http.cgi->lsp, wsi);
  114. return 0;
  115. }
  116. const struct lws_role_ops role_ops_cgi = {
  117. /* role name */ "cgi",
  118. /* alpn id */ NULL,
  119. /* check_upgrades */ NULL,
  120. /* pt_init_destroy */ rops_pt_init_destroy_cgi,
  121. /* init_vhost */ NULL,
  122. /* destroy_vhost */ NULL,
  123. /* service_flag_pending */ NULL,
  124. /* handle_POLLIN */ rops_handle_POLLIN_cgi,
  125. /* handle_POLLOUT */ rops_handle_POLLOUT_cgi,
  126. /* perform_user_POLLOUT */ NULL,
  127. /* callback_on_writable */ NULL,
  128. /* tx_credit */ NULL,
  129. /* write_role_protocol */ NULL,
  130. /* encapsulation_parent */ NULL,
  131. /* alpn_negotiated */ NULL,
  132. /* close_via_role_protocol */ NULL,
  133. /* close_role */ rops_close_role_cgi,
  134. /* close_kill_connection */ NULL,
  135. /* destroy_role */ rops_destroy_role_cgi,
  136. /* adoption_bind */ NULL,
  137. /* client_bind */ NULL,
  138. /* issue_keepalive */ NULL,
  139. /* adoption_cb clnt, srv */ { 0, 0 },
  140. /* rx_cb clnt, srv */ { 0, 0 },
  141. /* writeable cb clnt, srv */ { 0, 0 },
  142. /* close cb clnt, srv */ { 0, 0 },
  143. /* protocol_bind_cb c,s */ { 0, 0 },
  144. /* protocol_unbind_cb c,s */ { 0, 0 },
  145. /* file_handle */ 0,
  146. };