kopia lustrzana https://github.com/cirospaciari/socketify.py
Porównaj commity
112 Commity
Autor | SHA1 | Data |
---|---|---|
![]() |
2ed7ec1403 | |
![]() |
b370876074 | |
![]() |
464804edef | |
![]() |
d2f26c682c | |
![]() |
328e249c2c | |
![]() |
d73ed582d5 | |
![]() |
226ae66f6d | |
![]() |
63b1546bbb | |
![]() |
90b08b55d3 | |
![]() |
2f9c267e01 | |
![]() |
30cec004e0 | |
![]() |
488f7bd6ac | |
![]() |
0a3b3fa32f | |
![]() |
70199e8d7d | |
![]() |
833f07b711 | |
![]() |
7ef429ef9f | |
![]() |
c692e29ba2 | |
![]() |
8ca74b05d8 | |
![]() |
df212efc27 | |
![]() |
2a398a3543 | |
![]() |
293b2a97c2 | |
![]() |
2aba41ff7b | |
![]() |
2c51d97097 | |
![]() |
26709f4086 | |
![]() |
e6710b6a9e | |
![]() |
e8b4c21515 | |
![]() |
1edf1f7e78 | |
![]() |
fb1561dd4e | |
![]() |
3b28e0edfe | |
![]() |
263ff096c6 | |
![]() |
dd9bb7512f | |
![]() |
037a29f3e2 | |
![]() |
4a6629c7fe | |
![]() |
87db175640 | |
![]() |
299e36500a | |
![]() |
dc41c89a39 | |
![]() |
fcea3cf783 | |
![]() |
a259258cd7 | |
![]() |
ce1ee6fa94 | |
![]() |
bd753ae0e2 | |
![]() |
9060913d49 | |
![]() |
f2c8ef04b2 | |
![]() |
005b150d4a | |
![]() |
68beb7b40f | |
![]() |
ce55fd194b | |
![]() |
6439307307 | |
![]() |
21c1f0f5fb | |
![]() |
c96c243fd2 | |
![]() |
b3e7b494ab | |
![]() |
34f3d04036 | |
![]() |
375b5e0d14 | |
![]() |
a63b75e30a | |
![]() |
99e716e657 | |
![]() |
2cda5f7243 | |
![]() |
1d57eff163 | |
![]() |
40404519e5 | |
![]() |
98dad2117a | |
![]() |
41af724854 | |
![]() |
d954bad875 | |
![]() |
664b2d9ff1 | |
![]() |
0de0e99b02 | |
![]() |
6bf18b7a79 | |
![]() |
3afb7ec2f2 | |
![]() |
e0e28263f5 | |
![]() |
18892b729d | |
![]() |
27cfb3482b | |
![]() |
3f7c3d4670 | |
![]() |
819b1f62d6 | |
![]() |
043762d141 | |
![]() |
4744855d64 | |
![]() |
8badbc3eb5 | |
![]() |
037673c68d | |
![]() |
23f6e26d26 | |
![]() |
620b853607 | |
![]() |
7adfb90ccf | |
![]() |
f006601588 | |
![]() |
a340dda35b | |
![]() |
cc10a3e4ba | |
![]() |
ed8b0eec3a | |
![]() |
288c1d7e61 | |
![]() |
81f5b1e4a1 | |
![]() |
2ae9059e05 | |
![]() |
f562556844 | |
![]() |
a447904ec7 | |
![]() |
f9a76df2f1 | |
![]() |
ea202f8e4d | |
![]() |
741cff380e | |
![]() |
9fdf8daaf5 | |
![]() |
d2ecb6ccbf | |
![]() |
ba6d8861b4 | |
![]() |
e904e8aed6 | |
![]() |
4794a1a5d6 | |
![]() |
9b8660db01 | |
![]() |
c72704a420 | |
![]() |
a246161a54 | |
![]() |
ebd10964ba | |
![]() |
6c94ad5c8c | |
![]() |
0a78f58783 | |
![]() |
2cb295c161 | |
![]() |
d32f948e22 | |
![]() |
bdddaedda8 | |
![]() |
a84dba6eab | |
![]() |
f04a5fd642 | |
![]() |
2f05a44196 | |
![]() |
f1375cf589 | |
![]() |
6abb96824d | |
![]() |
65e54834e4 | |
![]() |
cf2e4b04a1 | |
![]() |
230bb841ff | |
![]() |
45ff9caf50 | |
![]() |
543e83b561 | |
![]() |
3fd00f3c4a |
|
@ -25,7 +25,7 @@ jobs:
|
|||
cmake -DCMAKE_BUILD_TYPE=Release -GNinja .. && ninja crypto ssl
|
||||
|
||||
cd ..\..\..\..\
|
||||
cl /MD /W3 /D /EHsc /Zc:__cplusplus /Ox /DLL /D_WINDLL /LD /D "NOMINMAX" /D "WIN32_LEAN_AND_MEAN" /D "UWS_NO_ZLIB" /D "UWS_WITH_PROXY" /D "LIBUS_USE_LIBUV" /I native/src/ /I uWebSockets/src /I uWebSockets/capi /I uWebSockets/uSockets/boringssl/include /D "LIBUS_USE_OPENSSL" /std:c++20 /I C:\vcpkg\packages\libuv_x64-windows-static-md\include /I uWebSockets/uSockets/src /Felibsocketify_windows_amd64.dll ./native/src/libsocketify.cpp uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/crypto/*.cpp uWebSockets/uSockets/src/eventing/*.c uWebSockets/uSockets/src/crypto/*.c advapi32.lib uWebSockets/uSockets/boringssl/amd64/ssl/ssl.lib uWebSockets/uSockets/boringssl/amd64/crypto/crypto.lib C:\vcpkg\installed\x64-windows-static-md\lib\uv_a.lib iphlpapi.lib userenv.lib psapi.lib user32.lib
|
||||
cl /MD /W3 /D /EHsc /Zc:__cplusplus /Ox /DLL /D_WINDLL /LD /D "NOMINMAX" /D "WIN32_LEAN_AND_MEAN" /D "UWS_NO_ZLIB" /D "UWS_WITH_PROXY" /D "LIBUS_USE_LIBUV" /I native/src/ /I uWebSockets/src /I uWebSockets/capi /I uWebSockets/uSockets/boringssl/include /D "LIBUS_USE_OPENSSL" /std:c++20 /I C:\vcpkg\packages\libuv_x64-windows-static-md\include /I uWebSockets/uSockets/src /Felibsocketify_windows_amd64.dll ./native/src/libsocketify.cpp uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/crypto/*.cpp uWebSockets/uSockets/src/eventing/*.c uWebSockets/uSockets/src/crypto/*.c advapi32.lib uWebSockets/uSockets/boringssl/amd64/ssl/ssl.lib uWebSockets/uSockets/boringssl/amd64/crypto/crypto.lib C:\vcpkg\installed\x64-windows-static-md\lib\libuv.lib iphlpapi.lib userenv.lib psapi.lib user32.lib shell32.lib dbghelp.lib ole32.lib uuid.lib ws2_32.lib
|
||||
|
||||
git add libsocketify_windows_amd64.dll
|
||||
git config --global user.email "ciro.spaciari@gmail.com"
|
||||
|
|
|
@ -6,3 +6,4 @@ __pycache__
|
|||
node_modules/
|
||||
yarn.lock
|
||||
.vscode
|
||||
/venv
|
62
README.md
62
README.md
|
@ -1,26 +1,26 @@
|
|||
# socketify.py
|
||||
|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/cirospaciari/socketify.py"><img src="https://raw.githubusercontent.com/cirospaciari/socketify.py/main/misc/logo.png" alt="Logo" height=170></a>
|
||||
<br />
|
||||
<br />
|
||||
<a href="https://github.com/cirospaciari/socketify.py/actions/workflows/linux.yml" target="_blank"><img src="https://github.com/cirospaciari/socketify.py/actions/workflows/linux.yml/badge.svg" /></a>
|
||||
<a href="https://github.com/cirospaciari/socketify.py/actions/workflows/windows.yml" target="_blank"><img src="https://github.com/cirospaciari/socketify.py/actions/workflows/windows.yml/badge.svg" /></a>
|
||||
<a href="https://github.com/cirospaciari/socketify.py/actions/workflows/macos.yml" target="_blank"><img src="https://github.com/cirospaciari/socketify.py/actions/workflows/macos.yml/badge.svg" /></a>
|
||||
<a href="https://github.com/cirospaciari/socketify.py/actions/workflows/macos_arm64.yml" target="_blank"><img src="https://github.com/cirospaciari/socketify.py/actions/workflows/macos_arm64.yml/badge.svg" /></a>
|
||||
<br/>
|
||||
<a href='https://github.com/cirospaciari/socketify.py'><img alt='GitHub Clones' src='https://img.shields.io/badge/dynamic/json?color=success&label=Clones&query=count&url=https://gist.githubusercontent.com/cirospaciari/2243d59951f4abe4fd2000f1e20bc561/raw/clone.json&logo=github'></a>
|
||||
<a href='https://pypi.org/project/socketify/' target="_blank"><img alt='PyPI Downloads' src='https://static.pepy.tech/personalized-badge/socketify?period=total&units=international_system&left_color=grey&right_color=brightgreen&left_text=Downloads'></a>
|
||||
<a href="https://github.com/sponsors/cirospaciari/" target="_blank"><img src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&link=https://github.com/sponsors/cirospaciari"/></a>
|
||||
|
||||
|
||||
<a href='https://discord.socketify.dev/' target="_blank"><img alt='Discord' src='https://img.shields.io/discord/1042529276219641906?label=Discord'></a>
|
||||
</p>
|
||||
|
||||
<div align="center">
|
||||
<a href="https://docs.socketify.dev">Documentation</a>
|
||||
<span> • </span>
|
||||
<a href="https://discord.socketify.dev/">Discord</a>
|
||||
<span> • </span>
|
||||
<a href="https://github.com/cirospaciari/socketify.py/issues">Issues</a>
|
||||
<span> • </span>
|
||||
<a href="https://github.com/cirospaciari/socketify.py/tree/main/examples">Examples</a>
|
||||
<br />
|
||||
</div>
|
||||
|
||||
## :bookmark_tabs: Documentation
|
||||
See the full docs in [docs.socketify.dev](https://docs.socketify.dev) or in [/docs/README.md](docs/README.md)
|
||||
|
||||
Also take a look in the examples in [/examples](https://github.com/cirospaciari/socketify.py/tree/main/examples)
|
||||
## 💡 Features
|
||||
|
||||
- WebSocket with pub/sub support
|
||||
|
@ -44,6 +44,7 @@ Also take a look in the examples in [/examples](https://github.com/cirospaciari/
|
|||
- [`Plugins/Extensions`](https://docs.socketify.dev/extensions.html)
|
||||
|
||||
## :mag_right: Upcoming Features
|
||||
|
||||
- In-Memory Cache Tools
|
||||
- Fetch like API powered by libuv
|
||||
- Async file IO powered by libuv
|
||||
|
@ -59,6 +60,7 @@ We created and adapted the full C API from [uNetworking/uWebSockets](https://git
|
|||
Join Github [`Discussions`](https://github.com/cirospaciari/socketify.py/discussions) or [`Discord`](https://discord.socketify.dev/) for help and have a look at the development progress.
|
||||
|
||||
## :zap: Benchmarks
|
||||
|
||||
Socketify WebFramework HTTP requests per second (Linux x64)
|
||||
|
||||

|
||||
|
@ -75,7 +77,6 @@ WebSocket messages per second (Linux x64)
|
|||
|
||||

|
||||
|
||||
|
||||
Http tested with TFB tool plaintext benchmark<br/>
|
||||
WebSocket tested with [Bun.sh](https://bun.sh) bench chat-client <br/>
|
||||
Source code in [TechEmPower](https://github.com/TechEmpower/FrameworkBenchmarks) and for websockets in [bench](https://github.com/cirospaciari/socketify.py/tree/main/bench)<br/>
|
||||
|
@ -83,6 +84,7 @@ Source code in [TechEmPower](https://github.com/TechEmpower/FrameworkBenchmarks)
|
|||
Machine OS: Debian GNU/Linux bookworm/sid x86_64 Kernel: 6.0.0-2-amd64 CPU: Intel i7-7700HQ (8) @ 3.800GHz Memory: 32066MiB
|
||||
|
||||
## 📦 Installation
|
||||
|
||||
For macOS x64 & Silicon, Linux x64, Windows
|
||||
|
||||
```bash
|
||||
|
@ -94,9 +96,11 @@ pypy3 -m pip install -e socketify
|
|||
```
|
||||
|
||||
Using install via requirements.txt
|
||||
|
||||
```text
|
||||
socketify
|
||||
```
|
||||
|
||||
```bash
|
||||
pip install -r ./requirements.txt
|
||||
#or specify PyPy3
|
||||
|
@ -106,19 +110,28 @@ pypy3 -m pip install -r ./requirements.txt
|
|||
If you are using linux or macOS, you may need to install libuv and zlib in your system
|
||||
|
||||
macOS
|
||||
|
||||
```bash
|
||||
brew install libuv
|
||||
brew install zlib
|
||||
```
|
||||
|
||||
Linux
|
||||
Linux (Ubuntu/Debian)
|
||||
|
||||
```bash
|
||||
apt install libuv1 zlib1g
|
||||
```
|
||||
|
||||
Linux (RHEL/OEL)
|
||||
|
||||
```bash
|
||||
yum install cmake zlib-devel libuv-devel
|
||||
```
|
||||
|
||||
## 🤔 Usage
|
||||
|
||||
Hello world app
|
||||
|
||||
```python
|
||||
from socketify import App
|
||||
|
||||
|
@ -129,6 +142,7 @@ app.run()
|
|||
```
|
||||
|
||||
SSL version sample
|
||||
|
||||
``` python
|
||||
from socketify import App, AppOptions
|
||||
|
||||
|
@ -139,8 +153,9 @@ app.run()
|
|||
```
|
||||
|
||||
WebSockets
|
||||
|
||||
```python
|
||||
from socketify import App, AppOptions, OpCode, CompressOptions
|
||||
from socketify import App, OpCode, CompressOptions
|
||||
|
||||
def ws_open(ws):
|
||||
print('A WebSocket got connected!')
|
||||
|
@ -159,7 +174,7 @@ app.ws("/*", {
|
|||
'message': ws_message,
|
||||
'drain': lambda ws: print(f'WebSocket backpressure: {ws.get_buffered_amount()}'),
|
||||
'close': lambda ws, code, message: print('WebSocket closed'),
|
||||
'subscription': lambda ws, topic, subscriptions, subscriptions_before: print(f'subscription/unsubscription on topic {topic} {subscriptions} {subscriptions_before}'),
|
||||
'subscription': lambda ws, topic, subscriptions, subscriptions_before: print(f'subscribe/unsubscribe on topic {topic} {subscriptions} {subscriptions_before}'),
|
||||
})
|
||||
app.any("/", lambda res,req: res.end("Nothing to see here!'"))
|
||||
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % (config.port)))
|
||||
|
@ -169,6 +184,7 @@ app.run()
|
|||
We have more than 20 examples [click here](https://github.com/cirospaciari/socketify.py/tree/main/examples) for more
|
||||
|
||||
## :hammer: Building from source
|
||||
|
||||
```bash
|
||||
#clone and update submodules
|
||||
git clone https://github.com/cirospaciari/socketify.py.git
|
||||
|
@ -185,6 +201,7 @@ pypy3 -m pip uninstall socketify
|
|||
```
|
||||
|
||||
## :briefcase: Commercially supported
|
||||
|
||||
I'm a Brazilian consulting & contracting company dealing with anything related with [socketify.py](https://github.com/cirospaciari/socketify.py) and [socketify.rb](https://github.com/cirospaciari/socketify.rb)
|
||||
|
||||
Don't hesitate sending a mail if you are in need of advice, support, or having other business inquiries in mind. We'll figure out what's best for both parties.
|
||||
|
@ -192,6 +209,7 @@ Don't hesitate sending a mail if you are in need of advice, support, or having o
|
|||
Special thank's to [uNetworking AB](https://github.com/uNetworking) to develop [uWebSockets](https://github.com/uNetworking/uWebSockets), [uSockets](https://github.com/uNetworking/uSockets) and allow us to bring this features and performance to Python and PyPy
|
||||
|
||||
## :heart: Sponsors
|
||||
|
||||
If you like to see this project thrive, you can sponsor us on GitHub too. We need all the help we can get.
|
||||
|
||||
Thank you [`Otavio Augusto`](https://github.com/middlebaws) to be the first sponsor of this project!
|
||||
|
@ -199,21 +217,17 @@ Thank you [`Otavio Augusto`](https://github.com/middlebaws) to be the first spon
|
|||
<a href="https://github.com/sponsors/cirospaciari/" target="_blank"><img src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&link=https://github.com/sponsors/cirospaciari"/></a>
|
||||
|
||||
## :star: Stargazers
|
||||
|
||||
[](https://github.com/cirospaciari/socketify.py/stargazers)
|
||||
|
||||
## :wrench: Forkers
|
||||
|
||||
[](https://github.com/cirospaciari/socketify.py/network/members)
|
||||
|
||||
|
||||
## :question: socketify.py vs japronto
|
||||
|
||||
People really want to compare with japronto, but this projects are not really comparable. Socketify is an active project and will be maintained over time with security updates and new features, japronto don't get any github updates since 2020 and don't get any src update since 2018, japronto don't support SSL, WebSockets, [`PyPy3`](https://www.pypy.org/), Windows or macOS Silicon, socketify will support Http3 and a lot more features.
|
||||
|
||||
And yes, we can be faster than japronto when all our features and goals are achieved, and we are probably faster than any current maintained solution out there.
|
||||
|
||||
## :grey_question: uvloop
|
||||
|
||||
We don't use uvloop, because uvloop don't support Windows and PyPy3 at this moment, this can change in the future, but right now we want to implement our own libuv + asyncio solution, and a lot more.
|
||||
|
||||
## :dizzy: CFFI vs Cython vs HPy
|
||||
Cython performs really well on Python3 but really bad on PyPy3, CFFI are chosen for better support PyPy3 until we got our hands on an stable [`HPy`](https://hpyproject.org/) integration.
|
||||
|
||||
Cython performs really well on Python3 but really bad on PyPy3, CFFI are chosen for better support PyPy3 until we got our hands on a stable [`HPy`](https://hpyproject.org/) integration.
|
||||
|
|
2
SSGI.md
2
SSGI.md
|
@ -15,7 +15,7 @@ class SSGIHttpResponse:
|
|||
# send chunk of data, can be used to perform with less backpressure than using send
|
||||
# total_size is the sum of all lengths in bytes of all chunks to be sended
|
||||
# connection will end when total_size is met
|
||||
# returns tuple(bool, bool) first bool represents if the chunk is succefully sended, the second if the connection has ended
|
||||
# returns tuple(bool, bool) first bool represents if the chunk is successfully sended, the second if the connection has ended
|
||||
def send_chunk(self, chunk: Union[str, bytes, bytearray, memoryview], total_size: int = False) -> Awaitable:
|
||||
pass
|
||||
|
||||
|
|
|
@ -8,11 +8,10 @@ class Home:
|
|||
resp.content_type = falcon.MEDIA_TEXT # Default is JSON, so override
|
||||
resp.text = "Hello, World!"
|
||||
def on_post(self, req, resp):
|
||||
raw_data = req.stream.getvalue()
|
||||
print("data", raw_data)
|
||||
raw_data = req.stream.read()
|
||||
resp.status = falcon.HTTP_200 # This is the default status
|
||||
resp.content_type = falcon.MEDIA_TEXT # Default is JSON, so override
|
||||
resp.text = raw_data
|
||||
resp.text = 'Ok'
|
||||
|
||||
|
||||
|
||||
|
@ -23,4 +22,4 @@ home = Home()
|
|||
app.add_route("/", home)
|
||||
|
||||
if __name__ == "__main__":
|
||||
WSGI(app).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(workers=8)
|
||||
WSGI(app).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(workers=1)
|
|
@ -1,6 +1,5 @@
|
|||
from socketify import ASGI
|
||||
|
||||
|
||||
async def app(scope, receive, send):
|
||||
assert scope['type'] == 'http'
|
||||
|
||||
|
@ -20,4 +19,4 @@ async def app(scope, receive, send):
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
ASGI(app).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(8)
|
||||
ASGI(app, lifespan=False).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(1)
|
||||
|
|
|
@ -1,8 +1,70 @@
|
|||
from socketify import WSGI
|
||||
payload = None
|
||||
with open("xml.zip", "rb") as file:
|
||||
payload = file.read()
|
||||
|
||||
chunk_size = 64 * 1024
|
||||
content_length = len(payload)
|
||||
|
||||
def app_chunked(environ, start_response):
|
||||
start_response('200 OK', [('Content-Type', 'application/zip'), ('Transfer-Encoding', 'chunked')])
|
||||
|
||||
sended = 0
|
||||
while content_length > sended:
|
||||
end = sended + chunk_size
|
||||
yield payload[sended:end]
|
||||
sended = end
|
||||
|
||||
|
||||
def app(environ, start_response):
|
||||
start_response('200 OK', [('Content-Type', 'application/zip'), ('Content-Length', str(content_length))])
|
||||
|
||||
sended = 0
|
||||
while content_length > sended:
|
||||
end = sended + chunk_size
|
||||
yield payload[sended:end]
|
||||
sended = end
|
||||
|
||||
# import gc
|
||||
|
||||
# gc.collect()
|
||||
# gc.set_threshold(50, 3, 3)
|
||||
|
||||
# import tracemalloc
|
||||
|
||||
# tracemalloc.start()
|
||||
|
||||
def app_hello(environ, start_response):
|
||||
# start_response('200 OK', [('Content-Type', 'text/plain'), ('Content-Length', '13')])
|
||||
start_response('200 OK', [('Content-Type', 'text/plain')])
|
||||
yield b'Hello, World!\n'
|
||||
|
||||
return [ b'Hello, World!']
|
||||
|
||||
if __name__ == "__main__":
|
||||
WSGI(app).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(8)
|
||||
# import fastwsgi
|
||||
# fastwsgi.run(wsgi_app=app_hello, host='127.0.0.1', port=8000, loglevel=0)
|
||||
# from meinheld import server
|
||||
# server.listen(("0.0.0.0", 8000))
|
||||
# server.run(app_hello)
|
||||
from socketify import WSGI
|
||||
WSGI(app_hello).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(1)
|
||||
# def run_app():
|
||||
# import fastwsgi
|
||||
# fastwsgi.run(wsgi_app=app_hello, host='127.0.0.1', port=8000)
|
||||
|
||||
# import os
|
||||
# pid_list = []
|
||||
# # fork limiting the cpu count - 1
|
||||
# for _ in range(1, 8):
|
||||
# pid = os.fork()
|
||||
# # n greater than 0 means parent process
|
||||
# if not pid > 0:
|
||||
# run_app()
|
||||
# break
|
||||
# pid_list.append(pid)
|
||||
|
||||
# run_app() # run app on the main process too :)
|
||||
|
||||
# # sigint everything to graceful shutdown
|
||||
# import signal
|
||||
# for pid in pid_list:
|
||||
# os.kill(pid, signal.SIGINT)
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
while true
|
||||
do
|
||||
wrk -t1 -c200 -d1 -H 'Connection: keep-alive' http://127.0.0.1:8000 > /dev/null
|
||||
done
|
|
@ -0,0 +1,83 @@
|
|||
import sys
|
||||
import io
|
||||
import time
|
||||
import datetime
|
||||
import socket
|
||||
import optparse
|
||||
|
||||
parser = optparse.OptionParser("usage: %prog [options]", add_help_option=False)
|
||||
parser.add_option("-h", "--host", dest="host", default='127.0.0.1', type="string")
|
||||
parser.add_option("-p", "--port", dest="port", default=3000, type="int")
|
||||
(opt, args) = parser.parse_args()
|
||||
|
||||
def get_request(path = r'/', host = '127.0.0.1', port = 3000):
|
||||
req = f'GET {path}' + r' HTTP/1.1' + '\r\n'
|
||||
req += f'Host: {host}:{port}\r\n'
|
||||
req += r'User-Agent: curl/7.66.0' + '\r\n'
|
||||
req += r'Accept: */*' + '\r\n'
|
||||
req += '\r\n'
|
||||
return req
|
||||
|
||||
payload_tiny = get_request(host = opt.host, port = opt.port)
|
||||
payload_tiny = payload_tiny.encode('utf-8')
|
||||
|
||||
def create_sock(timeout = 0.001):
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.settimeout(timeout)
|
||||
sock.connect((opt.host, opt.port))
|
||||
return sock
|
||||
|
||||
sock = create_sock()
|
||||
sock.sendall(payload_tiny)
|
||||
time.sleep(0.020)
|
||||
resp = sock.recv(4096)
|
||||
print('====== response ========')
|
||||
print(resp.decode('utf-8'))
|
||||
print('========================')
|
||||
sock.close()
|
||||
|
||||
start_time = datetime.datetime.now()
|
||||
test1_limit = start_time + datetime.timedelta(seconds = 1)
|
||||
test2_limit = test1_limit + datetime.timedelta(seconds = 10)
|
||||
|
||||
sock = create_sock()
|
||||
while True:
|
||||
if datetime.datetime.now() >= test1_limit:
|
||||
break
|
||||
sock.sendall(payload_tiny)
|
||||
try:
|
||||
resp = sock.recv(4096)
|
||||
except socket.timeout:
|
||||
pass
|
||||
|
||||
print(f'Test 1 completed!')
|
||||
sock.close()
|
||||
|
||||
req_num = 1000*1000
|
||||
payload_huge = payload_tiny * req_num
|
||||
#print(len(payload_huge))
|
||||
print(f'Run test 2 ...')
|
||||
totalsent = 0
|
||||
totalresp = b''
|
||||
sock = create_sock()
|
||||
while True:
|
||||
if datetime.datetime.now() >= test2_limit:
|
||||
print(f'Test 2: Timeout exceeded!')
|
||||
break
|
||||
try:
|
||||
rc = sock.send(payload_huge[totalsent:])
|
||||
if rc == 0:
|
||||
#raise RuntimeError("socket connection broken")
|
||||
pass
|
||||
totalsent += rc
|
||||
resp = sock.recv(65*1024)
|
||||
totalresp += resp
|
||||
except socket.timeout:
|
||||
pass
|
||||
except ConnectionResetError:
|
||||
print(f'totalsent = {totalsent}, totalrecv = {len(totalresp)}')
|
||||
print(f'LastResp: {totalresp[-256:]}')
|
||||
raise
|
||||
|
||||
sock.close()
|
||||
print("==== Test Finish =====")
|
|
@ -0,0 +1,12 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from quart import Quart
|
||||
|
||||
|
||||
app = Quart(__name__)
|
||||
|
||||
@app.get("/")
|
||||
async def plaintext():
|
||||
return "Hello, World!", {"Content-Type": "text/plain"}
|
||||
|
||||
# Quart perform really baddly for sure needs more optimizations, but socketify ASGI + PyPy performs better than uvicorn+httptools+gunicorn
|
|
@ -2,12 +2,15 @@ from socketify import App
|
|||
import os
|
||||
import multiprocessing
|
||||
import asyncio
|
||||
|
||||
def run_app():
|
||||
app = App(request_response_factory_max_items=200_000)
|
||||
async def home(res, req):
|
||||
res.end("Hello, World!")
|
||||
router = app.router()
|
||||
|
||||
@router.get("/")
|
||||
async def home(res, req):
|
||||
res.send(b"Hello, World!")
|
||||
|
||||
app.get("/", home)
|
||||
app.listen(
|
||||
8000,
|
||||
lambda config: print(
|
||||
|
@ -26,7 +29,7 @@ def create_fork():
|
|||
|
||||
|
||||
# fork limiting the cpu count - 1
|
||||
for i in range(1, multiprocessing.cpu_count()):
|
||||
create_fork()
|
||||
# for i in range(1, multiprocessing.cpu_count()):
|
||||
# create_fork()
|
||||
|
||||
run_app() # run app on the main process too :)
|
||||
|
|
|
@ -11,12 +11,22 @@
|
|||
<a href="https://github.com/cirospaciari/socketify.py/actions/workflows/macos_arm64.yml" target="_blank"><img src="https://github.com/cirospaciari/socketify.py/actions/workflows/macos_arm64.yml/badge.svg" /></a>
|
||||
<br/>
|
||||
<a href='https://github.com/cirospaciari/socketify.py'><img alt='GitHub Clones' src='https://img.shields.io/badge/dynamic/json?color=success&label=Clones&query=count&url=https://gist.githubusercontent.com/cirospaciari/2243d59951f4abe4fd2000f1e20bc561/raw/clone.json&logo=github'></a>
|
||||
<a href='https://pypi.org/project/socketify/' target="_blank"><img alt='PyPI Downloads' src='https://static.pepy.tech/personalized-badge/socketify?period=total&units=international_system&left_color=grey&right_color=brightgreen&left_text=Downloads'></a>
|
||||
<a href="https://github.com/sponsors/cirospaciari/" target="_blank"><img src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&link=https://github.com/sponsors/cirospaciari"/></a>
|
||||
|
||||
|
||||
<a href='https://discord.socketify.dev/' target="_blank"><img alt='Discord' src='https://img.shields.io/discord/1042529276219641906?label=Discord'></a>
|
||||
</p>
|
||||
<br/>
|
||||
<div align="center">
|
||||
<a href="https://github.com/cirospaciari/socketify.py">Github</a>
|
||||
<span> • </span>
|
||||
<a href="https://discord.socketify.dev/">Discord</a>
|
||||
<span> • </span>
|
||||
<a href="https://github.com/cirospaciari/socketify.py/issues">Issues</a>
|
||||
<span> • </span>
|
||||
<a href="https://github.com/cirospaciari/socketify.py/tree/main/examples">Examples</a>
|
||||
<br />
|
||||
</div>
|
||||
|
||||
<br/>
|
||||
Socketify.py is a reliable, high-performance Python web framework for building large-scale app backends and microservices.
|
||||
With no precedents websocket performance and a really fast HTTP server that can delivery encrypted TLS 1.3 quicker than most alternative servers can do even unencrypted, cleartext messaging.
|
||||
|
||||
|
@ -38,3 +48,4 @@ With no precedents websocket performance and a really fast HTTP server that can
|
|||
- [SSL](ssl.md)
|
||||
- [CLI, ASGI and WSGI](cli.md)
|
||||
- [API Reference](api.md)
|
||||
- [Examples](examples.md)
|
||||
|
|
|
@ -16,3 +16,4 @@
|
|||
- [SSL](ssl.md)
|
||||
- [CLI, ASGI and WSGI](cli.md)
|
||||
- [API Reference](api.md)
|
||||
- [Examples](examples.md)
|
||||
|
|
13
docs/api.md
13
docs/api.md
|
@ -38,11 +38,12 @@ class App:
|
|||
|
||||
```
|
||||
|
||||
## AppResponse
|
||||
## Response
|
||||
```python
|
||||
class AppResponse:
|
||||
class Response:
|
||||
def __init__(self, response, app):
|
||||
def cork(self, callback):
|
||||
def close(self):
|
||||
def set_cookie(self, name, value, options={}):
|
||||
def run_async(self, task):
|
||||
async def get_form_urlencoded(self, encoding="utf-8"):
|
||||
|
@ -61,7 +62,7 @@ class AppResponse:
|
|||
def get_proxied_remote_address_bytes(self):
|
||||
def get_proxied_remote_address(self):
|
||||
def cork_send(self, message: any, content_type: str = b'text/plain', status : str | bytes | int = b'200 OK', headers=None, end_connection=False):
|
||||
def send(self, message: any, content_type: str = b'text/plain', status : str | bytes | int = b'200 OK', headers=None, end_connection=False):
|
||||
def send(self, message: any = b"", content_type: str = b'text/plain', status : str | bytes | int = b'200 OK', headers=None, end_connection=False):
|
||||
def end(self, message, end_connection=False):
|
||||
def pause(self):
|
||||
def resume(self):
|
||||
|
@ -87,9 +88,9 @@ class AppResponse:
|
|||
def __del__(self):
|
||||
```
|
||||
|
||||
## AppRequest
|
||||
## Request
|
||||
```python
|
||||
class AppRequest:
|
||||
class Request:
|
||||
def __init__(self, request, app):
|
||||
def get_cookie(self, name):
|
||||
def get_url(self):
|
||||
|
@ -229,3 +230,5 @@ class MiddlewareRouter:
|
|||
def trace(self, path, handler):
|
||||
def any(self, path, handler):
|
||||
```
|
||||
|
||||
### Next [Examples](examples.md)
|
||||
|
|
|
@ -30,7 +30,7 @@ Options:
|
|||
--ws-close-on-backpressure-limit BOOLEAN Close connections that hits maximum backpressure [default: False]
|
||||
--lifespan [auto|on|off] Lifespan implementation. [default: auto]
|
||||
--interface [auto|asgi|asgi3|wsgi|ssgi|socketify] Select ASGI (same as ASGI3), ASGI3, WSGI or SSGI as the application interface. [default: auto]
|
||||
--disable-listen-log BOOLEAN Disable log when start listenning [default: False]
|
||||
--disable-listen-log BOOLEAN Disable log when start listening [default: False]
|
||||
--version or -v Display the socketify.py version and exit.
|
||||
--ssl-keyfile TEXT SSL key file
|
||||
--ssl-certfile TEXT SSL certificate file
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
## 📚 Examples
|
||||
|
||||
All examples are located in the [`examples`](https://github.com/cirospaciari/socketify.py/tree/main/examples) directory.
|
||||
|
||||
### 🚀 Getting Started
|
||||
|
||||
- [`hello_world.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/hello_world.py) - Basic HTTP server setup
|
||||
- [`hello_world_cli.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/hello_world_cli.py) - Command-line interface example
|
||||
- [`hello_world_cli_ws.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/hello_world_cli_ws.py) - CLI with WebSocket support
|
||||
- [`hello_world_unix_domain.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/hello_world_unix_domain.py) - Unix domain socket example
|
||||
|
||||
### 🔒 Security & HTTPS
|
||||
|
||||
- [`https.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/https.py) - HTTPS server with SSL/TLS configuration
|
||||
|
||||
### 🌐 WebSocket Examples
|
||||
|
||||
- [`websockets.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/websockets.py) - Basic WebSocket implementation
|
||||
- [`ws_close_connection.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/ws_close_connection.py) - WebSocket connection management
|
||||
- [`chat/`](https://github.com/cirospaciari/socketify.py/tree/main/examples/chat) - Real-time chat application
|
||||
- [`broadcast.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/broadcast.py) - Broadcasting messages to multiple clients
|
||||
- [`backpressure.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/backpressure.py) - Handling WebSocket backpressure
|
||||
|
||||
### ⚙️ Middleware & Routing
|
||||
|
||||
- [`middleware.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/middleware.py) - Basic middleware implementation
|
||||
- [`middleware_async.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/middleware_async.py) - Asynchronous middleware
|
||||
- [`middleware_sync.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/middleware_sync.py) - Synchronous middleware
|
||||
- [`middleware_router.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/middleware_router.py) - Router-based middleware
|
||||
- [`router_and_basics.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/router_and_basics.py) - Routing fundamentals
|
||||
|
||||
### 🔄 Async/Sync Programming
|
||||
|
||||
- [`async.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/async.py) - Asynchronous request handling
|
||||
- [`upgrade.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/upgrade.py) - Protocol upgrade examples
|
||||
- [`upgrade_async.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/upgrade_async.py) - Asynchronous protocol upgrades
|
||||
|
||||
### 📁 File Handling & Static Content
|
||||
|
||||
- [`static_files.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/static_files.py) - Serving static files
|
||||
- [`file_stream.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/file_stream.py) - File streaming capabilities
|
||||
- [`upload_or_post.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/upload_or_post.py) - File uploads and POST data handling
|
||||
|
||||
### 🎨 Template Engines
|
||||
|
||||
- [`template_jinja2.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/template_jinja2.py) - Jinja2 template integration
|
||||
- [`template_mako.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/template_mako.py) - Mako template integration
|
||||
- [`templates/`](https://github.com/cirospaciari/socketify.py/tree/main/examples/templates) - Template examples and resources
|
||||
|
||||
### 🛠️ Advanced Features
|
||||
|
||||
- [`custom_json_serializer.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/custom_json_serializer.py) - Custom JSON serialization
|
||||
- [`http_request_cache.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/http_request_cache.py) - HTTP request caching
|
||||
- [`proxy.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/proxy.py) - Proxy server implementation
|
||||
- [`automatic_port_selection.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/automatic_port_selection.py) - Dynamic port selection
|
||||
|
||||
### 🔧 Server Configuration
|
||||
|
||||
- [`listen_options.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/listen_options.py) - Server listening options
|
||||
- [`graceful_shutdown.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/graceful_shutdown.py) - Graceful server shutdown
|
||||
- [`forks.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/forks.py) - Multi-process server setup
|
||||
|
||||
### 📊 GraphQL Integration
|
||||
|
||||
- [`graphiql.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/graphiql.py) - GraphiQL interface setup
|
||||
- [`graphiql_raw.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/graphiql_raw.py) - Raw GraphQL implementation
|
||||
|
||||
### 🐳 Development & Deployment
|
||||
|
||||
- [`docker/`](https://github.com/cirospaciari/socketify.py/tree/main/examples/docker) - Docker containerization examples
|
||||
- [`requirements.txt`](https://github.com/cirospaciari/socketify.py/tree/main/examples/requirements.txt) - Example dependencies
|
||||
|
||||
### 🛡️ Error Handling & Logging
|
||||
|
||||
- [`error_handler.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/error_handler.py) - Error handling strategies
|
||||
- [`better_logging.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/better_logging.py) - Advanced logging setup
|
||||
- [`not_found.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/not_found.py) - Custom 404 error pages
|
||||
|
||||
### 🔨 Utilities & Helpers
|
||||
|
||||
- [`helpers/`](https://github.com/cirospaciari/socketify.py/tree/main/examples/helpers) - Utility functions and helper modules
|
|
@ -8,10 +8,14 @@ Hello world app
|
|||
```python
|
||||
from socketify import App
|
||||
|
||||
app = App()
|
||||
app.get("/", lambda res, req: res.end("Hello World socketify from Python!"))
|
||||
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
|
||||
app.run()
|
||||
def make_app(app: App):
|
||||
app.get("/", lambda res, req: res.end("Hello World socketify from Python!"))
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = App()
|
||||
make_app(app)
|
||||
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
|
||||
app.run()
|
||||
```
|
||||
> This example just show how intuitive is to start an simple hello world app.
|
||||
|
||||
|
@ -19,10 +23,13 @@ SSL version sample
|
|||
``` python
|
||||
from socketify import App, AppOptions
|
||||
|
||||
app = App(AppOptions(key_file_name="./misc/key.pem", cert_file_name="./misc/cert.pem", passphrase="1234"))
|
||||
app.get("/", lambda res, req: res.end("Hello World socketify from Python!"))
|
||||
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
|
||||
app.run()
|
||||
def make_app(app):
|
||||
app.get("/", lambda res, req: res.end("Hello World socketify from Python!"))
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = App(AppOptions(key_file_name="./misc/key.pem", cert_file_name="./misc/cert.pem", passphrase="1234"))
|
||||
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
|
||||
app.run()
|
||||
```
|
||||
|
||||
> We have a lot of SSL options, but this is the most common you can see all the options in the [API Reference](api.md)
|
||||
|
@ -39,19 +46,24 @@ def ws_message(ws, message, opcode):
|
|||
#Ok is false if backpressure was built up, wait for drain
|
||||
ok = ws.send(message, opcode)
|
||||
|
||||
app = App()
|
||||
app.ws("/*", {
|
||||
'compression': CompressOptions.SHARED_COMPRESSOR,
|
||||
'max_payload_length': 16 * 1024 * 1024,
|
||||
'idle_timeout': 12,
|
||||
'open': ws_open,
|
||||
'message': ws_message,
|
||||
'drain': lambda ws: print('WebSocket backpressure: %i' % ws.get_buffered_amount()),
|
||||
'close': lambda ws, code, message: print('WebSocket closed')
|
||||
})
|
||||
app.any("/", lambda res,req: res.end("Nothing to see here!'"))
|
||||
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % (config.port)))
|
||||
app.run()
|
||||
def make_app(app):
|
||||
app.ws("/*", {
|
||||
'compression': CompressOptions.SHARED_COMPRESSOR,
|
||||
'max_payload_length': 16 * 1024 * 1024,
|
||||
'idle_timeout': 12,
|
||||
'open': ws_open,
|
||||
'message': ws_message,
|
||||
'drain': lambda ws: print('WebSocket backpressure: %i' % ws.get_buffered_amount()),
|
||||
'close': lambda ws, code, message: print('WebSocket closed')
|
||||
})
|
||||
app.any("/", lambda res,req: res.end("Nothing to see here!'"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = App()
|
||||
make_app(app)
|
||||
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % (config.port)))
|
||||
app.run()
|
||||
```
|
||||
|
||||
> We can have multiple routes for WebSockets, but in this example we just get one for anything we need, adding an option of compression using SHARED_COMPRESSOR, max_payload_length of 1mb and an idle timeout of 12s just to show some most commonly used features you can see all these options in the [API Reference](api.md)
|
||||
|
|
|
@ -102,7 +102,7 @@ async def async_xablau(res, req):
|
|||
# this can be async no problems
|
||||
def on_error(error, res, req):
|
||||
# here you can log properly the error and do a pretty response to your clients
|
||||
print("Somethind goes %s" % str(error))
|
||||
print("Something goes %s" % str(error))
|
||||
# response and request can be None if the error is in an async function
|
||||
if res != None:
|
||||
# if response exists try to send something
|
||||
|
|
|
@ -3,6 +3,52 @@ WebSocket "routes" are registered similarly, but not identically.
|
|||
|
||||
Every websocket route has the same pattern and pattern matching as for Http, but instead of one single callback you have a whole set of them, here's an example:
|
||||
|
||||
Configuration details, notes:
|
||||
- *idle_timeout*: number of seconds of inactivity before client is disconnected. If set to 0, no policy is enforced (connections can be stale).
|
||||
- *open*: callback function for websocket connection being open
|
||||
```python
|
||||
def on_open(ws : WebSocket):
|
||||
"""
|
||||
ws: WebSocket - websocket connection
|
||||
"""
|
||||
...
|
||||
```
|
||||
- *close*: callback function for websocket connection closed
|
||||
```python
|
||||
def on_close(ws: WebSocket, code: int, msg: Union[bytes, str]):
|
||||
"""
|
||||
ws: WebSocket
|
||||
websocket connection
|
||||
code: int
|
||||
exit code from client
|
||||
msg: byte, str
|
||||
exit message
|
||||
"""
|
||||
...
|
||||
```
|
||||
- *upgrade*: callback function to upgrade socket connection details
|
||||
```python
|
||||
def on_upgrade(res: Response, req: Request, socket_context):
|
||||
"""
|
||||
res: Response
|
||||
req: Request
|
||||
"""
|
||||
...
|
||||
```
|
||||
- *message*: callback function for websocket message received
|
||||
```python
|
||||
def on_message(ws: WebSocket, msg: Union[bytes, str], opcode: OpCode):
|
||||
"""
|
||||
ws: WebSocket
|
||||
msg: bytes, str
|
||||
opcode: OpCode
|
||||
"""
|
||||
```
|
||||
- *drain*: in the event of backpressure, policy to drain ws buffer
|
||||
```python
|
||||
def on_drain(ws: WebSocket):
|
||||
...
|
||||
```
|
||||
```python
|
||||
app = App()
|
||||
app.ws(
|
||||
|
@ -11,10 +57,11 @@ app.ws(
|
|||
"compression": CompressOptions.SHARED_COMPRESSOR,
|
||||
"max_payload_length": 16 * 1024 * 1024,
|
||||
"idle_timeout": 12,
|
||||
"open": ws_open,
|
||||
"message": ws_message,
|
||||
'drain': lambda ws: print(f'WebSocket backpressure: {ws.get_buffered_amount()}'),
|
||||
"close": lambda ws, code, message: print("WebSocket closed"),
|
||||
"open": on_open,
|
||||
"message": on_message,
|
||||
"close": on_close,
|
||||
"upgrade": on_upgrade,
|
||||
'drain': on_drain,
|
||||
"subscription": lambda ws, topic, subscriptions, subscriptions_before: print(f'subscription/unsubscription on topic {topic} {subscriptions} {subscriptions_before}'),
|
||||
},
|
||||
)
|
||||
|
@ -24,6 +71,57 @@ You should use the provided user data feature to store and attach any per-socket
|
|||
|
||||
If you want to create something more elaborate you could have the user data hold a pointer to some dynamically allocated memory block that keeps a boolean whether the WebSocket is still valid or not. Sky is the limit here.
|
||||
|
||||
In order to do so, use the `upgrade` callback configuration in the `app.ws` settings.
|
||||
|
||||
Example:
|
||||
```python
|
||||
from socketify import App, WebSocket, OpCode
|
||||
app = App()
|
||||
|
||||
ID = 0
|
||||
|
||||
def on_open(ws: WebSocket):
|
||||
user_data = ws.get_user_data()
|
||||
print('ws %s connected' % user_data['user_id'])
|
||||
ws.send('Hello, world!')
|
||||
|
||||
def on_upgrade(res, req, socket_context):
|
||||
global ID
|
||||
ID += 1
|
||||
key = req.get_header("sec-websocket-key")
|
||||
protocol = req.get_header("sec-websocket-protocol")
|
||||
extensions = req.get_header("sec-websocket-extensions")
|
||||
user_data=dict(user_id=ID)
|
||||
res.upgrade(key, protocol, extensions, socket_context, user_data)
|
||||
|
||||
def on_message(ws: WebSocket, msg: str, opcode: OpCode):
|
||||
user_data = ws.get_user_data()
|
||||
print('ws %s: %s' % (user_data['user_id'], msg))
|
||||
|
||||
def on_close(ws, code, msg):
|
||||
user_data = ws.get_user_data()
|
||||
print('ws %s closed' % user_data['user_id'])
|
||||
|
||||
def on_drain(ws: WebSocket):
|
||||
user_data = ws.get_user_data()
|
||||
print('ws %s backpressure: %s' % (user_data['user_id'], ws.get_buffered_amount()))
|
||||
|
||||
app.ws(
|
||||
"/*",
|
||||
{
|
||||
"compression": CompressOptions.SHARED_COMPRESSOR,
|
||||
"max_payload_length": 16 * 1024 * 1024,
|
||||
"idle_timeout": 12,
|
||||
"open": on_open,
|
||||
"message": on_message,
|
||||
"close": on_close,
|
||||
"upgrade": on_upgrade,
|
||||
"drain": on_drain,
|
||||
"subscription": lambda ws, topic, subscriptions, subscriptions_before: print(f'subscription/unsubscription on topic {topic} {subscriptions} {subscriptions_before}'),
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
## WebSockets are valid from open to close
|
||||
All given WebSocket are guaranteed to live from open event (where you got your WebSocket) until close event is called.
|
||||
Message events will never emit outside of open/close. Calling ws.close or ws.end will immediately call the close handler.
|
||||
|
|
|
@ -2,7 +2,6 @@ from socketify import App
|
|||
import os
|
||||
import multiprocessing
|
||||
|
||||
|
||||
def run_app():
|
||||
app = App()
|
||||
app.get("/", lambda res, req: res.end("Hello, World!"))
|
||||
|
@ -16,15 +15,19 @@ def run_app():
|
|||
app.run()
|
||||
|
||||
|
||||
def create_fork():
|
||||
n = os.fork()
|
||||
# n greater than 0 means parent process
|
||||
if not n > 0:
|
||||
run_app()
|
||||
|
||||
|
||||
pid_list = []
|
||||
# fork limiting the cpu count - 1
|
||||
for i in range(1, multiprocessing.cpu_count()):
|
||||
create_fork()
|
||||
for _ in range(1, multiprocessing.cpu_count()):
|
||||
pid = os.fork()
|
||||
# n greater than 0 means parent process
|
||||
if not pid > 0:
|
||||
run_app()
|
||||
break
|
||||
pid_list.append(pid)
|
||||
|
||||
run_app() # run app on the main process too :)
|
||||
|
||||
# sigint everything to graceful shutdown
|
||||
import signal
|
||||
for pid in pid_list:
|
||||
os.kill(pid, signal.SIGINT)
|
|
@ -0,0 +1,21 @@
|
|||
from streaming_form_data import StreamingFormDataParser
|
||||
from socketify import Response
|
||||
def get_formdata(res: Response, parser: StreamingFormDataParser):
|
||||
_dataFuture = res.app.loop.create_future()
|
||||
|
||||
def is_aborted(res):
|
||||
res.aborted = True
|
||||
try:
|
||||
if not _dataFuture.done():
|
||||
_dataFuture.set_result(parser)
|
||||
except:
|
||||
pass
|
||||
|
||||
def get_chunks(res, chunk, is_end):
|
||||
parser.data_received(chunk)
|
||||
if is_end:
|
||||
_dataFuture.set_result(parser)
|
||||
|
||||
res.on_aborted(is_aborted)
|
||||
res.on_data(get_chunks)
|
||||
return _dataFuture
|
|
@ -1,6 +1,6 @@
|
|||
import strawberry
|
||||
import strawberry.utils.graphiql
|
||||
|
||||
from io import BytesIO
|
||||
|
||||
def graphiql_from(Query, Mutation=None):
|
||||
if Mutation:
|
||||
|
@ -8,32 +8,45 @@ def graphiql_from(Query, Mutation=None):
|
|||
else:
|
||||
schema = strawberry.Schema(Query)
|
||||
|
||||
async def post(res, req):
|
||||
def post(res, req):
|
||||
# we can pass whatever we want to context, query, headers or params, cookies etc
|
||||
context_value = req.preserve()
|
||||
|
||||
# get all incoming data and parses as json
|
||||
body = await res.get_json()
|
||||
buffer = BytesIO()
|
||||
def on_data(res, chunk, is_end):
|
||||
buffer.write(chunk)
|
||||
if is_end:
|
||||
try:
|
||||
body = res.app._json_serializer.loads(buffer.getvalue().decode("utf-8"))
|
||||
res.run_async(graph_ql(res, body, context_value))
|
||||
except Exception as err:
|
||||
res.app.trigger_error(err, res, None)
|
||||
|
||||
query = body["query"]
|
||||
variables = body.get("variables", None)
|
||||
root_value = body.get("root_value", None)
|
||||
operation_name = body.get("operation_name", None)
|
||||
res.grab_aborted_handler()
|
||||
res.on_data(on_data)
|
||||
|
||||
data = await schema.execute(
|
||||
query,
|
||||
variables,
|
||||
context_value,
|
||||
root_value,
|
||||
operation_name,
|
||||
)
|
||||
async def graph_ql(res, body, context_value):
|
||||
query = body["query"]
|
||||
|
||||
res.cork_end(
|
||||
{
|
||||
"data": (data.data),
|
||||
**({"errors": data.errors} if data.errors else {}),
|
||||
**({"extensions": data.extensions} if data.extensions else {}),
|
||||
}
|
||||
)
|
||||
|
||||
variables = body.get("variables", None)
|
||||
root_value = body.get("rootValue", None)
|
||||
operation_name = body.get("operationName", None)
|
||||
|
||||
data = await schema.execute(
|
||||
query,
|
||||
variables,
|
||||
context_value,
|
||||
root_value,
|
||||
operation_name,
|
||||
)
|
||||
|
||||
res.cork_send(
|
||||
{
|
||||
"data": (data.data),
|
||||
**({"errors": data.errors} if data.errors else {}),
|
||||
**({"extensions": data.extensions} if data.extensions else {}),
|
||||
}
|
||||
)
|
||||
|
||||
return post
|
||||
|
|
|
@ -9,9 +9,10 @@ app = App(
|
|||
)
|
||||
app.get("/", lambda res, req: res.end("Hello World socketify from Python!"))
|
||||
app.listen(
|
||||
3000,
|
||||
54321,
|
||||
lambda config: print("Listening on port https://localhost:%d now\n" % config.port),
|
||||
)
|
||||
app.run()
|
||||
|
||||
# mkdir misc
|
||||
# openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -passout pass:1234 -keyout ./misc/key.pem -out ./misc/cert.pem
|
||||
|
|
|
@ -6,7 +6,7 @@ from socketify import App
|
|||
def middleware(*functions):
|
||||
def middleware_route(res, req):
|
||||
data = None
|
||||
# cicle to all middlewares
|
||||
# circle to all middlewares
|
||||
for function in functions:
|
||||
# call middlewares
|
||||
data = function(res, req, data)
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
# using oha -c 400 -z 5s http://localhost:3000/
|
||||
|
||||
# nginx - try_files - 77630.15 req/s
|
||||
# pypy3 - socketify static - 15839.22 req/s
|
||||
# python3 - socketify static - 8294.96 req/s
|
||||
# pypy3 - socketify static - 16797.30 req/s
|
||||
# python3 - socketify static - 10140.19 req/s
|
||||
# node.js - @fastify/static - 5437.16 req/s
|
||||
# node.js - express.static - 4077.49 req/s
|
||||
# python3 - socketify static_aiofile - 2390.96 req/s
|
||||
|
|
|
@ -78,6 +78,69 @@ async def upload_multiple(res, req):
|
|||
# We respond when we are done
|
||||
res.cork_end("Thanks for the data!")
|
||||
|
||||
def upload_formdata(res, req):
|
||||
# using streaming_form_data package for parsing
|
||||
from streaming_form_data import StreamingFormDataParser
|
||||
from streaming_form_data.targets import ValueTarget, FileTarget
|
||||
|
||||
print(f"Posted to {req.get_url()}")
|
||||
parser = StreamingFormDataParser(headers=req.get_headers())
|
||||
name = ValueTarget()
|
||||
parser.register('name', name)
|
||||
file = FileTarget('/tmp/file')
|
||||
file2 = FileTarget('/tmp/file2')
|
||||
parser.register('file', file)
|
||||
parser.register('file2', file2)
|
||||
|
||||
|
||||
def on_data(res, chunk, is_end):
|
||||
parser.data_received(chunk)
|
||||
if is_end:
|
||||
res.cork(on_finish)
|
||||
|
||||
|
||||
def on_finish(res):
|
||||
print(name.value)
|
||||
|
||||
print(file.multipart_filename)
|
||||
print(file.multipart_content_type)
|
||||
|
||||
print(file2.multipart_filename)
|
||||
print(file2.multipart_content_type)
|
||||
|
||||
res.end("Thanks for the data!")
|
||||
|
||||
res.on_data(on_data)
|
||||
|
||||
|
||||
async def upload_formhelper(res, req):
|
||||
# using streaming_form_data package for parsing + helper
|
||||
from streaming_form_data import StreamingFormDataParser
|
||||
from streaming_form_data.targets import ValueTarget, FileTarget
|
||||
from helpers.form_data import get_formdata
|
||||
|
||||
|
||||
print(f"Posted to {req.get_url()}")
|
||||
parser = StreamingFormDataParser(headers=req.get_headers())
|
||||
name = ValueTarget()
|
||||
parser.register('name', name)
|
||||
file = FileTarget('/tmp/file')
|
||||
file2 = FileTarget('/tmp/file2')
|
||||
parser.register('file', file)
|
||||
parser.register('file2', file2)
|
||||
|
||||
await get_formdata(res, parser)
|
||||
|
||||
print(name.value)
|
||||
|
||||
print(file.multipart_filename)
|
||||
print(file.multipart_content_type)
|
||||
|
||||
print(file2.multipart_filename)
|
||||
print(file2.multipart_content_type)
|
||||
|
||||
res.cork_end("Thanks for the data!")
|
||||
|
||||
|
||||
app = App()
|
||||
app.post("/", upload)
|
||||
|
@ -86,6 +149,8 @@ app.post("/json", upload_json)
|
|||
app.post("/text", upload_text)
|
||||
app.post("/urlencoded", upload_urlencoded)
|
||||
app.post("/multiple", upload_multiple)
|
||||
app.post("/formdata", upload_formdata)
|
||||
app.post("/formdata2", upload_formhelper)
|
||||
|
||||
app.any("/*", lambda res, _: res.write_status(404).end("Not Found"))
|
||||
app.listen(
|
||||
|
|
|
@ -4,13 +4,14 @@ build-backend = "setuptools.build_meta"
|
|||
|
||||
[project]
|
||||
name = "socketify"
|
||||
version = "0.0.9"
|
||||
version = "0.0.31"
|
||||
dynamic = ["dependencies"]
|
||||
authors = [
|
||||
{ name="Ciro Spaciari", email="ciro.spaciari@gmail.com" },
|
||||
]
|
||||
description = "Bringing WebSockets, Http/Https High Performance servers for PyPy3 and Python3"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.7"
|
||||
requires-python = ">=3.8"
|
||||
classifiers = [
|
||||
"Programming Language :: Python :: 3",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
|
|
8
setup.py
8
setup.py
|
@ -1,8 +1,8 @@
|
|||
import sys
|
||||
|
||||
vi = sys.version_info
|
||||
if vi < (3, 7):
|
||||
raise RuntimeError("socketify requires Python 3.7 or greater")
|
||||
if vi < (3, 8):
|
||||
raise RuntimeError("socketify requires Python 3.8 or greater")
|
||||
|
||||
# if sys.platform in ('win32', 'cygwin', 'cli'):
|
||||
# raise RuntimeError('socketify does not support Windows at the moment')
|
||||
|
@ -58,7 +58,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
|
|||
|
||||
setuptools.setup(
|
||||
name="socketify",
|
||||
version="0.0.9",
|
||||
version="0.0.31",
|
||||
platforms=["any"],
|
||||
author="Ciro Spaciari",
|
||||
author_email="ciro.spaciari@gmail.com",
|
||||
|
@ -88,7 +88,7 @@ setuptools.setup(
|
|||
"./native/*/*/*",
|
||||
]
|
||||
},
|
||||
python_requires=">=3.7",
|
||||
python_requires=">=3.8",
|
||||
install_requires=["cffi>=1.0", "setuptools>=58.1.0"],
|
||||
has_ext_modules=lambda: True,
|
||||
cmdclass={}, # cmdclass={'sdist': Prepare, 'build_ext': Makefile},
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import asyncio
|
||||
|
||||
from .dataclasses import AppListenOptions, AppOptions
|
||||
from .tasks import TaskFactory, create_task, RequestTask
|
||||
from .socketify import (
|
||||
App,
|
||||
AppOptions,
|
||||
AppListenOptions,
|
||||
OpCode,
|
||||
SendStatus,
|
||||
CompressOptions,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from socketify import App, OpCode, Loop
|
||||
from socketify import App, OpCode
|
||||
from queue import SimpleQueue
|
||||
from .native import lib, ffi
|
||||
from .tasks import create_task, create_task_with_factory
|
||||
from .tasks import create_task, TaskFactory
|
||||
import os
|
||||
import platform
|
||||
import sys
|
||||
|
@ -10,7 +10,15 @@ import uuid
|
|||
import asyncio
|
||||
|
||||
is_pypy = platform.python_implementation() == "PyPy"
|
||||
@ffi.callback("void(uws_res_t*, void*)")
|
||||
def asgi_on_abort_handler(res, user_data):
|
||||
ctx = ffi.from_handle(user_data)
|
||||
ctx.aborted = True
|
||||
ctx.loop.is_idle = False
|
||||
|
||||
if ctx.abort_future is not None:
|
||||
ctx.abort_future.set_result(True)
|
||||
ctx.abort_future = None
|
||||
|
||||
async def task_wrapper(task):
|
||||
try:
|
||||
|
@ -25,7 +33,6 @@ async def task_wrapper(task):
|
|||
|
||||
EMPTY_RESPONSE = {"type": "http.request", "body": b"", "more_body": False}
|
||||
|
||||
|
||||
@ffi.callback("void(uws_websocket_t*, const char*, size_t, uws_opcode_t, void*)")
|
||||
def ws_message(ws, message, length, opcode, user_data):
|
||||
socket_data = ffi.from_handle(user_data)
|
||||
|
@ -50,10 +57,11 @@ def ws_open(ws, user_data):
|
|||
|
||||
|
||||
@ffi.callback(
|
||||
"void(int, uws_res_t*, socketify_asgi_ws_data, uws_socket_context_t* socket, void*, bool*)"
|
||||
"void(int, uws_res_t*, socketify_asgi_ws_data, uws_socket_context_t* socket, void*)"
|
||||
)
|
||||
def ws_upgrade(ssl, response, info, socket_context, user_data, aborted):
|
||||
def ws_upgrade(ssl, response, info, socket_context, user_data):
|
||||
app = ffi.from_handle(user_data)
|
||||
app.server.loop.is_idle = False
|
||||
headers = []
|
||||
next_header = info.header_list
|
||||
while next_header != ffi.NULL:
|
||||
|
@ -83,6 +91,7 @@ def ws_upgrade(ssl, response, info, socket_context, user_data, aborted):
|
|||
extensions = ffi.unpack(info.extensions, info.extensions_size).decode("utf8")
|
||||
compress = app.ws_compression
|
||||
ws = ASGIWebSocket(app.server.loop)
|
||||
lib.uws_res_on_aborted(ssl, response, asgi_on_abort_handler, ws._ptr)
|
||||
|
||||
scope = {
|
||||
"type": "websocket",
|
||||
|
@ -98,7 +107,7 @@ def ws_upgrade(ssl, response, info, socket_context, user_data, aborted):
|
|||
"root_path": "",
|
||||
"path": url.decode("utf8"),
|
||||
"raw_path": url,
|
||||
"query_string": ffi.unpack(info.query_string, info.query_string_size),
|
||||
"query_string": ffi.unpack(info.query_string, info.query_string_size)[1:],
|
||||
"headers": headers,
|
||||
"subprotocols": [protocol] if protocol else [],
|
||||
"extensions": {
|
||||
|
@ -109,8 +118,9 @@ def ws_upgrade(ssl, response, info, socket_context, user_data, aborted):
|
|||
}
|
||||
|
||||
async def send(options):
|
||||
if bool(aborted[0]):
|
||||
if ws.aborted:
|
||||
return False
|
||||
ws.loop.is_idle = False
|
||||
type = options["type"]
|
||||
if type == "websocket.send":
|
||||
data = options.get("bytes", None)
|
||||
|
@ -238,6 +248,7 @@ def ws_upgrade(ssl, response, info, socket_context, user_data, aborted):
|
|||
@ffi.callback("void(uws_res_t*, const char*, size_t, bool, void*)")
|
||||
def asgi_on_data_handler(res, chunk, chunk_length, is_end, user_data):
|
||||
data_response = ffi.from_handle(user_data)
|
||||
data_response.loop.is_idle = False
|
||||
data_response.is_end = bool(is_end)
|
||||
more_body = not data_response.is_end
|
||||
result = {
|
||||
|
@ -259,6 +270,22 @@ class ASGIDataQueue:
|
|||
self.is_end = False
|
||||
self.next_data_future = loop.create_future()
|
||||
|
||||
class ASGIContext:
|
||||
def __init__(self, ssl, response, loop):
|
||||
self._ptr = ffi.new_handle(self)
|
||||
self.aborted = False
|
||||
self.sended_empty = False
|
||||
self.data_queue = None
|
||||
self.ssl = ssl
|
||||
self.response = response
|
||||
self.loop = loop
|
||||
self.abort_future = None
|
||||
|
||||
async def wait_disconnect(self):
|
||||
if not self.aborted:
|
||||
if self.abort_future is None:
|
||||
self.abort_future = self.loop.create_future()
|
||||
await self.abort_future
|
||||
|
||||
class ASGIWebSocket:
|
||||
def __init__(self, loop):
|
||||
|
@ -273,6 +300,14 @@ class ASGIWebSocket:
|
|||
self._message = None
|
||||
self._ptr = ffi.new_handle(self)
|
||||
self.unregister = None
|
||||
self.aborted = False
|
||||
self.abort_future = None
|
||||
|
||||
async def wait_disconnect(self):
|
||||
if not self.aborted:
|
||||
if self.abort_future is None:
|
||||
self.abort_future = self.loop.create_future()
|
||||
await self.abort_future
|
||||
|
||||
def accept(self):
|
||||
self.accept_future = self.loop.create_future()
|
||||
|
@ -405,9 +440,10 @@ def uws_asgi_corked_403_handler(res, user_data):
|
|||
lib.uws_res_end_without_body(ssl, res, 0)
|
||||
|
||||
|
||||
@ffi.callback("void(int, uws_res_t*, socketify_asgi_data, void*, bool*)")
|
||||
def asgi(ssl, response, info, user_data, aborted):
|
||||
@ffi.callback("void(int, uws_res_t*, socketify_asgi_data, void*)")
|
||||
def asgi(ssl, response, info, user_data):
|
||||
app = ffi.from_handle(user_data)
|
||||
app.server.loop.is_idle = False
|
||||
|
||||
headers = []
|
||||
next_header = info.header_list
|
||||
|
@ -427,7 +463,7 @@ def asgi(ssl, response, info, user_data, aborted):
|
|||
"http_version": "1.1",
|
||||
"server": (app.SERVER_HOST, app.SERVER_PORT),
|
||||
"client": (
|
||||
ffi.unpack(info.remote_address, info.remote_address_size).decode("utf8"),
|
||||
None if info.remote_address == ffi.NULL else ffi.unpack(info.remote_address, info.remote_address_size).decode("utf8"),
|
||||
None,
|
||||
),
|
||||
"scheme": app.SERVER_SCHEME,
|
||||
|
@ -435,18 +471,25 @@ def asgi(ssl, response, info, user_data, aborted):
|
|||
"root_path": "",
|
||||
"path": url.decode("utf8"),
|
||||
"raw_path": url,
|
||||
"query_string": ffi.unpack(info.query_string, info.query_string_size),
|
||||
"query_string": ffi.unpack(info.query_string, info.query_string_size)[1:],
|
||||
"headers": headers,
|
||||
|
||||
}
|
||||
loop = app.server.loop
|
||||
ctx = ASGIContext(ssl, response, loop)
|
||||
if bool(info.has_content):
|
||||
data_queue = ASGIDataQueue(app.server.loop)
|
||||
data_queue = ASGIDataQueue(loop)
|
||||
lib.uws_res_on_data(ssl, response, asgi_on_data_handler, data_queue._ptr)
|
||||
else:
|
||||
data_queue = None
|
||||
ctx.data_queue = data_queue
|
||||
|
||||
lib.uws_res_on_aborted(ssl, response, asgi_on_abort_handler, ctx._ptr)
|
||||
|
||||
async def receive():
|
||||
if bool(aborted[0]):
|
||||
if ctx.aborted:
|
||||
return {"type": "http.disconnect"}
|
||||
|
||||
ctx.loop.is_idle = False
|
||||
data_queue = ctx.data_queue
|
||||
if data_queue:
|
||||
if data_queue.queue.empty():
|
||||
if not data_queue.is_end:
|
||||
|
@ -458,13 +501,24 @@ def asgi(ssl, response, info, user_data, aborted):
|
|||
else:
|
||||
return data_queue.queue.get(False) # consume queue
|
||||
|
||||
# no more body, just empty
|
||||
return EMPTY_RESPONSE
|
||||
# no more body, just EMPTY RESPONSE
|
||||
if not ctx.sended_empty:
|
||||
ctx.sended_empty = True
|
||||
return EMPTY_RESPONSE
|
||||
|
||||
# already sended empty body so wait for aborted request
|
||||
if not ctx.aborted:
|
||||
await ctx.wait_disconnect()
|
||||
return {"type": "http.disconnect"}
|
||||
|
||||
async def send(options):
|
||||
if bool(aborted[0]):
|
||||
if ctx.aborted:
|
||||
return False
|
||||
|
||||
ctx.loop.is_idle = False
|
||||
type = options["type"]
|
||||
ssl = ctx.ssl
|
||||
response = ctx.response
|
||||
if type == "http.response.start":
|
||||
# can also be more native optimized to do it in one GIL call
|
||||
# try socketify_res_write_int_status_with_headers and create and socketify_res_cork_write_int_status_with_headers
|
||||
|
@ -494,6 +548,12 @@ def asgi(ssl, response, info, user_data, aborted):
|
|||
data = message.encode("utf-8")
|
||||
lib.socketify_res_cork_end(ssl, response, data, len(data), 0)
|
||||
|
||||
|
||||
if ctx.abort_future is not None:
|
||||
ctx.aborted = True
|
||||
ctx.abort_future.set_result(False)
|
||||
ctx.abort_future = None
|
||||
|
||||
return True
|
||||
return False
|
||||
|
||||
|
@ -513,8 +573,8 @@ class _ASGI:
|
|||
self.server = App(options, task_factory_max_items=0)
|
||||
self.SERVER_PORT = None
|
||||
self.SERVER_HOST = ""
|
||||
self.SERVER_SCHEME = "https" if self.server.options else "http"
|
||||
self.SERVER_WS_SCHEME = "wss" if self.server.options else "ws"
|
||||
self.SERVER_SCHEME = "https" if options and options.cert_file_name is not None else "http"
|
||||
self.SERVER_WS_SCHEME = "wss" if options and options.cert_file_name is not None else "ws"
|
||||
self.task_factory_max_items = task_factory_max_items
|
||||
self.lifespan = lifespan
|
||||
|
||||
|
@ -523,38 +583,25 @@ class _ASGI:
|
|||
# internally will still use custom task factory for pypy because of Loop
|
||||
if is_pypy:
|
||||
if task_factory_max_items > 0:
|
||||
factory = create_task_with_factory(task_factory_max_items)
|
||||
factory = TaskFactory(task_factory_max_items)
|
||||
|
||||
def run_task(task):
|
||||
factory(loop, task_wrapper(task))
|
||||
loop._run_once()
|
||||
|
||||
self._run_task = run_task
|
||||
else:
|
||||
|
||||
def run_task(task):
|
||||
create_task(loop, task_wrapper(task))
|
||||
loop._run_once()
|
||||
future = create_task(loop, task_wrapper(task))
|
||||
future._log_destroy_pending = False
|
||||
|
||||
self._run_task = run_task
|
||||
|
||||
else:
|
||||
if sys.version_info >= (3, 8): # name fixed to avoid dynamic name
|
||||
|
||||
def run_task(task):
|
||||
future = loop.create_task(
|
||||
task_wrapper(task), name="socketify.py-request-task"
|
||||
)
|
||||
future = create_task(loop, task_wrapper(task))
|
||||
future._log_destroy_pending = False
|
||||
loop._run_once()
|
||||
|
||||
self._run_task = run_task
|
||||
else:
|
||||
|
||||
def run_task(task):
|
||||
future = loop.create_task(task_wrapper(task))
|
||||
future._log_destroy_pending = False
|
||||
loop._run_once()
|
||||
|
||||
self._run_task = run_task
|
||||
|
||||
|
@ -641,12 +688,13 @@ class _ASGI:
|
|||
asgi_app = self
|
||||
self.is_starting = True
|
||||
self.is_stopped = False
|
||||
self.status = 0 # 0 starting, 1 ok, 2 error, 3 stoping, 4 stopped, 5 stopped with error, 6 no lifespan
|
||||
self.status = 0 # 0 starting, 1 ok, 2 error, 3 stopping, 4 stopped, 5 stopped with error, 6 no lifespan
|
||||
self.status_message = ""
|
||||
self.stop_future = self.server.loop.create_future()
|
||||
|
||||
async def send(options):
|
||||
nonlocal asgi_app
|
||||
asgi_app.server.loop.is_idle = False
|
||||
type = options["type"]
|
||||
asgi_app.status_message = options.get("message", "")
|
||||
if type == "lifespan.startup.complete":
|
||||
|
@ -664,6 +712,7 @@ class _ASGI:
|
|||
|
||||
async def receive():
|
||||
nonlocal asgi_app
|
||||
asgi_app.server.loop.is_idle = False
|
||||
while not asgi_app.is_stopped:
|
||||
if asgi_app.is_starting:
|
||||
asgi_app.is_starting = False
|
||||
|
@ -686,7 +735,7 @@ class _ASGI:
|
|||
asgi_app.server.listen(port_or_options, handler)
|
||||
finally:
|
||||
return None
|
||||
|
||||
self.server.loop.is_idle = False
|
||||
# start lifespan
|
||||
self.server.loop.ensure_future(task_wrapper(self.app(scope, receive, send)))
|
||||
self.server.run()
|
||||
|
@ -724,10 +773,13 @@ class _ASGI:
|
|||
return self
|
||||
|
||||
def __del__(self):
|
||||
if self.asgi_http_info:
|
||||
lib.socketify_destroy_asgi_app_info(self.asgi_http_info)
|
||||
if self.asgi_ws_info:
|
||||
lib.socketify_destroy_asgi_ws_app_info(self.asgi_ws_info)
|
||||
try:
|
||||
if self.asgi_http_info:
|
||||
lib.socketify_destroy_asgi_app_info(self.asgi_http_info)
|
||||
if self.asgi_ws_info:
|
||||
lib.socketify_destroy_asgi_ws_app_info(self.asgi_ws_info)
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
# "Public" ASGI interface to allow easy forks/workers
|
||||
|
@ -748,12 +800,26 @@ class ASGI:
|
|||
self.listen_options = None
|
||||
self.task_factory_max_items = task_factory_max_items
|
||||
self.lifespan = lifespan
|
||||
self.server = None
|
||||
self.pid_list = None
|
||||
|
||||
def listen(self, port_or_options, handler=None):
|
||||
self.listen_options = (port_or_options, handler)
|
||||
return self
|
||||
|
||||
def run(self, workers=1):
|
||||
def close(self):
|
||||
# always wait a sec so forks can start properly if close is called too fast
|
||||
import time
|
||||
time.sleep(1)
|
||||
|
||||
if self.server is not None:
|
||||
self.server.close()
|
||||
if self.pid_list is not None:
|
||||
import signal
|
||||
for pid in self.pid_list:
|
||||
os.kill(pid, signal.SIGINT)
|
||||
|
||||
def run(self, workers=1, block=True):
|
||||
def run_task():
|
||||
server = _ASGI(
|
||||
self.app,
|
||||
|
@ -766,17 +832,27 @@ class ASGI:
|
|||
if self.listen_options:
|
||||
(port_or_options, handler) = self.listen_options
|
||||
server.listen(port_or_options, handler)
|
||||
self.server = server
|
||||
server.run()
|
||||
|
||||
def create_fork():
|
||||
n = os.fork()
|
||||
# n greater than 0 means parent process
|
||||
if not n > 0:
|
||||
run_task()
|
||||
|
||||
pid_list = []
|
||||
start = 1 if block else 0
|
||||
# fork limiting the cpu count - 1
|
||||
for _ in range(1, workers):
|
||||
create_fork()
|
||||
for _ in range(block, workers):
|
||||
pid = os.fork()
|
||||
# n greater than 0 means parent process
|
||||
if not pid > 0:
|
||||
run_task()
|
||||
break
|
||||
pid_list.append(pid)
|
||||
|
||||
self.pid_list = pid_list
|
||||
|
||||
if block:
|
||||
run_task() # run app on the main process too :)
|
||||
# sigint everything to graceful shutdown
|
||||
import signal
|
||||
for pid in pid_list:
|
||||
os.kill(pid, signal.SIGINT)
|
||||
|
||||
run_task() # run app on the main process too :)
|
||||
return self
|
||||
|
|
|
@ -28,7 +28,7 @@ Options:
|
|||
--ws-close-on-backpressure-limit BOOLEAN Close connections that hits maximum backpressure [default: False]
|
||||
--lifespan [auto|on|off] Lifespan implementation. [default: auto]
|
||||
--interface [auto|asgi|asgi3|wsgi|ssgi|socketify] Select ASGI (same as ASGI3), ASGI3, WSGI or SSGI as the application interface. [default: auto]
|
||||
--disable-listen-log BOOLEAN Disable log when start listenning [default: False]
|
||||
--disable-listen-log BOOLEAN Disable log when start listening [default: False]
|
||||
--version or -v Display the socketify.py version and exit.
|
||||
--ssl-keyfile TEXT SSL key file
|
||||
--ssl-certfile TEXT SSL certificate file
|
||||
|
@ -188,10 +188,10 @@ def execute(args):
|
|||
|
||||
elif interface == "ssgi":
|
||||
# if not is_ssgi(module):
|
||||
# return print("SSGI is in development yet but is comming soon")
|
||||
# return print("SSGI is in development yet but is coming soon")
|
||||
# from . import SSGI as Interface
|
||||
# interface = "ssgi"
|
||||
return print("SSGI is in development yet but is comming soon")
|
||||
return print("SSGI is in development yet but is coming soon")
|
||||
elif interface != "socketify":
|
||||
return print(f"{interface} interface is not supported yet")
|
||||
|
||||
|
@ -310,22 +310,20 @@ def execute(args):
|
|||
fork_app.listen(AppListenOptions(port=port, host=host), listen_log)
|
||||
fork_app.run()
|
||||
|
||||
# now we can start all over again
|
||||
def create_fork(_):
|
||||
n = os.fork()
|
||||
pid_list = []
|
||||
# fork limiting the cpu count - 1
|
||||
for _ in range(1, workers):
|
||||
pid = os.fork()
|
||||
# n greater than 0 means parent process
|
||||
if not n > 0:
|
||||
if not pid > 0:
|
||||
run_app()
|
||||
return n
|
||||
break
|
||||
pid_list.append(pid)
|
||||
|
||||
# run in all forks
|
||||
pid_list = list(map(create_fork, range(1, workers)))
|
||||
run_app() # run app on the main process too :)
|
||||
|
||||
# run in this process
|
||||
run_app()
|
||||
# sigint everything to gracefull shutdown
|
||||
# sigint everything to graceful shutdown
|
||||
import signal
|
||||
|
||||
for pid in pid_list:
|
||||
os.kill(pid, signal.SIGINT)
|
||||
else:
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
|
||||
from dataclasses import dataclass
|
||||
|
||||
@dataclass
|
||||
class AppListenOptions:
|
||||
port: int = 0
|
||||
host: str = None
|
||||
options: int = 0
|
||||
domain: str = None
|
||||
|
||||
def __post_init__(self):
|
||||
if not isinstance(self.port, int):
|
||||
raise RuntimeError("port must be an int")
|
||||
if not isinstance(self.host, (type(None), str)):
|
||||
raise RuntimeError("host must be a str if specified")
|
||||
if not isinstance(self.domain, (type(None), str)):
|
||||
raise RuntimeError("domain must be a str if specified")
|
||||
if not isinstance(self.options, int):
|
||||
raise RuntimeError("options must be an int")
|
||||
if self.domain and (self.host or self.port != 0):
|
||||
raise RuntimeError(
|
||||
"if domain is specified, host and port will be no effect"
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class AppOptions:
|
||||
key_file_name: str = None
|
||||
cert_file_name: str = None
|
||||
passphrase: str = None
|
||||
dh_params_file_name: str = None
|
||||
ca_file_name: str = None
|
||||
ssl_ciphers: str = None
|
||||
ssl_prefer_low_memory_usage: int = 0
|
||||
|
||||
def __post_init__(self):
|
||||
NoneType = type(None)
|
||||
|
||||
if not isinstance(self.key_file_name, (NoneType, str)):
|
||||
raise RuntimeError("key_file_name must be a str if specified")
|
||||
if not isinstance(self.cert_file_name, (NoneType, str)):
|
||||
raise RuntimeError("cert_file_name must be a str if specified")
|
||||
if not isinstance(self.passphrase, (NoneType, str)):
|
||||
raise RuntimeError("passphrase must be a str if specified")
|
||||
if not isinstance(self.dh_params_file_name, (NoneType, str)):
|
||||
raise RuntimeError("dh_params_file_name must be a str if specified")
|
||||
if not isinstance(self.ca_file_name, (NoneType, str)):
|
||||
raise RuntimeError("ca_file_name must be a str if specified")
|
||||
if not isinstance(self.ssl_ciphers, (NoneType, str)):
|
||||
raise RuntimeError("ssl_ciphers must be a str if specified")
|
||||
if not isinstance(self.ssl_prefer_low_memory_usage, int):
|
||||
raise RuntimeError("ssl_prefer_low_memory_usage must be an int")
|
|
@ -70,7 +70,7 @@ async def sendfile(res, req, filename):
|
|||
|
||||
def send_headers(res):
|
||||
res.write_status(status)
|
||||
# tells the broswer the last modified date
|
||||
# tells the browser the last modified date
|
||||
res.write_header(b"Last-Modified", last_modified)
|
||||
|
||||
# tells the browser that we support range
|
||||
|
@ -147,7 +147,7 @@ def middleware(*functions):
|
|||
|
||||
# we use Optional data=None at the end so you can use and middleware inside a middleware
|
||||
def optimized_middleware_route(res, req, data=None):
|
||||
# cicle to all middlewares
|
||||
# circle to all middlewares
|
||||
for function in syncs:
|
||||
# call middlewares
|
||||
data = function(res, req, data)
|
||||
|
@ -156,7 +156,7 @@ def middleware(*functions):
|
|||
return
|
||||
|
||||
async def wrapper(res, req, data):
|
||||
# cicle to all middlewares
|
||||
# circle to all middlewares
|
||||
for function in asyncs:
|
||||
# detect if is coroutine or not
|
||||
if inspect.iscoroutinefunction(function):
|
||||
|
@ -182,7 +182,7 @@ def middleware(*functions):
|
|||
def sync_middleware(*functions):
|
||||
# we use Optional data=None at the end so you can use and middleware inside a middleware
|
||||
def middleware_route(res, req, data=None):
|
||||
# cicle to all middlewares
|
||||
# circle to all middlewares
|
||||
for function in functions:
|
||||
# call middlewares
|
||||
data = function(res, req, data)
|
||||
|
@ -198,7 +198,7 @@ def async_middleware(*functions):
|
|||
# we use Optional data=None at the end so you can use and middleware inside a middleware
|
||||
async def middleware_route(res, req, data=None):
|
||||
some_async_as_run = False
|
||||
# cicle to all middlewares
|
||||
# circle to all middlewares
|
||||
for function in functions:
|
||||
# detect if is coroutine or not
|
||||
if inspect.iscoroutinefunction(function):
|
||||
|
@ -363,61 +363,61 @@ class MiddlewareRouter:
|
|||
self.middlewares = middlewares
|
||||
|
||||
def get(self, path, handler):
|
||||
middies = list(*self.middlewares)
|
||||
middies = list(self.middlewares)
|
||||
middies.append(handler)
|
||||
self.app.get(path, middleware(*middies))
|
||||
return self
|
||||
|
||||
def post(self, path, handler):
|
||||
middies = list(*self.middlewares)
|
||||
middies = list(self.middlewares)
|
||||
middies.append(handler)
|
||||
self.app.post(path, middleware(*middies))
|
||||
return self
|
||||
|
||||
def options(self, path, handler):
|
||||
middies = list(*self.middlewares)
|
||||
middies = list(self.middlewares)
|
||||
middies.append(handler)
|
||||
self.app.options(path, middleware(*middies))
|
||||
return self
|
||||
|
||||
def delete(self, path, handler):
|
||||
middies = list(*self.middlewares)
|
||||
middies = list(self.middlewares)
|
||||
middies.append(handler)
|
||||
self.app.delete(path, middleware(*middies))
|
||||
return self
|
||||
|
||||
def patch(self, path, handler):
|
||||
middies = list(*self.middlewares)
|
||||
middies = list(self.middlewares)
|
||||
middies.append(handler)
|
||||
self.app.patch(path, middleware(*middies))
|
||||
return self
|
||||
|
||||
def put(self, path, handler):
|
||||
middies = list(*self.middlewares)
|
||||
middies = list(self.middlewares)
|
||||
middies.append(handler)
|
||||
self.app.put(path, middleware(*middies))
|
||||
return self
|
||||
|
||||
def head(self, path, handler):
|
||||
middies = list(*self.middlewares)
|
||||
middies = list(self.middlewares)
|
||||
middies.append(handler)
|
||||
self.app.head(path, middleware(*middies))
|
||||
return self
|
||||
|
||||
def connect(self, path, handler):
|
||||
middies = list(*self.middlewares)
|
||||
middies = list(self.middlewares)
|
||||
middies.append(handler)
|
||||
self.app.connect(path, middleware(*middies))
|
||||
return self
|
||||
|
||||
def trace(self, path, handler):
|
||||
middies = list(*self.middlewares)
|
||||
middies = list(self.middlewares)
|
||||
middies.append(handler)
|
||||
self.app.trace(path, middleware(*middies))
|
||||
return self
|
||||
|
||||
def any(self, path, handler):
|
||||
middies = list(*self.middlewares)
|
||||
middies = list(self.middlewares)
|
||||
middies.append(handler)
|
||||
self.app.any(path, middleware(*middies))
|
||||
return self
|
||||
|
|
Plik binarny nie jest wyświetlany.
Plik binarny nie jest wyświetlany.
Plik binarny nie jest wyświetlany.
Plik binarny nie jest wyświetlany.
|
@ -1,6 +1,6 @@
|
|||
import asyncio
|
||||
import logging
|
||||
from .tasks import create_task, create_task_with_factory
|
||||
from .tasks import create_task, TaskFactory
|
||||
from .uv import UVLoop
|
||||
|
||||
import asyncio
|
||||
|
@ -8,7 +8,6 @@ import platform
|
|||
|
||||
is_pypy = platform.python_implementation() == "PyPy"
|
||||
|
||||
|
||||
async def task_wrapper(exception_handler, loop, response, task):
|
||||
try:
|
||||
return await task
|
||||
|
@ -26,16 +25,18 @@ async def task_wrapper(exception_handler, loop, response, task):
|
|||
|
||||
|
||||
class Loop:
|
||||
def __init__(self, exception_handler=None, task_factory_max_items=0):
|
||||
def __init__(self, exception_handler=None, task_factory_max_items=0, idle_relaxation_time=0.01):
|
||||
|
||||
# get the current running loop or create a new one without warnings
|
||||
self.loop = asyncio._get_running_loop()
|
||||
self._idle_count = 0
|
||||
self.is_idle = False
|
||||
self.idle_relaxation_time = idle_relaxation_time
|
||||
if self.loop is None:
|
||||
self.loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(self.loop)
|
||||
|
||||
self.uv_loop = UVLoop()
|
||||
|
||||
if hasattr(exception_handler, "__call__"):
|
||||
self.exception_handler = exception_handler
|
||||
self.loop.set_exception_handler(
|
||||
|
@ -47,20 +48,27 @@ class Loop:
|
|||
self.started = False
|
||||
if is_pypy: # PyPy async Optimizations
|
||||
if task_factory_max_items > 0: # Only available in PyPy for now
|
||||
self._task_factory = create_task_with_factory(task_factory_max_items)
|
||||
self._task_factory = TaskFactory(task_factory_max_items)
|
||||
else:
|
||||
self._task_factory = create_task
|
||||
self.run_async = self._run_async_pypy
|
||||
# custom task factory
|
||||
|
||||
# TODO: check if any framework breaks without current_task(loop) support
|
||||
# custom task factory for other tasks
|
||||
def pypy_task_factory(loop, coro, context=None):
|
||||
return create_task(loop, coro, context=context)
|
||||
|
||||
self.loop.set_task_factory(pypy_task_factory)
|
||||
else:
|
||||
# CPython performs worse using custom create_task, so native create_task is used
|
||||
# but this also did not allow the use of create_task_with_factory :/
|
||||
# native create_task do not allow to change context, callbacks, state etc
|
||||
|
||||
# TODO: check if any framework breaks without current_task(loop) support
|
||||
# custom task factory for other tasks
|
||||
def cpython_task_factory(loop, coro, context=None):
|
||||
return create_task(loop, coro, context=context)
|
||||
|
||||
self.loop.set_task_factory(cpython_task_factory)
|
||||
|
||||
# CPython performs equals or worse using TaskFactory
|
||||
self.run_async = self._run_async_cpython
|
||||
|
||||
def set_timeout(self, timeout, callback, user_data):
|
||||
|
@ -71,8 +79,30 @@ class Loop:
|
|||
|
||||
def _keep_alive(self):
|
||||
if self.started:
|
||||
self.uv_loop.run_once()
|
||||
self.loop.call_soon(self._keep_alive)
|
||||
relax = False
|
||||
if not self.is_idle:
|
||||
self._idle_count = 0
|
||||
elif self._idle_count < 10000:
|
||||
self._idle_count += 1
|
||||
else:
|
||||
relax = True
|
||||
|
||||
self.is_idle = True
|
||||
|
||||
if relax:
|
||||
self.uv_loop.run_nowait()
|
||||
|
||||
if self._idle_count < 15000:
|
||||
self._idle_count += 1
|
||||
# we are idle not for long, wait 5s until next relax mode
|
||||
self.loop.call_later(0.001, self._keep_alive)
|
||||
else:
|
||||
# we are really idle now lets use less CPU
|
||||
self.loop.call_later(self.idle_relaxation_time, self._keep_alive)
|
||||
else:
|
||||
self.uv_loop.run_nowait()
|
||||
# be more aggressive when needed
|
||||
self.loop.call_soon(self._keep_alive)
|
||||
|
||||
def create_task(self, *args, **kwargs):
|
||||
# this is not using optimized create_task yet
|
||||
|
@ -81,6 +111,40 @@ class Loop:
|
|||
def ensure_future(self, task):
|
||||
return asyncio.ensure_future(task, loop=self.loop)
|
||||
|
||||
def set_event_loop(self, loop):
|
||||
needs_start = False
|
||||
if self.loop.is_running():
|
||||
self.stop()
|
||||
|
||||
self.loop = loop
|
||||
if self.exception_handler is not None:
|
||||
self.loop.set_exception_handler(
|
||||
lambda loop, context: self.exception_handler(loop, context, None)
|
||||
)
|
||||
|
||||
if is_pypy: # PyPy async Optimizations
|
||||
# TODO: check if any framework breaks without current_task(loop) support
|
||||
# custom task factory for other tasks
|
||||
def pypy_task_factory(loop, coro, context=None):
|
||||
return create_task(loop, coro, context=context)
|
||||
|
||||
self.loop.set_task_factory(pypy_task_factory)
|
||||
else:
|
||||
|
||||
# TODO: check if any framework breaks without current_task(loop) support
|
||||
# custom task factory for other tasks
|
||||
def cpython_task_factory(loop, coro, context=None):
|
||||
return create_task(loop, coro, context=context)
|
||||
|
||||
self.loop.set_task_factory(cpython_task_factory)
|
||||
if needs_start:
|
||||
self.run()
|
||||
|
||||
def create_background_task(self, bg_task):
|
||||
def next_tick():
|
||||
self.ensure_future(bg_task())
|
||||
self.loop.call_soon(next_tick)
|
||||
|
||||
def run_until_complete(self, task=None):
|
||||
self.started = True
|
||||
if task is not None:
|
||||
|
@ -107,8 +171,15 @@ class Loop:
|
|||
|
||||
def run_once(self):
|
||||
# run one step of asyncio
|
||||
self.loop._stopping = True
|
||||
self.loop._run_once()
|
||||
# if loop._run_once is not available use loop.run_forever + loop.call_soon(loop.stop)
|
||||
# this is useful when using uvloop or custom loops
|
||||
try:
|
||||
self.loop._stopping = True
|
||||
self.loop._run_once()
|
||||
except Exception:
|
||||
# this can be _StopError with means we should not call run_forever, but we can ignore it
|
||||
self.loop.call_soon(self.loop.stop)
|
||||
self.loop.run_forever()
|
||||
# run one step of libuv
|
||||
self.uv_loop.run_once()
|
||||
|
||||
|
@ -123,24 +194,19 @@ class Loop:
|
|||
return self.uv_loop.get_native_loop()
|
||||
|
||||
def _run_async_pypy(self, task, response=None):
|
||||
# this garanties error 500 in case of uncaught exceptions, and can trigger the custom error handler
|
||||
# this guarantees error 500 in case of uncaught exceptions, and can trigger the custom error handler
|
||||
# using an coroutine wrapper generates less overhead than using add_done_callback
|
||||
# this is an custom task/future with less overhead
|
||||
# this is an custom task/future with less overhead and that calls the first step
|
||||
future = self._task_factory(
|
||||
self.loop, task_wrapper(self.exception_handler, self.loop, response, task)
|
||||
)
|
||||
# force asyncio run once to enable req in async functions before first await
|
||||
self.loop._run_once()
|
||||
return None # this future maybe already done and reused not safe to await
|
||||
|
||||
def _run_async_cpython(self, task, response=None):
|
||||
# this garanties error 500 in case of uncaught exceptions, and can trigger the custom error handler
|
||||
# this guarantees error 500 in case of uncaught exceptions, and can trigger the custom error handler
|
||||
# using an coroutine wrapper generates less overhead than using add_done_callback
|
||||
future = self.loop.create_task(
|
||||
task_wrapper(self.exception_handler, self.loop, response, task)
|
||||
)
|
||||
# force asyncio run once to enable req in async functions before first await
|
||||
self.loop._run_once()
|
||||
# this is an custom task/future with less overhead and that calls the first step
|
||||
future = create_task(self.loop, task_wrapper(self.exception_handler, self.loop, response, task))
|
||||
return None # this future is safe to await but we return None for compatibility, and in the future will be the same behavior as PyPy
|
||||
|
||||
def dispose(self):
|
||||
|
|
|
@ -187,7 +187,7 @@ void uws_add_server_name_with_options(int ssl, uws_app_t *app, const char *hostn
|
|||
void uws_missing_server_name(int ssl, uws_app_t *app, uws_missing_server_handler handler, void *user_data);
|
||||
void uws_filter(int ssl, uws_app_t *app, uws_filter_handler handler, void *user_data);
|
||||
|
||||
|
||||
void uws_res_close(int ssl, uws_res_t *res);
|
||||
void uws_res_end(int ssl, uws_res_t *res, const char *data, size_t length, bool close_connection);
|
||||
void uws_res_pause(int ssl, uws_res_t *res);
|
||||
void uws_res_resume(int ssl, uws_res_t *res);
|
||||
|
@ -215,7 +215,7 @@ size_t uws_res_get_proxied_remote_address_as_text(int ssl, uws_res_t *res, const
|
|||
|
||||
bool uws_req_is_ancient(uws_req_t *res);
|
||||
bool uws_req_get_yield(uws_req_t *res);
|
||||
void uws_req_set_field(uws_req_t *res, bool yield);
|
||||
void uws_req_set_yield(uws_req_t *res, bool yield);
|
||||
size_t uws_req_get_url(uws_req_t *res, const char **dest);
|
||||
size_t uws_req_get_method(uws_req_t *res, const char **dest);
|
||||
size_t uws_req_get_case_sensitive_method(uws_req_t *res, const char **dest);
|
||||
|
@ -352,21 +352,21 @@ typedef struct {
|
|||
socketify_header* header_list;
|
||||
} socketify_asgi_ws_data;
|
||||
|
||||
typedef void (*socketify_asgi_method_handler)(int ssl, uws_res_t *response, socketify_asgi_data request, void *user_data, bool* aborted);
|
||||
typedef void (*socketify_asgi_method_handler)(int ssl, uws_res_t *response, socketify_asgi_data request, void *user_data);
|
||||
typedef struct {
|
||||
int ssl;
|
||||
uws_app_t* app;
|
||||
socketify_asgi_method_handler handler;
|
||||
void * user_data;
|
||||
} socksocketify_asgi_app_info;
|
||||
typedef void (*socketify_asgi_ws_method_handler)(int ssl, uws_res_t *response, socketify_asgi_ws_data request, uws_socket_context_t* socket, void *user_data, bool* aborted);
|
||||
typedef void (*socketify_asgi_ws_method_handler)(int ssl, uws_res_t *response, socketify_asgi_ws_data request, uws_socket_context_t* socket, void *user_data);
|
||||
typedef struct {
|
||||
int ssl;
|
||||
uws_app_t* app;
|
||||
socketify_asgi_ws_method_handler handler;
|
||||
uws_socket_behavior_t behavior;
|
||||
void * user_data;
|
||||
} socksocketify_asgi_ws_app_info;
|
||||
} socketify_asgi_ws_app_info;
|
||||
|
||||
socketify_asgi_data socketify_asgi_request(int ssl, uws_req_t *req, uws_res_t *res);
|
||||
void socketify_destroy_headers(socketify_header* headers);
|
||||
|
@ -376,8 +376,8 @@ socketify_asgi_ws_data socketify_asgi_ws_request(int ssl, uws_req_t *req, uws_re
|
|||
bool socketify_res_write_int_status(int ssl, uws_res_t* res, int code);
|
||||
socksocketify_asgi_app_info* socketify_add_asgi_http_handler(int ssl, uws_app_t* app, socketify_asgi_method_handler handler, void* user_data);
|
||||
void socketify_destroy_asgi_app_info(socksocketify_asgi_app_info* app);
|
||||
socksocketify_asgi_ws_app_info* socketify_add_asgi_ws_handler(int ssl, uws_app_t* app, uws_socket_behavior_t behavior, socketify_asgi_ws_method_handler handler, void* user_data);
|
||||
void socketify_destroy_asgi_ws_app_info(socksocketify_asgi_ws_app_info* app);
|
||||
socketify_asgi_ws_app_info* socketify_add_asgi_ws_handler(int ssl, uws_app_t* app, uws_socket_behavior_t behavior, socketify_asgi_ws_method_handler handler, void* user_data);
|
||||
void socketify_destroy_asgi_ws_app_info(socketify_asgi_ws_app_info* app);
|
||||
|
||||
void socketify_res_cork_write(int ssl, uws_res_t *response, const char* data, size_t length);
|
||||
void socketify_res_cork_end(int ssl, uws_res_t *response, const char* data, size_t length, bool close_connection);
|
||||
|
@ -385,6 +385,12 @@ void socketify_ws_cork_send(int ssl, uws_websocket_t *ws, const char* data, size
|
|||
|
||||
|
||||
void socketify_ws_cork_send_with_options(int ssl, uws_websocket_t *ws, const char* data, size_t length, uws_opcode_t opcode, bool compress, bool close_connection);
|
||||
|
||||
void socketify_res_send_int_code(int ssl, uws_res_t *res, const char* content_data, size_t content_data_size, int code, const char *content_type, size_t content_type_size, bool close_connection);
|
||||
void socketify_res_send(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, const char *status_code, size_t status_code_size, const char *content_type, size_t content_type_size, bool close_connection);
|
||||
|
||||
void socketify_res_cork_send_int_code(int ssl, uws_res_t *res, const char* content_data, size_t content_data_size, int code, const char *content_type, size_t content_type_size, bool close_connection);
|
||||
void socketify_res_cork_send(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, const char *status_code, size_t status_code_size, const char *content_type, size_t content_type_size, bool close_connection);
|
||||
"""
|
||||
)
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
LIBRARY_NAME := libsocketify
|
||||
UWS_LIBRARY_NAME := libuwebsockets
|
||||
CC := clang
|
||||
CXX := clang++
|
||||
CC ?= clang
|
||||
CXX ?= clang++
|
||||
|
||||
ARCH := amd64
|
||||
ifeq ($(PLATFORM), arm64)
|
||||
|
@ -72,17 +72,14 @@ macos:
|
|||
# build boringssl
|
||||
cd ../uWebSockets/uSockets/boringssl && mkdir -p amd64 && cd amd64 && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_OSX_DEPLOYMENT_TARGET=10.14 .. && make crypto ssl
|
||||
|
||||
# build lsquic
|
||||
cd ../uWebSockets/uSockets/lsquic && cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBORINGSSL_DIR=../boringssl -DCMAKE_BUILD_TYPE=Release -DLSQUIC_BIN=Off . && make lsquic
|
||||
|
||||
# build uWebSockets
|
||||
cd ../uWebSockets/uSockets && $(CC) -mmacosx-version-min=10.14 -I src -I lsquic/include -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -DLIBUS_USE_QUIC -pthread -fPIC -std=c11 -O3 -c src/*.c src/eventing/*.c src/crypto/*.c
|
||||
cd ../uWebSockets/uSockets && $(CXX) -stdlib=libc++ -mmacosx-version-min=10.14 -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -DLIBUS_USE_QUIC -pthread -fPIC -std=c++17 -O3 -c src/crypto/*.cpp
|
||||
cd ../uWebSockets/uSockets && $(CC) -mmacosx-version-min=10.14 -I src -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -pthread -fPIC -std=c11 -O3 -c src/*.c src/eventing/*.c src/crypto/*.c
|
||||
cd ../uWebSockets/uSockets && $(CXX) -stdlib=libc++ -mmacosx-version-min=10.14 -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -pthread -fPIC -std=c++17 -O3 -c src/crypto/*.cpp
|
||||
cd ../uWebSockets/uSockets && $(AR) rvs uSockets_darwin_amd64.a *.o
|
||||
|
||||
# build CAPI + libsocketify
|
||||
$(CXX) -stdlib=libc++ -mmacosx-version-min=10.14 -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/lsquic/include -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
|
||||
$(CXX) -stdlib=libc++ -mmacosx-version-min=10.14 -shared -undefined dynamic_lookup -o ../$(LIBRARY_NAME)_darwin_amd64.so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_darwin_amd64.a ../uWebSockets/uSockets/boringssl/amd64/ssl/libssl.a ../uWebSockets/uSockets/boringssl/amd64/crypto/libcrypto.a ../uWebSockets/uSockets/lsquic/src/liblsquic/liblsquic.a -flto -fPIC -lz -luv
|
||||
$(CXX) -stdlib=libc++ -mmacosx-version-min=10.14 -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
|
||||
$(CXX) -stdlib=libc++ -mmacosx-version-min=10.14 -shared -undefined dynamic_lookup -o ../$(LIBRARY_NAME)_darwin_amd64.so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_darwin_amd64.a ../uWebSockets/uSockets/boringssl/amd64/ssl/libssl.a ../uWebSockets/uSockets/boringssl/amd64/crypto/libcrypto.a -flto -fPIC -lz -luv
|
||||
|
||||
linux:
|
||||
$(MAKE) clean
|
||||
|
@ -90,29 +87,26 @@ linux:
|
|||
# build boringssl
|
||||
cd ../uWebSockets/uSockets/boringssl && mkdir -p $(ARCH) && cd $(ARCH) && cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release .. && make crypto ssl
|
||||
|
||||
# build lsquic
|
||||
cd ../uWebSockets/uSockets/lsquic && cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBORINGSSL_DIR=../boringssl -DCMAKE_BUILD_TYPE=Release -DLSQUIC_BIN=Off . && make lsquic
|
||||
|
||||
# build uWebSockets
|
||||
cd ../uWebSockets/uSockets && $(CC) -I src -I lsquic/include -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -DLIBUS_USE_QUIC -pthread -fPIC -std=c11 -O3 -c src/*.c src/eventing/*.c src/crypto/*.c
|
||||
cd ../uWebSockets/uSockets && $(CXX) -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -DLIBUS_USE_QUIC -pthread -fPIC -std=c++17 -O3 -c src/crypto/*.cpp
|
||||
cd ../uWebSockets/uSockets && $(CC) -I src -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -pthread -fPIC -std=c11 -O3 -c src/*.c src/eventing/*.c src/crypto/*.c
|
||||
cd ../uWebSockets/uSockets && $(CXX) -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -pthread -fPIC -std=c++17 -O3 -c src/crypto/*.cpp
|
||||
cd ../uWebSockets/uSockets && $(AR) rvs uSockets_linux_$(ARCH).a *.o
|
||||
|
||||
# build CAPI + libsocketify
|
||||
$(CXX) -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/lsquic/include -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
|
||||
$(CXX) -shared -static-libstdc++ -static-libgcc -s -o ../$(LIBRARY_NAME)_linux_$(ARCH).so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_linux_$(ARCH).a ../uWebSockets/uSockets/boringssl/$(ARCH)/ssl/libssl.a ../uWebSockets/uSockets/boringssl/$(ARCH)/crypto/libcrypto.a ../uWebSockets/uSockets/lsquic/src/liblsquic/liblsquic.a -flto -fPIC -lz -luv
|
||||
$(CXX) -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
|
||||
$(CXX) -shared -static-libstdc++ -static-libgcc -s -o ../$(LIBRARY_NAME)_linux_$(ARCH).so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_linux_$(ARCH).a ../uWebSockets/uSockets/boringssl/$(ARCH)/ssl/libssl.a ../uWebSockets/uSockets/boringssl/$(ARCH)/crypto/libcrypto.a -flto -fPIC -lz -luv
|
||||
|
||||
linux-uws-socketify:
|
||||
# build uWebSockets
|
||||
cd ../uWebSockets/uSockets && $(CC) -I src -I lsquic/include -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -DLIBUS_USE_QUIC -pthread -fPIC -std=c11 -O3 -c src/*.c src/eventing/*.c src/crypto/*.c
|
||||
cd ../uWebSockets/uSockets && $(CXX) -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -DLIBUS_USE_QUIC -pthread -fPIC -std=c++17 -O3 -c src/crypto/*.cpp
|
||||
cd ../uWebSockets/uSockets && $(CC) -I src -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -pthread -fPIC -std=c11 -O3 -c src/*.c src/eventing/*.c src/crypto/*.c
|
||||
cd ../uWebSockets/uSockets && $(CXX) -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -pthread -fPIC -std=c++17 -O3 -c src/crypto/*.cpp
|
||||
cd ../uWebSockets/uSockets && $(AR) rvs uSockets_linux_$(ARCH).a *.o
|
||||
|
||||
# build CAPI + libsocketify
|
||||
$(CXX) -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/lsquic/include -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
|
||||
$(CXX) -shared -static-libstdc++ -static-libgcc -s -o ../$(LIBRARY_NAME)_linux_$(ARCH).so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_linux_$(ARCH).a ../uWebSockets/uSockets/boringssl/$(ARCH)/ssl/libssl.a ../uWebSockets/uSockets/boringssl/$(ARCH)/crypto/libcrypto.a ../uWebSockets/uSockets/lsquic/src/liblsquic/liblsquic.a -flto -fPIC -lz -luv
|
||||
$(CXX) -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
|
||||
$(CXX) -shared -static-libstdc++ -static-libgcc -s -o ../$(LIBRARY_NAME)_linux_$(ARCH).so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_linux_$(ARCH).a ../uWebSockets/uSockets/boringssl/$(ARCH)/ssl/libssl.a ../uWebSockets/uSockets/boringssl/$(ARCH)/crypto/libcrypto.a -flto -fPIC -lz -luv
|
||||
|
||||
linux-socketify-only:
|
||||
# build CAPI + libsocketify
|
||||
$(CXX) -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/lsquic/include -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
|
||||
$(CXX) -shared -static-libstdc++ -static-libgcc -s -o ../$(LIBRARY_NAME)_linux_$(ARCH).so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_linux_$(ARCH).a ../uWebSockets/uSockets/boringssl/$(ARCH)/ssl/libssl.a ../uWebSockets/uSockets/boringssl/$(ARCH)/crypto/libcrypto.a ../uWebSockets/uSockets/lsquic/src/liblsquic/liblsquic.a -flto -fPIC -lz -luv
|
||||
$(CXX) -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
|
||||
$(CXX) -shared -static-libstdc++ -static-libgcc -s -o ../$(LIBRARY_NAME)_linux_$(ARCH).so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_linux_$(ARCH).a ../uWebSockets/uSockets/boringssl/$(ARCH)/ssl/libssl.a ../uWebSockets/uSockets/boringssl/$(ARCH)/crypto/libcrypto.a -flto -fPIC -lz -luv
|
||||
|
|
Plik diff jest za duży
Load Diff
|
@ -8,136 +8,144 @@
|
|||
extern "C"
|
||||
{
|
||||
#endif
|
||||
DLL_EXPORT typedef void (*socketify_prepare_handler)(void* user_data);
|
||||
DLL_EXPORT typedef void (*socketify_timer_handler)(void* user_data);
|
||||
DLL_EXPORT typedef void (*socketify_prepare_handler)(void *user_data);
|
||||
DLL_EXPORT typedef void (*socketify_timer_handler)(void *user_data);
|
||||
|
||||
DLL_EXPORT typedef enum {
|
||||
SOCKETIFY_RUN_DEFAULT = 0,
|
||||
SOCKETIFY_RUN_ONCE,
|
||||
SOCKETIFY_RUN_NOWAIT
|
||||
} socketify_run_mode;
|
||||
DLL_EXPORT typedef enum {
|
||||
SOCKETIFY_RUN_DEFAULT = 0,
|
||||
SOCKETIFY_RUN_ONCE,
|
||||
SOCKETIFY_RUN_NOWAIT
|
||||
} socketify_run_mode;
|
||||
|
||||
DLL_EXPORT typedef struct {
|
||||
void* uv_prepare_ptr;
|
||||
DLL_EXPORT typedef struct
|
||||
{
|
||||
void *uv_prepare_ptr;
|
||||
socketify_prepare_handler on_prepare_handler;
|
||||
void* on_prepare_data;
|
||||
void* uv_loop;
|
||||
} socketify_loop;
|
||||
void *on_prepare_data;
|
||||
void *uv_loop;
|
||||
} socketify_loop;
|
||||
|
||||
DLL_EXPORT typedef struct{
|
||||
void* uv_timer_ptr;
|
||||
DLL_EXPORT typedef struct
|
||||
{
|
||||
void *uv_timer_ptr;
|
||||
socketify_timer_handler handler;
|
||||
void* user_data;
|
||||
} socketify_timer;
|
||||
void *user_data;
|
||||
} socketify_timer;
|
||||
|
||||
DLL_EXPORT typedef struct
|
||||
{
|
||||
|
||||
DLL_EXPORT typedef struct {
|
||||
const char *name;
|
||||
const char *value;
|
||||
|
||||
const char* name;
|
||||
const char* value;
|
||||
size_t name_size;
|
||||
size_t value_size;
|
||||
|
||||
size_t name_size;
|
||||
size_t value_size;
|
||||
void *next;
|
||||
} socketify_header;
|
||||
|
||||
void* next;
|
||||
} socketify_header;
|
||||
DLL_EXPORT typedef struct
|
||||
{
|
||||
|
||||
const char *full_url;
|
||||
const char *url;
|
||||
const char *query_string;
|
||||
const char *method;
|
||||
const char *remote_address;
|
||||
|
||||
DLL_EXPORT typedef struct {
|
||||
size_t full_url_size;
|
||||
size_t url_size;
|
||||
size_t query_string_size;
|
||||
size_t method_size;
|
||||
size_t remote_address_size;
|
||||
|
||||
const char* full_url;
|
||||
const char* url;
|
||||
const char* query_string;
|
||||
const char* method;
|
||||
const char* remote_address;
|
||||
socketify_header *header_list;
|
||||
|
||||
size_t full_url_size;
|
||||
size_t url_size;
|
||||
size_t query_string_size;
|
||||
size_t method_size;
|
||||
size_t remote_address_size;
|
||||
bool has_content;
|
||||
} socketify_asgi_data;
|
||||
|
||||
socketify_header* header_list;
|
||||
DLL_EXPORT typedef struct
|
||||
{
|
||||
|
||||
bool has_content;
|
||||
} socketify_asgi_data;
|
||||
const char *full_url;
|
||||
const char *url;
|
||||
const char *query_string;
|
||||
const char *method;
|
||||
const char *remote_address;
|
||||
|
||||
DLL_EXPORT typedef struct {
|
||||
size_t full_url_size;
|
||||
size_t url_size;
|
||||
size_t query_string_size;
|
||||
size_t method_size;
|
||||
size_t remote_address_size;
|
||||
|
||||
const char* full_url;
|
||||
const char* url;
|
||||
const char* query_string;
|
||||
const char* method;
|
||||
const char* remote_address;
|
||||
const char *protocol;
|
||||
const char *key;
|
||||
const char *extensions;
|
||||
size_t protocol_size;
|
||||
size_t key_size;
|
||||
size_t extensions_size;
|
||||
|
||||
size_t full_url_size;
|
||||
size_t url_size;
|
||||
size_t query_string_size;
|
||||
size_t method_size;
|
||||
size_t remote_address_size;
|
||||
socketify_header *header_list;
|
||||
} socketify_asgi_ws_data;
|
||||
|
||||
const char* protocol;
|
||||
const char* key;
|
||||
const char* extensions;
|
||||
size_t protocol_size;
|
||||
size_t key_size;
|
||||
size_t extensions_size;
|
||||
DLL_EXPORT typedef void (*socketify_asgi_method_handler)(int ssl, uws_res_t *response, socketify_asgi_data request, void *user_data);
|
||||
DLL_EXPORT typedef void (*socketify_asgi_ws_method_handler)(int ssl, uws_res_t *response, socketify_asgi_ws_data request, uws_socket_context_t *socket, void *user_data);
|
||||
DLL_EXPORT typedef struct
|
||||
{
|
||||
int ssl;
|
||||
uws_app_t *app;
|
||||
socketify_asgi_method_handler handler;
|
||||
void *user_data;
|
||||
} socksocketify_asgi_app_info;
|
||||
|
||||
socketify_header* header_list;
|
||||
} socketify_asgi_ws_data;
|
||||
DLL_EXPORT typedef struct
|
||||
{
|
||||
int ssl;
|
||||
uws_app_t *app;
|
||||
socketify_asgi_ws_method_handler handler;
|
||||
uws_socket_behavior_t behavior;
|
||||
void *user_data;
|
||||
} socketify_asgi_ws_app_info;
|
||||
|
||||
DLL_EXPORT typedef void (*socketify_asgi_method_handler)(int ssl, uws_res_t *response, socketify_asgi_data request, void *user_data, bool* aborted);
|
||||
DLL_EXPORT typedef void (*socketify_asgi_ws_method_handler)(int ssl, uws_res_t *response, socketify_asgi_ws_data request, uws_socket_context_t* socket, void *user_data, bool* aborted);
|
||||
DLL_EXPORT typedef struct {
|
||||
int ssl;
|
||||
uws_app_t* app;
|
||||
socketify_asgi_method_handler handler;
|
||||
void * user_data;
|
||||
} socksocketify_asgi_app_info;
|
||||
DLL_EXPORT socketify_loop *socketify_create_loop();
|
||||
DLL_EXPORT bool socketify_constructor_failed(socketify_loop *loop);
|
||||
DLL_EXPORT bool socketify_on_prepare(socketify_loop *loop, socketify_prepare_handler handler, void *user_data);
|
||||
DLL_EXPORT bool socketify_prepare_unbind(socketify_loop *loop);
|
||||
DLL_EXPORT void socketify_destroy_loop(socketify_loop *loop);
|
||||
DLL_EXPORT void *socketify_get_native_loop(socketify_loop *loop);
|
||||
|
||||
DLL_EXPORT typedef struct {
|
||||
int ssl;
|
||||
uws_app_t* app;
|
||||
socketify_asgi_ws_method_handler handler;
|
||||
uws_socket_behavior_t behavior;
|
||||
void * user_data;
|
||||
} socksocketify_asgi_ws_app_info;
|
||||
DLL_EXPORT int socketify_loop_run(socketify_loop *loop, socketify_run_mode mode);
|
||||
DLL_EXPORT void socketify_loop_stop(socketify_loop *loop);
|
||||
|
||||
DLL_EXPORT socketify_timer *socketify_create_timer(socketify_loop *loop, uint64_t timeout, uint64_t repeat, socketify_timer_handler handler, void *user_data);
|
||||
DLL_EXPORT void socketify_timer_destroy(socketify_timer *timer);
|
||||
DLL_EXPORT void socketify_timer_set_repeat(socketify_timer *timer, uint64_t repeat);
|
||||
|
||||
DLL_EXPORT socketify_loop * socketify_create_loop();
|
||||
DLL_EXPORT bool socketify_constructor_failed(socketify_loop* loop);
|
||||
DLL_EXPORT bool socketify_on_prepare(socketify_loop* loop, socketify_prepare_handler handler, void* user_data);
|
||||
DLL_EXPORT bool socketify_prepare_unbind(socketify_loop* loop);
|
||||
DLL_EXPORT void socketify_destroy_loop(socketify_loop* loop);
|
||||
DLL_EXPORT void* socketify_get_native_loop(socketify_loop* loop);
|
||||
DLL_EXPORT socketify_timer *socketify_create_check(socketify_loop *loop, socketify_timer_handler handler, void *user_data);
|
||||
DLL_EXPORT void socketify_check_destroy(socketify_timer *timer);
|
||||
|
||||
DLL_EXPORT int socketify_loop_run(socketify_loop* loop, socketify_run_mode mode);
|
||||
DLL_EXPORT void socketify_loop_stop(socketify_loop* loop);
|
||||
DLL_EXPORT socketify_asgi_data socketify_asgi_request(int ssl, uws_req_t *req, uws_res_t *res);
|
||||
DLL_EXPORT void socketify_destroy_headers(socketify_header *headers);
|
||||
DLL_EXPORT bool socketify_res_write_int_status_with_headers(int ssl, uws_res_t *res, int code, socketify_header *headers);
|
||||
DLL_EXPORT void socketify_res_write_headers(int ssl, uws_res_t *res, socketify_header *headers);
|
||||
DLL_EXPORT bool socketify_res_write_int_status(int ssl, uws_res_t *res, int code);
|
||||
DLL_EXPORT socketify_asgi_ws_data socketify_asgi_ws_request(int ssl, uws_req_t *req, uws_res_t *res);
|
||||
|
||||
DLL_EXPORT socketify_timer* socketify_create_timer(socketify_loop* loop, uint64_t timeout, uint64_t repeat, socketify_timer_handler handler, void* user_data);
|
||||
DLL_EXPORT void socketify_timer_destroy(socketify_timer* timer);
|
||||
DLL_EXPORT void socketify_timer_set_repeat(socketify_timer* timer, uint64_t repeat);
|
||||
DLL_EXPORT socksocketify_asgi_app_info *socketify_add_asgi_http_handler(int ssl, uws_app_t *app, socketify_asgi_method_handler handler, void *user_data);
|
||||
DLL_EXPORT void socketify_destroy_asgi_app_info(socksocketify_asgi_app_info *app);
|
||||
|
||||
DLL_EXPORT socketify_timer* socketify_create_check(socketify_loop* loop, socketify_timer_handler handler, void* user_data);
|
||||
DLL_EXPORT void socketify_check_destroy(socketify_timer* timer);
|
||||
DLL_EXPORT void socketify_res_cork_write(int ssl, uws_res_t *response, const char *data, size_t length);
|
||||
DLL_EXPORT void socketify_res_cork_end(int ssl, uws_res_t *response, const char *data, size_t length, bool close_connection);
|
||||
|
||||
DLL_EXPORT socketify_asgi_data socketify_asgi_request(int ssl, uws_req_t *req, uws_res_t *res);
|
||||
DLL_EXPORT void socketify_destroy_headers(socketify_header* headers);
|
||||
DLL_EXPORT bool socketify_res_write_int_status_with_headers(int ssl, uws_res_t* res, int code, socketify_header* headers);
|
||||
DLL_EXPORT void socketify_res_write_headers(int ssl, uws_res_t* res, socketify_header* headers);
|
||||
DLL_EXPORT bool socketify_res_write_int_status(int ssl, uws_res_t* res, int code);
|
||||
DLL_EXPORT socketify_asgi_ws_data socketify_asgi_ws_request(int ssl, uws_req_t *req, uws_res_t *res);
|
||||
|
||||
DLL_EXPORT socksocketify_asgi_app_info* socketify_add_asgi_http_handler(int ssl, uws_app_t* app, socketify_asgi_method_handler handler, void* user_data);
|
||||
DLL_EXPORT void socketify_destroy_asgi_app_info(socksocketify_asgi_app_info* app);
|
||||
|
||||
DLL_EXPORT void socketify_res_cork_write(int ssl, uws_res_t *response, const char* data, size_t length);
|
||||
DLL_EXPORT void socketify_res_cork_end(int ssl, uws_res_t *response, const char* data, size_t length, bool close_connection);
|
||||
|
||||
DLL_EXPORT socksocketify_asgi_ws_app_info* socketify_add_asgi_ws_handler(int ssl, uws_app_t* app, uws_socket_behavior_t behavior, socketify_asgi_ws_method_handler handler, void* user_data);
|
||||
DLL_EXPORT void socketify_destroy_asgi_ws_app_info(socksocketify_asgi_ws_app_info* app);
|
||||
DLL_EXPORT void socketify_ws_cork_send(int ssl, uws_websocket_t *ws, const char* data, size_t length, uws_opcode_t opcode);
|
||||
DLL_EXPORT void socketify_ws_cork_send_with_options(int ssl, uws_websocket_t *ws, const char* data, size_t length, uws_opcode_t opcode, bool compress, bool fin);
|
||||
DLL_EXPORT socketify_asgi_ws_app_info *socketify_add_asgi_ws_handler(int ssl, uws_app_t *app, uws_socket_behavior_t behavior, socketify_asgi_ws_method_handler handler, void *user_data);
|
||||
DLL_EXPORT void socketify_destroy_asgi_ws_app_info(socketify_asgi_ws_app_info *app);
|
||||
DLL_EXPORT void socketify_ws_cork_send(int ssl, uws_websocket_t *ws, const char *data, size_t length, uws_opcode_t opcode);
|
||||
DLL_EXPORT void socketify_ws_cork_send_with_options(int ssl, uws_websocket_t *ws, const char *data, size_t length, uws_opcode_t opcode, bool compress, bool fin);
|
||||
DLL_EXPORT void socketify_res_send_int_code(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, int code, const char *content_type, size_t content_type_size, bool close_connection);
|
||||
DLL_EXPORT void socketify_res_send(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, const char *status_code, size_t status_code_size, const char *content_type, size_t content_type_size, bool close_connection);
|
||||
DLL_EXPORT void socketify_res_cork_send_int_code(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, int code, const char *content_type, size_t content_type_size, bool close_connection);
|
||||
DLL_EXPORT void socketify_res_cork_send(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, const char *status_code, size_t status_code_size, const char *content_type, size_t content_type_size, bool close_connection);
|
||||
#endif
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -8,15 +8,14 @@ import signal
|
|||
import uuid
|
||||
from urllib.parse import parse_qs, quote_plus, unquote_plus
|
||||
import logging
|
||||
import traceback
|
||||
|
||||
from .native import ffi, lib
|
||||
from .loop import Loop
|
||||
from .status_codes import status_codes
|
||||
from .helpers import static_route
|
||||
from dataclasses import dataclass
|
||||
from .helpers import DecoratorRouter
|
||||
from typing import Union
|
||||
|
||||
from .dataclasses import AppListenOptions
|
||||
|
||||
@ffi.callback("void(const char*, size_t, void*)")
|
||||
def uws_missing_server_name(hostname, hostname_length, user_data):
|
||||
|
@ -35,7 +34,7 @@ def uws_missing_server_name(hostname, hostname_length, user_data):
|
|||
handler(data)
|
||||
except Exception as err:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception:\n %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -43,6 +42,7 @@ def uws_missing_server_name(hostname, hostname_length, user_data):
|
|||
def uws_websocket_factory_drain_handler(ws, user_data):
|
||||
if user_data != ffi.NULL:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
instances = app._ws_factory.get(app, ws)
|
||||
ws, dispose = instances
|
||||
try:
|
||||
|
@ -68,7 +68,7 @@ def uws_websocket_factory_drain_handler(ws, user_data):
|
|||
if dispose:
|
||||
app._ws_factory.dispose(instances)
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -78,6 +78,7 @@ def uws_websocket_drain_handler_with_extension(ws, user_data):
|
|||
try:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
ws = WebSocket(ws, app)
|
||||
app.loop.is_idle = False
|
||||
# bind methods to websocket
|
||||
app._ws_extension.set_properties(ws)
|
||||
# set default value in properties
|
||||
|
@ -89,7 +90,7 @@ def uws_websocket_drain_handler_with_extension(ws, user_data):
|
|||
handler(ws)
|
||||
except Exception as err:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -99,6 +100,7 @@ def uws_websocket_drain_handler(ws, user_data):
|
|||
try:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
ws = WebSocket(ws, app)
|
||||
app.loop.is_idle = False
|
||||
handler = handlers.drain
|
||||
if inspect.iscoroutinefunction(handler):
|
||||
app.run_async(handler(ws))
|
||||
|
@ -106,7 +108,7 @@ def uws_websocket_drain_handler(ws, user_data):
|
|||
handler(ws)
|
||||
except Exception as err:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -121,6 +123,7 @@ def uws_websocket_factory_subscription_handler(
|
|||
):
|
||||
if user_data != ffi.NULL:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
instances = app._ws_factory.get(app, ws)
|
||||
ws, dispose = instances
|
||||
try:
|
||||
|
@ -187,7 +190,7 @@ def uws_websocket_factory_subscription_handler(
|
|||
if dispose:
|
||||
app._ws_factory.dispose(instances)
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -203,6 +206,7 @@ def uws_websocket_subscription_handler(
|
|||
if user_data != ffi.NULL:
|
||||
try:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
ws = WebSocket(ws, app)
|
||||
handler = handlers.subscription
|
||||
|
||||
|
@ -229,7 +233,7 @@ def uws_websocket_subscription_handler(
|
|||
)
|
||||
except Exception as err:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -245,6 +249,7 @@ def uws_websocket_subscription_handler_with_extension(
|
|||
if user_data != ffi.NULL:
|
||||
try:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
ws = WebSocket(ws, app)
|
||||
# bind methods to websocket
|
||||
app._ws_extension.set_properties(ws)
|
||||
|
@ -275,7 +280,7 @@ def uws_websocket_subscription_handler_with_extension(
|
|||
)
|
||||
except Exception as err:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -283,6 +288,7 @@ def uws_websocket_subscription_handler_with_extension(
|
|||
def uws_websocket_factory_open_handler(ws, user_data):
|
||||
if user_data != ffi.NULL:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
instances = app._ws_factory.get(app, ws)
|
||||
ws, dispose = instances
|
||||
try:
|
||||
|
@ -307,7 +313,7 @@ def uws_websocket_factory_open_handler(ws, user_data):
|
|||
if dispose:
|
||||
app._ws_factory.dispose(instances)
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -317,6 +323,7 @@ def uws_websocket_open_handler_with_extension(ws, user_data):
|
|||
if user_data != ffi.NULL:
|
||||
try:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
ws = WebSocket(ws, app)
|
||||
# bind methods to websocket
|
||||
app._ws_extension.set_properties(ws)
|
||||
|
@ -329,7 +336,7 @@ def uws_websocket_open_handler_with_extension(ws, user_data):
|
|||
handler(ws)
|
||||
except Exception as err:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -339,6 +346,7 @@ def uws_websocket_open_handler(ws, user_data):
|
|||
if user_data != ffi.NULL:
|
||||
try:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
ws = WebSocket(ws, app)
|
||||
handler = handlers.open
|
||||
if inspect.iscoroutinefunction(handler):
|
||||
|
@ -347,7 +355,7 @@ def uws_websocket_open_handler(ws, user_data):
|
|||
handler(ws)
|
||||
except Exception as err:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -355,6 +363,7 @@ def uws_websocket_open_handler(ws, user_data):
|
|||
def uws_websocket_factory_message_handler(ws, message, length, opcode, user_data):
|
||||
if user_data != ffi.NULL:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
instances = app._ws_factory.get(app, ws)
|
||||
ws, dispose = instances
|
||||
try:
|
||||
|
@ -388,7 +397,7 @@ def uws_websocket_factory_message_handler(ws, message, length, opcode, user_data
|
|||
if dispose:
|
||||
app._ws_factory.dispose(instances)
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -399,6 +408,7 @@ def uws_websocket_message_handler_with_extension(
|
|||
if user_data != ffi.NULL:
|
||||
try:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
ws = WebSocket(ws, app)
|
||||
# bind methods to websocket
|
||||
app._ws_extension.set_properties(ws)
|
||||
|
@ -421,7 +431,7 @@ def uws_websocket_message_handler_with_extension(
|
|||
|
||||
except Exception as err:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -430,6 +440,7 @@ def uws_websocket_message_handler(ws, message, length, opcode, user_data):
|
|||
if user_data != ffi.NULL:
|
||||
try:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
ws = WebSocket(ws, app)
|
||||
|
||||
if message == ffi.NULL:
|
||||
|
@ -448,7 +459,7 @@ def uws_websocket_message_handler(ws, message, length, opcode, user_data):
|
|||
|
||||
except Exception as err:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -456,6 +467,7 @@ def uws_websocket_message_handler(ws, message, length, opcode, user_data):
|
|||
def uws_websocket_factory_pong_handler(ws, message, length, user_data):
|
||||
if user_data != ffi.NULL:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
instances = app._ws_factory.get(app, ws)
|
||||
ws, dispose = instances
|
||||
try:
|
||||
|
@ -486,7 +498,7 @@ def uws_websocket_factory_pong_handler(ws, message, length, user_data):
|
|||
if dispose:
|
||||
app._ws_factory.dispose(instances)
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -495,6 +507,7 @@ def uws_websocket_pong_handler_with_extension(ws, message, length, user_data):
|
|||
if user_data != ffi.NULL:
|
||||
try:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
ws = WebSocket(ws, app)
|
||||
# bind methods to websocket
|
||||
app._ws_extension.set_properties(ws)
|
||||
|
@ -512,7 +525,7 @@ def uws_websocket_pong_handler_with_extension(ws, message, length, user_data):
|
|||
handler(ws, data)
|
||||
except Exception as err:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -521,6 +534,7 @@ def uws_websocket_pong_handler(ws, message, length, user_data):
|
|||
if user_data != ffi.NULL:
|
||||
try:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
ws = WebSocket(ws, app)
|
||||
if message == ffi.NULL:
|
||||
data = None
|
||||
|
@ -534,7 +548,7 @@ def uws_websocket_pong_handler(ws, message, length, user_data):
|
|||
handler(ws, data)
|
||||
except Exception as err:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -542,6 +556,7 @@ def uws_websocket_pong_handler(ws, message, length, user_data):
|
|||
def uws_websocket_factory_ping_handler(ws, message, length, user_data):
|
||||
if user_data != ffi.NULL:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
instances = app._ws_factory.get(app, ws)
|
||||
ws, dispose = instances
|
||||
|
||||
|
@ -573,7 +588,7 @@ def uws_websocket_factory_ping_handler(ws, message, length, user_data):
|
|||
if dispose:
|
||||
app._ws_factory.dispose(instances)
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -582,6 +597,7 @@ def uws_websocket_ping_handler_with_extension(ws, message, length, user_data):
|
|||
if user_data != ffi.NULL:
|
||||
try:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
ws = WebSocket(ws, app)
|
||||
# bind methods to websocket
|
||||
app._ws_extension.set_properties(ws)
|
||||
|
@ -601,7 +617,7 @@ def uws_websocket_ping_handler_with_extension(ws, message, length, user_data):
|
|||
|
||||
except Exception as err:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -610,6 +626,7 @@ def uws_websocket_ping_handler(ws, message, length, user_data):
|
|||
if user_data != ffi.NULL:
|
||||
try:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
ws = WebSocket(ws, app)
|
||||
|
||||
if message == ffi.NULL:
|
||||
|
@ -625,7 +642,7 @@ def uws_websocket_ping_handler(ws, message, length, user_data):
|
|||
|
||||
except Exception as err:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -633,6 +650,7 @@ def uws_websocket_ping_handler(ws, message, length, user_data):
|
|||
def uws_websocket_factory_close_handler(ws, code, message, length, user_data):
|
||||
if user_data != ffi.NULL:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
instances = app._ws_factory.get(app, ws)
|
||||
ws, dispose = instances
|
||||
|
||||
|
@ -651,7 +669,7 @@ def uws_websocket_factory_close_handler(ws, code, message, length, user_data):
|
|||
|
||||
if inspect.iscoroutinefunction(handler):
|
||||
|
||||
async def wrapper(app, instances, handler, ws, data, code, dispose):
|
||||
async def wrapper(app, instances, handler, ws, code, data, dispose):
|
||||
try:
|
||||
return await handler(ws, code, data)
|
||||
finally:
|
||||
|
@ -662,7 +680,7 @@ def uws_websocket_factory_close_handler(ws, code, message, length, user_data):
|
|||
app._ws_factory.dispose(instances)
|
||||
|
||||
app.run_async(
|
||||
wrapper(app, instances, handler, ws, data, int(code), dispose)
|
||||
wrapper(app, instances, handler, ws, int(code), data, dispose)
|
||||
)
|
||||
else:
|
||||
handler(ws, int(code), data)
|
||||
|
@ -674,7 +692,7 @@ def uws_websocket_factory_close_handler(ws, code, message, length, user_data):
|
|||
|
||||
except Exception as err:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -683,6 +701,7 @@ def uws_websocket_close_handler_with_extension(ws, code, message, length, user_d
|
|||
if user_data != ffi.NULL:
|
||||
try:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
# pass to free data on WebSocket if needed
|
||||
ws = WebSocket(ws, app)
|
||||
# bind methods to websocket
|
||||
|
@ -702,7 +721,7 @@ def uws_websocket_close_handler_with_extension(ws, code, message, length, user_d
|
|||
|
||||
if inspect.iscoroutinefunction(handler):
|
||||
|
||||
async def wrapper(app, handler, ws, data, code, dispose):
|
||||
async def wrapper(app, handler, ws, code, data):
|
||||
try:
|
||||
return await handler(ws, code, data)
|
||||
finally:
|
||||
|
@ -710,7 +729,7 @@ def uws_websocket_close_handler_with_extension(ws, code, message, length, user_d
|
|||
if key is not None:
|
||||
app._socket_refs.pop(key, None)
|
||||
|
||||
app.run_async(wrapper(app, handler, ws, data, int(code)))
|
||||
app.run_async(wrapper(app, handler, ws, int(code), data))
|
||||
else:
|
||||
handler(ws, int(code), data)
|
||||
key = ws.get_user_data_uuid()
|
||||
|
@ -719,7 +738,7 @@ def uws_websocket_close_handler_with_extension(ws, code, message, length, user_d
|
|||
|
||||
except Exception as err:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -728,6 +747,7 @@ def uws_websocket_close_handler(ws, code, message, length, user_data):
|
|||
if user_data != ffi.NULL:
|
||||
try:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
# pass to free data on WebSocket if needed
|
||||
ws = WebSocket(ws, app)
|
||||
|
||||
|
@ -743,7 +763,7 @@ def uws_websocket_close_handler(ws, code, message, length, user_data):
|
|||
|
||||
if inspect.iscoroutinefunction(handler):
|
||||
|
||||
async def wrapper(app, handler, ws, data, code, dispose):
|
||||
async def wrapper(app, handler, ws, code, data):
|
||||
try:
|
||||
return await handler(ws, code, data)
|
||||
finally:
|
||||
|
@ -751,7 +771,7 @@ def uws_websocket_close_handler(ws, code, message, length, user_data):
|
|||
if key is not None:
|
||||
app._socket_refs.pop(key, None)
|
||||
|
||||
app.run_async(wrapper(app, handler, ws, data, int(code)))
|
||||
app.run_async(wrapper(app, handler, ws, int(code), data))
|
||||
else:
|
||||
handler(ws, int(code), data)
|
||||
key = ws.get_user_data_uuid()
|
||||
|
@ -760,7 +780,7 @@ def uws_websocket_close_handler(ws, code, message, length, user_data):
|
|||
|
||||
except Exception as err:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(err)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
|
||||
|
||||
|
@ -768,6 +788,7 @@ def uws_websocket_close_handler(ws, code, message, length, user_data):
|
|||
def uws_generic_factory_method_handler(res, req, user_data):
|
||||
if user_data != ffi.NULL:
|
||||
(handler, app) = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
instances = app._factory.get(app, res, req)
|
||||
(response, request, dispose) = instances
|
||||
try:
|
||||
|
@ -802,6 +823,7 @@ def uws_generic_factory_method_handler(res, req, user_data):
|
|||
def uws_websocket_factory_upgrade_handler(res, req, context, user_data):
|
||||
if user_data != ffi.NULL:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
instances = app._factory.get(app, res, req)
|
||||
(response, request, dispose) = instances
|
||||
try:
|
||||
|
@ -839,6 +861,7 @@ def uws_websocket_factory_upgrade_handler(res, req, context, user_data):
|
|||
def uws_websocket_upgrade_handler_with_extension(res, req, context, user_data):
|
||||
if user_data != ffi.NULL:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
response = AppResponse(res, app)
|
||||
# set default value in properties
|
||||
app._response_extension.set_properties(response)
|
||||
|
@ -866,6 +889,7 @@ def uws_websocket_upgrade_handler_with_extension(res, req, context, user_data):
|
|||
def uws_websocket_upgrade_handler(res, req, context, user_data):
|
||||
if user_data != ffi.NULL:
|
||||
handlers, app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
response = AppResponse(res, app)
|
||||
request = AppRequest(req, app)
|
||||
try:
|
||||
|
@ -910,6 +934,7 @@ def uws_req_for_each_header_handler(
|
|||
def uws_generic_factory_method_handler(res, req, user_data):
|
||||
if user_data != ffi.NULL:
|
||||
(handler, app) = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
instances = app._factory.get(app, res, req)
|
||||
(response, request, dispose) = instances
|
||||
try:
|
||||
|
@ -945,6 +970,7 @@ def uws_generic_factory_method_handler(res, req, user_data):
|
|||
def uws_generic_method_handler_with_extension(res, req, user_data):
|
||||
if user_data != ffi.NULL:
|
||||
(handler, app) = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
response = AppResponse(res, app)
|
||||
# set default value in properties
|
||||
app._response_extension.set_properties(response)
|
||||
|
@ -971,6 +997,7 @@ def uws_generic_method_handler_with_extension(res, req, user_data):
|
|||
def uws_generic_method_handler(res, req, user_data):
|
||||
if user_data != ffi.NULL:
|
||||
(handler, app) = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
response = AppResponse(res, app)
|
||||
request = AppRequest(req, app)
|
||||
|
||||
|
@ -1008,6 +1035,7 @@ def uws_generic_listen_handler(listen_socket, config, user_data):
|
|||
|
||||
if user_data != ffi.NULL:
|
||||
app = ffi.from_handle(user_data)
|
||||
app.loop.is_idle = False
|
||||
config.port = lib.us_socket_local_port(app.SSL, listen_socket)
|
||||
if hasattr(app, "_listen_handler") and hasattr(app._listen_handler, "__call__"):
|
||||
app.socket = listen_socket
|
||||
|
@ -1043,6 +1071,7 @@ def uws_generic_aborted_handler(response, user_data):
|
|||
def uws_generic_on_data_handler(res, chunk, chunk_length, is_end, user_data):
|
||||
if user_data != ffi.NULL:
|
||||
res = ffi.from_handle(user_data)
|
||||
res.app.loop.is_idle = False
|
||||
if chunk == ffi.NULL:
|
||||
data = None
|
||||
else:
|
||||
|
@ -1055,6 +1084,7 @@ def uws_generic_on_data_handler(res, chunk, chunk_length, is_end, user_data):
|
|||
def uws_generic_on_writable_handler(res, offset, user_data):
|
||||
if user_data != ffi.NULL:
|
||||
res = ffi.from_handle(user_data)
|
||||
res.app.loop.is_idle = False
|
||||
result = res.trigger_writable_handler(offset)
|
||||
return result
|
||||
return False
|
||||
|
@ -1143,10 +1173,9 @@ class WebSocket:
|
|||
self.socket_data = None
|
||||
self.got_socket_data = False
|
||||
|
||||
|
||||
def clone(self):
|
||||
# clone and preserve this websocket in another instance
|
||||
return WebSocket(self.websocket, self.app)
|
||||
return WebSocket(self.ws, self.app)
|
||||
|
||||
def trigger_for_each_topic_handler(self, topic):
|
||||
if hasattr(self, "_for_each_topic_handler") and hasattr(
|
||||
|
@ -1310,6 +1339,7 @@ class WebSocket:
|
|||
return None
|
||||
|
||||
def send_fragment(self, message, compress=False):
|
||||
self.app.loop.is_idle = False
|
||||
try:
|
||||
if isinstance(message, str):
|
||||
data = message.encode("utf-8")
|
||||
|
@ -1330,6 +1360,7 @@ class WebSocket:
|
|||
return None
|
||||
|
||||
def send_last_fragment(self, message, compress=False):
|
||||
self.app.loop.is_idle = False
|
||||
try:
|
||||
if isinstance(message, str):
|
||||
data = message.encode("utf-8")
|
||||
|
@ -1350,6 +1381,7 @@ class WebSocket:
|
|||
return None
|
||||
|
||||
def send_first_fragment(self, message, opcode=OpCode.BINARY, compress=False):
|
||||
self.app.loop.is_idle = False
|
||||
try:
|
||||
if isinstance(message, str):
|
||||
data = message.encode("utf-8")
|
||||
|
@ -1376,6 +1408,7 @@ class WebSocket:
|
|||
return self
|
||||
|
||||
def send(self, message, opcode=OpCode.BINARY, compress=False, fin=True):
|
||||
self.app.loop.is_idle = False
|
||||
try:
|
||||
if isinstance(message, str):
|
||||
data = message.encode("utf-8")
|
||||
|
@ -1402,6 +1435,7 @@ class WebSocket:
|
|||
return self
|
||||
|
||||
def end(self, code=0, message=None):
|
||||
self.app.loop.is_idle = False
|
||||
try:
|
||||
if not isinstance(code, int):
|
||||
raise RuntimeError("code must be an int")
|
||||
|
@ -1450,6 +1484,8 @@ class AppResponse:
|
|||
self._data = None
|
||||
|
||||
def cork(self, callback):
|
||||
self.app.loop.is_idle = False
|
||||
|
||||
if not self.aborted:
|
||||
self.grab_aborted_handler()
|
||||
self._cork_handler = callback
|
||||
|
@ -1588,13 +1624,14 @@ class AppResponse:
|
|||
) # if aborted set to done True and ok False
|
||||
return self._chunkFuture
|
||||
|
||||
self._lastChunkOffset = self.get_write_offset()
|
||||
|
||||
(ok, done) = self.try_end(buffer, total_size)
|
||||
if ok:
|
||||
self._chunkFuture.set_result((ok, done))
|
||||
return self._chunkFuture
|
||||
# failed to send chunk
|
||||
self._lastChunkOffset = self.get_write_offset()
|
||||
|
||||
# failed to send chunk
|
||||
return self._chunkFuture
|
||||
|
||||
def get_data(self):
|
||||
|
@ -1610,7 +1647,8 @@ class AppResponse:
|
|||
pass
|
||||
|
||||
def get_chunks(self, chunk, is_end):
|
||||
self._data.write(chunk)
|
||||
if chunk is not None:
|
||||
self._data.write(chunk)
|
||||
if is_end:
|
||||
self._dataFuture.set_result(self._data)
|
||||
self._data = None
|
||||
|
@ -1631,7 +1669,9 @@ class AppResponse:
|
|||
def redirect(self, location, status_code=302):
|
||||
self.write_status(status_code)
|
||||
self.write_header("Location", location)
|
||||
self.end_without_body(False)
|
||||
# this is buggy need to update uWS
|
||||
# self.end_without_body(False)
|
||||
self.end("")
|
||||
return self
|
||||
|
||||
def write_offset(self, offset):
|
||||
|
@ -1640,7 +1680,14 @@ class AppResponse:
|
|||
)
|
||||
return self
|
||||
|
||||
def close(self):
|
||||
lib.uws_res_close(
|
||||
self.app.SSL, self.res
|
||||
)
|
||||
return self
|
||||
|
||||
def try_end(self, message, total_size, end_connection=False):
|
||||
self.app.loop.is_idle = False
|
||||
try:
|
||||
if self.aborted:
|
||||
return False, True
|
||||
|
@ -1734,6 +1781,7 @@ class AppResponse:
|
|||
headers=None,
|
||||
end_connection: bool = False,
|
||||
):
|
||||
# TODO: use socketify_res_cork_send_int_code and socketify_res_cork_send after optimize headers
|
||||
self.cork(
|
||||
lambda res: res.send(message, content_type, status, headers, end_connection)
|
||||
)
|
||||
|
@ -1741,46 +1789,114 @@ class AppResponse:
|
|||
|
||||
def send(
|
||||
self,
|
||||
message: any,
|
||||
message: any = b"",
|
||||
content_type: Union[str, bytes] = b"text/plain",
|
||||
status: Union[str, bytes, int] = b"200 OK",
|
||||
headers=None,
|
||||
headers = None,
|
||||
end_connection: bool = False,
|
||||
):
|
||||
if self.aborted:
|
||||
return self
|
||||
|
||||
self.write_status(status)
|
||||
self.app.loop.is_idle = False
|
||||
|
||||
# TODO: optimize headers
|
||||
if headers is not None:
|
||||
for name, value in headers:
|
||||
self.write_header(name, value)
|
||||
try:
|
||||
|
||||
# TODO: optimize Set-Cookie
|
||||
if self._write_jar is not None:
|
||||
self.write_header("Set-Cookie", self._write_jar.output(header=""))
|
||||
self._write_jar = None
|
||||
|
||||
if isinstance(message, str):
|
||||
data = message.encode("utf-8")
|
||||
self.write_header(b"Content-Type", content_type)
|
||||
elif isinstance(message, bytes):
|
||||
self.write_header(b"Content-Type", content_type)
|
||||
data = message
|
||||
elif message is None:
|
||||
self.write_header(b"Content-Type", content_type)
|
||||
self.end_without_body(end_connection)
|
||||
if isinstance(status, int):
|
||||
lib.socketify_res_send_int_code(
|
||||
self.app.SSL,
|
||||
self.res,
|
||||
ffi.NULL,
|
||||
0,
|
||||
status,
|
||||
content_type,
|
||||
len(content_type),
|
||||
1 if end_connection else 0,
|
||||
)
|
||||
elif isinstance(status, str):
|
||||
status = status.encode("utf-8")
|
||||
lib.socketify_res_send(
|
||||
self.app.SSL,
|
||||
self.res,
|
||||
ffi.NULL,
|
||||
0,
|
||||
status,
|
||||
len(status),
|
||||
content_type,
|
||||
len(content_type),
|
||||
1 if end_connection else 0,
|
||||
)
|
||||
else:
|
||||
lib.socketify_res_send(
|
||||
self.app.SSL,
|
||||
self.res,
|
||||
ffi.NULL,
|
||||
0,
|
||||
status,
|
||||
len(status),
|
||||
content_type,
|
||||
len(content_type),
|
||||
1 if end_connection else 0,
|
||||
)
|
||||
return self
|
||||
else:
|
||||
data = self.app._json_serializer.dumps(message).encode("utf-8")
|
||||
# ignores content_type should always be json here
|
||||
self.write_header(b"Content-Type", b"application/json")
|
||||
content_type = b"application/json"
|
||||
|
||||
if isinstance(status, int):
|
||||
lib.socketify_res_send_int_code(
|
||||
self.app.SSL,
|
||||
self.res,
|
||||
data,
|
||||
len(data),
|
||||
status,
|
||||
content_type,
|
||||
len(content_type),
|
||||
1 if end_connection else 0,
|
||||
)
|
||||
elif isinstance(status, str):
|
||||
status = status.encode("utf-8")
|
||||
lib.socketify_res_send(
|
||||
self.app.SSL,
|
||||
self.res,
|
||||
ffi.NULL,
|
||||
0,
|
||||
status,
|
||||
len(status),
|
||||
content_type,
|
||||
len(content_type),
|
||||
1 if end_connection else 0,
|
||||
)
|
||||
else:
|
||||
lib.socketify_res_send(
|
||||
self.app.SSL,
|
||||
self.res,
|
||||
data,
|
||||
len(data),
|
||||
status,
|
||||
len(status),
|
||||
content_type,
|
||||
len(content_type),
|
||||
1 if end_connection else 0,
|
||||
)
|
||||
|
||||
lib.uws_res_end(
|
||||
self.app.SSL, self.res, data, len(data), 1 if end_connection else 0
|
||||
)
|
||||
finally:
|
||||
return self
|
||||
|
||||
def end(self, message, end_connection=False):
|
||||
self.app.loop.is_idle = False
|
||||
|
||||
try:
|
||||
if self.aborted:
|
||||
return self
|
||||
|
@ -1809,16 +1925,19 @@ class AppResponse:
|
|||
return self
|
||||
|
||||
def resume(self):
|
||||
self.app.loop.is_idle = False
|
||||
if not self.aborted:
|
||||
lib.uws_res_resume(self.app.SSL, self.res)
|
||||
return self
|
||||
|
||||
def write_continue(self):
|
||||
self.app.loop.is_idle = False
|
||||
if not self.aborted:
|
||||
lib.uws_res_write_continue(self.app.SSL, self.res)
|
||||
return self
|
||||
|
||||
def write_status(self, status_or_status_text):
|
||||
self.app.loop.is_idle = False
|
||||
if not self.aborted:
|
||||
if isinstance(status_or_status_text, int):
|
||||
if bool(
|
||||
|
@ -1844,6 +1963,7 @@ class AppResponse:
|
|||
return self
|
||||
|
||||
def write_header(self, key, value):
|
||||
self.app.loop.is_idle = False
|
||||
if not self.aborted:
|
||||
if isinstance(key, str):
|
||||
key_data = key.encode("utf-8")
|
||||
|
@ -1877,6 +1997,7 @@ class AppResponse:
|
|||
return self
|
||||
|
||||
def end_without_body(self, end_connection=False):
|
||||
self.app.loop.is_idle = False
|
||||
if not self.aborted:
|
||||
if self._write_jar is not None:
|
||||
self.write_header("Set-Cookie", self._write_jar.output(header=""))
|
||||
|
@ -1886,6 +2007,7 @@ class AppResponse:
|
|||
return self
|
||||
|
||||
def write(self, message):
|
||||
self.app.loop.is_idle = False
|
||||
if not self.aborted:
|
||||
if isinstance(message, str):
|
||||
data = message.encode("utf-8")
|
||||
|
@ -2250,6 +2372,24 @@ class AppRequest:
|
|||
except Exception:
|
||||
return None
|
||||
|
||||
def get_cookies(self):
|
||||
if self.read_jar is None:
|
||||
if self.jar_parsed:
|
||||
return None
|
||||
|
||||
if self._headers:
|
||||
raw_cookies = self._headers.get("cookie", None)
|
||||
else:
|
||||
raw_cookies = self.get_header("cookie")
|
||||
|
||||
if raw_cookies:
|
||||
self.jar_parsed = True
|
||||
self.read_jar = cookies.SimpleCookie(raw_cookies)
|
||||
else:
|
||||
self.jar_parsed = True
|
||||
return None
|
||||
return self.read_jar
|
||||
|
||||
def get_url(self):
|
||||
if self._url:
|
||||
return self._url
|
||||
|
@ -2411,7 +2551,7 @@ class AppRequest:
|
|||
return self
|
||||
|
||||
def set_yield(self, has_yield):
|
||||
lib.uws_req_set_field(self.req, 1 if has_yield else 0)
|
||||
lib.uws_req_set_yield(self.req, 1 if has_yield else 0)
|
||||
|
||||
def get_yield(self):
|
||||
return bool(lib.uws_req_get_yield(self.req))
|
||||
|
@ -2477,50 +2617,78 @@ class App:
|
|||
websocket_factory_max_items=0,
|
||||
task_factory_max_items=100_000,
|
||||
lifespan=True,
|
||||
idle_relaxation_time=0.01,
|
||||
):
|
||||
|
||||
socket_options_ptr = ffi.new("struct us_socket_context_options_t *")
|
||||
socket_options = socket_options_ptr[0]
|
||||
self.options = options
|
||||
self._options = options
|
||||
self._template = None
|
||||
self.lifespan = lifespan
|
||||
# keep socket data alive for CFFI
|
||||
self._socket_refs = {}
|
||||
self._native_options = []
|
||||
if options is not None:
|
||||
self.is_ssl = True
|
||||
self.SSL = ffi.cast("int", 1)
|
||||
socket_options.key_file_name = (
|
||||
|
||||
key_filename = (
|
||||
ffi.NULL
|
||||
if options.key_file_name is None
|
||||
else ffi.new("char[]", options.key_file_name.encode("utf-8"))
|
||||
)
|
||||
socket_options.cert_file_name = (
|
||||
self._native_options.append(key_filename)
|
||||
socket_options.key_file_name = key_filename
|
||||
|
||||
cert_file_name = (
|
||||
ffi.NULL
|
||||
if options.cert_file_name is None
|
||||
else ffi.new("char[]", options.cert_file_name.encode("utf-8"))
|
||||
)
|
||||
socket_options.passphrase = (
|
||||
|
||||
self._native_options.append(cert_file_name)
|
||||
socket_options.cert_file_name = cert_file_name
|
||||
|
||||
passphrase = (
|
||||
ffi.NULL
|
||||
if options.passphrase is None
|
||||
else ffi.new("char[]", options.passphrase.encode("utf-8"))
|
||||
)
|
||||
socket_options.dh_params_file_name = (
|
||||
|
||||
self._native_options.append(passphrase)
|
||||
socket_options.passphrase = passphrase
|
||||
|
||||
dh_params_file_name = (
|
||||
ffi.NULL
|
||||
if options.dh_params_file_name is None
|
||||
else ffi.new("char[]", options.dh_params_file_name.encode("utf-8"))
|
||||
)
|
||||
socket_options.ca_file_name = (
|
||||
|
||||
self._native_options.append(dh_params_file_name)
|
||||
socket_options.dh_params_file_name = dh_params_file_name
|
||||
|
||||
ca_file_name = (
|
||||
ffi.NULL
|
||||
if options.ca_file_name is None
|
||||
else ffi.new("char[]", options.ca_file_name.encode("utf-8"))
|
||||
)
|
||||
socket_options.ssl_ciphers = (
|
||||
|
||||
self._native_options.append(ca_file_name)
|
||||
socket_options.ca_file_name = ca_file_name
|
||||
|
||||
ssl_ciphers = (
|
||||
ffi.NULL
|
||||
if options.ssl_ciphers is None
|
||||
else ffi.new("char[]", options.ssl_ciphers.encode("utf-8"))
|
||||
)
|
||||
|
||||
self._native_options.append(ssl_ciphers)
|
||||
socket_options.ssl_ciphers = ssl_ciphers
|
||||
|
||||
socket_options.ssl_prefer_low_memory_usage = ffi.cast(
|
||||
"int", options.ssl_prefer_low_memory_usage
|
||||
)
|
||||
|
||||
else:
|
||||
self.is_ssl = False
|
||||
self.SSL = ffi.cast("int", 0)
|
||||
|
@ -2528,8 +2696,9 @@ class App:
|
|||
self.loop = Loop(
|
||||
lambda loop, context, response: self.trigger_error(context, response, None),
|
||||
task_factory_max_items,
|
||||
idle_relaxation_time,
|
||||
)
|
||||
|
||||
self.run_async = self.loop.run_async
|
||||
# set async loop to be the last created (is thread_local), App must be one per thread otherwise will use only the lasted loop
|
||||
# needs to be called before uws_create_app or otherwise will create another loop and will not receive the right one
|
||||
lib.uws_get_loop_with_native(self.loop.get_native_loop())
|
||||
|
@ -2826,6 +2995,7 @@ class App:
|
|||
)
|
||||
|
||||
def publish(self, topic, message, opcode=OpCode.BINARY, compress=False):
|
||||
self.loop.is_idle = False
|
||||
|
||||
if isinstance(topic, str):
|
||||
topic_data = topic.encode("utf-8")
|
||||
|
@ -3167,10 +3337,10 @@ class App:
|
|||
native_options = ffi.new("uws_app_listen_config_t *")
|
||||
options = native_options[0]
|
||||
port = port_or_options.get("port", 0)
|
||||
options = port_or_options.get("options", 0)
|
||||
options_ = port_or_options.get("options", 0)
|
||||
host = port_or_options.get("host", "0.0.0.0")
|
||||
options.port = (
|
||||
ffi.cast("int", port, 0)
|
||||
ffi.cast("int", port)
|
||||
if isinstance(port, int)
|
||||
else ffi.cast("int", 0)
|
||||
)
|
||||
|
@ -3180,8 +3350,8 @@ class App:
|
|||
else ffi.NULL
|
||||
)
|
||||
options.options = (
|
||||
ffi.cast("int", port)
|
||||
if isinstance(options, int)
|
||||
ffi.cast("int", options_)
|
||||
if isinstance(options_, int)
|
||||
else ffi.cast("int", 0)
|
||||
)
|
||||
self.native_options_listen = native_options # Keep alive native_options
|
||||
|
@ -3217,8 +3387,6 @@ class App:
|
|||
|
||||
return self
|
||||
|
||||
def run_async(self, task, response=None):
|
||||
return self.loop.run_async(task, response)
|
||||
|
||||
def run(self):
|
||||
# populate factories
|
||||
|
@ -3227,7 +3395,11 @@ class App:
|
|||
if self._ws_factory is not None:
|
||||
self._ws_factory.populate()
|
||||
|
||||
signal.signal(signal.SIGINT, lambda sig, frame: self.close())
|
||||
def signal_handler(sig, frame):
|
||||
self.close()
|
||||
exit(0)
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
self.loop.run()
|
||||
if self.lifespan:
|
||||
|
||||
|
@ -3271,7 +3443,7 @@ class App:
|
|||
if self.error_handler is None:
|
||||
try:
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(error)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
response.write_status(500).end("Internal Error")
|
||||
finally:
|
||||
|
@ -3288,7 +3460,7 @@ class App:
|
|||
try:
|
||||
# Error handler got an error :D
|
||||
logging.error(
|
||||
"Uncaught Exception: %s" % str(error)
|
||||
"Uncaught Exception: %s" % traceback.format_exc()
|
||||
) # just log in console the error to call attention
|
||||
response.write_status(500).end("Internal Error")
|
||||
finally:
|
||||
|
@ -3305,60 +3477,13 @@ class App:
|
|||
self.loop = None
|
||||
|
||||
def __del__(self):
|
||||
if self.app: # only destroy if exists
|
||||
self.close()
|
||||
lib.uws_app_destroy(self.SSL, self.app)
|
||||
if self.loop:
|
||||
self.loop.dispose()
|
||||
self.loop = None
|
||||
try:
|
||||
if self.app: # only destroy if exists
|
||||
self.close()
|
||||
lib.uws_app_destroy(self.SSL, self.app)
|
||||
if self.loop:
|
||||
self.loop.dispose()
|
||||
self.loop = None
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
class AppListenOptions:
|
||||
port: int = 0
|
||||
host: str = None
|
||||
options: int = 0
|
||||
domain: str = None
|
||||
|
||||
def __post_init__(self):
|
||||
if not isinstance(self.port, int):
|
||||
raise RuntimeError("port must be an int")
|
||||
if not isinstance(self.host, (type(None), str)):
|
||||
raise RuntimeError("host must be a str if specified")
|
||||
if not isinstance(self.domain, (type(None), str)):
|
||||
raise RuntimeError("domain must be a str if specified")
|
||||
if not isinstance(self.options, int):
|
||||
raise RuntimeError("options must be an int")
|
||||
if self.domain and (self.host or self.port != 0):
|
||||
raise RuntimeError(
|
||||
"if domain is specified, host and port will be no effect"
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class AppOptions:
|
||||
key_file_name: str = None
|
||||
cert_file_name: str = None
|
||||
passphrase: str = None
|
||||
dh_params_file_name: str = None
|
||||
ca_file_name: str = None
|
||||
ssl_ciphers: str = None
|
||||
ssl_prefer_low_memory_usage: int = 0
|
||||
|
||||
def __post_init__(self):
|
||||
NoneType = type(None)
|
||||
|
||||
if not isinstance(self.key_file_name, (NoneType, str)):
|
||||
raise RuntimeError("key_file_name must be a str if specified")
|
||||
if not isinstance(self.cert_file_name, (NoneType, str)):
|
||||
raise RuntimeError("cert_file_name must be a str if specified")
|
||||
if not isinstance(self.passphrase, (NoneType, str)):
|
||||
raise RuntimeError("passphrase must be a str if specified")
|
||||
if not isinstance(self.dh_params_file_name, (NoneType, str)):
|
||||
raise RuntimeError("dh_params_file_name must be a str if specified")
|
||||
if not isinstance(self.ca_file_name, (NoneType, str)):
|
||||
raise RuntimeError("ca_file_name must be a str if specified")
|
||||
if not isinstance(self.ssl_ciphers, (NoneType, str)):
|
||||
raise RuntimeError("ssl_ciphers must be a str if specified")
|
||||
if not isinstance(self.ssl_prefer_low_memory_usage, int):
|
||||
raise RuntimeError("ssl_prefer_low_memory_usage must be an int")
|
||||
|
|
|
@ -25,7 +25,7 @@ class SSGIHttpResponse:
|
|||
# send chunk of data, can be used to perform with less backpressure than using send
|
||||
# total_size is the sum of all lengths in bytes of all chunks to be sended
|
||||
# connection will end when total_size is met
|
||||
# returns tuple(bool, bool) first bool represents if the chunk is succefully sended, the second if the connection has ended
|
||||
# returns tuple(bool, bool) first bool represents if the chunk is successfully sended, the second if the connection has ended
|
||||
def send_chunk(self, chunk: Union[bytes, bytearray, memoryview], total_size: int) -> Awaitable:
|
||||
return self.res.send_chunk(chunk, total_size)
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ from asyncio import (
|
|||
futures,
|
||||
_register_task,
|
||||
_enter_task,
|
||||
current_task,
|
||||
_leave_task,
|
||||
_unregister_task,
|
||||
)
|
||||
|
@ -36,7 +37,9 @@ class RequestTask:
|
|||
|
||||
Differences:
|
||||
|
||||
- This class is only used by socketify.py loop.run_async
|
||||
- This class do not support current_task
|
||||
|
||||
- This class executes the first step like node.js Promise
|
||||
|
||||
- This class is not thread-safe.
|
||||
|
||||
|
@ -89,6 +92,7 @@ class RequestTask:
|
|||
# status is still pending
|
||||
_log_destroy_pending = True
|
||||
|
||||
_parent_task = None
|
||||
def __init__(
|
||||
self, coro, loop, default_done_callback=None, no_register=False, context=None
|
||||
):
|
||||
|
@ -115,8 +119,12 @@ class RequestTask:
|
|||
self._log_destroy_pending = False
|
||||
if self._loop.get_debug():
|
||||
self._source_traceback = format_helpers.extract_stack(sys._getframe(1))
|
||||
self._loop.call_soon(self.__step, context=self._context)
|
||||
_register_task(self)
|
||||
if loop.is_running():
|
||||
self.__step()
|
||||
else:
|
||||
self._loop.call_soon(self.__step, context=self._context)
|
||||
|
||||
|
||||
def _reuse(self, coro, loop, default_done_callback=None):
|
||||
"""Reuse an future that is not pending anymore."""
|
||||
|
@ -147,8 +155,12 @@ class RequestTask:
|
|||
self._fut_waiter = None
|
||||
self._coro = coro
|
||||
|
||||
self._loop.call_soon(self.__step, context=self._context)
|
||||
_register_task(self)
|
||||
# if current_task():
|
||||
# self._loop.call_soon(self.__step, context=self._context)
|
||||
# else:
|
||||
self.__step()
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
return base_tasks._task_repr(self)
|
||||
|
@ -488,6 +500,10 @@ class RequestTask:
|
|||
coro = self._coro
|
||||
self._fut_waiter = None
|
||||
|
||||
_parent_task = current_task(self._loop)
|
||||
if _parent_task is not None:
|
||||
_leave_task(self._loop, _parent_task)
|
||||
self._parent_task = _parent_task
|
||||
_enter_task(self._loop, self)
|
||||
# Call either coro.throw(exc) or coro.send(None).
|
||||
try:
|
||||
|
@ -559,6 +575,9 @@ class RequestTask:
|
|||
self._loop.call_soon(self.__step, new_exc, context=self._context)
|
||||
finally:
|
||||
_leave_task(self._loop, self)
|
||||
if self._parent_task is not None:
|
||||
_enter_task(self._loop, self._parent_task)
|
||||
self._parent_task = None
|
||||
self = None # Needed to break cycles when an exception occurs.
|
||||
|
||||
def __wakeup(self, future):
|
||||
|
@ -580,29 +599,29 @@ class RequestTask:
|
|||
__iter__ = __await__ # make compatible with 'yield from'.
|
||||
|
||||
|
||||
def create_task_with_factory(task_factory_max_items=100_000):
|
||||
items = []
|
||||
for _ in range(0, task_factory_max_items):
|
||||
task = RequestTask(None, None, None, True)
|
||||
if task._source_traceback:
|
||||
del task._source_traceback[-1]
|
||||
items.append(task)
|
||||
async def factory_task_wrapper(task, dispose):
|
||||
try:
|
||||
await task
|
||||
finally:
|
||||
dispose()
|
||||
|
||||
def factory(loop, coro, default_done_callback=None):
|
||||
if len(items) == 0:
|
||||
return create_task(loop, coro, default_done_callback)
|
||||
task = items.pop()
|
||||
class TaskFactory:
|
||||
def __init__(self, task_factory_max_items=100_000):
|
||||
self.items = []
|
||||
for _ in range(0, task_factory_max_items):
|
||||
task = RequestTask(None, None, None, True)
|
||||
if task._source_traceback:
|
||||
del task._source_traceback[-1]
|
||||
self.items.append(task)
|
||||
|
||||
def done(f):
|
||||
if default_done_callback is not None:
|
||||
default_done_callback(f)
|
||||
items.append(f)
|
||||
def __call__(self, loop, coro):
|
||||
if len(self.items) == 0:
|
||||
return create_task(loop, coro)
|
||||
task = self.items.pop()
|
||||
|
||||
task._reuse(coro, loop, done)
|
||||
task._reuse(factory_task_wrapper(coro, lambda : self.items.append(task)), loop)
|
||||
return task
|
||||
|
||||
return factory
|
||||
|
||||
|
||||
def create_task(loop, coro, default_done_callback=None, context=None):
|
||||
"""Schedule a coroutine object.
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 7187fc3d658d4335cdf0c79371eeb8310717b95c
|
||||
Subproject commit f33291d10a2051ba0f10c18761c030fb00390fdf
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
from .native import ffi, lib
|
||||
|
||||
|
||||
|
|
|
@ -7,14 +7,16 @@ from .native import lib, ffi
|
|||
import platform
|
||||
|
||||
is_pypy = platform.python_implementation() == "PyPy"
|
||||
from .tasks import create_task, create_task_with_factory
|
||||
from .tasks import create_task, TaskFactory
|
||||
import sys
|
||||
import logging
|
||||
|
||||
import uuid
|
||||
|
||||
@ffi.callback("void(uws_res_t*, const char*, size_t, bool, void*)")
|
||||
def wsgi_on_data_handler(res, chunk, chunk_length, is_end, user_data):
|
||||
data_response = ffi.from_handle(user_data)
|
||||
data_response.app.server.loop.is_idle = False
|
||||
|
||||
if chunk != ffi.NULL:
|
||||
data_response.buffer.write(ffi.unpack(chunk, chunk_length))
|
||||
if bool(is_end):
|
||||
|
@ -25,6 +27,87 @@ def wsgi_on_data_handler(res, chunk, chunk_length, is_end, user_data):
|
|||
data_response._ptr,
|
||||
)
|
||||
|
||||
@ffi.callback("void(uws_res_t*, void*)")
|
||||
def wsgi_on_data_ref_abort_handler(res, user_data):
|
||||
data_retry = ffi.from_handle(user_data)
|
||||
data_retry.aborted = True
|
||||
data_retry.server.loop.is_idle = False
|
||||
if data_retry.id is not None:
|
||||
data_retry.app._data_refs.pop(data_retry.id, None)
|
||||
|
||||
|
||||
@ffi.callback("bool(uws_res_t*, uintmax_t, void*)")
|
||||
def wsgi_on_writable_handler(res, offset, user_data):
|
||||
data_retry = ffi.from_handle(user_data)
|
||||
if data_retry.aborted:
|
||||
return False
|
||||
|
||||
chunks = data_retry.chunks
|
||||
last_sended_offset = data_retry.last_offset
|
||||
server = data_retry.app.server
|
||||
ssl = server.SSL
|
||||
server.loop.is_idle = False
|
||||
content_length = data_retry.content_length
|
||||
|
||||
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 True
|
||||
data = chunks[0]
|
||||
|
||||
result = lib.uws_res_try_end(
|
||||
ssl,
|
||||
res,
|
||||
data,
|
||||
data_size,
|
||||
content_length,
|
||||
0,
|
||||
)
|
||||
has_responded = bool(result.has_responded)
|
||||
ok = bool(result.ok)
|
||||
data_retry.last_offset = int(lib.uws_res_get_write_offset(ssl, res))
|
||||
|
||||
if 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)
|
||||
elif 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)
|
||||
|
||||
|
||||
return ok
|
||||
|
||||
class WSGIBody:
|
||||
def __init__(self, buffer):
|
||||
|
@ -115,14 +198,25 @@ class WSGIBody:
|
|||
|
||||
|
||||
class WSGIDataResponse:
|
||||
def __init__(self, app, environ, start_response, aborted, buffer, on_data):
|
||||
def __init__(self, app, environ, start_response, buffer, on_data):
|
||||
self.buffer = buffer
|
||||
self.aborted = aborted
|
||||
self._ptr = ffi.new_handle(self)
|
||||
self.on_data = on_data
|
||||
self.environ = environ
|
||||
self.app = app
|
||||
self.start_response = start_response
|
||||
self.id = None
|
||||
self.aborted = False
|
||||
|
||||
class WSGIRetryDataSend:
|
||||
def __init__(self, app, chunks, content_length, last_offset):
|
||||
self.chunks = chunks
|
||||
self._ptr = ffi.new_handle(self)
|
||||
self.app = app
|
||||
self.content_length = content_length
|
||||
self.last_offset = last_offset
|
||||
self.id = None
|
||||
self.aborted = False
|
||||
|
||||
|
||||
@ffi.callback("void(uws_res_t*, void*)")
|
||||
|
@ -131,9 +225,12 @@ def wsgi_corked_response_start_handler(res, user_data):
|
|||
data_response.on_data(data_response, res)
|
||||
|
||||
|
||||
@ffi.callback("void(int, uws_res_t*, socketify_asgi_data request, void*, bool*)")
|
||||
def wsgi(ssl, response, info, user_data, aborted):
|
||||
|
||||
@ffi.callback("void(int, uws_res_t*, socketify_asgi_data request, void*)")
|
||||
def wsgi(ssl, response, info, user_data):
|
||||
app = ffi.from_handle(user_data)
|
||||
app.server.loop.is_idle = False
|
||||
|
||||
# reusing the dict is slower than cloning because we need to clear HTTP headers
|
||||
environ = dict(app.BASIC_ENVIRON)
|
||||
|
||||
|
@ -141,7 +238,7 @@ def wsgi(ssl, response, info, user_data, aborted):
|
|||
environ["PATH_INFO"] = ffi.unpack(info.url, info.url_size).decode("utf8")
|
||||
environ["QUERY_STRING"] = ffi.unpack(
|
||||
info.query_string, info.query_string_size
|
||||
).decode("utf8")
|
||||
).decode("utf8")[1:]
|
||||
if info.remote_address != ffi.NULL:
|
||||
environ["REMOTE_ADDR"] = ffi.unpack(
|
||||
info.remote_address, info.remote_address_size
|
||||
|
@ -163,11 +260,13 @@ def wsgi(ssl, response, info, user_data, aborted):
|
|||
headers_set = None
|
||||
headers_written = False
|
||||
status_text = None
|
||||
|
||||
is_chunked = False
|
||||
content_length = -1
|
||||
def write_headers(headers):
|
||||
nonlocal headers_written, headers_set, status_text
|
||||
nonlocal headers_written, headers_set, status_text, content_length, is_chunked, app
|
||||
if headers_written or not headers_set:
|
||||
return
|
||||
app.server.loop.is_idle = False
|
||||
|
||||
headers_written = True
|
||||
|
||||
|
@ -183,20 +282,33 @@ def wsgi(ssl, response, info, user_data, aborted):
|
|||
if (
|
||||
key == "content-length"
|
||||
or key == "Content-Length"
|
||||
or key == "Transfer-Encoding"
|
||||
):
|
||||
content_length = int(value)
|
||||
continue # auto generated by try_end
|
||||
if (
|
||||
key == "Transfer-Encoding"
|
||||
or key == "transfer-encoding"
|
||||
):
|
||||
continue # auto
|
||||
is_chunked = str(value) == "chunked"
|
||||
if is_chunked:
|
||||
continue
|
||||
|
||||
key_data = key.encode("utf-8")
|
||||
elif isinstance(key, bytes):
|
||||
# this is faster than using .lower()
|
||||
if (
|
||||
key == b"content-length"
|
||||
or key == b"Content-Length"
|
||||
or key == b"Transfer-Encoding"
|
||||
):
|
||||
content_length = int(value)
|
||||
continue # auto
|
||||
if (
|
||||
key == b"Transfer-Encoding"
|
||||
or key == b"transfer-encoding"
|
||||
):
|
||||
continue # auto
|
||||
is_chunked = str(value) == "chunked"
|
||||
if is_chunked:
|
||||
continue
|
||||
key_data = key
|
||||
|
||||
if isinstance(value, str):
|
||||
|
@ -216,9 +328,14 @@ def wsgi(ssl, response, info, user_data, aborted):
|
|||
lib.uws_res_write_header(
|
||||
ssl, response, key_data, len(key_data), value_data, len(value_data)
|
||||
)
|
||||
# no content-length
|
||||
if content_length < 0:
|
||||
is_chunked = True
|
||||
content_length = ffi.cast("uintmax_t", content_length)
|
||||
|
||||
def start_response(status, headers, exc_info=None):
|
||||
nonlocal headers_set, status_text
|
||||
nonlocal headers_set, status_text, app
|
||||
app.server.loop.is_idle = False
|
||||
if exc_info:
|
||||
try:
|
||||
if headers_written:
|
||||
|
@ -233,8 +350,12 @@ def wsgi(ssl, response, info, user_data, aborted):
|
|||
status_text = status
|
||||
|
||||
def write(data):
|
||||
nonlocal is_chunked, app
|
||||
app.server.loop.is_idle = False
|
||||
if not headers_written:
|
||||
write_headers(headers_set)
|
||||
# will allow older frameworks only with is_chunked
|
||||
is_chunked = True
|
||||
|
||||
if isinstance(data, bytes):
|
||||
lib.uws_res_write(ssl, response, data, len(data))
|
||||
|
@ -244,32 +365,69 @@ def wsgi(ssl, response, info, user_data, aborted):
|
|||
|
||||
return write
|
||||
|
||||
|
||||
|
||||
|
||||
# check for body
|
||||
if bool(info.has_content):
|
||||
WSGI_INPUT = BytesIO()
|
||||
environ["wsgi.input"] = WSGIBody(WSGI_INPUT)
|
||||
|
||||
def on_data(data_response, response):
|
||||
if bool(data_response.aborted[0]):
|
||||
return
|
||||
last_offset = -1
|
||||
data_retry = None
|
||||
failed_chunks = None
|
||||
|
||||
|
||||
if data_response.aborted:
|
||||
return
|
||||
data_response.app.server.loop.is_idle = False
|
||||
ssl = data_response.app.server.SSL
|
||||
data_response.environ["CONTENT_LENGTH"] = str(
|
||||
data_response.buffer.getbuffer().nbytes
|
||||
)
|
||||
if data_response.id is not None:
|
||||
data_response.app._data_refs.pop(data_response.id, None)
|
||||
|
||||
app_iter = data_response.app.wsgi(
|
||||
data_response.environ, data_response.start_response
|
||||
)
|
||||
|
||||
try:
|
||||
for data in app_iter:
|
||||
if data and not headers_written:
|
||||
write_headers(headers_set)
|
||||
if data:
|
||||
if not headers_written:
|
||||
write_headers(headers_set)
|
||||
|
||||
if isinstance(data, bytes):
|
||||
lib.uws_res_write(ssl, response, data, len(data))
|
||||
elif isinstance(data, str):
|
||||
data = data.encode("utf-8")
|
||||
lib.uws_res_write(ssl, response, data, len(data))
|
||||
if is_chunked:
|
||||
if isinstance(data, bytes):
|
||||
lib.uws_res_write(ssl, response, data, len(data))
|
||||
elif isinstance(data, str):
|
||||
data = data.encode("utf-8")
|
||||
lib.uws_res_write(ssl, response, data, len(data))
|
||||
else:
|
||||
if isinstance(data, str):
|
||||
data = data.encode("utf-8")
|
||||
if failed_chunks:
|
||||
failed_chunks.append(data)
|
||||
else:
|
||||
last_offset = int(lib.uws_res_get_write_offset(ssl, response))
|
||||
result = lib.uws_res_try_end(
|
||||
ssl,
|
||||
response,
|
||||
data,
|
||||
len(data),
|
||||
content_length,
|
||||
0,
|
||||
)
|
||||
# this should be very very rare for HTTP
|
||||
if not bool(result.ok):
|
||||
failed_chunks = []
|
||||
# just mark the chunks
|
||||
failed_chunks.append(data)
|
||||
# add on writable handler
|
||||
data_retry = WSGIRetryDataSend(
|
||||
app, failed_chunks, content_length, last_offset
|
||||
)
|
||||
|
||||
except Exception as error:
|
||||
logging.exception(error)
|
||||
|
@ -279,26 +437,75 @@ def wsgi(ssl, response, info, user_data, aborted):
|
|||
|
||||
if not headers_written:
|
||||
write_headers(headers_set)
|
||||
lib.uws_res_end_without_body(ssl, response, 0)
|
||||
|
||||
if is_chunked:
|
||||
lib.uws_res_end_without_body(ssl, response, 0)
|
||||
elif data_retry is not None:
|
||||
_id = uuid.uuid4()
|
||||
app._data_refs[_id] = data_retry
|
||||
lib.uws_res_on_aborted(ssl, response, wsgi_on_data_ref_abort_handler, data_retry._ptr)
|
||||
lib.uws_res_on_writable(ssl, response, wsgi_on_writable_handler, data_retry._ptr)
|
||||
elif result is None or (not bool(result.has_responded) and bool(result.ok)): # not reaches Content-Length
|
||||
logging.error(AssertionError("Content-Length do not match sended content"))
|
||||
lib.uws_res_close(
|
||||
ssl,
|
||||
response
|
||||
)
|
||||
data_response = WSGIDataResponse(
|
||||
app, environ, start_response, aborted, WSGI_INPUT, on_data
|
||||
app, environ, start_response, WSGI_INPUT, on_data
|
||||
)
|
||||
|
||||
_id = uuid.uuid4()
|
||||
data_response.id = _id
|
||||
app._data_refs[_id] = data_response
|
||||
lib.uws_res_on_aborted(ssl, response, wsgi_on_data_ref_abort_handler, data_response._ptr)
|
||||
lib.uws_res_on_data(ssl, response, wsgi_on_data_handler, data_response._ptr)
|
||||
else:
|
||||
environ["wsgi.input"] = None
|
||||
failed_chunks = None
|
||||
last_offset = -1
|
||||
data_retry = None
|
||||
# Django do not check for None with is lame
|
||||
# we use the same empty for everyone to avoid extra allocations
|
||||
environ["wsgi.input"] = app.EMPTY_WSGI_BODY
|
||||
# we also set CONTENT_LENGTH to 0 so if Django is lame again its covered
|
||||
environ["CONTENT_LENGTH"] = "0"
|
||||
app_iter = app.wsgi(environ, start_response)
|
||||
result = None
|
||||
try:
|
||||
for data in app_iter:
|
||||
if data and not headers_written:
|
||||
write_headers(headers_set)
|
||||
if data:
|
||||
if not headers_written:
|
||||
write_headers(headers_set)
|
||||
if is_chunked:
|
||||
if isinstance(data, bytes):
|
||||
lib.uws_res_write(ssl, response, data, len(data))
|
||||
elif isinstance(data, str):
|
||||
data = data.encode("utf-8")
|
||||
lib.uws_res_write(ssl, response, data, len(data))
|
||||
else:
|
||||
if isinstance(data, str):
|
||||
data = data.encode("utf-8")
|
||||
if failed_chunks: # if failed once, will fail again later
|
||||
failed_chunks.append(data)
|
||||
else:
|
||||
last_offset = int(lib.uws_res_get_write_offset(ssl, response))
|
||||
result = lib.uws_res_try_end(
|
||||
ssl,
|
||||
response,
|
||||
data,
|
||||
len(data),
|
||||
content_length,
|
||||
0,
|
||||
)
|
||||
# this should be very very rare for HTTP
|
||||
if not bool(result.ok):
|
||||
failed_chunks = []
|
||||
# just mark the chunks
|
||||
failed_chunks.append(data)
|
||||
# add on writable handler
|
||||
data_retry = WSGIRetryDataSend(
|
||||
app, failed_chunks, content_length, last_offset
|
||||
)
|
||||
|
||||
|
||||
if isinstance(data, bytes):
|
||||
lib.uws_res_write(ssl, response, data, len(data))
|
||||
elif isinstance(data, str):
|
||||
data = data.encode("utf-8")
|
||||
lib.uws_res_write(ssl, response, data, len(data))
|
||||
except Exception as error:
|
||||
logging.exception(error)
|
||||
finally:
|
||||
|
@ -307,7 +514,20 @@ def wsgi(ssl, response, info, user_data, aborted):
|
|||
|
||||
if not headers_written:
|
||||
write_headers(headers_set)
|
||||
lib.uws_res_end_without_body(ssl, response, 0)
|
||||
if is_chunked:
|
||||
lib.uws_res_end_without_body(ssl, response, 0)
|
||||
elif data_retry is not None:
|
||||
_id = uuid.uuid4()
|
||||
data_retry.id = _id
|
||||
app._data_refs[_id] = data_retry
|
||||
lib.uws_res_on_aborted(ssl, response, wsgi_on_data_ref_abort_handler, data_retry._ptr)
|
||||
lib.uws_res_on_writable(ssl, response, wsgi_on_writable_handler, data_retry._ptr)
|
||||
elif result is None or (not bool(result.has_responded) and bool(result.ok)): # not reaches Content-Length
|
||||
logging.error(AssertionError("Content-Length do not match sended content"))
|
||||
lib.uws_res_close(
|
||||
ssl,
|
||||
response
|
||||
)
|
||||
|
||||
|
||||
def is_asgi(module):
|
||||
|
@ -328,11 +548,12 @@ class _WSGI:
|
|||
self.server = App(options, task_factory_max_items=0)
|
||||
self.SERVER_HOST = None
|
||||
self.SERVER_PORT = None
|
||||
self.SERVER_WS_SCHEME = "wss" if self.server.options else "ws"
|
||||
self.SERVER_WS_SCHEME = "wss" if self.server._options else "ws"
|
||||
self.wsgi = app
|
||||
self.EMPTY_WSGI_BODY = WSGIBody(BytesIO())
|
||||
self.BASIC_ENVIRON = dict(os.environ)
|
||||
self.ws_compression = False
|
||||
|
||||
self._data_refs = {}
|
||||
self._ptr = ffi.new_handle(self)
|
||||
self.asgi_http_info = lib.socketify_add_asgi_http_handler(
|
||||
self.server.SSL, self.server.app, wsgi, self._ptr
|
||||
|
@ -351,18 +572,17 @@ class _WSGI:
|
|||
# internally will still use custom task factory for pypy because of Loop
|
||||
if is_pypy:
|
||||
if task_factory_max_items > 0:
|
||||
factory = create_task_with_factory(task_factory_max_items)
|
||||
factory = TaskFactory(task_factory_max_items)
|
||||
|
||||
def run_task(task):
|
||||
factory(loop, task)
|
||||
loop._run_once()
|
||||
|
||||
self._run_task = run_task
|
||||
else:
|
||||
|
||||
def run_task(task):
|
||||
create_task(loop, task)
|
||||
loop._run_once()
|
||||
future = create_task(loop, task)
|
||||
future._log_destroy_pending = False
|
||||
|
||||
self._run_task = run_task
|
||||
|
||||
|
@ -370,15 +590,15 @@ class _WSGI:
|
|||
if sys.version_info >= (3, 8): # name fixed to avoid dynamic name
|
||||
|
||||
def run_task(task):
|
||||
loop.create_task(task, name="socketify.py-request-task")
|
||||
loop._run_once()
|
||||
future = create_task(loop, task)
|
||||
future._log_destroy_pending = False
|
||||
|
||||
self._run_task = run_task
|
||||
else:
|
||||
|
||||
def run_task(task):
|
||||
loop.create_task(task)
|
||||
loop._run_once()
|
||||
future = create_task(loop, task)
|
||||
future._log_destroy_pending = False
|
||||
|
||||
self._run_task = run_task
|
||||
|
||||
|
@ -451,7 +671,7 @@ class _WSGI:
|
|||
"wsgi.errors": sys.stderr,
|
||||
"wsgi.version": (1, 0),
|
||||
"wsgi.run_once": False,
|
||||
"wsgi.url_scheme": "https" if self.server.options else "http",
|
||||
"wsgi.url_scheme": "https" if self.server._options and self.server._options.cert_file_name is not None else "http",
|
||||
"wsgi.multithread": False,
|
||||
"wsgi.multiprocess": False,
|
||||
"wsgi.file_wrapper": None, # No file wrapper support for now
|
||||
|
@ -471,10 +691,13 @@ class _WSGI:
|
|||
return self
|
||||
|
||||
def __del__(self):
|
||||
if self.asgi_http_info:
|
||||
lib.socketify_destroy_asgi_app_info(self.asgi_http_info)
|
||||
if self.asgi_ws_info:
|
||||
lib.socketify_destroy_asgi_ws_app_info(self.asgi_ws_info)
|
||||
try:
|
||||
if self.asgi_http_info:
|
||||
lib.socketify_destroy_asgi_app_info(self.asgi_http_info)
|
||||
if self.asgi_ws_info:
|
||||
lib.socketify_destroy_asgi_ws_app_info(self.asgi_ws_info)
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
# "Public" WSGI interface to allow easy forks/workers
|
||||
|
@ -494,14 +717,28 @@ class WSGI:
|
|||
self.websocket_options = websocket_options
|
||||
self.listen_options = None
|
||||
self.task_factory_max_items = task_factory_max_items
|
||||
self.server = None
|
||||
self.pid_list = None
|
||||
# lifespan is not supported in WSGI
|
||||
|
||||
def listen(self, port_or_options, handler=None):
|
||||
self.listen_options = (port_or_options, handler)
|
||||
return self
|
||||
|
||||
def run(self, workers=1):
|
||||
def run_app():
|
||||
def close(self):
|
||||
# always wait a sec so forks can start properly if close is called too fast
|
||||
import time
|
||||
time.sleep(1)
|
||||
|
||||
if self.server is not None:
|
||||
self.server.close()
|
||||
if self.pid_list is not None:
|
||||
import signal
|
||||
for pid in self.pid_list:
|
||||
os.kill(pid, signal.SIGINT)
|
||||
|
||||
def run(self, workers=1, block=True):
|
||||
def run_task():
|
||||
server = _WSGI(
|
||||
self.app,
|
||||
self.options,
|
||||
|
@ -512,17 +749,28 @@ class WSGI:
|
|||
if self.listen_options:
|
||||
(port_or_options, handler) = self.listen_options
|
||||
server.listen(port_or_options, handler)
|
||||
self.server = server
|
||||
server.run()
|
||||
|
||||
def create_fork():
|
||||
n = os.fork()
|
||||
# n greater than 0 means parent process
|
||||
if not n > 0:
|
||||
run_app()
|
||||
pid_list = []
|
||||
|
||||
start = 1 if block else 0
|
||||
# fork limiting the cpu count - 1
|
||||
for i in range(1, workers):
|
||||
create_fork()
|
||||
for _ in range(start, workers):
|
||||
pid = os.fork()
|
||||
# n greater than 0 means parent process
|
||||
if not pid > 0:
|
||||
run_task()
|
||||
break
|
||||
pid_list.append(pid)
|
||||
|
||||
self.pid_list = pid_list
|
||||
|
||||
if block:
|
||||
run_task() # run app on the main process too :)
|
||||
# sigint everything to graceful shutdown
|
||||
import signal
|
||||
for pid in pid_list:
|
||||
os.kill(pid, signal.SIGINT)
|
||||
|
||||
run_app() # run app on the main process too :)
|
||||
return self
|
||||
|
|
Ładowanie…
Reference in New Issue