
    q&f0F                        d dl Z d dlZd dlZddlmZ ddlmZmZmZm	Z	m
Z
mZ ddlm	Z ddlmZmZ e	j"                  Ze	j$                  Ze	j&                  ZdZdZd	Zd
Ze
j0                  Zd Z ej4                  e
j6                         G d d             Zd Zd Zd Zd Z d Z!d Z"d Z# ej4                  e
j6                         G d d             Z$ddZ% G d d      Z&y)    N   )_)encodingerrorpycompatutilwireprototypeswireprotov1server)r   )compression
stringutil   s   application/mercurial-0.1s   application/mercurial-0.2s   application/hg-errorc                     g }d}	 | j                   j                  d||fz        }|n*|j                  t        j                  |             |dz  }Mdj                  |      S )zjDecode a long value from multiple HTTP request headers.

    Returns the value as a bytes, not a str.
    r   s   %s-%d    )headersgetappendr   bytesurljoin)reqheaderprefixchunksivs        ;/usr/lib/python3/dist-packages/mercurial/wireprotoserver.pydecodevaluefromheadersr   )   sk    
 F	A
KKOOHa'8899h''*+	Q  88Fr   c                   p    e Zd Zd Zed        Zd Zd Zd Zd Z	e
j                  d        Zd Zd	 Zd
 Zy)httpv1protocolhandlerc                 <    || _         || _        || _        d | _        y N)_req_ui
_checkperm
_protocaps)selfr   ui	checkperms       r   __init__zhttpv1protocolhandler.__init__<   s    	#r   c                      y)Ns   http-v1 r$   s    r   namezhttpv1protocolhandler.nameB   s    r   c                 
   | j                         }i }|j                         }|D ]E  }|dk(  r3i }|j                         D ]  }|dk7  s	||vs||   d   ||<    ||d<   ;||   d   ||<   G |D cg c]  }||   	 c}S c c}w )N   *   cmdr   )_argssplitkeys)r$   args	knownargsdatar1   kstarkeys           r   getargszhttpv1protocolhandler.getargsF   s    JJL	zz| 	*ADy$>>+ 6Cf}D$-cN1$5S	6 "T
#A,q/Q	* "&&AQ&&&s   1B c                    | j                   j                  j                         }t        | j                   j                  j                  dd            }|rK|j                  t        j                  | j                   j                  j                  |      d             |S t        | j                   d      }|j                  t        j                  |d             |S )N   X-HgArgs-Postr   T)keep_blank_valuess   X-HgArg)r    qsparamsasdictoflistsintr   r   updateurlreqparseqsbodyfhreadr   )r$   r2   postlenargvalues       r   r/   zhttpv1protocolhandler._argsU   s    yy!!//1dii''++,<a@AKKII$$))'2d  
 K)$))Z@FNN8tNDEr   c                     | j                   5t        | j                  d      }t        |j	                  d            | _         | j                   S )Ns	   X-HgProto    )r#   r   r    setr0   )r$   values     r   getprotocapsz"httpv1protocolhandler.getprotocapsd   s;    ??"*499lCE!%++d"34DOr   c                     t        | j                  j                  d         }|t        | j                  j                  j                  dd            z  }t	        j
                  | j                  j                  |      S )Ns   Content-Lengthr:   r   )limit)r>   r    r   r   r   filechunkiterrB   )r$   lengths     r   
getpayloadz httpv1protocolhandler.getpayloadj   sb    TYY&&'89: 	#dii''++,<a@AA!!$))"2"2&AAr   c              #   l  K   | j                   j                  }| j                   j                  }t        j                         }	 || j                   _        || j                   _        | || j                   _        || j                   _        y # || j                   _        || j                   _        w xY wwr   )r!   foutferrr   stringio)r$   oldoutolderrouts       r   mayberedirectstdioz(httpv1protocolhandler.mayberedirectstdios   sz     mmo	#DHHMDHHMI"DHHM"DHHM #DHHM"DHHMs   AB4&B *#B4$B11B4c                     d| j                   j                  t        j                  | j                   j                  xs d      t        j                  | j                   j
                  xs d      fz  S )Ns   remote:%s:%s:%sr   )r    	urlschemer@   quote
remotehost
remoteuserr*   s    r   clientzhttpv1protocolhandler.client   sW    !IILL--45LL--45%
 
 	
r   c                    |j                  d       |j                  d|j                  j                  dd      z         |j                  j                  dd      r|j                  d       |j                  d       t	        j
                  |j                  t        j                        }|r,dj                  d	 |D              }|j                  d
|z         |S )N   batchs   httpheader=%d   servers   maxhttpheaderlens   experimentals   httppostargss   httpmediatype=0.1rx,0.1tx,0.2tx   ,c              3   v   K   | ]1  }t         j                  |j                         j                         3 y wr   )r@   rZ   wireprotosupportr+   ).0es     r   	<genexpr>z8httpv1protocolhandler.addcapabilities.<locals>.<genexpr>   s-      "<=Q//1667"s   79s   compression=%s)	r   r%   	configint
configboolr	   supportedcompenginesr   
SERVERROLEr   )r$   repocapscompengines	comptypess        r   addcapabilitiesz%httpv1protocolhandler.addcapabilities   s    Htww00<OPP	
 77o?KK( 	67$99GG[++
 		 "AL" I KK)I56r   c                 $    | j                  |      S r   )r"   r$   perms     r   r&   zhttpv1protocolhandler.checkperm   s    t$$r   N)__name__
__module____qualname__r'   propertyr+   r8   r/   rJ   rO   
contextlibcontextmanagerrW   r]   ro   r&   r)   r   r   r   r   :   sY      'B # #
0%r   r   c                 &    | t         j                  v S r   )r
   commands)cmds    r   iscmdr|      s    #,,,,r   c                     ddl m}  j                  }dj                  vryj                  d   }t	        |      syj
                  r>|j                  d      |_        t        |j                  d<   |j                  ddz         y	t        |j                   fd
      }	 t        ||||       y	# |j                  $ r}|j                  D ]  \  }	}
|
|j                  |	<    |j                  |j                  t!        j"                  |            |_        |j                  dt!        j"                  |      z         Y d}~y	d}~ww xY w)a  Possibly process a wire protocol request.

    If the current request is a wire protocol request, the request is
    processed by this function.

    ``req`` is a ``parsedrequest`` instance.
    ``res`` is a ``wsgiresponse`` instance.

    Returns a bool indicating if the request was serviced. If set, the caller
    should stop processing the request, as a response has already been issued.
    r   commonr.   Fi     Content-Type   0
%s
s	   Not FoundTc                      |       S r   r)   )rr   r&   rctxr   s    r   <lambda>z#handlewsgirequest.<locals>.<lambda>   s    9T3#= r   N)hgwebr   rk   r<   r|   dispatchpathstatusmessagestatusHGTYPEr   setbodybytesr   r%   	_callhttpErrorResponsecoder   forcebytestr)r   r   resr&   hgwebcommonrk   r{   protore   r5   r   s   `` `       r   handlewsgirequestr      s:    -99D
 S\\!
,,v
C :  ..s3
'-O$ 	l23!TWW=E
B$S%-  $$ BII 	DAqCKKN	 ..FFJ++A.


 	j&=&=a&@@AABs   'B7 7EA>E		Ec                    d|j                         v r|rt        t        j                         i fS t	        j
                  |      }t        j                  | t        j                        D ]U  }|j                         j                  |v s i }| j                  dd|j                         z        }|||d<   t        ||fc S  d| j                  dd      i}t        t        j                  d   |fS )zDetermine the appropriate response type and compression settings.

    Returns a tuple of (mediatype, compengine, engineopts).
    s   0.2r`   s   %slevels   levels	   zliblevels   zlib)rJ   HGTYPE2r   _noopenginer
   clientcompressionsupportr	   ri   rj   rc   r+   rg   r   r   rm   )r%   r   prefer_uncompressedcompformatsengineoptslevels          r   _httpresponsetyper      s     ##%%K335r99 (@@G$99&&
 		-F &&(--<Y
V[[]0JK$%*DN,,		-" bll9l;<D4##G,d22r   c                 (   ddl m d }dfd	}t        j                  j	                  ||      s |t
        t        t        d             y |j                  t        j                  |   j                         j                  | |      }t        j                  | |||      }t        |t              r |t
        t        |       y t        |t        j                         r |t
        t        |j"                         y t        |t        j$                        r |t
        t        |j&                         y t        |t        j(                        rf|j&                  }	t+        | j,                  ||j.                        \  }
}}|j1                  |	|      }	|
t2        k(  r
 ||	||      }	 |t
        |
|	       y t        |t        j4                        r/d|j6                  |j8                  fz  } |t
        t        |       y t        |t        j:                        r*d	|j6                  z  }d
_         |t
        t        |       y t        |t        j>                        r |t
        t        |j@                         y tC        jD                  d|      )Nr   r~   c              3      K   |j                         j                  }dt        |      cxk  rdk  sJ  J t        j                  dt        |             | | D ]  }|  y w)Nr         B)rc   r+   lenstructpack)genr   
engineoptsr+   chunks        r   genversion2z_callhttp.<locals>.genversion2  sg      &&(--3t9"s"""""kk$D	**
 	EK	s   A%A'c                     | t         k(  rd_        nj                  |       _        |j                  d<   |j	                  |       |j                  |       y y )Ns   200 Script output followsr   )HTTP_OKr   r   r   r   
setbodygen)r   contenttype	bodybytesbodygenr   r   s       r   setresponsez_callhttp.<locals>.setresponse&  s\    7?5CJ$2248CJ'2O$ Y'NN7# r   s:   requested wire protocol command is not available over HTTP)r   )r   s   %d
%sr   Ts   hgweb.protocol internal failure)NN)#r   r   r
   rz   commandavailabler   	HGERRTYPEr   r&   
permissionhashiddenaccessdispatch
isinstancebytesr   r	   bytesresponser4   streamreslegacyr   	streamresr   r%   r   compressstreamr   pushresr   outputpusherrdrainooberrormessager   ProgrammingError)rk   r   r   r   r{   r   r   accesshiddenrspr   	mediatyper   r   r   s     `          @r   r   r     s   ,	$ %%66sEB	
 		OO%..s3>>?..tS9L

$
$T5#|
DC#uGVs3	C55	6GVsxx8	C77	8GVSWW5	C11	2gg ):GGUC33)
%	6: ##C4c6:6CGY4	C//	0377CJJ//GVs3	C//	0377"	GVs3	C00	1GY#++>$$%GMMr   c                     | j                  dt        |      z         | j                  |       | j                          y)z-Send a bytes response for protocol version 1.s   %d
N)writer   flush)rQ   rI   s     r   _sshv1respondbytesr   d  s,    JJwU#$JJuJJLr   c                 n    | j                   }|j                  D ]
  } ||        | j                          y r   )r   r   r   )rQ   sourcer   r   s       r   _sshv1respondstreamr   k  s.    JJE eJJLr   c                     |j                  d|z         |j                          | j                  d       | j                          y )Ns   %s
-
   
)r   r   )rQ   rR   r   s      r   _sshv1respondooberrorr   r  s1    JJzC JJLJJuJJLr   c                   n    e Zd ZdZd Zed        Zd Zd Zd Z	e
j                  d        Zd Zd	 Zd
 Zy)sshv1protocolhandlerz<Handler for requests services via version 1 of SSH protocol.c                 L    || _         || _        || _        t               | _        y r   )r!   _fin_foutrH   r#   )r$   r%   finrQ   s       r   r'   zsshv1protocolhandler.__init__}  s     	
%r   c                 "    t         j                  S r   )r	   SSHV1r*   s    r   r+   zsshv1protocolhandler.name  s    ###r   c                    i }|j                         }t        t        |            D ]   }| j                  j	                         d d }|j                         \  }}||vr!t        j                  t        d      |z        |dk(  rzi }t        t        |            D ][  }	| j                  j	                         d d }|j                         \  }}| j                  j                  t        |            }
|
||<   ] ||d<   | j                  j                  t        |            }
|
||<    |D 	cg c]  }	||	   	 c}	S c c}	w )Ns   unexpected parameter %rr-   )
r0   ranger   r   readliner   Abortr   r>   rC   )r$   r2   r4   r1   narglinearglr6   r5   vals              r   r8   zsshv1protocolhandler.getargs  s!   zz|s4y! 	 Aii((*3B/G]]_FC$kk!$>"?#"EFFd{s1v $A"ii0023B7G$]]_FC))..Q0C #DI	$
 "T
iinnSV,S		   "&&AQ&&&s   /D>c                     | j                   S r   )r#   r*   s    r   rJ   z!sshv1protocolhandler.getprotocaps  s    r   c              #   
  K   t        | j                  d       t        | j                  j	                               }|rD| j                  j                  |       t        | j                  j	                               }|rCy y w)Nr   )r   r   r>   r   r   rC   )r$   counts     r   rO   zsshv1protocolhandler.getpayload  sa      	4::s+ DII&&()))..''		**,-E s   A>BBc              #      K   d  y wr   r)   r*   s    r   rW   z'sshv1protocolhandler.mayberedirectstdio  s     
s   c                 r    t         j                  j                  dd      j                  dd      d   }d|z   S )Ns
   SSH_CLIENTr   rG   r   r   s   remote:ssh:)r   environr   r0   )r$   r]   s     r   r]   zsshv1protocolhandler.client  s7    !!%%mS9??aHK&&r   c                     | j                   t        j                  k(  r|j                  d       |j                  d       |S )Ns	   protocapsr_   )r+   r	   r   r   )r$   rk   rl   s      r   ro   z$sshv1protocolhandler.addcapabilities  s1    99,,,KK%Hr   c                      y r   r)   rq   s     r   r&   zsshv1protocolhandler.checkperm  s    r   N)rs   rt   ru   __doc__r'   rv   r+   r8   rJ   rO   rw   rx   rW   r]   ro   r&   r)   r   r   r   r   y  sV    F  $ $'*.   'r   r   c                    d}t        | ||      }|j                         sC|dk(  r|j                         d d }|sd}/t        j                  j                  ||      }	|	st        |d       ^t        j                  ||||      }
|j                  j                  j                          |j                  j                  j                          t        |
t              rt        ||
       ndt        |
t        j                        rt        ||
j                          n2t        |
t        j"                        rt%        ||
       n
t        |
t        j&                        rt%        ||
       nt        |
t        j(                        r&t        |d       t        |d|
j*                  z         nt        |
t        j,                        rt        ||
j*                         nrt        |
t        j.                        r"t1        || j                  |
j2                         n6t5        j6                  d|
z        |dk(  ry t5        j6                  d|z        |j                         sBy y )	Ns   protov1-servingr   s   shutdownr   )r   s   %ds6   unhandled response type from wire protocol command: %ss   unhandled ssh server state: %s)r   is_setr   r
   rz   r   r   r   r%   rQ   r   rR   r   r   r	   r   r4   r   r   r   r   r   r   r   r   r   r   r   )r%   rk   r   rQ   evr   stater   request	availabler   s              r   _runsshserverr     s   $ E S$/Eiik&&llnSb)G #)22CCI "4-#,,eW<C GGLL GGLL #u%"4-C!=!=>"42C!9!9:#D#.C!?!?@#D#.C!7!78"4-"49C!7!78"41C!8!89%dBGGS[[A,,1367 
 k! ((1E9 g iikkr   c                        e Zd ZddZd Zd Zy)	sshserverNc                    || _         || _        |j                         \  | _        | _        || _        |rQt        j                  || j                  dd      | _        t        j                  ||j                  dd      |_        y y )N   oT)logdata   e)	r!   _repoprotectfinoutr   r   _accesshiddenr   makeloggingfileobjectrR   )r$   r%   rk   logfhr   s        r   r'   zsshserver.__init__  sy    
 " 0 0 2	4:) 33tzz4DJ 00rwwdBG	 r   c                     | j                  t        j                                | j                  j	                  | j
                  | j                         y r   )
serveuntil	threadingEventr!   restorefinoutr   r   r*   s    r   serve_foreverzsshserver.serve_forever  s1    	)*tyy$**5r   c                     t        | j                  | j                  | j                  | j                  || j
                         y)z%Serve until a threading.Event is set.N)r   r!   r   r   r   r   )r$   r   s     r   r   zsshserver.serveuntil"  s-    HHdjj$))TZZT=O=O	
r   )NF)rs   rt   ru   r'   r   r   r)   r   r   r   r     s    6
r   r   )F)'rw   r   r   i18nr    r   r   r   r   r	   r
   
interfacesinterfaceutilutilsr   r   rS   urlerrr@   r   r   r   r   r   r   implementerbaseprotocolhandlerr   r|   r   r   r   r   r   r   r   r   r   r)   r   r   <module>r
     s        .
 ==		
	%
&#	" >==>g% g% ?g%\-CL!3HJNZ >==>D D ?DNJZ
 
r   