mirror of
https://github.com/curl/curl.git
synced 2026-07-22 05:27:16 +03:00
Websocket frames need to be written individualy, so that applications can access the frame meta data correctly. This worked until the write function triggered a pause. Remaining frames accumulated in the "out" writer's buffer and on unpaused were written in one chunk. ws decode writer will now stop writing frames when the client writer is paused. To handle the writing of buffered raw data after an unpause, client writers have gotten a new "flush" method. Add pytest test_20_12 with a new client to check handling of pauses and websocket frames. Reported-by: Hendrik Hübner Fixes #22273 Closes #22283
75 lines
2.2 KiB
Python
Executable file
75 lines
2.2 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
#***************************************************************************
|
|
# _ _ ____ _
|
|
# Project ___| | | | _ \| |
|
|
# / __| | | | |_) | |
|
|
# | (__| |_| | _ <| |___
|
|
# \___|\___/|_| \_\_____|
|
|
#
|
|
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
|
#
|
|
# This software is licensed as described in the file COPYING, which
|
|
# you should have received as part of this distribution. The terms
|
|
# are also available at https://curl.se/docs/copyright.html.
|
|
#
|
|
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
# copies of the Software, and permit persons to whom the Software is
|
|
# furnished to do so, under the terms of the COPYING file.
|
|
#
|
|
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
# KIND, either express or implied.
|
|
#
|
|
# SPDX-License-Identifier: curl
|
|
#
|
|
###########################################################################
|
|
#
|
|
import argparse
|
|
import asyncio
|
|
import logging
|
|
|
|
import websockets
|
|
|
|
MESSAGES = [
|
|
"Hello 1",
|
|
"Hello 2",
|
|
"Hello 3",
|
|
"Hello 4",
|
|
]
|
|
|
|
|
|
async def handler(websocket):
|
|
peer = websocket.remote_address
|
|
print(f"client from {peer[0]}:{peer[1]}", flush=True)
|
|
print("handshake complete", flush=True)
|
|
|
|
await asyncio.sleep(0.1)
|
|
for index, payload in enumerate(MESSAGES, start=1):
|
|
await websocket.send(payload)
|
|
print(f"sent frame {index}: {payload!r}", flush=True)
|
|
# await asyncio.sleep(0.2)
|
|
|
|
# await asyncio.sleep(2.0)
|
|
print("server done", flush=True)
|
|
|
|
|
|
async def main():
|
|
parser = argparse.ArgumentParser(prog='scorecard', description="""
|
|
Run a websocket 4frames server.
|
|
""")
|
|
parser.add_argument("--port", type=int,
|
|
default=9876, help="port to listen on")
|
|
args = parser.parse_args()
|
|
|
|
logging.basicConfig(
|
|
format="%(asctime)s %(message)s",
|
|
level=logging.DEBUG,
|
|
)
|
|
|
|
print(f"listening on ws://localhost:{args.port}", flush=True)
|
|
async with websockets.serve(handler, 'localhost', args.port):
|
|
await asyncio.Future()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|