
    wOgF6                        d Z ddlZddlZ	 ddlmZ n# e$ r	 ddlmZ Y nw xY w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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          Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*d  Z+d! Z,d" Z-d# Z.d*d$Z/d% Z0d+d&Z1d' Z2d( Z3d) Z4dS ),z
    core
    ~~~~
    Core functionality shared between the extension and the decorator.

    :copyright: (c) 2016 by Cory Dolphin.
    :license: MIT, see LICENSE for more details.
    N)Iterable)	timedelta)string_types)requestcurrent_app)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-Request-MethodzAccess-Control-Request-Headers)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_FLASK_CORS_EVALUATED *FTz/*)originsmethodsallow_headersexpose_headerssupports_credentialsmax_agesend_wildcardautomatic_optionsvary_header	resourcesintercept_exceptionsalways_sendc                    t          | t                    r3d |                                 D             } d }t          | |d          S t          | t                    rt          |           i fgS t          | t                    rd | D             S t          | t                    rt          |           i fgS t          d          )Nc                 6    g | ]\  }}t          |          |fS  re_fix.0kvs      I/var/www/AppWebDeiv/myenv/lib/python3.11/site-packages/flask_cors/core.py
<listcomp>z#parse_resources.<locals>.<listcomp>F   s&    BBB1fQii^BBB    c                 D    | \  }}t          t          |                    S N)lenget_regexp_pattern)pairmaybe_regex_s      r5   pattern_lengthz'parse_resources.<locals>.pattern_lengthK   s"    !NK)+66777r7   T)keyreversec                 0    g | ]}t          |          i fS r.   r/   )r2   rs     r5   r6   z#parse_resources.<locals>.<listcomp>W   s"    333AB333r7   z(Unexpected value for resources argument.)	
isinstancedictitemssortedr   r0   r   RegexObject
ValueError)r)   r?   s     r5   parse_resourcesrJ   B   s    )T"" E CB	0A0ABBB	
	8 	8 	8 i("$ $ $ 	$ 
I|	,	, E	""B'((	Ix	(	( 	E333333 
I	,	, E	""B'(( CDDDr7   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
    )patternAttributeErrorstr)regexps    r5   r;   r;   b   s<    ~   6{{s   	 %%c                 4   |                      d          }d|v }|rt                              d|           |r2|                      d          rt                              d           dgS t          ||          rt                              d           |gS t                              d           d S |                      d	          r5|r|                      d
          rd S dgS t	          d |D                       S t                              d           d S )Nr    .*z&CORS request received with 'Origin' %sr&   z=Allowed origins are set to '*'. Sending wildcard CORS header.r   z:The request's Origin header matches. Sending CORS headers.zBThe request's Origin header does not match any of allowed origins.r+   r$   c                 0    g | ]}t          |          |S r.   )probably_regex)r2   os     r5   r6   z$get_cors_origins.<locals>.<listcomp>   s%    GGG^A5F5FG1GGGr7   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_anyrG   )optionsrequest_originr    wildcards       r5   get_cors_originsr\   q   sD   kk)$$GH  '		:NKKK  	O44 	IIUVVV5L >733 	IIRUUU ###IIZ[[[4 
]	#	#  	I {{122 tu GGgGGGHHH 			  \  	]  	]  	]tr7   c                      |rTd |                     d          D             }t           fd|          }d                    t          |                    S d S )Nc                 6    g | ]}|                                 S r.   )strip)r2   hs     r5   r6   z%get_allow_headers.<locals>.<listcomp>   s     MMM17799MMMr7   ,c                 J    t          |                     d                    S )Nr"   )rX   rU   )r`   rY   s    r5   <lambda>z#get_allow_headers.<locals>.<lambda>   s    mAw{{?'C'CDD r7   , )splitfilterjoinrG   )rY   acl_request_headersrequest_headersmatching_headerss   `   r5   get_allow_headersrk      sp     	3MM.A.G.G.L.LMMM "DDDD
 

 yy 0112224r7   c                 r   t          | |                    d                    }t                      }|s|S |D ]}|                    t          |           |                     d          |t
          <   |                     d          r
d|t          <   |dk    r|                    t          d                                          }|r||                     d          v rkt          | |                    t                              |t          <   |                     d          |t          <   |                     d          |t          <   nt                              d	           |                     d
          r|t                   dk    rnt!          |                     d                    dk    sHt!          |          dk    s5t#          t%          t&          |                     d                              r|                    dd           t          d |                                D                       S )NOriginr#   r$   truer   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       Varyc              3   (   K   | ]\  }}|||fV  d S r9   r.   r1   s      r5   	<genexpr>z#get_cors_headers.<locals>.<genexpr>   s/      ==11=aV======r7   )r\   rU   r	   add
ACL_ORIGINACL_EXPOSE_HEADERSACL_CREDENTIALSACL_REQUEST_METHODupperrk   ACL_REQUEST_HEADERSACL_ALLOW_HEADERSACL_MAX_AGEACL_METHODSrV   infor:   anymaprS   rF   )rY   ri   request_methodorigins_to_setheadersoriginacl_request_methods          r5   get_cors_headersr      s   %g/B/B8/L/LMMNkkG   ( (J''''")++.>"?"?G{{)** *#)  "",001CRHHNNPP  		M"4I8N8N"N"N
 *;7ODWDWXkDlDl)m)mG%&#*;;y#9#9GK #*;;y#9#9GK  HH  L  M  M  M {{=!! 	* :#%%'++i(())A--.!!A%%#ngkk)&<&<==>> &KK)))========r7   c                    t          | t                    rt                              d           | S t	          | j        t                    s3t	          | j        t                    st          | j                  | _        t          |t          j        t          j
                  }t                              dt          |                     |                                D ] \  }}| j                            ||           !| S )z
    Performs the actual evaluation of Flas-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_EVALUATEDrV   rW   rD   r   r   r	   r   r   methodrN   rF   rs   )resprY   headers_to_setr3   r4   s        r5   set_cors_headersr      s     t)** 		>???
 t|W-- /dlI66/ ..%gwOONII)3~+>+>???$$&&  1AKr7   c                 p     t           t                    rdS g d}t           fd|D                       S )NT)
r   \]?$^[r   ()c              3       K   | ]}|v V  	d S r9   r.   )r2   cr=   s     r5   rr   z!probably_regex.<locals>.<genexpr>  s(      AAA$AAAAAAr7   )rD   rH   r~   )r=   common_regex_charss   ` r5   rS   rS      sM    +{++ CtPPP AAAA.@AAABBBr7   c                     | dk    rdn| 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   rQ   r.   )regs    r5   r0   r0     s    
 4KK55S(r7   c                 :     t           fd|D                       S )Nc              3   8   K   | ]}t          |          V  d S r9   )	try_match)r2   rL   insts     r5   rr   z try_match_any.<locals>.<genexpr>  s-      @@Gyw''@@@@@@r7   )r~   )r   patternss   ` r5   rX   rX     s&    @@@@x@@@@@@r7   c                 8   t          |t                    rt          j        ||           S t	          |          r!t          j        || t          j                  S 	 |                                 |                                k    S # t          $ r	 | |k    cY S w xY w)zASafely attempts to match a pattern or string to a request origin.)flags)rD   rH   rematchrS   
IGNORECASElowerrM   )rZ   r=   s     r5   r   r     s    +{++ 1x^444		$	$ 1x^2=IIII	1!''))[->->-@-@@@ 	1 	1 	1![0000	1s   )B BBc                     t                                           }|                    t          |                      |r|D ]}|                    |           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dictsrY   ds       r5   get_cors_optionsr      sj     ""$$GNN%k22333  	 	ANN1W%%%r7   c                 ^    | pt           }t          |di           fdt          D             S )z;Returns the dictionary of CORS specific app configurations.configc                     i | ]S}                     |          |                                                    dd                               |          TS )Ncors_r   )rU   r   replace)r2   r3   
app_configs     r5   
<dictcomp>z&get_app_kwarg_dict.<locals>.<dictcomp>6  s[       >>!( 	
		'2&&
q(9(9(((r7   )r   getattrCONFIG_OPTIONS)r   appr   s     @r5   r   r   /  sK    %+C h++J      r7   c                     | dS t          | t                    sAt          | t                    r,d                    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.
    Nrd   c              3   4   K   | ]}t          |          V  d S r9   )rN   )r2   items     r5   rr   zflexible_str.<locals>.<genexpr>H  s(      ;;tT;;;;;;r7   )rD   r   r   rg   rG   rN   )objs    r5   flexible_strr   =  sd     {t\** 3))yy;;vc{{;;;;;;3xxr7   c                 r    || v r2t          | |                   }|r|                                n|| |<   d S d S r9   )r   rx   )options_dictr@   rx   values       r5   serialize_optionr   M  sH    
l\#.//-2=EKKMMMS r7   c                 f    t          | t                    r| gS t          | t                    s| gS | S )zT
    Wraps scalars or string types as a list, or returns the iterable instance.
    )rD   r   r   )r   s    r5   ensure_iterabler   S  s;     $%% vh'' vr7   c                 4    d t          |           D             S )Nc                 ,    g | ]}t          |          S r.   r/   )r2   xs     r5   r6   z(sanitize_regex_param.<locals>.<listcomp>_  s    666!F1II666r7   )r   )params    r5   sanitize_regex_paramr   ^  s    66u556666r7   c                    | pi                                  }|                                 D ]&}|t          vrt                              d|           't          |                    d                    |d<   t          |                    d                    |d<   d|d         v r|d         r|d         rt          d          t          |d           t          |d	d
           t          |                    d          t                    r7t          t          |d                                                             |d<   |S )zL
    A helper method to serialize and processes the options dictionary.
    z'Unknown option passed to Flask-CORS: %sr    r"   rQ   r$   r&   z}Cannot use supports_credentials in conjunction withan origin string of '*'. See: http://www.w3.org/TR/cors/#resource-requestsr#   r!   T)rx   r%   )r   keysr   rV   warningr   rU   rI   r   rD   r   rN   inttotal_seconds)optsrY   r@   s      r5   r   r   b  sK    zr!!Gyy{{ H Ho%%KKA3GGG .gkk).D.DEEGI3GKK4P4PQQGO 	"""w/E'F"7SbKc" H I I 	I W.///Wit4444'++i(()44 J WY%7%E%E%G%G!H!HII	Nr7   r9   )F)5__doc__r   loggingcollections.abcr   ImportErrorcollectionsdatetimer   sixr   flaskr   r   werkzeug.datastructuresr   r	   	getLogger__name__rV   rt   r|   rz   ru   rv   r{   rw   ry   ALL_METHODSr   r   typecompilerH   rE   r   rJ   r;   r\   rk   r   r   rS   r0   rX   r   r   r   r   r   r   r   r   r.   r7   r5   <module>r      s    
			 %((((((( % % %$$$$$$$$%             & & & & & & & & 6 6 6 6 6 6 6 6g!! +
,2 4 4& 5 6 JJJ& & & /  d:2:b>>""$s*%(&*,1#%*)-#'!&,0#') ) )E E E@  - - -`  -> -> ->`  <C C C) ) )A A A
1 
1 
1& & &      > > > >	 	 	7 7 7    s    !!