
    )Jf	>                        d Z g dZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZ	 ddlZn# e$ r dZY nw xY w	 ddlZn# e$ r dZY nw xY wdej        de
j        dZdZddZd	 Zdad
 Zd dZd Z G d de          Z G d de          Zd Z G d de          Z  G d de          Z! G d de          Z" G d de           Z# G d de          Z$ G d de          Z%dS )!zN
This module contains the HTTP fetcher interface and several implementations.
)fetchgetDefaultFetchersetDefaultFetcherHTTPResponseHTTPFetchercreateHTTPFetcherHTTPFetchingError	HTTPError    Nzpython-openid/z ()   c                 L    t                      }|                    | ||          S )zInvoke the fetch method on the default fetcher. Most users
    should need only this method.

    @raises Exception: any exceptions that may be raised by the default fetcher
    )r   r   )urlbodyheadersfetchers       R/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/openid/fetchers.pyr   r   *   s%      !!G==dG,,,    c                  N    t           t                      } nt                      } | S )zDCreate a default HTTP fetcher instance

    prefers Curl to urllib2.)pycurlUrllib2FetcherCurlHTTPFetcherr   s    r   r   r   4   s&     ~ ""!##Nr   c                  T    t           t          t                                 t           S )zReturn the default fetcher instance
    if no fetcher has been set, it will create a default fetcher.

    @return: the default fetcher
    @rtype: HTTPFetcher
    )_default_fetcherr   r    r   r   r   r   F   s%     +--...r   Tc                 4    | |s| a dS t          |           a dS )a  Set the default fetcher

    @param fetcher: The fetcher to use as the default HTTP fetcher
    @type fetcher: HTTPFetcher

    @param wrap_exceptions: Whether to wrap exceptions thrown by the
        fetcher wil HTTPFetchingError so that they may be caught
        easier. By default, exceptions will be wrapped. In general,
        unwrapped fetchers are useful for debugging of fetching errors
        or if your fetcher raises well-known exceptions that you would
        like to catch.
    @type wrap_exceptions: bool
    N)r   ExceptionWrappingFetcher)r   wrap_exceptionss     r   r   r   U   s-     o"3G<<r   c                      t                      } t          | t                    r| j        } t          | t                    S )z>Whether the currently set HTTP fetcher is a Curl HTTP fetcher.)r   
isinstancer   r   r   r   s    r   	usingCurlr!   j   s7    !!G'344 "/g///r   c                   0    e Zd ZdZdZdZdZdZddZd Z	dS )r   zXXX document attributesNc                 >    || _         || _        || _        || _        d S N)	final_urlstatusr   r   )selfr%   r&   r   r   s        r   __init__zHTTPResponse.__init__y   s"    "			r   c                 B    d| j         j        d| j        d| j        dS )N<z status z for >)	__class____name__r&   r%   r'   s    r   __repr__zHTTPResponse.__repr__   s-     *..*A*A*A4;;;*....: 	:r   )NNNN)
r-   
__module____qualname____doc__r   r&   r   r%   r(   r/   r   r   r   r   r   r   sQ        !!GFDI   : : : : :r   r   c                       e Zd ZdZddZdS )r   z
    This class is the interface for openid HTTP fetchers.  This
    interface is only important if you need to write a new fetcher for
    some reason.
    Nc                     t           )a  
        This performs an HTTP POST or GET, following redirects along
        the way. If a body is specified, then the request will be a
        POST. Otherwise, it will be a GET.


        @param headers: HTTP headers to include with the request
        @type headers: {str:str}

        @return: An object representing the server's HTTP response. If
            there are network or protocol errors, an exception will be
            raised. HTTP error responses, like 404 or 500, do not
            cause exceptions.

        @rtype: L{HTTPResponse}

        @raise Exception: Different implementations will raise
            different errors based on the underlying HTTP library.
        )NotImplementedError)r'   r   r   r   s       r   r   zHTTPFetcher.fetch   s
    ( "!r   NN)r-   r0   r1   r2   r   r   r   r   r   r      s2         " " " " " "r   r   c                 T    t           j                            |           }|d         dv S )Nr
   )httphttps)urllibparseurlparse)r   parseds     r   _allowedURLr>      s'    \""3''F!9)))r   c                       e Zd ZdZddZdS )r   zException that is wrapped around all exceptions that are raised
    by the underlying fetcher when using the ExceptionWrappingFetcher

    @ivar why: The exception that caused this exception
    Nc                 J    t                               | |           || _        d S r$   )	Exceptionr(   why)r'   rB   s     r   r(   zHTTPFetchingError.__init__   s#    4%%%r   r$   )r-   r0   r1   r2   r(   r   r   r   r   r      s2              r   r   c                   (    e Zd ZdZeeefZd Zd Z	dS )r   zFetcher that wraps another fetcher, causing all exceptions

    @cvar uncaught_exceptions: Exceptions that should be exposed to the
        user if they are raised by the fetch call
    c                     || _         d S r$   r   )r'   r   s     r   r(   z!ExceptionWrappingFetcher.__init__   s    r   c                     	  | j         j        |i |S # | j        $ r   t          j                    d d         \  }}||}t          |          xY w)N   )rB   )r   r   uncaught_exceptionssysexc_infor   )r'   argskwargsexc_clsexc_insts        r   r   zExceptionWrappingFetcher.fetch   sw    
	2%4<%t6v666' 	 	 		2 #rr 2GX"#1111s	    >AN)
r-   r0   r1   r2   
SystemExitKeyboardInterruptMemoryErrorrG   r(   r   r   r   r   r   r      sL          &'8+F  2 2 2 2 2r   r   c                   V    e Zd ZdZ eej        j                  ZddZd Z	d Z
d ZdS )r   z,An C{L{HTTPFetcher}} that uses urllib2.
    Nc                 N   t          |          st          d|          |i }|                    dt          dt          j        j                   t          |t                    rt          |d          }t          j        
                    |||          }d }	 |                     |          }t          j        |          5  |                     |          cd d d            S # 1 swxY w Y   d S # t          j        j        $ rS}t          j        |          5  |                     |          }|cd d d            cY d }~S # 1 swxY w Y   Y d }~d S d }~wt          j        j        t$          j        j        f$ r} d }~wt*          $ r}t-          |          d }~ww xY w)NzBad URL scheme: 
User-Agentz Python-urllib/zutf-8)encoding)datar   )r>   
ValueError
setdefault
USER_AGENTr:   request__version__r    strbytesRequesturlopen
contextlibclosing_makeResponseerrorr	   URLErrorr8   clientBadStatusLinerA   AssertionError)r'   r   r   r   requrl_resourcerB   resps           r   r   zUrllib2Fetcher.fetch   sr   3 	=*SS;<<<?G<&JJ(B(B*D 	E 	E 	E dC   	1000Dn$$StW$EE	&<<,,L#L11 8 8)),778 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8|% 	 	 	#C((  ))#..                           %t{'@A 	 	 	 	& 	& 	& %%%	&s~    )C8 	C+C8 +C//C8 2C/3C8 8F$E E	7EF$	E	EE	E(F$FF$FF$c                    t                      }|                    t          dz            |_        |                                |_        |                     t          t          |	                                
                                                              |_        t          |d          r|j        |_        nd|_        |                     |j                            dd                    \  }}|                    dd          }	 |j                            |          |_        n# t$          $ r Y nw xY w|S )z
        Construct an HTTPResponse from the the urllib response. Attempt to
        decode the response body from bytes to str if the necessary information
        is available.
        r   code   zcontent-type charsetlatin1)r   readMAX_RESPONSE_KBr   geturlr%   _lowerCaseKeysdictlistinfoitemsr   hasattrrk   r&   _parseHeaderValuegetdecoderA   )r'   urllib2_responseri   _
extra_dictrn   s         r   ra   zUrllib2Fetcher._makeResponse   s-    ~~$))/D*@AA	)0022**&++--335566779 9 #V,, 	*/DKKDK..L^R002 2:
 ..H55		((11DII 	 	 	D	 s   D. .
D;:D;c                 l    i }|                                 D ]\  }}|||                                <   |S r$   )rw   lower)r'   headers_dictnew_dictkvs        r   rs   zUrllib2Fetcher._lowerCaseKeys  s@     &&(( 	$ 	$DAq"#HQWWYYr   c                 j   |                     dd          }t          |          dk    r
|d         i fS |d         |d                              d          }}i }|D ]U}	 |                     dd          \  }}|                                ||                                <   F# t          $ r Y Rw xY w||fS )z
        Parse out a complex header value (such as Content-Type, with a value
        like "text/html; charset=utf-8") into a main value and a dictionary of
        extra information (in this case, 'text/html' and {'charset': 'utf8'}).
        ;   r
   =)splitlenstriprV   )	r'   header_valuevalues
main_valueextra_valuesr~   value_stringkeyvalues	            r   ry   z Urllib2Fetcher._parseHeaderValue  s     ##C++v;;!!9b= #)!9fQiooc.B.BL

( 	 	L)//Q77
U*/++--
399;;''    :%%s   AB!!
B.-B.r6   )r-   r0   r1   r2   staticmethodr:   rY   r^   r   ra   rs   ry   r   r   r   r   r      su         
 l6>122G& & & &:  <  & & & & &r   r   c                       e Zd ZdZdS )r	   z
    This exception is raised by the C{L{CurlHTTPFetcher}} when it
    encounters an exceptional situation fetching a URL.
    N)r-   r0   r1   r2   r   r   r   r	   r	   -  s          	Dr   r	   c                   0    e Zd ZdZdZd Zd Zd ZddZdS )	r   zh
    An C{L{HTTPFetcher}} that uses pycurl for fetching.
    See U{http://pycurl.sourceforge.net/}.
       c                 f    t                               |            t          t          d          d S )NzCannot find pycurl library)r   r(   r   RuntimeErrorr.   s    r   r(   zCurlHTTPFetcher.__init__=  s1    T""">;<<< >r   c                 &   |                     d           d |D             }i }|D ]j}	 |                    dd          \  }}n # t          $ r t          d|          w xY w|                                }|                                }|||<   k|S )Nr
   c                 b    g | ],}d |v |                                                                 -S )   :)r{   r   ).0lines     r   
<listcomp>z1CurlHTTPFetcher._parseHeaders.<locals>.<listcomp>F  s1    OOO4$$,,$$&&,,,r   :r   z(Malformed HTTP header line in response: )seekr   rV   r	   r   r   )r'   header_filelinesr   r   namer   s          r   _parseHeaderszCurlHTTPFetcher._parseHeadersB  s     PO;OOO 	" 	"D*"jja00ee * * *i!%!) * * ** KKMME ::<<D!GDMMs   AA c                      t          |          S r$   )r>   )r'   r   s     r   	_checkURLzCurlHTTPFetcher._checkURLX  s     3r   Nc                    t          t          j                              | j        z   }| j        }|i }|                    dt          dt
          j                   g }|H|                                D ]3\  }}|d|}	|                    |		                                           4t          j
                    }
	 |
                    t
          j        d           |r |
                    t
          j        |           |@|
                    t
          j        d           |
                    t
          j        |           |dk    r|                     |          st#          d|          t%          j                    fd}t%          j                    }|
                    t
          j        |           |
                    t
          j        |j                   |
                    t
          j        |           |
                    t
          j        t2          j                            |                     |
                                 |                     |          }|
                    t
          j                  }|dv rJ|                    d	          }|t#          d
|z            |
                    t
          j        d           ndtA                      }||_!        ||_"        ||_#        $                                %                                |_&        ||
'                                 S |t          t          j                              z
  }|dk    t#          d|          # |
'                                 w xY w)NrS    z: r   r
   zFetching URL not allowed: c                 r                                     dt          z  k    rdS                     |           S )Nr   r
   )tellrq   write)chunkrU   s    r   
write_dataz)CurlHTTPFetcher.fetch.<locals>.write_data  s2    yy{{d_&<== q#zz%000r   )i-  i.  i/  i3  locationz)Redirect (%s) returned without a locationzTimed out fetching: )(inttimeALLOWED_TIMErW   rX   r   versionrw   appendencodeCurlsetoptNOSIGNAL
HTTPHEADERPOST
POSTFIELDSr   r	   ioBytesIOWRITEFUNCTIONHEADERFUNCTIONr   TIMEOUTURLopenidurinormperformr   getinfoRESPONSE_CODErz   r   r   r&   r%   getvaluer{   r   close)r'   r   r   r   stopoffheader_listheader_namer   headercr   response_header_dataresponse_headersrk   ri   rU   s                   @r   r   zCurlHTTPFetcher.fetch]  s4   49;;$"33?G<&0jj&..C	E 	E 	E -4]]__ 4 4)\%0[[,,?""6==??3333KMM8	HHV_a((( 9*K888 a(((*D111''~~c** N#)cc$LMMMz||1 1 1 1 1 (*z||$-z:::.0D0JKKK---V^%;%;C%@%@AAA		#'#5#56J#K#K yy!566///*..z::C{'G$NP P P HHV[!,,,,
 (>>D#3DL"&DK%(DN $ 6 6 8 8DI GGIIII	 S---S ''V )>???GGIIIIs   ?I$M4 8<M4 4N
r6   )	r-   r0   r1   r2   r   r(   r   r   r   r   r   r   r   r   6  sl          L= = =
  ,     
I I I I I Ir   r   c                   ,     e Zd ZdZd fd	ZddZ xZS )HTTPLib2FetcherzA fetcher that uses C{httplib2} for performing HTTP
    requests. This implementation supports HTTP caching.

    @see: http://bitworking.org/projects/httplib2/
    Nc                     t           t          d          t          t          |                                            t          j        |          | _         d| j         _        dS )z@param cache: An object suitable for use as an C{httplib2}
            cache. If a string is passed, it is assumed to be a
            directory name.
        NzJCannot find httplib2 library. See http://bitworking.org/projects/httplib2/F)httplib2r   superr   r(   Httpforce_exception_to_status_code)r'   cacher,   s     r   r(   zHTTPLib2Fetcher.__init__  si    
   N O O O 	ot$$--/// !e,, 8=444r   c           
         |rd}nd}|i }|                     d          s'|                     d          st          d|          | j                            ||||          \  }}	 |d         }n(# t          $ r |j        rJ |j        d	k    sJ |}Y nw xY wt          |                                |t          t          |                                                    |j        
          S )zPerform an HTTP request

        @raises Exception: Any exception that can be raised by httplib2

        @see: C{L{HTTPFetcher.fetch}}
        r   GETNzhttp://zhttps://zURL is not a HTTP URL: )r   r   zcontent-locationrl   )r   r%   r   r&   )
startswithrV   r   rY   KeyErrorpreviousr&   r   r{   rt   ru   rw   )r'   r   r   r   methodhttplib2_responsecontentr%   s           r   r   zHTTPLib2Fetcher.fetch  s7     	FFF?G y)) 	DS^^J-G-G 	D*CCBCCC%)]%:%:dG &; &5 &5"7	)*<=II 	 	 	(1111 %+s2222III	 !!/55778899$+	/ / / 	/s   *A3 3"BBr$   r6   )r-   r0   r1   r2   r(   r   __classcell__)r,   s   @r   r   r     s[         = = = = = =$+/ +/ +/ +/ +/ +/ +/ +/r   r   r6   )T)&r2   __all__urllib.requestr:   urllib.errorurllib.parsehttp.clientr8   r   r   rH   r_   r   openid.urinormr   ImportErrorr   rZ   platformrX   rq   r   r   r   r   r   r!   objectr   r   r>   rA   r   r   r   r	   r   r   r   r   r   <module>r      s     
                  				 



         OOOO   HHH
MMMM   FFF )/(:(:(:CLLLI
- - - -	 	 	    = = = =*0 0 0: : : : :6 : : :$" " " " "& " " "<* * *	 	 	 	 		 	 	 	2 2 2 2 2{ 2 2 24\& \& \& \& \&[ \& \& \&~	 	 	 	 	! 	 	 	p p p p pk p p pfD/ D/ D/ D/ D/k D/ D/ D/ D/ D/s   5 ??A AA