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 4a56424842d71403a8119006dc653b0a859b0c0e
parent 0b84a9c1d4c76ecc1619bbcf8949c10bf501bebd
Author: Joris Vink <joris@coders.se>
Date:   Mon, 13 Mar 2017 12:29:42 +0100

Improve python websocket handling a bit.

Convert the data parameter to a string if the op was WEBSOCKET_OP_TEXT
or convert to bytes if op was WEBSOCKET_OP_BINARY so the callee does
not have to do this anymore.

Diffstat:
src/python.c | 14++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/python.c b/src/python.c @@ -377,8 +377,18 @@ python_runtime_wsmessage(void *addr, struct connection *c, u_int8_t op, if ((pyop = PyLong_FromLong((long)op)) == NULL) fatal("python_runtime_wsmessage: PyLong_FromLong failed"); - if ((pydata = PyBytes_FromStringAndSize(data, len)) == NULL) - fatal("python_runtime_wsmessage: PyBytes_FromString failed"); + switch (op) { + case WEBSOCKET_OP_TEXT: + if ((pydata = PyUnicode_FromStringAndSize(data, len)) == NULL) + fatal("wsmessage: PyUnicode_AsUTF8AndSize failed"); + break; + case WEBSOCKET_OP_BINARY: + if ((pydata = PyBytes_FromStringAndSize(data, len)) == NULL) + fatal("wsmessage: PyBytes_FromString failed"); + break; + default: + fatal("python_runtime_wsmessage: invalid op"); + } if ((args = PyTuple_New(3)) == NULL) fatal("python_runtime_wsmessage: PyTuple_New failed");