138 lines
3.7 KiB
C
Raw Normal View History

2025-03-21 08:25:05 +01:00
#ifndef PGS_PY_H
#define PGS_PY_H
2024-12-31 06:44:19 +01:00
#define PY_SSIZE_T_CLEAN 1
#include "pgs_api.h"
2024-12-31 02:57:48 +01:00
#include <Python.h>
#include <stdbool.h>
PyObject *_pModule = NULL;
bool python_initialized = false;
PyObject *py_construct() {
2024-12-31 06:44:19 +01:00
PyStatus status;
PyConfig config;
2024-12-31 02:57:48 +01:00
if (!python_initialized) {
2024-12-31 06:44:19 +01:00
if (PyImport_AppendInittab("pgs", PyInit_mymodule) == -1) {
fprintf(stderr, "Failed to add mymodule to the interpreter's table\n");
return NULL;
}
PyConfig_InitPythonConfig(&config);
/* optional but recommended */
status = PyConfig_SetBytesString(&config, &config.program_name, "pgs");
if (PyStatus_Exception(status)) {
goto exception;
}
status = Py_InitializeFromConfig(&config);
if (PyStatus_Exception(status)) {
goto exception;
}
PyConfig_Clear(&config);
// Py_Initialize();
if (!Py_IsInitialized()) {
fprintf(stderr, "Python initialization failed!\n");
return NULL;
}
PyGILState_STATE gstate = PyGILState_Ensure();
PyRun_SimpleString("import pgs");
// Py_InitModule("pgscript", NULL);
2024-12-31 02:57:48 +01:00
python_initialized = true;
PyObject *sysPath = PySys_GetObject("path");
PyList_Append(sysPath, PyUnicode_FromString("."));
// Py_DECREF(sysPath);
PyObject *pName = PyUnicode_DecodeFSDefault("pgscript");
_pModule = PyImport_Import(pName);
Py_DECREF(pName);
2024-12-31 06:44:19 +01:00
PyGILState_Release(gstate);
2024-12-31 02:57:48 +01:00
python_initialized = true;
}
return _pModule;
2024-12-31 06:44:19 +01:00
exception:
PyConfig_Clear(&config);
Py_ExitStatusException(status);
return NULL;
2024-12-31 02:57:48 +01:00
}
void py_destruct() {
if (!python_initialized)
return;
Py_DECREF(_pModule);
Py_Finalize();
python_initialized = false;
}
2025-03-21 08:25:05 +01:00
char py_on_headers(int downstream, char *headers) {
2024-12-31 02:57:48 +01:00
PyObject *pModule = py_construct();
2025-03-21 08:25:05 +01:00
char *new_headers = NULL;
2024-12-31 02:57:48 +01:00
if (pModule != NULL) {
2025-03-21 08:25:05 +01:00
PyObject *pFunc = PyObject_GetAttrString(pModule, "on_headers");
2024-12-31 02:57:48 +01:00
if (PyCallable_Check(pFunc)) {
PyObject *pArgs = PyTuple_Pack(2, PyLong_FromLong(downstream),
2025-03-21 08:25:05 +01:00
PyUnicode_FromString(headers));
PyGILState_STATE gstate = PyGILState_Ensure();
new_headers = PyBytes_AsString(PyObject_CallObject(pFunc, pArgs));
PyGILState_Release(gstate);
Py_DECREF(pArgs);
}
}
return new_headers ? new_headers : headers;
}
char * py_http_intercept_headers(int sock, char * headers){
PyObject *pModule = py_construct();
char *new_headers = NULL;
if (pModule != NULL) {
PyObject *pFunc = PyObject_GetAttrString(pModule, "http_intercept_headers");
if (PyCallable_Check(pFunc)) {
PyObject *pArgs = PyTuple_Pack(2, PyLong_FromLong(sock),
PyUnicode_FromString(headers));
PyGILState_STATE gstate = PyGILState_Ensure();
new_headers = PyBytes_AsString(PyObject_CallObject(pFunc, pArgs));
PyGILState_Release(gstate);
Py_DECREF(pArgs);
}
}
return new_headers ? new_headers : headers;
}
int py_on_connect(int downstream) {
PyObject *pModule = py_construct();
int upstream_fd = -1;
if (pModule != NULL) {
PyObject *pFunc = PyObject_GetAttrString(pModule, "on_connect");
if (PyCallable_Check(pFunc)) {
PyObject *pArgs = PyTuple_Pack(1, PyLong_FromLong(downstream));
2024-12-31 06:44:19 +01:00
PyGILState_STATE gstate = PyGILState_Ensure();
2024-12-31 02:57:48 +01:00
PyObject *pValue = PyObject_CallObject(pFunc, pArgs);
2024-12-31 06:44:19 +01:00
PyGILState_Release(gstate);
2024-12-31 02:57:48 +01:00
Py_DECREF(pArgs);
if (pValue != NULL) {
upstream_fd = PyLong_AsLong(pValue);
Py_DECREF(pValue);
} else {
PyErr_Print();
fprintf(stderr, "Call failed\n");
}
} else {
PyErr_Print();
fprintf(stderr, "Cannot find function 'route'\n");
}
Py_XDECREF(pFunc);
} else {
PyErr_Print();
fprintf(stderr, "Failed to load 'script'\n");
}
2025-03-21 08:25:05 +01:00
return upstream_fd;
2024-12-31 02:57:48 +01:00
}
2025-03-21 08:25:05 +01:00
#endif