Tests about a simple python3 fastcgi runner using libfcgi and the Python-C API.
python
c
wsgi
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

python_pyfcgi.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (C) 2019 Weber Yann
  3. *
  4. * This file is part of PyFCGI.
  5. *
  6. * PyFCGI is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * any later version.
  10. *
  11. * PyFCGI 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
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PyFCGI. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**@defgroup libpyfcgi libpyfcgi python module
  20. * @brief The libpyfcgi python module
  21. *
  22. * PyFCGI defines a python module : libpyfcgi for PEP333 implementation.
  23. * This module contains 2 methods : start_response() and write_body()
  24. *
  25. * The first one is given as argument to the application function, and
  26. * the second one is returned when start_method() is called.
  27. */
  28. /**@file python_pyfcgi.h
  29. * @ingroup libpyfcgi
  30. */
  31. #ifndef _PYTHON_PYFCGI__H__
  32. #define _PYTHON_PYFCGI__H__
  33. #include "config.h"
  34. #include <fcgiapp.h>
  35. #include <fcgi_stdio.h> /* fcgi library; put it first*/
  36. #define PY_SSIZE_T_CLEAN
  37. #include <Python.h>
  38. #include "structmember.h"
  39. #include "logger.h"
  40. #include "python_ioin.h"
  41. #define LIBPYFCGI_TIMEOUT_HEADERS "Content-Type: text/plain\r\nStatus: 504 Gateway Timeout\r\n\r\n"
  42. #define LIBPYFCGI_DEFAULT_HEADERS "Content-Type: text/html\r\nStatus: %s\r\n\r\n"
  43. #define LIBPYFCGI_DEFAULT_STATUS "200 OK"
  44. #define LIBPYFCGI_DEFAULT_CTYPE "Content-Type: text/html\r\n"
  45. #define LIBPYFCGI_STATUS_SZ 128
  46. struct libpyfcgi_context_s
  47. {
  48. /**@brief PEP333 handling python module reference
  49. * @ingroup libpyfcgi */
  50. PyObject *self;
  51. /**@brief PEP333 status ref
  52. * @ingroup libpyfcgi */
  53. PyObject *status;
  54. /**@brief PEP333 headers ref
  55. * @ingroup libpyfcgi */
  56. PyObject *headers;
  57. /**@brief libpyfcgi.IoIn instance */
  58. IoIn *ioin;
  59. /**@brief Indicate if headers was sent in a PEP333 application
  60. * @ingroup libpyfcgi */
  61. short headers_sent;
  62. /**@brief Stores the libFCGI stream for PEP333 application
  63. * @ingroup libpyfcgi */
  64. FCGX_Stream *out;
  65. /**@brief Stores the libFCGI stream from wich HTTP request can be read
  66. * @ingroup libpyfcgi */
  67. FCGX_Stream *in;
  68. /**@brief Persistent buffer (avoid malloc) for PEP333 headers */
  69. char *heads_buf;
  70. /**@brief Buffer size */
  71. size_t heads_buf_sz;
  72. /**@brief Persistent buffer for PEP333 status */
  73. char status_buf[LIBPYFCGI_STATUS_SZ];
  74. size_t rep_sz;
  75. };
  76. typedef struct libpyfcgi_context_s libpyfcgi_context_t;
  77. /**@brief Stores python module context */
  78. extern libpyfcgi_context_t libpyfcgi;
  79. /**@brief libpyfcgi methods */
  80. extern PyMethodDef pyfcgimodule_methods[];
  81. /**@brief libpyfcgi module structure */
  82. extern PyModuleDef pyfcgimodule;
  83. /**@brief Clean response_status & response_headers globals */
  84. inline void libpyfcgi_clean_response()
  85. {
  86. if(libpyfcgi.status) { Py_DECREF(libpyfcgi.status); }
  87. libpyfcgi.status = NULL;
  88. if(libpyfcgi.headers) { Py_DECREF(libpyfcgi.headers); }
  89. libpyfcgi.headers = NULL;
  90. libpyfcgi.headers_sent = 0;
  91. libpyfcgi.rep_sz = 0;
  92. libpyfcgi.ioin->eof=0;
  93. libpyfcgi.ioin->closed=Py_False;
  94. }
  95. /**@brief Send headers stored in @ref libpyfcgi context
  96. * @note Set python error if called from outside a valid context
  97. */
  98. void libpyfcgi_send_headers();
  99. /**@brief Send body to fcgi
  100. * @param PyObject* the body data object (returned by PEP333 app)
  101. * @return Python None
  102. */
  103. PyObject* _pyfcgi_write_body(PyObject *body_data);
  104. /**@brief Called by the SIGALRM sighandler @ref worker_sigalrmhandler()
  105. *
  106. * If no headers sent send a timeout header.
  107. *
  108. * Attempt to generate a python exception and to log exception before
  109. * exiting.
  110. */
  111. void libpyfcgi_timeout();
  112. /* Defining Python module */
  113. /**@brief Public module initialisation function
  114. * @ingroup libpyfcgi
  115. * @return A python module (PyObject*)
  116. */
  117. PyMODINIT_FUNC PyInit_libpyfcgi(void);
  118. /**@brief libpyfcgi.start_response() python callable
  119. * @ingroup libpyfcgi
  120. * @note This python callable is a fastcall C method of libpyfcgi module.
  121. *
  122. * The python function header is : start_response(status, response_headers, exc_info = None)
  123. * @param PyObject* self
  124. * @param PyObject* argv
  125. * @param Py_ssize_t argc
  126. * @return A PyObject* referencing a callable allowing to write data without
  127. * cache : libpyfcgi.write_body()
  128. */
  129. PyObject* pyfcgi_start_response(PyObject*, PyObject**, Py_ssize_t);
  130. /**@brief libpyfcgi.write_body() python callable
  131. * @ingroup libpyfcgi
  132. * @note This python callable is a fastcall C method of libpyfcgi module.
  133. *
  134. * The python function header is : write_body(body_data)
  135. * @param PyObject* self
  136. * @param PyObject* argv
  137. * @param Py_ssize_t argc
  138. * @return ???
  139. */
  140. PyObject* pyfcgi_write_body(PyObject*, PyObject**, Py_ssize_t);
  141. #endif