MCPcopy Create free account
hub / github.com/apache/httpd / on_async_conn

Function on_async_conn

test/modules/http2/ws_server.py:32–75  ·  view source on GitHub ↗
(conn)

Source from the content-addressed store, hash-verified

30
31
32async def on_async_conn(conn):
33 rpath = str(conn.path)
34 pcomps = rpath[1:].split('/')
35 if len(pcomps) == 0:
36 pcomps = ['echo'] # default handler
37 log.info(f'connection for {pcomps}')
38 if pcomps[0] == 'echo':
39 log.info(f'/echo endpoint')
40 for message in await conn.recv():
41 await conn.send(message)
42 elif pcomps[0] == 'text':
43 await conn.send('hello!')
44 elif pcomps[0] == 'file':
45 if len(pcomps) < 2:
46 conn.close(code=4999, reason='unknown file')
47 return
48 fpath = os.path.join('../', pcomps[1])
49 if not os.path.exists(fpath):
50 conn.close(code=4999, reason='file not found')
51 return
52 bufsize = 0
53 if len(pcomps) > 2:
54 bufsize = int(pcomps[2])
55 if bufsize <= 0:
56 bufsize = 16*1024
57 delay_ms = 0
58 if len(pcomps) > 3:
59 delay_ms = int(pcomps[3])
60 n = 1
61 if len(pcomps) > 4:
62 n = int(pcomps[4])
63 for _ in range(n):
64 with open(fpath, 'r+b') as fd:
65 while True:
66 buf = fd.read(bufsize)
67 if buf is None or len(buf) == 0:
68 break
69 await conn.send(buf)
70 if delay_ms > 0:
71 time.sleep(delay_ms/1000)
72 else:
73 log.info(f'unknown endpoint: {rpath}')
74 await conn.close(code=4999, reason='path unknown')
75 await conn.close(code=1000, reason='')
76
77
78async def run_server(port):

Callers

nothing calls this directly

Calls 3

sendMethod · 0.80
closeMethod · 0.80
joinMethod · 0.80

Tested by

no test coverage detected