def _is_same_origin(websocket_origin, host_scheme, host_port, host_policy):
    """
    Internal helper. Returns True if the provided websocket_origin
    triple should be considered valid given the provided policy and
    expected host_port.

    Currently, the policy is just the list of allowedOriginsPatterns
    from the WebSocketProtocol instance. Schemes and ports are matched
    first, and only if there is not a mismatch do we compare each
    allowed-origin pattern against the host.
    """
    if websocket_origin == 'null':
        return False
    if not isinstance(websocket_origin, tuple) or not len(websocket_origin
        ) == 3:
        raise ValueError("'websocket_origin' must be a 3-tuple")
    origin_scheme, origin_host, origin_port = websocket_origin
    template = '{scheme}://{host}:{port}'
    origin_header = template.format(scheme=origin_scheme, host=origin_host,
        port=origin_port)
    for origin_pattern in host_policy:
        if origin_pattern.match(origin_header):
            return True
    return False
