From 66e893f1d41a48c638a99c1e5b7148ab5dba37a2 Mon Sep 17 00:00:00 2001 From: Joris Vink Date: Sat, 4 Mar 2023 23:15:49 +0100 Subject: [PATCH] Python API domain improvement. Add redirect() method to add a redirect on a domain much like in the Kore configuration file. eg: domain.redirect("^/account/(.*)$", 301, "https://site/account/$1") --- include/kore/python_methods.h | 2 ++ src/python.c | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/include/kore/python_methods.h b/include/kore/python_methods.h index 4c7a1fd..0c06cff 100644 --- a/include/kore/python_methods.h +++ b/include/kore/python_methods.h @@ -231,10 +231,12 @@ struct pydomain { }; static PyObject *pydomain_filemaps(struct pydomain *, PyObject *); +static PyObject *pydomain_redirect(struct pydomain *, PyObject *); static PyObject *pydomain_route(struct pydomain *, PyObject *, PyObject *); static PyMethodDef pydomain_methods[] = { METHOD("filemaps", pydomain_filemaps, METH_VARARGS), + METHOD("redirect", pydomain_redirect, METH_VARARGS), METHOD("route", pydomain_route, METH_VARARGS | METH_KEYWORDS), METHOD(NULL, NULL, -1) }; diff --git a/src/python.c b/src/python.c index 312e410..ed00c3f 100644 --- a/src/python.c +++ b/src/python.c @@ -5616,6 +5616,23 @@ pydomain_filemaps(struct pydomain *domain, PyObject *args) Py_RETURN_NONE; } +static PyObject * +pydomain_redirect(struct pydomain *domain, PyObject *args) +{ + int status; + const char *src, *dst; + + if (!PyArg_ParseTuple(args, "sis", &src, &status, &dst)) + return (NULL); + + if (!http_redirect_add(domain->config, src, status, dst)) { + fatal("failed to add redirect '%s' on '%s'", + src, domain->config->domain); + } + + Py_RETURN_NONE; +} + static PyObject * pydomain_route(struct pydomain *domain, PyObject *args, PyObject *kwargs) {