a
    xd                     @   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   M/var/www/html/Ranjet/env/lib/python3.9/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r6td|jf ||j|_|j	d|_
t||_|jr| rt|j}|jdvrt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   s0  | j d}|r.|ddd  tkr.| jS | j t }|s(|pJd}|dd}t|dkr|d 	 
dr|d ddd 	 }nd	}t| jtrz| j|}W nD ty   z| jd	}W n  ty   | j|d
}Y n0 Y n0 n| j}ztt|}W n ttfy&   Y n0 |S d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replaceN)r!   r(   splitlowerr   r'   r   rsplitlenstrip
startswith
isinstancer*   bytesdecodeUnicodeErrorr   r   r
   )r-   r   Z	yadis_locencodingcontentr   r   r   r)   h   s8    r)   N)__all__ior   Zopenidr   Zopenid.yadis.constantsr   r   r	   Zopenid.yadis.parsehtmlr
   r   r   r   objectr   r   r)   r   r   r   r   <module>   s   	&,