kopia lustrzana https://github.com/cirospaciari/socketify.py
fix WSGI try_end
rodzic
3f7c3d4670
commit
27cfb3482b
|
@ -1,13 +1,7 @@
|
||||||
from io import BytesIO
|
|
||||||
|
|
||||||
payload = None
|
payload = None
|
||||||
with open("xml.zip", "rb") as file:
|
with open("xml.zip", "rb") as file:
|
||||||
payload = file.read()
|
payload = file.read()
|
||||||
|
|
||||||
|
|
||||||
stream = BytesIO()
|
|
||||||
stream.write(payload)
|
|
||||||
|
|
||||||
chunk_size = 64 * 1024
|
chunk_size = 64 * 1024
|
||||||
content_length = len(payload)
|
content_length = len(payload)
|
||||||
|
|
||||||
|
@ -37,6 +31,6 @@ def app_hello(environ, start_response):
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
from socketify import WSGI
|
from socketify import WSGI
|
||||||
WSGI(app_hello).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(1)
|
WSGI(app).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(4)
|
||||||
# import fastwsgi
|
# import fastwsgi
|
||||||
# fastwsgi.run(wsgi_app=app_hello, host='127.0.0.1', port=8000)
|
# fastwsgi.run(wsgi_app=app_hello, host='127.0.0.1', port=8000)
|
||||||
|
|
|
@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "socketify"
|
name = "socketify"
|
||||||
version = "0.0.18"
|
version = "0.0.19"
|
||||||
authors = [
|
authors = [
|
||||||
{ name="Ciro Spaciari", email="ciro.spaciari@gmail.com" },
|
{ name="Ciro Spaciari", email="ciro.spaciari@gmail.com" },
|
||||||
]
|
]
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -58,7 +58,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
|
||||||
|
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name="socketify",
|
name="socketify",
|
||||||
version="0.0.18",
|
version="0.0.19",
|
||||||
platforms=["any"],
|
platforms=["any"],
|
||||||
author="Ciro Spaciari",
|
author="Ciro Spaciari",
|
||||||
author_email="ciro.spaciari@gmail.com",
|
author_email="ciro.spaciari@gmail.com",
|
||||||
|
|
|
@ -39,21 +39,59 @@ def wsgi_on_writable_handler(res, offset, user_data):
|
||||||
if data_retry.aborted:
|
if data_retry.aborted:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
chunks = data_retry.chunks
|
chunks = data_retry.chunks
|
||||||
last_sended_offset = data_retry.last_offset
|
while len(chunks) > 0:
|
||||||
ssl = data_retry.app.server.SSL
|
last_sended_offset = data_retry.last_offset
|
||||||
content_length = data_retry.content_length
|
ssl = data_retry.app.server.SSL
|
||||||
|
content_length = data_retry.content_length
|
||||||
data = chunks[0]
|
|
||||||
last_offset = int(lib.uws_res_get_write_offset(ssl, res))
|
|
||||||
if last_sended_offset != last_offset:
|
|
||||||
offset = last_offset - last_sended_offset
|
|
||||||
data = data[offset:]
|
|
||||||
if len(data) == 0:
|
|
||||||
chunks.pop(0)
|
|
||||||
|
|
||||||
if len(chunks) == 0:
|
data = chunks[0]
|
||||||
|
data_size = len(data)
|
||||||
|
last_offset = int(lib.uws_res_get_write_offset(ssl, res))
|
||||||
|
if last_sended_offset != last_offset:
|
||||||
|
offset = last_offset - last_sended_offset
|
||||||
|
data = data[offset:data_size]
|
||||||
|
data_size = len(data)
|
||||||
|
if data_size == 0:
|
||||||
|
chunks.pop(0)
|
||||||
|
|
||||||
|
if len(chunks) == 0:
|
||||||
|
logging.error(AssertionError("Content-Length do not match sended content"))
|
||||||
|
lib.uws_res_close(
|
||||||
|
ssl,
|
||||||
|
res
|
||||||
|
)
|
||||||
|
if data_retry.id is not None:
|
||||||
|
data_retry.app._data_refs.pop(data_retry.id, None)
|
||||||
|
|
||||||
|
return False
|
||||||
|
data = chunks[0]
|
||||||
|
|
||||||
|
result = lib.uws_res_try_end(
|
||||||
|
ssl,
|
||||||
|
res,
|
||||||
|
data,
|
||||||
|
data_size,
|
||||||
|
content_length,
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
has_responded = bool(result.has_responded)
|
||||||
|
data_retry.last_offset = int(lib.uws_res_get_write_offset(ssl, res))
|
||||||
|
|
||||||
|
if bool(result.ok):
|
||||||
|
chunks.pop(0)
|
||||||
|
if not has_responded and len(chunks) == 0:
|
||||||
|
logging.error(AssertionError("Content-Length do not match sended content"))
|
||||||
|
lib.uws_res_close(
|
||||||
|
ssl,
|
||||||
|
res
|
||||||
|
)
|
||||||
|
if data_retry.id is not None:
|
||||||
|
data_retry.app._data_refs.pop(data_retry.id, None)
|
||||||
|
elif has_responded and data_retry.id is not None:
|
||||||
|
data_retry.app._data_refs.pop(data_retry.id, None)
|
||||||
|
break
|
||||||
|
elif not has_responded and len(chunks) == 0:
|
||||||
logging.error(AssertionError("Content-Length do not match sended content"))
|
logging.error(AssertionError("Content-Length do not match sended content"))
|
||||||
lib.uws_res_close(
|
lib.uws_res_close(
|
||||||
ssl,
|
ssl,
|
||||||
|
@ -61,29 +99,10 @@ def wsgi_on_writable_handler(res, offset, user_data):
|
||||||
)
|
)
|
||||||
if data_retry.id is not None:
|
if data_retry.id is not None:
|
||||||
data_retry.app._data_refs.pop(data_retry.id, None)
|
data_retry.app._data_refs.pop(data_retry.id, None)
|
||||||
|
elif has_responded and data_retry.id is not None:
|
||||||
return False
|
data_retry.app._data_refs.pop(data_retry.id, None)
|
||||||
data = chunks[0]
|
break
|
||||||
|
|
||||||
result = lib.uws_res_try_end(
|
|
||||||
ssl,
|
|
||||||
res,
|
|
||||||
data,
|
|
||||||
len(data),
|
|
||||||
ffi.cast("uintmax_t", content_length),
|
|
||||||
0,
|
|
||||||
)
|
|
||||||
if bool(result.ok):
|
|
||||||
chunks.pop(0)
|
|
||||||
|
|
||||||
if not bool(result.has_responded) and len(chunks) == 0:
|
|
||||||
logging.error(AssertionError("Content-Length do not match sended content"))
|
|
||||||
lib.uws_res_close(
|
|
||||||
ssl,
|
|
||||||
res
|
|
||||||
)
|
|
||||||
if bool(result.has_responded) and data_retry.id is not None:
|
|
||||||
data_retry.app._data_refs.pop(data_retry.id, None)
|
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -385,7 +404,6 @@ def wsgi(ssl, response, info, user_data, aborted):
|
||||||
)
|
)
|
||||||
# this should be very very rare for HTTP
|
# this should be very very rare for HTTP
|
||||||
if not bool(result.ok):
|
if not bool(result.ok):
|
||||||
last_offset = int(lib.uws_res_get_write_offset(ssl, response))
|
|
||||||
failed_chunks = []
|
failed_chunks = []
|
||||||
# just mark the chunks
|
# just mark the chunks
|
||||||
failed_chunks.append(data)
|
failed_chunks.append(data)
|
||||||
|
@ -393,7 +411,6 @@ def wsgi(ssl, response, info, user_data, aborted):
|
||||||
data_retry = WSGIRetryDataSend(
|
data_retry = WSGIRetryDataSend(
|
||||||
app, failed_chunks, content_length, last_offset
|
app, failed_chunks, content_length, last_offset
|
||||||
)
|
)
|
||||||
break
|
|
||||||
|
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
logging.exception(error)
|
logging.exception(error)
|
||||||
|
@ -465,7 +482,6 @@ def wsgi(ssl, response, info, user_data, aborted):
|
||||||
data_retry = WSGIRetryDataSend(
|
data_retry = WSGIRetryDataSend(
|
||||||
app, failed_chunks, content_length, last_offset
|
app, failed_chunks, content_length, last_offset
|
||||||
)
|
)
|
||||||
break
|
|
||||||
|
|
||||||
|
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
|
|
Ładowanie…
Reference in New Issue