async def _browser_websocket_url(address: str, port: int, netloc: str) -> str | None:
    async with asyncio.timeout(PROBE_STEP_TIMEOUT_S):
        stream, writer = await asyncio.open_connection(address, port, limit=IDENTITY_READ_MAX_BYTES)
        try:
            writer.write(VERSION_REQUEST.format(netloc=netloc).encode('ascii'))
            head = (await stream.readuntil(b'\r\n\r\n')).decode('latin-1')
            length = _ok_content_length(head)
            if length is None:
                return None
            body = json.loads(await stream.readexactly(length))
        finally:
            writer.close()
    found = body.get('webSocketDebuggerUrl') if isinstance(body, dict) else None
    return found if isinstance(found, str) else None

def _ok_content_length(head: str) -> int | None:
    status, _, rest = head.partition('\r\n')
    if re.fullmatch('HTTP/1\\.[01] 200(?: [^\\r\\n]*)?', status) is None:
        return None
    lengths: list[str] = []
    for line in rest.split('\r\n'):
        name, _, value = line.partition(':')
        if name.strip().lower() == 'transfer-encoding':
            return None
        if name.strip().lower() == 'content-length':
            lengths.append(value.strip())
    if len(lengths) != 1 or not lengths[0].isdigit():
        return None
    length = int(lengths[0])
    return length if length <= IDENTITY_READ_MAX_BYTES else None

def _is_browser_websocket(ws_url: str, netloc: str) -> bool:
    parts = urlsplit(ws_url)
    return parts.scheme == 'ws' and parts.netloc == netloc and (re.fullmatch('/devtools/browser/[A-Za-z0-9-]+', parts.path) is not None) and (not parts.query) and (not parts.fragment)

def ui_window_target(answer: object, ui_port: int) -> str | None:
    infos = answer.get('targetInfos') if isinstance(answer, Mapping) else None
    if not isinstance(infos, list) or not 0 < ui_port < 65536:
        return None
    origins = {f'127.0.0.1:{ui_port}', f'localhost:{ui_port}'}
    for info in infos:
        if not isinstance(info, Mapping) or info.get('type') != 'page':
            continue
        url, target_id = (info.get('url'), info.get('targetId'))
        if not isinstance(url, str) or not isinstance(target_id, str):
            continue
        parts = urlsplit(url)
        if parts.scheme == 'http' and parts.netloc in origins and (re.fullmatch('[A-Za-z0-9-]{1,64}', target_id) is not None):
            return target_id
    return None

async def _read_process_info(ws_url: str, ui_port: int | None=None, dedicated: Callable[[int], bool] | None=None) -> tuple[int | None, bool]:
    from websockets.asyncio.client import connect

    class NoRedirect(connect):

        def process_redirect(self, exc: Exception) -> Exception:
            return exc

    async def answer(ws: Any, number: int) -> Mapping[str, Any]:
        while True:
            message = json.loads(await ws.recv())
            if isinstance(message, dict) and type(message.get('id')) is int and (message['id'] == number):
                return message

    async def talk(ws: Any, deadline: asyncio.Timeout) -> tuple[int | None, bool]:
        await ws.send(json.dumps({'id': 1, 'method': 'SystemInfo.getProcessInfo'}))
        info = (await answer(ws, 1)).get('result')
        pid = browser_pid_from_process_info(info) if isinstance(info, Mapping) else None
        if pid is None or ui_port is None or dedicated is None:
            return (pid, False)
        deadline.reschedule(None)
        try:
            if not dedicated(pid):
                return (pid, False)
            async with asyncio.timeout(FRONT_STEP_TIMEOUT_S):
                await ws.send(json.dumps({'id': 2, 'method': 'Target.getTargets'}))
                target_id = ui_window_target((await answer(ws, 2)).get('result'), ui_port)
                if target_id is None:
                    return (pid, False)
                await ws.send(json.dumps({'id': 3, 'method': 'Target.activateTarget', 'params': {'targetId': target_id}}))
                done = await answer(ws, 3)
        except Exception as exc:
            log.info("could not bring netkeep's window to the front: %s", personal_exc(exc))
            return (pid, False)
        finally:
            deadline.reschedule(asyncio.get_running_loop().time() + FRONT_STEP_TIMEOUT_S)
        return (pid, 'error' not in done and isinstance(done.get('result'), Mapping))
    found: tuple[int | None, bool] = (None, False)
    answered = False
    try:
        async with asyncio.timeout(PROBE_STEP_TIMEOUT_S) as deadline:
            async with NoRedirect(ws_url, proxy=None, compression=None, max_size=IDENTITY_READ_MAX_BYTES) as ws:
                found = await talk(ws, deadline)
                answered = True
    except TimeoutError:
        if not answered:
            raise
        log.info("the identity read's websocket was slow to close")
    return found
