Skip to content

Commit 8daa88e

Browse files
committed
Move AbortHandshake to the legacy package.
It is only used by the legacy implementation.
1 parent 94343f6 commit 8daa88e

File tree

6 files changed

+53
-48
lines changed

6 files changed

+53
-48
lines changed

src/websockets/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"HeadersLike",
1515
"MultipleValuesError",
1616
# .exceptions
17-
"AbortHandshake",
1817
"ConnectionClosed",
1918
"ConnectionClosedError",
2019
"ConnectionClosedOK",
@@ -45,6 +44,7 @@
4544
"connect",
4645
"unix_connect",
4746
# .legacy.exceptions
47+
"AbortHandshake",
4848
"InvalidMessage",
4949
"InvalidStatusCode",
5050
# .legacy.protocol
@@ -72,7 +72,6 @@
7272
from .client import ClientProtocol
7373
from .datastructures import Headers, HeadersLike, MultipleValuesError
7474
from .exceptions import (
75-
AbortHandshake,
7675
ConnectionClosed,
7776
ConnectionClosedError,
7877
ConnectionClosedOK,
@@ -101,7 +100,11 @@
101100
basic_auth_protocol_factory,
102101
)
103102
from .legacy.client import WebSocketClientProtocol, connect, unix_connect
104-
from .legacy.exceptions import InvalidMessage, InvalidStatusCode
103+
from .legacy.exceptions import (
104+
AbortHandshake,
105+
InvalidMessage,
106+
InvalidStatusCode,
107+
)
105108
from .legacy.protocol import WebSocketCommonProtocol
106109
from .legacy.server import (
107110
WebSocketServer,
@@ -131,7 +134,6 @@
131134
"HeadersLike": ".datastructures",
132135
"MultipleValuesError": ".datastructures",
133136
# .exceptions
134-
"AbortHandshake": ".exceptions",
135137
"ConnectionClosed": ".exceptions",
136138
"ConnectionClosedError": ".exceptions",
137139
"ConnectionClosedOK": ".exceptions",
@@ -162,6 +164,7 @@
162164
"connect": ".legacy.client",
163165
"unix_connect": ".legacy.client",
164166
# .legacy.exceptions
167+
"AbortHandshake": ".legacy.exceptions",
165168
"InvalidMessage": ".legacy.exceptions",
166169
"InvalidStatusCode": ".legacy.exceptions",
167170
# .legacy.protocol

src/websockets/exceptions.py

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* :exc:`DuplicateParameter`
2020
* :exc:`InvalidParameterName`
2121
* :exc:`InvalidParameterValue`
22-
* :exc:`AbortHandshake`
22+
* :exc:`AbortHandshake` (legacy)
2323
* :exc:`RedirectHandshake`
2424
* :exc:`InvalidState`
2525
* :exc:`InvalidURI`
@@ -30,13 +30,11 @@
3030

3131
from __future__ import annotations
3232

33-
import http
3433
import typing
3534
import warnings
3635

37-
from . import datastructures, frames, http11
36+
from . import frames, http11
3837
from .imports import lazy_import
39-
from .typing import StatusLike
4038

4139

4240
__all__ = [
@@ -302,40 +300,6 @@ def __str__(self) -> str:
302300
return f"invalid value for parameter {self.name}: {self.value}"
303301

304302

305-
class AbortHandshake(InvalidHandshake):
306-
"""
307-
Raised to abort the handshake on purpose and return an HTTP response.
308-
309-
This exception is an implementation detail.
310-
311-
The public API is
312-
:meth:`~websockets.legacy.server.WebSocketServerProtocol.process_request`.
313-
314-
Attributes:
315-
status (~http.HTTPStatus): HTTP status code.
316-
headers (Headers): HTTP response headers.
317-
body (bytes): HTTP response body.
318-
"""
319-
320-
def __init__(
321-
self,
322-
status: StatusLike,
323-
headers: datastructures.HeadersLike,
324-
body: bytes = b"",
325-
) -> None:
326-
# If a user passes an int instead of a HTTPStatus, fix it automatically.
327-
self.status = http.HTTPStatus(status)
328-
self.headers = datastructures.Headers(headers)
329-
self.body = body
330-
331-
def __str__(self) -> str:
332-
return (
333-
f"HTTP {self.status:d}, "
334-
f"{len(self.headers)} headers, "
335-
f"{len(self.body)} bytes"
336-
)
337-
338-
339303
class RedirectHandshake(InvalidHandshake):
340304
"""
341305
Raised when a handshake gets redirected.
@@ -393,6 +357,7 @@ class ProtocolError(WebSocketException):
393357
# When type checking, import non-deprecated aliases eagerly. Else, import on demand.
394358
if typing.TYPE_CHECKING:
395359
from .legacy.exceptions import (
360+
AbortHandshake,
396361
InvalidMessage,
397362
InvalidStatusCode,
398363
)
@@ -402,6 +367,7 @@ class ProtocolError(WebSocketException):
402367
lazy_import(
403368
globals(),
404369
aliases={
370+
"AbortHandshake": ".legacy.exceptions",
405371
"InvalidMessage": ".legacy.exceptions",
406372
"InvalidStatusCode": ".legacy.exceptions",
407373
"WebSocketProtocolError": ".legacy.exceptions",

src/websockets/legacy/exceptions.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import http
2+
13
from .. import datastructures
24
from ..exceptions import (
35
InvalidHandshake,
46
ProtocolError as WebSocketProtocolError, # noqa: F401
57
)
8+
from ..typing import StatusLike
69

710

811
class InvalidMessage(InvalidHandshake):
@@ -24,3 +27,37 @@ def __init__(self, status_code: int, headers: datastructures.Headers) -> None:
2427

2528
def __str__(self) -> str:
2629
return f"server rejected WebSocket connection: HTTP {self.status_code}"
30+
31+
32+
class AbortHandshake(InvalidHandshake):
33+
"""
34+
Raised to abort the handshake on purpose and return an HTTP response.
35+
36+
This exception is an implementation detail.
37+
38+
The public API is
39+
:meth:`~websockets.legacy.server.WebSocketServerProtocol.process_request`.
40+
41+
Attributes:
42+
status (~http.HTTPStatus): HTTP status code.
43+
headers (Headers): HTTP response headers.
44+
body (bytes): HTTP response body.
45+
"""
46+
47+
def __init__(
48+
self,
49+
status: StatusLike,
50+
headers: datastructures.HeadersLike,
51+
body: bytes = b"",
52+
) -> None:
53+
# If a user passes an int instead of a HTTPStatus, fix it automatically.
54+
self.status = http.HTTPStatus(status)
55+
self.headers = datastructures.Headers(headers)
56+
self.body = body
57+
58+
def __str__(self) -> str:
59+
return (
60+
f"HTTP {self.status:d}, "
61+
f"{len(self.headers)} headers, "
62+
f"{len(self.body)} bytes"
63+
)

src/websockets/legacy/server.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from ..asyncio.compatibility import asyncio_timeout
2525
from ..datastructures import Headers, HeadersLike, MultipleValuesError
2626
from ..exceptions import (
27-
AbortHandshake,
2827
InvalidHandshake,
2928
InvalidHeader,
3029
InvalidOrigin,
@@ -42,7 +41,7 @@
4241
from ..http11 import SERVER
4342
from ..protocol import State
4443
from ..typing import ExtensionHeader, LoggerLike, Origin, StatusLike, Subprotocol
45-
from .exceptions import InvalidMessage
44+
from .exceptions import AbortHandshake, InvalidMessage
4645
from .handshake import build_response, check_request
4746
from .http import read_request
4847
from .protocol import WebSocketCommonProtocol, broadcast

tests/legacy/test_exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ def test_str(self):
1515
InvalidStatusCode(403, Headers()),
1616
"server rejected WebSocket connection: HTTP 403",
1717
),
18+
(
19+
AbortHandshake(200, Headers(), b"OK\n"),
20+
"HTTP 200, 0 headers, 3 bytes",
21+
),
1822
]:
1923
with self.subTest(exception=exception):
2024
self.assertEqual(str(exception), exception_str)

tests/test_exceptions.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,6 @@ def test_str(self):
151151
InvalidParameterValue("a", "|"),
152152
"invalid value for parameter a: |",
153153
),
154-
(
155-
AbortHandshake(200, Headers(), b"OK\n"),
156-
"HTTP 200, 0 headers, 3 bytes",
157-
),
158154
(
159155
RedirectHandshake("wss://example.com"),
160156
"redirect to wss://example.com",

0 commit comments

Comments
 (0)