Skip to content

Commit

Permalink
Remove User-Agent/Server from the Sans-I/O layer.
Browse files Browse the repository at this point in the history
It doesn't make sense to set it at the library level. It should be
set by the embedding program.

Partially reverts 2a07325.
  • Loading branch information
aaugustin committed Oct 15, 2022
1 parent cdd30e4 commit eb86f67
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 77 deletions.
9 changes: 0 additions & 9 deletions src/websockets/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
parse_subprotocol,
parse_upgrade,
)
from .http import USER_AGENT
from .http11 import Request, Response
from .typing import (
ConnectionOption,
Expand Down Expand Up @@ -64,9 +63,6 @@ class ClientConnection(Connection):
logger: logger for this connection;
defaults to ``logging.getLogger("websockets.client")``;
see the :doc:`logging guide <../topics/logging>` for details.
user_agent_header: value of the ``User-Agent`` request header;
defaults to ``"Python/x.y.z websockets/X.Y"``;
:obj:`None` removes the header.
"""

Expand All @@ -76,7 +72,6 @@ def __init__(
origin: Optional[Origin] = None,
extensions: Optional[Sequence[ClientExtensionFactory]] = None,
subprotocols: Optional[Sequence[Subprotocol]] = None,
user_agent_header: Optional[str] = USER_AGENT,
state: State = CONNECTING,
max_size: Optional[int] = 2**20,
logger: Optional[LoggerLike] = None,
Expand All @@ -91,7 +86,6 @@ def __init__(
self.origin = origin
self.available_extensions = extensions
self.available_subprotocols = subprotocols
self.user_agent_header = user_agent_header
self.key = generate_key()

def connect(self) -> Request: # noqa: F811
Expand Down Expand Up @@ -136,9 +130,6 @@ def connect(self) -> Request: # noqa: F811
protocol_header = build_subprotocol(self.available_subprotocols)
headers["Sec-WebSocket-Protocol"] = protocol_header

if self.user_agent_header is not None:
headers["User-Agent"] = self.user_agent_header

return Request(self.wsuri.resource_name, headers)

def process_response(self, response: Response) -> None:
Expand Down
12 changes: 0 additions & 12 deletions src/websockets/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
parse_subprotocol,
parse_upgrade,
)
from .http import USER_AGENT
from .http11 import Request, Response
from .typing import (
ConnectionOption,
Expand Down Expand Up @@ -64,9 +63,6 @@ class ServerConnection(Connection):
logger: logger for this connection;
defaults to ``logging.getLogger("websockets.client")``;
see the :doc:`logging guide <../topics/logging>` for details.
server_header: value of the ``Server`` response header;
defaults to ``"Python/x.y.z websockets/X.Y"``;
:obj:`None` removes the header.
"""

Expand All @@ -75,7 +71,6 @@ def __init__(
origins: Optional[Sequence[Optional[Origin]]] = None,
extensions: Optional[Sequence[ServerExtensionFactory]] = None,
subprotocols: Optional[Sequence[Subprotocol]] = None,
server_header: Optional[str] = USER_AGENT,
state: State = CONNECTING,
max_size: Optional[int] = 2**20,
logger: Optional[LoggerLike] = None,
Expand All @@ -89,7 +84,6 @@ def __init__(
self.origins = origins
self.available_extensions = extensions
self.available_subprotocols = subprotocols
self.server_header = server_header

def accept(self, request: Request) -> Response:
"""
Expand Down Expand Up @@ -175,9 +169,6 @@ def accept(self, request: Request) -> Response:
if protocol_header is not None:
headers["Sec-WebSocket-Protocol"] = protocol_header

if self.server_header is not None:
headers["Server"] = self.server_header

self.logger.info("connection open")
return Response(101, "Switching Protocols", headers)

Expand Down Expand Up @@ -477,9 +468,6 @@ def reject(
("Content-Type", "text/plain; charset=utf-8"),
]
)
if self.server_header is not None:
headers["Server"] = self.server_header

response = Response(status.value, status.phrase, headers, body)
# When reject() is called from accept(), handshake_exc is already set.
# If a user calls reject(), set handshake_exc to guarantee invariant:
Expand Down
25 changes: 0 additions & 25 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from websockets.datastructures import Headers
from websockets.exceptions import InvalidHandshake, InvalidHeader
from websockets.frames import OP_TEXT, Frame
from websockets.http import USER_AGENT
from websockets.http11 import Request, Response
from websockets.uri import parse_uri
from websockets.utils import accept_key
Expand Down Expand Up @@ -38,7 +37,6 @@ def test_send_connect(self):
f"Connection: Upgrade\r\n"
f"Sec-WebSocket-Key: {KEY}\r\n"
f"Sec-WebSocket-Version: 13\r\n"
f"User-Agent: {USER_AGENT}\r\n"
f"\r\n".encode()
],
)
Expand All @@ -58,7 +56,6 @@ def test_connect_request(self):
"Connection": "Upgrade",
"Sec-WebSocket-Key": KEY,
"Sec-WebSocket-Version": "13",
"User-Agent": USER_AGENT,
}
),
)
Expand Down Expand Up @@ -130,7 +127,6 @@ def test_receive_accept(self):
f"Connection: Upgrade\r\n"
f"Sec-WebSocket-Accept: {ACCEPT}\r\n"
f"Date: {DATE}\r\n"
f"Server: {USER_AGENT}\r\n"
f"\r\n"
).encode(),
)
Expand All @@ -148,7 +144,6 @@ def test_receive_reject(self):
(
f"HTTP/1.1 404 Not Found\r\n"
f"Date: {DATE}\r\n"
f"Server: {USER_AGENT}\r\n"
f"Content-Length: 13\r\n"
f"Content-Type: text/plain; charset=utf-8\r\n"
f"Connection: close\r\n"
Expand All @@ -173,7 +168,6 @@ def test_accept_response(self):
f"Connection: Upgrade\r\n"
f"Sec-WebSocket-Accept: {ACCEPT}\r\n"
f"Date: {DATE}\r\n"
f"Server: {USER_AGENT}\r\n"
f"\r\n"
).encode(),
)
Expand All @@ -188,7 +182,6 @@ def test_accept_response(self):
"Connection": "Upgrade",
"Sec-WebSocket-Accept": ACCEPT,
"Date": DATE,
"Server": USER_AGENT,
}
),
)
Expand All @@ -202,7 +195,6 @@ def test_reject_response(self):
(
f"HTTP/1.1 404 Not Found\r\n"
f"Date: {DATE}\r\n"
f"Server: {USER_AGENT}\r\n"
f"Content-Length: 13\r\n"
f"Content-Type: text/plain; charset=utf-8\r\n"
f"Connection: close\r\n"
Expand All @@ -218,7 +210,6 @@ def test_reject_response(self):
Headers(
{
"Date": DATE,
"Server": USER_AGENT,
"Content-Length": "13",
"Content-Type": "text/plain; charset=utf-8",
"Connection": "close",
Expand Down Expand Up @@ -574,22 +565,6 @@ def test_unsupported_subprotocol(self):
raise client.handshake_exc
self.assertEqual(str(raised.exception), "unsupported subprotocol: otherchat")

def test_no_user_agent_header(self):
client = ClientConnection(
parse_uri("wss://example.com/"),
user_agent_header=None,
)
request = client.connect()
self.assertNotIn("User-Agent", request.headers)

def test_custom_user_agent_header(self):
client = ClientConnection(
parse_uri("wss://example.com/"),
user_agent_header="websockets",
)
request = client.connect()
self.assertEqual(request.headers["User-Agent"], "websockets")


class MiscTests(unittest.TestCase):
def test_bypass_handshake(self):
Expand Down
31 changes: 0 additions & 31 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from websockets.datastructures import Headers
from websockets.exceptions import InvalidHeader, InvalidOrigin, InvalidUpgrade
from websockets.frames import OP_TEXT, Frame
from websockets.http import USER_AGENT
from websockets.http11 import Request, Response
from websockets.server import *

Expand All @@ -32,7 +31,6 @@ def test_receive_connect(self):
f"Connection: Upgrade\r\n"
f"Sec-WebSocket-Key: {KEY}\r\n"
f"Sec-WebSocket-Version: 13\r\n"
f"User-Agent: {USER_AGENT}\r\n"
f"\r\n"
).encode(),
)
Expand All @@ -51,7 +49,6 @@ def test_connect_request(self):
f"Connection: Upgrade\r\n"
f"Sec-WebSocket-Key: {KEY}\r\n"
f"Sec-WebSocket-Version: 13\r\n"
f"User-Agent: {USER_AGENT}\r\n"
f"\r\n"
).encode(),
)
Expand All @@ -66,7 +63,6 @@ def test_connect_request(self):
"Connection": "Upgrade",
"Sec-WebSocket-Key": KEY,
"Sec-WebSocket-Version": "13",
"User-Agent": USER_AGENT,
}
),
)
Expand All @@ -83,7 +79,6 @@ def make_request(self):
"Connection": "Upgrade",
"Sec-WebSocket-Key": KEY,
"Sec-WebSocket-Version": "13",
"User-Agent": USER_AGENT,
}
),
)
Expand All @@ -102,7 +97,6 @@ def test_send_accept(self):
f"Upgrade: websocket\r\n"
f"Connection: Upgrade\r\n"
f"Sec-WebSocket-Accept: {ACCEPT}\r\n"
f"Server: {USER_AGENT}\r\n"
f"\r\n".encode()
],
)
Expand All @@ -123,7 +117,6 @@ def test_send_reject(self):
f"Connection: close\r\n"
f"Content-Length: 13\r\n"
f"Content-Type: text/plain; charset=utf-8\r\n"
f"Server: {USER_AGENT}\r\n"
f"\r\n"
f"Sorry folks.\n".encode(),
b"",
Expand All @@ -147,7 +140,6 @@ def test_accept_response(self):
"Upgrade": "websocket",
"Connection": "Upgrade",
"Sec-WebSocket-Accept": ACCEPT,
"Server": USER_AGENT,
}
),
)
Expand All @@ -168,7 +160,6 @@ def test_reject_response(self):
"Connection": "close",
"Content-Length": "13",
"Content-Type": "text/plain; charset=utf-8",
"Server": USER_AGENT,
}
),
)
Expand Down Expand Up @@ -608,28 +599,6 @@ def test_unsupported_subprotocol(self):
self.assertNotIn("Sec-WebSocket-Protocol", response.headers)
self.assertIsNone(server.subprotocol)

def test_no_server_header(self):
server = ServerConnection(server_header=None)
request = self.make_request()
response = server.accept(request)
self.assertNotIn("Server", response.headers)

def test_custom_server_header(self):
server = ServerConnection(server_header="websockets")
request = self.make_request()
response = server.accept(request)
self.assertEqual(response.headers["Server"], "websockets")

def test_reject_response_no_server_header(self):
server = ServerConnection(server_header=None)
response = server.reject(http.HTTPStatus.OK, "Hello world!\n")
self.assertNotIn("Server", response.headers)

def test_reject_response_custom_server_header(self):
server = ServerConnection(server_header="websockets")
response = server.reject(http.HTTPStatus.OK, "Hello world!\n")
self.assertEqual(response.headers["Server"], "websockets")


class MiscTests(unittest.TestCase):
def test_bypass_handshake(self):
Expand Down

0 comments on commit eb86f67

Please sign in to comment.