From 7136beeff7a917e6215be273f0cdbf556ddaa55c Mon Sep 17 00:00:00 2001 From: Roberto De Ioris Date: Sun, 21 Oct 2012 18:19:14 +0200 Subject: [PATCH] added uwsgi.offload_transfer() --- core/sendfile.c | 10 ++++++++++ plugins/python/uwsgi_pymodule.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/core/sendfile.c b/core/sendfile.c index e052fb3d..5f07d4ac 100644 --- a/core/sendfile.c +++ b/core/sendfile.c @@ -10,6 +10,8 @@ enqueue a file transfer to the offload thread int uwsgi_offload_request_do(struct wsgi_request *wsgi_req, char *filename, size_t len) { + struct stat st; + // avoid closing the connection wsgi_req->fd_closed = 1; @@ -21,6 +23,14 @@ int uwsgi_offload_request_do(struct wsgi_request *wsgi_req, char *filename, size goto error; } uor.s = wsgi_req->poll.fd; + // make a fstat to get the file size + if (!len) { + if (fstat(uor.fd, &st)) { + uwsgi_error("fstat()"); + goto error2; + } + len = st.st_size; + } uor.pos = 0; uor.len = len; uor.written = 0; diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index 9dca48fb..630783ad 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -783,6 +783,36 @@ PyObject *py_uwsgi_send(PyObject * self, PyObject * args) { } +PyObject *py_uwsgi_offload_transfer(PyObject * self, PyObject * args) { + size_t len = 0; + char *filename = NULL; + struct wsgi_request *wsgi_req = current_wsgi_req(); + + if (!PyArg_ParseTuple(args, "s|i:offload_transfer", &filename, &len)) { + return NULL; + } + + if (!wsgi_req->socket->can_offload) { + return PyErr_Format(PyExc_ValueError, "The current socket does not support offloading"); + } + + if (!wsgi_req->headers_sent) { + if (uwsgi_python_do_send_headers(wsgi_req)) { + return PyErr_Format(PyExc_ValueError, "unable to send headers before offload transfer"); + } + } + + + UWSGI_RELEASE_GIL + if (uwsgi_offload_request_do(wsgi_req, filename, len)) { + UWSGI_GET_GIL + return PyErr_Format(PyExc_ValueError, "Unable to offload the request"); + } + UWSGI_GET_GIL + + return PyString_FromString(""); +} + #ifdef UWSGI_SENDFILE PyObject *py_uwsgi_advanced_sendfile(PyObject * self, PyObject * args) { @@ -3179,6 +3209,7 @@ static PyMethodDef uwsgi_advanced_methods[] = { #ifdef UWSGI_SENDFILE {"sendfile", py_uwsgi_advanced_sendfile, METH_VARARGS, ""}, #endif + {"offload_transfer", py_uwsgi_offload_transfer, METH_VARARGS, ""}, {"set_warning_message", py_uwsgi_warning, METH_VARARGS, ""}, {"mem", py_uwsgi_mem, METH_VARARGS, ""}, {"has_hook", py_uwsgi_has_hook, METH_VARARGS, ""},