o
    ˷e                     @   sx   g d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
mZ G dd deZG dd	 d	eZd
d Zdd ZdS ))discoverDiscoveryResultDiscoveryFailure    )StringIO)fetchers)YADIS_HEADER_NAMEYADIS_CONTENT_TYPEYADIS_ACCEPT_HEADER)MetaNotFoundfindHTMLMetac                   @   s   e Zd ZdZdZdd ZdS )r   zBRaised when a YADIS protocol error occurs in the discovery processNc                 C   s   t | | || _d S )N)	Exception__init__http_response)selfmessager    r   L/var/www/ideatree/venv/lib/python3.10/site-packages/openid/yadis/discover.pyr      s   
zDiscoveryFailure.__init__)__name__
__module____qualname____doc__identity_urlr   r   r   r   r   r      s    r   c                   @   s<   e Zd ZdZdZdZdZdZdZdd Z	dd Z
dd ZdS )	r   z:Contains the result of performing Yadis discovery on a URINc                 C   s
   || _ dS )zgInitialize the state of the object

        sets all attributes to None except the request_uri
        N)request_uri)r   r   r   r   r   r   )   s   
zDiscoveryResult.__init__c                 C   s   | j du rdS | j| j kS )z*Was the Yadis protocol's indirection used?NF)xrds_urinormalized_urir   r   r   r   usedYadisLocation0   s   
z!DiscoveryResult.usedYadisLocationc                 C   s   |   p| jtkS )z5Is the response text supposed to be an XRDS document?)r   content_typer   r   r   r   r   isXRDS6   s   zDiscoveryResult.isXRDS)r   r   r   r   r   r   r   r   response_textr   r   r   r   r   r   r   r      s    r   c                 C   s   t | }tj| dtid}|jdvrtd|jf ||j|_|j	d|_
t||_|jrS| rSt|j}|jdvrLtd|jf |}|j|_||j	d|_
|j|_|S )a  Discover services for a given URI.

    @param uri: The identity URI as a well-formed http or https
        URI. The well-formedness and the protocol are not checked, but
        the results of this function are undefined if those properties
        do not hold.

    @return: DiscoveryResult object

    @raises Exception: Any exception that can be raised by fetching a URL with
        the given fetcher.
    @raises DiscoveryFailure: When the HTTP response does not have a 200 code.
    Accept)headers)      zEHTTP Response status from identity URL host is not 200. Got status %rcontent-typez>HTTP Response status from Yadis host is not 200. Got status %r)r   r   fetchr	   statusr   	final_urlr   r!   getr   whereIsYadisr   r   r   bodyr   )uriresultrespexcr   r   r   r   <   s4   


r   c                 C   s*  | j d}|r|ddd  tkr| jS | j t }|s|p$d}|dd}t|dkrG|d 	 
drG|d ddd 	 }nd	}t| jtrzz| j|}W n% tyy   z| jd	}W n tyv   | j|d
}Y nw Y nw | j}z	tt|}W |S  ttfy   Y |S w |S )zGiven a HTTPResponse, return the location of the Yadis document.

    May be the URL just retrieved, another URL, or None if no suitable URL can
    be found.

    [non-blocking]

    @returns: str or None
    r$   ;   r       zcharset==zutf-8replace)r!   r(   splitlowerr   r'   r   rsplitlenstrip
startswith
isinstancer*   bytesdecodeUnicodeErrorr   r   r
   )r-   r   	yadis_locencodingcontentr   r   r   r)   h   sB   r)   N)__all__ior   openidr   openid.yadis.constantsr   r   r	   openid.yadis.parsehtmlr
   r   r   r   objectr   r   r)   r   r   r   r   <module>   s   	&,