expose new cookie stuff to python.

req.populate_cookies()
value = req.cookie("name")
This commit is contained in:
Joris Vink 2017-02-07 22:54:42 +01:00
parent b8c6cddc3d
commit e9b4f966c2
4 changed files with 44 additions and 0 deletions

View File

@ -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

View File

@ -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'')

View File

@ -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),

View File

@ -812,6 +812,13 @@ pyhttp_populate_multi(struct pyhttp_request *pyreq, PyObject *args)
Py_RETURN_TRUE;
}
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)
{
@ -834,6 +841,28 @@ pyhttp_argument(struct pyhttp_request *pyreq, PyObject *args)
return (value);
}
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)
{