添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
热心肠的人字拖  ·  Error: CommTCP_read: ...·  2 周前    · 
欢快的香烟  ·  Windows Server 安装 AD ...·  1 月前    · 
留胡子的火车  ·  二次元虫洞·  1 月前    · 

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account "more_body" : False ,

Nginx Unit currently allows body to be only bytes type, but Python types bytearray and memoryview could be supported, to avoid conversion to bytes and copying memory.
bytearray is faster then bytes when appending lots of small parts.
memoryview over bytes or bytearray objects is used to avoid memory copying.

Maybe all objects with buffer protocol could be supported https://docs.python.org/3/c-api/buffer.html

Looking at file https://github.com/nginx/unit/blob/master/src/python/nxt_python_asgi_http.c
https://docs.python.org/3/c-api/memoryview.html
https://docs.python.org/3/c-api/bytearray.html
this should be straightforward.

Uvicorn ASGI server support these types.

Hi @filiphanes ,

Thank you for your report. Could you please try following patch with bytearray support if it works for you:

diff --git a/src/python/nxt_python_asgi_http.c b/src/python/nxt_python_asgi_http.c
--- a/src/python/nxt_python_asgi_http.c
+++ b/src/python/nxt_python_asgi_http.c
@@ -362,16 +362,6 @@ nxt_py_asgi_http_response_body(nxt_py_as
     Py_ssize_t              body_len, body_off;
     nxt_py_asgi_ctx_data_t  *ctx_data;
-    body = PyDict_GetItem(dict, nxt_py_body_str);
-    if (nxt_slow_path(body != NULL && !PyBytes_Check(body))) {
-        return PyErr_Format(PyExc_TypeError, "'body' is not a byte string");
-    }
-    more_body = PyDict_GetItem(dict, nxt_py_more_body_str);
-    if (nxt_slow_path(more_body != NULL && !PyBool_Check(more_body))) {
-        return PyErr_Format(PyExc_TypeError, "'more_body' is not a bool");
-    }
     if (nxt_slow_path(http->complete)) {
         return PyErr_Format(PyExc_RuntimeError,
                             "Unexpected ASGI message 'http.response.body' "
@@ -382,9 +372,26 @@ nxt_py_asgi_http_response_body(nxt_py_as
         return PyErr_Format(PyExc_RuntimeError, "Concurrent send");
+    more_body = PyDict_GetItem(dict, nxt_py_more_body_str);
+    if (nxt_slow_path(more_body != NULL && !PyBool_Check(more_body))) {
+        return PyErr_Format(PyExc_TypeError, "'more_body' is not a bool");
+    }
+    body = PyDict_GetItem(dict, nxt_py_body_str);
     if (body != NULL) {
-        body_str = PyBytes_AS_STRING(body);
-        body_len = PyBytes_GET_SIZE(body);
+        if (PyBytes_Check(body)) {
+            body_str = PyBytes_AS_STRING(body);
+            body_len = PyBytes_GET_SIZE(body);
+        } else if (PyByteArray_Check(body)) {
+            body_str = PyByteArray_AS_STRING(body);
+            body_len = PyByteArray_GET_SIZE(body);
+        } else {
+            return PyErr_Format(PyExc_TypeError,
+                                "'body' is not a byte string or bytearray");
+        }
         nxt_unit_req_debug(http->req, "asgi_http_response_body: %d, %d",
                            (int) body_len, (more_body == Py_True) );
      
@filiphanes requested support for bytearray
and memoryview in the request body here:
nginx#648
This patch implements bytearray body support only.
Memoryview body still need to be implemented.
@filiphanes requested support for bytearray
and memoryview in the request body here:
<nginx#648>
This patch implements bytearray body support only.
Memoryview body still need to be implemented.
@filiphanes requested support for bytearray
and memoryview in the request body here:
<nginx#648>
This patch implements bytearray body support only.
Memoryview body still need to be implemented.