/* this is a hack for supporting non-file object passed to wsgi.file_wrapper */ static void uwsgi_python_consume_file_wrapper_read(struct wsgi_request *wsgi_req, PyObject *pychunk) { PyObject *read_method_args = NULL; PyObject *read_method = PyObject_GetAttrString(pychunk, "read"); if (wsgi_req->sendfile_fd_chunk > 0) { read_method_args = PyTuple_New(1); PyTuple_SetItem(read_method_args, 0, PyInt_FromLong(wsgi_req->sendfile_fd_chunk)); } else { read_method_args = PyTuple_New(0); } for(;;) { PyObject *read_method_output = PyEval_CallObject(read_method, read_method_args); if (PyErr_Occurred()) { uwsgi_manage_exception(wsgi_req, 0); break; } if (!read_method_output) break; if (PyString_Check(read_method_output)) { char *content = PyString_AsString(read_method_output); size_t content_len = PyString_Size(read_method_output); if (content_len == 0) { Py_DECREF(read_method_output); break; } UWSGI_RELEASE_GIL uwsgi_response_write_body_do(wsgi_req, content, content_len); UWSGI_GET_GIL } Py_DECREF(read_method_output); if (wsgi_req->sendfile_fd_chunk == 0) break; } Py_DECREF(read_method_args); Py_DECREF(read_method); }
int uwsgi_python_send_body(struct wsgi_request *wsgi_req, PyObject *chunk) { char *content = NULL; size_t content_len = 0; #if defined(PYTHREE) || defined(Py_TPFLAGS_HAVE_NEWBUFFER) Py_buffer pbuf; int has_buffer = 0; #endif if (!up.wsgi_accept_buffer && !wsgi_req->is_raw) goto strict; #if defined(PYTHREE) || defined(Py_TPFLAGS_HAVE_NEWBUFFER) if (PyObject_CheckBuffer(chunk)) { if (!PyObject_GetBuffer(chunk, &pbuf, PyBUF_SIMPLE)) { content = (char *) pbuf.buf; content_len = (size_t) pbuf.len; has_buffer = 1; goto found; } } #else if (PyObject_CheckReadBuffer(chunk)) { #ifdef UWSGI_PYTHON_OLD int buffer_len = 0; if (!PyObject_AsCharBuffer(chunk, (const char **) &content, &buffer_len)) { #else if (!PyObject_AsCharBuffer(chunk, (const char **) &content, (Py_ssize_t *) &content_len)) { #endif PyErr_Clear(); goto found; } #ifdef UWSGI_PYTHON_OLD content_len = buffer_len; #endif } #endif strict: // fallback if (PyString_Check(chunk)) { content = PyString_AsString(chunk); content_len = PyString_Size(chunk); } found: if (content) { UWSGI_RELEASE_GIL uwsgi_response_write_body_do(wsgi_req, content, content_len); UWSGI_GET_GIL #if defined(PYTHREE) || defined(Py_TPFLAGS_HAVE_NEWBUFFER) if (has_buffer) PyBuffer_Release(&pbuf); #endif uwsgi_py_check_write_errors { uwsgi_py_write_exception(wsgi_req); return -1; } return 1; } return 0; } /* this is a hack for supporting non-file object passed to wsgi.file_wrapper */ static void uwsgi_python_consume_file_wrapper_read(struct wsgi_request *wsgi_req, PyObject *pychunk) { PyObject *read_method_args = NULL; PyObject *read_method = PyObject_GetAttrString(pychunk, "read"); if (wsgi_req->sendfile_fd_chunk > 0) { read_method_args = PyTuple_New(1); PyTuple_SetItem(read_method_args, 0, PyInt_FromLong(wsgi_req->sendfile_fd_chunk)); } else { read_method_args = PyTuple_New(0); } for(;;) { PyObject *read_method_output = PyEval_CallObject(read_method, read_method_args); if (PyErr_Occurred()) { uwsgi_manage_exception(wsgi_req, 0); break; } if (!read_method_output) break; if (PyString_Check(read_method_output)) { char *content = PyString_AsString(read_method_output); size_t content_len = PyString_Size(read_method_output); if (content_len == 0) { Py_DECREF(read_method_output); break; } UWSGI_RELEASE_GIL uwsgi_response_write_body_do(wsgi_req, content, content_len); UWSGI_GET_GIL } Py_DECREF(read_method_output); if (wsgi_req->sendfile_fd_chunk == 0) break; } Py_DECREF(read_method_args); Py_DECREF(read_method); }