HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/cheroot/__pycache__/server.cpython-310.pyc
o

�N�g�*�@s�dZddlmZmZmZeZddlZddlZddl	Z	ddl
ZddlZddl
Z
ddlZddlZddlZddlZddlZddlZzddlmZWneyUddlmZYnwddlZddlmZddlmZddlmZmZm Z dd	l!m"Z"m#Z#dd
l!m$Z$ddl%m&Z&ddl'm(Z(m)Z)d
Z*e�+�dkZ,	e�-dd��.d�Z/	e,o�e/Z0	e0r�z
ddl1Z1ddl2Z2Wney�	dZ0d\Z1Z2Ynwddl3Z3e,r�e4ed�r�e4ed�s�de_5e4ed�s�de_6e4ed�s�	e$r�dnde_7dZ8dZ9dZ:dZ;d Z<d!Z=d"Z>d#Z?d$Z@d%ZAe	�Bd"�Cd&eAf��ZDeE�ZFgd'�ZGe4ed(��sie_HGd)d*�d*�ZIGd+d,�d,eI�ZJGd-d.�d.�ZKGd/d0�d0�ZLGd1d2�d2�ZMGd3d4�d4�ZNGd5d6�d6�ZOGd7d8�d8�ZPGd9d:�d:�ZQd;d<d=�ZRdAd?d@�ZSdS)Bas
A high-speed, production ready, thread pooled, generic HTTP server.

For those of you wanting to understand internals of this module, here's the
basic call flow. The server's listening thread runs a very tight loop,
sticking incoming connections onto a Queue::

    server = HTTPServer(...)
    server.start()
    ->  serve()
        while ready:
            _connections.run()
                while not stop_requested:
                    child = socket.accept()  # blocks until a request comes in
                    conn = HTTPConnection(child, ...)
                    server.process_conn(conn)  # adds conn to threadpool

Worker threads are kept in a pool and poll the Queue, popping off and then
handling each connection in turn. Each connection can consist of an arbitrary
number of requests and their responses, so we run a nested loop::

    while True:
        conn = server.requests.get()
        conn.communicate()
        ->  while True:
                req = HTTPRequest(...)
                req.parse_request()
                ->  # Read the Request-Line, e.g. "GET /page HTTP/1.1"
                    req.rfile.readline()
                    read_headers(req.rfile, req.inheaders)
                req.respond()
                ->  response = app(...)
                    try:
                        for chunk in response:
                            if chunk:
                                req.write(chunk)
                    finally:
                        if hasattr(response, "close"):
                            response.close()
                if req.close_connection:
                    return

For running a server you can invoke :func:`start() <HTTPServer.start()>` (it
will run the server forever) or use invoking :func:`prepare()
<HTTPServer.prepare()>` and :func:`serve() <HTTPServer.serve()>` like this::

    server = HTTPServer(...)
    server.prepare()
    try:
        threading.Thread(target=server.serve).start()

        # waiting/detecting some appropriate stop condition here
        ...

    finally:
        server.stop()

And now for a trivial doctest to exercise the test suite

>>> 'HTTPServer' in globals()
True
�)�absolute_import�division�print_functionN)�	lru_cache)�queue)�urllib�)�connections�errors�__version__)�bton�ntou)�IS_PPC)�
threadpool)�MakeFile�StreamWriter)
�HTTPRequest�HTTPConnection�
HTTPServer�HeaderReader�DropUnderscoreHeaderReader�SizeCheckWrapper�KnownLengthRFile�ChunkedRFile�Gateway�get_ssl_adapter_class�WindowsZSERVER_SOFTWARE�zGoogle App Engine/F)NN�AF_INET6�IPPROTO_IPV6�)�IPV6_V6ONLY��SO_PEERCRED���
s
�	� �:�;��*�/s%2Fs(?i))sAcceptsAccept-CharsetsAccept-EncodingsAccept-Languages
Accept-RangessAllows
Cache-Control�
ConnectionsContent-EncodingsContent-Language�ExpectsIf-Matchs
If-None-MatchsPragmasProxy-AuthenticatesTEsTrailer�Transfer-EncodingsUpgradesVarysViasWarningsWWW-Authenticate�
statisticsc@s*eZdZdZd	dd�Zdd�Zdd�ZdS)
rz`Object for reading headers from an HTTP request.

    Interface and default implementation.
    NcCs�|duri}	|��}|std��|tkr	|S|�t�s!td��|dttfvr.|��}n!z
|�td�\}}WntyCtd��w|��}|�	|�}|}|�
|�sUq|tvrg|�|�}|rgd�
||f�}|||<q)	a�
        Read headers from the given stream into the given header dict.

        If hdict is None, a new header dict is created. Returns the populated
        header dict.

        Headers which are repeated are folded together using a comma if their
        specification so dictates.

        This function raises ValueError when the read bytes violate the HTTP
        spec.
        You should probably return "400 Bad Request" if this happens.
        NT�Illegal end of headers.�HTTP requires CRLF terminatorsrrzIllegal header line.s, )�readline�
ValueError�CRLF�endswith�SPACE�TAB�strip�split�COLON�_transform_key�
_allow_header�comma_separated_headers�get�join)�self�rfileZhdict�line�v�k�hname�existing�rI�B/opt/saltstack/salt/lib/python3.10/site-packages/cheroot/server.py�__call__�s:
�
�


�zHeaderReader.__call__cCsdS)NTrI�rB�key_namerIrIrJr>�szHeaderReader._allow_headercCs|����S�N)r:�titlerLrIrIrJr=�szHeaderReader._transform_keyrN)�__name__�
__module__�__qualname__�__doc__rKr>r=rIrIrIrJr�s

4rcs eZdZdZ�fdd�Z�ZS)rzDCustom HeaderReader to exclude any headers with underscores in them.cstt|��|�}|o
d|vS)N�_)�superrr>)rBrM�orig��	__class__rIrJr>�sz(DropUnderscoreHeaderReader._allow_header)rPrQrRrSr>�
__classcell__rIrIrWrJr�src@sZeZdZdZdd�Zdd�Zddd�Zdd	d
�Zddd
�Zdd�Z	dd�Z
dd�ZeZdS)rz�Wraps a file-like object, raising MaxSizeExceeded if too large.

    :param rfile: ``file`` of a limited size
    :param int maxlen: maximum length of the file being read
    cCs||_||_d|_dS)z%Initialize SizeCheckWrapper instance.rN)rC�maxlen�
bytes_read)rBrCrZrIrIrJ�__init__s
zSizeCheckWrapper.__init__cCs"|jr
|j|jkrt���dSdSrN)rZr[r
�MaxSizeExceeded�rBrIrIrJ�
_check_length
s�zSizeCheckWrapper._check_lengthNcCs*|j�|�}|jt|�7_|��|S)��Read a chunk from ``rfile`` buffer and return it.

        :param int size: amount of data to read

        :returns: chunk from ``rfile``, limited by size if specified
        :rtype: bytes
        )rC�readr[�lenr_�rB�size�datarIrIrJraszSizeCheckWrapper.readcCs�|dur|j�|�}|jt|�7_|��|Sg}	|j�d�}|jt|�7_|��|�|�t|�dksB|dd�tkrGt�|�Sq)��Read a single line from ``rfile`` buffer and return it.

        :param int size: minimum amount of data to read

        :returns: one line from ``rfile``
        :rtype: bytes
        NT����)	rCr4r[rbr_�append�LF�EMPTYrA)rBrdre�resrIrIrJr4s

�zSizeCheckWrapper.readlinercC�Xd}g}|�|�}|r*|�|�|t|�7}d|kr"|kr#	|S|�|�}|s|S�z�Read all lines from ``rfile`` buffer and return them.

        :param int sizehint: hint of minimum amount of data to read

        :returns: lines of bytes read from ``rfile``
        :rtype: list[bytes]
        r�r4rirb�rB�sizehint�total�linesrDrIrIrJ�	readlines8�	

�
�zSizeCheckWrapper.readlinescC�|j��dS�z*Release resources allocated for ``rfile``.N�rC�closer^rIrIrJryL�zSizeCheckWrapper.closecC�|S�zReturn file iterator.rIr^rIrIrJ�__iter__P�zSizeCheckWrapper.__iter__cCs(t|j�}|jt|�7_|��|S�zGenerate next file chunk.)�nextrCr[rbr_�rBrerIrIrJ�__next__Ts
zSizeCheckWrapper.__next__rN�r)
rPrQrRrSr\r_rar4rtryr}r�r�rIrIrIrJrs


rc@sReZdZdZdd�Zddd�Zddd�Zdd
d�Zdd
�Zdd�Z	dd�Z
e
ZdS)rz�Wraps a file-like object, returning an empty string when exhausted.

    :param rfile: ``file`` of a known size
    :param int content_length: length of the file being read
    cCs||_||_dS)z%Initialize KnownLengthRFile instance.N)rC�	remaining)rBrC�content_lengthrIrIrJr\es
zKnownLengthRFile.__init__NcC�L|jdkrdS|dur|j}nt||j�}|j�|�}|jt|�8_|S)z�Read a chunk from ``rfile`` buffer and return it.

        :param int size: amount of data to read

        :rtype: bytes
        :returns: chunk from ``rfile``, limited by size if specified
        rr+N)r��minrCrarbrcrIrIrJraj�
zKnownLengthRFile.readcCr�)rfrr+N)r�r�rCr4rbrcrIrIrJr4}r�zKnownLengthRFile.readlinercCrmrnrorprIrIrJrt�ruzKnownLengthRFile.readlinescCrvrwrxr^rIrIrJry�rzzKnownLengthRFile.closecCr{r|rIr^rIrIrJr}�r~zKnownLengthRFile.__iter__cCs t|j�}|jt|�8_|Sr)r�rCr�rbr�rIrIrJr��s
zKnownLengthRFile.__next__rNr�)rPrQrRrSr\rar4rtryr}r�r�rIrIrIrJr^s


rc@sPeZdZdZddd�Zdd�Zddd	�Zdd
d�Zdd
d�Zdd�Z	dd�Z
dS)ra�Wraps a file-like object, returning an empty string when exhausted.

    This class is intended to provide a conforming wsgi.input value for
    request entities that have been encoded with the 'chunked' transfer
    encoding.

    :param rfile: file encoded with the 'chunked' transfer encoding
    :param int maxlen: maximum length of the file being read
    :param int bufsize: size of the buffer used to read the file
    � cCs(||_||_d|_t|_||_d|_dS)z!Initialize ChunkedRFile instance.rFN)rCrZr[rk�buffer�bufsize�closed)rBrCrZr�rIrIrJr\�s
zChunkedRFile.__init__cCs|jrdS|j��}|jt|�7_|jr#|j|jkr#t�d|j��|���	t
d�}z|�d�}t|d�}Wnt
yFt
dj|d���w|dkrPd|_dS|jr_|j||jkr_td��|j�|�}|jt|�7_|j|7_|j�d�}|tkr�t
d	t|�d
��dS)N�Request Entity Too Largerr�z)Bad chunked transfer size: {chunk_size!r})�
chunk_sizeT�z2Bad chunked transfer coding (expected '\r\n', got �))r�rCr4r[rbrZr
r]r:r;�	SEMICOLON�pop�intr5�format�IOErrorrar�r6�repr)rBrDr��chunkZcrlfrIrIrJ�_fetch�sJ
�
�������zChunkedRFile._fetchNcCs�t}|dkr|S	|rt|�|kr|S|js|��|js|S|r9|t|�}||jd|�7}|j|d�|_n||j7}t|_q	)r`rTN)rkrbr�r�)rBrdrer�rIrIrJra�s"
�zChunkedRFile.readcCs�t}|dkr|S	|rt|�|kr|S|js|��|js|S|j�t�}|r^|dkrC|t|�}||jd|�7}|j|d�|_n9t|t|�|�}||jd|�7}|j|d�|_n|dkrk||j7}t|_n||jd|�7}|j|d�|_q	)rfrTrhN)rkrbr�r��findrjr�)rBrdreZnewline_posr�rIrIrJr4s2
�zChunkedRFile.readlinercCrmrnrorprIrIrJrt=ruzChunkedRFile.readlinesccsz�|jstd��	|j��}|std��|jt|�7_|jr*|j|jkr*td��|tkr0dS|�	t�s9td��|Vq	)zqRead HTTP headers and yield them.

        Returns:
            Generator: yields CRLF separated lines.

        z:Cannot read trailers until the request body has been read.Tr2r�r3N)
r�r5rCr4r[rbrZr�r6r7)rBrDrIrIrJ�read_trailer_linesQs$��

�zChunkedRFile.read_trailer_linescCrvrwrxr^rIrIrJryorzzChunkedRFile.close)r�rNr�)rPrQrRrSr\r�rar4rtr�ryrIrIrIrJr�s
	
+

)rc@s�eZdZdZdZ	dZ	iZ	gZ	dZ	dZ		dZ
	e�Z	ddd�Z
dd�Zd	d
�Zdd�Zd
d�Zddd�Zdd�Zdd�Zdd�ZdS)rzrAn HTTP Request (and response).

    A single HTTP connection may consist of multiple request/response pairs.
    NFTcCsx||_||_d|_d|_d|_|jjdurd|_d|_i|_d|_g|_	d|_
|jj|_d|_
|jj|_||_||_dS)aInitialize HTTP request container instance.

        Args:
            server (HTTPServer): web server object receiving this request
            conn (HTTPConnection): HTTP connection object for this request
            proxy_mode (bool): whether this HTTPServer should behave as a PROXY
            server for certain requests
            strict_mode (bool): whether we should return a 400 Bad Request when
            we encounter a request that a HTTP compliant client should not be
            making
        FshttpNshttpszHTTP/1.0r)�server�conn�ready�started_request�scheme�ssl_adapter�response_protocol�	inheaders�status�
outheaders�sent_headersrX�close_connection�chunked_read�
chunked_write�
proxy_mode�strict_mode)rBr�r�r�r�rIrIrJr\�s"


zHTTPRequest.__init__cCs�t|jj|jj�|_z|��}Wntjy!|�dd�YdSw|s&dSz|�	�}Wntjy=|�dd�YdSw|sBdSd|_
dS)z;Parse the next HTTP request start-line and message-headers.z414 Request-URI Too LongzHThe Request-URI sent with the request exceeds the maximum allowed bytes.N�413 Request Entity Too LargezCThe headers sent with the request exceed the maximum allowed bytes.T)rr�rCr��max_request_header_size�read_request_liner
r]�simple_response�read_request_headersr�)rB�successrIrIrJ�
parse_request�s6�����
zHTTPRequest.parse_requestc
Cs�|j��}d|_|sdS|tkr|j��}|sdS|�t�s&|�dd�dSzH|���td�\}}}|�	d�s@|�dd�WdS|dd	��d
d�}t
|�dkrY|�dd�WdSttt
|��}|d
krm|�dd�WdSWnttfy�|�dd�YdSw||_|��|_|jr�||jkr�d}|�d|�dSztjr�	tt|d�d�tj�|�\}}}	}
}Wnty�|�dd�YdSw|p�|}|jdkr�|jr�|r�|n|	}	n�|jdk�r3|js�|�d�dStj�d�d|f��}
|
\}}}}}t}z|
j}Wn
t�yYnw||k�p|�pt ||||f�}|�r(|�dd�dS|}}	t}}
}nz|j�o=|j�o=|}|�rJ	|�dd�dS|j�oW|�	t!��oW|}|�rf	d}|�d|�dS|�rq|�dd�dS|	d	u�r~|�dd�dSzdd�t"�|	�D�}Wnt�y�}z|�d|j#d �WYd	}~dSd	}~wwt$�|�}	|	�	t!��s�t!|	}	|tu�r�||_%||_&|	|_'|
|_(t
|j)j*d�t
|j)j*d!�f}|d |d k�r�|�d�dS||_+d"t,||�|_-dS)#z�Read and parse first line of the HTTP request.

        Returns:
            bool: True if the request line is valid or False if it's malformed.

        TF�400 Bad Requestr3r�sHTTP/z$Malformed Request-Line: bad protocol�N�.rz#Malformed Request-Line: bad version)rrz505 HTTP Version Not SupportedzCannot fulfill requestzMalformed Request-Linez�Malformed method name: According to RFC 2616 (section 5.1.1) and its successors RFC 7230 (section 3.1.1) and RFC 7231 (section 4.1) method names are case-sensitive and uppercase.�asciizMalformed Request-URIsOPTIONSsCONNECTz405 Method Not Allowedr+s//zFInvalid path in Request-URI: request-target must match authority-form.z2Absolute URI not allowed if server is not a proxy.z�Invalid path in Request-URI: request-target must contain origin-form which starts with absolute-path (URI starting with a slash "/").z!Illegal #fragment in Request-URI.zInvalid path in Request-URI.cSsg|]}tj�|��qSrI)r�parse�unquote_to_bytes��.0�xrIrIrJ�
<listcomp>�s
��z1HTTPRequest.read_request_line.<locals>.<listcomp>r�z
HTTP/%s.%s).rCr4r�r6r7r�r:r;r8�
startswithrb�tuple�mapr�r5�
IndexError�uri�upper�methodr��sixZPY2r
rrr��urlsplit�UnicodeErrorr�rArk�port�any�
FORWARD_SLASH�QUOTED_SLASH_REGEX�args�QUOTED_SLASHr��	authority�path�qsr��protocolZrequest_protocolr�r�)rBZrequest_liner�r�Zreq_protocolZrpZrespr�r�r�r��fragmentZuri_is_absolute_formZ	uri_splitZ_schemeZ
_authority�_pathZ_qsZ	_fragmentZ_portZinvalid_pathZdisallowed_absoluteZatoms�ex�sprIrIrJr��s"


�
�����
��
���

�
������
����
�

���

 
zHTTPRequest.read_request_linec
Cs�z
|�|j|j�Wnty&}z|�d|jd�WYd}~dSd}~ww|jj}zt|j�	dd��}WntyF|�dd�YdSw|rU||krU|�dd�dS|j
d	krg|j�	d
d�dkrfd
|_n|j�	d
d�dkrsd
|_d}|j
d	kr�|j�	d�}|r�dd�|�d�D�}d|_
|r�|D]}|dkr�d
|_
q�|�d�d
|_dS|j�	dd�dkr�d�|jj�d�tdttf�}z
|jj�|�Wd
Stjy�}z|jdtjvr܂WYd}~d
Sd}~wwd
S)z�Read ``self.rfile`` into ``self.inheaders``.

        Ref: :py:attr:`self.inheaders <HTTPRequest.outheaders>`.

        :returns: success status
        :rtype: bool
        r�rNF�Content-Lengthz Malformed Content-Length Header.r��CThe entity sent with the request exceeds the maximum allowed bytes.�HTTP/1.1r.r+�closeT�
Keep-Aliver0cSs g|]}|��r|�����qSrI)r:�lowerr�rIrIrJr��s z4HTTPRequest.read_request_headers.<locals>.<listcomp>�,�chunkedz501 Unimplementedr/s100-continuer�s100 Continue)�
header_readerrCr�r5r�r�r��max_request_body_sizer�r@r�r�r;r�rAr��encoder8r6r��wfile�write�socket�errorr
�socket_errors_to_ignore)rBr��mrbs�cl�te�enc�msgrIrIrJr��st	�����
�

��
���z HTTPRequest.read_request_headerscCs�|jj}|jrt|jj|�|_n"t|j�dd��}|r*||kr*|j	s(|�
dd�dSt|jj|�|_|j�|��
�|jo@|��|jrM|jj�d�dSdS)z/Call the gateway and write its iterable output.r�rr�r�Ns0

)r�r�r�rr�rCr�r�r@r�r�r�gateway�respondr��ensure_headers_sentr�r�r�)rBr�r�rIrIrJr�$s$���zHTTPRequest.respondrc
Cs�t|�}d|jj|f}dt|�}d}|�d�|�d�|�d�g}|dd�dvr:d|_|jd	kr8|�d
�nd}|�t�|rQt	|t
j�rL|�d�}|�|�z
|jj
�t�|��WdStjy{}z|jdtjvrp�WYd}~dSd}~ww)
z+Write a simple response back to the client.z%s %s
zContent-Length: %s
zContent-Type: text/plain
�
ISO-8859-1N�)Z413Z414Tr�sConnection: close
r�r)�strr�r�rbr�r�r�rir6�
isinstancer��	text_typer�r�r�rkrAr�r�r�r
r�)rBr�r�Zproto_statusr��content_type�bufr�rIrIrJr�;s4�



���zHTTPRequest.simple_responsecCs|jsd|_|��dSdS)z:Ensure headers are sent to the client if not already sent.TN)r��send_headersr^rIrIrJr�`s�zHTTPRequest.ensure_headers_sentcCsZ|jr$|r$tt|��dd��d�}|t|tg}|jj�t�	|��dS|jj�|�dS)z$Write unbuffered data to the client.r�Nr�)
r��hexrbr�r6r�r�r�rkrA)rBr�Zchunk_size_hexr�rIrIrJr�fs

zHTTPRequest.writec
Cs�dd�|jD�}t|jdd��}|dkrd|_n&d|vr?|dks%|d	vr&n|jd
ko/|jdk}|r<d|_|j�d�nd|_|jsJ|jj	}||_d
|vrf|jd
kr]|jr\|j�d�n	|jsf|j�d�d|jvr||j�ddj
|jjd��d�f�|js�|j
s�t|jdd�}|dkr�|j�|�d|vr�|j�dtjjdd��d�f�d|vr�|j�d|jj�d�f�|jj�d�}|t|jtg}|jD]\}}	|�|tt|	t�q�|�t�|jj�t�|��dS)z�Assert, process, and send the HTTP response message-headers.

        You must set ``self.status``, and :py:attr:`self.outheaders
        <HTTPRequest.outheaders>` before calling this.
        cSsg|]\}}|���qSrI)r�)r��key�valuerIrIrJr�usz,HTTPRequest.send_headers.<locals>.<listcomp>Nr�i�Tscontent-length��)����i0r�sHEAD)r0r�s
connection)r.r�)r.r�r�ztimeout={connection_timeout})Zconnection_timeoutr�r�rsdatesDate)�usegmtsserversServerr�)r�r�r�r�r�r�r�rir��can_add_keepalive_connectionr��timeoutr�r��getattrrCra�email�utils�
formatdate�server_namer�r8r6r<r�r�r�rkrA)
rBZhkeysr�Z
needs_chunkedZcan_keepr��protor�rFrErIrIrJr�osh
�
�
��
��
zHTTPRequest.send_headers)FT)r)rPrQrRrSr�r�r�r�r�r�r�rr�r\r�r�r�r�r�r�r�r�rIrIrIrJrts8
!"h`
%	rc@s�eZdZdZdZdZdZejZ	ejZ
eZdZ
dZdZefdd�Zdd�ZdZdd	�Zd
d�Zdd
�Zdd�Zedd��Zedd��Zedd��Zdd�Zedd��Zedd��Zdd�ZdS)rz#An HTTP connection (active socket).NFcCsn||_||_||d|j�|_||d|j�|_d|_|jj|_|jj|_t	dd�|j
�|_
t	dd�|j�|_dS)aBInitialize HTTPConnection instance.

        Args:
            server (HTTPServer): web server object receiving this request
            sock (socket._socketobject): the raw socket object (usually
                TCP) for this connection
            makefile (file): a fileobject class for reading from the socket
        �rb�wbrr)�maxsizeN)r�r��rbufsizerC�wbufsizer��
requests_seen�peercreds_enabled�peercreds_resolve_enabledr�resolve_peer_creds�get_peer_creds)rBr��sock�makefilerIrIrJr\�s	

��zHTTPConnection.__init__c
Cs�d}z-|�|j|�}|��|jjdr|jd7_|js!WdSd}|��|js-WdSWdStj	y�}zG|j
d}d}||vrQ|rJ|rP|jrP|�|d�n'|t
jvrq|jjdt|�tjdd	�|�|d
�WYd}~dSWYd}~dSWYd}~dSd}~wttfy��t
jy�YdSt
jy�|�|�YdSty�}z|jjt|�tjdd	�|�|d
�WYd}~dSd}~ww)zrRead each request and respond appropriately.

        Returns true if the connection should be kept open.
        F�EnabledrTr)z	timed outzThe read operation timed outz408 Request Timeoutzsocket.error %s��level�	tracebackz500 Internal Server ErrorN)�RequestHandlerClassr�r��statsr
r�r�r�r�r�r�r��_conditional_errorr
r��	error_logr��logging�WARNING�KeyboardInterrupt�
SystemExit�
FatalSSLAlert�
NoSSLError�_handle_no_ssl�	Exception�ERROR)rBZrequest_seen�reqr�ZerrnumZtimeout_errsrIrIrJ�communicate�sb��
�

�
�
��������zHTTPConnection.communicatecCs`|r|jrdSz|jj}Wn
ty|jj}Ynwt|d|j�|_d}|�d|�d|_	dS)NrzUThe client sent a plain HTTP request, but this server only speaks HTTPS on this port.r�T)
r�r��_sock�AttributeError�_socketrr	r�r��linger)rBr"Z	resp_sockr�rIrIrJr*s
��
zHTTPConnection._handle_no_sslcCsT|r|jrdSz|�|�WdStjyYdStjy)|�|�YdSw)zvRespond with an error.

        Don't bother writing if a response
        has already started being written.
        N)r�r�r
rrr)rBr"�responserIrIrJr;s
�z!HTTPConnection._conditional_errorcCs,|j��|js|��|j��dS	dS)z,Close the socket underlying this connection.N)rCryr'�_close_kernel_socketr�r^rIrIrJryKs

zHTTPConnection.closec
Cs�d}ts|jjtjkrtd��|jstd��z|j�tjtj	t
�|��}Wntjy@}z	t
�t|�WYd}~dSd}~wwt
�||�\}}}|||fS)a�Return the PID/UID/GID tuple of the peer socket for UNIX sockets.

        This function uses SO_PEERCRED to query the UNIX PID, UID, GID
        of the peer, which is only available if the bind address is
        a UNIX domain socket.

        Raises:
            NotImplementedError: in case of unsupported socket type
            RuntimeError: in case of SO_PEERCRED lookup unsupported or disabled

        Z3iz5SO_PEERCRED is only supported in Linux kernel and WSLz0Peer creds lookup is disabled within this serverN)�
IS_WINDOWSr��family�AF_UNIX�NotImplementedErrorr�RuntimeError�
getsockopt�
SOL_SOCKETr#�struct�calcsizer�r��
raise_from�unpack)rBZPEERCRED_STRUCT_DEFZ
peer_credsZ
socket_err�pid�uid�gidrIrIrJr^s0������
zHTTPConnection.get_peer_credscCs|��\}}}|S)z,Return the id of the connected peer process.�r)rBr5rTrIrIrJ�peer_pid��zHTTPConnection.peer_pidcCs|��\}}}|S)z1Return the user id of the connected peer process.r8)rBrTr6rIrIrJ�peer_uid�r:zHTTPConnection.peer_uidcCs|��\}}}|S)z2Return the group id of the connected peer process.r8)rBrTr7rIrIrJ�peer_gid�r:zHTTPConnection.peer_gidcCs>tstd��|js
td��t�|j�j}t�	|j
�j}||fS)aLook up the username and group tuple of the ``PEERCREDS``.

        :returns: the username and group tuple of the ``PEERCREDS``

        :raises NotImplementedError: if the OS is unsupported
        :raises RuntimeError: if UID/GID lookup is unsupported or disabled
        z�UID/GID lookup is unavailable under current platform. It can only be done under UNIX-like OS but not under the Google App Enginez-UID/GID lookup is disabled within this server)�IS_UID_GID_RESOLVABLEr-rr.�pwd�getpwuidr;�pw_name�grp�getgrgidr<�gr_name)rB�user�grouprIrIrJr
�s��z!HTTPConnection.resolve_peer_credscCs|��\}}|S)z2Return the username of the connected peer process.�r
)rBrDrTrIrIrJ�	peer_user��zHTTPConnection.peer_usercCs|��\}}|S)z/Return the group of the connected peer process.rF)rBrTrErIrIrJ�
peer_group�rHzHTTPConnection.peer_groupc
Csnt|jd|jj�}z|tj�WdStjyYdStjy6}z|jtjvr+�WYd}~dSd}~ww)z0Terminate the connection at the transport level.Z
sock_shutdownN)	r�r��shutdown�	SHUT_RDWRr
Z#acceptable_sock_shutdown_exceptionsr��errnoZ$acceptable_sock_shutdown_error_codes)rBrJ�erIrIrJr)�s����z#HTTPConnection._close_kernel_socket) rPrQrRrSZremote_addrZremote_portZssl_env�io�DEFAULT_BUFFER_SIZErr	rrrrZ	last_usedrr\r#r'rrryr�propertyr9r;r<r
rGrIr)rIrIrIrJr�s<3.




rc@s�eZdZdZdZdZdZ	dZ	dZ	dZ		dZ
	dZ	dZ	dZ
	dZ	djed	�Z	dZ	d
Z	dZ	dZ	dZ	eZ	dZ	d
Z	d
Z	dZ			
d>dd�Zdd�Zdd�Zdd�Z e!dd��Z"e"j#dd��Z"dd�Z$dd�Z%dd�Z&dd �Z'e(j)d!d"��Z*e!d#d$��Z+d%d&�Z,d?d)d*�Z-d@d+d,�Z.d-d.�Z/e0d/d0��Z1e0d1d2��Z2e0d3d4��Z3d5d6�Z4e!d7d8��Z5e!d9d:��Z6e5j#d;d8��Z5d<d=�Z7dS)ArzAn HTTP server.z	127.0.0.1Nr�r��
g�?zCheroot/{version!s})�versionFrTrhcCsN||_||_tj||pd|d�|_|s|j}||_||_|o||_|�	�dS)a�Initialize HTTPServer instance.

        Args:
            bind_addr (tuple): network interface to listen to
            gateway (Gateway): gateway for processing HTTP requests
            minthreads (int): minimum number of threads for HTTP thread pool
            maxthreads (int): maximum number of threads for HTTP thread pool
            server_name (str): web server name to be advertised via Server
                HTTP header
        r)r��maxN)
�	bind_addrr�rZ
ThreadPool�requestsrRrrr�clear_stats)rBrTr��
minthreads�
maxthreadsrrrrIrIrJr\/s
��zHTTPServer.__init__cs�d�_d�_idd�d�fdd��d�fd	d��d
d�d�fdd��d
�fdd��d�fdd��d�fdd��dd�ddd��ddd��ddd��ddd��ddd��ddd��d i��_�jtjd!t��<dS)"zReset server stat counters..NrrFzBind Addresscs
t�j�SrN)r�rT��sr^rIrJ�<lambda>Ts
z(HTTPServer.clear_stats.<locals>.<lambda>zRun timecs|drdp
���S)Nrrh��runtimerYr^rIrJr[Us�AcceptszAccepts/seccs|d���S)Nr^r\rYr^rIrJr[Ws�Queuec�t�jdd�S)N�qsize�r�rUrYr^rIrJr[X�ZThreadscstt�jdg��S)NZ_threads)rbr�rUrYr^rIrJr[YszThreads Idlecr`)NZidlerbrYr^rIrJr[Zrcz
Socket Errors�RequestscS�*|drdptdd�|d��D�d�S)Nrrhcs��|]	}|d|�VqdS)rdNrI�r��wrIrIrJ�	<genexpr>]���;HTTPServer.clear_stats.<locals>.<lambda>.<locals>.<genexpr>�Worker Threadsr��sum�valuesrYrIrIrJr[\���
Bytes ReadcSre)Nrrhcsrf)rqNrIrgrIrIrJri`rjrkrlrrmrYrIrIrJr[_rp�
Bytes WrittencSre)Nrrhcsrf)rrNrIrgrIrIrJricrjrkrlrrmrYrIrIrJr[bs��	Work TimecSre)Nrrhcsrf)rsNrIrgrIrIrJrigrjrkrlrrmrYrIrIrJr[frpzRead ThroughputcSre)Nrrhcs�,�|]}|d|�|d|�pdVqdS)rqrs���ư>NrIrgrIrIrJrij�
��
�rkrlrrmrYrIrIrJr[i�
��zWrite ThroughputcSre)Nrrhcsrt)rrrsruNrIrgrIrIrJriprvrkrlrrmrYrIrIrJr[orwrlzCheroot HTTPServer %d)�_start_time�	_run_timerrr1�idr^rIr^rJrVNsH��������	�
�
�����#�%zHTTPServer.clear_statscCs$|jdur|jS|jt��|jS)zReturn server uptime.N)rxry�timer^rIrIrJr]ys
zHTTPServer.runtimecCsd|j|jj|jfS)z1Render Server instance representing bind address.z	%s.%s(%r))rQrXrPrTr^rIrIrJ�__str__�s
�zHTTPServer.__str__cC�|jS)a�Return the interface on which to listen for connections.

        For TCP sockets, a (host, port) tuple. Host values may be any
        :term:`IPv4` or :term:`IPv6` address, or any valid hostname.
        The string 'localhost' is a synonym for '127.0.0.1' (or '::1',
        if your hosts file prefers :term:`IPv6`).
        The string '0.0.0.0' is a special :term:`IPv4` entry meaning
        "any active interface" (INADDR_ANY), and '::' is the similar
        IN6ADDR_ANY for :term:`IPv6`.
        The empty string or :py:data:`None` are not allowed.

        For UNIX sockets, supply the file name as a string.

        Systemd socket activation is automatic and doesn't require tempering
        with this variable.

        .. glossary::

           :abbr:`IPv4 (Internet Protocol version 4)`
              Internet Protocol version 4

           :abbr:`IPv6 (Internet Protocol version 6)`
              Internet Protocol version 6
        )�
_bind_addrr^rIrIrJrT�szHTTPServer.bind_addrcCs(t|t�r|ddvrtd��||_dS)z5Set the interface on which to listen for connections.r)rNzzHost values of '' or None are not allowed. Use '0.0.0.0' (IPv4) or '::' (IPv6) instead to listen on all active interfaces.N)r�r�r5r~)rBr�rIrIrJrT�s
�
c	CsVz|��WdSttfy|�d�|���ty*|�d�|���w)z4Run the server forever, and stop it cleanly on exit.z!Keyboard Interrupt: shutting downz SystemExit raised: shutting downN)�startrr�r�stoprr^rIrIrJ�
safe_start�s

�zHTTPServer.safe_startcCs�d|_|jdurd|j|_d|_d}t�dd�r$t�dtjtj�|_n�t	|j
tjtj
f�r[z|�|j
�Wn�tjyZ}zd||j
|f}t�t�|�|�WYd}~nyd}~ww|j
\}}zt�||tjtjdtj�}Wn$tjy�tj}|j
}d|vr�tj}|d	}|tjdd
|fg}Ynw|D]8}|\}	}
}}}
z
|�|	|
|�Wn%tjy�}zd||
|f}|jr�|j��d|_WYd}~q�d}~ww|js�t�|��|j�d�|j�|j�t�|�|_|j��d|_ t!�!�|_"dS)
z�Prepare server to serving requests.

        It binds a socket's port, setups the socket to ``listen()`` and does
        other preparing things.
        Nz	%s ServerzNo socket could be createdZ
LISTEN_PIDr�z%s -- (%s: %s)r�:)rrrrT)#�
_interrupt�softwarerRr��os�getenv�fromfd�AF_INET�SOCK_STREAMr�rTr�r��binary_type�bind_unix_socketr�r3�getaddrinfo�	AF_UNSPEC�
AI_PASSIVE�gaierrorr�bindry�
settimeout�listen�request_queue_sizer	ZConnectionManager�_connectionsrUrr�r{rx)rBr�Zserr�hostr��infoZ	sock_typerTrl�af�socktyper�	canonname�sarIrIrJ�prepare�sb
��

��

��

zHTTPServer.preparec	Cs�|jr1|js1z	|j�|j�Wnttfy�ty*|jdt	j
dd�Ynw|jr1|jr|jrE|jr?t�
d�|js7|jrG|j�dSdS)z1Serve requests, after invoking :func:`prepare()`.zError in HTTPServer.serveTrg�������?N)r��	interruptr��run�expiration_intervalrrr rrr!�_stopping_for_interruptr{�sleepr^rIrIrJ�serves(
���

��zHTTPServer.servecCs|��|��dS)zmRun the server forever.

        It is shortcut for invoking :func:`prepare()` then :func:`serve()`.
        N)r�r�r^rIrIrJr#s	zHTTPServer.startccsJ�|��tj|jd�}|�d�|��z
|VW|��dS|��w)z4Context manager for running this server in a thread.)�targetTN)r��	threading�Threadr��	setDaemonrr�)rB�threadrIrIrJ�_run_in_thread/s�
zHTTPServer._run_in_threadcCs|jo|jjS)z>Flag whether it is allowed to add a new keep-alive connection.)r�r�r�r^rIrIrJr�;sz'HTTPServer.can_add_keepalive_connectioncCs"|jr|j�|�dS|��dS)z7Put an idle connection back into the ConnectionManager.N)r�r��putry�rBr�rIrIrJ�put_conn@szHTTPServer.put_connr�cCsHtj�dj|d��tj��|r"t��}tj�|�tj��dSdS)z�Write error message to log.

        Args:
            msg (str): error message
            level (int): logging level
            traceback (bool): add traceback to output or not
        z{msg!s}
)r�N)�sys�stderrr�r��flush�
traceback_�
format_exc)rBr�rrZtblinesrIrIrJrHs	
�zHTTPServer.error_logcCs>|�|j||||j|j�}|�||j�}|_|�|�|_|S)z.Create (or recreate) the actual socket object.)�prepare_socketrT�nodelayr��bind_socketr��resolve_real_bind_addr)rBr+�typerrrIrIrJr�Xs�zHTTPServer.bindc
Cs�tr	td��d}zt�|j�WnMtyYnEty<}zt|�}d|vr2d|vr2d|vr2�WYd}~n'd}~wty^}zt|�}d|vrTd|vrTd|vrT�WYd}~nd}~ww|j|t	j
t	jd	|j|j
d
�}z
	t�|��|�d}Wnty�d}Ynwz|�||�}Wn
t	jy�|���w|�|�}z!	|s�zt�||�Wnty�tj||dd
�Ynwd}Wn	ty�Ynw|s�|jdtjd�||_||_	|S)z*Create (or recreate) a UNIX socket object.z0AF_UNIX sockets are not supported under Windows.i�zJremove() argument 1 must be encoded string without null bytes, not unicodezembedded NUL characterz0argument must be a string without NUL charactersNz'unlink: embedded null character in pathzembedded null byter)rTr+r�rr�r�TF)�follow_symlinksz(Failed to set socket fs mode permissions)r)r*r5r��unlinkrT�OSError�	TypeErrorr�r�r�r,r�r�r��fchmod�filenor�r�ryr��lchmodr%�chmodrrr)rBrTZfs_permissionsZtyp_err�err_msgZval_errrZFS_PERMS_SETrIrIrJr�cs���������
���
����zHTTPServer.bind_unix_socketc	Cs�t�|||�}t�|�|dd�\}}|dk}	ts&|	s&	|�tjtjd�|r:t|tj	tj
f�s:|�tjtjd�|durC|�
|�}ttd�oP|tjkoP|dv}
|
rmz|�tjtjd�W|SttjfylY|Sw|S)z%Create and prepare the socket object.Nr�rrr)z::z::0z	::0.0.0.0)r�r	Zprevent_socket_inheritancer*�
setsockoptr0�SO_REUSEADDRr�r�r�r��IPPROTO_TCP�TCP_NODELAYr��hasattrrrr!r%r�)rTr+r�rr�r�rr�r�ZIS_EPHEMERAL_PORTZlistening_ipv6rIrIrJr��s<


�

��
���zHTTPServer.prepare_socketcCs|�|�|S)z#Bind the socket to given interface.)r��Zsocket_rTrIrIrJr��s
zHTTPServer.bind_socketcCs@|��}|jtjtjfvr	|dd�St|tj�rt|�}|S)z/Retrieve actual bind address from bound socket.Nr�)	�getsocknamer+r�r�rr�r�r�rr�rIrIrJr��s�z!HTTPServer.resolve_real_bind_addrcCs2z	|j�|�WdStjy|��YdSw)z#Process an incoming HTTPConnection.N)rUr�r�Fullryr�rIrIrJ�process_conns
�zHTTPServer.process_conncCr})zFlag interrupt of the server.)r�r^rIrIrJr�szHTTPServer.interruptcCs
|jtuS)z8Return whether the server is responding to an interrupt.)r��_STOPPING_FOR_INTERRUPTr^rIrIrJr�s
z"HTTPServer._stopping_for_interruptcCst|_|��||_dS)aPerform the shutdown of this server and save the exception.

        Typically invoked by a worker thread in
        :py:mod:`~cheroot.workers.threadpool`, the exception is raised
        from the thread running :py:meth:`serve` once :py:meth:`stop`
        has completed.
        N)r�r�r�)rBr�rIrIrJr�s	
c
Csj|jsdSd|_|jdur|jt��|j7_d|_|j��t|dd�}|r�t|jt	j
t	jf�s�z|��dd�\}}Wnt
jy[}z|jdtjvrQ�WYd}~nDd}~wwt
�||t
jt
j�D]4}|\}}}}	}
d}zt
�
|||�}|�d�|�||f�|��Wqft
jy�|r�|��Yqfwt|d�r�|��d|_
|j��|j�|j�dS)z5Gracefully shutdown a server that is serving forever.NFr�r�rg�?ry)r�rxryr{r�r�r�r�rTr�r�r�r�r�r�r�r
r�r�r�r�r��connectryr�rU�shutdown_timeout)rBrr�r�r�rlr�r�rr�r�rZrIrIrJr�%sT


�����
��

zHTTPServer.stop)rQrhNFF)rr�Fr�)8rPrQrRrSr~r�r�rWrXrr�r�r�r�r�r�rrRr�r�r�r�r�rZConnectionClassr�rrZkeep_alive_conn_limitr\rVr]r|rPrT�setterr�r�r�r�
contextlib�contextmanagerr�r�r�rr�r��staticmethodr�r�r�r�r�r�r�rIrIrIrJr�s�
�+

G



U
1




rc@s eZdZdZdd�Zdd�ZdS)rzDBase class to interface HTTPServer with other systems, such as WSGI.cCs
||_dS)zuInitialize Gateway instance with request.

        Args:
            req (HTTPRequest): current HTTP request
        N)r")rBr"rIrIrJr\as
zGateway.__init__cCst�)z>Process the current request. Must be overridden in a subclass.)r-r^rIrIrJr�ir~zGateway.respondN)rPrQrRrSr\r�rIrIrIrJr^srz%cheroot.ssl.builtin.BuiltinSSLAdapterz&cheroot.ssl.pyopenssl.pyOpenSSLAdapter)�builtinZ	pyopensslr�cCs�t|��}t|tj�rY|�d�}||dd�}|d|�}ztj|}|dur,t��Wnty@t	|t
�t�dg�}Ynwzt||�}W|St
yXt
d||f��w|S)z/Return an SSL adapter class for the given name.�.rNrz!'%s' object has no attribute '%s')�ssl_adaptersr�r�r�Zstring_types�rfindr��modules�KeyError�
__import__�globals�localsr�r%)�nameZadapterZlast_dotZ	attr_nameZmod_path�modrIrIrJrvs0

������r)r�)TrSZ
__future__rrrr�Z
__metaclass__r�rN�re�email.utilsrr�r�r{rr�r�platformr�r��	functoolsr�ImportErrorZbackports.functools_lru_cacher�Z	six.movesrrrr	r
rZ_compatrr
r�workersrrrr�__all__�systemr*r�r�ZIS_GAEr=rAr>r1r�rr!r#rjr6r9r8r<r�rkZASTERISKr�r��compilerAr��objectr�r?r1rrrrrrrrrr�rrIrIrIrJ�<module>s�?��



B^W@[
�