MCPcopy Index your code
hub / github.com/python-websockets/websockets / process_request

Function process_request

src/websockets/asyncio/server.py:951–995  ·  view source on GitHub ↗

Perform HTTP Basic Authentication. If it succeeds, set the connection's ``username`` attribute and return :obj:`None`. If it fails, return an HTTP 401 Unauthorized responss.

(
        connection: ServerConnection,
        request: Request,
    )

Source from the content-addressed store, hash-verified

949 assert check_credentials is not None # help mypy
950
951 async def process_request(
952 connection: ServerConnection,
953 request: Request,
954 ) -> Response | None:
955 """
956 Perform HTTP Basic Authentication.
957
958 If it succeeds, set the connection's ``username`` attribute and return
959 :obj:`None`. If it fails, return an HTTP 401 Unauthorized responss.
960
961 """
962 try:
963 authorization = request.headers["Authorization"]
964 except KeyError:
965 response = connection.respond(
966 http.HTTPStatus.UNAUTHORIZED,
967 "Missing credentials\n",
968 )
969 response.headers["WWW-Authenticate"] = build_www_authenticate_basic(realm)
970 return response
971
972 try:
973 username, password = parse_authorization_basic(authorization)
974 except InvalidHeader:
975 response = connection.respond(
976 http.HTTPStatus.UNAUTHORIZED,
977 "Unsupported credentials\n",
978 )
979 response.headers["WWW-Authenticate"] = build_www_authenticate_basic(realm)
980 return response
981
982 valid_credentials = check_credentials(username, password)
983 if isinstance(valid_credentials, Awaitable):
984 valid_credentials = await valid_credentials
985
986 if not valid_credentials:
987 response = connection.respond(
988 http.HTTPStatus.UNAUTHORIZED,
989 "Invalid credentials\n",
990 )
991 response.headers["WWW-Authenticate"] = build_www_authenticate_basic(realm)
992 return response
993
994 connection.username = username
995 return None
996
997 return process_request

Callers 1

handshakeMethod · 0.70

Calls 4

check_credentialsFunction · 0.70
respondMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…