Spamworldpro Mini Shell
Spamworldpro


Server : Apache/2.4.52 (Ubuntu)
System : Linux webserver 6.8.0-49-generic #49~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Nov 6 17:42:15 UTC 2 x86_64
User : www-data ( 33)
PHP Version : 8.1.2-1ubuntu2.21
Disable Function : NONE
Directory :  /lib/python3/dist-packages/twisted/web/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //lib/python3/dist-packages/twisted/web/__pycache__/http.cpython-310.pyc
o

5�@g��@s�dZgd�ZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlm
Z
ddlmZmZmZmZddlmZmZmZddlmZmZmZmZddlmZdd	l m!Z!m"Z"m#Z#dd
l$m%Z%ddl&m'Z'ddl(m)Z)dd
l*m+Z+ddl,m-Z-m.Z.ddl/m0Z0ddl1m2Z2m3Z3m4Z4ddl5m6Z6ddl7m8Z8ddl9m:Z:ddl;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgddlhmiZimjZjddlkmlZlmmZmmnZnz
ddlompZpdZqWner�y,dZqYnwdZsdZtdZue`Zvgd�Zwgd�Zxdd �ewD�Zyd!d �exD�Zzd"d#�Z{d$d%�Zdkd&d'�Z|dld(d)�Z}dld*d+�Z~d,d-�Zd.d/�Z�d0d1�Z�d2e�d3e�fd4d5�Z�d2e�d3e�fd6d7�Z�d8e�d3ee�e�ffd9d:�Z�d;d<�Z�Gd=d>�d>e�Z�Gd?d@�d@�Z�GdAdB�dBe-j��Z�dCZ�e��Z�dDdE�Z�e��dF�Z�ee"j�e��GdGdH�dH��Z�GdIdJ�dJe��Z�GdKdL�dLe��Z�GdMdN�dNe��Z�GdOdP�dP�Z�dQZ�dRZ�	GdSdT�dT�Z�ee"j��GdUdV�dV��Z�ee"j�e"j�e"j��GdWdX�dXe-j�e.j���Z�dYdZ�Z�eel�d[d\��Z�ee"j��Gd]d^�d^��Z�Gd_d`�d`e6enda��Z�eel�dbdc��Z�Gddde�dee6e)df��Z�dgdh�Z�Gdidj�dje#j��Z�dS)ma�
HyperText Transfer Protocol implementation.

This is the basic server-side protocol implementation used by the Twisted
Web server.  It can parse HTTP 1.0 requests and supports many HTTP 1.1
features as well.  Additionally, some functionality implemented here is
also useful for HTTP clients (such as the chunked encoding parser).

@var CACHED: A marker value to be returned from cache-related request methods
    to indicate to the caller that a cached response will be usable and no
    response body should be generated.

@var FOUND: An HTTP response code indicating a temporary redirect.

@var NOT_MODIFIED: An HTTP response code indicating that a requested
    pre-condition (for example, the condition represented by an
    I{If-Modified-Since} header is present in the request) has succeeded.  This
    indicates a response body cached by the client can be used.

@var PRECONDITION_FAILED: An HTTP response code indicating that a requested
    pre-condition (for example, the condition represented by an I{If-None-Match}
    header is present in the request) has failed.  This should typically
    indicate that the server has not taken the requested action.

@var maxChunkSizeLineLength: Maximum allowable length of the CRLF-terminated
    line that indicates the size of a chunk and the extensions associated with
    it, as in the HTTP 1.1 chunked I{Transfer-Encoding} (RFC 7230 section 4.1).
    This limits how much data may be buffered when decoding the line.
)=�	SWITCHING�OK�CREATED�ACCEPTED�NON_AUTHORITATIVE_INFORMATION�
NO_CONTENT�
RESET_CONTENT�PARTIAL_CONTENT�MULTI_STATUS�MULTIPLE_CHOICE�MOVED_PERMANENTLY�FOUND�	SEE_OTHER�NOT_MODIFIED�	USE_PROXY�TEMPORARY_REDIRECT�PERMANENT_REDIRECT�BAD_REQUEST�UNAUTHORIZED�PAYMENT_REQUIRED�	FORBIDDEN�	NOT_FOUND�NOT_ALLOWED�NOT_ACCEPTABLE�PROXY_AUTH_REQUIRED�REQUEST_TIMEOUT�CONFLICT�GONE�LENGTH_REQUIRED�PRECONDITION_FAILED�REQUEST_ENTITY_TOO_LARGE�REQUEST_URI_TOO_LONG�UNSUPPORTED_MEDIA_TYPE�REQUESTED_RANGE_NOT_SATISFIABLE�EXPECTATION_FAILED�INTERNAL_SERVER_ERROR�NOT_IMPLEMENTED�BAD_GATEWAY�SERVICE_UNAVAILABLE�GATEWAY_TIMEOUT�HTTP_VERSION_NOT_SUPPORTED�INSUFFICIENT_STORAGE_SPACE�NOT_EXTENDED�	RESPONSES�CACHED�urlparse�parse_qs�datetimeToString�datetimeToLogString�timegm�stringToDatetime�toChunk�	fromChunk�parseContentRange�StringTransport�
HTTPClient�
NO_BODY_CODES�Request�PotentialDataLoss�HTTPChannel�HTTPFactory�N)�BytesIO)�AnyStr�Callable�Optional�Tuple)�ParseResultBytes�unquote_to_bytesr.)�	Attribute�	Interface�implementer�provider)�Version)�address�
interfaces�protocol)�_PullToPush)�Deferred)�	IProtocol)�Logger)�basic�policies)�log)�	_PY37PLUS�nativeString�
networkString)�proxyForInterface)�
deprecated)�Failure),rr&rrrr#rrr(rr)r*r$rrr	r
rrrrr+rr%rrrrrrrrrr r"rr,r
r'rrrr!r)�Headers�_sanitizeLinearWhitespace)�IAccessLogFormatter�INonQueuedRequestFactory�IRequest)�H2ConnectionTF�<zHTTP/1.1z�Magic constant returned by http.Request methods to set cache
validation headers when the request is conditional and the value fails
the condition.)�Mon�Tue�Wed�Thu�Fri�Sat�Sun)
N�Jan�Feb�Mar�Apr�May�Jun�Jul�Aug�Sep�Oct�Nov�DeccC�g|]}|���qS���lower��.0�namervrv�2/usr/lib/python3/dist-packages/twisted/web/http.py�
<listcomp>��r}cCsg|]}|o	|���qSrvrwryrvrvr|r}�scCs8t�|�d��\}}|�d�}dd�|��D�}||fS)N�charmapcSsi|]
\}}||�d��qS)r��encode�rz�x�yrvrvr|�
<dictcomp>��z _parseHeader.<locals>.<dictcomp>)�cgi�parse_header�decoder��items)�line�key�pdictrvrvr|�_parseHeader�s
r�cCstt|t�r	td��t|�\}}}}}}t|t�r1|�d�}|�d�}|�d�}|�d�}|�d�}t||||||�S)a�
    Parse an URL into six components.

    This is similar to C{urlparse.urlparse}, but rejects C{str} input
    and always produces C{bytes} output.

    @type url: C{bytes}

    @raise TypeError: The given url was a C{str} string instead of a
        C{bytes}.

    @return: The scheme, net location, path, params, query string, and fragment
        of the URL - all as C{bytes}.
    @rtype: C{ParseResultBytes}
    zurl must be bytes, not unicode�ascii)�
isinstance�str�	TypeError�	_urlparser�rD)�url�scheme�netloc�path�params�query�fragmentrvrvr|r.�s






r.c	Cs�i}dd�|�d�D�}|D]>}z
|�dd�\}}Wnty&|r$�Yqw|s+|rLt|�dd��}t|�dd��}||vrG||�|�q|g||<q|S)zn
    Like C{cgi.parse_qs}, but with support for parsing byte strings on Python 3.

    @type qs: C{bytes}
    cSs g|]}|�d�D]}|�q	qS)�;)�split)rz�s1�s2rvrvr|r}s zparse_qs.<locals>.<listcomp>�&�=��+� )r��
ValueError�unquote�replace�append)�qs�keep_blank_values�strict_parsing�dr��item�k�vrvrvr|r/
s$�
�r/c	CsR|dkrt��}t�|�\	}}}}}}}}}	tdt||t|||||f�}
|
S)zT
    Convert seconds since epoch to HTTP datetime string.

    @rtype: C{bytes}
    Nz#%s, %02d %3s %4d %02d:%02d:%02d GMT)�time�gmtimerW�weekdayname�	monthname��msSinceEpoch�year�month�day�hh�mm�ss�wdr��z�srvrvr|r0#s��r0c	CsH|dkrt��}t�|�\	}}}}}}}}}	d|t|||||f}
|
S)zQ
    Convert seconds since epoch to log datetime string.

    @rtype: C{str}
    Nz#[%02d/%3s/%4d:%02d:%02d:%02d +0000])r�r�r�r�rvrvr|r13s�r1cCs�d}||kr
td|f��d|krdksJ�J�d||t�||�}td|�D]	}|tj|}q,|dkrCt�|�rC|d}||d}|d|}	|	d|}
|
d|}|S)	z?
    Convert time tuple in GMT to seconds since epoch, GMT
    i�zYears prior to %d not supportedr��im��ra)r��calendar�leapdays�range�mdays�isleap)r�r�r��hour�minute�second�EPOCH�days�i�hours�minutes�secondsrvrvr|r2Gsr2c
Cs~t|���}|ddd���tvr#ztd|�WSty"Ynwt|�}|dks/|dkrF|d��rF|d}|d}|d}|d}nR|dksN|dkr{|d�d	�d
kr{|d�d	�\}}}|d}t	|�}|dkrr|d}n&|d
krz|d}nt|�dkr�|d}|d}|d}|d}ntd|��t	|�}t	t
�|����}t	|�}tt	|�d��\}}}	t	t
||||||	��S)zt
    Convert an HTTP date string (one of three formats) to seconds since epoch.

    @type dateString: C{bytes}
    r>�sSun, ��r�r���-����Ei��dilzUnknown datetime format %r�:)rVr�rx�weekdayname_lowerr3r��len�isdigit�find�int�monthname_lower�index�mapr2)
�
dateString�parts�partlenr�r�r�r�r��min�secrvrvr|r3[sB�
"
�
r3cCstt|�d��d|dfS)z�
    Convert string to a chunk.

    @type data: C{bytes}

    @returns: a tuple of C{bytes} representing the chunked encoding of data
    r��
)rWr���datarvrvr|r4�sr4�b�returncCs |D]	}|dvrdSq|dkS)z�
    Is the string case-insensitively hexidecimal?

    It must be composed of one or more characters in the ranges a-f, A-F
    and 0-9.
    s0123456789abcdefABCDEFF�rv)r��crvrvr|�_ishexdigits�s
�r�cCst|�st|��t|d�S)z�
    Decode a hexadecimal integer.

    Unlike L{int(b, 16)}, this raises L{ValueError} when the integer has
    a prefix like C{b'0x'}, C{b'+'}, or C{b'-'}, which is desirable when
    parsing network protocols.
    �)r�r�r�)r�rvrvr|�_hexint�s
r�r�cCsf|�dd�\}}t|�}|dkrtd|f��|||d�dkr%td��|d|�||dd�fS)aA
    Convert chunk to string.

    Note that this function is not specification compliant: it doesn't handle
    chunk extensions.

    @type data: C{bytes}

    @return: tuple of (result, remaining) - both C{bytes}.

    @raise ValueError: If the given data is not a correctly formatted chunked
        byte string.
    r�r�r>z!Chunk length must be >= 0, not %dr�zchunk must end with CRLFN)r�r�r�)r��prefix�rest�lengthrvrvr|r5�sr5cCsf|����\}}|��dkrtd��|�d�\}}tt|�d��\}}|dkr*d}nt|�}|||fS)z�
    Parse a content-range header into (start, end, realLength).

    realLength might be None if real length is not known ('*').
    �bytesz#a range of type %r is not supported�/r��*N)�stripr�rxr�r�r�)�header�kind�other�startend�
realLength�start�endrvrvr|r6�s
r6c@steZdZdZed�Zed�Zdd�Zdd�Zdd	�Z	d
d�Z
dd
�Zdede
fdd�Zdede
fdd�Zdd�ZdS)�)_IDeprecatedHTTPChannelToRequestInterfacez=
    The interface L{HTTPChannel} expects of L{Request}.
    zLA L{http_headers.Headers} instance giving all received HTTP request headers.zPA L{http_headers.Headers} instance holding all HTTP response headers to be sent.cC�dS)z�
        The underlying connection has been lost.

        @param reason: A failure instance indicating the reason why
            the connection was lost.
        @type reason: L{twisted.python.failure.Failure}
        Nrv)�reasonrvrvr|�connectionLost��z8_IDeprecatedHTTPChannelToRequestInterface.connectionLostcCr�)a 
        Called when L{HTTPChannel} has determined the length, if any,
        of the incoming request's body.

        @param length: The length of the request's body.
        @type length: L{int} if the request declares its body's length
            and L{None} if it does not.
        Nrv�r�rvrvr|�	gotLength�rz3_IDeprecatedHTTPChannelToRequestInterface.gotLengthcCr�)z�
        Deliver a received chunk of body data to the request.  Note
        this does not imply chunked transfer encoding.

        @param data: The received chunk.
        @type data: L{bytes}
        Nrvr�rvrvr|�handleContentChunkrz<_IDeprecatedHTTPChannelToRequestInterface.handleContentChunkcCr�)zF
        Parse the request's cookies out of received headers.
        Nrvrvrvrvr|�parseCookiesrz6_IDeprecatedHTTPChannelToRequestInterface.parseCookiescCr�)a�
        Called when the entire request, including its body, has been
        received.

        @param command: The request's HTTP command.
        @type command: L{bytes}

        @param path: The request's path.  Note: this is actually what
            RFC7320 calls the URI.
        @type path: L{bytes}

        @param version: The request's HTTP version.
        @type version: L{bytes}
        Nrv)�commandr��versionrvrvr|�requestReceivedrz9_IDeprecatedHTTPChannelToRequestInterface.requestReceivedr�r�cCr�)a
        Determines if two requests are the same object.

        @param other: Another object whose identity will be compared
            to this instance's.

        @return: L{True} when the two are the same object and L{False}
            when not.
        Nrv�r�rvrvr|�__eq__ rz0_IDeprecatedHTTPChannelToRequestInterface.__eq__cCr�)a
        Determines if two requests are not the same object.

        @param other: Another object whose identity will be compared
            to this instance's.

        @return: L{True} when the two are not the same object and
            L{False} when they are.
        Nrvr	rvrvr|�__ne__+rz0_IDeprecatedHTTPChannelToRequestInterface.__ne__cCr�)z{
        Generate a hash value for the request.

        @return: The request's hash value.
        @rtype: L{int}
        Nrvrvrvrvr|�__hash__6rz2_IDeprecatedHTTPChannelToRequestInterface.__hash__N)�__name__�
__module__�__qualname__�__doc__rF�requestHeaders�responseHeadersrrrrr�object�boolr
rrrvrvrvr|r��s ��	
	r�c@�(eZdZdZdd�Zdd�Zdd�ZdS)	r7zo
    I am a BytesIO wrapper that conforms for the transport API. I support
    the `writeSequence' method.
    cCst�|_dS�N)r?r���selfrvrvr|�__init__E�zStringTransport.__init__cCs|j�d�|��dS)Nr�)r��write�join)r�seqrvrvr|�
writeSequenceHszStringTransport.writeSequencecCst|jd|�S)Nr�)�getattr�__dict__)r�attrrvrvr|�__getattr__K�zStringTransport.__getattr__N)r
rrrrrr"rvrvrvr|r7?s
r7c@s�eZdZdZdZdZdZdZdd�Zdd�Z	d	d
�Z
dd�Zd
d�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�ZdS)r8a@
    A client for HTTP 1.0.

    Notes:
    You probably want to send a 'Host' header with the name of the site you're
    connecting to, in order to not break name based virtual hosting.

    @ivar length: The length of the request body in bytes.
    @type length: C{int}

    @ivar firstLine: Are we waiting for the first header line?
    @type firstLine: C{bool}

    @ivar __buffer: The buffer that stores the response to the HTTP request.
    @type __buffer: A C{BytesIO} object.

    @ivar _header: Part or all of an HTTP request header.
    @type _header: C{bytes}
    NTr�cCs|j�|d|dg�dS)Nr�s HTTP/1.0
��	transportr)rrr�rvrvr|�sendCommandiszHTTPClient.sendCommandcCs>t|t�stt|��}t|�}t|�}|j�|d|dg�dS)N�: r�)r�r�rWr�r\r%r)rr{�value�santizedName�
santizedValuervrvr|�
sendHeaderls

zHTTPClient.sendHeadercCs|j�d�dS)Nr��r%rrrvrvr|�
endHeaderstr#zHTTPClient.endHeaderscCsB|�dd�\}}|��}|�||�|��dkrt|�|_dSdS)z�
        Given a complete HTTP header, extract the field name and value and
        process the header.

        @param header: a complete HTTP request header of the form
            'field-name: value'.
        @type header: C{bytes}
        �:r��content-lengthN)r��lstrip�handleHeaderrxr�r�)rr�r��valrvrvr|�
extractHeaderws	�zHTTPClient.extractHeadercCs�|jr/d|_|�dd�}|d}|d}z|d}Wnty%d}Ynw|�|||�dS|sJ|jdkr<|�|j�t�|_|��|�	�dS|�
d�sT|�
d�r\|j||_dS|jrj|�|j�||_dS||_dS)	z�
        Parse the status line and headers for an HTTP request.

        @param line: Part of an HTTP request header. Request bodies are parsed
            in L{HTTPClient.rawDataReceived}.
        @type line: C{bytes}
        FNr�r>r�r��	r�)�	firstLiner��
IndexError�handleStatus�_headerr3r?�_HTTPClient__buffer�handleEndHeaders�
setRawMode�
startswith)rr��lr�status�messagervrvr|�lineReceived�s2�


zHTTPClient.lineReceivedcCs|��dSr)�handleResponseEnd)rr�rvrvr|r�rzHTTPClient.connectionLostcCs,|jdur|j��}d|_|�|�dSdS)z~
        The response has been completely received.

        This callback may be invoked more than once per request.
        N)r9�getvalue�handleResponse)rr�rvrvr|rA�s


�zHTTPClient.handleResponseEndcCs|j�|�dSr)r9r�rr�rvrvr|�handleResponsePart�r#zHTTPClient.handleResponsePartcC�dSrrvrrvrvr|�connectionMade�szHTTPClient.connectionMadecCr�)z�
        Called when the status-line is received.

        @param version: e.g. 'HTTP/1.0'
        @param status: e.g. '200'
        @type status: C{bytes}
        @param message: e.g. 'OK'
        Nrv)rrr>r?rvrvr|r7�rzHTTPClient.handleStatuscCr�)z9
        Called every time a header is received.
        Nrv)rr�r2rvrvr|r1�rzHTTPClient.handleHeadercCr�)z=
        Called when all headers have been received.
        Nrvrrvrvr|r:�rzHTTPClient.handleEndHeaderscCsn|jdur|d|j�||jd�}}|jt|�8_nd}|�|�|jdkr5|��|�|�dSdS)Nr�r>)r�r�rErA�setLineMode)rr�r�rvrvr|�rawDataReceived�s


�zHTTPClient.rawDataReceived)r
rrrr�r5r9r8r&r+r-r3r@rrArErGr7r1r:rIrvrvrvr|r8Os&)
r8)��i0cCs|dur|dkrt�St��S)zS
    Get a writeable file-like object to which request content can be written.
    Ni��)r?�tempfile�
TemporaryFilerrvrvr|�_getContentFile�srMs^\[?(?P<host>.*?)\]?(:\d+)?$c@s�eZdZdZdZdZeZeeZ	dZ
dZdZdZ
dZdZdZdZdZdZdZdZdZe�Zefdd	�Zd
d�Zeedd
dd��dd��Zdd�Zdd�Z dd�Z!dd�Z"de#fdd�Z$dd�Z%dd�Z&d d!�Z'd"d#�Z(d$d%�Z)d&d'�Z*d(d)�Z+d*d+�Z,								dYd,d-�Z-dZd.d/�Z.d0d1�Z/d2d3�Z0d4d5�Z1d6d7�Z2d8d9�Z3d:d;�Z4d<d=�Z5d[d>d?�Z6eedd@dAd�dBdC�dDdE��Z7dFdG�Z8dHdI�Z9dJdK�Z:dLdM�Z;dNdO�Z<dPdQ�Z=dRdS�Z>dTe?de@fdUdV�ZAdWdX�ZBdS)\r:a�
    A HTTP request.

    Subclasses should override the process() method to determine how
    the request will be processed.

    @ivar method: The HTTP method that was used, e.g. C{b'GET'}.
    @type method: L{bytes}

    @ivar uri: The full encoded URI which was requested (including query
        arguments), e.g. C{b'/a/b%20/c?q=v'}.
    @type uri: L{bytes}

    @ivar path: The encoded path of the request URI (not including query
        arguments), e.g. C{b'/a/b%20/c'}.
    @type path: L{bytes}

    @ivar args: A mapping of decoded query argument names as L{bytes} to
        corresponding query argument values as L{list}s of L{bytes}.
        For example, for a URI with C{foo=bar&foo=baz&quux=spam}
        as its query part C{args} will be C{{b'foo': [b'bar', b'baz'],
        b'quux': [b'spam']}}.
    @type args: L{dict} of L{bytes} to L{list} of L{bytes}

    @ivar content: A file-like object giving the request body.  This may be
        a file on disk, an L{io.BytesIO}, or some other type.  The
        implementation is free to decide on a per-request basis.
    @type content: L{typing.BinaryIO}

    @ivar cookies: The cookies that will be sent in the response.
    @type cookies: L{list} of L{bytes}

    @type requestHeaders: L{http_headers.Headers}
    @ivar requestHeaders: All received HTTP request headers.

    @type responseHeaders: L{http_headers.Headers}
    @ivar responseHeaders: All HTTP response headers to be sent.

    @ivar notifications: A L{list} of L{Deferred}s which are waiting for
        notification that the response to this request has been finished
        (successfully or with an error).  Don't use this attribute directly,
        instead use the L{Request.notifyFinish} method.

    @ivar _disconnected: A flag which is C{False} until the connection over
        which this request was received is closed and which is C{True} after
        that.
    @type _disconnected: L{bool}

    @ivar _log: A logger instance for request related messages.
    @type _log: L{twisted.logger.Logger}
    Nr>s(no method yet)s(no clientproto yet)s(no uri yet)FcCs`g|_||_|j��|_|j��|_t�|_i|_t�|_	g|_
|jj|_|tur+d}||_
dS)z�
        @param channel: the channel we're connected to.
        @param queued: (deprecated) are we in the request queue, or can we
            start writing to the transport?
        FN)�
notifications�channel�getPeer�client�getHost�hostr[r�received_cookiesr�cookiesr%�_QUEUED_SENTINEL�queued)rrOrWrvrvr|r@s

zRequest.__init__cCs�|jr|j�dttd|j�����|��|j�|�|`|j	dur6z|j	�
�Wn	ty3Ynw|`	|jD]}|�
d�q9g|_dS)zP
        Called when have finished responding and are no longer queued.
        �z"Producer was not unregistered for N)�producer�_log�failurerZ�RuntimeError�uri�unregisterProducerrO�requestDone�content�close�OSErrorrN�callback)rr�rvrvr|�_cleanupZs$�
�

zRequest._cleanup�Twistedr�r�cCr�)a'
        Notify the object that it is no longer queued.

        We start writing whatever data we have to the transport, etc.

        This method is not intended for users.

        In 16.3 this method was changed to become a no-op, as L{Request}
        objects are now never queued.
        Nrvrrvrvr|�noLongerQueuedsszRequest.noLongerQueuedcCst|�|_dS)a0
        Called when HTTP channel got length of content in this request.

        This method is not intended for users.

        @param length: The length of the request body, as indicated by the
            request headers.  L{None} if the request headers do not indicate a
            length.
        N)rMr`)rr�rvrvr|r�s
zRequest.gotLengthc
Csr|j�d�}|durdS|D](}|r6|�d�D]}|��}z|�dd�\}}||j|<Wqty5YqwqdS)zW
        Parse cookie headers.

        This method is not intended for users.
        scookieNr�r�r�)r�
getRawHeadersr�r0rTr�)r�
cookieheaders�	cookietxt�cookr�r�rvrvr|r�s���zRequest.parseCookiescC�|j�|�dS)zX
        Write a chunk of data.

        This method is not intended for users.
        N)r`rrDrvrvr|r�szRequest.handleContentChunkc
Cs�|j��}|j�dd�i|_|||_|_||_|j�dd�}t|�dkr+|j|_	n|\|_	}t
|d�|_|j}|j�d�}|durG|d}|jdkr�|r�|r�d}	t
|�\}
}||d<|
d	krl|�t
|j��d��nY|
|	kr�z3tr~tj|j|d
dd�}nt�|j|�}tr�|j�d
d�|��D��n
|j�dd�|��D��Wn!ty�}
z|j��t|
tttf�r�WYd}
~
dS�d}
~
ww|j�dd�|��dS)a�
        Called by channel when all data has been received.

        This method is not intended for users.

        @type command: C{bytes}
        @param command: The HTTP verb of this request.  This has the case
            supplied by the client (eg, it maybe "get" rather than "GET").

        @type path: C{bytes}
        @param path: The URI of this request.

        @type version: C{bytes}
        @param version: The HTTP version of this request.
        r>�?r�scontent-typeNsPOSTsmultipart/form-datazCONTENT-LENGTHs!application/x-www-form-urlencoded�utf8�surrogateescape)�encoding�errorscSs0i|]\}}t|t�r|�d�dd�|D��qS)�
iso-8859-1cSs&g|]}t|t�r|�dd�n|�qS)rmrn�r�r�r�)rzr�rvrvr|r}�s���z6Request.requestReceived.<locals>.<dictcomp>.<listcomp>rrr�rvrvr|r��s���z+Request.requestReceived.<locals>.<dictcomp>cSsi|]
\}}|�d�|�qS)rqr�r�rvrvr|r��r�)r`�tell�seek�args�methodr]�clientprotor�r�r�r/rrgr��update�readrUr��parse_multipartr��	ExceptionrO�!_respondToBadRequestAndDisconnectr�r�r��KeyError�process)rrr�r�clengthr��	argstringru�ctype�mfdr�r��cgiArgs�ervrvr|r�sb


�����
��	zRequest.requestReceivedr�cCs,d�|jjt|�t|j�t|j�t|j��S)z�
        Return a string description of the request including such information
        as the request method and request URI.

        @return: A string loosely describing this L{Request} object.
        @rtype: L{str}
        z.<{} at 0x{:x} method={} uri={} clientproto={}>)�format�	__class__r
�idrVrvr]rwrrvrvr|�__repr__
s�zRequest.__repr__cCr�)zY
        Override in subclasses.

        This method is not intended for users.
        Nrvrrvrvr|r~szRequest.processcCs6|jrtd||jf��||_||_|j�||�dS)z&
        Register a producer.
        zAregistering producer %s before previous one (%s) was unregisteredN)rYr��streamingProducerrO�registerProducer�rrY�	streamingrvrvr|r�$s��zRequest.registerProducercCs|j��d|_dS)z*
        Unregister the producer.
        N)rOr^rYrrvrvr|r^2s

zRequest.unregisterProducercCs |j�|�}|dur|dSdS)a�
        Get an HTTP request header.

        @type key: C{bytes} or C{str}
        @param key: The name of the header to get the value of.

        @rtype: C{bytes} or C{str} or L{None}
        @return: The value of the specified header, or L{None} if that header
            was not present in the request. The string type of the result
            matches the type of C{key}.
        Nr�)rrg)rr�r(rvrvr|�	getHeader;s�zRequest.getHeadercCs|j�|�S)a.
        Get a cookie that was sent from the network.

        @type key: C{bytes}
        @param key: The name of the cookie to get.

        @rtype: C{bytes} or C{None}
        @returns: The value of the specified cookie, or L{None} if that cookie
            was not present in the request.
        )rT�get)rr�rvrvr|�	getCookieKszRequest.getCookiecCs|j�t��|jdS)a�
        Notify when the response to this request has finished.

        @note: There are some caveats around the reliability of the delivery of
            this notification.

                1. If this L{Request}'s channel is paused, the notification
                   will not be delivered.  This can happen in one of two ways;
                   either you can call C{request.transport.pauseProducing}
                   yourself, or,

                2. In order to deliver this notification promptly when a client
                   disconnects, the reactor must continue reading from the
                   transport, so that it can tell when the underlying network
                   connection has gone away.  Twisted Web will only keep
                   reading up until a finite (small) maximum buffer size before
                   it gives up and pauses the transport itself.  If this
                   occurs, you will not discover that the connection has gone
                   away until a timeout fires or until the application attempts
                   to send some data via L{Request.write}.

                3. It is theoretically impossible to distinguish between
                   successfully I{sending} a response and the peer successfully
                   I{receiving} it.  There are several networking edge cases
                   where the L{Deferred}s returned by C{notifyFinish} will
                   indicate success, but the data will never be received.
                   There are also edge cases where the connection will appear
                   to fail, but in reality the response was delivered.  As a
                   result, the information provided by the result of the
                   L{Deferred}s returned by this method should be treated as a
                   guess; do not make critical decisions in your applications
                   based upon it.

        @rtype: L{Deferred}
        @return: A L{Deferred} which will be triggered when the request is
            finished -- with a L{None} value if the request finishes
            successfully or with an error if the request is interrupted by an
            error (for example, the client closing the connection prematurely).
        r�)rNr�rOrrvrvr|�notifyFinishXs(
zRequest.notifyFinishcCs�|jrtd��|jrtjddd�dS|js|�d�|jr$|j�d�t	|jd�r7|jj
dur7|jj
�|�d	|_|jsC|�
�dSdS)
zV
        Indicate that all response data has been written to this L{Request}.
        zqRequest.finish called on a request after its connection was lost; use Request.notifyFinish to keep track of this.z%Warning! request.finish called twice.r�)�
stacklevelNr�s0

�factoryr�)�
_disconnectedr\�finished�warnings�warn�startedWritingr�chunkedrO�hasattrr�rTrWrdrrvrvr|�finish�s"�
�zRequest.finishc
Cs�|jrtd��|jrdS|js�d|_|j}d|jf}|j}g}|dkr>|j�d�dur>|j	dkr>|jt
vr>|�d�d|_|j
dur[|j�d	�rP|j�d
�n|j�d	t|j
�g�|jduri|j�d|jg�|j��D]\}}|D]	}|�||f�qtqn|jD]	}	|�d|	f�q�|j�||||�|j	dkr�d
d�|_dS|jt
vr�dd�|_dS|jt|�|_|r�|jr�|j�t|��dS|j�|�dSdS)z�
        Write some data as a result of an HTTP request.  The first
        time this is called, it writes out response data.

        @type data: C{bytes}
        @param data: Some bytes to be sent as part of the response body.
        zBRequest.write called on a request after Request.finish was called.Nr�s%d�HTTP/1.1r/�HEAD)sTransfer-Encoding�chunkeds
last-modifiedzPWarning: last-modified specified both in header list and lastModified attribute.sETags
Set-CookiecSrFrrvr�rvrvr|�<lambda>�rzRequest.write.<locals>.<lambda>cSrFrrvr�rvrvr|r��r)r�r\r�r�rw�code�code_messagerrgrvr9r�r��lastModified�	hasHeaderrZ�info�
setRawHeadersr0�etag�getAllRawHeadersrUrO�writeHeadersr�
sentLengthr�rr4)
rr�rr�r��headersr{�valuesr(�cookiervrvr|r�s^�



��
�




�z
Request.writecCsdd�}dd�}|||��d|||��}
|dur$|
d|||��}
|dur2|
d|||��}
|dur@|
d	|||��}
|durN|
d
|||��}
|dur\|
d|||��}
|rb|
d}
|	rh|
d
}
|
r�||
���}
|
dvr|tdt|
���|
d|
7}
|j�|
�dS)a	
        Set an outgoing HTTP cookie.

        In general, you should consider using sessions instead of cookies, see
        L{twisted.web.server.Request.getSession} and the
        L{twisted.web.server.Session} class for details.

        @param k: cookie name
        @type k: L{bytes} or L{str}

        @param v: cookie value
        @type v: L{bytes} or L{str}

        @param expires: cookie expire attribute value in
            "Wdy, DD Mon YYYY HH:MM:SS GMT" format
        @type expires: L{bytes} or L{str}

        @param domain: cookie domain
        @type domain: L{bytes} or L{str}

        @param path: cookie path
        @type path: L{bytes} or L{str}

        @param max_age: cookie expiration in seconds from reception
        @type max_age: L{bytes} or L{str}

        @param comment: cookie comment
        @type comment: L{bytes} or L{str}

        @param secure: direct browser to send the cookie on encrypted
            connections only
        @type secure: L{bool}

        @param httpOnly: direct browser not to expose cookies through channels
            other than HTTP (and HTTPS) requests
        @type httpOnly: L{bool}

        @param sameSite: One of L{None} (default), C{'lax'} or C{'strict'}.
            Direct browsers not to send this cookie on cross-origin requests.
            Please see:
            U{https://tools.ietf.org/html/draft-west-first-party-cookies-07}
        @type sameSite: L{None}, L{bytes} or L{str}

        @raise ValueError: If the value for C{sameSite} is not supported.
        cSs$|dur|St|t�r
|S|�d�S)z�
            Ensure that C{val} is bytes, encoding using UTF-8 if
            needed.

            @param val: L{bytes} or L{str}

            @return: L{bytes}
            Nrm)r�r�r��r2rvrvr|�_ensureBytes)s
	

z'Request.addCookie.<locals>._ensureBytescSst|��dd�S)z�
            Replace linear whitespace (C{\r}, C{\n}, C{\r\n}) and
            semicolons C{;} in C{val} with a single space.

            @param val: L{bytes}
            @return: L{bytes}
            r�r�)r\r�r�rvrvr|�	_sanitize;sz$Request.addCookie.<locals>._sanitizer�Ns
; Expires=s	; Domain=s; Path=s
; Max-Age=s
; Comment=s; Secures
; HttpOnly)slaxsstrictzInvalid value for sameSite: s; SameSite=)rxr��reprrUr�)rr�r��expires�domainr��max_age�comment�secure�httpOnly�sameSiter�r�r�rvrvr|�	addCookie�s.;
zRequest.addCookiecCsJt|t�s	td��||_|rt|t�std��||_dSt�|d�|_dS)zi
        Set the HTTP response code.

        @type code: L{int}
        @type message: L{bytes}
        z&HTTP response code must be int or longz*HTTP response status message must be bytessUnknown StatusN)r�r�r�r�r�r�r,r�)rr�r?rvrvr|�setResponseCode[s


zRequest.setResponseCodecCs|j�||g�dS)a�
        Set an HTTP response header.  Overrides any previously set values for
        this header.

        @type name: L{bytes} or L{str}
        @param name: The name of the header for which to set the value.

        @type value: L{bytes} or L{str}
        @param value: The value to set for the named header. A L{str} will be
            UTF-8 encoded, which may not interoperable with other
            implementations. Avoid passing non-ASCII characters if possible.
        N)rr�)rr{r(rvrvr|�	setHeaderls
zRequest.setHeadercCs|�t�|�d|�dS)a-
        Utility function that does a redirect.

        Set the response code to L{FOUND} and the I{Location} header to the
        given URL.

        The request should have C{finish()} called after this.

        @param url: I{Location} header value.
        @type url: L{bytes} or L{str}
        slocationN)r�rr�)rr�rvrvr|�redirect{s
zRequest.redirectcCs�tt�|��}|jr|j|kr||_|�d�}|r>|�dd�d}zt|�}Wn
ty1YdSw||jkr>|�t	�t
SdS)a
        Set the C{Last-Modified} time for the response to this request.

        If I am called more than once, I ignore attempts to set
        Last-Modified earlier, only replacing the Last-Modified time
        if it is to a later value.

        If I am a conditional request, I may modify my response code
        to L{NOT_MODIFIED} if appropriate for the time given.

        @param when: The last time the resource being returned was
            modified, in seconds since the epoch.
        @type when: number
        @return: If I am a I{If-Modified-Since} conditional request and
            the time given is not newer than the condition, I return
            L{http.CACHED<CACHED>} to indicate that you should write no
            body.  Otherwise, I return a false value.
        sif-modified-sincer�r�r>N)r��math�ceilr�r�r�r3r�r�rr-)r�when�
modifiedSince�	firstPartrvrvr|�setLastModified�s
�

zRequest.setLastModifiedcCsP|r||_|�d�}|r&|��}||vsd|vr&|�|jdvr!tp"t�tSdS)a�
        Set an C{entity tag} for the outgoing response.

        That's "entity tag" as in the HTTP/1.1 C{ETag} header, "used
        for comparing two or more entities from the same requested
        resource."

        If I am a conditional request, I may modify my response code
        to L{NOT_MODIFIED} or L{PRECONDITION_FAILED}, if appropriate
        for the tag given.

        @param etag: The entity tag for the resource being returned.
        @type etag: string
        @return: If I am a C{If-None-Match} conditional request and
            the tag matches one in the request, I return
            L{http.CACHED<CACHED>} to indicate that you should write
            no body.  Otherwise, I return a false value.
        s
if-none-match�*)r�sGETN)r�r�r�r�rvrrr-)rr��tagsrvrvr|�setETag�s
�zRequest.setETagcCs,i}|j��D]\}}|d||��<q|S)a
        Return dictionary mapping the names of all received headers to the last
        value received for each.

        Since this method does not return all header information,
        C{self.requestHeaders.getAllRawHeaders()} may be preferred.
        r�)rr�rx)rr�r�r�rvrvr|�
getAllHeaders�szRequest.getAllHeaderscCs<|�d�}|durt�|�}|dur|�d�St|��j�S)z�
        Get the hostname that the HTTP client passed in to the request.

        @see: L{IRequest.getRequestHostname}

        @returns: the requested hostname

        @rtype: C{bytes}
        �hostNrS)r��_hostHeaderExpression�match�grouprWrRrS)rrSr�rvrvr|�getRequestHostname�s



zRequest.getRequestHostnamecC�|jS)a
        Get my originally requesting transport's host.

        Don't rely on the 'transport' attribute, since Request objects may be
        copied remotely.  For information on this method's return value, see
        L{twisted.internet.tcp.Port}.
        �rSrrvrvr|rR�szRequest.getHostcCsV||_|��r
d}nd}||kr|}nd||f}|j�d|g�t�d||�|_dS)a�
        Change the host and port the request thinks it's using.

        This method is useful for working with reverse HTTP proxies (e.g.
        both Squid and Apache's mod_proxy can do this), when the address
        the HTTP client is using is different than the one we're listening on.

        For example, Apache may be listening on https://www.example.com/, and
        then forwarding requests to http://localhost:8080/, but we don't want
        HTML produced by Twisted to say b'http://localhost:8080/', they should
        say b'https://www.example.com/', so we do::

           request.setHost(b'www.example.com', 443, ssl=1)

        @type host: C{bytes}
        @param host: The value to which to change the host header.

        @type ssl: C{bool}
        @param ssl: A flag which, if C{True}, indicates that the request is
            considered secure (if C{True}, L{isSecure} will return C{True}).
        i��Ps%b:%dr��TCPN)�	_forceSSL�isSecurerr�rK�IPv4AddressrS)rrS�port�ssl�default�
hostHeaderrvrvr|�setHost�szRequest.setHost�r��getClientAddress)�replacementcCs t|jtjtjf�r|jjSdS)z�
        Return the IP address of the client who submitted this request.

        This method is B{deprecated}.  Use L{getClientAddress} instead.

        @returns: the client IP address
        @rtype: C{str}
        N)r�rQrKr��IPv6AddressrSrrvrvr|�getClientIPs
zRequest.getClientIPcCr�)a~
        Return the address of the client who submitted this request.

        This may not be a network address (e.g., a server listening on
        a UNIX domain socket will cause this to return
        L{UNIXAddress}).  Callers must check the type of the returned
        address.

        @since: 18.4

        @return: the client's address.
        @rtype: L{IAddress}
        )rQrrvrvr|r�)szRequest.getClientAddresscCs*|jrdSt|dd�}|durdS|��S)a�
        Return L{True} if this request is using a secure transport.

        Normally this method returns L{True} if this request's L{HTTPChannel}
        instance is using a transport that implements
        L{interfaces.ISSLTransport}.

        This will also return L{True} if L{Request.setHost} has been called
        with C{ssl=True}.

        @returns: L{True} if this request is secure
        @rtype: C{bool}
        TrONF)r�rr�)rrOrvrvr|r�9szRequest.isSecurec	Cs�z1|�d�}|sd|_|_WdS|��\}}|��dkr t��t�|�}|�dd�\|_|_WdStj	tfyDd|_|_YdSt
yY|j�d�d|_|_YdSw)Ns
Authorizationr�sbasicr.r�rX)
r��user�passwordr�rxr��base64�	b64decode�binascii�Error�
BaseExceptionrZr[)r�authh�bas�upwrvrvr|�
_authorizeNs 

�zRequest._authorizecC�*z|jWSty
Ynw|��|jS)z�
        Return the HTTP user sent with this request, if any.

        If no user was supplied, return the empty string.

        @returns: the HTTP user, if any
        @rtype: C{bytes}
        )r�r�r�rrvrvr|�getUser`�	�zRequest.getUsercCr�)z�
        Return the HTTP password sent with this request, if any.

        If no password was supplied, return the empty string.

        @returns: the HTTP password, if any
        @rtype: C{bytes}
        )r�r�r�rrvrvr|�getPasswordpr�zRequest.getPasswordcCs@d|_d|_|jdur|j��|jD]}|�|�qg|_dS)z�
        There is no longer a connection for this request to respond over.
        Clean up anything which can't be useful anymore.
        TN)r�rOr`rarN�errback)rr�r�rvrvr|r�s



zRequest.connectionLostcC�|jdur|j��dSdS)zL
        Pass the loseConnection through to the underlying channel.
        N)rO�loseConnectionrrvrvr|r��s
�zRequest.loseConnectionr�cCst|t�r	||uStS)a
        Determines if two requests are the same object.

        @param other: Another object whose identity will be compared
            to this instance's.

        @return: L{True} when the two are the same object and L{False}
            when not.
        @rtype: L{bool}
        )r�r:�NotImplemented)rr�rvrvr|r
�s
zRequest.__eq__cCst|�S)z�
        A C{Request} is hashable so that it can be used as a mapping key.

        @return: A C{int} based on the instance's identity.
        )r�rrvrvr|r��zRequest.__hash__)NNNNNNFNr)r>)Cr
rrrrYr�rr�r,r�rvrwr]r�r�r�r�r�rur�r`r�r�rQrZrVrrdrYrJrfrrrrr�r�r~r�r^r�r�r�r�rr�r�r�r�r�r�r�r�rRr�r�r�r�r�r�r�rr�rrr
rrvrvrvr|r:�s�4

`
	
+R
�
m%!


"

r:c@�eZdZdZdS)�	_DataLossa;
    L{_DataLoss} indicates that not all of a message body was received. This
    is only one of several possible exceptions which may indicate that data
    was lost.  Because of this, it should not be checked for by
    specifically; any unexpected exception should be treated as having
    caused data loss.
    N�r
rrrrvrvrvr|r���r�c@r�)r;a�
    L{PotentialDataLoss} may be raised by a transfer encoding decoder's
    C{noMoreData} method to indicate that it cannot be determined if the
    entire response body has been delivered.  This only occurs when making
    requests to HTTP servers which do not set I{Content-Length} or a
    I{Transfer-Encoding} in the response because in this case the end of the
    response is indicated by the connection being closed, an event which may
    also be due to a transient network problem or other error.
    Nr�rvrvrvr|r;�r�r;c@r�)�_MalformedChunkedDataErrora
    C{_ChunkedTransferDecoder} raises L{_MalformedChunkedDataError} from its
    C{dataReceived} method when it encounters malformed data. This exception
    indicates a client-side error. If this exception is raised, the connection
    should be dropped with a 400 error.
    Nr�rvrvrvr|r��r�r�c@r)	�_IdentityTransferDecoderaZ
    Protocol for accumulating bytes up to a specified length.  This handles the
    case where no I{Transfer-Encoding} is specified.

    @ivar contentLength: Counter keeping track of how many more bytes there are
        to receive.

    @ivar dataCallback: A one-argument callable which will be invoked each
        time application data is received.

    @ivar finishCallback: A one-argument callable which will be invoked when
        the terminal chunk is received.  It will be invoked with all bytes
        which were delivered to this protocol which came after the terminal
        chunk.
    cCs||_||_||_dSr)�
contentLength�dataCallback�finishCallback)rr�r�r�rvrvr|r�s
z!_IdentityTransferDecoder.__init__cCs�|jdur	td��|jdur|�|�dSt|�|jkr,|jt|�8_|�|�dS|j}|j}|j}d|_|_d|_||d|��|||d��dS)a;
        Interpret the next chunk of bytes received.  Either deliver them to the
        data callback or invoke the finish callback if enough bytes have been
        received.

        @raise RuntimeError: If the finish callback has already been invoked
            during a previous call to this methood.
        Nz;_IdentityTransferDecoder cannot decode data after finishingr>)r�r\r�r�r�)rr�r�r�r�rvrvr|�dataReceived�s 
	�
z%_IdentityTransferDecoder.dataReceivedcCs>|j}d|_|_|jdur|d�t��|jdkrt��dS)ap
        All data which will be delivered to this decoder has been.  Check to
        make sure as much data as was expected has been received.

        @raise PotentialDataLoss: If the content length is unknown.
        @raise _DataLoss: If the content length is known and fewer than that
            many bytes have been delivered.

        @return: L{None}
        Nr�r>)r�r�r�r;r�)rr�rvrvr|�
noMoreDatas

�z#_IdentityTransferDecoder.noMoreDataN)r
rrrrr�r�rvrvrvr|r��s
 r�is�	 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~�����������������������������������������������������������������������������������������������������������������������������c@s�eZdZdZdZdeegdfdeegdfddfdd�Zdefd	d
�Z	defdd�Z
defd
d�Zdefdd�Zdefdd�Z
deddfdd�Zddd�ZdS)�_ChunkedTransferDecodera�	
    Protocol for decoding I{chunked} Transfer-Encoding, as defined by RFC 7230,
    section 4.1.  This protocol can interpret the contents of a request or
    response body which uses the I{chunked} Transfer-Encoding.  It cannot
    interpret any of the rest of the HTTP protocol.

    It may make sense for _ChunkedTransferDecoder to be an actual IProtocol
    implementation.  Currently, the only user of this class will only ever
    call dataReceived on it.  However, it might be an improvement if the
    user could connect this to a transport and deliver connection lost
    notification.  This way, `dataCallback` becomes `self.transport.write`
    and perhaps `finishCallback` becomes `self.transport.loseConnection()`
    (although I'm not sure where the extra data goes in that case).  This
    could also allow this object to indicate to the receiver of data that
    the stream was not completely received, an error case which should be
    noticed. -exarkun

    @ivar dataCallback: A one-argument callable which will be invoked each
        time application data is received. This callback is not reentrant.

    @ivar finishCallback: A one-argument callable which will be invoked when
        the terminal chunk is received.  It will be invoked with all bytes
        which were delivered to this protocol which came after the terminal
        chunk.

    @ivar length: Counter keeping track of how many more bytes in a chunk there
        are to receive.

    @ivar state: One of C{'CHUNK_LENGTH'}, C{'CRLF'}, C{'TRAILER'},
        C{'BODY'}, or C{'FINISHED'}.  For C{'CHUNK_LENGTH'}, data for the
        chunk length line is currently being read.  For C{'CRLF'}, the CR LF
        pair which follows each chunk is being read. For C{'TRAILER'}, the CR
        LF pair which follows the terminal 0-length chunk is currently being
        read. For C{'BODY'}, the contents of a chunk are being read. For
        C{'FINISHED'}, the last chunk has been completely read and no more
        input is valid.

    @ivar _buffer: Accumulated received data for the current state. At each
        state transition this is truncated at the front so that index 0 is
        where the next state shall begin.

    @ivar _start: While in the C{'CHUNK_LENGTH'} state, tracks the index into
        the buffer at which search for CRLF should resume. Resuming the search
        at this position avoids doing quadratic work if the chunk length line
        arrives over many calls to C{dataReceived}.

        Not used in any other state.
    �CHUNK_LENGTHr�Nr�r�cCs||_||_t�|_d|_dS)Nr>)r�r��	bytearray�_buffer�_start)rr�r�rvrvr|rzs
z _ChunkedTransferDecoder.__init__cCs|j�d|j�}|tks|dkrt|j�tkrtd�t���|dkr,t|j�d|_dS|j�dd|�}|dkr:|}|jd|�}zt|�}WntyRtd��w|j|d|�}|rn|�	d	t
�d
krntd|�d���|dkrvd
|_nd|_||_|jd|d�=d|_dS)ak
        Read the chunk size line, ignoring any extensions.

        @returns: C{True} once the line has been read and removed from
            C{self._buffer}.  C{False} when more data is required.

        @raises _MalformedChunkedDataError: when the chunk size cannot be
            decoded or the length of the line exceeds L{maxChunkSizeLineLength}.
        r�r�z,Chunk size line exceeds maximum of {} bytes.r�Fr�r>zChunk-size must be an integer.Nr�z(Invalid characters in chunk extensions: �.�TRAILER�BODYr�T)
r�r�r��maxChunkSizeLineLengthr�r�r�r�r��	translate�_chunkExtChars�stater�)r�eolIndex�endOfLengthIndex�	rawLengthr��extrvrvr|�_dataReceived_CHUNK_LENGTH�s@
���
�z2_ChunkedTransferDecoder._dataReceived_CHUNK_LENGTHcCs<t|j�dkr	dS|j�d�std��d|_|jdd�=dS)a;
        Await the carriage return and line feed characters that are the end of
        chunk marker that follow the chunk data.

        @returns: C{True} when the CRLF have been read, otherwise C{False}.

        @raises _MalformedChunkedDataError: when anything other than CRLF are
            received.
        r�Fr��Chunk did not end with CRLFr�r>T)r�r�r<r�rrrvrvr|�_dataReceived_CRLF�s
z*_ChunkedTransferDecoder._dataReceived_CRLFcCs\t|j�dkr	dS|j�d�std��t|j�dd���}|jdd�=d|_|�|�dS)a�
        Await the carriage return and line feed characters that follow the
        terminal zero-length chunk. Then invoke C{finishCallback} and switch to
        state C{'FINISHED'}.

        @returns: C{False}, as there is either insufficient data to continue,
            or no data remains.

        @raises _MalformedChunkedDataError: when anything other than CRLF is
            received.
        r�Fr�rN�FINISHED)r�r�r<r��
memoryview�tobytesrr�rDrvrvr|�_dataReceived_TRAILER�s
z-_ChunkedTransferDecoder._dataReceived_TRAILERcCs�t|j�|jkr%t|j�d|j���}|jd|j�=d|_|�|�dSt|j�}|jt|�8_|jdd�=|�|�dS)z�
        Deliver any available chunk data to the C{dataCallback}. When all the
        remaining data for the chunk arrives, switch to state C{'CRLF'}.

        @returns: C{True} to continue processing of any buffered data.
        N�CRLFT)r�r�r�rrrr�r�)r�chunkrvrvr|�_dataReceived_BODY�s

�
z*_ChunkedTransferDecoder._dataReceived_BODYcCstd��)z�
        Once C{finishCallback} has been invoked receipt of additional data
        raises L{RuntimeError} because it represents a programming error in
        the caller.
        zJ_ChunkedTransferDecoder.dataReceived called after last chunk was processed)r\rrvrvr|�_dataReceived_FINISHED�s�z._ChunkedTransferDecoder._dataReceived_FINISHEDr�cCsH|j|7_d}|r|jr"t|d|j��}|r |jsdSdSdSdS)zu
        Interpret data from a request or response body which uses the
        I{chunked} Transfer-Encoding.
        T�_dataReceived_N)r�rr)rr��goOnrvrvr|r�s

�z$_ChunkedTransferDecoder.dataReceivedcCs|jdkr
td|jf��dS)zj
        Verify that all data has been received.  If it has not been, raise
        L{_DataLoss}.
        r
zRChunked decoder in %r state, still expecting more data to get to 'FINISHED' state.N)rr�rrvrvr|r�
s
���z"_ChunkedTransferDecoder.noMoreData)r�N)r
rrrrrAr�rrr
rrrrr�r�rvrvrvr|r�Fs"1��
�
5
r�c@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�_NoPushProducerz�
    A no-op version of L{interfaces.IPushProducer}, used to abstract over the
    possibility that a L{HTTPChannel} transport does not provide
    L{IPushProducer}.
    cCr�)z�
        Pause producing data.

        Tells a producer that it has produced too much data to process for
        the time being, and to stop until resumeProducing() is called.
        Nrvrrvrvr|�pauseProducing!rz_NoPushProducer.pauseProducingcCr�)z�
        Resume producing data.

        This tells a producer to re-add itself to the main loop and produce
        more data for its consumer.
        Nrvrrvrvr|�resumeProducing)rz_NoPushProducer.resumeProducingcCr�)z�
        Register to receive data from a producer.

        @param producer: The producer to register.
        @param streaming: Whether this is a streaming producer or not.
        Nrvr�rvrvr|r�1rz _NoPushProducer.registerProducercCr�)zM
        Stop consuming data from a producer, without disconnecting.
        Nrvrrvrvr|r^9rz"_NoPushProducer.unregisterProducercCr�)z)
        IProducer.stopProducing
        Nrvrrvrvr|�
stopProducing>rz_NoPushProducer.stopProducingN)	r
rrrrrr�r^rrvrvrvr|rsrc@sDeZdZdZdZdZdZdZdZdZ	dZ
dZeZ
dZdZdZdZdZd	ZdZdZe�Zd
d�Zdd
�Zdd�Zdd�Zdd�Zdd�Zdd�Zdeddfdd�Z dd�Z!dd�Z"d d!�Z#d"d#�Z$d$d%�Z%d&d'�Z&d(d)�Z'd*d+�Z(d,d-�Z)d.d/�Z*d0d1�Z+d2d3�Z,d4d5�Z-d6d7�Z.d8d9�Z/d:d;�Z0d<d=�Z1d>d?�Z2d@dA�Z3dBdC�Z4dS)Dr<a
    A receiver for HTTP requests.

    The L{HTTPChannel} provides L{interfaces.ITransport} and
    L{interfaces.IConsumer} to the L{Request} objects it creates.  It also
    implements L{interfaces.IPushProducer} to C{self.transport}, allowing the
    transport to pause it.

    @ivar MAX_LENGTH: Maximum length for initial request line and each line
        from the header.

    @ivar _transferDecoder: L{None} or a decoder instance if the request body
        uses the I{chunked} Transfer-Encoding.
    @type _transferDecoder: L{_ChunkedTransferDecoder}

    @ivar maxHeaders: Maximum number of headers allowed per request.
    @type maxHeaders: C{int}

    @ivar totalHeadersSize: Maximum bytes for request line plus all headers
        from the request.
    @type totalHeadersSize: C{int}

    @ivar _receivedHeaderSize: Bytes received so far for the header.
    @type _receivedHeaderSize: C{int}

    @ivar _handlingRequest: Whether a request is currently being processed.
    @type _handlingRequest: L{bool}

    @ivar _dataBuffer: Any data that has been received from the connection
        while processing an outstanding request.
    @type _dataBuffer: L{list} of L{bytes}

    @ivar _networkProducer: Either the transport, if it provides
        L{interfaces.IPushProducer}, or a null implementation of
        L{interfaces.IPushProducer}.  Used to attempt to prevent the transport
        from producing excess data when we're responding to a request.
    @type _networkProducer: L{interfaces.IPushProducer}

    @ivar _requestProducer: If the L{Request} object or anything it calls
        registers itself as an L{interfaces.IProducer}, it will be stored here.
        This is used to create a producing pipeline: pause/resume producing
        methods will be propagated from the C{transport}, through the
        L{HTTPChannel} instance, to the c{_requestProducer}.

        The reason we proxy through the producing methods rather than the old
        behaviour (where we literally just set the L{Request} object as the
        producer on the transport) is because we want to be able to exert
        backpressure on the client to prevent it from sending in arbitrarily
        many requests without ever reading responses.  Essentially, if the
        client never reads our responses we will eventually stop reading its
        requests.

    @type _requestProducer: L{interfaces.IPushProducer}

    @ivar _requestProducerStreaming: A boolean that tracks whether the producer
        on the L{Request} side of this channel has registered itself as a
        L{interfaces.IPushProducer} or an L{interfaces.IPullProducer}.
    @type _requestProducerStreaming: L{bool} or L{None}

    @ivar _waitingForTransport: A boolean that tracks whether the transport has
        asked us to stop producing.  This is used to keep track of what we're
        waiting for: if the transport has asked us to stop producing then we
        don't want to unpause the transport until it asks us to produce again.
    @type _waitingForTransport: L{bool}

    @ivar abortTimeout: The number of seconds to wait after we attempt to shut
        the transport down cleanly to give up and forcibly terminate it.  This
        is only used when we time a connection out, to prevent errors causing
        the FD to get leaked.  If this is L{None}, we will wait forever.
    @type abortTimeout: L{int}

    @ivar _abortingCall: The L{twisted.internet.base.DelayedCall} that will be
        used to forcibly close the transport if it doesn't close cleanly.
    @type _abortingCall: L{twisted.internet.base.DelayedCall}

    @ivar _optimisticEagerReadSize: When a resource takes a long time to answer
        a request (via L{twisted.web.server.NOT_DONE_YET}, hopefully one day by
        a L{Deferred}), we would like to be able to let that resource know
        about the underlying transport disappearing as promptly as possible,
        via L{Request.notifyFinish}, and therefore via
        C{self.requests[...].connectionLost()} on this L{HTTPChannel}.

        However, in order to simplify application logic, we implement
        head-of-line blocking, and do not relay pipelined requests to the
        application until the previous request has been answered.  This means
        that said application cannot dispose of any entity-body that comes in
        from those subsequent requests, which may be arbitrarily large, and it
        may need to be buffered in memory.

        To implement this tradeoff between prompt notification when possible
        (in the most frequent case of non-pipelined requests) and correct
        behavior when not (say, if a client sends a very long-running GET
        request followed by a PUT request with a very large body) we will
        continue reading pipelined requests into C{self._dataBuffer} up to a
        given limit.

        C{_optimisticEagerReadSize} is the number of bytes we will accept from
        the client and buffer before pausing the transport.

        This behavior has been in place since Twisted 17.9.0 .

    @type _optimisticEagerReadSize: L{int}
    i�i@�r>r�r�NFcCsg|_d|_g|_d|_dS�NF)�requests�_handlingRequest�_dataBuffer�_transferDecoderrrvrvr|r�s
zHTTPChannel.__init__cCs0|�|j�t�|jt��|_|j�|d�dS)NT)�
setTimeout�timeOutrL�
IPushProducerr%r�_networkProducerr�rrvrvr|rG�s
�zHTTPChannel.connectionMadecCs�|��|jt|�7_|j|jkr|��dS|jr�|js)dd�|_|_dS|s5|jdkr5d|_dSt	�
|j�rA|�|�}n	|�|t|j��}|j�
|�d|_|��}t|�dkrc|��dS|\}}}z|�d�Wnty}|��YdSw||_||_||_dS|d	kr�|jr�|�|j�}|s�dSd	|_|��|jdkr�|��dS|��dS|dd
vr�|jd|�d
�7_dS|jr�|�|j�||_dS)zp
        Called for each line from request until the end of headers when
        it enters binary mode.
        NcWrFrrv)rurvrvr|r��rz*HTTPChannel.lineReceived.<locals>.<lambda>r�r�r>r�r�r�� 	r�)�resetTimeout�_receivedHeaderSizer��totalHeadersSizer|�_HTTPChannel__first_line�
persistentr�r@r^�
providedBy�requestFactoryrr�r�r��UnicodeDecodeError�_command�_path�_version�_HTTPChannel__header�headerReceived�allHeadersReceivedr��allContentReceivedr;r0)rr��requestr�rr�okrvrvr|r@�s\
�


zHTTPChannel.lineReceivedcCs|j�|�|��dSr)rr�r4rDrvrvr|�_finishRequestBody	szHTTPChannel._finishRequestBodycs��fdd�}|dkr0|��s|�Szt|�}Wnty#|�YSwt|�jdj�j�}n$|dkrR|��dkrGd}t�jdj�j�}n
|��dkrOd	S|�Sd	S�j	dur\|�S|�_
|�_	d	S)
z�
        If the provided header is C{content-length} or
        C{transfer-encoding}, choose the appropriate decoder if any.

        Returns L{True} if the request can proceed and L{False} if not.
        cs���d�_dSr)r|r�rvrrvr|�fail(	sz5HTTPChannel._maybeChooseTransferDecoder.<locals>.failr/r�stransfer-encodingr�NsidentityT)r�r�r�r�rrr7rxr�r r�)rr�r�r8r��newTransferDecoderrvrr|�_maybeChooseTransferDecoder 	s6
���
z'HTTPChannel._maybeChooseTransferDecodercCs�z
|�dd�\}}Wnty|��YdSw|r#|dd���r)|��dS|��}|�d�}|�||�s:dS|jdj}|�	|�}|durO|�
|�n|�||g�|jd7_|j|j
kri|��dSdS)af
        Do pre-processing (for content-length) and store this header away.
        Enforce the per-request header limit.

        @type line: C{bytes}
        @param line: A line from the header section of a request, excluding the
            line delimiter.

        @return: A flag indicating whether the header was valid.
        @rtype: L{bool}
        r.r�Fr�Nr%T)r�r�r|�isspacerxr�r:rrrgr�r��_receivedHeaderCount�
maxHeaders)rr�r�r��
reqHeadersr�rvrvr|r2O	s.�

zHTTPChannel.headerReceivedcCsx|j}|j}|j}d|_d|_d|_d|_d|_|`|`|`|jr'|�	d�|_
d|_|��|j
d}|�|||�dS)Nr>r�Tr�)r.r/r0r�r<r'r)r r"r!�
_savedTimeOutrr;rr)rrr�r�reqrvrvr|r4y	s
zHTTPChannel.allContentReceivedr�r�cCst|jr|j�|�ttt|j��|jkr|js|j�	�dS|�
�z	|j�|�WdSt
y9|��YdSw)a�
        This is called when this HTTP/1.1 parser is in raw mode rather than
        line mode.

        It may be in raw mode for one of two reasons:

            1. All the headers of a request have been received and this
               L{HTTPChannel} is currently receiving its body.

            2. The full content of a request has been received and is currently
               being processed asynchronously, and this L{HTTPChannel} is
               buffering the data of all subsequent requests to be parsed
               later.

        In the second state, the data will be played back later.

        @note: This isn't really a public API, and should be invoked only by
            L{LineReceiver}'s line parsing logic.  If you wish to drive an
            L{HTTPChannel} from a custom data source, call C{dataReceived} on
            it directly.

        @see: L{LineReceive.rawDataReceived}
        N)rrr��sumr�r��_optimisticEagerReadSize�_waitingForTransportr$rr&r r�r�r|rDrvrvr|rI�	s�
�zHTTPChannel.rawDataReceivedcCsp|jd}|��|�||j�|_|�|j�|j�d�}|r2|d�	�dkr4|jdkr6|�
�dSdSdSdS)Nr�sexpectr>s100-continuer�)rr�checkPersistencer0r*rr�rrgrx�_send100Continue)rr@�expectContinuervrvr|r3�	s
�
�zHTTPChannel.allHeadersReceivedcCsZ|j�d�}|rdd�|d�d�D�}ng}|dkr+d|vr)|j�ddg�dSd	SdS)
a�
        Check if the channel should close or not.

        @param request: The request most recently received over this channel
            against which checks will be made to determine if this connection
            can remain open after a matching response is returned.

        @type version: C{bytes}
        @param version: The version of the request.

        @rtype: C{bool}
        @return: A flag which, if C{True}, indicates that this connection may
            remain open to receive another request; if C{False}, the connection
            must be closed in order to indicate the completion of the response
            to C{request}.
        s
connectioncSrurvrw)rz�trvrvr|r}�	r~z0HTTPChannel.checkPersistence.<locals>.<listcomp>r>r�r�scloseFT)rrgr�rr�)rr5r�
connection�tokensrvrvr|rD�	s
zHTTPChannel.checkPersistencecCst||jdkr	t�|jd=|js|j��|jr4d|_|jr$|�|j�d�	|j
�}g|_
|�|�dS|��dS)zC
        Called by first request in queue when it is done.
        r>Fr�N)
rr�rCr$rr*rr?r!rrrHr�)rr5r�rvrvr|r_�	s
zHTTPChannel.requestDonecCsB|jjdt|j���d�|jdur|�|j|j�|_|�	�dS)NzTiming out client: {peer}��peer)
rZr�r�r%rP�abortTimeout�	callLater�forceAbortClient�
_abortingCallr�rrvrvr|�timeoutConnection
s
�zHTTPChannel.timeoutConnectioncCs.|jjdt|j���d�d|_|j��dS)a
        Called if C{abortTimeout} seconds have passed since the timeout fired,
        and the connection still hasn't gone away. This can really only happen
        on extremely bad connections or when clients are maliciously attempting
        to keep connections open.
        z"Forcibly timing out client: {peer}rJN)rZr�r�r%rPrO�abortConnectionrrvrvr|rN
s
�zHTTPChannel.forceAbortClientcCsB|�d�|jD]}|�|�q|jdur|j��d|_dSdSr)r!rrrO�cancel)rr�r5rvrvr|r,
s




�zHTTPChannel.connectionLostcCst�|jd�durdSdS)a#
        Return L{True} if this channel is using a secure transport.

        Normally this method returns L{True} if this instance is using a
        transport that implements L{interfaces.ISSLTransport}.

        @returns: L{True} if this request is secure
        @rtype: C{bool}
        NTF)rL�
ISSLTransportr%rrvrvr|r�6
s
zHTTPChannel.isSecurec
Cspt�}|D]
\}}|�||�q|d|d|d}|g}	|	�dd�|��D��|	�d�|j�|	�dS)a�
        Called by L{Request} objects to write a complete set of HTTP headers to
        a transport.

        @param version: The HTTP version in use.
        @type version: L{bytes}

        @param code: The HTTP status code to write.
        @type code: L{bytes}

        @param reason: The HTTP reason phrase to write.
        @type reason: L{bytes}

        @param headers: The headers to write to the transport.
        @type headers: L{twisted.web.http_headers.Headers}
        r�r�css.�|]\}}|D]}|d|dVqqdS)r'r�Nrv)rzr{r�r(rvrvr|�	<genexpr>[
s����z+HTTPChannel.writeHeaders.<locals>.<genexpr>N)r[�addRawHeader�extendr�r�r%r)
rrr�r�r��sanitizedHeadersr{r(�responseLine�headerSequencervrvr|r�D
s
�
zHTTPChannel.writeHeaderscCrk)z�
        Called by L{Request} objects to write response data.

        @param data: The data chunk to write to the stream.
        @type data: L{bytes}

        @return: L{None}
        Nr,rDrvrvr|rc
�	zHTTPChannel.writecCrk)z�
        Write a list of strings to the HTTP response.

        @param iovec: A list of byte strings to write to the stream.
        @type iovec: L{list} of L{bytes}

        @return: L{None}
        Nr$)r�iovecrvrvr|rn
rZzHTTPChannel.writeSequencecC�
|j��S)zg
        Get the remote address of this connection.

        @return: An L{IAddress} provider.
        )r%rPrrvrvr|rPy
�
zHTTPChannel.getPeercCr\)zf
        Get the local address of this connection.

        @return: An L{IAddress} provider.
        )r%rRrrvrvr|rR�
r]zHTTPChannel.getHostcCs|j��|j��S)z�
        Closes the connection. Will write any data that is pending to be sent
        on the network, but if this response has not yet been written to the
        network will not write anything.

        @return: L{None}
        )r$r^r%r�rrvrvr|r��
s

zHTTPChannel.loseConnectioncCsJ|jdurtd||jf��|st||�}||_||_|s#|��dSdS)a
        Register to receive data from a producer.

        This sets self to be a consumer for a producer.  When this object runs
        out of data (as when a send(2) call on a socket succeeds in moving the
        last data from a userspace buffer into a kernelspace buffer), it will
        ask the producer to resumeProducing().

        For L{IPullProducer} providers, C{resumeProducing} will be called once
        each time data is required.

        For L{IPushProducer} providers, C{pauseProducing} will be called
        whenever the write buffer fills up and C{resumeProducing} will only be
        called when it empties.

        @type producer: L{IProducer} provider
        @param producer: The L{IProducer} that will be producing data.

        @type streaming: L{bool}
        @param streaming: C{True} if C{producer} provides L{IPushProducer},
        C{False} if C{producer} provides L{IPullProducer}.

        @raise RuntimeError: If a producer is already registered.

        @return: L{None}
        NzHCannot register producer %s, because producer %s was never unregistered.)�_requestProducerr\rN�_requestProducerStreaming�startStreamingr�rvrvr|r��
s
��
�zHTTPChannel.registerProducercCs.|jdurdS|js|j��d|_d|_dS)zg
        Stop consuming data from a producer, without disconnecting.

        @return: L{None}
        N)r^r_�
stopStreamingrrvrvr|r^�
s


zHTTPChannel.unregisterProducercCr�)a&
        Stop producing data.

        The HTTPChannel doesn't *actually* implement this, beacuse the
        assumption is that it will only be called just before C{loseConnection}
        is called. There's nothing sensible we can do other than call
        C{loseConnection} anyway.
        N)r^rrrvrvr|r�
s
	�zHTTPChannel.stopProducingcC�2d|_|jdur
|j��|js|j��dSdS)a"
        Pause producing data.

        This will be called by the transport when the send buffers have been
        filled up. We want to simultaneously pause the producing L{Request}
        object and also pause our transport.

        The logic behind pausing the transport is specifically to avoid issues
        like https://twistedmatrix.com/trac/ticket/8868. In this case, our
        inability to send does not prevent us handling more requests, which
        means we increasingly queue up more responses in our send buffer
        without end. The easiest way to handle this is to ensure that if we are
        unable to send our responses, we will not read further data from the
        connection until the client pulls some data out. This is a bit of a
        blunt instrument, but it's ok.

        Note that this potentially interacts with timeout handling in a
        positive way. Once the transport is paused the client may run into a
        timeout which will cause us to tear the connection down. That's a good
        thing!
        TN)rCr^rrr$rrvrvr|r�
s

�zHTTPChannel.pauseProducingcCrb)a!
        Resume producing data.

        This will be called by the transport when the send buffer has dropped
        enough to actually send more data. When this happens we can unpause any
        outstanding L{Request} producers we have, and also unpause our
        transport.
        FN)rCr^rrr$rrvrvr|r�
s	

�zHTTPChannel.resumeProducingcCs|j�d�dS)z}
        Sends a 100 Continue response, used to signal to clients that further
        processing will be performed.
        sHTTP/1.1 100 Continue

Nr,rrvrvr|rEszHTTPChannel._send100ContinuecCs|j�d�|��dS)a&
        This is a quick and dirty way of responding to bad requests.

        As described by HTTP standard we should be patient and accept the
        whole request from the client before sending a polite bad request
        response, even in the case when clients send tons of data.
        sHTTP/1.1 400 Bad Request

N)r%rr�rrvrvr|r|sz-HTTPChannel._respondToBadRequestAndDisconnect)5r
rrrr=r(rLr�r*r1r)�_HTTPChannel__contentr:r,r?r<r'r^r_rCrOrBrQrZrrGr@r7r:r2r4r�rIr3rDr_rPrNrr�r�rrrPrRr�r�r^rrrrEr|rvrvrvr|r<Ds`hI/*++	
*#r<cCsxt|t�s
|�d�}t|�}t|t�s|�d�}|�d�r#|dd�}|�d�r6|dd��dd��d	d�S|dd�S)
z�
    Return a string like python repr, but always escaped as if surrounding
    quotes were double quotes.

    @param s: The string to escape.
    @type s: L{bytes} or L{str}

    @return: An escaped string.
    @rtype: L{str}
    r�r�r�N�'r��"z\"z\')r�r�r�r�r�r�r<r�)r��rrvrvr|�_escape"s





rgcCs�|��}t|tjtjtf�r|j}nd}t|�d�pd�}t|�d�p$d�}dt	t|�|t|j
�t|j�t|j�|j
|jp>d||d�	}|S)zm
    @return: A combined log formatted log line for the given request.

    @see: L{IAccessLogFormatter}
    �-sreferers
user-agentzk"%(ip)s" - - %(timestamp)s "%(method)s %(uri)s %(protocol)s" %(code)d %(length)s "%(referrer)s" "%(agent)s"r�)	�ip�	timestamprvr]rMr�r��referrer�agent)r�r�rKr�r��_XForwardedForAddressrSrgr��dictrvr]rwr�r�)rjr5�
clientAddrrirkrlr�rvrvr|�combinedLogFormatter:s.����rpc@seZdZdZdd�ZdS)rmz�
    L{IAddress} which represents the client IP to log for a request, as gleaned
    from an X-Forwarded-For header.

    @ivar host: An IP address or C{b"-"}.
    @type host: L{bytes}

    @see: L{proxiedLogFormatter}
    cCs
||_dSrr��rrSrvrvr|rhs
z_XForwardedForAddress.__init__N)r
rrrrrvrvrvr|rm\s
rmc@s<eZdZdZdd�Zedd��Zedd��Zedd	��Zd
S)�_XForwardedForRequestz�
    Add a layer on top of another request that only uses the value of an
    X-Forwarded-For header as the result of C{getClientAddress}.
    cCs,|jj�ddg�d�d�d��}t|�S)a8
        The client address (the first address) in the value of the
        I{X-Forwarded-For header}.  If the header is not present, the IP is
        considered to be C{b"-"}.

        @return: L{_XForwardedForAddress} which wraps the client address as
            expected by L{combinedLogFormatter}.
        sx-forwarded-forrhr>�,)�_requestrrgr�r�rmrqrvrvr|r�rs
��z&_XForwardedForRequest.getClientAddresscC�|jjS)zX
        @return: The protocol version in the request.
        @rtype: L{bytes}
        )rtrwrrvrvr|rw�r�z!_XForwardedForRequest.clientprotocCru)zT
        @return: The response code for the request.
        @rtype: L{int}
        )rtr�rrvrvr|r��r�z_XForwardedForRequest.codecCru)z`
        @return: The number of bytes sent in the response body.
        @rtype: L{int}
        )rtr�rrvrvr|r��r�z _XForwardedForRequest.sentLengthN)	r
rrrr��propertyrwr�r�rvrvrvr|rrls

rrrtcCst|t|��S)z�
    @return: A combined log formatted log line for the given request but use
        the value of the I{X-Forwarded-For} header as the value for the client
        IP address.

    @see: L{IAccessLogFormatter}
    )rprr)rjr5rvrvr|�proxiedLogFormatter�s	rwc@s�eZdZdZdZeZdZdZdZ	dZ
edd��Zej
dd��Zedd��Zej
dd��Zed	d
��Zej
dd
��Zedd
��Zej
dd
��Zedd��Zej
dd��Zdd�ZdS)�_GenericHTTPChannelProtocola�
    A proxy object that wraps one of the HTTP protocol objects, and switches
    between them depending on TLS negotiated protocol.

    @ivar _negotiatedProtocol: The protocol negotiated with ALPN or NPN, if
        any.
    @type _negotiatedProtocol: Either a bytestring containing the ALPN token
        for the negotiated protocol, or L{None} if no protocol has yet been
        negotiated.

    @ivar _channel: The object capable of behaving like a L{HTTPChannel} that
        is backing this object. By default this is a L{HTTPChannel}, but if a
        HTTP protocol upgrade takes place this may be a different channel
        object. Must implement L{IProtocol}.
    @type _channel: L{HTTPChannel}

    @ivar _requestFactory: A callable to use to build L{IRequest} objects.
    @type _requestFactory: L{IRequest}

    @ivar _site: A reference to the creating L{twisted.web.server.Site} object.
    @type _site: L{twisted.web.server.Site}

    @ivar _factory: A reference to the creating L{HTTPFactory} object.
    @type _factory: L{HTTPFactory}

    @ivar _timeOut: A timeout value to pass to the backing channel.
    @type _timeOut: L{int} or L{None}

    @ivar _callLater: A value for the C{callLater} callback.
    @type _callLater: L{callable}
    NcCru)z=
        @see: L{_genericHTTPChannelProtocolFactory}
        )�_channelr�rrvrvr|r���z#_GenericHTTPChannelProtocol.factorycCs||_||j_dSr)�_factoryryr��rr(rvrvr|r��scCru)z
        A callable to use to build L{IRequest} objects.

        Retries the object from the current backing channel.
        )ryr,rrvrvr|r,��z*_GenericHTTPChannelProtocol.requestFactorycC�||_||j_dS)a 
        A callable to use to build L{IRequest} objects.

        Sets the object on the backing channel and also stores the value for
        propagation to any new channel.

        @param value: The new callable to use.
        @type value: A L{callable} returning L{IRequest}
        N)�_requestFactoryryr,r|rvrvr|r,��cCru)z�
        A reference to the creating L{twisted.web.server.Site} object.

        Returns the site object from the backing channel.
        )ry�siterrvrvr|r��r}z _GenericHTTPChannelProtocol.sitecCr~)a;
        A reference to the creating L{twisted.web.server.Site} object.

        Sets the object on the backing channel and also stores the value for
        propagation to any new channel.

        @param value: The L{twisted.web.server.Site} object to set.
        @type value: L{twisted.web.server.Site}
        N)�_siteryr�r|rvrvr|r��r�cCru)z;
        The idle timeout for the backing channel.
        )ryr"rrvrvr|r"
rzz#_GenericHTTPChannelProtocol.timeOutcCr~)a
        The idle timeout for the backing channel.

        Sets the idle timeout on both the backing channel and stores it for
        propagation to any new backing channel.

        @param value: The timeout to set.
        @type value: L{int} or L{float}
        N)�_timeOutryr"r|rvrvr|r"r�cCru)z�
        A value for the C{callLater} callback. This callback is used by the
        L{twisted.protocols.policies.TimeoutMixin} to handle timeouts.
        )ryrMrrvrvr|rMr�z%_GenericHTTPChannelProtocol.callLatercCr~)z�
        Sets the value for the C{callLater} callback. This callback is used by
        the L{twisted.protocols.policies.TimeoutMixin} to handle timeouts.

        @param value: The new callback to use.
        @type value: L{callable}
        N)�
_callLaterryrMr|rvrvr|rM's	cCs�|jdurpz|jjj}Wntyd}Ynw|durd}|dkrets(td��|jj}|��|j�	d�|jj}t
�|_|j|j_|j
|j_|j|j_|j|j_|j|j_|j�|�|�|jd�n|dksmJd��||_|j�|�S)zi
        An override of L{IProtocol.dataReceived} that checks what protocol we're
        using.
        Nshttp/1.1sh2z"Negotiated HTTP/2 without support.TzUnsupported protocol negotiated)�_negotiatedProtocolryr%�negotiatedProtocol�AttributeError�
H2_ENABLEDr�r$r^r!r`rr,r�r�r{r�r�r"r�rM�makeConnectionr�r�)rr�r��networkProducerr%rvrvr|r�3s8
�





�z(_GenericHTTPChannelProtocol.dataReceived)r
rrrr�r:rr{r�r�r�rvr��setterr,r�r"rMr�rvrvrvr|rx�s: 









rxrycCs
tt��S)zK
    Returns an appropriately initialized _GenericHTTPChannelProtocol.
    )rxr<rrvrvr|�"_genericHTTPChannelProtocolFactorygs
r�c@s^eZdZdZeZdZeZdeddfdd�Z	dd�Z
dd�Zd	d
�Zdd�Z
d
d�Zdd�ZdS)r=a
    Factory for HTTP server.

    @ivar _logDateTime: A cached datetime string for log messages, updated by
        C{_logDateTimeCall}.
    @type _logDateTime: C{str}

    @ivar _logDateTimeCall: A delayed call for the next update to the cached
        log datetime string.
    @type _logDateTimeCall: L{IDelayedCall} provided

    @ivar _logFormatter: See the C{logFormatter} parameter to L{__init__}

    @ivar _nativeize: A flag that indicates whether the log file being written
        to wants native strings (C{True}) or bytes (C{False}).  This is only to
        support writing to L{twisted.python.log} which, unfortunately, works
        with native strings.

    @ivar reactor: An L{IReactorTime} provider used to manage connection
        timeouts and compute logging timestamps.
    NcCsX|sddlm}||_|durtj�|�}||_||_|dur!t}||_d|_	d|_
dS)a
        @param logPath: File path to which access log messages will be written
            or C{None} to disable logging.
        @type logPath: L{str} or L{bytes}

        @param timeout: The initial value of L{timeOut}, which defines the idle
            connection timeout in seconds, or C{None} to disable the idle
            timeout.
        @type timeout: L{float}

        @param logFormatter: An object to format requests into log lines for
            the access log.  L{combinedLogFormatter} when C{None} is passed.
        @type logFormatter: L{IAccessLogFormatter} provider

        @param reactor: An L{IReactorTime} provider used to manage connection
            timeouts and compute logging timestamps. Defaults to the global
            reactor.
        r>)�reactorN)�twisted.internetr��osr��abspath�logPathr"rp�
_logFormatter�_logDateTime�_logDateTimeCall)rr��timeout�logFormatterr�rvrvr|r�s
zHTTPFactory.__init__cCs&t|j���|_|j�d|j�|_dS)zY
        Update log datetime periodically, so we aren't always recalculating it.
        r�N)r1r�r�r�rM�_updateLogDateTimer�rrvrvr|r��szHTTPFactory._updateLogDateTimecCs$tj�||�}|jj|_|j|_|Sr)rM�
ServerFactory�
buildProtocolr�rMr")r�addr�prvrvr|r��s
zHTTPFactory.buildProtocolcCs6|jdur	|��|jr|�|j�|_dStj|_dS)z6
        Set up request logging if necessary.
        N)r�r�r��_openLogFile�logFilerT�logfilerrvrvr|�startFactory�s

zHTTPFactory.startFactorycCsTt|d�r|jtjkr|j��|`|jdur&|j��r(|j��d|_dSdSdS)Nr�)r�r�rTr�rar��activerRrrvrvr|�stopFactory�s



�zHTTPFactory.stopFactorycCst|dd�}|S)zP
        Override in subclasses, e.g. to use L{twisted.python.logfile}.
        �abr�)�open)rr��frvrvr|r��szHTTPFactory._openLogFilecCsFz|j}Wn
tyYdSw|�|j|�d}|�|�d��dS)z�
        Write a line representing C{request} to the access log file.

        @param request: The request object about which to log.
        @type request: L{Request}
        �
rmN)r�r�r�r�rr�)rr5r�r�rvrvr|rT�s
�zHTTPFactory.log)r
rrrr�rMr��_REQUEST_TIMEOUTr"rr�r�r�r�r�rTrvrvrvr|r=ns
�%
r=)r>r>r)�r�__all__r�r�r�r�r�r��rerKr�r��ior?�typingr@rArBrC�urllib.parserDrEr�r.r��zope.interfacerFrGrHrI�incrementalrJr�rKrLrM�"twisted.internet._producer_helpersrN�twisted.internet.deferrO�twisted.internet.interfacesrP�twisted.loggerrQ�twisted.protocolsrRrS�twisted.pythonrT�twisted.python.compatrUrVrW�twisted.python.componentsrX�twisted.python.deprecaterY�twisted.python.failurerZ�twisted.web._responsesrr&rrrr#rrr(rr)r*r$rrr	r
rrrrr+rr%rrrrrrrrrr r"rr,r
r'rrrr!r�twisted.web.http_headersr[r\�twisted.web.iwebr]r^r_�twisted.web._http2r`r��ImportErrorr��protocol_versionr-�	responsesr�r�r�r�r�r/r0r1r2r3r4r�rr�r�r�r5r6r�r7�LineReceiverr8r9rrVrM�compiler��	IConsumerr:r{r�r;r�r�rrr�r#r�
ITransport�TimeoutMixinr<rgrp�IAddressrmrrrwrxr�r�r=rvrvrvr|�<module>s�A�.�


7

_
	
@
	J�
T*b
!0
@

Spamworldpro Mini