
    6hM8                     t   d dl Z d dlZd dlmZ d dlmZ d dlmZmZ d dl	m
Z
mZ  e j                  e      ZdZdZdZd	Zd
ZdZdZdZdZdZg dZg dZdZ e ej:                  d            Z ededdddddddddd      Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd  Z(d*d!Z)d*d"Z*d# Z+d+d$Z,d% Z-d,d&Z.d' Z/d( Z0d) Z1y)-    N)Iterable)	timedelta)current_apprequest)Headers	MultiDictzAccess-Control-Allow-OriginzAccess-Control-Allow-MethodszAccess-Control-Allow-HeaderszAccess-Control-Expose-Headersz Access-Control-Allow-CredentialszAccess-Control-Max-Agez$Access-Control-Allow-Private-NetworkzAccess-Control-Request-MethodzAccess-Control-Request-Headersz&Access-Control-Request-Private-Network)GETHEADPOSTOPTIONSPUTPATCHDELETE)CORS_ORIGINSCORS_METHODSCORS_ALLOW_HEADERSCORS_EXPOSE_HEADERSCORS_SUPPORTS_CREDENTIALSCORS_MAX_AGECORS_SEND_WILDCARDCORS_AUTOMATIC_OPTIONSCORS_VARY_HEADERCORS_RESOURCESCORS_INTERCEPT_EXCEPTIONSCORS_ALWAYS_SENDCORS_ALLOW_PRIVATE_NETWORK_FLASK_CORS_EVALUATED *FTz/*)originsmethodsallow_headersexpose_headerssupports_credentialsmax_agesend_wildcardautomatic_optionsvary_header	resourcesintercept_exceptionsalways_sendallow_private_networkc                    t        | t              r=| j                         D cg c]  \  }}t        |      |f } }}d }t	        | |      S t        | t
              rt        |       i fgS t        | t              r| D cg c]  }t        |      i f c}S t        | t              rt        |       i fgS t        d      c c}}w c c}w )Nc                 $   | \  }}t        |t              r3dd|j                  j                  d      t	        |j                         fS t        |      rdd|j                  d      t	        |       fS dd|j                  d      t	        |       fS )N   r   /)
isinstanceRegexObjectpatterncountlenprobably_regex)pairr3   _s      8D:\EasyAligner\venv\Lib\site-packages\flask_cors/core.pysort_keyz!parse_resources.<locals>.sort_keyI   s    JGQ';/1goo33C83w;O:OPP(1gmmC03w<-@@1gmmC03w<-@@    )keyz(Unexpected value for resources argument.)	r1   dictitemsre_fixsortedstrr   r2   
ValueError)r)   kvr:   rs        r9   parse_resourcesrF   B   s    )T" 1:0AB0A1fQi^0A	B	A iX..	Is	#	"B'((	Ix	()23AB33 
I{	+	"B'(( CDD5 C$ 4s   C  Cc                 P    	 | j                   S # t        $ r t        |       cY S w xY w)z
    Helper that returns regexp pattern from given value.

    :param regexp: regular expression to stringify
    :type regexp: _sre.SRE_Pattern or str
    :returns: string representation of given regexp pattern
    :rtype: str
    )r3   AttributeErrorrA   )regexps    r9   get_regexp_patternrJ   c   s*    ~~ 6{s    %%c                    | j                  d      }d|v }|r}t        j                  d|       |r)| j                  d      rt        j                  d       dgS t        ||d      rt        j                  d	       |gS t        j                  d
       y | j                  d      r<|r| j                  d      ry dgS t	        |D cg c]  }t        |      r| c}      S t        j                  d       y c c}w )Nr    .*z&CORS request received with 'Origin' %sr&   z=Allowed origins are set to '*'. Sending wildcard CORS header.r   FcaseSensitivez:The request's Origin header matches. Sending CORS headers.zBThe request's Origin header does not match any of allowed origins.r+   r$   zThe request did not contain an 'Origin' header. This means the browser or client did not request CORS, ensure the Origin Header is set.)getLOGdebugtry_match_any_patternr@   r6   )optionsrequest_originr    wildcardos        r9   get_cors_originsrW   r   s    kk)$GH 		:NK O4IIUV5L #>7%PIIL ###IIZ[	]	# {{12u gGg^A5F1gGHH 			 V	
  Hs   C8C8c                      |rU|j                  d      D cg c]  }|j                          }}t         fd|      }dj                  t	        |            S y c c}w )N,c                 >    t        | j                  d      d      S )Nr"   FrM   )rR   rO   )hrS   s    r9   <lambda>z#get_allow_headers.<locals>.<lambda>   s    ,A!W[[Q`Eaqv,wr;   , )splitstripfilterjoinr@   )rS   acl_request_headersr[   request_headersmatching_headerss   `    r9   get_allow_headersre      sc    .A.G.G.LM.L1779.LM ""w  zI  Jyy 0122 Ns   Ac                    t        | |j                  d            }t               }|s|S |D ]  }|j                  t        |        | j                  d      |t
        <   | j                  d      r	d|t        <   t        |v r6|j                  t              dk(  r| j                  d      rdnd}||t        <   |dk(  r|j                  t        d      j                         }|rj|| j                  d	      v rWt        | |j                  t                    |t        <   | j                  d
      |t        <   | j                  d	      |t        <   nt         j#                  d       | j                  d      rr|t           dk(  rnet%        | j                  d            dkD  s6t%        |      dkD  s(t'        t)        t*        | j                  d                  r|j                  dd       t        d |j-                         D              S )NOriginr#   r$   truer,   falser   r   r!   r%   ztThe request's Access-Control-Request-Method header does not match allowed methods. CORS headers will not be applied.r(   r   r    r/   Varyc              3   0   K   | ]  \  }}|s	||f  y wN ).0rC   rD   s      r9   	<genexpr>z#get_cors_headers.<locals>.<genexpr>   s     =11aVs   
	)rW   rO   r   add
ACL_ORIGINACL_EXPOSE_HEADERSACL_CREDENTIALS"ACL_REQUEST_HEADER_PRIVATE_NETWORKACL_RESPONSE_PRIVATE_NETWORKACL_REQUEST_METHODupperre   ACL_REQUEST_HEADERSACL_ALLOW_HEADERSACL_MAX_AGEACL_METHODSrP   infor5   anymapr6   r>   )rS   rc   request_methodorigins_to_setheadersoriginr,   acl_request_methods           r9   get_cors_headersr      s   %g/B/B8/LMNkG J' ! #*++.>"?G{{)*#)  	+o= BCvM*1++6M*NT[0E,- ",001CRHNNP "4I8N"N *;7ODWDWXkDl)mG%&#*;;y#9GK #*;;y#9GK HH G
 {{=! :#%I&'!+>"Q&3~w{{9'=>?KK)====r;   c                    t        | t              rt        j                  d       | S t	        | j
                  t              s4t	        | j
                  t              st        | j
                        | _        t        |t        j
                  t        j                        }t        j                  dt        |             |j                         D ]!  \  }}| j
                  j                  ||       # | S )z
    Performs the actual evaluation of Flask-CORS options and actually
    modifies the response object.

    This function is used both in the decorator and the after_request
    callback
    z*CORS have been already evaluated, skippingzSettings CORS headers: %s)hasattrFLASK_CORS_EVALUATEDrP   rQ   r1   r   r   r   r   r   methodrA   r>   rp   )resprS   headers_to_setrC   rD   s        r9   set_cors_headersr      s     t)*		>?
 dllG,Zi5X .%gwONII)3~+>?$$&1A ' Kr;   c                 V     t         t              ryg d}t         fd|D              S )NT)
r   \]?$^[r   ()c              3   &   K   | ]  }|v  
 y wrl   rm   )rn   cmaybe_regexs     r9   ro   z!probably_regex.<locals>.<genexpr>  s     @-?1#-?s   )r1   r2   r}   )r   common_regex_charss   ` r9   r6   r6   
  s*    +{+P @-?@@@r;   c                     | dk(  rdS | S )z
    Replace the invalid regex r'*' with the valid, wildcard regex r'/.*' to
    enable the CORS app extension to have a more user friendly api.
    r   rL   rm   )regs    r9   r?   r?     s    
 4K5(S(r;   c                 0     t         fd|D              S )Nc              3   8   K   | ]  }t        |        y wrl   )try_match_pattern)rn   r3   rN   insts     r9   ro   z(try_match_any_pattern.<locals>.<genexpr>  s     Wh7 w>hs   )r}   )r   patternsrN   s   ` `r9   rR   rR     s    WhWWWr;   c                    t        |t              rt        j                  ||       S t	        |      r-|rdnt        j
                  }	 t        j                  || |      S 	 t        |       }t        |      }|r||k(  S |j                         |j                         k(  S # t        j                  $ r Y yw xY w# t        $ r | |k(  cY S w xY w)a:  
    Safely attempts to match a pattern or string to a value. This
    function can be used to match request origins, headers, or paths.
    The value of caseSensitive should be set in accordance to the
    data being compared e.g. origins and headers are case insensitive
    whereas paths are case-sensitive
    r   )flagsF)
r1   r2   rematchr6   
IGNORECASEerrorrA   casefold	Exception)valuer3   rN   r   rD   ps         r9   r   r     s     ';'xx''g"	88GU%88 JL&qAvHAJJLAJJL,HH xx 		    s*   B  B7 = B7 B43B47CCc                     t         j                         }|j                  t        |              |r|D ]  }|j                  |        t	        |      S )z
    Compute CORS options for an application by combining the DEFAULT_OPTIONS,
    the app's configuration-specified options and any dictionaries passed. The
    last specified option wins.
    )DEFAULT_OPTIONScopyupdateget_app_kwarg_dictserialize_options)appInstancedictsrS   ds       r9   get_cors_optionsr   6  sJ     ""$GNN%k23ANN1  W%%r;   c                     | xs t         }t        |di       }t        D ci c]D  }|j                  |      |j	                         j                  dd      |j                  |      F c}S c c}w )z;Returns the dictionary of CORS specific app configurations.configcors_r   )r   getattrCONFIG_OPTIONSrO   lowerreplace)r   app
app_configrC   s       r9   r   r   E  sg    

$C h+JGUw~!YcYgYghiYjYvAGGIgr*JNN1,==~wwws
   A,2A,c                     | yt        | t              s1t        | t              r!dj                  d t	        |       D              S t        |       S )z
    A more flexible str function which intelligently handles stringifying
    strings, lists and other iterables. The results are lexographically sorted
    to ensure generated responses are consistent when iterables such as Set
    are used.
    Nr]   c              3   2   K   | ]  }t        |        y wrl   )rA   )rn   items     r9   ro   zflexible_str.<locals>.<genexpr>Y  s     ;{tT{s   )r1   rA   r   ra   r@   )objs    r9   flexible_strr   O  sA     {S!jh&?yy;vc{;;;3xr;   c                 X    || v r&t        | |         }|r|j                         n|| |<   y y rl   )r   rw   )options_dictr<   rw   r   s       r9   serialize_optionr   ^  s2    
l\#./-2EKKMS r;   c                 L    t        | t              st        | t              s| gS | S )zT
    Wraps scalars or string types as a list, or returns the iterable instance.
    )r1   rA   r   )r   s    r9   ensure_iterabler   d  s#     $JtX$>vr;   c                 P    t        |       D cg c]  }t        |       c}S c c}w rl   )r   r?   )paramxs     r9   sanitize_regex_paramr   n  s%    .u565!F1I5666s   #c                    | xs i j                         }| j                         D ]!  }|t        vst        j	                  d|       # t        |j                  d            |d<   t        |j                  d            |d<   d|d   v r|d   r|d   rt        d      t        |d       t        |d	d
       t        |j                  d      t              r(t        t        |d   j                                     |d<   |S )zL
    A helper method to serialize and processes the options dictionary.
    z'Unknown option passed to Flask-CORS: %sr    r"   rL   r$   r&   z}Cannot use supports_credentials in conjunction withan origin string of '*'. See: http://www.w3.org/TR/cors/#resource-requestsr#   r!   T)rw   r%   )r   keysr   rP   warningr   rO   rB   r   r1   r   rA   inttotal_seconds)optsrS   r<   s      r9   r   r   r  s     zr!Gyy{o%KKA3G 
 .gkk).DEGI3GKK4PQGO 	""w/E'F7SbKc;
 	
 W./Wit4'++i()4 WY%7%E%E%G!HI	Nr;   )Trl   )F)2loggingr   collections.abcr   datetimer   flaskr   r   werkzeug.datastructuresr   r   	getLogger__name__rP   rq   r{   ry   rr   rs   rz   ru   rv   rx   rt   ALL_METHODSr   r   typecompiler2   r=   r   rF   rJ   rW   re   r   r   r6   r?   rR   r   r   r   r   r   r   r   r   rm   r;   r9   <module>r      s    	 $  & 6g! +
,2 4 4&E  5 6 %M "J$ /  :2::b>""EB1h	7>t<A)X .&x>7r;   