
    h<Q                     *   S r SSKJr  SSKJr  SSKJrJr  SSKJ	r	  SSK
Jr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  S\\\4   S\4S jrS\S\4S jr " S S5      r " S S5      r " S S5      r\" \5       " S S5      5       rS/r g)zo
An implementation of
U{Python Web Server Gateway Interface v1.0.1<http://www.python.org/dev/peps/pep-3333/>}.
    )Sequence)exc_info)ListUnion)warn)implementer)blockingCallFromThread)Logger)Failure)INTERNAL_SERVER_ERROR)	IResource)NOT_DONE_YETstringreturnc                     [        U [        5      (       a   U R                  S5      R                  S5      $ U R                  S5      $ )a;  
Convert C{string} to a WSGI "bytes-as-unicode" string.

If it's a byte string, decode as ISO-8859-1. If it's a Unicode string,
round-trip it to bytes and back using ISO-8859-1 as the encoding.

@type string: C{str} or C{bytes}
@rtype: C{str}

@raise UnicodeEncodeError: If C{string} contains non-ISO-8859-1 chars.

iso-8859-1)
isinstancestrencodedecoder   s    W/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/web/wsgi.py_wsgiStringr   %   s:     &#}}\*11,??}}\**    c                 $    U R                  S5      $ )z
Convert C{string} from a WSGI "bytes-as-unicode" string to an
ISO-8859-1 byte string.

@type string: C{str}
@rtype: C{bytes}

@raise UnicodeEncodeError: If C{string} contains non-ISO-8859-1 chars.
r   )r   r   s    r   _wsgiStringToBytesr   7   s     ==&&r   c                   V    \ rS rSrSr\" 5       rS\SS4S jrS\	\   SS4S jr
S	 rS
rg)_ErrorStreamD   a  
File-like object instances of which are used as the value for the
C{'wsgi.errors'} key in the C{environ} dictionary passed to the application
object.

This simply passes writes on to L{logging<twisted.logger>} system as
error events from the C{'wsgi'} system.  In the future, it may be desirable
to expose more information in the events it logs, such as the application
object which generated the message.
datar   Nc                     [        U[        5      (       d'  [        SU< S[        U5      R                  < S35      eU R
                  R                  USSU4S9  g)z
Generate an event for the logging system with the given bytes as the
message.

This is called in a WSGI application thread, not the I/O thread.

@type data: str

@raise TypeError: if C{data} is not a native string.
z"write() argument must be str, not  ()wsgiT)systemisErrormessageN)r   r   	TypeErrortype__name___logerror)selfr    s     r   write_ErrorStream.writeR   sN     $$$d,,.  			VTD7Kr   iovecc                 D    U R                  SR                  U5      5        g)a"  
Join the given lines and pass them to C{write} to be handled in the
usual way.

This is called in a WSGI application thread, not the I/O thread.

@param iovec: A C{list} of C{'\n'}-terminated C{str} which will be
    logged.

@raise TypeError: if C{iovec} contains any non-native strings.
 N)r.   join)r-   r0   s     r   
writelines_ErrorStream.writelinesg   s     	

2775>"r   c                     g)z
Nothing is buffered, so flushing does nothing.  This method is required
to exist by PEP 333, though.

This is called in a WSGI application thread, not the I/O thread.
N r-   s    r   flush_ErrorStream.flushu   s    r   r7   )r*   
__module____qualname____firstlineno____doc__r
   r+   r   r.   r   r4   r9   __static_attributes__r7   r   r   r   r   D   sB    	 8DL# L$ L*#S	 #d #r   r   c                   B    \ rS rSrSrS rS
S jrS
S jrS
S jrS r	S	r
g)_InputStream~   al  
File-like object instances of which are used as the value for the
C{'wsgi.input'} key in the C{environ} dictionary passed to the application
object.

This only exists to make the handling of C{readline(-1)} consistent across
different possible underlying file-like object implementations.  The other
supported methods pass through directly to the wrapped object.
c                     Xl         g)z\
Initialize the instance.

This is called in the I/O thread, not a WSGI application thread.
N_wrapped)r-   inputs     r   __init___InputStream.__init__   s	     r   Nc                 r    Uc  U R                   R                  5       $ U R                   R                  U5      $ )zk
Pass through to the underlying C{read}.

This is called in a WSGI application thread, not the I/O thread.
)rE   readr-   sizes     r   rJ   _InputStream.read   s1     <==%%''}}!!$''r   c                 ~    US:X  d  Uc  U R                   R                  5       $ U R                   R                  U5      $ )z
Pass through to the underlying C{readline}, with a size of C{-1} replaced
with a size of L{None}.

This is called in a WSGI application thread, not the I/O thread.
)rE   readlinerK   s     r   rP   _InputStream.readline   s7     2:==))++}}%%d++r   c                 r    Uc  U R                   R                  5       $ U R                   R                  U5      $ )zp
Pass through to the underlying C{readlines}.

This is called in a WSGI application thread, not the I/O thread.
)rE   	readlinesrK   s     r   rS   _InputStream.readlines   s1     <==**,,}}&&t,,r   c                 ,    [        U R                  5      $ )zo
Pass through to the underlying C{__iter__}.

This is called in a WSGI application thread, not the I/O thread.
)iterrE   r8   s    r   __iter___InputStream.__iter__   s     DMM""r   rD   N)r*   r;   r<   r=   r>   rG   rJ   rP   rS   rW   r?   r7   r   r   rA   rA   ~   s     	(,	-#r   rA   c                   X    \ rS rSrSrSr\" 5       rS rS r	SS jr
S rS	 rS
 rS rSrg)_WSGIResponse   a  
Helper for L{WSGIResource} which drives the WSGI application using a
threadpool and hooks it up to the L{http.Request}.

@ivar started: A L{bool} indicating whether or not the response status and
    headers have been written to the request yet.  This may only be read or
    written in the WSGI application thread.

@ivar reactor: An L{IReactorThreads} provider which is used to call methods
    on the request in the I/O thread.

@ivar threadpool: A L{ThreadPool} which is used to call the WSGI
    application object in a non-I/O thread.

@ivar application: The WSGI application object.

@ivar request: The L{http.Request} upon which the WSGI environment is
    based and to which the application's output will be sent.

@ivar environ: The WSGI environment L{dict}.

@ivar status: The HTTP response status L{str} supplied to the WSGI
    I{start_response} callable by the application.

@ivar headers: A list of HTTP response headers supplied to the WSGI
    I{start_response} callable by the application.

@ivar _requestFinished: A flag which indicates whether it is possible to
    generate more response data or not.  This is L{False} until
    L{http.Request.notifyFinish} tells us the request is done,
    then L{True}.
Fc                    SU l         Xl        X l        X0l        X@l        U R                  R                  5       R                  U R                  5        UR                  (       a  SSR                  UR                  5      -   nOSnUR                  (       a  SSR                  UR                  5      -   nOSnUR                  R                  SS5      n[        U5      S:X  a  SnOUS   nUR                  5       n	[        UR                   5      [        U	R"                  5      [        [%        U	R&                  5      5      [        U5      [        U5      [        U5      [        UR)                  S5      =(       d    S5      [        UR)                  S5      =(       d    S5      [        UR+                  5       5      [        [%        UR-                  5       R&                  5      5      [        UR.                  5      S	.U l        S U R                  l        UR4                  R7                  5        Hf  u  pS
[        U
5      R9                  5       R;                  SS5      -   n
SR                  S U 5       5      R;                  SS5      U R0                  U
'   Mh     U R0                  R=                  SUR?                  5       =(       a    S=(       d    SSSS[A        5       [C        URD                  5      S.5        g )NF   /r      ?   s   content-typer2   s   content-length)REQUEST_METHODREMOTE_ADDRREMOTE_PORTSCRIPT_NAME	PATH_INFOQUERY_STRINGCONTENT_TYPECONTENT_LENGTHSERVER_NAMESERVER_PORTSERVER_PROTOCOLHTTP_-_,c              3   8   #    U  H  n[        U5      v   M     g 7frY   )r   ).0vs     r   	<genexpr>)_WSGIResponse.__init__.<locals>.<genexpr>  s     )I&Q+a..&s   
 )r`   r   httpshttpT)zwsgi.versionzwsgi.url_schemezwsgi.run_oncezwsgi.multithreadzwsgi.multiprocesszwsgi.errorsz
wsgi.input)#startedreactor
threadpoolapplicationrequestnotifyFinishaddBoth	_finishedprepathr3   postpathurisplitlengetClientAddressr   methodhostr   port	getHeadergetRequestHostnamegetHostclientprotoenvirondefaultContentTyperequestHeadersgetAllRawHeadersupperreplaceupdateisSecurer   rA   content)r-   rz   r{   r|   r}   
scriptNamepathInfopartsqueryString
remotePeernamevaluess               r   rG   _WSGIResponse.__init__   sG   $&!!#++DNN;??		'// ::JJdii(8(899HH!!$*u:?K(K --/
)'..9&z7&s:??';<&z2$X.'4'(9(9/(J(PbQ)'*;*;<M*N*TRTU&w'A'A'CD&s7??+<+A+A'BC*7+>+>?
" +/'#22CCELD[.446>>sCHHD "%)I&)I!I!Q!Qc"DLL	 F 	 &#*#3#3#5#A'#KV!&$(%*+~ +7??;+	
r   c                     SU l         g)zK
Record the end of the response generation for the request being
serviced.
TN)_requestFinished)r-   ignoreds     r   r   _WSGIResponse._finished2  s    
 !%r   Nc           	         U R                   (       a  Ub  US   R                  US   5      e[        U[        5      (       d.  [	        SR                  U[        U5      R                  5      5      e[        U[        5      (       a  Og[        U[        5      (       a+  [        SU< S[        U5      R                  < S3[        S9  O'[	        SU< S[        U5      R                  < S35      eU H  n[        U[        5      (       a  Og[        U[        5      (       a+  [        S	U< S[        U5      R                  < S3[        S9  O'[	        S
U< S[        U5      R                  < S35      e[        U5      S:w  a  [	        S
U< 35      eU H'  n[        U[        5      (       a  M  [	        SU< 35      e   M     Xl        X l        U R                   $ )z
The WSGI I{start_response} callable.  The given values are saved until
they are needed to generate the response.

This will be called in a non-I/O thread.
r`      z!status must be str, not {!r} ({})zheaders should be a list, not r"   r#   )categoryzheaders must be a list, not z)header should be a (str, str) tuple, not z'header must be a (str, str) tuple, not z%header must be (str, str) tuple, not )ry   with_tracebackr   r   r(   formatr)   r*   listr   r   RuntimeWarningtupler   statusheadersr.   )r-   r   r   excInfoheaderelems         r   startResponse_WSGIResponse.startResponse9  s    <<G/!*++GAJ77 &#&&3::DL11  gt$$**DM224' DM224  F&%((FH--tF|446+  tF|446  6{a"I& TUU
 !$,,#&KF:$VWW - 4 zzr   c                 ~   ^ ^ UU 4S jn [        T R                  UT R                  5      ST l        $ ! ST l        f = f)z
The WSGI I{write} callable returned by the I{start_response} callable.
The given bytes will be written to the response body, possibly flushing
the status and headers first.

This will be called in a non-I/O thread.
c                 j   > U (       d  TR                  5         TR                  R                  T5        g rY   )_sendResponseHeadersr}   r.   )ry   r    r-   s    r   	wsgiWrite&_WSGIResponse.write.<locals>.wsgiWrite  s$    ))+LLt$r   T)r	   rz   ry   )r-   r    r   s   `` r   r.   _WSGIResponse.write~  s1    B	%
	 )$,,	4<<PDL4DLs    3 	<c                 f   U R                   R                  SS5      u  p[        U5      nU R                  R	                  U[        U5      5        U R                   HS  u  p4UR                  5       S;  d  M  U R                  R                  R                  [        U5      [        U5      5        MU     g)z
Set the response code and response headers on the request object, but
do not flush them.  The caller is responsible for doing a write in
order for anything to actually be written out in response to the
request.

This must be called in the I/O thread.
Nr`   )serverdate)
r   r   intr}   setResponseCoder   r   lowerresponseHeadersaddRawHeader)r-   coder'   r   values        r   r   "_WSGIResponse._sendResponseHeaders  s     ))$24y$$T+=g+FG<<KDzz|#55,,99&t,.@.G (r   c                 N    U R                   R                  U R                  5        g)zW
Start the WSGI application in the threadpool.

This must be called in the I/O thread.
N)r{   callInThreadrunr8   s    r   start_WSGIResponse.start  s     	$$TXX.r   c                   ^   T R                  T R                  T R                  5      nU H.  nU(       a  T R                  U5        T R                  (       d  M.    O   [        USS5      nUb  U" 5         U 4S jnT R                  R                  UT R                  5        ST l        g! [         a9    U 4S jnT R                  R                  " UT R                  /[        5       Q76    NJf = f)z
Call the WSGI application object, iterate it, and handle its output.

This must be called in a non-I/O thread (ie, a WSGI application
thread).
closeNc                    > TR                   (       d2  U (       d  TR                  5         TR                  R                  5         g g rY   )r   r   r}   finish)ry   r-   s    r   
wsgiFinish%_WSGIResponse.run.<locals>.wsgiFinish  s0    ,,"113LL'') -r   c                   > TR                   R                  S[        X!U5      S9  U (       a  TR                  R	                  5         g TR                  R                  [        5        TR                  R                  5         g )NzWSGI application error)failure)r+   r   r   r}   loseConnectionr   r   r   )ry   r)   r   	tracebackr-   s       r   	wsgiError$_WSGIResponse.run.<locals>.wsgiError  s^    		!!,ge96U "  LL//1LL001FGLL'')r   T)r|   r   r   r.   r   getattrrz   callFromThreadry   BaseExceptionr   )r-   appIteratorr   r   r   r   s   `     r   r   _WSGIResponse.run  s    	B**4<<9K9KLK#JJt$(((	 $
 K$7E  * LL''
DLLA-  	N* LL''	4<<M(*M	Ns   AB( B( (A C+*C+)	r   r|   r   r   rz   r}   ry   r   r{   rY   )r*   r;   r<   r=   r>   r   r
   r+   rG   r   r   r.   r   r   r   r?   r7   r   r   r[   r[      s?    B 8DN
`%CJ) V(/'r   r[   c                   4    \ rS rSrSrSrS rS rS rS r	Sr
g	)
WSGIResourcei  a/  
An L{IResource} implementation which delegates responsibility for all
resources hierarchically inferior to it to a WSGI application.

The C{environ} argument passed to the application, includes the
C{REMOTE_PORT} key to complement the C{REMOTE_ADDR} key.

@ivar _reactor: An L{IReactorThreads} provider which will be passed on to
    L{_WSGIResponse} to schedule calls in the I/O thread.

@ivar _threadpool: A L{ThreadPool} which will be passed on to
    L{_WSGIResponse} to run the WSGI application object.

@ivar _application: The WSGI application object.
Tc                 (    Xl         X l        X0l        g rY   )_reactor_threadpool_application)r-   rz   r{   r|   s       r   rG   WSGIResource.__init__  s    %'r   c                     [        U R                  U R                  U R                  U5      nUR	                  5         [
        $ )a  
Turn the request into the appropriate C{environ} C{dict} suitable to be
passed to the WSGI application object and then pass it on.

The WSGI application object is given almost complete control of the
rendering process.  C{NOT_DONE_YET} will always be returned in order
and response completion will be dictated by the application object, as
will the status, headers, and the response body.
)r[   r   r   r   r   r   )r-   r}   responses      r   renderWSGIResource.render
  s9     !MM4++T->->
 	r   c                     [        S5      e)z
Reject attempts to retrieve a child resource.  All path segments beyond
the one which refers to this resource are handled by the WSGI
application object.
z/Cannot get IResource children from WSGIResourceRuntimeError)r-   r   r}   s      r   getChildWithDefault WSGIResource.getChildWithDefault  s     LMMr   c                     [        S5      e)z
Reject attempts to add a child resource to this resource.  The WSGI
application object handles all path segments beneath this resource, so
L{IResource} children can never be found.
z0Cannot put IResource children under WSGIResourcer   )r-   pathchilds      r   putChildWSGIResource.putChild"  s     MNNr   )r   r   r   N)r*   r;   r<   r=   r>   isLeafrG   r   r   r   r?   r7   r   r   r   r     s$    $ F(
 NOr   r   N)!r>   collections.abcr   sysr   typingr   r   warningsr   zope.interfacer   twisted.internet.threadsr	   twisted.loggerr
   twisted.python.failurer   twisted.web.httpr   twisted.web.resourcer   twisted.web.serverr   r   bytesr   r   r   rA   r[   r   __all__r7   r   r   <module>r      s   
 %    & ; ! * 2 * + +c5j) +c +$
's 
'u 
'7 7t<# <#~o od	 Y8O 8O 8Ov 
r   