kore

Kore is a web application platform for writing scalable, concurrent web based processes in C or Python.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 7b48959c32f6194b28edf28b20be6f625aedafea
parent 7a6753ca332d3b6ce6651b6aae3ddb143c0ec8bb
Author: Joris Vink <joris@coders.se>
Date:   Thu, 29 Dec 2022 12:58:43 +0100

Python HTTP API improvement: add req.headers()

Calling req.headers() will return all the request headers in
a dictionary to the caller.

Diffstat:
include/kore/python_methods.h | 2++
src/python.c | 40++++++++++++++++++++++++++++++++++++++++
2 files changed, 42 insertions(+), 0 deletions(-)

diff --git a/include/kore/python_methods.h b/include/kore/python_methods.h @@ -750,6 +750,7 @@ static void pyhttp_dealloc(struct pyhttp_request *); static void pyhttp_file_dealloc(struct pyhttp_file *); static PyObject *pyhttp_cookie(struct pyhttp_request *, PyObject *); +static PyObject *pyhttp_headers(struct pyhttp_request *, PyObject *); static PyObject *pyhttp_response(struct pyhttp_request *, PyObject *); static PyObject *pyhttp_argument(struct pyhttp_request *, PyObject *); static PyObject *pyhttp_body_read(struct pyhttp_request *, PyObject *); @@ -765,6 +766,7 @@ static PyObject *pyhttp_websocket_handshake(struct pyhttp_request *, static PyMethodDef pyhttp_request_methods[] = { METHOD("cookie", pyhttp_cookie, METH_VARARGS), + METHOD("headers", pyhttp_headers, METH_NOARGS), METHOD("response", pyhttp_response, METH_VARARGS), METHOD("argument", pyhttp_argument, METH_VARARGS), METHOD("body_read", pyhttp_body_read, METH_VARARGS), diff --git a/src/python.c b/src/python.c @@ -5094,6 +5094,46 @@ pyhttp_cookie(struct pyhttp_request *pyreq, PyObject *args) } static PyObject * +pyhttp_headers(struct pyhttp_request *pyreq, PyObject *args) +{ + struct http_header *hdr; + struct http_request *req; + PyObject *obj, *dict, *ret; + + ret = NULL; + obj = NULL; + dict = NULL; + + req = pyreq->req; + + if ((dict = PyDict_New()) == NULL) + goto cleanup; + + if ((obj = PyUnicode_FromString(req->host)) == NULL) + goto cleanup; + + if (PyDict_SetItemString(dict, "host", obj) == -1) + goto cleanup; + + TAILQ_FOREACH(hdr, &req->req_headers, list) { + if ((obj = PyUnicode_FromString(hdr->value)) == NULL) + goto cleanup; + if (PyDict_SetItemString(dict, hdr->header, obj) == -1) + goto cleanup; + } + + ret = dict; + obj = NULL; + dict = NULL; + +cleanup: + Py_XDECREF(obj); + Py_XDECREF(dict); + + return (ret); +} + +static PyObject * pyhttp_file_lookup(struct pyhttp_request *pyreq, PyObject *args) { const char *name;