U
    BfJ$                     @   s   d dl Z d dlZd dlZd dlZd dlZd dl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mZmZmZmZmZmZmZmZmZ d dlmZmZ d dlmZ d d	lmZ G d
d deZ dS )    N)datetime)randint)SSLError)util)
ForbiddenProxyRequestInvalidHeaderInvalidHeaderNameInvalidHTTPVersionInvalidProxyLineInvalidRequestLineInvalidRequestMethodInvalidSchemeHeadersLimitRequestHeadersLimitRequestLine)Responsedefault_environ)reloader_engines)	WorkerTmpc                   @   s   e Zd Zdd d D Zg 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dd Zdd Zdd Zdd Zdd ZdS )Workerc                 C   s   g | ]}t td | qS )zSIG%s)getattrsignal).0x r   9/tmp/pip-unpacked-wheel-s8855r93/gunicorn/workers/base.py
<listcomp>   s     zWorker.<listcomp>z+ABRT HUP QUIT INT TERM USR1 USR2 WINCH CHLDc           	      C   s   || _ d| _|| _|| _|| _|| _|| _d| _d| _d| _	d| _
|jdkrftd|j}|j| | _ntj| _d| _|| _t|| _dS )z        This is called pre-fork so it shouldn't do anything to the
        current process. If there's a need to make process wide
        changes you'll want to do that in ``self.init_process()``.
        z	[booting]FNr   T)agepidppidsocketsapptimeoutcfgbootedZabortedreloadernrZmax_requestsr   Zmax_requests_jittersysmaxsizealivelogr   tmp)	selfr   r   r   r    r!   r"   r)   jitterr   r   r   __init__%   s$    
zWorker.__init__c                 C   s
   d| j  S )Nz<Worker %s>)r   r+   r   r   r   __str__B   s    zWorker.__str__c                 C   s   | j   dS )z        Your worker subclass must arrange to have this method called
        once every ``self.timeout`` seconds. If you fail in accomplishing
        this task, the master process will murder your workers.
        N)r*   notifyr.   r   r   r   r0   E   s    zWorker.notifyc                 C   s
   t  dS )z        This is the mainloop of a worker process. You should override
        this method in a subclass to provide the intended behaviour
        for your particular evil schemes.
        N)NotImplementedErrorr.   r   r   r   runM   s    z
Worker.runc                    s,   j jr( j j D ]\}}|tj|< qtj j j j j j j	d t
  t  _ jD ]}t| t| q\ jD ]}t| q|t j   j jd g  _ j      j jr fdd}t j j }| j j|d _    jr j   j   d _   dS )z        If you override this method in a subclass, the last statement
        in the function should be to call this method with
        super().init_process() so that the ``run()`` loop is initiated.
        )
initgroupsr   c                    sJ    j d|  d _t jd d  j  t	d t
d d S )NzWorker reloading: %s modifiedF      1皙?r   )r)   infor(   oswritePIPEr"   
worker_inttimesleepr&   exit)fnamer.   r   r   changedz   s    
z$Worker.init_process.<locals>.changed)Zextra_filescallbackTN)r"   envitemsr8   environr   Zset_owner_processuidgidr3   seedpiper:   Zset_non_blockingZclose_on_execr   r*   filenoZwait_fdsr)   init_signalsreloadr   Zreload_engineZreload_extra_filesr$   	load_wsgistartZpost_worker_initr#   r2   )r+   kvpsr@   Zreloader_clsr   r.   r   init_processU   s:    





zWorker.init_processc              
   C   s   z| j  | _W n tk
r } zf| jjs, | j| zFt \}}}| j	
|j t }tj||d t| | _W 5 ~X W 5 d }~X Y nX d S )N)file)r    ZwsgiSyntaxErrorr"   rK   r)   	exceptionr&   exc_infor$   Zadd_extra_filefilenameioStringIO	tracebackprint_tbr   Zmake_fail_appgetvalue)r+   eexc_tb_exc_valZ	tb_stringr   r   r   rL      s    zWorker.load_wsgic                 C   s   | j D ]}t|tj qttj| j ttj| j ttj| j ttj| j	 ttj
| j ttj| j ttjd ttj
d ttdrt| jd  d S )NFset_wakeup_fdr4   )SIGNALSr   SIG_DFLSIGQUIThandle_quitSIGTERMhandle_exitSIGINTSIGWINCHhandle_winchSIGUSR1handle_usr1SIGABRThandle_abortsiginterrupthasattrra   r:   )r+   rQ   r   r   r   rJ      s    

zWorker.init_signalsc                 C   s   | j   d S )N)r)   Zreopen_filesr+   sigframer   r   r   rl      s    zWorker.handle_usr1c                 C   s
   d| _ d S )NF)r(   rq   r   r   r   rg      s    zWorker.handle_exitc                 C   s*   d| _ | j|  td td d S )NFr6   r   )r(   r"   r;   r<   r=   r&   r>   rq   r   r   r   re      s    
zWorker.handle_quitc                 C   s    d| _ | j|  td d S )NFr4   )r(   r"   Zworker_abortr&   r>   rq   r   r   r   rn      s    zWorker.handle_abortc                 C   sh  t  }|pd}t|ttttttt	t
tttfr~d}d}t|trTdt| }nt|trldt| }nt|trdt| }nt|ttfrdt| }|st|dr|j}nt|trdt| }nt|t	rd	}d
t| }d}nnt|t
rdt| }nTt|tr d}d}d}n:t|tr:dt| }n t|trZd}dt| }d}d}	| j|	j|d t|d n6t|dr| jd|j n| jd d}d}d}|d k	r,t  | }
t||| j}|d |d< t|d |d< t||| j}d||f |_t||_| j||||
 zt|||| W n" tk
rb   | j d Y nX d S )N) i  zBad RequestzInvalid Request Line '%s'zInvalid Method '%s'zInvalid HTTP Version '%s'z%sreqzRequest Header Fields Too LargezError parsing headers: '%s'i  z'%s'	ForbiddenzRequest forbiddeni  z%Invalid request from ip={ip}: {error}r   )iperrorurizError handling request %sz$Error handling request (no URI read)i  zInternal Server Errorrt   ZREMOTE_ADDRr4   ZREMOTE_PORTz%s %szFailed to send error message.)!r   now
isinstancer   r   r	   r   r   r   r   r
   r   r   r   strrp   rv   r)   warningformatrU   rz   r   r"   r   statuslenZresponse_lengthaccessr   Zwrite_error	Exceptiondebug)r+   rv   clientaddrexcZrequest_startZ
status_intreasonZmesgmsgZrequest_timerD   respr   r   r   handle_error   s         	




 

zWorker.handle_errorc                 C   s   | j d d S )Nzworker: SIGWINCH ignored.)r)   r   )r+   rr   r?   r   r   r   rj     s    zWorker.handle_winchN)__name__
__module____qualname__splitrb   r:   r-   r/   r0   r2   rR   rL   rJ   rl   rg   re   rn   r   rj   r   r   r   r   r      s"   ;Fr   )!rX   r8   r   r&   r<   rZ   r   randomr   sslr   Zgunicornr   Zgunicorn.http.errorsr   r   r   r	   r
   r   r   r   r   r   Zgunicorn.http.wsgir   r   Zgunicorn.reloaderr   Zgunicorn.workers.workertmpr   objectr   r   r   r   r   <module>   s   0