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 e9b4f966c2525fe87daca708d6231be69dbc687a
parent b8c6cddc3d6f8143cfa1a9f5d0d9449bedd6643b
Author: Joris Vink <joris@coders.se>
Date:   Tue,  7 Feb 2017 22:54:42 +0100

expose new cookie stuff to python.

req.populate_cookies()
value = req.cookie("name")

Diffstat:
examples/python/conf/python.conf | 1+
examples/python/src/index.py | 10++++++++++
includes/python_methods.h | 4++++
src/python.c | 29+++++++++++++++++++++++++++++
4 files changed, 44 insertions(+), 0 deletions(-)

diff --git a/examples/python/conf/python.conf b/examples/python/conf/python.conf @@ -34,6 +34,7 @@ domain * { static /json json_parse static /ws ws_connect static /upload upload + static /kaka kaka # Use the builtin asset_serve_* to serve frontend HTML. static /wspage asset_serve_frontend_html diff --git a/examples/python/src/index.py b/examples/python/src/index.py @@ -117,3 +117,13 @@ def json_parse(req): # def minimal(req): req.response(200, b'') + +# +# Small handler that grabs a cookie if set. +# +def kaka(req): + req.populate_cookies() + cookie = req.cookie("hello") + if cookie is not None: + kore.log(kore.LOG_INFO, "got hello with value %s" % cookie) + req.response(200, b'') diff --git a/includes/python_methods.h b/includes/python_methods.h @@ -89,6 +89,7 @@ static void pyhttp_file_dealloc(struct pyhttp_file *); #if defined(KORE_USE_PGSQL) static PyObject *pyhttp_pgsql(struct pyhttp_request *, PyObject *); #endif +static PyObject *pyhttp_cookie(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 *); @@ -96,6 +97,7 @@ static PyObject *pyhttp_file_lookup(struct pyhttp_request *, PyObject *); static PyObject *pyhttp_populate_get(struct pyhttp_request *, PyObject *); static PyObject *pyhttp_populate_post(struct pyhttp_request *, PyObject *); static PyObject *pyhttp_populate_multi(struct pyhttp_request *, PyObject *); +static PyObject *pyhttp_populate_cookies(struct pyhttp_request *, PyObject *); static PyObject *pyhttp_request_header(struct pyhttp_request *, PyObject *); static PyObject *pyhttp_response_header(struct pyhttp_request *, PyObject *); static PyObject *pyhttp_websocket_handshake(struct pyhttp_request *, @@ -105,6 +107,7 @@ static PyMethodDef pyhttp_request_methods[] = { #if defined(KORE_USE_PGSQL) METHOD("pgsql", pyhttp_pgsql, METH_VARARGS), #endif + METHOD("cookie", pyhttp_cookie, METH_VARARGS), METHOD("response", pyhttp_response, METH_VARARGS), METHOD("argument", pyhttp_argument, METH_VARARGS), METHOD("body_read", pyhttp_body_read, METH_VARARGS), @@ -112,6 +115,7 @@ static PyMethodDef pyhttp_request_methods[] = { METHOD("populate_get", pyhttp_populate_get, METH_NOARGS), METHOD("populate_post", pyhttp_populate_post, METH_NOARGS), METHOD("populate_multi", pyhttp_populate_multi, METH_NOARGS), + METHOD("populate_cookies", pyhttp_populate_cookies, METH_NOARGS), METHOD("request_header", pyhttp_request_header, METH_VARARGS), METHOD("response_header", pyhttp_response_header, METH_VARARGS), METHOD("websocket_handshake", pyhttp_websocket_handshake, METH_VARARGS), diff --git a/src/python.c b/src/python.c @@ -813,6 +813,13 @@ pyhttp_populate_multi(struct pyhttp_request *pyreq, PyObject *args) } static PyObject * +pyhttp_populate_cookies(struct pyhttp_request *pyreq, PyObject *args) +{ + http_populate_cookies(pyreq->req); + Py_RETURN_TRUE; +} + +static PyObject * pyhttp_argument(struct pyhttp_request *pyreq, PyObject *args) { const char *name; @@ -835,6 +842,28 @@ pyhttp_argument(struct pyhttp_request *pyreq, PyObject *args) } static PyObject * +pyhttp_cookie(struct pyhttp_request *pyreq, PyObject *args) +{ + const char *name; + PyObject *value; + char *string; + + if (!PyArg_ParseTuple(args, "s", &name)) { + PyErr_SetString(PyExc_TypeError, "invalid parameters"); + return (NULL); + } + + if (!http_request_cookie(pyreq->req, name, &string)) { + Py_RETURN_NONE; + } + + if ((value = PyUnicode_FromString(string)) == NULL) + return (PyErr_NoMemory()); + + return (value); +} + +static PyObject * pyhttp_file_lookup(struct pyhttp_request *pyreq, PyObject *args) { const char *name;