Tests about a simple python3 fastcgi runner using libfcgi and the Python-C API.
python
c
wsgi
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

python_pyfcgi.h 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. PyIO_t *ioin;
  59. /**@brief libpyfcgi.IoOut instances for stdout & stderr */
  60. PyIO_t *stdio[2];
  61. /**@brief Indicate if headers was sent in a PEP333 application
  62. * @ingroup libpyfcgi */
  63. short headers_sent;
  64. /**@brief Stores the libFCGI stream for PEP333 application
  65. * @ingroup libpyfcgi */
  66. FCGX_Stream *out;
  67. /**@brief Stores the libFCGI stream from wich HTTP request can be read
  68. * @ingroup libpyfcgi */
  69. FCGX_Stream *in;
  70. /**@brief Persistent buffer (avoid malloc) for PEP333 headers */
  71. char *heads_buf;
  72. /**@brief Buffer size */
  73. size_t heads_buf_sz;
  74. /**@brief Persistent buffer for PEP333 status */
  75. char status_buf[LIBPYFCGI_STATUS_SZ];
  76. size_t rep_sz;
  77. };
  78. typedef struct libpyfcgi_context_s libpyfcgi_context_t;
  79. /**@brief Stores python module context */
  80. extern libpyfcgi_context_t libpyfcgi;
  81. /**@brief libpyfcgi methods */
  82. extern PyMethodDef pyfcgimodule_methods[];
  83. /**@brief libpyfcgi module structure */
  84. extern PyModuleDef pyfcgimodule;
  85. /**@brief Clean response_status & response_headers globals */
  86. static inline void libpyfcgi_clean_response()
  87. {
  88. if(libpyfcgi.status) { Py_DECREF(libpyfcgi.status); }
  89. libpyfcgi.status = NULL;
  90. if(libpyfcgi.headers) { Py_DECREF(libpyfcgi.headers); }
  91. libpyfcgi.headers = NULL;
  92. libpyfcgi.headers_sent = 0;
  93. libpyfcgi.rep_sz = 0;
  94. libpyfcgi.ioin->eof=0;
  95. libpyfcgi.ioin->closed=Py_False;
  96. }
  97. /**@brief Send headers stored in @ref libpyfcgi context
  98. * @note Set python error if called from outside a valid context
  99. */
  100. void libpyfcgi_send_headers();
  101. /**@brief Send body to fcgi
  102. * @param body_data the body data object (returned by PEP333 app)
  103. * @return Python None
  104. */
  105. PyObject* _pyfcgi_write_body(PyObject *body_data);
  106. /**@brief Called by the SIGALRM sighandler @ref worker_sigalrmhandler()
  107. *
  108. * If no headers sent send a timeout header.
  109. *
  110. * Attempt to generate a python exception and to log exception before
  111. * exiting.
  112. */
  113. void libpyfcgi_timeout();
  114. /* Defining Python module */
  115. /**@brief Public module initialisation function
  116. * @ingroup libpyfcgi
  117. * @return A python module (PyObject*)
  118. */
  119. PyMODINIT_FUNC PyInit_libpyfcgi(void);
  120. /**@brief libpyfcgi.start_response() python callable
  121. * @ingroup libpyfcgi
  122. * @note This python callable is a fastcall C method of libpyfcgi module.
  123. *
  124. * The python function header is :
  125. * start_response(status, response_headers, exc_info = None)
  126. * @param self
  127. * @param argv
  128. * @param argc
  129. * @return A PyObject* referencing a callable allowing to write data without
  130. * cache : libpyfcgi.write_body()
  131. */
  132. PyObject* pyfcgi_start_response(PyObject* self, PyObject** argv,
  133. Py_ssize_t argc);
  134. /**@brief libpyfcgi.write_body() python callable
  135. * @ingroup libpyfcgi
  136. * @note This python callable is a fastcall C method of libpyfcgi module.
  137. *
  138. * The python function header is : write_body(body_data)
  139. * @param self
  140. * @param argv
  141. * @param argc
  142. * @return ???
  143. */
  144. PyObject* pyfcgi_write_body(PyObject* self, PyObject** argv, Py_ssize_t argc);
  145. int _libpyfcgi_stdout_write(const char*, size_t);
  146. int _libpyfcgi_stderr_write(const char*, size_t);
  147. #endif