zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
U
§N®dï6ã@sndZddlZddlZddlmZddlmZddlmZm    Z    ddl
m Z m Z e  e¡ZdZdZd    Zd
Zd Zd Zd ZdZdZdZdddddddgZddddddddd d!d"d#g Zd$Zee d%¡ƒZe d&ed&dd'dd'd(d(d)d(d(d* Z!d+d,„Z"d-d.„Z#d/d0„Z$d1d2„Z%d3d4„Z&d5d6„Z'd7d8„Z(d9d:„Z)d;d<„Z*d=d>„Z+d?d@„Z,dMdAdB„Z-dCdD„Z.dNdEdF„Z/dGdH„Z0dIdJ„Z1dKdL„Z2dS)Oz¶
    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)ÚrequestÚ current_app)ÚHeadersÚ    MultiDictzAccess-Control-Allow-OriginzAccess-Control-Allow-MethodszAccess-Control-Allow-HeaderszAccess-Control-Expose-Headersz Access-Control-Allow-CredentialszAccess-Control-Max-Agez$Access-Control-Allow-Private-NetworkzAccess-Control-Request-MethodzAccess-Control-Request-Headersz&Access-Control-Request-Private-NetworkÚGETÚHEADÚPOSTÚOPTIONSÚPUTÚPATCHÚDELETEZ CORS_ORIGINSZ CORS_METHODSZCORS_ALLOW_HEADERSZCORS_EXPOSE_HEADERSZCORS_SUPPORTS_CREDENTIALSZ CORS_MAX_AGEZCORS_SEND_WILDCARDZCORS_AUTOMATIC_OPTIONSZCORS_VARY_HEADERZCORS_RESOURCESZCORS_INTERCEPT_EXCEPTIONSZCORS_ALWAYS_SENDZ_FLASK_CORS_EVALUATEDÚÚ*FTz/*) ÚoriginsÚmethodsÚ allow_headersÚexpose_headersÚsupports_credentialsÚmax_ageÚ send_wildcardZautomatic_optionsÚ vary_headerÚ    resourcesZintercept_exceptionsÚ always_sendcCs†t|tƒr2dd„| ¡Dƒ}dd„}t||ddSt|tƒrJt|ƒifgSt|tƒrbdd„|DƒSt|tƒrzt|ƒifgStdƒ‚dS)    NcSsg|]\}}t|ƒ|f‘qS©©Úre_fix©Ú.0ÚkÚvrrúFd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\flask_cors/core.pyÚ
<listcomp>Bsz#parse_resources.<locals>.<listcomp>cSs|\}}tt|ƒƒS©N)ÚlenÚget_regexp_pattern)ÚpairÚ maybe_regexÚ_rrr"Úpattern_lengthGsz'parse_resources.<locals>.pattern_lengthT)ÚkeyÚreversecSsg|]}t|ƒif‘qSrr)rÚrrrr"r#Ssz(Unexpected value for resources argument.)    Ú
isinstanceÚdictÚitemsÚsortedÚstrrrÚ RegexObjectÚ
ValueError)rr*rrr"Úparse_resources>s
þ
 
 
r5cCs*z|jWStk
r$t|ƒYSXdS)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
    N)ÚpatternÚAttributeErrorr2)Úregexprrr"r&^s    r&cCs´| d¡}d|k}|rjt d|¡|r@| d¡r@t d¡dgSt||ƒrZt d¡|gSt d¡dSnF| d    ¡r¢|rŽ| d
¡r†dSdgSq°td d „|DƒƒSnt d ¡dSdS)Nrú.*z&CORS request received with 'Origin' %srz=Allowed origins are set to '*'. Sending wildcard CORS header.rz:The request's Origin header matches. Sending CORS headers.zBThe request's Origin header does not match any of allowed origins.rrcSsg|]}t|ƒs|‘qSr)Úprobably_regex)rÚorrr"r#•sz$get_cors_origins.<locals>.<listcomp>z‡The 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_anyr1)ÚoptionsÚrequest_originrZwildcardrrr"Úget_cors_originsms(
 
 
 
 
 
 
 
rBcs<|r8dd„| d¡Dƒ}t‡fdd„|ƒ}d t|ƒ¡SdS)NcSsg|] }| ¡‘qSr)Ústrip)rÚhrrr"r#Ÿsz%get_allow_headers.<locals>.<listcomp>ú,cst|ˆ d¡ƒS)Nr)r?r<)rD©r@rr"Ú<lambda>£óz#get_allow_headers.<locals>.<lambda>ú, )ÚsplitÚfilterÚjoinr1)r@Zacl_request_headersÚrequest_headersZmatching_headersrrFr"Úget_allow_headerss
þrNcCsLt|| d¡ƒ}tƒ}|s|S|D]}| t|¡q"| d¡|t<| d¡rTd|t<t|krr| t¡dkrrd|t<|dkrØ| t    d¡ 
¡}|rÎ|| d¡krÎt || t ¡ƒ|t <| d¡|t<| d¡|t<n
t d    ¡| d
¡r6|td krònDt| d ¡ƒd ks*t|ƒd ks*ttt| d ¡ƒƒr6| dd¡tdd„| ¡DƒƒS)NZOriginrrÚtruer rrrztThe request's Access-Control-Request-Method header does not match allowed methods. CORS headers will not be applied.rrréZVarycss|]\}}|r||fVqdSr$rrrrr"Ú    <genexpr>Ýsz#get_cors_headers.<locals>.<genexpr>)rBr<rÚaddÚ
ACL_ORIGINÚACL_EXPOSE_HEADERSÚACL_CREDENTIALSÚ"ACL_REQUEST_HEADER_PRIVATE_NETWORKÚACL_RESPONSE_PRIVATE_NETWORKÚACL_REQUEST_METHODÚupperrNÚACL_REQUEST_HEADERSÚACL_ALLOW_HEADERSÚ ACL_MAX_AGEÚ ACL_METHODSr=Úinfor%ÚanyÚmapr:r0)r@rMÚrequest_methodZorigins_to_setÚheadersÚoriginZacl_request_methodrrr"Úget_cors_headers¬s<
 ÿ
 
ÿþ rdcCs€t|tƒrt d¡|St|jtƒs<t|jtƒs<t|jƒ|_t|t    jt    j
ƒ}t dt |ƒ¡|  ¡D]\}}|j  ||¡qd|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, skippingzSettings CORS headers: %s)ÚhasattrÚFLASK_CORS_EVALUATEDr=r>r.rbrrrdrÚmethodr2r0rR)Úrespr@Zheaders_to_setr r!rrr"Úset_cors_headersàs
 
 
 
ÿ ric
s@tˆtƒrdSddddddddd    d
g
}t‡fd d „|DƒƒSdS) NTrú\ú]ú?ú$ú^ú[ú(ú)c3s|]}|ˆkVqdSr$r)rÚc©r(rr"rQsz!probably_regex.<locals>.<genexpr>)r.r3r_)r(Zcommon_regex_charsrrsr"r:þs
r:cCs|dkr dS|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.
    rr9r)Úregrrr"rsrcst‡fdd„|DƒƒS)Nc3s|]}tˆ|ƒVqdSr$)Ú    try_match)rr6©Úinstrr"rQsz try_match_any.<locals>.<genexpr>)r_)rwÚpatternsrrvr"r?sr?cCsdt|tƒrt ||¡St|ƒr0tj||tjdSz| ¡| ¡kWStk
r^||kYSXdS)zASafely attempts to match a pattern or string to a request origin.)ÚflagsN)r.r3ÚreÚmatchr:Ú
IGNORECASEÚlowerr7)rAr(rrr"rus
 rucGs6t ¡}| t|ƒ¡|r.|D]}| |¡qt|ƒ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)Ú appInstanceZdictsr@Údrrr"Úget_cors_options s  r…cs&|pt}t|diƒ‰‡fdd„tDƒS)z;Returns the dictionary of CORS specific app configurations.Úconfigcs2i|]*}ˆ |¡dk    r| ¡ dd¡ˆ |¡“qS)NZcors_r)r<r}Úreplace)rr ©Z
app_configrr"Ú
<dictcomp>6sþz&get_app_kwarg_dict.<locals>.<dictcomp>)rÚgetattrÚCONFIG_OPTIONS)rƒZapprrˆr"r/s
 
þrcCsD|dkr dSt|tƒs8t|tƒr8d dd„t|ƒDƒ¡St|ƒSdS)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.
    NrIcss|]}t|ƒVqdSr$)r2)rÚitemrrr"rQGszflexible_str.<locals>.<genexpr>)r.r2rrLr1)Úobjrrr"Ú flexible_str=s
rŽcCs,||kr(t||ƒ}|r | ¡n|||<dSr$)rŽrY)Z options_dictr+rYÚvaluerrr"Úserialize_optionLs rcCs(t|tƒr|gSt|tƒs |gS|SdS)zT
    Wraps scalars or string types as a list, or returns the iterable instance.
    N)r.r2rrvrrr"Úensure_iterableRs
 
 
r‘cCsdd„t|ƒDƒS)NcSsg|] }t|ƒ‘qSrr)rÚxrrr"r#^sz(sanitize_regex_param.<locals>.<listcomp>)r‘)Úparamrrr"Úsanitize_regex_param]sr”cCsº|pi ¡}| ¡D]}|tkrt d|¡qt| d¡ƒ|d<t| d¡ƒ|d<d|dkrv|drv|drvtdƒ‚t|dƒt|d    d
d t    | d ¡t
ƒr¶t t |d   ¡ƒƒ|d <|S) zL
    A helper method to serialize and processes the options dictionary.
    z'Unknown option passed to Flask-CORS: %srrr9rrz}Cannot use supports_credentials in conjunction withan origin string of '*'. See: http://www.w3.org/TR/cors/#resource-requestsrrT)rYr)rÚkeysr~r=Úwarningr”r<r4rr.rr2ÚintÚ total_seconds)Úoptsr@r+rrr"r‚as  
r‚)N)F)3Ú__doc__rzÚloggingÚcollections.abcrÚdatetimerZflaskrrZwerkzeug.datastructuresrrÚ    getLoggerÚ__name__r=rSr]r[rTrUr\rWrXrZrVZ ALL_METHODSr‹rfÚtypeÚcompiler3r/r~r5r&rBrNrdrir:rr?rur…rrŽrr‘r”r‚rrrr"Ú<module>sz  
û    õ 04