U
    
ˆj'  ã                   @   sˆ   d dl Z d dlZd dlZd dl mZmZ d dlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ e e¡Zd	Zd
ZG dd„ dƒZdS )é    N)ÚArgumentErrorÚ	Namespace)Úguarantee_single_callableé   )ÚAccessLogGenerator)Ú"build_endpoint_description_strings)ÚServer)Úimport_by_pathz	127.0.0.1i@  c                   @   s`   e Zd ZdZdZeZdd„ Zedd„ ƒZ	e
edœdd	„Zed
œdd„Zed
œdd„Zdd„ ZdS )ÚCommandLineInterfacezB
    Acts as the main CLI entry point for running the server.
    zDjango HTTP/WebSocket serverc                 C   sÒ  t j| jd�| _| jjddtdd d� | jjdddd	d d
� | jjdtddd� | jjdtddd� | jjddddd d
� | jjdtddd d� | jjdddddg d� | jjdd td!d"d� | jjd#d$td%d d� | jjd&d'd d(� | jjd)d*d+d(� | jjd,td-d.d� | jjd/td0d1d� | jjd2td3d4d� | jjd5d6d7d8d
� | jjd9d:d;d<d=d>� | jjd?d@dAd<dBd>�| _| jjdCdDdEd<dBd>�| _| jjdFdGdH� | jjdIdJdKdLdMd
� | jjdNdKdOd8dP� d | _d S )QN)Údescriptionz-pz--portzPort number to listen on)ÚtypeÚhelpÚdefaultz-bz--bindÚhostzThe host/address to bind to)Údestr   r   z--websocket_timeoutzCMaximum time to allow a websocket to be connected. -1 for infinite.i€Q z--websocket_connect_timeoutz@Maximum time to allow a connection to handshake. -1 for infiniteé   z-uz--unix-socketÚunix_socketz1Bind to a UNIX socket rather than a TCP host/portz--fdÚfile_descriptorzJBind to a file descriptor rather than a TCP host/port or named unix socket)r   r   r   r   z-ez
--endpointÚsocket_stringsÚappendz1Use raw server strings passed directly to twisted)r   Úactionr   r   z-vz--verbosityzHow verbose to make the outputr   z-tz--http-timeoutz>How long to wait for worker before timing out HTTP connectionsz--access-logzIWhere to write the access log (- for stdout, the default for verbosity=1))r   r   z	--log-fmtzLog format to usez*%(asctime)-15s %(levelname)-8s %(message)sz--ping-intervalzNThe number of seconds a WebSocket must be idle before a keepalive ping is senté   z--ping-timeoutzUThe number of seconds before a WebSocket is closed if no response to a keepalive pingé   z--application-close-timeoutzaThe number of seconds an ASGI application has to exit after client disconnect before it is killedé
   z--root-pathÚ	root_pathz+The setting for the ASGI root_path variableÚ z--proxy-headersÚproxy_headerszmEnable parsing and using of X-Forwarded-For and X-Forwarded-Port headers and using that as the client addressFÚ
store_true)r   r   r   r   z--proxy-headers-hostÚproxy_headers_hostzÌSpecify which header will be used for getting the host part. Can be omitted, requires --proxy-headers to be specified when passed. "X-Real-IP" (when passed by your webserver) is a good candidate for this.Ústorez--proxy-headers-portÚproxy_headers_portz‚Specify which header will be used for getting the port part. Can be omitted, requires --proxy-headers to be specified when passed.Úapplicationz>The application to dispatch to as path.to.module:instance.path)r   z-sz--server-nameÚserver_namezHspecify which value should be passed to response header Server attributeZdaphnez--no-server-nameÚstore_const)r   r   Úconst)	ÚargparseÚArgumentParserr   ÚparserÚadd_argumentÚintÚarg_proxy_hostÚarg_proxy_portÚserver)Úself© r.   ú./tmp/pip-unpacked-wheel-ibzxqxay/daphne/cli.pyÚ__init__   s       ÿûüüûûúûûýýüüüüúø
ù	þû   ÿzCommandLineInterface.__init__c                 C   s   | ƒ   tjdd… ¡ dS )z6
        Main entrypoint for external starts.
        r   N)ÚrunÚsysÚargv)Úclsr.   r.   r/   Ú
entrypoint¦   s    zCommandLineInterface.entrypoint©ÚargumentÚargsc                 C   s   |j r
dS t|dd�‚dS )z1Raise if the `--proxy-headers` weren't specified.Nz4--proxy-headers has to be passed for this parameter.)r7   Úmessage)r   r   )r-   r7   r8   r.   r.   r/   Ú_check_proxy_headers_passed­   s    þz0CommandLineInterface._check_proxy_headers_passed©r8   c                 C   s*   |j r| j| j|d� |j S |jr&dS dS )úm
        Return the default host header from which the remote hostname/ip
        will be extracted.
        r6   zX-Forwarded-ForN)r   r:   r*   r   ©r-   r8   r.   r.   r/   Ú_get_forwarded_host¶   s
    z(CommandLineInterface._get_forwarded_hostc                 C   s*   |j r| j| j|d� |j S |jr&dS dS )r<   r6   zX-Forwarded-PortN)r    r:   r+   r   r=   r.   r.   r/   Ú_get_forwarded_portÁ   s
    z(CommandLineInterface._get_forwarded_portc                 C   s¤  | j  |¡}tjtjtjtjtjdœ|j |jd� d}|j	r`|j	dkrPt
j}qpt|j	ddƒ}n|jdkrpt
j}t
j dd¡ t|jƒ}t|ƒ}t|j|jdk	|j|jdk	|jgƒsÂt|_t|_n.|jrÚ|jdkrÚt|_n|jdk	rð|jsðt|_t|j|j|j|jd	�}t|j| ƒ}t d
 d |¡¡¡ | j |||j!|j"|j#|j$|j%|j%|j&|�r^t'|ƒnd|j(|j| j)|d�| j*|d�|j+�rˆdnd|j,d�| _-| j- .¡  dS )z_
        Pass in raw argument list and it will decode them
        and run the server.
        )r   r   é   é   )ÚlevelÚformatNú-Úar   r   Ú.)r   Úportr   r   zStarting server at {}z, r;   zX-Forwarded-Proto)r!   Ú	endpointsÚhttp_timeoutÚping_intervalÚping_timeoutÚwebsocket_timeoutÚwebsocket_connect_timeoutZwebsocket_handshake_timeoutÚapplication_close_timeoutZaction_loggerr   Ú	verbosityZproxy_forwarded_address_headerZproxy_forwarded_port_headerZproxy_forwarded_proto_headerr"   )/r'   Ú
parse_argsÚloggingÚbasicConfigÚWARNÚINFOÚDEBUGrO   Zlog_fmtZ
access_logr2   ÚstdoutÚopenÚpathÚinsertr	   r!   r   Úanyr   rG   r   r   r   ÚDEFAULT_HOSTÚDEFAULT_PORTr   ÚsortedÚloggerÚinforC   ÚjoinÚserver_classrI   rJ   rK   rL   rM   rN   r   r   r>   r?   r   r"   r,   r1   )r-   r8   Zaccess_log_streamr!   rH   r.   r.   r/   r1   Ì   s|    üûù



ûÿ
ü

ìzCommandLineInterface.runN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ra   r0   Úclassmethodr5   Ústrr   r:   r>   r?   r1   r.   r.   r.   r/   r
      s    
	r
   )r%   rQ   r2   r   r   Zasgiref.compatibilityr   Úaccessr   rH   r   r,   r   Úutilsr	   Ú	getLoggerrb   r^   r[   r\   r
   r.   r.   r.   r/   Ú<module>   s   
