
    h                     z   d dl mZmZ d dlZd dlZd dlZd dlZd dlZd dlZ	 ej                  j                  d      r e       dZd dlZddlm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 ddlmZm Z  ddl!m"Z" ddlm#Z#m$Z$m%Z% erddl&m'Z'  ejP                  d      Z) G d de*      Z+y#  dZY pxY w)    )print_functionunicode_literalsNPRTY_NO_TLSTF   )util)TYPE_CHECKING	EnvParams)AuthSrv)HttpCli)Ico)HAVE_FFMPEG)ThumbCli)HAVE_PIL	HAVE_VIPS)U2idx)HMaccasNetMapshut_socket)HttpSrvs   [A-Z]{3}[A-Z ]c                   >    e Zd ZdZd Zd Zd
dZddZd Zd Z	d	 Z
y)HttpConnz
    spawned by HttpSrv to handle an incoming client connection,
    creates an HttpCli for each request (Connection: Keep-Alive)
    c                 \   || _         d | _        d | _        || _        || _        |j
                  | _        |j                  | _        | j                  j                  | _        |j                  | _        |j                  | _	        |j                  | _
        |j                  | _        |j                  | _        |j                  | _        |j                  | _        |j                  | _        |j                   j"                  | _        |j$                  | _        |j&                  | _        t(        xs t*        xs t,        xr | j                  j.                   }|rt1        |      nd | _        t5        | j                        | _        t9        j8                         | _        d| _        d| _        d| _         d| _!        d | _"        |jF                  | _$        d| _%        | j                  jL                  r)tO        jP                  | j                  jL                        nd | _&        | jS                          y )Ng        Fr   httpconn)*ssrcliaddrhsrvu2mutexargsEasrvu2fhpipesipu_iuipu_nmipa_nmxff_nmxff_lanbrokeriphashbansacloser   r   r   no_thumbr   thumbclir   icotimet0freshen_pwdstoppingnreqnbyteu2idxloglog_funclog_srclf_urlrecompile
set_rproxy)selfsckr   r   enths        G/jellyfin/media/venv/lib/python3.12/site-packages/copyparty/httpconn.py__init__zHttpConn.__init__,   s    		YY	))++YY	YY	jj
{{{{{{{{))ii	4I4Pdii>P>P:P+/$T		N99;	

",0II,<,<BJJtyy''($ 	 	    c                 d    d| _         	 t        | j                  | j                  d       y #  Y y xY w)NTr   )r5   r   r9   r   r@   s    rC   shutdownzHttpConn.shutdownU   s,    	$&&!,	s   !+ /Nc                     |d}| j                   d   }d | _        n	d}|| _        || _        d||| j                   d   fz  j                  d      | _        | j                  S )N$   r   "   z
%s [%dm%dr      )r   rproxyipljustr;   )r@   rN   colors      rC   r?   zHttpConn.set_rproxy\   sc    :E1BDKEDK'2udiil*CCJJ2N||rE   c                 >    | j                  | j                  ||       y N)r:   r;   )r@   msgcs      rC   r9   zHttpConn.logi   s    dllC+rE   c                     | j                   s3| j                  j                  t        | j                              | _         | j                   S rR   )r8   r   	get_u2idxstrr   rG   s    rC   rV   zHttpConn.get_u2idxl   s3     zz,,S^<DJzzrE   c                    	 | j                   j                  dt        j                        }t        |      dk7  r[dj                  t        |            }|r| j                  |       | j                   j                  d|j!                  d      z          y| xs t#        t$        j'                  |             S # t        j                  $ r Y yt
        $ r | j                   j                  d      }t        j                  | j                   | j                        | _	        || j                  _
        t        | j                   dd       s%| j                   j                  | j                   _        Y Dw xY w)N   Fsendallz1need at least 4 bytes in the first packet; got {}s   HTTP/1.1 400 Bad Request

zutf-8)r   recvsocketMSG_PEEKtimeoutAttributeErrorUtilUnrecvr9   r   bufgetattrsendrZ   lenformatencodeboolPTN_HTTPmatch)r@   methoderrs      rC   _detect_httpszHttpConn._detect_httpsu   s   	-VV[[FOO4F v;!ELLFC FFKK;cjj>QQRz=hnnV&<!===/ ~~ 	 		-VV[[^Fkk$&&$((3DG DGGK 4669d3!%		-s   *B9 9E1BE10E1c           	      r	   | j                   j                  d       d | _        | j                  j                  rd}n)| j                  j
                  rd}n| j                         }|r| j                  r| j                  dd       y | j                  j                  dd      | _        	 t        j                  t        j                  j                        }|j                  | j                  j                         | j                  j                   rS|xj"                  | j                  j$                   z  c_        |xj"                  | j                  j&                  z  c_        | j                  j(                  r	 | j                  j(                  |_        | j                  j,                  r%|j/                  | j                  j,                         |j1                  | j                   d
      | _         t3        g d| j                   j5                               D cg c]  \  }}d||fz   }}}| j                  dj7                  |      dz          | j                  j8                  rt;        | j                   d      r| j                   j=                         }|D cg c]  }t?        |d d d          }}| j                  ddj7                  |      z          d| j                   jA                         gd| j                   jC                         gd| j                   jE                         gfD ]*  \  }	}
| j                  djG                  |	|
xs d             , | j                  s/tK        jL                  | j                   | j                        | _        | jN                  s| xjP                  dz  c_(        tS        |       | _*        | jT                  jW                         sy | jX                  r@| jZ                  j]                  t?        | j^                        | jX                         d | _,        | j`                  r| jc                          | jN                  sy y #  | j                  d	       Y xY wc c}}w c c}w # tH        $ r0}t?        |      }d|v rn| j                  d|z   d       Y d }~y d }~ww xY w)N
   TFzTODO: cannot do https in jythonz1;31)rT   z[36mz[35mz(keylog failed; openssl or python too old)server_side)r      r   z
[1;3%dm%s z[0mshared_ciphersr   zTLS cipher overlap:
compressionz
ALPN protoz	NPN protoz
TLS {}: {}nahALERT_CERTIFICATE_UNKNOWNzhandshake[0m rq   r   )2r   
settimeoutr   r!   
https_only	http_onlyrm   r9   r;   replacesslcreate_default_contextPurposeCLIENT_AUTHload_cert_chaincertssl_veroptionsssl_flags_enssl_flags_dessl_logkeylog_filenameciphersset_cipherswrap_socketzipcipherjoinssl_dbghasattrrs   rW   ru   selected_alpn_protocolselected_npn_protocolrf   	Exceptionr`   ra   r5   r6   r   r   runr8   r   	put_u2idxr   rM   r?   )r@   is_httpsctxrT   r   rS   r   yoverlapkvexems                rC   r   zHttpConn.run   s   "99HYY  H ))+Hww:fE<<//?DL-001H1HI##DIINN399$$KKDII$:$:#::KKK499#9#99K 99$$M.2ii.?.?+ 99$$OODII$5$56TB !$Itvv}} ?1 $q!f,  #2399$$9I)J"ff335G5<=s1TrT7|=G=HH2TYYw5GGH&(:(:(<=%tvv'D'D'FG$dff&B&B&DE! E1
 !4!4Q
U!CDE& wwkk$&&$((3DG--IINIt}DH88<<>zz		##C		NDJJ?!
{{! --OM!KL >  
W."4 HH025H;
sL   *CQ= <Q BQ= Q2.A0Q= Q86B%Q= Q/,Q= =	R6&R11R6rR   )r   )__name__
__module____qualname____doc__rD   rH   r?   r9   rV   rm   r    rE   rC   r   r   &   s,    
'R,>8O"rE   r   ),
__future__r   r   argparseosr=   r\   	threadingr2   environgetr   HAVE_SSLr|    r   r`   rD   r   r	   authsrvr
   httpclir   r1   r   mtagr   th_clir   th_srvr   r   r8   r   r   r   r   httpsrvr   r>   ri   objectr   r   rE   rC   <module>r      s    7  	 	   	zz~~m$kH  .      '  . .  2::()z"v z"+Hs   (B4 4B: