Skip to content

Commit

Permalink
Cleanup websocket handchake response concat
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Oct 26, 2021
1 parent 23b1e17 commit d35854b
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions sanic/server/protocols/websocket_protocol.py
Expand Up @@ -133,21 +133,18 @@ async def websocket_handshake(
)
raise ServerError(msg, status_code=500)
if 100 <= resp.status_code <= 299:
rbody = "".join(
[
"HTTP/1.1 ",
str(resp.status_code),
" ",
resp.reason_phrase,
"\r\n",
]
first_line = (
f"HTTP/1.1 {resp.status_code} {resp.reason_phrase}\r\n"
).encode()
rbody = bytearray(first_line)
rbody += b"".join(
[f"{k}: {v}\r\n".encode() for k, v in resp.headers.items()]
)
rbody += "".join(f"{k}: {v}\r\n" for k, v in resp.headers.items())
rbody += b"\r\n"
if resp.body is not None:
rbody += f"\r\n{resp.body.decode('utf-8')}\r\n\r\n"
else:
rbody += "\r\n"
await super().send(rbody.encode())
rbody += resp.body
rbody += b"\r\n\r\n"
await super().send(rbody)
else:
raise ServerError(resp.body, resp.status_code)
self.websocket = WebsocketImplProtocol(
Expand Down

0 comments on commit d35854b

Please sign in to comment.