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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #define LIBPYFCGI_DEFAULT_HEADERS "Content-Type: text/html\r\nStatus: %s\r\n\r\n"
  41. #define LIBPYFCGI_DEFAULT_STATUS "200 OK"
  42. #define LIBPYFCGI_DEFAULT_CTYPE "Content-Type: text/html\r\n"
  43. #define LIBPYFCGI_STATUS_SZ 128
  44. struct libpyfcgi_context_s
  45. {
  46. /**@brief PEP333 handling python module reference
  47. * @ingroup libpyfcgi */
  48. PyObject *self;
  49. /**@brief PEP333 status ref
  50. * @ingroup libpyfcgi */
  51. PyObject *status;
  52. /**@brief PEP333 headers ref
  53. * @ingroup libpyfcgi */
  54. PyObject *headers;
  55. /**@brief Indicate if headers was sent in a PEP333 application
  56. * @ingroup libpyfcgi */
  57. short headers_sent;
  58. /**@brief Stores the libFCGI stream for PEP333 application
  59. * @ingroup libpyfcgi */
  60. FCGX_Stream *out;
  61. /**@brief Persistent buffer (avoid malloc) for PEP333 headers */
  62. char *heads_buf;
  63. /**@brief Buffer size */
  64. size_t heads_buf_sz;
  65. /**@brief Persistent buffer for PEP333 status */
  66. char status_buf[LIBPYFCGI_STATUS_SZ];
  67. };
  68. typedef struct libpyfcgi_context_s libpyfcgi_context_t;
  69. /**@brief Stores python module context */
  70. extern libpyfcgi_context_t libpyfcgi;
  71. /**@brief libpyfcgi methods */
  72. extern PyMethodDef pyfcgimodule_methods[];
  73. /**@brief libpyfcgi module structure */
  74. extern PyModuleDef pyfcgimodule;
  75. /**@brief Clean response_status & response_headers globals */
  76. inline void libpyfcgi_clean_response()
  77. {
  78. if(libpyfcgi.status) { Py_DECREF(libpyfcgi.status); }
  79. libpyfcgi.status = NULL;
  80. if(libpyfcgi.headers) { Py_DECREF(libpyfcgi.headers); }
  81. libpyfcgi.headers = NULL;
  82. libpyfcgi.headers_sent = 0;
  83. }
  84. /**@brief Send headers stored in @ref libpyfcgi context
  85. * @note Set python error if called from outside a valid context
  86. */
  87. void libpyfcgi_send_headers();
  88. /**@brief Send body to fcgi
  89. * @param PyObject* the body data object (returned by PEP333 app)
  90. * @return Python None
  91. */
  92. PyObject* _pyfcgi_write_body(PyObject *body_data);
  93. /* Defining Python module */
  94. /**@brief Public module initialisation function
  95. * @ingroup libpyfcgi
  96. * @return A python module (PyObject*)
  97. */
  98. PyMODINIT_FUNC PyInit_libpyfcgi(void);
  99. /**@brief libpyfcgi.start_response() python callable
  100. * @ingroup libpyfcgi
  101. * @note This python callable is a fastcall C method of libpyfcgi module.
  102. *
  103. * The python function header is : start_response(status, response_headers, exc_info = None)
  104. * @param PyObject* self
  105. * @param PyObject* argv
  106. * @param Py_ssize_t argc
  107. * @return A PyObject* referencing a callable allowing to write data without
  108. * cache : libpyfcgi.write_body()
  109. */
  110. PyObject* pyfcgi_start_response(PyObject*, PyObject**, Py_ssize_t);
  111. /**@brief libpyfcgi.write_body() python callable
  112. * @ingroup libpyfcgi
  113. * @note This python callable is a fastcall C method of libpyfcgi module.
  114. *
  115. * The python function header is : write_body(body_data)
  116. * @param PyObject* self
  117. * @param PyObject* argv
  118. * @param Py_ssize_t argc
  119. * @return ???
  120. */
  121. PyObject* pyfcgi_write_body(PyObject*, PyObject**, Py_ssize_t);
  122. #endif