zmc
2023-12-22 9fdbf60165db0400c2e8e6be2dc6e88138ac719a
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
92
93
94
95
96
97
98
99
U
Þ=®d\6ã@s dZddlmZddlZddlmZddlmZddl    m
Z
ddl m Z dd    l mZdd
lmZejrŠdd lmZdd lmZdd lmZGdd„deƒZGdd„deƒZefdddddœdd„ZGdd„dƒZGdd„dƒZGdd„dƒZGdd „d ƒZGd!d"„d"ƒZdS)#a‹
WSGI Protocol Linter
====================
 
This module provides a middleware that performs sanity checks on the
behavior of the WSGI server and application. It checks that the
:pep:`3333` WSGI spec is properly implemented. It also warns on some
common HTTP errors such as non-empty responses for 304 status codes.
 
.. autoclass:: LintMiddleware
 
:copyright: 2007 Pallets
:license: BSD-3-Clause
é)Ú annotationsN)Ú TracebackType)Úurlparse)Úwarné)ÚHeaders)Úis_entity_header)Ú FileWrapper)Ú StartResponse)ÚWSGIApplication)ÚWSGIEnvironmentc@seZdZdZdS)Ú WSGIWarningz Warning class for WSGI warnings.N©Ú__name__Ú
__module__Ú __qualname__Ú__doc__©rrúOd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\werkzeug/middleware/lint.pyr  sr c@seZdZdZdS)Ú HTTPWarningz Warning class for HTTP warnings.Nrrrrrr$srÚstrÚobjectzt.TypeÚNone)ÚcontextÚobjÚneedÚreturncCs8t|ƒ|k    r4t|›d|j›dt|ƒj›dtdddS)Nz
 requires z, got Ú.é©Ú
stacklevel)Útyperrr )rrrrrrÚ
check_type(s  ýr"c@sXeZdZdddœdd„Zdddœd    d
„Zdddœd d „Zd dœdd„Zddœdd„ZdS)Ú InputStreamz t.IO[bytes]r©ÚstreamrcCs
||_dS©N©Ú_stream©Úselfr%rrrÚ__init__2szInputStream.__init__út.AnyÚbytes)ÚargsrcGsBt|ƒdkrtdtddnt|ƒdkr6tdtdd|jj|ŽS)NrzŸWSGI does not guarantee an EOF marker on the input stream, thus making calls to 'wsgi.input.read()' unsafe. Conforming servers may never return from this call.rréz2Too many parameters passed to 'wsgi.input.read()'.)Úlenrr r(Úread©r*r.rrrr15s û ýzInputStream.readcGsLt|ƒdkrtdtddn$t|ƒdkr8tdtddntdƒ‚|jj|ŽS)Nrz_Calls to 'wsgi.input.readline()' without arguments are unsafe. Use 'wsgi.input.read()' instead.rrr/z~'wsgi.input.readline()' was called with a size hint. WSGI does not support this, although it's available on all major servers.z5Too many arguments passed to 'wsgi.input.readline()'.)r0rr Ú    TypeErrorr(Úreadliner2rrrr4Fs ü üzInputStream.readlinezt.Iterator[bytes]©rcCs<z t|jƒWStk
r6tdtddtdƒYSXdS)Nz'wsgi.input' is not iterable.rrr)Úiterr(r3rr ©r*rrrÚ__iter__Ys
 zInputStream.__iter__cCstdtdd|j ¡dS)Nz(The application closed the input stream!rr©rr r(Úcloser7rrrr:`szInputStream.closeN)rrrr+r1r4r8r:rrrrr#1s
r#c@sXeZdZdddœdd„Zdddœdd    „Zdd
œd d „Zd ddœdd„Zdd
œdd„ZdS)Ú ErrorStreamz    t.IO[str]rr$cCs
||_dSr&r'r)rrrr+fszErrorStream.__init__r©ÚsrcCstd|tƒ|j |¡dS)Nzwsgi.error.write())r"rr(Úwrite©r*r=rrrr>is zErrorStream.writer5cCs|j ¡dSr&)r(Úflushr7rrrr@mszErrorStream.flushzt.Iterable[str])ÚseqrcCs|D]}| |¡qdSr&)r>)r*rAÚlinerrrÚ
writelinespszErrorStream.writelinescCstdtdd|j ¡dS)Nz(The application closed the error stream!rrr9r7rrrr:tszErrorStream.closeN)rrrr+r>r@rCr:rrrrr;es
r;c@s.eZdZddddœdd„Zdddœd    d
„Zd S) Ú GuardedWritezt.Callable[[bytes], object]ú    list[int]r)r>ÚchunksrcCs||_||_dSr&)Ú_writeÚ_chunks)r*r>rFrrrr+zszGuardedWrite.__init__r-r<cCs*td|tƒ| |¡|j t|ƒ¡dS)Nzwrite())r"r-rGrHÚappendr0r?rrrÚ__call__~s 
zGuardedWrite.__call__N)rrrr+rJrrrrrDysrDc@sXeZdZdddddœdd„Zddœd    d
„Zd dœd d „Zddœdd„Zddœdd„ZdS)ÚGuardedIteratorút.Iterable[bytes]útuple[int, Headers]rEr)ÚiteratorÚ headers_setrFrcCs(||_t|ƒj|_d|_||_||_dS)NF)Ú    _iteratorr6Ú__next__Ú_nextÚclosedrOrF)r*rNrOrFrrrr+…s
 zGuardedIterator.__init__r5cCs|Sr&rr7rrrr8‘szGuardedIterator.__iter__r-cCsP|jrtdtdd| ¡}|js0tdtddtd|tƒ|j t    |ƒ¡|S)Nz Iterated over closed 'app_iter'.rrz8The application returned before it started the response.zapplication iterator items)
rSrr rRrOr"r-rFrIr0)r*ÚrvrrrrQ”sý zGuardedIterator.__next__cCsþd|_t|jdƒr|j ¡|jrú|j\}}t|jƒ}|jdtd}|dkr”|D]2\}}|     ¡}|dkrPt
|ƒrPt d|›dt ƒqP|rút d    t ƒnfd
|kr¨d ks²n|d krà|d krÊt |›dt ƒ|rút |›dt ƒn|dk    rú||krút dt ƒdS)NTr:zcontent-length)r!i0)Úexpireszcontent-locationzEntity header z found in 304 response.z#304 responses must not have a body.édéÈéÌrz- responses must have an empty content length.z  responses must not have a body.zGContent-Length and the number of bytes sent to the client do not match.)rSÚhasattrrPr:rOÚsumrFÚgetÚintÚlowerrrrr )r*Ú status_codeÚheadersZ
bytes_sentÚcontent_lengthÚkeyÚ_valuerrrr:¥s@ 
 
 
 
ÿ
ÿ þýzGuardedIterator.closecCs.|js*ztdtƒWntk
r(YnXdS)Nz4Iterator was garbage collected before it was closed.)rSrr Ú    Exceptionr7rrrÚ__del__ÊsÿzGuardedIterator.__del__N)rrrr+r8rQr:rdrrrrrK„s
 %rKc@sveZdZdZdddœdd„Zdddœd    d
„Zd d d ddœdd„Zdddœdd„Zdddœdd„Zddddœdd„Z    dS)ÚLintMiddlewareaœWarns about common errors in the WSGI and HTTP behavior of the
    server and wrapped application. Some of the issues it checks are:
 
    -   invalid status codes
    -   non-bytes sent to the WSGI server
    -   strings returned from the WSGI application
    -   non-empty conditional responses
    -   unquoted etags
    -   relative URLs in the Location header
    -   unsafe calls to wsgi.input
    -   unclosed iterators
 
    Error information is emitted using the :mod:`warnings` module.
 
    :param app: The WSGI application to wrap.
 
    .. code-block:: python
 
        from werkzeug.middleware.lint import LintMiddleware
        app = LintMiddleware(app)
    r r)ÚapprcCs
||_dSr&)rf)r*rfrrrr+ëszLintMiddleware.__init__r )ÚenvironrcCsÀt|ƒtk    rtdtdddD]"}||krtd|›dtddq|dd    kr\td
tdd| d d ¡}| d d ¡}|r˜|ddkr˜td|›tdd|r¼|ddkr¼td|›tdddS)Nz/WSGI environment is not a standard Python dict.ér)    ÚREQUEST_METHODZ SERVER_NAMEZ SERVER_PORTú wsgi.versionú
wsgi.inputú wsgi.errorszwsgi.multithreadzwsgi.multiprocessz wsgi.run_oncezRequired environment key z
 not foundrrj)r/rz"Environ is not a WSGI 1.0 environ.Z SCRIPT_NAMEÚZ    PATH_INFOrú/z+'SCRIPT_NAME' does not start with a slash: z)'PATH_INFO' does not start with a slash: )r!Údictrr r[)r*rgraÚ script_nameZ    path_inforrrÚ check_environîs: ý 
ý   ýýzLintMiddleware.check_environrzlist[tuple[str, str]]z?None | tuple[type[BaseException], BaseException, TracebackType]rM)Ústatusr_Úexc_inforc    Cs\td|tƒ| dd¡d}t|ƒdks0| ¡s>tdtddt|ƒdksV|ddkrltd    |›d
tddt|ƒ}|d krŠtd tddt|ƒt    k    r¤td tdd|D]t}t|ƒt
k    sÄt|ƒdkrÒtdtdd|\}}t|ƒtk    sôt|ƒtk    rtdtdd|  ¡dkr¨tdtddq¨|dk    rBt |t
ƒsBtdtddt |ƒ}| |¡||fS)Nrrr/rrz!Status code must be three digits.rrhú zInvalid value for status zJ. Valid status strings are three digits, a space and a status explanation.rVzStatus code < 100 detected.zHeader list is not a list.rz#Header items must be 2-item tuples.z'Header keys and values must be strings.zFThe status header is not supported due to conflicts with the CGI spec.zInvalid value for exc_info.)r"rÚsplitr0Ú    isdecimalrr r\r!ÚlistÚtupler]Ú
isinstancerÚ check_headers)    r*rrr_rsZstatus_code_strr^ÚitemÚnameÚvaluerrrÚcheck_start_responsesH 
ü ÿ ü
z#LintMiddleware.check_start_responser)r_rcCs¢| d¡}|dk    rt| d¡r@| d¡r4tdtdd|dd…}|dd…|d    d…krdd
kstntd tdd| d ¡}|dk    ržt|ƒjsžtd tdddS)NÚetag)zW/úw/r€z)Weak etag indicator should be upper case.rhrrr/éÿÿÿÿú"zUnquoted etag emitted.Úlocationz+Absolute URLs required for location header.)r[Ú
startswithrrrÚnetloc)r*r_rrƒrrrrzNs(
 
 
ý &
 
ýzLintMiddleware.check_headersrL)Úapp_iterrcCst|tƒrtdtdddS)Nz The application returned a string. The response will send one character at a time to the client, which will kill performance. Return a list or iterable instead.rr)ryrrr )r*r†rrrÚcheck_iteratoris 
ûzLintMiddleware.check_iteratorr,©r.ÚkwargsrcsÎt|ƒdkrtdtdd|r,tdtdd|d}|d‰ˆ |¡t|dƒ|d<t|dƒ|d<t|d    <g‰g‰d
d
d d œ‡‡‡‡fd d„ }ˆ |t     d|¡¡}ˆ 
|¡t |t     tj t tfˆ¡ˆƒS)NrzA WSGI app takes two arguments.rz+A WSGI app does not take keyword arguments.rr/rkrlzwsgi.file_wrapperr,zt.Callable[[bytes], None]rˆcs„t|ƒdkr&tdt|ƒ›dtdd|r4tdtƒ|d}|d}t|ƒd    krX|dnd}ˆ |||¡ˆdd…<tˆ|||ƒˆƒS)
N>rrzInvalid number of arguments: z, expected 2 or 3.rrz1'start_response' does not take keyword arguments.rr/r)r0rr r~rD)r.r‰rrr_rs©rFrOr*Zstart_responserrÚchecking_start_responseŠs ý
þz8LintMiddleware.__call__.<locals>.checking_start_responser
)r0rr rqr#r;r    rfÚtÚcastr‡rKÚTupler\r)r*r.r‰rgr‹r†rrŠrrJss0 ÿ
 
ÿzLintMiddleware.__call__N)
rrrrr+rqr~rzr‡rJrrrrreÔs,4
re) rÚ
__future__rÚtypingrŒÚtypesrÚ urllib.parserÚwarningsrZdatastructuresrÚhttprZwsgir    Ú TYPE_CHECKINGZ_typeshed.wsgir
r r ÚWarningr rrr"r#r;rDrKrerrrrÚ<module>s(              4 P