HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/twisted/web/__pycache__/server.cpython-38.pyc
U

��W[uk�@sVdZddlmZmZddlZddlZddlZzddlmZWn(e	k
rdddl
mZdd�ZYnXddlZddl
mZddlmZdd	lmZmZmZdd
lmZmZddlmZmZddlmZmZdd
lmZm Z m!Z!ddl"m#Z#ddl$m%Z%m&Z&m'Z'ddl(m)Z)ddlm*Z*ddl+m,Z,ddl-m.Z.ddl/m0Z0ddlm1Z1ddl2m3Z3dZ4dddddddgZ5e0e.dd dd�d!d"d#�e0e.dd dd�d$d"d%�e j6Z7e j8Z9d&Z:d'd(�Z;eej<�Gd)d�dee j=e'j>��Z=eej?�Gd*d�de@��ZAeejB�Gd+d,�d,e@��ZCGd-d.�d.�ZDGd/d�de'j>�ZEed0e)jFf�ZFeejG�Gd1d�de jH��ZIdS)2a�
This is a web server which integrates with the twisted.internet infrastructure.

@var NOT_DONE_YET: A token value which L{twisted.web.resource.IResource.render}
    implementations can return to indicate that the application will later call
    C{.write} and C{.finish} to complete the request, and that the HTTP
    connection should be left open.
@type NOT_DONE_YET: Opaque; do not depend on any particular type for this
    value.
�)�division�absolute_importN��quotecOst|�d�f|�|��d�S)N�charmap)�_quote�decode�encode)�string�args�kwargs�r
�4/usr/lib/python3/dist-packages/twisted/web/server.pyrs����r)�hexlify)�implementer)�
networkString�nativeString�
intToBytes)�Copyable�	ViewPoint)�address�
interfaces)�
AlreadyCalled�AlreadyCancelled)�iweb�http�util)�unquote)�reflect�failure�
components)�	copyright)�resource)�UnsupportedMethod)�Version)�deprecatedModuleAttribute)�escape)�Logger��supportedMethods�Request�Session�Site�version�NOT_DONE_YET�GzipEncoderFactoryZTwisted�z4Please use twisted.web.http.datetimeToString insteadztwisted.web.server�date_time_stringz4Please use twisted.web.http.stringToDatetime instead�string_date_time)�GET�HEADsPOSTcCs<t|tj�rd|j|jfSt|tj�r0d|jfSt|�SdS)NZINETZUNIX)�
isinstancerZIPv4Address�host�portZUNIXAddress�name�tuple)�addrr
r
r�_addressToTupleRs

r;c@seZdZdZdZdZdZdZdZdZ	e
�Zdd�Zdd	�Z
d
d�Zdd
�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd:d$d%�Zd&d'�Zd(d)�ZdZdZe d*d+��Z!d;d,d-�Z"d.d/�Z#d0d1�Z$d2d3�Z%d4d5�Z&d6d7�Z'd8d9�Z(dS)<r*a�
    An HTTP request.

    @ivar defaultContentType: A C{bytes} giving the default I{Content-Type}
        value to send in responses if no other value is set.  L{None} disables
        the default.

    @ivar _insecureSession: The L{Session} object representing state that will
        be transmitted over plain-text HTTP.

    @ivar _secureSession: The L{Session} object representing the state that
        will be transmitted only over HTTPS.
    �	text/htmlNzunusednames=issuerFcOs$tjj|f|�|�tj�|�dS�N)rr*�__init__r �
Componentized)�selfr�kwr
r
rr>uszRequest.__init__cCs�|j��}|d=|d=|d=|d=|j�dd�|j��|d<t||�|d<t|d�|d<t|d	�|d	<t|d
���|d
<|S)NZ	transport�channel�content�siterZcontent_data�remoter6Zclient�requestHeaders)	�__dict__�copyrC�seek�readrr;�listZgetAllRawHeaders)r@�issuer�xr
r
r�getStateToCopyForzs
zRequest.getStateToCopyForcCs |jrt|j�d|S|SdS)zT
        Return the text that links to a sibling of the requested resource.
        �../N)�postpath�len)r@r8r
r
r�sibLink�szRequest.sibLinkcCs\t|j�}|dkr"|dd|S|dkr.|St|j�rT|jdrT|jdd|S|SdS)zR
        Return the text that links to a child of the requested resource.
        r(rO����/N)rQrP�prepath)r@r8Zlppr
r
r�	childLink�s
zRequest.childLinkcCs�|jj|_|�dt�|�dt���g|_ttt	|j
dd��d���|_|j
dkrb|�
�dSz>|j�|�}tj�|�r�|�|�}|dk	r�||_|�|�Wn|�t���YnXdS)z$
        Process a request.
        sserversdater(NrT�*)rBrD�	setHeaderr-r�datetimeToStringrUrK�mapr�path�splitrP�_handleStar�getResourceForr"Z_IEncodingResourceZ
providedByZ
getEncoder�_encoder�render�processingFailedrZFailure)r@�resrc�encoderr
r
r�process�s"
 

zRequest.processcCs�|jsl|jtjtjfk}|j�d�}|j�d�}|o>|ddk}|rl|dkrl|jdk	rl|sl|j�d|jg�|j	s�|j
r�|j
�|�}tj�
||�dS)z�
        Write data to the transport (if not responding to a HEAD request).

        @param data: A string to write to the response.
        �content-type�content-lengthr�0N)�startedWriting�coderZNOT_MODIFIEDZ
NO_CONTENT�responseHeaders�
getRawHeaders�defaultContentType�
setRawHeaders�_inFakeHeadr_r	r*�write)r@�dataZneedsCTZcontentTypeZ
contentLengthZcontentLengthZeror
r
rro�s(����z
Request.writecCs.|jr"|j��}|r"tj�||�tj�|�S)zH
        Override C{http.Request.finish} for possible encoding.
        )r_�finishrr*ro�r@rpr
r
rrq�s

zRequest.finishcCs\z|�|�}W�ndtk
�rt}�zB|j}|jdkr�d|kr�|jjd|d�d|_d|_|�|�}|tkr~|jjd|d�n|�dt	t
|���d|_d|_|�d	�|��WY��d
S|jt
k�r8|�dd�|��d
tt|j��t|j�t
|�dko�d�pdd�dd�|D��d�}t�tjd|�}|�|�}n,t�tjddt|j�d��f�}|�|�}W5d
}~XYnX|tk�r�d
St|t��s�t�tjddt�t�|��ddt�t�|��ddt�t�|����|�}|jdk�r2t
|�dk�r&|jjd||d �|�dt	t
|���|�d	�n|�dt	t
|���|�|�|��d
S)!zn
        Ask a resource to render itself.

        @param resrc: a L{twisted.web.resource.IResource}.
        r4r3z,Using GET to fake a HEAD request for {resrc})rbTzBTried to fake a HEAD request for {resrc}, but it got away from me.rfF�N�Allows, zzYour browser approached me (at %(URI)s) with the method "%(method)s".  I only allow the method%(plural)s %(allowed)s here.r(�s�z, cSsg|]}t|��qSr
)r)�.0rMr
r
r�
<listcomp>-sz"Request.render.<locals>.<listcomp>)ZURI�methodZpluralZallowedzMethod Not AllowedzHuh?z'I don't know how to treat a %s request.rzRequest did not return bytesz	Request: z<br />z
Resource: zValue: rzbWarning: HEAD request {slf} for resource {resrc} is returning a message body. I think I'll eat it.)Zslfrb)r`r#�allowedMethodsry�_log�infornr.rXrrQrorqr)�joinr&rZurir"Z	ErrorPager�NOT_ALLOWEDZNOT_IMPLEMENTEDrr5�bytes�INTERNAL_SERVER_ERRORrZ_PRErZ	safe_repr)r@rb�body�erzruZepager
r
rr`�s��
�

���	���
��������
�
�
zRequest.rendercCsr|jjd|d�|jjr,dt�|�d}nd}|�tj�|�	dd�|�	dt
t|���|�|�|�
�|S)	aO
        Finish this request with an indication that processing failed and
        possibly display a traceback.

        @param reason: Reason this request has failed.
        @type reason: L{twisted.python.failure.Failure}

        @return: The reason passed to this method.
        @rtype: L{twisted.python.failure.Failure}
        rv)rs�<html><head><title>web.Server Traceback (most recent call last)</title></head><body><b>web.Server Traceback (most recent call last):</b>

s

</body></html>
s_<html><head><title>Processing Failed</title></head><body><b>Processing Failed</b></body></html>rer<rf)r{rrD�displayTracebacksrZ
formatFailure�setResponseCoderr�rXrrQrorq)r@�reasonr�r
r
rraWs��
zRequest.processingFailedcCs|�|�dS)z1Remote version of write; same interface.
        N)ro)r@rLrpr
r
r�
view_writewszRequest.view_writecCs|��dS)z2Remote version of finish; same interface.
        N)rq�r@rLr
r
r�view_finish}szRequest.view_finishcKs|j||f|�dS)z5Remote version of addCookie; same interface.
        N)�	addCookie)r@rL�k�vrr
r
r�view_addCookie�szRequest.view_addCookiecCs|�||�dS)z5Remote version of setHeader; same interface.
        N)rX)r@rLr�r�r
r
r�view_setHeader�szRequest.view_setHeadercCs|�|�dS)z;Remote version of setLastModified; same interface.
        N)ZsetLastModified)r@rLZwhenr
r
r�view_setLastModified�szRequest.view_setLastModifiedcCs|�|�dS)z3Remote version of setETag; same interface.
        N)ZsetETag)r@rL�tagr
r
r�view_setETag�szRequest.view_setETagcCs|�||�dS)zD
        Remote version of setResponseCode; same interface.
        N)r�)r@rLri�messager
r
r�view_setResponseCode�szRequest.view_setResponseCodecCs|�t|�|�dS)zbRemote version of registerProducer; same interface.
        (requires a remote producer.)
        N)ZregisterProducer�_RemoteProducerWrapper)r@rLZproducerZ	streamingr
r
r�view_registerProducer�szRequest.view_registerProducercCs|��dSr=)ZunregisterProducerr�r
r
r�view_unregisterProducer�szRequest.view_unregisterProducercCs|��r|jS|jSdS)a�
        If a session has already been created or looked up with
        L{Request.getSession}, this will return that object.  (This will always
        be the session that matches the security of the request; so if
        C{forceNotSecure} is used on a secure request, this will not return
        that session.)

        @return: the session attribute
        @rtype: L{Session} or L{None}
        N)�isSecure�_secureSession�_insecureSession�r@r
r
r�session�szRequest.sessionc		Cs�|��o|}|sd}d}nd}d}t||�}|dk	r`z|��Wnttfk
r^d}YnX|dkr�d�|g|j�}|�|�}|r�z|j�	|�}Wnt
k
r�YnX|s�|j��}|j||j
d|d�t|||�|r�|�|�S|S)	a
        Check if there is a session cookie, and if not, create it.

        By default, the cookie with be secure for HTTPS requests and not secure
        for HTTP requests.  If for some reason you need access to the insecure
        cookie from a secure request you can set C{forceNotSecure = True}.

        @param forceNotSecure: Should we retrieve a session that will be
            transmitted over HTTP, even if this L{Request} was delivered over
            HTTPS?
        @type forceNotSecure: L{bool}
        sTWISTED_SESSIONr�sTWISTED_SECURE_SESSIONr�N�_rT)r[�secure)r��getattr�touchrrr}�sitepathZ	getCookierD�
getSession�KeyError�makeSessionr��uid�setattrZgetComponent)	r@ZsessionInterfaceZforceNotSecurer�ZcookieStringZsessionAttributer�Z
cookienameZ
sessionCookier
r
rr��s:



�
zRequest.getSessioncCst|��j}|��rd}nd}||kr*d}nd|}td|��rBdpDdt|���|f�}d�dd	�|D��}||S)
Ni��Prvz:%dzhttp%s://%s%s/rurTcSsg|]}t|dd��qS)rs)Zsafer)rwZsegmentr
r
rrx
sz'Request._prePathURL.<locals>.<listcomp>)ZgetHostr7r�rrZgetRequestHostnamer})r@rUr7�defaultZhostport�prefixr[r
r
r�_prePathURL�s

�zRequest._prePathURLcCs|�|j�Sr=)r�rUr�r
r
r�
prePathURLszRequest.prePathURLcCsddlm}|j�|�S)Nr)�urlpath)�twisted.pythonr��URLPathZfromRequest)r@r�r
r
rr�szRequest.URLPathcCs|�|jdd��}||_dS)z_
        Remember the currently-processed part of the URL for later
        recalling.
        NrS)r�rU�
appRootURL)r@Zurlr
r
r�rememberRootURLszRequest.rememberRootURLcCs|jS)z2
        Get a previously-remembered URL.
        )r�r�r
r
r�
getRootURL szRequest.getRootURLcCsH|jdkr|�tj�n|�tj�|�dd�|�dd�|��dS)a�
        Handle receiving a request whose path is '*'.

        RFC 7231 defines an OPTIONS * request as being something that a client
        can send as a low-effort way to probe server capabilities or readiness.
        Rather than bother the user with this, we simply fast-path it back to
        an empty 200 OK. Any non-OPTIONS verb gets a 405 Method Not Allowed
        telling the client they can only use OPTIONS.
        sOPTIONSrtsContent-LengthrgN)ryr�rZOKr~rXrqr�r
r
rr]'s

zRequest._handleStar)N)NF))�__name__�
__module__�__qualname__�__doc__rlrDr�Z
__pychecker__rnr_r'r{r>rNrRrVrdrorqr`rar�r�r�r�r�r�r�r�r�r�r��propertyr�r�r�r�r�r�r�r]r
r
r
rr*\sJ
 %[ 


9	c@s&eZdZdZe�d�ZdZdd�ZdS)r/z~
    @cvar compressLevel: The compression level used by the compressor, default
        to 9 (highest).

    @since: 12.3
    s(:?^|[\s,])gzip(:?$|[\s,])�	cCsfd�|j�dg��}|j�|�rb|j�d�}|rBd�|dg�}nd}|j�d|g�t|j|�SdS)zo
        Check the headers if the client accepts gzip encoding, and encodes the
        request if so.
        �,saccept-encodingscontent-encodingsgzipN)	r}rFrk�_gzipCheckRegex�searchrjrm�_GzipEncoder�
compressLevel)r@�requestZ
acceptHeaders�encodingr
r
r�encoderForRequestHs���z$GzipEncoderFactory.encoderForRequestN)	r�r�r�r��re�compiler�r�r�r
r
r
rr/=s
c@s,eZdZdZdZdd�Zdd�Zdd�ZdS)	r�z�
    An encoder which supports gzip.

    @ivar _zlibCompressor: The zlib compressor instance used to compress the
        stream.

    @ivar _request: A reference to the originating request.

    @since: 12.3
    NcCs"t�|tjdtj�|_||_dS)N�)�zlibZcompressobjZDEFLATEDZ	MAX_WBITS�_zlibCompressor�_request)r@r�r�r
r
rr>ls�z_GzipEncoder.__init__cCs"|jjs|jj�d�|j�|�S)zR
        Write to the request, automatically compressing data on the fly.
        rf)r�rhrjZremoveHeaderr��compressrrr
r
rr	rsz_GzipEncoder.encodecCs|j��}d|_|S)zf
        Finish handling the request request, flushing any data from the zlib
        buffer.
        N)r��flush)r@Zremainr
r
rrq}s
z_GzipEncoder.finish)r�r�r�r�r�r>r	rqr
r
r
rr�]s
r�c@seZdZdd�ZdS)r�cCs(|�d�|_|�d�|_|�d�|_dS)N�resumeProducing�pauseProducing�
stopProducing)ZremoteMethodr�r�r�)r@rEr
r
rr>�sz_RemoteProducerWrapper.__init__N)r�r�r�r>r
r
r
rr��sr�c@sBeZdZdZdZdZddd�Zdd�Zdd	�Zd
d�Z	dd
�Z
dS)r+ap
    A user's session with a system.

    This utility class contains no functionality, but is used to
    represent a session.

    @ivar uid: A unique identifier for the session.
    @type uid: L{bytes}

    @ivar _reactor: An object providing L{IReactorTime} to use for scheduling
        expiration.
    @ivar sessionTimeout: timeout of a session, in seconds.
    i�NcCsJtj�|�|dkr ddlm}||_||_||_g|_|�	�i|_
dS)zI
        Initialize a session with a unique ID for that session.
        Nr)�reactor)r r?r>�twisted.internetr��_reactorrDr��expireCallbacksr�ZsessionNamespaces)r@rDr�r�r
r
rr>�szSession.__init__cCs|j�|j|j�|_dS)zF
        Start expiration tracking.

        @return: L{None}
        N)r�Z	callLater�sessionTimeout�expire�_expireCallr�r
r
r�startCheckingExpiration�s�zSession.startCheckingExpirationcCs|j�|�dS)zJ
        Call this callback when the session expires or logs out.
        N)r��append)r@�callbackr
r
r�notifyOnExpire�szSession.notifyOnExpirecCsH|jj|j=|jD]
}|�qg|_|jrD|j��rD|j��d|_dS)z/
        Expire/logout of the session.
        N)rD�sessionsr�r�r�ZactiveZcancel)r@�cr
r
rr��s

zSession.expirecCs(|j��|_|jdk	r$|j�|j�dS)z.
        Notify session modification.
        N)r�ZsecondsZlastModifiedr��resetr�r�r
r
rr��s
z
Session.touch)N)r�r�r�r�r�r�r>r�r�r�r�r
r
r
rr+�s


z
TwistedWeb/%sc@s�eZdZdZdZeZdZeZ	dZ
ejZ
ddd�Zdd	�Zd
d�Zdd
�Zdd�Zdd�Zdd�ZdZdd�Zdd�Zdd�Zdd�ZdS)r,a#
    A web site: manage log, sessions, and resources.

    @ivar counter: increment value used for generating unique sessions ID.
    @ivar requestFactory: A factory which is called with (channel)
        and creates L{Request} instances. Default to L{Request}.
    @ivar displayTracebacks: if set, Twisted internal errors are displayed on
        rendered pages. Default to C{True}.
    @ivar sessionFactory: factory for sessions objects. Default to L{Session}.
    @ivar sessionCheckTime: Deprecated.  See L{Session.sessionTimeout} instead.
    rTiNcOs2tjj|f|�|�i|_||_|dk	r.||_dS)a�
        @param resource: The root of the resource hierarchy.  All request
            traversal for requests received by this factory will begin at this
            resource.
        @type resource: L{IResource} provider
        @param requestFactory: Overwrite for default requestFactory.
        @type requestFactory: C{callable} or C{class}.

        @see: L{twisted.web.http.HTTPFactory.__init__}
        N)r�HTTPFactoryr>r�r"�requestFactory)r@r"r�rrr
r
rr>�s
z
Site.__init__cCs(ddlm}|�tj�|�tj�|��S)Nr)�logfile)r�r�ZLogFile�osr[�basename�dirname)r@r[r�r
r
r�_openLogFileszSite._openLogFilecCs|j��}i|d<|S)Nr�)rGrH)r@�dr
r
r�__getstate__
s
zSite.__getstate__cCs|jd|_t|�d��S)zP
        (internal) Generate an opaque, unique ID for a user's session.
        r(� )�counterr�_entropyr�r
r
r�_mkuidszSite._mkuidcCs*|��}|�||�}|j|<|��|S)zU
        Generate a new Session instance, and store it for future reference.
        )r��sessionFactoryr�r�)r@r�r�r
r
rr�szSite.makeSessioncCs
|j|S)z�
        Get a previously generated session.

        @param uid: Unique ID of the session.
        @type uid: L{bytes}.

        @raise: L{KeyError} if the session is not found.
        )r�)r@r�r
r
rr�"s	zSite.getSessioncCs tj�||�}|j|_||_|S)z;
        Generate a channel attached to this site.
        )rr��
buildProtocolr�rD)r@r:rBr
r
rr�.szSite.buildProtocolcCs|�|��d�|��dS)z@
        Redirect because a Site is always a directory.
        rTN)Zredirectr�rq�r@r�r
r
rr`9szSite.rendercCs||_|j�||�S)z7
        Emulate a resource's getChild method.
        )rDr"�getChildWithDefault)r@ZpathElr�r
r
rr�AszSite.getChildWithDefaultcCs"||_t�|j�|_t�|j|�S)z�
        Get a resource for a request.

        This iterates through the resource hierarchy, calling
        getChildWithDefault on each resource it finds for a path element,
        stopping when it hits an element where isLeaf is true.
        )rDrHrUr�r"ZgetChildForRequestr�r
r
rr^IszSite.getResourceForcCsdg}tjr|�dd�|S)z2
        Protocols this server can speak.
        shttp/1.1rsh2)rZ
H2_ENABLED�insert)r@Z
baseProtocolsr
r
r�acceptableProtocolsXszSite.acceptableProtocols)N)r�r�r�r�r�r*r�r�r+r�ZsessionCheckTimer��urandomr�r>r�r�r�r�r�r�ZisLeafr`r�r^r�r
r
r
rr,�s&

	)Jr�Z
__future__rrrHr�r�Zurllibr�ImportErrorZurllib.parserr�ZbinasciirZzope.interfacerZtwisted.python.compatrrrZtwisted.spread.pbrrr�rrZtwisted.internet.errorrrZtwisted.webrrrZtwisted.web.httprr�rrr Ztwistedr!r"Ztwisted.web.errorr#Zincrementalr$Ztwisted.python.deprecater%r&Ztwisted.loggerr'r.�__all__rYr1ZstringToDatetimer2r)r;ZIRequestr*r?Z_IRequestEncoderFactory�objectr/Z_IRequestEncoderr�r�r+r-ZIProtocolNegotiationFactoryr�r,r
r
r
r�<module>s����
c*K